You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2010/11/15 19:41:26 UTC

svn commit: r1035397 - in /lucene/dev/trunk/lucene: ./ src/java/org/apache/lucene/search/spans/

Author: rmuir
Date: Mon Nov 15 18:41:26 2010
New Revision: 1035397

URL: http://svn.apache.org/viewvc?rev=1035397&view=rev
Log:
LUCENE-2760: Optimize SpanFirstQuery,SpanPositionRangeQuery

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1035397&r1=1035396&r2=1035397&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Mon Nov 15 18:41:26 2010
@@ -801,6 +801,9 @@ Optimizations
   by the improved SorterTemplate class.
   (Uwe Schindler, Robert Muir, Mike McCandless)
 
+* LUCENE-2760: Optimize SpanFirstQuery and SpanPositionRangeQuery.
+  (Robert Muir)
+
 Build
 
 * LUCENE-2124: Moved the JDK-based collation support from contrib/collation 

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java?rev=1035397&r1=1035396&r2=1035397&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java Mon Nov 15 18:41:26 2010
@@ -37,8 +37,14 @@ public class SpanFirstQuery extends Span
   }
 
   @Override
-  protected boolean acceptPosition(Spans spans) throws IOException {
-    return spans.end() <= end;
+  protected AcceptStatus acceptPosition(Spans spans) throws IOException {
+    assert spans.start() != spans.end();
+    if (spans.start() >= end)
+      return AcceptStatus.NO_AND_ADVANCE;
+    else if (spans.end() <= end)
+      return AcceptStatus.YES;
+    else
+      return AcceptStatus.NO;
   }
 
 

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java?rev=1035397&r1=1035396&r2=1035397&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java Mon Nov 15 18:41:26 2010
@@ -43,7 +43,7 @@ public class SpanNearPayloadCheckQuery e
   }
 
   @Override
-  protected boolean acceptPosition(Spans spans) throws IOException {
+  protected AcceptStatus acceptPosition(Spans spans) throws IOException {
     boolean result = spans.isPayloadAvailable();
     if (result == true) {
       Collection<byte[]> candidate = spans.getPayload();
@@ -62,15 +62,15 @@ public class SpanNearPayloadCheckQuery e
         }
         if (matches == payloadToMatch.size()){
           //we've verified all the bytes
-          return true;
+          return AcceptStatus.YES;
         } else {
-          return false;
+          return AcceptStatus.NO;
         }
       } else {
-        return false;
+        return AcceptStatus.NO;
       }
     }
-    return false;
+    return AcceptStatus.NO;
   }
 
   public String toString(String field) {

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java?rev=1035397&r1=1035396&r2=1035397&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java Mon Nov 15 18:41:26 2010
@@ -50,7 +50,7 @@ public class SpanPayloadCheckQuery exten
   }
 
   @Override
-  protected boolean acceptPosition(Spans spans) throws IOException {
+  protected AcceptStatus acceptPosition(Spans spans) throws IOException {
     boolean result = spans.isPayloadAvailable();
     if (result == true){
       Collection<byte[]> candidate = spans.getPayload();
@@ -62,16 +62,16 @@ public class SpanPayloadCheckQuery exten
         for (byte[] candBytes : candidate) {
           //if one is a mismatch, then return false
           if (Arrays.equals(candBytes, toMatchIter.next()) == false){
-            return false;
+            return AcceptStatus.NO;
           }
         }
         //we've verified all the bytes
-        return true;
+        return AcceptStatus.YES;
       } else {
-        return false;
+        return AcceptStatus.NO;
       }
     }
-    return result;
+    return AcceptStatus.YES;
   } 
 
   public String toString(String field) {

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java?rev=1035397&r1=1035396&r2=1035397&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java Mon Nov 15 18:41:26 2010
@@ -57,6 +57,12 @@ public abstract class SpanPositionCheckQ
 	    match.extractTerms(terms);
   }
 
