You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ch...@apache.org on 2025/03/03 12:17:30 UTC

(lucene) branch branch_10x updated: Fix DirectIOIndexInput seek to not read when position is within buffer (#14320)

This is an automated email from the ASF dual-hosted git repository.

chegar pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_10x by this push:
     new 212924e5000 Fix DirectIOIndexInput seek to not read when position is within buffer (#14320)
212924e5000 is described below

commit 212924e5000c066d82f1ce00fcde77254f3686c5
Author: Chris Hegarty <62...@users.noreply.github.com>
AuthorDate: Mon Mar 3 12:16:31 2025 +0000

    Fix DirectIOIndexInput seek to not read when position is within buffer (#14320)
    
    This commit changes DirectIOIndexInput::seek so that it repositions the indexInput's position without a read, when the new position is within the bounds of the current buffer.
---
 lucene/CHANGES.txt                                 |  2 ++
 .../lucene/misc/store/DirectIODirectory.java       |  8 ++++++-
 .../lucene/misc/store/TestDirectIODirectory.java   | 26 ++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 00629515550..4aac0ee48be 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -133,6 +133,8 @@ Bug Fixes
 
 * GITHUB#14215: Fix degenerate case in HNSW where all vectors have the same score. (Ben Trent)
 
+* GITHUB#14320: Fix DirectIOIndexInput seek to not read when position is within buffer (Chris Hegarty)
+
 Changes in Runtime Behavior
 ---------------------
 * GITHUB#14189: Bump floor segment size to 16MB in TieredMergePolicy and
diff --git a/lucene/misc/src/java/org/apache/lucene/misc/store/DirectIODirectory.java b/lucene/misc/src/java/org/apache/lucene/misc/store/DirectIODirectory.java
index 5f2ce259810..8164f80f061 100644
--- a/lucene/misc/src/java/org/apache/lucene/misc/store/DirectIODirectory.java
+++ b/lucene/misc/src/java/org/apache/lucene/misc/store/DirectIODirectory.java
@@ -373,7 +373,13 @@ public class DirectIODirectory extends FilterDirectory {
     @Override
     public void seek(long pos) throws IOException {
       if (pos != getFilePointer()) {
-        seekInternal(pos);
+        final long absolutePos = pos + offset;
+        if (absolutePos >= filePos && absolutePos <= filePos + buffer.limit()) {
+          // the new position is within the existing buffer
+          buffer.position(Math.toIntExact(absolutePos - filePos));
+        } else {
+          seekInternal(pos); // do an actual seek/read
+        }
       }
       assert pos == getFilePointer();
     }
diff --git a/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.java b/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.java
index c603502ec15..e61b7293c13 100644
--- a/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.java
+++ b/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.java
@@ -209,4 +209,30 @@ public class TestDirectIODirectory extends BaseDirectoryTestCase {
               OptionalLong.of(largeSize)));
     }
   }
+
+  // Ping-pong seeks should be really fast, since the position should be within buffer.
+  // The test should complete within sub-second times, not minutes.
+  public void testSeekSmall() throws IOException {
+    Path tmpDir = createTempDir("testSeekSmall");
+    try (Directory dir = getDirectory(tmpDir)) {
+      int len = atLeast(100);
+      try (IndexOutput o = dir.createOutput("out", newIOContext(random()))) {
+        byte[] b = new byte[len];
+        for (int i = 0; i < len; i++) {
+          b[i] = (byte) i;
+        }
+        o.writeBytes(b, 0, len);
+      }
+      try (IndexInput in = dir.openInput("out", newIOContext(random()))) {
+        for (int i = 0; i < 100_000; i++) {
+          in.seek(2);
+          assertEquals(2, in.readByte());
+          in.seek(1);
+          assertEquals(1, in.readByte());
+          in.seek(0);
+          assertEquals(0, in.readByte());
+        }
+      }
+    }
+  }
 }