// 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;
*/
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
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);
}
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);
}
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);
}
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);
}
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);
}
@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);
}
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 {
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:
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();
continue;
}
break;
- case 4:
+ case 5:
// Prev
if (is_valid) {
iter.prev();
}
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));
}
}
+ @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`
*