]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/rocksdb/java/src/main/java/org/rocksdb/util/ReverseBytewiseComparator.java
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rocksdb / java / src / main / java / org / rocksdb / util / ReverseBytewiseComparator.java
index 7fbac2fd6b906f02e07fdc25cf120f8069959c45..4c06f80aacd7f31cbb38f500f0e592944934fbbb 100644 (file)
@@ -5,10 +5,13 @@
 
 package org.rocksdb.util;
 
+import org.rocksdb.AbstractComparator;
 import org.rocksdb.BuiltinComparator;
 import org.rocksdb.ComparatorOptions;
 import org.rocksdb.Slice;
 
+import java.nio.ByteBuffer;
+
 /**
  * This is a Java Native implementation of the C++
  * equivalent ReverseBytewiseComparatorImpl using {@link Slice}
@@ -19,7 +22,7 @@ import org.rocksdb.Slice;
  * and you most likely instead wanted
  * {@link BuiltinComparator#REVERSE_BYTEWISE_COMPARATOR}
  */
-public class ReverseBytewiseComparator extends BytewiseComparator {
+public final class ReverseBytewiseComparator extends AbstractComparator {
 
   public ReverseBytewiseComparator(final ComparatorOptions copt) {
     super(copt);
@@ -31,7 +34,55 @@ public class ReverseBytewiseComparator extends BytewiseComparator {
   }
 
   @Override
-  public int compare(final Slice a, final Slice b) {
-    return -super.compare(a, b);
+  public int compare(final ByteBuffer a, final ByteBuffer b) {
+    return -BytewiseComparator._compare(a, b);
+  }
+
+  @Override
+  public void findShortestSeparator(final ByteBuffer start,
+      final ByteBuffer limit) {
+    // Find length of common prefix
+    final int minLength = Math.min(start.remaining(), limit.remaining());
+    int diffIndex = 0;
+    while (diffIndex < minLength &&
+        start.get(diffIndex) == limit.get(diffIndex)) {
+      diffIndex++;
+    }
+
+    assert(diffIndex <= minLength);
+    if (diffIndex == minLength) {
+      // Do not shorten if one string is a prefix of the other
+      //
+      // We could handle cases like:
+      //     V
+      // A A 2 X Y
+      // A A 2
+      // in a similar way as BytewiseComparator::FindShortestSeparator().
+      // We keep it simple by not implementing it. We can come back to it
+      // later when needed.
+    } else {
+      final int startByte = start.get(diffIndex) & 0xff;
+      final int limitByte = limit.get(diffIndex) & 0xff;
+      if (startByte > limitByte && diffIndex < start.remaining() - 1) {
+        // Case like
+        //     V
+        // A A 3 A A
+        // A A 1 B B
+        //
+        // or
+        //     v
+        // A A 2 A A
+        // A A 1 B B
+        // In this case "AA2" will be good.
+//#ifndef NDEBUG
+//        std::string old_start = *start;
+//#endif
+        start.limit(diffIndex + 1);
+//#ifndef NDEBUG
+//        assert(old_start >= *start);
+//#endif
+        assert(BytewiseComparator._compare(start.duplicate(), limit.duplicate()) > 0);
+      }
+    }
   }
 }