You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2013/10/17 18:27:25 UTC

svn commit: r1533135 - in /lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search: DisjunctionMaxQuery.java DisjunctionMaxScorer.java DisjunctionScorer.java DisjunctionSumScorer.java

Author: mikemccand
Date: Thu Oct 17 16:27:25 2013
New Revision: 1533135

URL: http://svn.apache.org/r1533135
Log:
LUCENE-5292: remove code dup

Modified:
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxScorer.java
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionScorer.java
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionSumScorer.java

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java?rev=1533135&r1=1533134&r2=1533135&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java Thu Oct 17 16:27:25 2013
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.lucene.index.AtomicReaderContext;
@@ -154,17 +155,20 @@ public class DisjunctionMaxQuery extends
     @Override
     public Scorer scorer(AtomicReaderContext context, boolean scoreDocsInOrder,
         boolean topScorer, Bits acceptDocs) throws IOException {
-      Scorer[] scorers = new Scorer[weights.size()];
-      int idx = 0;
+      List<Scorer> scorers = new ArrayList<Scorer>();
       for (Weight w : weights) {
         // we will advance() subscorers
         Scorer subScorer = w.scorer(context, true, false, acceptDocs);
         if (subScorer != null) {
-          scorers[idx++] = subScorer;
+          scorers.add(subScorer);
+
         }
       }
-      if (idx == 0) return null; // all scorers did not have documents
-      DisjunctionMaxScorer result = new DisjunctionMaxScorer(this, tieBreakerMultiplier, scorers, idx);
+      if (scorers.isEmpty()) {
+        // no sub-scorers had any documents
+        return null;
+      }
+      DisjunctionMaxScorer result = new DisjunctionMaxScorer(this, tieBreakerMultiplier, scorers.toArray(new Scorer[scorers.size()]));
       return result;
     }
 

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxScorer.java?rev=1533135&r1=1533134&r2=1533135&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxScorer.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxScorer.java Thu Oct 17 16:27:25 2013
@@ -27,7 +27,6 @@ import java.io.IOException;
 class DisjunctionMaxScorer extends DisjunctionScorer {
   /* Multiplier applied to non-maximum-scoring subqueries for a document as they are summed into the result. */
   private final float tieBreakerMultiplier;
-  private int doc = -1;
   private int freq = -1;
 
   /* Used when scoring currently matching doc. */
@@ -44,40 +43,13 @@ class DisjunctionMaxScorer extends Disju
    *          document as they are summed into the result.
    * @param subScorers
    *          The sub scorers this Scorer should iterate on
-   * @param numScorers
-   *          The actual number of scorers to iterate on. Note that the array's
-   *          length may be larger than the actual number of scorers.
    */
   public DisjunctionMaxScorer(Weight weight, float tieBreakerMultiplier,
-      Scorer[] subScorers, int numScorers) {
-    super(weight, subScorers, numScorers);
+      Scorer[] subScorers) {
+    super(weight, subScorers);
     this.tieBreakerMultiplier = tieBreakerMultiplier;
   }
 
-  @Override
-  public int nextDoc() throws IOException {
-    assert doc != NO_MORE_DOCS;
-    while(true) {
-      if (subScorers[0].nextDoc() != NO_MORE_DOCS) {
-        heapAdjust(0);
-      } else {
-        heapRemoveRoot();
-        if (numScorers == 0) {
-          return doc = NO_MORE_DOCS;
-        }
-      }
-      if (subScorers[0].docID() != doc) {
-        afterNext();
-        return doc;
-      }
-    }
-  }
-
-  @Override
-  public int docID() {
-    return doc;
-  }
-
   /** Determine the current document score.  Initially invalid, until {@link #nextDoc()} is called the first time.
    * @return the score of the current generated document
    */
@@ -86,7 +58,8 @@ class DisjunctionMaxScorer extends Disju
     return scoreMax + (scoreSum - scoreMax) * tieBreakerMultiplier;
   }
   
