Skip to content

Commit 65ba065

Browse files
GH-4581 Remove limitedsize evaluation strategies.
As well as supplier methods later better done with the collection factory API.
1 parent b7a2976 commit 65ba065

13 files changed

Lines changed: 92 additions & 42 deletions

File tree

core/common/iterator/src/main/java/org/eclipse/rdf4j/common/iteration/DistinctIteration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ public class DistinctIteration<E> extends FilterIteration<E> {
4141
public DistinctIteration(CloseableIteration<? extends E> iter) {
4242
super(iter);
4343

44-
excludeSet = makeSet();
44+
excludeSet = new HashSet<>();
45+
}
46+
47+
public DistinctIteration(CloseableIteration<? extends E> iter, Set<E> set) {
48+
super(iter);
49+
excludeSet = set;
4550
}
4651

4752
public DistinctIteration(CloseableIteration<? extends E> iter, Supplier<Set<E>> setMaker) {
@@ -86,9 +91,4 @@ private boolean inExcludeSet(E object) {
8691
protected boolean add(E object) {
8792
return excludeSet.add(object);
8893
}
89-
90-
protected Set<E> makeSet() {
91-
return new HashSet<>();
92-
}
93-
9494
}

core/common/iterator/src/main/java/org/eclipse/rdf4j/common/iteration/IntersectIteration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public IntersectIteration(CloseableIteration<? extends E> arg1, CloseableIterati
107107
protected boolean accept(E object) {
108108
if (!initialized) {
109109
// Build set of elements-to-include from second argument
110-
includeSet = Iterations.asSet(arg2);
110+
includeSet = Iterations.addAll(arg2, setMaker.get());
111111
initialized = true;
112112
}
113113

core/query/src/main/java/org/eclipse/rdf4j/query/QueryResults.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.ArrayList;
1616
import java.util.Collections;
1717
import java.util.HashMap;
18+
import java.util.HashSet;
1819
import java.util.List;
1920
import java.util.Map;
2021
import java.util.NoSuchElementException;
@@ -575,7 +576,11 @@ private static class GraphQueryResultFilter extends AbstractCloseableIteration<S
575576
private final GraphQueryResult unfiltered;
576577

577578
public GraphQueryResultFilter(GraphQueryResult wrappedResult) {
578-
this.filter = new DistinctIteration<>(wrappedResult);
579+
this(wrappedResult, new HashSet<>());
580+
}
581+
582+
public GraphQueryResultFilter(GraphQueryResult wrappedResult, Set<Statement> distinctSet) {
583+
this.filter = new DistinctIteration<>(wrappedResult, distinctSet);
579584
this.unfiltered = wrappedResult;
580585
}
581586

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

641646
public TupleQueryResultFilter(TupleQueryResult wrappedResult) {
642-
this.filter = new DistinctIteration<>(wrappedResult);
647+
this(wrappedResult, new HashSet<>());
648+
}
649+
650+
public TupleQueryResultFilter(TupleQueryResult wrappedResult, Set<BindingSet> distinct) {
651+
this.filter = new DistinctIteration<>(wrappedResult, distinct);
643652
this.unfiltered = wrappedResult;
644653
}
645654

core/query/src/test/java/org/eclipse/rdf4j/common/iteration/DistinctIterationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class DistinctIterationTest extends CloseableIterationTest {
1616

1717
@Override
1818
protected CloseableIteration<String> createTestIteration() {
19-
return new DistinctIteration<>(createStringList1Iteration());
19+
return new DistinctIteration<>(createStringList1Iteration(), new HashSet<>());
2020
}
2121

2222
@Override

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/EvaluationStrategy.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
*******************************************************************************/
1111
package org.eclipse.rdf4j.query.algebra.evaluation;
1212

13-
import java.util.Queue;
14-
import java.util.Set;
1513
import java.util.function.Supplier;
1614

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

161-
default <T> Set<T> makeSet() {
162-
return new DefaultCollectionFactory().createSet();
163-
}
164-
165-
default <T> Queue<T> makeQueue() {
166-
return new DefaultCollectionFactory().createQueue();
167-
}
168-
169159
/**
170160
* Set the collection factory that will create the collections to use during query evaluaton.
171161
*

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/DefaultEvaluationStrategy.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ protected QueryEvaluationStep prepare(Intersection node, QueryEvaluationContext
542542
throws QueryEvaluationException {
543543
QueryEvaluationStep leftArg = precompile(node.getLeftArg(), context);
544544
QueryEvaluationStep rightArg = precompile(node.getRightArg(), context);
545-
return new IntersectionQueryEvaluationStep(leftArg, rightArg, this::makeSet);
545+
return new IntersectionQueryEvaluationStep(leftArg, rightArg, this.getCollectionFactory().get());
546546
}
547547

548548
protected QueryEvaluationStep prepare(Join node, QueryEvaluationContext context) throws QueryEvaluationException {
@@ -710,12 +710,22 @@ protected QueryEvaluationStep prepare(DescribeOperator node, QueryEvaluationCont
710710
protected QueryEvaluationStep prepare(Distinct node, QueryEvaluationContext context)
711711
throws QueryEvaluationException {
712712
final QueryEvaluationStep child = precompile(node.getArg(), context);
713+
CollectionFactory cf = this.getCollectionFactory().get();
713714
return bindings -> {
714715
final CloseableIteration<BindingSet> evaluate = child.evaluate(bindings);
715-
return new DistinctIteration<BindingSet>(evaluate,
716-
DefaultEvaluationStrategy.this::makeSet);
717-
};
716+
return new DistinctIteration<BindingSet>(evaluate, cf::createSet) {
717+
718+
@Override
719+
protected void handleClose() {
720+
try {
721+
cf.close();
722+
} finally {
723+
super.handleClose();
724+
}
725+
}
718726

727+
};
728+
};
719729
}
720730

721731
protected QueryEvaluationStep prepare(Reduced node, QueryEvaluationContext context)

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/evaluationsteps/IntersectionQueryEvaluationStep.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,54 @@
1010
*******************************************************************************/
1111
package org.eclipse.rdf4j.query.algebra.evaluation.impl.evaluationsteps;
1212

13-
import java.util.Set;
1413
import java.util.function.Function;
15-
import java.util.function.Supplier;
1614

15+
import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
1716
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
1817
import org.eclipse.rdf4j.common.iteration.IntersectIteration;
1918
import org.eclipse.rdf4j.query.BindingSet;
19+
import org.eclipse.rdf4j.query.QueryEvaluationException;
2020
import org.eclipse.rdf4j.query.algebra.evaluation.QueryEvaluationStep;
2121

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

27+
private static final class IntersectIterationUsingSetFromCollectionFactory
28+
extends IntersectIteration<BindingSet> {
29+
private final CollectionFactory cf;
30+
31+
private IntersectIterationUsingSetFromCollectionFactory(CloseableIteration<BindingSet> arg1,
32+
CloseableIteration<BindingSet> arg2, CollectionFactory cf) {
33+
super(arg1, arg2, false, cf::createSetOfBindingSets);
34+
this.cf = cf;
35+
}
36+
37+
@Override
38+
protected void handleClose() throws QueryEvaluationException {
39+
try {
40+
cf.close();
41+
} finally {
42+
super.handleClose();
43+
}
44+
}
45+
}
46+
2747
private final QueryEvaluationStep leftArg;
2848
private final Function<BindingSet, DelayedEvaluationIteration> rightArgDelayed;
29-
private final Supplier<Set<BindingSet>> setMaker;
49+
private final CollectionFactory collectionFactory;
3050

3151
public IntersectionQueryEvaluationStep(QueryEvaluationStep leftArg, QueryEvaluationStep rightArg,
32-
Supplier<Set<BindingSet>> setMaker) {
33-
this.setMaker = setMaker;
52+
CollectionFactory collectionFactory) {
53+
this.collectionFactory = collectionFactory;
3454
this.leftArg = leftArg;
3555
rightArgDelayed = bs -> new DelayedEvaluationIteration(rightArg, bs);
3656
}
3757

3858
@Override
3959
public CloseableIteration<BindingSet> evaluate(BindingSet bs) {
40-
return new IntersectIteration<>(leftArg.evaluate(bs), rightArgDelayed.apply(bs), setMaker);
60+
return new IntersectIterationUsingSetFromCollectionFactory(leftArg.evaluate(bs), rightArgDelayed.apply(bs),
61+
collectionFactory);
4162
}
4263
}

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/iterator/PathIteration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ public PathIteration(EvaluationStrategy strategy, Scope scope, Var startVar,
110110

111111
this.currentLength = minLength;
112112
this.bindings = bindings;
113-
114-
collectionFactory = strategy.getCollectionFactory().get();
113+
this.collectionFactory = strategy.getCollectionFactory().get();
115114

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

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/iterator/ZeroLengthPathIteration.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Set;
1616
import java.util.function.BiConsumer;
1717

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

7273
private final BiConsumer<Value, MutableBindingSet> setContext;
7374

75+
private final CollectionFactory cf;
76+
7477
public ZeroLengthPathIteration(EvaluationStrategy evaluationStrategyImpl, Var subjectVar, Var objVar, Value subj,
7578
Value obj, Var contextVar, BindingSet bindings, QueryEvaluationContext context) {
7679
this.evaluationStrategy = evaluationStrategyImpl;
@@ -99,14 +102,14 @@ public ZeroLengthPathIteration(EvaluationStrategy evaluationStrategyImpl, Var su
99102
} else {
100103
setContext = null;
101104
}
102-
105+
this.cf = evaluationStrategy.getCollectionFactory().get();
103106
}
104107

105108
@Override
106109
protected BindingSet getNextElement() throws QueryEvaluationException {
107110
if (subj == null && obj == null) {
108111
if (this.reportedValues == null) {
109-
reportedValues = evaluationStrategy.makeSet();
112+
reportedValues = cf.createValueSet();
110113
}
111114
if (this.iter == null) {
112115
// join with a sequence so we iterate over every entry twice
@@ -174,7 +177,7 @@ public Var createAnonVar(String varName) {
174177
}
175178

176179
@Override
177-
protected void handleClose() {
178-
180+
protected void handleClose() throws QueryEvaluationException {
181+
cf.close();
179182
}
180183
}

core/repository/api/src/main/java/org/eclipse/rdf4j/repository/RepositoryResult.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.util.ArrayList;
1414
import java.util.Collection;
15+
import java.util.HashSet;
1516
import java.util.Iterator;
1617
import java.util.List;
1718

@@ -88,7 +89,7 @@ public void enableDuplicateFilter() throws RepositoryException {
8889
return;
8990
}
9091

91-
wrappedIter = new DistinctIteration<T>(wrappedIter);
92+
wrappedIter = new DistinctIteration<T>(wrappedIter, new HashSet<>());
9293
}
9394

9495
/**

0 commit comments

Comments
 (0)