Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-4520 improve sorting performance #4521

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
public interface BNode extends Resource {

@Override
default Type getValueType() {
return Type.BNODE;
}

@Override
default boolean isBNode() {
return true;
Expand Down
5 changes: 5 additions & 0 deletions core/model-api/src/main/java/org/eclipse/rdf4j/model/IRI.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
*/
public interface IRI extends Resource {

@Override
default Type getValueType() {
return Type.IRI;
}

@Override
default boolean isIRI() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
*/
public interface Literal extends Value {

@Override
default Type getValueType() {
return Type.LITERAL;
}

@Override
default boolean isLiteral() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public interface Resource extends Value {

// Empty place holder as common supertype of IRI and BNode

default Type getValueType(){
if(isIRI()) return Type.IRI;
if(isBNode()) return Type.BNODE;
return Type.TRIPLE;
}

@Override
default boolean isResource() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
@Experimental
public interface Triple extends Resource {

@Override
default Type getValueType() {
return Type.TRIPLE;
}

@Override
default boolean isTriple() {
return true;
Expand Down
14 changes: 14 additions & 0 deletions core/model-api/src/main/java/org/eclipse/rdf4j/model/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
*/
public interface Value extends Serializable {

public enum Type {
BNODE,
IRI,
LITERAL,
TRIPLE
}

default Type getValueType(){
if(isIRI()) return Type.IRI;
if(isLiteral()) return Type.LITERAL;
if(isBNode()) return Type.BNODE;
return Type.TRIPLE;
}

/**
* Check if the object is an instance of the given type. Typically 2x than using instanceof.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ public TemporalAmount temporalAmountValue() throws DateTimeException {

@Override
public XMLGregorianCalendar calendarValue() {
return value(CalendarLiteral::parseCalendar);
XMLGregorianCalendar xmlGregorianCalendar = CalendarLiteral.parseCalendar(getLabel());
if (xmlGregorianCalendar == null) {
throw new IllegalArgumentException("malformed value");
}
return xmlGregorianCalendar;
}

@Override
Expand Down Expand Up @@ -207,6 +211,7 @@ static class TypedLiteral extends AbstractLiteral {
private final String label;
private final CoreDatatype coreDatatype;
private final IRI datatype;
transient private XMLGregorianCalendar cachedCalendarValue;

TypedLiteral(String label) {
this.label = label;
Expand Down Expand Up @@ -260,6 +265,16 @@ public IRI getDatatype() {
public CoreDatatype getCoreDatatype() {
return coreDatatype;
}

@Override
public XMLGregorianCalendar calendarValue() {
XMLGregorianCalendar localCalendar = cachedCalendarValue;
if (localCalendar == null) {
localCalendar = super.calendarValue();
cachedCalendarValue = localCalendar;
}
return localCalendar;
}
}

static class TaggedLiteral extends AbstractLiteral {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ public interface CoreDatatype {

CoreDatatype NONE = DefaultDatatype.NONE;

static int compare(CoreDatatype left, CoreDatatype right) {
if (left.getClass() == right.getClass()) {
return Integer.compare(left.ordinal(), right.ordinal());
}

int compare;

if (left.isXSDDatatype()) {
if (right.isRDFDatatype() || right.isGEODatatype()) {
compare = 1;
} else {
compare = 0;
}
} else if (left.isRDFDatatype()) {
if (right.isXSDDatatype()) {
compare = -1;
} else if (right.isGEODatatype()) {
compare = 1;
} else {
compare = 0;
}
} else if (left.isGEODatatype()) {
if (right.isXSDDatatype() || right.isRDFDatatype()) {
compare = -1;
} else {
compare = 0;
}
} else {
compare = 0;
}

return compare;
}

int ordinal();

/**
* Checks whether the supplied datatype is an XML Schema Datatype.
*
Expand All @@ -42,6 +78,10 @@ default Optional<XSD> asXSDDatatype() {
return Optional.empty();
}

default XSD asXSDDatatypeOrNull() {
return null;
}

default Optional<RDF> asRDFDatatype() {
return Optional.empty();
}
Expand Down Expand Up @@ -266,6 +306,11 @@ public String toString() {
return iri.toString();
}

@Override
public final XSD asXSDDatatypeOrNull() {
return this;
}

}

enum RDF implements CoreDatatype {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public IRI getDatatype() {
public Optional<XSD.Datatype> getXsdDatatype() {
CoreDatatype coreDatatype = getCoreDatatype();

return org.eclipse.rdf4j.model.vocabulary.XSD.Datatype.from(coreDatatype.asXSDDatatype().orElse(null));
return org.eclipse.rdf4j.model.vocabulary.XSD.Datatype.from(coreDatatype.asXSDDatatypeOrNull());
}

// Overrides Object.equals(Object), implements Literal.equals(Object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

public class LeftJoinIterator extends LookAheadIteration<BindingSet, QueryEvaluationException> {

private static final EmptyIteration<BindingSet, QueryEvaluationException> EMPTY_ITERATION = new EmptyIteration<>();

/*-----------*
* Variables *
*-----------*/
Expand Down Expand Up @@ -62,7 +64,7 @@ public LeftJoinIterator(EvaluationStrategy strategy, LeftJoin join, BindingSet b
leftIter = strategy.evaluate(join.getLeftArg(), bindings);

// Initialize with empty iteration so that var is never null
rightIter = new EmptyIteration<>();
rightIter = EMPTY_ITERATION;

prepareRightArg = strategy.precompile(join.getRightArg(), context);
join.setAlgorithm(this);
Expand All @@ -82,7 +84,7 @@ public LeftJoinIterator(QueryEvaluationStep left, QueryEvaluationStep right, Que
leftIter = left.evaluate(bindings);

// Initialize with empty iteration so that var is never null
rightIter = new EmptyIteration<>();
rightIter = EMPTY_ITERATION;

prepareRightArg = right;
this.joinCondition = joinCondition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public class OrderComparator implements Comparator<BindingSet> {

private final static Logger logger = LoggerFactory.getLogger(OrderComparator.class);

private final ValueComparator cmp;
private final Comparator<Value> cmp;

private final Comparator<BindingSet> bindingContentsComparator;

public OrderComparator(EvaluationStrategy strategy, Order order, ValueComparator cmp,
public OrderComparator(EvaluationStrategy strategy, Order order, Comparator<Value> cmp,
QueryEvaluationContext context) {
this.cmp = cmp;
this.bindingContentsComparator = precompileComparator(strategy, order, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static boolean getEffectiveBooleanValue(Value value) throws ValueExprEval
if (value.isLiteral()) {
Literal literal = (Literal) value;
String label = literal.getLabel();
CoreDatatype.XSD datatype = literal.getCoreDatatype().asXSDDatatype().orElse(null);
CoreDatatype datatype = literal.getCoreDatatype();

if (datatype == CoreDatatype.XSD.STRING) {
return label.length() > 0;
Expand All @@ -79,16 +79,17 @@ public static boolean getEffectiveBooleanValue(Value value) throws ValueExprEval
} catch (IllegalArgumentException e) {
return false;
}
} else if (datatype != null && datatype.isIntegerDatatype()) {
} else if (datatype instanceof CoreDatatype.XSD && ((CoreDatatype.XSD) datatype).isIntegerDatatype()) {
try {
String normInt = XMLDatatypeUtil.normalize(label, datatype);
String normInt = XMLDatatypeUtil.normalize(label, ((CoreDatatype.XSD) datatype));
return !normInt.equals("0");
} catch (IllegalArgumentException e) {
return false;
}
} else if (datatype != null && datatype.isFloatingPointDatatype()) {
} else if (datatype instanceof CoreDatatype.XSD
&& ((CoreDatatype.XSD) datatype).isFloatingPointDatatype()) {
try {
String normFP = XMLDatatypeUtil.normalize(label, datatype);
String normFP = XMLDatatypeUtil.normalize(label, ((CoreDatatype.XSD) datatype));
return !normFP.equals("0.0E0") && !normFP.equals("NaN");
} catch (IllegalArgumentException e) {
return false;
Expand Down Expand Up @@ -162,8 +163,8 @@ public static boolean compareLiterals(Literal leftLit, Literal rightLit, Compare
// - CoreDatatype.XSD:string
// - RDF term (equal and unequal only)

CoreDatatype.XSD leftCoreDatatype = leftLit.getCoreDatatype().asXSDDatatype().orElse(null);
CoreDatatype.XSD rightCoreDatatype = rightLit.getCoreDatatype().asXSDDatatype().orElse(null);
CoreDatatype.XSD leftCoreDatatype = leftLit.getCoreDatatype().asXSDDatatypeOrNull();
CoreDatatype.XSD rightCoreDatatype = rightLit.getCoreDatatype().asXSDDatatypeOrNull();

boolean leftLangLit = Literals.isLanguageLiteral(leftLit);
boolean rightLangLit = Literals.isLanguageLiteral(rightLit);
Expand Down
Loading