LUCENE-10597: move globalMaxScore to MaxScoreCache (#931)

This commit is contained in:
Tomoko Uchida 2022-06-02 10:20:25 +09:00 committed by GitHub
parent a383253fe1
commit ee99a2a452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 12 deletions

View File

@ -34,7 +34,6 @@ public final class ImpactsDISI extends DocIdSetIterator {
private final DocIdSetIterator in;
private final ImpactsSource impactsSource;
private final MaxScoreCache maxScoreCache;
private final float globalMaxScore;
private float minCompetitiveScore = 0;
private int upTo = DocIdSetIterator.NO_MORE_DOCS;
private float maxScore = Float.MAX_VALUE;
@ -50,7 +49,6 @@ public final class ImpactsDISI extends DocIdSetIterator {
this.in = in;
this.impactsSource = impactsSource;
this.maxScoreCache = new MaxScoreCache(impactsSource, scorer);
this.globalMaxScore = scorer.score(Float.MAX_VALUE, 1L);
}
/**
@ -88,12 +86,7 @@ public final class ImpactsDISI extends DocIdSetIterator {
* @see Scorer#getMaxScore(int)
*/
public float getMaxScore(int upTo) throws IOException {
final int level = maxScoreCache.getLevel(upTo);
if (level == -1) {
return globalMaxScore;
} else {
return maxScoreCache.getMaxScoreForLevel(level);
}
return maxScoreCache.getMaxScore(upTo);
}
private int advanceTarget(int target) throws IOException {
@ -104,7 +97,7 @@ public final class ImpactsDISI extends DocIdSetIterator {
}
upTo = advanceShallow(target);
maxScore = maxScoreCache.getMaxScoreForLevel(0);
maxScore = maxScoreCache.getMaxScoreForLevelZero();
while (true) {
assert upTo >= target;
@ -126,7 +119,7 @@ public final class ImpactsDISI extends DocIdSetIterator {
target = skipUpTo + 1;
}
upTo = advanceShallow(target);
maxScore = maxScoreCache.getMaxScoreForLevel(0);
maxScore = maxScoreCache.getMaxScoreForLevelZero();
}
}

View File

@ -35,6 +35,7 @@ final class MaxScoreCache {
private final ImpactsSource impactsSource;
private final SimScorer scorer;
private final float globalMaxScore;
private float[] maxScoreCache;
private int[] maxScoreCacheUpTo;
@ -42,6 +43,7 @@ final class MaxScoreCache {
public MaxScoreCache(ImpactsSource impactsSource, SimScorer scorer) {
this.impactsSource = impactsSource;
this.scorer = scorer;
this.globalMaxScore = scorer.score(Float.MAX_VALUE, 1L);
maxScoreCache = new float[0];
maxScoreCacheUpTo = new int[0];
}
@ -63,11 +65,19 @@ final class MaxScoreCache {
return maxScore;
}
float getMaxScore(int upTo) throws IOException {
final int level = getLevel(upTo);
if (level == -1) {
return globalMaxScore;
}
return getMaxScoreForLevel(level);
}
/**
* Return the first level that includes all doc IDs up to {@code upTo}, or -1 if there is no such
* level.
*/
int getLevel(int upTo) throws IOException {
private int getLevel(int upTo) throws IOException {
final Impacts impacts = impactsSource.getImpacts();
for (int level = 0, numLevels = impacts.numLevels(); level < numLevels; ++level) {
final int impactsUpTo = impacts.getDocIdUpTo(level);
@ -78,8 +88,13 @@ final class MaxScoreCache {
return -1;
}
float getMaxScoreForLevelZero() throws IOException {
return getMaxScoreForLevel(0);
}
/** Return the maximum score for the given {@code level}. */
float getMaxScoreForLevel(int level) throws IOException {
private float getMaxScoreForLevel(int level) throws IOException {
assert level >= 0 : "level must not be a negative integer; got " + level;
final Impacts impacts = impactsSource.getImpacts();
ensureCacheSize(level + 1);
final int levelUpTo = impacts.getDocIdUpTo(level);