Skip to content

Commit a58ff06

Browse files
GH-4581 Make sure collection factories are used when we use a collection
This allows to always be sure that we can fall back to disk if required. Also allows optimized datastructures to be injected.
1 parent 65ba065 commit a58ff06

10 files changed

Lines changed: 61 additions & 39 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,30 @@ public class DistinctIteration<E> extends FilterIteration<E> {
3838
*
3939
* @param iter The underlying iterator.
4040
*/
41+
@Deprecated
4142
public DistinctIteration(CloseableIteration<? extends E> iter) {
4243
super(iter);
4344

4445
excludeSet = new HashSet<>();
4546
}
4647

47-
public DistinctIteration(CloseableIteration<? extends E> iter, Set<E> set) {
48+
/**
49+
* Creates a new DistinctIterator.
50+
*
51+
* @param Set<E> a hopefully optimized set
52+
* @param iter The underlying iterator.
53+
*/
54+
public DistinctIteration(CloseableIteration<? extends E> iter, Set<E> excludeSet) {
4855
super(iter);
49-
excludeSet = set;
56+
this.excludeSet = excludeSet;
5057
}
5158

59+
/**
60+
* Creates a new DistinctIterator.
61+
*
62+
* @param Supplier<Set<E>> a supplier of a hopefully optimized set
63+
* @param iter The underlying iterator.
64+
*/
5265
public DistinctIteration(CloseableIteration<? extends E> iter, Supplier<Set<E>> setMaker) {
5366
super(iter);
5467
excludeSet = setMaker.get();

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(), new HashSet<>());
19+
return new DistinctIteration<>(createStringList1Iteration(), HashSet::new);
2020
}
2121

2222
@Override

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,15 @@ protected QueryEvaluationStep prepare(Difference node, QueryEvaluationContext co
533533
}
534534

535535
protected QueryEvaluationStep prepare(Group node, QueryEvaluationContext context) throws QueryEvaluationException {
536-
return bindings -> new GroupIterator(DefaultEvaluationStrategy.this, node, bindings,
537-
iterationCacheSyncThreshold,
538-
context);
536+
return bindings -> new GroupIterator(DefaultEvaluationStrategy.this, node, bindings, context);
539537
}
540538

541539
protected QueryEvaluationStep prepare(Intersection node, QueryEvaluationContext context)
542540
throws QueryEvaluationException {
543541
QueryEvaluationStep leftArg = precompile(node.getLeftArg(), context);
544542
QueryEvaluationStep rightArg = precompile(node.getRightArg(), context);
545-
return new IntersectionQueryEvaluationStep(leftArg, rightArg, this.getCollectionFactory().get());
543+
544+
return new IntersectionQueryEvaluationStep(leftArg, rightArg, getCollectionFactory());
546545
}
547546

548547
protected QueryEvaluationStep prepare(Join node, QueryEvaluationContext context) throws QueryEvaluationException {
@@ -710,20 +709,19 @@ protected QueryEvaluationStep prepare(DescribeOperator node, QueryEvaluationCont
710709
protected QueryEvaluationStep prepare(Distinct node, QueryEvaluationContext context)
711710
throws QueryEvaluationException {
712711
final QueryEvaluationStep child = precompile(node.getArg(), context);
713-
CollectionFactory cf = this.getCollectionFactory().get();
712+
final CollectionFactory cf = this.getCollectionFactory().get();
714713
return bindings -> {
715714
final CloseableIteration<BindingSet> evaluate = child.evaluate(bindings);
716-
return new DistinctIteration<BindingSet>(evaluate, cf::createSet) {
715+
return new DistinctIteration<BindingSet>(evaluate, cf.createSetOfBindingSets()) {
717716

718717
@Override
719-
protected void handleClose() {
718+
protected void handleClose() throws QueryEvaluationException {
720719
try {
721720
cf.close();
722721
} finally {
723722
super.handleClose();
724723
}
725724
}
726-
727725
};
728726
};
729727
}

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,43 @@
1111
package org.eclipse.rdf4j.query.algebra.evaluation.impl.evaluationsteps;
1212

1313
import java.util.function.Function;
14+
import java.util.function.Supplier;
1415

1516
import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
1617
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
1718
import org.eclipse.rdf4j.common.iteration.IntersectIteration;
1819
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-
4727
private final QueryEvaluationStep leftArg;
4828
private final Function<BindingSet, DelayedEvaluationIteration> rightArgDelayed;
49-
private final CollectionFactory collectionFactory;
29+
private final Supplier<CollectionFactory> cfs;
5030

5131
public IntersectionQueryEvaluationStep(QueryEvaluationStep leftArg, QueryEvaluationStep rightArg,
52-
CollectionFactory collectionFactory) {
53-
this.collectionFactory = collectionFactory;
32+
Supplier<CollectionFactory> cfs) {
33+
this.cfs = cfs;
5434
this.leftArg = leftArg;
5535
rightArgDelayed = bs -> new DelayedEvaluationIteration(rightArg, bs);
5636
}
5737

5838
@Override
5939
public CloseableIteration<BindingSet> evaluate(BindingSet bs) {
60-
return new IntersectIterationUsingSetFromCollectionFactory(leftArg.evaluate(bs), rightArgDelayed.apply(bs),
61-
collectionFactory);
40+
CollectionFactory cf = cfs.get();
41+
return new IntersectIteration<>(leftArg.evaluate(bs), rightArgDelayed.apply(bs), cf::createSetOfBindingSets) {
42+
@Override
43+
protected void handleClose() {
44+
try {
45+
cf.close();
46+
} finally {
47+
super.handleClose();
48+
}
49+
}
50+
};
6251
}
52+
6353
}

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

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

111111
this.currentLength = minLength;
112112
this.bindings = bindings;
113+
113114
this.collectionFactory = strategy.getCollectionFactory().get();
114115

115116
// This is all necessary for optimized collections to be usable. This only becomes important on very large

tools/federation/src/main/java/org/eclipse/rdf4j/federated/FedXConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import java.util.Optional;
1414

15+
import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
16+
import org.eclipse.rdf4j.collection.factory.impl.DefaultCollectionFactory;
1517
import org.eclipse.rdf4j.federated.cache.SourceSelectionCache;
1618
import org.eclipse.rdf4j.federated.cache.SourceSelectionCacheFactory;
1719
import org.eclipse.rdf4j.federated.cache.SourceSelectionMemoryCache;
@@ -66,6 +68,7 @@ public class FedXConfig {
6668

6769
private int consumingIterationMax = 1000;
6870

71+
private CollectionFactory cf = new DefaultCollectionFactory();
6972
/* factory like setters */
7073

7174
/**
@@ -472,4 +475,19 @@ public FedXConfig withConsumingIterationMax(int max) {
472475
public int getConsumingIterationMax() {
473476
return consumingIterationMax;
474477
}
478+
479+
/**
480+
* Set the CollectionFactory to be used by the federation
481+
*
482+
* <p>
483+
* Can only be set before federation initialization.
484+
* </p>
485+
*
486+
* @param cf
487+
* @return the current config
488+
*/
489+
public FedXConfig withCollectionFactory(CollectionFactory cf) {
490+
this.cf = cf;
491+
return this;
492+
}
475493
}

tools/federation/src/main/java/org/eclipse/rdf4j/federated/FedXConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ protected SailException convert(RuntimeException e) {
275275
};
276276
return new DistinctIteration<Resource>(conv, cf::createSet) {
277277

278+
@Override
278279
protected void handleClose() {
279280
try {
280281
cf.close();

tools/federation/src/main/java/org/eclipse/rdf4j/federated/FedXFactory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ public FedXRepository create() {
220220
if (this.writeStrategyFactory != null) {
221221
federation.setWriteStrategyFactory(writeStrategyFactory);
222222
}
223-
224223
FedXRepository repo = new FedXRepository(federation, this.config);
225224
if (this.repositoryResolver != null) {
226225
repo.setRepositoryResolver(repositoryResolver);

tools/federation/src/main/java/org/eclipse/rdf4j/federated/evaluation/FederationEvalStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import java.util.Set;
1616
import java.util.concurrent.Executor;
1717
import java.util.concurrent.atomic.AtomicBoolean;
18+
import java.util.function.Supplier;
1819
import java.util.stream.Collectors;
1920

21+
import org.eclipse.rdf4j.collection.factory.api.CollectionFactory;
2022
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
2123
import org.eclipse.rdf4j.common.iteration.EmptyIteration;
2224
import org.eclipse.rdf4j.common.iteration.SingletonIteration;
@@ -987,5 +989,4 @@ protected CloseableIteration<BindingSet> evaluateAtStatementSources(
987989
throw new QueryEvaluationException(e);
988990
}
989991
}
990-
991992
}

tools/federation/src/test/java/org/eclipse/rdf4j/federated/FedXRule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void beforeEach(ExtensionContext ctx) {
4343
for (Consumer<FedXConfig> configConsumer : configurations) {
4444
configConsumer.accept(fedxConfig);
4545
}
46+
4647
List<Endpoint> endpoints = Collections.<Endpoint>emptyList();
4748
repository = FedXFactory.newFederation().withMembers(endpoints).withConfig(fedxConfig).create();
4849
repository.init();

0 commit comments

Comments
 (0)