From e002a6122fdcd2508a7fa14e1ef82659cebc1019 Mon Sep 17 00:00:00 2001 From: fyrz Date: Tue, 25 Nov 2014 21:32:19 +0100 Subject: [PATCH] [RocksJava] Comparator tests for CF - Added AbstractComparatorTest. - Fixed a bug in the JNI Part about Java comparators --- .../rocksdb/test/AbstractComparatorTest.java | 99 +++++++++++++++++-- java/org/rocksdb/test/ComparatorTest.java | 28 +++++- .../rocksdb/test/DirectComparatorTest.java | 2 +- java/rocksjni/options.cc | 4 +- 4 files changed, 123 insertions(+), 10 deletions(-) diff --git a/java/org/rocksdb/test/AbstractComparatorTest.java b/java/org/rocksdb/test/AbstractComparatorTest.java index 6694b5be2..04abeb34d 100644 --- a/java/org/rocksdb/test/AbstractComparatorTest.java +++ b/java/org/rocksdb/test/AbstractComparatorTest.java @@ -9,7 +9,8 @@ import org.rocksdb.*; import java.io.IOException; import java.nio.file.*; -import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.List; import java.util.Random; import static org.assertj.core.api.Assertions.assertThat; @@ -40,7 +41,7 @@ public abstract class AbstractComparatorTest { * * @throws java.io.IOException if IO error happens. */ - public void testRoundtrip(final Path db_path) throws IOException { + public void testRoundtrip(final Path db_path) throws IOException, RocksDBException { Options opt = null; RocksDB db = null; @@ -65,7 +66,6 @@ public abstract class AbstractComparatorTest { } db.close(); - // re-open db and read from start to end // integer keys should be in ascending // order as defined by SimpleIntComparator @@ -84,9 +84,6 @@ public abstract class AbstractComparatorTest { assertThat(count).isEqualTo(ITERATIONS); - } catch (final RocksDBException e) { - System.err.format("[ERROR]: %s%n", e); - e.printStackTrace(); } finally { if (db != null) { db.close(); @@ -98,6 +95,96 @@ public abstract class AbstractComparatorTest { } } + /** + * Test which stores random keys into a column family + * in the database + * using an @see getAscendingIntKeyComparator + * it then checks that these keys are read back in + * ascending order + * + * @param db_path A path where we can store database + * files temporarily + * + * @throws java.io.IOException if IO error happens. + */ + public void testRoundtripCf(final Path db_path) throws IOException, + RocksDBException { + + DBOptions opt = null; + RocksDB db = null; + List cfDescriptors = + new ArrayList<>(); + cfDescriptors.add(new ColumnFamilyDescriptor( + RocksDB.DEFAULT_COLUMN_FAMILY)); + cfDescriptors.add(new ColumnFamilyDescriptor("new_cf", + new ColumnFamilyOptions().setComparator( + getAscendingIntKeyComparator()))); + List cfHandles = new ArrayList<>(); + try { + opt = new DBOptions(). + setCreateIfMissing(true). + setCreateMissingColumnFamilies(true); + + // store 10,000 random integer keys + final int ITERATIONS = 10000; + + db = RocksDB.open(opt, db_path.toString(), cfDescriptors, cfHandles); + assertThat(cfDescriptors.size()).isEqualTo(2); + assertThat(cfHandles.size()).isEqualTo(2); + + final Random random = new Random(); + for (int i = 0; i < ITERATIONS; i++) { + final byte key[] = intToByte(random.nextInt()); + if (i > 0 && db.get(cfHandles.get(1), key) != null) { // does key already exist (avoid duplicates) + i--; // generate a different key + } else { + db.put(cfHandles.get(1), key, "value".getBytes()); + } + } + for (ColumnFamilyHandle handle : cfHandles) { + handle.dispose(); + } + cfHandles.clear(); + db.close(); + + // re-open db and read from start to end + // integer keys should be in ascending + // order as defined by SimpleIntComparator + db = RocksDB.open(opt, db_path.toString(), cfDescriptors, cfHandles); + assertThat(cfDescriptors.size()).isEqualTo(2); + assertThat(cfHandles.size()).isEqualTo(2); + final RocksIterator it = db.newIterator(cfHandles.get(1)); + it.seekToFirst(); + int lastKey = Integer.MIN_VALUE; + int count = 0; + for (it.seekToFirst(); it.isValid(); it.next()) { + final int thisKey = byteToInt(it.key()); + assertThat(thisKey).isGreaterThan(lastKey); + lastKey = thisKey; + count++; + } + for (ColumnFamilyHandle handle : cfHandles) { + handle.dispose(); + } + cfHandles.clear(); + db.close(); + assertThat(count).isEqualTo(ITERATIONS); + + } finally { + for (ColumnFamilyHandle handle : cfHandles) { + handle.dispose(); + } + + if (db != null) { + db.close(); + } + + if (opt != null) { + opt.dispose(); + } + } + } + /** * Compares integer keys * so that they are in ascending order diff --git a/java/org/rocksdb/test/ComparatorTest.java b/java/org/rocksdb/test/ComparatorTest.java index 299d8f62d..290ff21f7 100644 --- a/java/org/rocksdb/test/ComparatorTest.java +++ b/java/org/rocksdb/test/ComparatorTest.java @@ -26,7 +26,7 @@ public class ComparatorTest { public TemporaryFolder dbFolder = new TemporaryFolder(); @Test - public void javaComparator() throws IOException { + public void javaComparator() throws IOException, RocksDBException { final AbstractComparatorTest comparatorTest = new AbstractComparatorTest() { @Override @@ -51,6 +51,32 @@ public class ComparatorTest { dbFolder.getRoot().getAbsolutePath())); } + @Test + public void javaComparatorCf() throws IOException, RocksDBException { + + final AbstractComparatorTest comparatorTest = new AbstractComparatorTest() { + @Override + public AbstractComparator getAscendingIntKeyComparator() { + return new Comparator(new ComparatorOptions()) { + + @Override + public String name() { + return "test.AscendingIntKeyComparator"; + } + + @Override + public int compare(final Slice a, final Slice b) { + return compareIntKeys(a.data(), b.data()); + } + }; + } + }; + + // test the round-tripability of keys written and read with the Comparator + comparatorTest.testRoundtripCf(FileSystems.getDefault().getPath( + dbFolder.getRoot().getAbsolutePath())); + } + @Test public void builtinForwardComparator() throws RocksDBException { diff --git a/java/org/rocksdb/test/DirectComparatorTest.java b/java/org/rocksdb/test/DirectComparatorTest.java index f09d94843..328ea0089 100644 --- a/java/org/rocksdb/test/DirectComparatorTest.java +++ b/java/org/rocksdb/test/DirectComparatorTest.java @@ -23,7 +23,7 @@ public class DirectComparatorTest { public TemporaryFolder dbFolder = new TemporaryFolder(); @Test - public void directComparator() throws IOException { + public void directComparator() throws IOException, RocksDBException { final AbstractComparatorTest comparatorTest = new AbstractComparatorTest() { @Override diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 339a3b095..d139b1a57 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -1874,9 +1874,9 @@ void Java_org_rocksdb_ColumnFamilyOptions_setComparatorHandle__JI( * Method: setComparatorHandle * Signature: (JJ)V */ -void Java_org_rocksdb_ColumnFamilyOptions_setComparatorHandle__JI( +void Java_org_rocksdb_ColumnFamilyOptions_setComparatorHandle__JJ( JNIEnv* env, jobject jobj, jlong jopt_handle, jlong jcomparator_handle) { - reinterpret_cast(jopt_handle)->comparator = + reinterpret_cast(jopt_handle)->comparator = reinterpret_cast(jcomparator_handle); }