You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by to...@apache.org on 2022/06/02 01:20:32 UTC

[lucene] branch main updated: LUCENE-10597: move globalMaxScore to MaxScoreCache (#931)

This is an automated email from the ASF dual-hosted git repository.

tomoko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new ee99a2a4526 LUCENE-10597: move globalMaxScore to MaxScoreCache (#931)
ee99a2a4526 is described below

commit ee99a2a45261be6c0492bc1decfd5432787b650a
Author: Tomoko Uchida <to...@gmail.com>
AuthorDate: Thu Jun 2 10:20:25 2022 +0900

    LUCENE-10597: move globalMaxScore to MaxScoreCache (#931)
---
 .../java/org/apache/lucene/search/ImpactsDISI.java    | 13 +++----------
 .../java/org/apache/lucene/search/MaxScoreCache.java  | 19 +++++++++++++++++--
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/search/ImpactsDISI.java b/lucene/core/src/java/org/apache/lucene/search/ImpactsDISI.java
index 843bb4ad57b..20efacc25c9 100644
--- a/lucene/core/src/java/org/apache/lucene/search/ImpactsDISI.java
+++ b/lucene/core/src/java/org/apache/lucene/search/ImpactsDISI.java
@@ -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();
     }
   }
 
diff --git a/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java b/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java
index d2ef78067e5..70a10ee0561 100644
--- a/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java
+++ b/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java
@@ -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);