navneet1v commented on code in PR #15376:
URL: https://github.com/apache/lucene/pull/15376#discussion_r2670300004


##########
lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java:
##########
@@ -1076,4 +1137,123 @@ public long cost() {
       return length;
     }
   }
+
+  /**
+   * We can recurse the {@link BKDPointTree} using {@link 
TwoPhaseIntersectVisitor}. This visitor
+   * travere {@link BKDPointTree} in two phases. In the first phase, it 
recurses over the {@link
+   * BKDPointTree} optionally triggering IO for some of the blocks and caching 
them. In the second
+   * phase, once the recursion is over it visits the cached blocks one by one.
+   *
+   * @lucene.experimental
+   */
+  public interface TwoPhaseIntersectVisitor extends IntersectVisitor {
+    /** return the last deferred block ordinal during recursion. */
+    public int lastDeferredBlockOrdinal();
+
+    /** set last deferred block ordinal */
+    public void setLastDeferredBlockOrdinal(int leafNodeOrdinal);
+
+    /** Defer this block for processing in the second phase. */
+    public void deferBlock(long leafFp);
+
+    /** Returns a snapshot of the currently deferred blocks. */
+    public List<Long> deferredBlocks();
+
+    /** Mark the given block as processed and remove it from the deferred set. 
*/
+    public void onProcessingDeferredBlock(long leafFp);
+  }
+
+  /**
+   * Base implementation of {@link TwoPhaseIntersectVisitor} that maintains a 
list of deferred
+   * blocks from first phase of traversal and visits them in the second phase.
+   *
+   * @lucene.experimental
+   */
+  public abstract static class BaseTwoPhaseIntersectVisitor implements 
TwoPhaseIntersectVisitor {
+
+    int lastDeferredBlockOrdinal = -1;
+    List<Long> deferredBlocks = new ArrayList<>();
+
+    /**
+     * return the last deferred block ordinal - this is used to avoid 
prefetching call for
+     * contiguous ordinals assuming contiguous ordinals prefetching can be 
taken care by readaheads.
+     */
+    @Override
+    public int lastDeferredBlockOrdinal() {
+      return lastDeferredBlockOrdinal;
+    }
+
+    /** set last deferred block ordinal * */
+    @Override
+    public void setLastDeferredBlockOrdinal(int leafNodeOrdinal) {
+      lastDeferredBlockOrdinal = leafNodeOrdinal;
+    }
+
+    /** Defer this block for processing in the second phase. */
+    @Override
+    public void deferBlock(long leafFp) {
+      deferredBlocks.add(leafFp);
+    }
+
+    /** Returns a snapshot of the currently deferred blocks. */
+    @Override
+    public List<Long> deferredBlocks() {
+      return new ArrayList<>(deferredBlocks);
+    }
+
+    /** Mark the given block as processed and remove it from the deferred set. 
*/
+    @Override
+    public void onProcessingDeferredBlock(long leafFp) {
+      deferredBlocks.remove(leafFp);
+    }
+  }
+
+  /**
+   * Finds all documents and points matching the provided visitor. This method 
does not enforce live
+   * documents, so it's up to the caller to test whether each document is 
deleted, if necessary.
+   */
+  @Override
+  public final void intersect(IntersectVisitor visitor) throws IOException {
+    final BKDPointTree pointTree = (BKDPointTree) getPointTree();
+    if (visitor instanceof TwoPhaseIntersectVisitor twoPhaseIntersectVisitor) {
+      intersect(twoPhaseIntersectVisitor, pointTree);
+      List<Long> fps = twoPhaseIntersectVisitor.deferredBlocks();

Review Comment:
   instead of using `List<Long>` I think it will be good to use `LongArrayList` 
which uses native arrays to build the list which can avoid autoBoxing of long.  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to