+  /** Return value if the match should be accepted {@code YES}, rejected {@code NO},
+   * or rejected and enumeration should advance to the next document {@code NO_AND_ADVANCE}.
+   * @see #acceptPosition(Spans)
+   */
+  protected static enum AcceptStatus { YES, NO, NO_AND_ADVANCE };
+  
   /**
    * Implementing classes are required to return whether the current position is a match for the passed in
    * "match" {@link org.apache.lucene.search.spans.SpanQuery}.
@@ -66,36 +72,12 @@ public abstract class SpanPositionCheckQ
    *
    *
    * @param spans The {@link org.apache.lucene.search.spans.Spans} instance, positioned at the spot to check
-   * @return true if it is a match, else false.
+   * @return whether the match is accepted, rejected, or rejected and should move to the next doc.
    *
    * @see org.apache.lucene.search.spans.Spans#next()
    *
    */
-  protected abstract boolean acceptPosition(Spans spans) throws IOException;
-
-  /**
-   * Implementing classes are required to return whether the position at the target is someplace that
-   * can be skipped to.  For instance, the {@link org.apache.lucene.search.spans.SpanFirstQuery} returns
-   * false if the target position is beyond the maximum position allowed or if {@link Spans#next()} is true.
-   * <p/>
-   * Note, this method is only called if the underlying match {@link org.apache.lucene.search.spans.SpanQuery} can
-   * skip to the target.
-   * <p/>
-   * It is safe to assume that the passed in {@link org.apache.lucene.search.spans.Spans} object for the underlying {@link org.apache.lucene.search.spans.SpanQuery} is
-   * positioned at the target.
-   * <p/>
-   * The default implementation is to return true if either {@link #acceptPosition(Spans)} or {@link org.apache.lucene.search.spans.Spans#next()} is true for the
-   * passed in instance of Spans.
-   *<p/>
-   * @param spans The {@link org.apache.lucene.search.spans.Spans} to check
-   * @return true if the instance can skip to this position
-   *
-   * @see Spans#skipTo(int)
-   * @throws java.io.IOException if there is a low-level IO error
-   */
-  protected boolean acceptSkipTo(Spans spans) throws IOException{
-    return acceptPosition(spans) || spans.next();
-  }
+  protected abstract AcceptStatus acceptPosition(Spans spans) throws IOException;
 
   @Override
   public Spans getSpans(final IndexReader reader) throws IOException {
@@ -123,21 +105,16 @@ public abstract class SpanPositionCheckQ
   protected class PositionCheckSpan extends Spans {
     private Spans spans;
 
-    private final IndexReader reader;
-
     public PositionCheckSpan(IndexReader reader) throws IOException {
-      this.reader = reader;
       spans = match.getSpans(reader);
     }
 
     @Override
     public boolean next() throws IOException {
-      //TODO: optimize to skip ahead to start
-      while (spans.next()) {                  // scan to next match
-        if (acceptPosition(this))
-          return true;
-      }
-      return false;
+      if (!spans.next())
+        return false;
+      
+      return doNext();
     }
 
     @Override
@@ -145,8 +122,23 @@ public abstract class SpanPositionCheckQ
       if (!spans.skipTo(target))
         return false;
 
-      return acceptSkipTo(this);
-
+      return doNext();
+    }
+    
+    protected boolean doNext() throws IOException {
+      for (;;) {
+        switch(acceptPosition(this)) {
+          case YES: return true;
+          case NO: 
+            if (!spans.next()) 
+              return false;
+            break;
+          case NO_AND_ADVANCE: 
+            if (!spans.skipTo(spans.doc()+1)) 
+              return false;
+            break;
+        }
+      }
     }
 
     @Override

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java?rev=1035397&r1=1035396&r2=1035397&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java Mon Nov 15 18:41:26 2010
@@ -39,8 +39,14 @@ public class SpanPositionRangeQuery exte
 
 
   @Override
-  protected boolean acceptPosition(Spans spans) throws IOException {
-    return spans.start() >= start && spans.end() <= end;
+  protected AcceptStatus acceptPosition(Spans spans) throws IOException {
+    assert spans.start() != spans.end();
+    if (spans.start() >= end)
+      return AcceptStatus.NO_AND_ADVANCE;
+    else if (spans.start() >= start && spans.end() <= end)
+      return AcceptStatus.YES;
+    else
+      return AcceptStatus.NO;
   }