Skip to content

Commit

Permalink
GH-4581 Remove limitedsize evaluation strategies.
Browse files Browse the repository at this point in the history
As well as supplier methods later better done with the collection
factory API.
  • Loading branch information
JervenBolleman committed May 31, 2024
1 parent b7a2976 commit a424d30
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public class DistinctIteration<E> extends FilterIteration<E> {
public DistinctIteration(CloseableIteration<? extends E> iter) {
super(iter);

excludeSet = makeSet();
excludeSet = new HashSet<>();
}

public DistinctIteration(CloseableIteration<? extends E> iter, Set<E> set) {
super(iter);
excludeSet = set;
}

public DistinctIteration(CloseableIteration<? extends E> iter, Supplier<Set<E>> setMaker) {
Expand Down Expand Up @@ -86,9 +91,4 @@ private boolean inExcludeSet(E object) {
protected boolean add(E object) {
return excludeSet.add(object);
}

protected Set<E> makeSet() {
return new HashSet<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public IntersectIteration(CloseableIteration<? extends E> arg1, CloseableIterati
protected boolean accept(E object) {
if (!initialized) {
// Build set of elements-to-include from second argument
includeSet = Iterations.asSet(arg2);
includeSet = Iterations.addAll(arg2, setMaker.get());
initialized = true;
}

Expand Down
13 changes: 11 additions & 2 deletions core/query/src/main/java/org/eclipse/rdf4j/query/QueryResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -575,7 +576,11 @@ private static class GraphQueryResultFilter extends AbstractCloseableIteration<S
private final GraphQueryResult unfiltered;

public GraphQueryResultFilter(GraphQueryResult wrappedResult) {
this.filter = new DistinctIteration<>(wrappedResult);
this(wrappedResult, new HashSet<>());
}

public GraphQueryResultFilter(GraphQueryResult wrappedResult, Set<Statement> distinctSet) {
this.filter = new DistinctIteration<>(wrappedResult, distinctSet);
this.unfiltered = wrappedResult;
}

Expand Down Expand Up @@ -639,7 +644,11 @@ private static class TupleQueryResultFilter extends AbstractCloseableIteration<B
private final TupleQueryResult unfiltered;

public TupleQueryResultFilter(TupleQueryResult wrappedResult) {
this.filter = new DistinctIteration<>(wrappedResult);
this(wrappedResult, new HashSet<>());
}

public TupleQueryResultFilter(TupleQueryResult wrappedResult, Set<BindingSet> distinct) {
this.filter = new DistinctIteration<>(wrappedResult, distinct);
this.unfiltered = wrappedResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DistinctIterationTest extends CloseableIterationTest {

@Override
protected CloseableIteration<String> createTestIteration() {
return new DistinctIteration<>(createStringList1Iteration());
return new DistinctIteration<>(createStringList1Iteration(), new HashSet<>());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*******************************************************************************/
package org.eclipse.rdf4j.query.algebra.evaluation;

import java.util.Queue;
import java.util.Set;
import java.util.function.Supplier;

import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
Expand Down Expand Up @@ -158,14 +156,6 @@ default QueryValueEvaluationStep precompile(ValueExpr arg, QueryEvaluationContex
return new QueryValueEvaluationStep.Minimal(this, arg);
}

default <T> Set<T> makeSet() {
return new DefaultCollectionFactory().createSet();
}

default <T> Queue<T> makeQueue() {
return new DefaultCollectionFactory().createQueue();
}

/**
* Set the collection factory that will create the collections to use during query evaluaton.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ protected QueryEvaluationStep prepare(Intersection node, QueryEvaluationContext
throws QueryEvaluationException {
QueryEvaluationStep leftArg = precompile(node.getLeftArg(), context);
QueryEvaluationStep rightArg = precompile(node.getRightArg(), context);
return new IntersectionQueryEvaluationStep(leftArg, rightArg, this::makeSet);
return new IntersectionQueryEvaluationStep(leftArg, rightArg, this.getCollectionFactory().get());
}

protected QueryEvaluationStep prepare(Join node, QueryEvaluationContext context) throws QueryEvaluationException {
Expand Down Expand Up @@ -710,12 +710,22 @@ protected QueryEvaluationStep prepare(DescribeOperator node, QueryEvaluationCont
protected QueryEvaluationStep prepare(Distinct node, QueryEvaluationContext context)
throws QueryEvaluationException {
final QueryEvaluationStep child = precompile(node.getArg(), context);
CollectionFactory cf = this.getCollectionFactory().get();
return bindings -> {
final CloseableIteration<BindingSet> evaluate = child.evaluate(bindings);
return new DistinctIteration<BindingSet>(evaluate,
DefaultEvaluationStrategy.this::makeSet);
};
return new DistinctIteration<BindingSet>(evaluate, cf::createSet) {

@Override
protected void handleClose() {
try {
cf.close();
} finally {
super.handleClose();
}
}

};
};
}

protected QueryEvaluationStep prepare(Reduced node, QueryEvaluationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,54 @@
*******************************************************************************/
package org.eclipse.rdf4j.query.algebra.evaluation.impl.evaluationsteps;

import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;

import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.eclipse.rdf4j.common.iteration.IntersectIteration;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.algebra.evaluation.QueryEvaluationStep;

/**
* A step that prepares the arguments of an Intersection operator before execution.
*/
public class IntersectionQueryEvaluationStep implements QueryEvaluationStep {

private static final class IntersectIterationUsingSetFromCollectionFactory
extends IntersectIteration<BindingSet> {
private final CollectionFactory cf;

private IntersectIterationUsingSetFromCollectionFactory(CloseableIteration<BindingSet> arg1,
CloseableIteration<BindingSet> arg2, CollectionFactory cf) {
super(arg1, arg2, false, cf::createSetOfBindingSets);
this.cf = cf;
}

@Override
protected void handleClose() throws QueryEvaluationException {
try {
cf.close();
} finally {
super.handleClose();
}
}
}

private final QueryEvaluationStep leftArg;
private final Function<BindingSet, DelayedEvaluationIteration> rightArgDelayed;
private final Supplier<Set<BindingSet>> setMaker;
private final CollectionFactory collectionFactory;

public IntersectionQueryEvaluationStep(QueryEvaluationStep leftArg, QueryEvaluationStep rightArg,
Supplier<Set<BindingSet>> setMaker) {
this.setMaker = setMaker;
CollectionFactory collectionFactory) {
this.collectionFactory = collectionFactory;
this.leftArg = leftArg;
rightArgDelayed = bs -> new DelayedEvaluationIteration(rightArg, bs);
}

@Override
public CloseableIteration<BindingSet> evaluate(BindingSet bs) {
return new IntersectIteration<>(leftArg.evaluate(bs), rightArgDelayed.apply(bs), setMaker);
return new IntersectIterationUsingSetFromCollectionFactory(leftArg.evaluate(bs), rightArgDelayed.apply(bs),
collectionFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public PathIteration(EvaluationStrategy strategy, Scope scope, Var startVar,

this.currentLength = minLength;
this.bindings = bindings;

collectionFactory = strategy.getCollectionFactory().get();
this.collectionFactory = strategy.getCollectionFactory().get();

// This is all necessary for optimized collections to be usable. This only becomes important on very large
// stores with large intermediary results.
Expand All @@ -121,7 +120,6 @@ public PathIteration(EvaluationStrategy strategy, Scope scope, Var startVar,
PathIteration::getGet, PathIteration::getSet);
this.valueQueue = collectionFactory.createBindingSetQueue(ValuePair::new, PathIteration::getHas,
PathIteration::getGet, PathIteration::getSet);

createIteration();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;
import java.util.function.BiConsumer;

import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.eclipse.rdf4j.common.iteration.LookAheadIteration;
import org.eclipse.rdf4j.model.Literal;
Expand Down Expand Up @@ -71,6 +72,8 @@ public class ZeroLengthPathIteration extends LookAheadIteration<BindingSet> {

private final BiConsumer<Value, MutableBindingSet> setContext;

private final CollectionFactory cf;

public ZeroLengthPathIteration(EvaluationStrategy evaluationStrategyImpl, Var subjectVar, Var objVar, Value subj,
Value obj, Var contextVar, BindingSet bindings, QueryEvaluationContext context) {
this.evaluationStrategy = evaluationStrategyImpl;
Expand Down Expand Up @@ -99,14 +102,14 @@ public ZeroLengthPathIteration(EvaluationStrategy evaluationStrategyImpl, Var su
} else {
setContext = null;
}

this.cf = evaluationStrategy.getCollectionFactory().get();
}

@Override
protected BindingSet getNextElement() throws QueryEvaluationException {
if (subj == null && obj == null) {
if (this.reportedValues == null) {
reportedValues = evaluationStrategy.makeSet();
reportedValues = cf.createValueSet();
}
if (this.iter == null) {
// join with a sequence so we iterate over every entry twice
Expand Down Expand Up @@ -174,7 +177,7 @@ public Var createAnonVar(String varName) {
}

@Override
protected void handleClose() {

protected void handleClose() throws QueryEvaluationException {
cf.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -88,7 +89,7 @@ public void enableDuplicateFilter() throws RepositoryException {
return;
}

wrappedIter = new DistinctIteration<T>(wrappedIter);
wrappedIter = new DistinctIteration<T>(wrappedIter, new HashSet<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -312,8 +313,10 @@ public CloseableIteration<? extends Triple> getTriples(Resource subj, IRI pred,
changes.getApprovedTriples(subj, pred, obj).iterator());

// merge newly approved triples in the changeset with data from the backing source
// TODO: see if use of collection factory is possible here.
return new DistinctIteration<>(
DualUnionIteration.getWildcardInstance(iter, tripleExceptionCloseableIteratorIteration));
DualUnionIteration.getWildcardInstance(iter, tripleExceptionCloseableIteratorIteration),
new HashSet<>());
}

// nothing relevant in the backing source, just return all matching approved triples from the changeset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.rdf4j.sail.base;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.rdf4j.common.annotation.InternalUseOnly;
Expand Down Expand Up @@ -119,7 +120,8 @@ public CloseableIteration<? extends Triple> getRdfStarTriples(Resource subj, IRI
return triples;
}
iterationWrapper = new TripleSourceIterationWrapper<>(triples);
return new DistinctIteration<>(iterationWrapper);
// TODO: see if use of collection factory is possible here.
return new DistinctIteration<>(iterationWrapper, new HashSet<>());
} catch (Throwable t) {
try {
if (triples != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.eclipse.rdf4j.common.iteration.DistinctIteration;
import org.eclipse.rdf4j.common.iteration.EmptyIteration;
Expand Down Expand Up @@ -264,13 +265,25 @@ public void cancel() {

// execute the union in a separate thread
federationContext.getManager().getExecutor().execute(union);
CollectionFactory cf = federation.getCollectionFactory().get();
ExceptionConvertingIteration<Resource, SailException> conv = new ExceptionConvertingIteration<>(union) {

return new DistinctIteration<>(new ExceptionConvertingIteration<>(union) {
@Override
protected SailException convert(RuntimeException e) {
return new SailException(e);
}
});
};
return new DistinctIteration<Resource>(conv, cf::createSet) {

protected void handleClose() {
try {
cf.close();
} finally {
super.handleClose();
}
}

};
}

@Override
Expand Down

0 comments on commit a424d30

Please sign in to comment.