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.

Signed-off-by: Jerven Bolleman <jerven.bolleman@sib.swiss>
  • Loading branch information
JervenBolleman committed Apr 26, 2024
1 parent e283cde commit ff3b803
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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, Supplier<Set<E>> setMaker) {
Expand Down Expand Up @@ -86,9 +86,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 @@ -34,7 +34,7 @@ public class IntersectIteration<E> extends FilterIteration<E> {

private boolean initialized;

private Set<E> includeSet;
private final Set<E> includeSet;

private final Supplier<Set<E>> setMaker;

Expand Down Expand Up @@ -64,6 +64,7 @@ public IntersectIteration(CloseableIteration<? extends E> arg1, CloseableIterati
* @param arg1 An Iteration containing the first set of elements.
* @param arg2 An Iteration containing the second set of elements.
* @param distinct Flag indicating whether duplicate elements should be filtered from the result.
* @param set A set used to determine the intersection
*/
public IntersectIteration(CloseableIteration<? extends E> arg1, CloseableIteration<? extends E> arg2,
boolean distinct) {
Expand All @@ -74,7 +75,7 @@ public IntersectIteration(CloseableIteration<? extends E> arg1, CloseableIterati
this.arg2 = arg2;
this.distinct = distinct;
this.initialized = false;
this.setMaker = this::makeSet;
this.setMaker = HashSet::new;
}

/**
Expand Down Expand Up @@ -106,9 +107,7 @@ public IntersectIteration(CloseableIteration<? extends E> arg1, CloseableIterati
@Override
protected boolean accept(E object) {
if (!initialized) {
// Build set of elements-to-include from second argument
includeSet = Iterations.asSet(arg2);
initialized = true;
initialize();
}

if (inIncludeSet(object)) {
Expand All @@ -134,10 +133,6 @@ protected boolean inIncludeSet(E object) {
return includeSet.contains(object);
}

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

@Override
protected void handleClose() {
if (arg2 != null) {
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 @@ -11,6 +11,7 @@
package org.eclipse.rdf4j.common.iteration;

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

public class IntersectionIterationTest extends CloseableIterationTest {
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,10 +710,26 @@ protected QueryEvaluationStep prepare(DescribeOperator node, QueryEvaluationCont
protected QueryEvaluationStep prepare(Distinct node, QueryEvaluationContext context)
throws QueryEvaluationException {
final QueryEvaluationStep child = precompile(node.getArg(), context);
return bindings -> {
final CloseableIteration<BindingSet> evaluate = child.evaluate(bindings);
return new DistinctIteration<BindingSet>(evaluate,
DefaultEvaluationStrategy.this::makeSet);
final CollectionFactory cf = this.getCollectionFactory().get();
return new QueryEvaluationStep() {

@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings) {
final CloseableIteration<BindingSet, QueryEvaluationException> evaluate = child.evaluate(bindings);
return new DistinctIteration<BindingSet, QueryEvaluationException>(evaluate,
cf.createSetOfBindingSets()) {

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

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.common.iteration.Iteration;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.algebra.evaluation.QueryEvaluationStep;

Expand All @@ -24,19 +25,41 @@
*/
public class IntersectionQueryEvaluationStep implements QueryEvaluationStep {

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

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

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

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 @@ -65,6 +65,8 @@ public class PathIteration extends LookAheadIteration<BindingSet> {

private ValuePair currentVp;

private final CollectionFactory cf;

private static final String JOINVAR_PREFIX = "intermediate_join_";

private final Set<String> namedIntermediateJoins = new HashSet<>();
Expand All @@ -88,7 +90,8 @@ public PathIteration(EvaluationStrategy strategy, Scope scope, Var startVar,
this.currentLength = minLength;
this.bindings = bindings;

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

this.collectionFactory = strategy.getCollectionFactory().get();
this.reportedValues = collectionFactory.createSet();
this.unreportedValues = collectionFactory.createSet();
this.valueQueue = collectionFactory.createQueue();
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 @@ -177,4 +180,10 @@ public Var createAnonVar(String varName) {
protected void handleClose() {

}

@Override
protected void handleClose() throws QueryEvaluationException {
cf.close();
super.handleClose();
}
}
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 @@ -12,6 +12,7 @@

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

import org.eclipse.rdf4j.common.annotation.InternalUseOnly;
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
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
Loading

0 comments on commit ff3b803

Please sign in to comment.