-  private void afterNext() throws IOException {
+  @Override
+  protected void afterNext() throws IOException {
     doc = subScorers[0].docID();
     if (doc != NO_MORE_DOCS) {
       scoreSum = scoreMax = subScorers[0].score();
@@ -112,23 +85,4 @@ class DisjunctionMaxScorer extends Disju
   public int freq() throws IOException {
     return freq;
   }
-
-  @Override
-  public int advance(int target) throws IOException {
-    assert doc != NO_MORE_DOCS;
-    while(true) {
-      if (subScorers[0].advance(target) != NO_MORE_DOCS) {
-        heapAdjust(0);
-      } else {
-        heapRemoveRoot();
-        if (numScorers == 0) {
-          return doc = NO_MORE_DOCS;
-        }
-      }
-      if (subScorers[0].docID() >= target) {
-        afterNext();
-        return doc;
-      }
-    }
-  }
 }

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionScorer.java?rev=1533135&r1=1533134&r2=1533135&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionScorer.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionScorer.java Thu Oct 17 16:27:25 2013
@@ -17,6 +17,7 @@ package org.apache.lucene.search;
  * limitations under the License.
  */
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 
@@ -26,12 +27,14 @@ import java.util.Collection;
  */
 abstract class DisjunctionScorer extends Scorer {
   protected final Scorer subScorers[];
+  /** The document number of the current match. */
+  protected int doc = -1;
   protected int numScorers;
   
-  protected DisjunctionScorer(Weight weight, Scorer subScorers[], int numScorers) {
+  protected DisjunctionScorer(Weight weight, Scorer subScorers[]) {
     super(weight);
     this.subScorers = subScorers;
-    this.numScorers = numScorers;
+    this.numScorers = subScorers.length;
     heapify();
   }
   
@@ -114,4 +117,59 @@ abstract class DisjunctionScorer extends
     }
     return sum;
   } 
+  
+  @Override
+  public int docID() {
+   return doc;
+  }
+ 
+  @Override
+  public int nextDoc() throws IOException {
+    assert doc != NO_MORE_DOCS;
+    while(true) {
+      if (subScorers[0].nextDoc() != NO_MORE_DOCS) {
+        heapAdjust(0);
+      } else {
+        heapRemoveRoot();
+        if (numScorers == 0) {
+          return doc = NO_MORE_DOCS;
+        }
+      }
+      if (subScorers[0].docID() != doc) {
+        afterNext();
+        return doc;
+      }
+    }
+  }
+  
+  @Override
+  public int advance(int target) throws IOException {
+    assert doc != NO_MORE_DOCS;
+    while(true) {
+      if (subScorers[0].advance(target) != NO_MORE_DOCS) {
+        heapAdjust(0);
+      } else {
+        heapRemoveRoot();
+        if (numScorers == 0) {
+          return doc = NO_MORE_DOCS;
+        }
+      }
+      if (subScorers[0].docID() >= target) {
+        afterNext();
+        return doc;
+      }
+    }
+  }
+  
+  /** 
+   * Called after next() or advance() land on a new document.
+   * <p>
+   * {@code subScorers[0]} will be positioned to the new docid,
+   * which could be {@code NO_MORE_DOCS} (subclass must handle this).
+   * <p>
+   * implementations should assign {@code doc} appropriately, and do any
+   * other work necessary to implement {@code score()} and {@code freq()}
+   */
+  // TODO: make this less horrible
+  protected abstract void afterNext() throws IOException;
 }

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionSumScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionSumScorer.java?rev=1533135&r1=1533134&r2=1533135&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionSumScorer.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/DisjunctionSumScorer.java Thu Oct 17 16:27:25 2013
@@ -23,8 +23,6 @@ import java.io.IOException;
  * This Scorer implements {@link Scorer#advance(int)} and uses advance() on the given Scorers. 
  */
 class DisjunctionSumScorer extends DisjunctionScorer { 
-  /** The document number of the current match. */
-  private int doc = -1;
 
   /** The number of subscorers that provide the current match. */
   protected int nrMatchers = -1;
@@ -38,34 +36,16 @@ class DisjunctionSumScorer extends Disju
    * @param coord Table of coordination factors
    */
   DisjunctionSumScorer(Weight weight, Scorer[] subScorers, float[] coord) throws IOException {
-    super(weight, subScorers, subScorers.length);
+    super(weight, subScorers);
 
     if (numScorers <= 1) {
       throw new IllegalArgumentException("There must be at least 2 subScorers");
     }
     this.coord = coord;
   }
-
-  @Override
-  public int nextDoc() throws IOException {
-    assert doc != NO_MORE_DOCS;
-    while(true) {
-      if (subScorers[0].nextDoc() != NO_MORE_DOCS) {
-        heapAdjust(0);
-      } else {
-        heapRemoveRoot();
-        if (numScorers == 0) {
-          return doc = NO_MORE_DOCS;
-        }
-      }
-      if (subScorers[0].docID() != doc) {
-        afterNext();
-        return doc;
-      }
-    }
-  }
   
-  private void afterNext() throws IOException {
+  @Override
+  protected void afterNext() throws IOException {
     final Scorer sub = subScorers[0];
     doc = sub.docID();
     if (doc != NO_MORE_DOCS) {
@@ -96,43 +76,9 @@ class DisjunctionSumScorer extends Disju
   public float score() throws IOException { 
     return (float)score * coord[nrMatchers]; 
   }
-   
-  @Override
-  public int docID() {
-    return doc;
-  }
 
   @Override
   public int freq() throws IOException {
     return nrMatchers;
   }
-
-  /**
-   * Advances to the first match beyond the current whose document number is
-   * greater than or equal to a given target. <br>
-   * The implementation uses the advance() method on the subscorers.
-   * 
-   * @param target
-   *          The target document number.
-   * @return the document whose number is greater than or equal to the given
-   *         target, or -1 if none exist.
-   */
-  @Override
-  public int advance(int target) throws IOException {
-    assert doc != NO_MORE_DOCS;
-    while(true) {
-      if (subScorers[0].advance(target) != NO_MORE_DOCS) {
-        heapAdjust(0);
-      } else {
-        heapRemoveRoot();
-        if (numScorers == 0) {
-          return doc = NO_MORE_DOCS;
-        }
-      }
-      if (subScorers[0].docID() >= target) {
-        afterNext();
-        return doc;
-      }
-    }
-  }
 }