Skip to content

Apache Lucene open-source search software

Fix RandomApproximationQuery to not share Random instances across threads.

Adrien Grand456187051bc9
diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java b/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java
index a863ab8cc29..62bd77ef73d 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java
@@ -76,91 +76,112 @@ public class RandomApproximationQuery extends Query {
@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
final Weight weight = query.createWeight(searcher, needsScores);
- return new Weight(RandomApproximationQuery.this) {
+ return new RandomApproximationWeight(weight, new Random(random.nextLong()));
+ }
- @Override
- public Explanation explain(LeafReaderContext context, int doc) throws IOException {
- return weight.explain(context, doc);
- }
+ private static class RandomApproximationWeight extends Weight {
- @Override
- public float getValueForNormalization() throws IOException {
- return weight.getValueForNormalization();
- }
+ private final Weight weight;
+ private final Random random;
- @Override
- public void normalize(float norm, float topLevelBoost) {
- weight.normalize(norm, topLevelBoost);
- }
+ RandomApproximationWeight(Weight weight, Random random) {
+ super(weight.getQuery());
+ this.weight = weight;
+ this.random = random;
+ }
+
+ @Override
+ public Explanation explain(LeafReaderContext context, int doc) throws IOException {
+ return weight.explain(context, doc);
+ }
+
+ @Override
+ public float getValueForNormalization() throws IOException {
+ return weight.getValueForNormalization();
+ }
+
+ @Override
+ public void normalize(float norm, float topLevelBoost) {
+ weight.normalize(norm, topLevelBoost);
+ }
- @Override
- public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
- final Scorer scorer = weight.scorer(context, acceptDocs);
- if (scorer == null) {
- return null;
- }
- final RandomTwoPhaseView twoPhaseView = new RandomTwoPhaseView(random, scorer);
- return new Scorer(this) {
-
- @Override
- public TwoPhaseDocIdSetIterator asTwoPhaseIterator() {
- return twoPhaseView;
- }
-
- @Override
- public float score() throws IOException {
- return scorer.score();
- }
-
- @Override
- public int freq() throws IOException {
- return scorer.freq();
- }
-
- @Override
- public int nextPosition() throws IOException {
- return scorer.nextPosition();
- }
-
- @Override
- public int startOffset() throws IOException {
- return scorer.startOffset();
- }
-
- @Override
- public int endOffset() throws IOException {
- return scorer.endOffset();
- }
-
- @Override
- public BytesRef getPayload() throws IOException {
- return scorer.getPayload();
- }
-
- @Override
- public int docID() {
- return scorer.docID();
- }
-
- @Override
- public int nextDoc() throws IOException {
- return scorer.nextDoc();
- }
-
- @Override
- public int advance(int target) throws IOException {
- return scorer.advance(target);
- }
-
- @Override
- public long cost() {
- return scorer.cost();
- }
-
- };
+ @Override
+ public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
+ final Scorer scorer = weight.scorer(context, acceptDocs);
+ if (scorer == null) {
+ return null;
}
+ return new RandomApproximationScorer(scorer, new Random(random.nextLong()));
+ }
+
+ }
+
+ private static class RandomApproximationScorer extends Scorer {
+
+ private final Scorer scorer;
+ private final RandomTwoPhaseView twoPhaseView;
+
+ RandomApproximationScorer(Scorer scorer, Random random) {
+ super(scorer.getWeight());
+ this.scorer = scorer;
+ this.twoPhaseView = new RandomTwoPhaseView(random, scorer);
+ }
+
+ @Override
+ public TwoPhaseDocIdSetIterator asTwoPhaseIterator() {
+ return twoPhaseView;
+ }
+
+ @Override
+ public float score() throws IOException {
+ return scorer.score();
+ }
+
+ @Override
+ public int freq() throws IOException {
+ return scorer.freq();
+ }
+
+ @Override
+ public int nextPosition() throws IOException {
+ return scorer.nextPosition();
+ }
+
+ @Override
+ public int startOffset() throws IOException {
+ return scorer.startOffset();
+ }
+
+ @Override
+ public int endOffset() throws IOException {
+ return scorer.endOffset();
+ }
+
+ @Override
+ public BytesRef getPayload() throws IOException {
+ return scorer.getPayload();
+ }
+
+ @Override
+ public int docID() {
+ return scorer.docID();
+ }
+
+ @Override
+ public int nextDoc() throws IOException {
+ return scorer.nextDoc();
+ }
+
+ @Override
+ public int advance(int target) throws IOException {
+ return scorer.advance(target);
+ }
+
+ @Override
+ public long cost() {
+ return scorer.cost();
+ }
- };
}
private static class RandomTwoPhaseView extends TwoPhaseDocIdSetIterator {
@@ -182,7 +203,7 @@ public class RandomApproximationQuery extends Query {
public boolean matches() throws IOException {
return approximation.doc == disi.docID();
}
-
+
}
private static class RandomApproximation extends DocIdSetIterator {
@@ -191,7 +212,7 @@ public class RandomApproximationQuery extends Query {
private final DocIdSetIterator disi;
int doc = -1;
-
+
public RandomApproximation(Random random, DocIdSetIterator disi) {
this.random = random;
this.disi = disi;
@@ -201,7 +222,7 @@ public class RandomApproximationQuery extends Query {
public int docID() {
return doc;
}
-
+
@Override
public int nextDoc() throws IOException {
return advance(doc + 1);