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

aryangupta1998 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new c639efc7227 HDDS-16350. Avoid a point-get for the start-key check in 
RDBTable.getRangeKVs (#11169)
c639efc7227 is described below

commit c639efc72275f49ae5c8a9fdf18887682c70c8a7
Author: KUAN-HAO HUANG <[email protected]>
AuthorDate: Thu Sep 3 19:15:45 2026 +0800

    HDDS-16350. Avoid a point-get for the start-key check in 
RDBTable.getRangeKVs (#11169)
---
 .../org/apache/hadoop/hdds/utils/db/RDBTable.java  | 11 ++++++--
 .../hadoop/hdds/utils/db/TestRDBTableStore.java    | 33 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 3 deletions(-)

diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/RDBTable.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/RDBTable.java
index e1fc7297d4d..d594eaf7014 100644
--- 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/RDBTable.java
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/RDBTable.java
@@ -20,6 +20,7 @@
 import java.io.File;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.function.Supplier;
 import org.apache.hadoop.hdds.annotation.InterfaceAudience;
@@ -298,12 +299,16 @@ public List<KeyValue<byte[], byte[]>> getRangeKVs(
       if (startKey == null) {
         it.seekToFirst();
       } else {
+        // seek() positions the iterator and returns the landing entry without
+        // consuming it, so the loop below still starts from startKey. 
Comparing
+        // the landing key to startKey avoids a separate point-get just to 
check
+        // that startKey exists.
+        final KeyValue<byte[], byte[]> seeked = it.seek(startKey);
         if ((prefix == null || startKey.length > prefix.length)
-            && get(startKey) == null) {
-          // Key not found, return empty list
+            && (seeked == null || !Arrays.equals(seeked.getKey(), startKey))) {
+          // start key not found, return empty list
           return result;
         }
-        it.seek(startKey);
       }
 
       while (it.hasNext() && result.size() < count) {
diff --git 
a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBTableStore.java
 
b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBTableStore.java
index 2741834c9d7..586de2bf7f5 100644
--- 
a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBTableStore.java
+++ 
b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBTableStore.java
@@ -654,6 +654,39 @@ public void testPrefixedRangeKVs() throws Exception {
     assertEquals(0, rangeKVs.size());
   }
 
+  @Test
+  public void testRangeKVsStartKeyInclusiveAndAbsent() throws Exception {
+    Table<byte[], byte[]> testTable = rdbStore.getTable("PrefixFour");
+    byte[] prefix = "p/".getBytes(StandardCharsets.UTF_8);
+    for (String suffix : new String[] {"a", "c", "e"}) {
+      byte[] key = ("p/" + suffix).getBytes(StandardCharsets.UTF_8);
+      testTable.put(key, key);
+    }
+    byte[] present = "p/c".getBytes(StandardCharsets.UTF_8);
+    byte[] absentMiddle = "p/b".getBytes(StandardCharsets.UTF_8);
+    byte[] absentPastEnd = "p/z".getBytes(StandardCharsets.UTF_8);
+
+    // Existing start key: the range starts at that key (inclusive).
+    List<Table.KeyValue<byte[], byte[]>> rangeKVs =
+        testTable.getRangeKVs(present, 10, prefix);
+    assertEquals(2, rangeKVs.size());
+    assertArrayEquals(present, rangeKVs.get(0).getKey());
+
+    // Absent start key with a later key under the prefix: seek lands on a
+    // different key, so the range is empty.
+    assertEquals(0, testTable.getRangeKVs(absentMiddle, 10, prefix).size());
+    // Absent start key past every key: seek lands on nothing, range is empty.
+    assertEquals(0, testTable.getRangeKVs(absentPastEnd, 10, prefix).size());
+
+    // Same checks with no prefix (whole-table range).
+    rangeKVs = testTable.getRangeKVs(present, 10, null);
+    assertEquals(2, rangeKVs.size());
+    assertArrayEquals(present, rangeKVs.get(0).getKey());
+    assertEquals(0, testTable.getRangeKVs(absentMiddle, 10, null).size());
+    // Absent start key past every key with no prefix: seek lands on nothing.
+    assertEquals(0, testTable.getRangeKVs(absentPastEnd, 10, null).size());
+  }
+
   @Test
   public void testDumpAndLoadBasic() throws Exception {
     int containerCount = 3;


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

Reply via email to