Apache Lucene open-source search software
LUCENE-5608: Move readCell() back to SPT from Cell.
David Wayne Smiley449600db8281
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 992f78ff5f0..a72d08a71c8 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@@ -128,7 +128,7 @@ public abstract class AbstractVisitingPrefixTreeFilter extends AbstractPrefixTreprivate VNode curVNode;//current pointer, derived from query shapeprivate BytesRef curVNodeTerm = new BytesRef();//curVNode.cell's term, without leaf- private Cell scanCell = grid.getWorldCell();+ private Cell scanCell;private BytesRef thisTerm;//the result of termsEnum.term()@@ -224,7 +224,7 @@ public abstract class AbstractVisitingPrefixTreeFilter extends AbstractPrefixTreif (hasIndexedLeaves && cell.getLevel() != 0) {//If the next indexed term just adds a leaf marker ('+') to cell,// then add all of those docs- scanCell.readCell(thisTerm);+ scanCell = grid.readCell(thisTerm, scanCell);assert curVNode.cell.isPrefixOf(scanCell);if (scanCell.getLevel() == cell.getLevel() && scanCell.isLeaf()) {visitLeaf(scanCell);@@ -276,7 +276,7 @@ public abstract class AbstractVisitingPrefixTreeFilter extends AbstractPrefixTrefor ( ;thisTerm != null;thisTerm = termsEnum.next()) {- scanCell.readCell(thisTerm);+ scanCell = grid.readCell(thisTerm, scanCell);if (!curVNode.cell.isPrefixOf(scanCell))break;diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/ContainsPrefixTreeFilter.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/ContainsPrefixTreeFilter.javaindex 6395862fd5d..4bf903d4899 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/ContainsPrefixTreeFilter.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/ContainsPrefixTreeFilter.java@@ -84,7 +84,7 @@ public class ContainsPrefixTreeFilter extends AbstractPrefixTreeFilter {}BytesRef termBytes = new BytesRef();//no leaf- Cell nextCell = grid.getWorldCell();//see getLeafDocs+ Cell nextCell;//see getLeafDocs/** This is the primary algorithm; recursive. Returns null if finds none. */private SmallDocSet visit(Cell cell, Bits acceptContains) throws IOException {@@ -158,7 +158,7 @@ public class ContainsPrefixTreeFilter extends AbstractPrefixTreeFilter {termsEnum = null;//signals all donereturn null;}- nextCell.readCell(nextTerm);+ nextCell = grid.readCell(nextTerm, nextCell);assert leafCell.isPrefixOf(nextCell);if (nextCell.getLevel() == leafCell.getLevel() && nextCell.isLeaf()) {return collectDocs(acceptContains);diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PointPrefixTreeFieldCacheProvider.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PointPrefixTreeFieldCacheProvider.javaindex 764a24a36d7..cf8481951bc 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PointPrefixTreeFieldCacheProvider.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PointPrefixTreeFieldCacheProvider.java@@ -31,17 +31,16 @@ import org.apache.lucene.util.BytesRef;public class PointPrefixTreeFieldCacheProvider extends ShapeFieldCacheProvider<Point> {private final SpatialPrefixTree grid;- private Cell scanCell;+ private Cell scanCell;//re-used in readShape to save GCpublic PointPrefixTreeFieldCacheProvider(SpatialPrefixTree grid, String shapeField, int defaultSize) {super( shapeField, defaultSize );this.grid = grid;- this.scanCell = grid.getWorldCell();//re-used in readShape to save GC}@Overrideprotected Point readShape(BytesRef term) {- scanCell.readCell(term);+ scanCell = grid.readCell(term, scanCell);;if (scanCell.getLevel() == grid.getMaxLevels() && !scanCell.isLeaf())//points are never flagged as leafreturn scanCell.getShape().getCenter();return null;diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/Cell.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/Cell.javaindex dc36d0dbd00..2ea23e9f7c8 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/Cell.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/Cell.java@@ -25,21 +25,13 @@ import org.apache.lucene.util.BytesRef;* Represents a grid cell. Cell instances are generally very transient and may be re-used* internally. To get an instance, you could start with {@link SpatialPrefixTree#getWorldCell()}.* And from there you could either traverse down the tree with {@link #getNextLevelCells(com.spatial4j.core.shape.Shape)},- * or you could read an indexed term via {@link #readCell(org.apache.lucene.util.BytesRef)}.+ * or you could read an indexed term via {@link SpatialPrefixTree#readCell(org.apache.lucene.util.BytesRef,Cell)}.* When a cell is read from a term, it is comprised of just the base bytes plus optionally a leaf flag.** @lucene.experimental*/public interface Cell {- /** This initializes the cell with the given bytes and clears any previous state.- * Warning: Refers to the same byte array (no copy). If {@link #setLeaf()} is subsequently called,- * then it may modify these bytes.- *- * @param bytes the bytes of the new cell. WARNING: copy by reference.- */- void readCell(BytesRef bytes);-// If we bring this back; perhaps do so as a method that un-shares its internal state: void unshare();// /** Resets the state of this cell such that it is identical to {@code source}. This can be used for// * cloning a cell to have a safe copy, and it also might be used to position this celldiff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/GeohashPrefixTree.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/GeohashPrefixTree.javaindex ba32a168153..20c1d98499e 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/GeohashPrefixTree.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/GeohashPrefixTree.java@@ -116,7 +116,7 @@ public class GeohashPrefixTree extends LegacyPrefixTree {protected GeohashPrefixTree getGrid() { return GeohashPrefixTree.this; }@Override- public void readCell(BytesRef bytesRef) {+ protected void readCell(BytesRef bytesRef) {super.readCell(bytesRef);geohash = null;}diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyCell.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyCell.javaindex 8612f0b7a44..e1357b9262f 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyCell.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyCell.java@@ -59,8 +59,7 @@ public abstract class LegacyCell implements Cell {readLeafAdjust();}- @Override- public void readCell(BytesRef bytes) {+ protected void readCell(BytesRef bytes) {shapeRel = null;shape = null;this.bytes = bytes.bytes;diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyPrefixTree.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyPrefixTree.javaindex a56601f8ef6..bd3445d7776 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyPrefixTree.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/LegacyPrefixTree.java@@ -50,6 +50,15 @@ abstract class LegacyPrefixTree extends SpatialPrefixTree {*/protected abstract Cell getCell(Point p, int level);+ @Override+ public Cell readCell(BytesRef term, Cell scratch) {+ LegacyCell cell = (LegacyCell) scratch;+ if (cell == null)+ cell = (LegacyCell) getWorldCell();+ cell.readCell(term);+ return cell;+ }+@Overridepublic CellIterator getTreeCellIterator(Shape shape, int detailLevel) {if (!(shape instanceof Point))@@ -65,9 +74,8 @@ abstract class LegacyPrefixTree extends SpatialPrefixTree {//fill in reverse order to be sortedCell[] cells = new Cell[detailLevel];for (int i = 1; i < detailLevel; i++) {- Cell parentCell = getWorldCell();fullBytes.length = i;- parentCell.readCell(fullBytes);+ Cell parentCell = readCell(fullBytes, null);cells[i-1] = parentCell;}cells[detailLevel-1] = cell;diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/SpatialPrefixTree.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/SpatialPrefixTree.javaindex 726ff019efe..1de862e23ef 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/SpatialPrefixTree.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/SpatialPrefixTree.java@@ -80,11 +80,19 @@ public abstract class SpatialPrefixTree {public abstract double getDistanceForLevel(int level);/**- * Returns the level 0 cell which encompasses all spatial data. Equivalent to {@link Cell#readCell(BytesRef)}+ * Returns the level 0 cell which encompasses all spatial data. Equivalent to {@link #readCell(BytesRef,Cell)}* with no bytes.*/public abstract Cell getWorldCell(); //another possible name: getTopCell+ /**+ * This creates a new Cell (or re-using {@code scratch} if provided), initialized to the state as read+ * by the bytes.+ * Warning: An implementation may refer to the same byte array (no copy). If {@link Cell#setLeaf()} is+ * subsequently called, it would then modify these bytes.+ */+ public abstract Cell readCell(BytesRef term, Cell scratch);+/*** Gets the intersecting cells for the specified shape, without exceeding* detail level. If a cell is within the query shape then it's marked as a