Apache Lucene open-source search software
LUCENE-10262: Lift up restrictions for navigating PointValues#PointTree (#476)
Ignacio Veraa590c6d2a08d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 27ba87f2dcc..27739b46ad2 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -58,6 +58,9 @@ Improvements* LUCENE-9820: Extract BKD tree interface and move intersecting logic to thePointValues abstract class. (Ignacio Vera, Adrien Grand)++* LUCENE-10262: Lift up restrictions for navigating PointValues#PointTree+ added in LUCENE-9820 (Ignacio Vera)Optimizations---------------------diff --git a/lucene/core/src/java/org/apache/lucene/index/PointValues.java b/lucene/core/src/java/org/apache/lucene/index/PointValues.javaindex 51fcf1f139f..45416459786 100644--- a/lucene/core/src/java/org/apache/lucene/index/PointValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/PointValues.java@@ -238,16 +238,12 @@ public abstract class PointValues {*/public interface PointTree extends Cloneable {- /**- * Clone, the current node becomes the root of the new tree. The method should not be called- * after a successful call to {@link #moveToParent()}- */+ /** Clone, the current node becomes the root of the new tree. */PointTree clone();/*** Move to the first child node and return {@code true} upon success. Returns {@code false} for- * leaf nodes and {@code true} otherwise. The method should not be called after a successful- * call to {@link #moveToParent()}+ * leaf nodes and {@code true} otherwise.*/boolean moveToChild() throws IOException;diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.javaindex a50528610f8..ff2584fd8dd 100644--- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java+++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java@@ -132,6 +132,8 @@ public class BKDReader extends PointValues {private final IndexInput leafNodes;// holds the minimum (left most) leaf block file pointer for each level we've recursed to:private final long[] leafBlockFPStack;+ // holds the address, in the off-heap index, after reading the node data of each level:+ private final int[] readNodeDataPositions;// holds the address, in the off-heap index, of the right-node of each level:private final int[] rightNodePositions;// holds the splitDim position for each level:@@ -229,6 +231,7 @@ public class BKDReader extends PointValues {splitValuesStack = new byte[treeDepth][];splitValuesStack[0] = new byte[config.packedIndexBytesLength];leafBlockFPStack = new long[treeDepth + 1];+ readNodeDataPositions = new int[treeDepth + 1];rightNodePositions = new int[treeDepth];splitDimsPos = new int[treeDepth];negativeDeltas = new boolean[config.numIndexDims * treeDepth];@@ -270,6 +273,7 @@ public class BKDReader extends PointValues {if (isLeafNode() == false) {// copy node dataindex.rightNodePositions[index.level] = rightNodePositions[level];+ index.readNodeDataPositions[index.level] = readNodeDataPositions[level];index.splitValuesStack[index.level] = splitValuesStack[level].clone();System.arraycopy(negativeDeltas,@@ -297,11 +301,18 @@ public class BKDReader extends PointValues {if (isLeafNode()) {return false;}+ resetNodeDataPosition();pushBoundsLeft();pushLeft();return true;}+ private void resetNodeDataPosition() throws IOException {+ // move position of the inner nodes index to visit the first child+ assert readNodeDataPositions[level] <= innerNodes.getFilePointer();+ innerNodes.seek(readNodeDataPositions[level]);+ }+private void pushBoundsLeft() {final int splitDimPos = splitDimsPos[level];if (splitDimValueStack[level] == null) {@@ -485,6 +496,7 @@ public class BKDReader extends PointValues {@Overridepublic void visitDocIDs(PointValues.IntersectVisitor visitor) throws IOException {+ resetNodeDataPosition();addAll(visitor, false);}@@ -515,15 +527,20 @@ public class BKDReader extends PointValues {@Overridepublic void visitDocValues(PointValues.IntersectVisitor visitor) throws IOException {+ resetNodeDataPosition();+ visitLeavesOneByOne(visitor);+ }++ private void visitLeavesOneByOne(PointValues.IntersectVisitor visitor) throws IOException {if (isLeafNode()) {// Leaf nodevisitDocValues(visitor, getLeafBlockFP());} else {pushLeft();- visitDocValues(visitor);+ visitLeavesOneByOne(visitor);pop();pushRight();- visitDocValues(visitor);+ visitLeavesOneByOne(visitor);pop();}}@@ -636,6 +653,7 @@ public class BKDReader extends PointValues {leftNumBytes = 0;}rightNodePositions[level] = Math.toIntExact(innerNodes.getFilePointer()) + leftNumBytes;+ readNodeDataPositions[level] = Math.toIntExact(innerNodes.getFilePointer());}}diff --git a/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java b/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.javaindex 3f4e6a01c0f..c7ffeaa6b90 100644--- a/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java+++ b/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java@@ -890,30 +890,60 @@ public class TestBKD extends LuceneTestCase {private void assertSize(PointValues.PointTree tree) throws IOException {final PointValues.PointTree clone = tree.clone();assertEquals(clone.size(), tree.size());- final long[] size = new long[] {0};- clone.visitDocIDs(+ // rarely continue with the clone tree+ tree = rarely() ? clone : tree;+ final long[] visitDocIDSize = new long[] {0};+ final long[] visitDocValuesSize = new long[] {0};+ final IntersectVisitor visitor =new IntersectVisitor() {@Overridepublic void visit(int docID) {- size[0]++;+ visitDocIDSize[0]++;}@Overridepublic void visit(int docID, byte[] packedValue) {- throw new UnsupportedOperationException();+ visitDocValuesSize[0]++;}@Overridepublic Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {- throw new UnsupportedOperationException();+ return Relation.CELL_CROSSES_QUERY;}- });- assertEquals(size[0], tree.size());+ };+ if (random().nextBoolean()) {+ tree.visitDocIDs(visitor);+ tree.visitDocValues(visitor);+ } else {+ tree.visitDocValues(visitor);+ tree.visitDocIDs(visitor);+ }+ assertEquals(visitDocIDSize[0], visitDocValuesSize[0]);+ assertEquals(visitDocIDSize[0], tree.size());if (tree.moveToChild()) {do {+ randomPointTreeNavigation(tree);assertSize(tree);} while (tree.moveToSibling());+ tree.moveToParent();+ }+ }++ private void randomPointTreeNavigation(PointValues.PointTree tree) throws IOException {+ byte[] minPackedValue = tree.getMinPackedValue().clone();+ byte[] maxPackedValue = tree.getMaxPackedValue().clone();+ long size = tree.size();+ if (random().nextBoolean() && tree.moveToChild()) {+ randomPointTreeNavigation(tree);+ if (random().nextBoolean() && tree.moveToSibling()) {+ randomPointTreeNavigation(tree);+ }+ tree.moveToParent();}+ // we always finish on the same node we started+ assertArrayEquals(minPackedValue, tree.getMinPackedValue());+ assertArrayEquals(maxPackedValue, tree.getMaxPackedValue());+ assertEquals(size, tree.size());}private void assertHits(BitSet hits, BitSet expected) {diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.javaindex 168b24103ef..ffb4f1739dd 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.java@@ -1139,12 +1139,10 @@ public class AssertingLeafReader extends FilterLeafReader {}}- /** Validates that we don't call moveToChild() or clone() after having called moveToParent() */static class AssertingPointTree implements PointValues.PointTree {final PointValues pointValues;final PointValues.PointTree in;- private boolean moveToParent;AssertingPointTree(PointValues pointValues, PointValues.PointTree in) {this.pointValues = pointValues;@@ -1153,25 +1151,21 @@ public class AssertingLeafReader extends FilterLeafReader {@Overridepublic PointValues.PointTree clone() {- assert moveToParent == false : "calling clone() after calling moveToParent()";return new AssertingPointTree(pointValues, in.clone());}@Overridepublic boolean moveToChild() throws IOException {- assert moveToParent == false : "calling moveToChild() after calling moveToParent()";return in.moveToChild();}@Overridepublic boolean moveToSibling() throws IOException {- moveToParent = false;return in.moveToSibling();}@Overridepublic boolean moveToParent() throws IOException {- moveToParent = true;return in.moveToParent();}diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BasePointsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BasePointsFormatTestCase.javaindex 5519d07533c..a89b3113d2d 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BasePointsFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BasePointsFormatTestCase.java@@ -1066,33 +1066,62 @@ public abstract class BasePointsFormatTestCase extends BaseIndexFileFormatTestCaprivate void assertSize(PointValues.PointTree tree) throws IOException {final PointValues.PointTree clone = tree.clone();assertEquals(clone.size(), tree.size());- final long[] size = new long[] {0};- clone.visitDocIDs(+ // rarely continue with the clone tree+ tree = rarely() ? clone : tree;+ final long[] visitDocIDSize = new long[] {0};+ final long[] visitDocValuesSize = new long[] {0};+ final IntersectVisitor visitor =new IntersectVisitor() {@Overridepublic void visit(int docID) {- size[0]++;+ visitDocIDSize[0]++;}@Overridepublic void visit(int docID, byte[] packedValue) {- throw new UnsupportedOperationException();+ visitDocValuesSize[0]++;}@Overridepublic Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {- throw new UnsupportedOperationException();+ return Relation.CELL_CROSSES_QUERY;}- });- assertEquals(size[0], tree.size());+ };+ if (random().nextBoolean()) {+ tree.visitDocIDs(visitor);+ tree.visitDocValues(visitor);+ } else {+ tree.visitDocValues(visitor);+ tree.visitDocIDs(visitor);+ }+ assertEquals(visitDocIDSize[0], visitDocValuesSize[0]);+ assertEquals(visitDocIDSize[0], tree.size());if (tree.moveToChild()) {do {+ randomPointTreeNavigation(tree);assertSize(tree);} while (tree.moveToSibling());tree.moveToParent();}}+ private void randomPointTreeNavigation(PointValues.PointTree tree) throws IOException {+ byte[] minPackedValue = tree.getMinPackedValue().clone();+ byte[] maxPackedValue = tree.getMaxPackedValue().clone();+ long size = tree.size();+ if (random().nextBoolean() && tree.moveToChild()) {+ randomPointTreeNavigation(tree);+ if (random().nextBoolean() && tree.moveToSibling()) {+ randomPointTreeNavigation(tree);+ }+ tree.moveToParent();+ }+ // we always finish on the same node we started+ assertArrayEquals(minPackedValue, tree.getMinPackedValue());+ assertArrayEquals(maxPackedValue, tree.getMaxPackedValue());+ assertEquals(size, tree.size());+ }+public void testAddIndexes() throws IOException {Directory dir1 = newDirectory();RandomIndexWriter w = new RandomIndexWriter(random(), dir1);