Skip to content

Commit 0a40f9e

Browse files
authored
Clean up how the test framework creates asserting scorables. (#14452)
This removes the `WrappedScorer` class and uses the existing `AssertingScorer` class instead.
1 parent 81f2517 commit 0a40f9e

File tree

4 files changed

+38
-62
lines changed

4 files changed

+38
-62
lines changed

lucene/core/src/test/org/apache/lucene/search/TestSloppyPhraseQuery.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.apache.lucene.tests.analysis.MockAnalyzer;
3030
import org.apache.lucene.tests.analysis.MockTokenizer;
3131
import org.apache.lucene.tests.index.RandomIndexWriter;
32-
import org.apache.lucene.tests.search.AssertingScorable;
3332
import org.apache.lucene.tests.search.QueryUtils;
3433
import org.apache.lucene.tests.store.MockDirectoryWrapper;
3534
import org.apache.lucene.tests.util.LuceneTestCase;
@@ -157,7 +156,8 @@ private float checkPhraseQuery(Document doc, PhraseQuery query, int slop, int ex
157156

158157
IndexReader reader = writer.getReader();
159158

160-
IndexSearcher searcher = newSearcher(reader);
159+
// Disable assertions as the collector expects a PhraseScorer, not an asserting wrapper.
160+
IndexSearcher searcher = newSearcher(reader, random().nextBoolean(), false);
161161
Result result = searcher.search(query, new MaxFreqCollectorManager());
162162
assertEquals(
163163
"slop: " + slop + " query: " + query + " doc: " + doc + " Wrong number of hits",
@@ -218,7 +218,7 @@ static class MaxFreqCollector extends SimpleCollector {
218218

219219
@Override
220220
public void setScorer(Scorable scorer) throws IOException {
221-
this.scorer = (Scorer) AssertingScorable.unwrap(scorer);
221+
this.scorer = (Scorer) scorer;
222222
}
223223

224224
@Override
@@ -250,7 +250,7 @@ public SimpleCollector newCollector() {
250250

251251
@Override
252252
public void setScorer(Scorable scorer) {
253-
this.scorer = (Scorer) AssertingScorable.unwrap(scorer);
253+
this.scorer = (Scorer) scorer;
254254
}
255255

256256
@Override
@@ -323,7 +323,8 @@ public void testInfiniteFreq1() throws Exception {
323323
IndexReader ir = iw.getReader();
324324
iw.close();
325325

326-
IndexSearcher is = newSearcher(ir);
326+
// Disable assertions as the collector expects a PhraseScorer, not an asserting wrapper.
327+
IndexSearcher is = newSearcher(ir, random().nextBoolean(), false);
327328
PhraseQuery.Builder builder = new PhraseQuery.Builder();
328329
builder.add(new Term("lyrics", "drug"), 1);
329330
builder.add(new Term("lyrics", "drug"), 3);
@@ -377,7 +378,8 @@ public void testInfiniteFreq2() throws Exception {
377378
IndexReader ir = iw.getReader();
378379
iw.close();
379380

380-
IndexSearcher is = newSearcher(ir);
381+
// Disable assertions as the collector expects a PhraseScorer, not an asserting wrapper.
382+
IndexSearcher is = newSearcher(ir, random().nextBoolean(), false);
381383

382384
PhraseQuery.Builder builder = new PhraseQuery.Builder();
383385
builder.add(new Term("lyrics", "drug"), 1);

lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.IOException;
2121
import org.apache.lucene.search.FilterScorable;
22-
import org.apache.lucene.search.FilterScorer;
2322
import org.apache.lucene.search.Scorable;
2423
import org.apache.lucene.search.Scorer;
2524

@@ -44,45 +43,16 @@ public void setMinCompetitiveScore(float minScore) throws IOException {
4443
}
4544

4645
public static Scorable wrap(Scorable in) {
47-
if (in instanceof WrappedScorer || in instanceof AssertingScorable) {
46+
if (in instanceof AssertingScorable || in instanceof AssertingScorer) {
4847
return in;
4948
}
49+
5050
// If `in` is Scorer, we need to wrap it as a Scorer instead of Scorable because
5151
// NumericComparator uses the iterator cost of a Scorer in sort optimization.
52-
if (in instanceof Scorer) {
53-
return new WrappedScorer((Scorer) in);
52+
if (in instanceof Scorer scorer) {
53+
return AssertingScorer.wrap(scorer, true, true);
5454
} else {
5555
return new AssertingScorable(in);
5656
}
5757
}
58-
59-
private static class WrappedScorer extends FilterScorer {
60-
WrappedScorer(Scorer in) {
61-
super(in);
62-
}
63-
64-
@Override
65-
public float score() throws IOException {
66-
return new AssertingScorable(in).score();
67-
}
68-
69-
@Override
70-
public void setMinCompetitiveScore(float minScore) throws IOException {
71-
in.setMinCompetitiveScore(minScore);
72-
}
73-
74-
@Override
75-
public float getMaxScore(int upTo) throws IOException {
76-
return in.getMaxScore(upTo);
77-
}
78-
}
79-
80-
public static Scorable unwrap(Scorable in) {
81-
while (true) {
82-
if (in instanceof AssertingScorable) in = ((AssertingScorable) in).in;
83-
else if (in instanceof AssertingScorer) in = ((AssertingScorer) in).in;
84-
else if (in instanceof WrappedScorer) in = ((WrappedScorer) in).unwrap();
85-
else return in;
86-
}
87-
}
8858
}

lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import java.io.IOException;
2020
import java.util.Collection;
2121
import java.util.Collections;
22-
import java.util.Random;
2322
import org.apache.lucene.search.DocIdSetIterator;
2423
import org.apache.lucene.search.FilterDocIdSetIterator;
25-
import org.apache.lucene.search.ScoreMode;
2624
import org.apache.lucene.search.Scorer;
2725
import org.apache.lucene.search.TwoPhaseIterator;
2826
import org.apache.lucene.util.FixedBitSet;
@@ -37,31 +35,37 @@ enum IteratorState {
3735
FINISHED
3836
};
3937

40-
public static Scorer wrap(
41-
Random random, Scorer other, ScoreMode scoreMode, boolean canCallMinCompetitiveScore) {
38+
/**
39+
* Wrap the given scorer with additional checks.
40+
*
41+
* @param other the scorer
42+
* @param canScore whether the scorer is allowed to compute scores, typically true for scoring
43+
* clauses of the query if the collector needs scores
44+
* @param canSetMinCompetitiveScore whether the scorer is allowed to set min competitive scores,
45+
* typically true if the score mode is TOP_SCORES and this scorer is the top-level scoring
46+
* clause
47+
*/
48+
public static Scorer wrap(Scorer other, boolean canScore, boolean canSetMinCompetitiveScore) {
4249
if (other == null) {
4350
return null;
4451
}
45-
return new AssertingScorer(random, other, scoreMode, canCallMinCompetitiveScore);
52+
return new AssertingScorer(other, canScore, canSetMinCompetitiveScore);
4653
}
4754

48-
final Random random;
4955
final Scorer in;
50-
final ScoreMode scoreMode;
51-
final boolean canCallMinCompetitiveScore;
56+
final boolean canScore;
57+
final boolean canSetMinCompetitiveScore;
5258

5359
IteratorState state = IteratorState.ITERATING;
5460
int doc;
5561
float minCompetitiveScore = 0;
5662
int lastShallowTarget = -1;
5763

58-
private AssertingScorer(
59-
Random random, Scorer in, ScoreMode scoreMode, boolean canCallMinCompetitiveScore) {
60-
this.random = random;
64+
private AssertingScorer(Scorer in, boolean canScore, boolean canSetMinCompetitiveScore) {
6165
this.in = in;
62-
this.scoreMode = scoreMode;
66+
this.canScore = canScore;
67+
this.canSetMinCompetitiveScore = canSetMinCompetitiveScore;
6368
this.doc = in.docID();
64-
this.canCallMinCompetitiveScore = canCallMinCompetitiveScore;
6569
}
6670

6771
public Scorer getIn() {
@@ -81,8 +85,8 @@ boolean iterating() {
8185

8286
@Override
8387
public void setMinCompetitiveScore(float score) throws IOException {
84-
assert scoreMode == ScoreMode.TOP_SCORES;
85-
assert canCallMinCompetitiveScore;
88+
assert canScore;
89+
assert canSetMinCompetitiveScore;
8690
assert Float.isNaN(score) == false;
8791
assert score >= minCompetitiveScore;
8892
in.setMinCompetitiveScore(score);
@@ -91,7 +95,7 @@ public void setMinCompetitiveScore(float score) throws IOException {
9195

9296
@Override
9397
public int advanceShallow(int target) throws IOException {
94-
assert scoreMode.needsScores();
98+
assert canScore;
9599
assert target >= lastShallowTarget
96100
: "called on decreasing targets: target = "
97101
+ target
@@ -107,7 +111,7 @@ public int advanceShallow(int target) throws IOException {
107111

108112
@Override
109113
public float getMaxScore(int upTo) throws IOException {
110-
assert scoreMode.needsScores();
114+
assert canScore;
111115
assert upTo >= lastShallowTarget : "uTo = " + upTo + " < last target = " + lastShallowTarget;
112116
assert docID() >= 0 || lastShallowTarget >= 0
113117
: "Cannot get max scores until the iterator is positioned or advanceShallow has been called";
@@ -117,7 +121,7 @@ assert docID() >= 0 || lastShallowTarget >= 0
117121

118122
@Override
119123
public float score() throws IOException {
120-
assert scoreMode.needsScores();
124+
assert canScore;
121125
assert iterating() : state;
122126
final float score = in.score();
123127
assert !Float.isNaN(score) : "NaN score for in=" + in;

lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public Scorer get(long leadCost) throws IOException {
7171
assert getCalled == false;
7272
getCalled = true;
7373
assert leadCost >= 0 : leadCost;
74+
boolean canScore = scoreMode.needsScores();
75+
boolean canSetMinCompetitiveScore =
76+
scoreMode == ScoreMode.TOP_SCORES && topLevelScoringClause;
7477
return AssertingScorer.wrap(
75-
new Random(random.nextLong()),
76-
inScorerSupplier.get(leadCost),
77-
scoreMode,
78-
topLevelScoringClause);
78+
inScorerSupplier.get(leadCost), canScore, canSetMinCompetitiveScore);
7979
}
8080

8181
@Override

0 commit comments

Comments
 (0)