Apache Lucene open-source search software
LUCENE-6182: spatial refactor VisitorTemplate.visitScanned needn't be abstract. And have collectDocs specify BitSet not FixedBitSet. (these are internal APIs)
David Wayne Smiley4a17f2318291
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractPrefixTreeFilter.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractPrefixTreeFilter.javaindex 71d36935a42..6a2e2834d9f 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractPrefixTreeFilter.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractPrefixTreeFilter.java@@ -17,23 +17,21 @@ package org.apache.lucene.spatial.prefix;* limitations under the License.*/+import java.io.IOException;+import com.spatial4j.core.shape.Shape;+import org.apache.lucene.index.DocsEnum;import org.apache.lucene.index.LeafReader;import org.apache.lucene.index.LeafReaderContext;-import org.apache.lucene.index.DocsEnum;import org.apache.lucene.index.Terms;import org.apache.lucene.index.TermsEnum;-import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.search.Filter;import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;+import org.apache.lucene.util.BitSet;import org.apache.lucene.util.Bits;-import org.apache.lucene.util.FixedBitSet;--import java.io.IOException;/*** Base class for Lucene Filters on SpatialPrefixTree fields.- ** @lucene.experimental*/public abstract class AbstractPrefixTreeFilter extends Filter {@@ -73,14 +71,15 @@ public abstract class AbstractPrefixTreeFilter extends Filter {}/** Holds transient state and docid collecting utility methods as part of- * traversing a {@link TermsEnum}. */- public abstract class BaseTermsEnumTraverser {+ * traversing a {@link TermsEnum} for a {@link org.apache.lucene.index.LeafReaderContext}. */+ public abstract class BaseTermsEnumTraverser {//TODO rename to LeafTermsEnumTraverser ?+ //note: only 'fieldName' (accessed in constructor) keeps this from being a static inner classprotected final LeafReaderContext context;protected Bits acceptDocs;protected final int maxDoc;- protected TermsEnum termsEnum;//remember to check for null in getDocIdSet+ protected TermsEnum termsEnum;//remember to check for null!protected DocsEnum docsEnum;public BaseTermsEnumTraverser(LeafReaderContext context, Bits acceptDocs) throws IOException {@@ -93,32 +92,12 @@ public abstract class AbstractPrefixTreeFilter extends Filter {this.termsEnum = terms.iterator(null);}- protected void collectDocs(FixedBitSet bitSet) throws IOException {- //WARN: keep this specialization in sync+ protected void collectDocs(BitSet bitSet) throws IOException {assert termsEnum != null;docsEnum = termsEnum.docs(acceptDocs, docsEnum, DocsEnum.FLAG_NONE);- int docid;- while ((docid = docsEnum.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- bitSet.set(docid);- }+ bitSet.or(docsEnum);}- /* Eventually uncomment when needed.-- protected void collectDocs(Collector collector) throws IOException {- //WARN: keep this specialization in sync- assert termsEnum != null;- docsEnum = termsEnum.docs(acceptDocs, docsEnum, DocsEnum.FLAG_NONE);- int docid;- while ((docid = docsEnum.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- collector.collect(docid);- }- }-- public abstract class Collector {- abstract void collect(int docid) throws IOException;- }- */}}diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractVisitingPrefixTreeFilter.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractVisitingPrefixTreeFilter.javaindex 5063860022c..fed0dc728aa 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractVisitingPrefixTreeFilter.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/AbstractVisitingPrefixTreeFilter.java@@ -17,6 +17,9 @@ package org.apache.lucene.spatial.prefix;* limitations under the License.*/+import java.io.IOException;+import java.util.Iterator;+import com.spatial4j.core.shape.Shape;import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.TermsEnum;@@ -27,9 +30,6 @@ import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;-import java.io.IOException;-import java.util.Iterator;-/*** Traverses a {@link SpatialPrefixTree} indexed field, using the template and* visitor design patterns for subclasses to guide the traversal and collect@@ -326,17 +326,21 @@ public abstract class AbstractVisitingPrefixTreeFilter extends AbstractPrefixTreprotected abstract DocIdSet finish() throws IOException;/**- * Visit an indexed cell returned from- * {@link #findSubCellsToVisit(org.apache.lucene.spatial.prefix.tree.Cell)}.+ * Visit an indexed non-leaf cell returned from+ * {@link #findSubCellsToVisit(org.apache.lucene.spatial.prefix.tree.Cell)}+ * that is also found in the index.+ * It will also be called by the default implementation of+ * {@link #visitScanned(org.apache.lucene.spatial.prefix.tree.Cell)} for+ * cells at the bottom detail level.*- * @param cell An intersecting cell.+ * @param cell An intersecting cell; not a leaf.* @return true to descend to more levels. It is an error to return true* if cell.level == detailLevel*/protected abstract boolean visit(Cell cell) throws IOException;/**- * Called after visit() returns true and an indexed leaf cell is found. An+ * Called when an indexed leaf cell is found. An* indexed leaf cell means associated documents generally won't be found at* further detail levels.*/@@ -345,8 +349,19 @@ public abstract class AbstractVisitingPrefixTreeFilter extends AbstractPrefixTre/*** The cell is either indexed as a leaf or is the last level of detail. It* might not even intersect the query shape, so be sure to check for that.+ * The default implementation will check that and if passes then call+ * {@link #visitLeaf(org.apache.lucene.spatial.prefix.tree.Cell)} or+ * {@link #visit(org.apache.lucene.spatial.prefix.tree.Cell)}.*/- protected abstract void visitScanned(Cell cell) throws IOException;+ protected void visitScanned(Cell cell) throws IOException {+ if (queryShape.relate(cell.getShape()).intersects()) {+ if (cell.isLeaf()) {+ visitLeaf(cell);+ } else {+ visit(cell);+ }+ }+ }protected void preSiblings(VNode vNode) throws IOException {}diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/IntersectsPrefixTreeFilter.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/IntersectsPrefixTreeFilter.javaindex c162f724521..bf5544da8e6 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/IntersectsPrefixTreeFilter.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/IntersectsPrefixTreeFilter.java@@ -17,19 +17,18 @@ package org.apache.lucene.spatial.prefix;* limitations under the License.*/+import java.io.IOException;+import com.spatial4j.core.shape.Shape;import com.spatial4j.core.shape.SpatialRelation;-import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.search.DocIdSet;import org.apache.lucene.spatial.prefix.tree.Cell;import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;-import org.apache.lucene.util.Bits;import org.apache.lucene.util.BitDocIdSet;+import org.apache.lucene.util.Bits;import org.apache.lucene.util.FixedBitSet;-import java.io.IOException;-/*** A Filter matching documents that have an {@link SpatialRelation#INTERSECTS}* (i.e. not DISTINCT) relationship with a provided query shape.@@ -90,12 +89,6 @@ public class IntersectsPrefixTreeFilter extends AbstractVisitingPrefixTreeFiltercollectDocs(results);}- @Override- protected void visitScanned(Cell cell) throws IOException {- if (queryShape.relate(cell.getShape()).intersects())- collectDocs(results);- }-}.getDocIdSet();}diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/WithinPrefixTreeFilter.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/WithinPrefixTreeFilter.javaindex 4191a1f106b..c245aed0c67 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/WithinPrefixTreeFilter.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/WithinPrefixTreeFilter.java@@ -19,15 +19,6 @@ package org.apache.lucene.spatial.prefix;import java.io.IOException;-import org.apache.lucene.index.LeafReaderContext;-import org.apache.lucene.search.DocIdSet;-import org.apache.lucene.spatial.prefix.tree.Cell;-import org.apache.lucene.spatial.prefix.tree.CellIterator;-import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;-import org.apache.lucene.util.Bits;-import org.apache.lucene.util.BitDocIdSet;-import org.apache.lucene.util.FixedBitSet;-import com.spatial4j.core.context.SpatialContext;import com.spatial4j.core.distance.DistanceUtils;import com.spatial4j.core.shape.Circle;@@ -35,6 +26,14 @@ import com.spatial4j.core.shape.Point;import com.spatial4j.core.shape.Rectangle;import com.spatial4j.core.shape.Shape;import com.spatial4j.core.shape.SpatialRelation;+import org.apache.lucene.index.LeafReaderContext;+import org.apache.lucene.search.DocIdSet;+import org.apache.lucene.spatial.prefix.tree.Cell;+import org.apache.lucene.spatial.prefix.tree.CellIterator;+import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;+import org.apache.lucene.util.BitDocIdSet;+import org.apache.lucene.util.Bits;+import org.apache.lucene.util.FixedBitSet;/*** Finds docs where its indexed shape is {@link org.apache.lucene.spatial.query.SpatialOperation#IsWithin@@ -165,7 +164,10 @@ public class WithinPrefixTreeFilter extends AbstractVisitingPrefixTreeFilter {@Overrideprotected void visitLeaf(Cell cell) throws IOException {- //visitRelation is declared as a field, populated by visit() so we don't recompute it+ //visitRelation is declared as a field, populated by visit() so we don't recompute it.+ // We have a specialized visitScanned() which doesn't call this. If we didn't, we would+ // not be able to assume visitRelation is from a prior visit() call since in scanning,+ // parent cells aren't visited.assert detailLevel != cell.getLevel();assert visitRelation == cell.getShape().relate(queryShape);if (allCellsIntersectQuery(cell, visitRelation))@@ -199,6 +201,7 @@ public class WithinPrefixTreeFilter extends AbstractVisitingPrefixTreeFilter {@Overrideprotected void visitScanned(Cell cell) throws IOException {+ //slightly optimize over default impl; required for our 'visitRelation' field re-use aboveif (allCellsIntersectQuery(cell, null)) {collectDocs(inside);} else {