]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/rocksdb/java/src/test/java/org/rocksdb/util/BytewiseComparatorTest.java
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / util / BytewiseComparatorTest.java
index 01ea52c49c200341cc28abc76238dbfee446f3a6..8149a4800a0a21ef771b3a1653353f84ca6c1d71 100644 (file)
@@ -1,7 +1,7 @@
 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
-// This source code is licensed under the BSD-style license found in the
-// LICENSE file in the root directory of this source tree. An additional grant
-// of patent rights can be found in the PATENTS file in the same directory.
+//  This source code is licensed under both the GPLv2 (found in the
+//  COPYING file in the root directory) and Apache 2.0 License
+//  (found in the LICENSE.Apache file in the root directory).
 
 package org.rocksdb.util;
 
@@ -27,6 +27,9 @@ import static org.junit.Assert.*;
  */
 public class BytewiseComparatorTest {
 
+  private List<String> source_strings = Arrays.asList("b", "d", "f", "h", "j", "l");
+  private List<String> interleaving_strings = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m");
+
   /**
    * Open the database using the C++ BytewiseComparatorImpl
    * and test the results against our Java BytewiseComparator
@@ -38,14 +41,18 @@ public class BytewiseComparatorTest {
       final Path dbDir = Files.createTempDirectory("comparator_db_test");
       try(final RocksDB db = openDatabase(dbDir,
           BuiltinComparator.BYTEWISE_COMPARATOR)) {
+
         final Random rnd = new Random(rand_seed);
-        doRandomIterationTest(
-            db,
-            toJavaComparator(new BytewiseComparator(new ComparatorOptions())),
-            Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i"),
-            rnd,
-            8, 100, 3
-        );
+        try(final ComparatorOptions copt2 = new ComparatorOptions();
+            final Comparator comparator2 = new BytewiseComparator(copt2)) {
+          final java.util.Comparator<String> jComparator = toJavaComparator(comparator2);
+          doRandomIterationTest(
+              db,
+              jComparator,
+              rnd,
+              8, 100, 3
+          );
+        }
       } finally {
         removeData(dbDir);
       }
@@ -61,16 +68,21 @@ public class BytewiseComparatorTest {
       throws IOException, RocksDBException {
     for(int rand_seed = 301; rand_seed < 306; rand_seed++) {
       final Path dbDir = Files.createTempDirectory("comparator_db_test");
-      try(final RocksDB db = openDatabase(dbDir, new BytewiseComparator(
-          new ComparatorOptions()))) {
+      try(final ComparatorOptions copt = new ComparatorOptions();
+          final Comparator comparator = new BytewiseComparator(copt);
+          final RocksDB db = openDatabase(dbDir, comparator)) {
+
         final Random rnd = new Random(rand_seed);
-        doRandomIterationTest(
-            db,
-            toJavaComparator(new BytewiseComparator(new ComparatorOptions())),
-            Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i"),
-            rnd,
-            8, 100, 3
-        );
+        try(final ComparatorOptions copt2 = new ComparatorOptions();
+            final Comparator comparator2 = new BytewiseComparator(copt2)) {
+          final java.util.Comparator<String> jComparator = toJavaComparator(comparator2);
+          doRandomIterationTest(
+              db,
+              jComparator,
+              rnd,
+              8, 100, 3
+          );
+        }
       } finally {
         removeData(dbDir);
       }
@@ -88,16 +100,18 @@ public class BytewiseComparatorTest {
       final Path dbDir = Files.createTempDirectory("comparator_db_test");
       try(final RocksDB db = openDatabase(dbDir,
           BuiltinComparator.BYTEWISE_COMPARATOR)) {
+
         final Random rnd = new Random(rand_seed);
-        doRandomIterationTest(
-            db,
-            toJavaComparator(new DirectBytewiseComparator(
-                new ComparatorOptions())
-            ),
-            Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i"),
-            rnd,
-            8, 100, 3
-        );
+        try(final ComparatorOptions copt2 = new ComparatorOptions();
+            final DirectComparator comparator2 = new DirectBytewiseComparator(copt2)) {
+          final java.util.Comparator<String> jComparator = toJavaComparator(comparator2);
+          doRandomIterationTest(
+              db,
+              jComparator,
+              rnd,
+              8, 100, 3
+          );
+        }
       } finally {
         removeData(dbDir);
       }
@@ -113,18 +127,21 @@ public class BytewiseComparatorTest {
       throws IOException, RocksDBException {
     for(int rand_seed = 301; rand_seed < 306; rand_seed++) {
       final Path dbDir = Files.createTempDirectory("comparator_db_test");
-      try(final RocksDB db = openDatabase(dbDir, new DirectBytewiseComparator(
-            new ComparatorOptions()))) {
+      try (final ComparatorOptions copt = new ComparatorOptions();
+          final DirectComparator comparator = new DirectBytewiseComparator(copt);
+          final RocksDB db = openDatabase(dbDir, comparator)) {
+
         final Random rnd = new Random(rand_seed);
-        doRandomIterationTest(
-            db,
-            toJavaComparator(new DirectBytewiseComparator(
-                new ComparatorOptions())
-            ),
-            Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i"),
-            rnd,
-            8, 100, 3
-        );
+        try(final ComparatorOptions copt2 = new ComparatorOptions();
+            final DirectComparator comparator2 = new DirectBytewiseComparator(copt2)) {
+          final java.util.Comparator<String> jComparator = toJavaComparator(comparator2);
+          doRandomIterationTest(
+              db,
+              jComparator,
+              rnd,
+              8, 100, 3
+          );
+        }
       } finally {
         removeData(dbDir);
       }
@@ -142,16 +159,18 @@ public class BytewiseComparatorTest {
       final Path dbDir = Files.createTempDirectory("comparator_db_test");
       try(final RocksDB db = openDatabase(dbDir,
           BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR)) {
+
         final Random rnd = new Random(rand_seed);
-        doRandomIterationTest(
-            db,
-            toJavaComparator(
-                new ReverseBytewiseComparator(new ComparatorOptions())
-            ),
-            Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i"),
-            rnd,
-            8, 100, 3
-        );
+        try(final ComparatorOptions copt2 = new ComparatorOptions();
+            final Comparator comparator2 = new ReverseBytewiseComparator(copt2)) {
+          final java.util.Comparator<String> jComparator = toJavaComparator(comparator2);
+          doRandomIterationTest(
+              db,
+              jComparator,
+              rnd,
+              8, 100, 3
+          );
+        }
       } finally {
         removeData(dbDir);
       }
@@ -165,21 +184,23 @@ public class BytewiseComparatorTest {
   @Test
   public void java_vs_java_reverseBytewiseComparator()
       throws IOException, RocksDBException {
-
     for(int rand_seed = 301; rand_seed < 306; rand_seed++) {
       final Path dbDir = Files.createTempDirectory("comparator_db_test");
-      try(final RocksDB db = openDatabase(dbDir, new ReverseBytewiseComparator(
-            new ComparatorOptions()))) {
+      try (final ComparatorOptions copt = new ComparatorOptions();
+           final Comparator comparator = new ReverseBytewiseComparator(copt);
+           final RocksDB db = openDatabase(dbDir, comparator)) {
+
         final Random rnd = new Random(rand_seed);
-        doRandomIterationTest(
-            db,
-            toJavaComparator(
-                new ReverseBytewiseComparator(new ComparatorOptions())
-            ),
-            Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i"),
-            rnd,
-            8, 100, 3
-        );
+        try(final ComparatorOptions copt2 = new ComparatorOptions();
+            final Comparator comparator2 = new ReverseBytewiseComparator(copt2)) {
+          final java.util.Comparator<String> jComparator = toJavaComparator(comparator2);
+          doRandomIterationTest(
+              db,
+              jComparator,
+              rnd,
+              8, 100, 3
+          );
+        }
       } finally {
         removeData(dbDir);
       }
@@ -188,7 +209,7 @@ public class BytewiseComparatorTest {
 
   private void doRandomIterationTest(
       final RocksDB db, final java.util.Comparator<String> javaComparator,
-      final List<String> source_strings, final Random rnd,
+      final Random rnd,
       final int num_writes, final int num_iter_ops,
       final int num_trigger_flush) throws RocksDBException {
 
@@ -228,7 +249,7 @@ public class BytewiseComparatorTest {
       for (int i = 0; i < num_iter_ops; i++) {
         // Random walk and make sure iter and result_iter returns the
         // same key and value
-        final int type = rnd.nextInt(6);
+        final int type = rnd.nextInt(7);
         iter.status();
         switch (type) {
           case 0:
@@ -242,14 +263,22 @@ public class BytewiseComparatorTest {
             result_iter.seekToLast();
             break;
           case 2: {
-            // Seek to random key
-            final int key_idx = rnd.nextInt(source_strings.size());
-            final String key = source_strings.get(key_idx);
+            // Seek to random (existing or non-existing) key
+            final int key_idx = rnd.nextInt(interleaving_strings.size());
+            final String key = interleaving_strings.get(key_idx);
             iter.seek(bytes(key));
             result_iter.seek(bytes(key));
             break;
           }
-          case 3:
+          case 3: {
+            // SeekForPrev to random (existing or non-existing) key
+            final int key_idx = rnd.nextInt(interleaving_strings.size());
+            final String key = interleaving_strings.get(key_idx);
+            iter.seekForPrev(bytes(key));
+            result_iter.seekForPrev(bytes(key));
+            break;
+          }
+          case 4:
             // Next
             if (is_valid) {
               iter.next();
@@ -258,7 +287,7 @@ public class BytewiseComparatorTest {
               continue;
             }
             break;
-          case 4:
+          case 5:
             // Prev
             if (is_valid) {
               iter.prev();
@@ -268,7 +297,7 @@ public class BytewiseComparatorTest {
             }
             break;
           default: {
-            assert (type == 5);
+            assert (type == 6);
             final int key_idx = rnd.nextInt(source_strings.size());
             final String key = source_strings.get(key_idx);
             final byte[] result = db.get(new ReadOptions(), bytes(key));
@@ -413,6 +442,16 @@ public class BytewiseComparatorTest {
       }
     }
 
+    @Override
+    public void seekForPrev(final byte[] target) {
+      for(offset = entries.size()-1; offset >= 0; offset--) {
+        if(comparator.compare(entries.get(offset).getKey(),
+            (K)new String(target, StandardCharsets.UTF_8)) <= 0) {
+          return;
+        }
+      }
+    }
+
     /**
      * Is `a` a prefix of `b`
      *