[RocksJava] Integrated review comments from D28209

main
fyrz 10 years ago
parent a4b28c1ae7
commit 628e39e97d
  1. 11
      java/Makefile
  2. 1
      java/org/rocksdb/Options.java
  3. 244
      java/org/rocksdb/test/BackupableDBTest.java
  4. 229
      java/org/rocksdb/test/ComparatorTest.java
  5. 47
      java/org/rocksdb/test/FilterTest.java
  6. 80
      java/org/rocksdb/test/KeyMayExistTest.java
  7. 205
      java/org/rocksdb/test/MemTableTest.java
  8. 353
      java/org/rocksdb/test/MergeTest.java
  9. 553
      java/org/rocksdb/test/OptionsTest.java
  10. 441
      java/org/rocksdb/test/ReadOnlyTest.java
  11. 50
      java/org/rocksdb/test/ReadOptionsTest.java
  12. 39
      java/org/rocksdb/test/RocksEnvTest.java
  13. 120
      java/org/rocksdb/test/RocksIteratorTest.java
  14. 117
      java/org/rocksdb/test/SnapshotTest.java
  15. 52
      java/org/rocksdb/test/StatisticsCollectorTest.java
  16. 2
      java/rocksjni/backupablejni.cc
  17. 11
      java/rocksjni/options.cc

@ -66,6 +66,7 @@ JAVA_TESTS = org.rocksdb.test.AbstractComparatorTest\
org.rocksdb.test.PlainTableConfigTest\ org.rocksdb.test.PlainTableConfigTest\
org.rocksdb.test.ReadOnlyTest\ org.rocksdb.test.ReadOnlyTest\
org.rocksdb.test.ReadOptionsTest\ org.rocksdb.test.ReadOptionsTest\
org.rocksdb.test.RocksEnvTest\
org.rocksdb.test.RocksIteratorTest\ org.rocksdb.test.RocksIteratorTest\
org.rocksdb.test.SnapshotTest\ org.rocksdb.test.SnapshotTest\
org.rocksdb.test.StatisticsCollectorTest\ org.rocksdb.test.StatisticsCollectorTest\
@ -112,11 +113,11 @@ column_family_sample: java
resolve_test_deps: resolve_test_deps:
mkdir -p "$(JAVA_TEST_LIBDIR)" mkdir -p "$(JAVA_TEST_LIBDIR)"
test -s "$(JAVA_JUNIT_JAR)" || curl -L -o $(JAVA_JUNIT_JAR) http://search.maven.org/remotecontent?filepath=junit/junit/4.12-beta-2/junit-4.12-beta-2.jar test -s "$(JAVA_JUNIT_JAR)" || curl -k -L -o $(JAVA_JUNIT_JAR) http://search.maven.org/remotecontent?filepath=junit/junit/4.12-beta-2/junit-4.12-beta-2.jar
test -s "$(JAVA_HAMCR_JAR)" || curl -L -o $(JAVA_HAMCR_JAR) http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar test -s "$(JAVA_HAMCR_JAR)" || curl -k -L -o $(JAVA_HAMCR_JAR) http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
test -s "$(JAVA_MOCKITO_JAR)" || curl -L -o "$(JAVA_MOCKITO_JAR)" http://search.maven.org/remotecontent?filepath=org/mockito/mockito-all/1.9.5/mockito-all-1.9.5.jar test -s "$(JAVA_MOCKITO_JAR)" || curl -k -L -o "$(JAVA_MOCKITO_JAR)" http://search.maven.org/remotecontent?filepath=org/mockito/mockito-all/1.9.5/mockito-all-1.9.5.jar
test -s "$(JAVA_CGLIB_JAR)" || curl -L -o "$(JAVA_CGLIB_JAR)" http://search.maven.org/remotecontent?filepath=cglib/cglib/2.2.2/cglib-2.2.2.jar test -s "$(JAVA_CGLIB_JAR)" || curl -k -L -o "$(JAVA_CGLIB_JAR)" http://search.maven.org/remotecontent?filepath=cglib/cglib/2.2.2/cglib-2.2.2.jar
test -s "$(JAVA_ASSERTJ_JAR)" || curl -L -o "$(JAVA_ASSERTJ_JAR)" http://central.maven.org/maven2/org/assertj/assertj-core/1.7.0/assertj-core-1.7.0.jar test -s "$(JAVA_ASSERTJ_JAR)" || curl -k -L -o "$(JAVA_ASSERTJ_JAR)" http://central.maven.org/maven2/org/assertj/assertj-core/1.7.0/assertj-core-1.7.0.jar
test: java resolve_test_deps test: java resolve_test_deps
javac -cp $(JAVA_TESTCLASSPATH) org/rocksdb/test/*.java javac -cp $(JAVA_TESTCLASSPATH) org/rocksdb/test/*.java

@ -1010,7 +1010,6 @@ public class Options extends RocksObject
long cfOptHandle); long cfOptHandle);
private native void disposeInternal(long handle); private native void disposeInternal(long handle);
private native void setEnv(long optHandle, long envHandle); private native void setEnv(long optHandle, long envHandle);
private native long getEnvHandle(long handle);
private native void prepareForBulkLoad(long handle); private native void prepareForBulkLoad(long handle);
// DB native handles // DB native handles

@ -29,117 +29,137 @@ public class BackupableDBTest {
@Test @Test
public void backupableDb() throws RocksDBException { public void backupableDb() throws RocksDBException {
Options opt = null;
Options opt = new Options(); BackupableDBOptions bopt = null;
opt.setCreateIfMissing(true); BackupableDB bdb = null;
RestoreOptions ropt = null;
BackupableDBOptions bopt = new BackupableDBOptions( RestoreBackupableDB rdb = null;
backupFolder.getRoot().getAbsolutePath(), false, try {
true, false, true, 0, 0); opt = new Options();
BackupableDB bdb; opt.setCreateIfMissing(true);
List<BackupInfo> backupInfos;
List<BackupInfo> restoreInfos; bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath(), false,
bdb = BackupableDB.open(opt, bopt, true, false, true, 0, 0);
dbFolder.getRoot().getAbsolutePath()); assertThat(bopt.backupDir()).isEqualTo(
bdb.put("abc".getBytes(), "def".getBytes()); backupFolder.getRoot().getAbsolutePath());
bdb.put("ghi".getBytes(), "jkl".getBytes());
List<BackupInfo> backupInfos;
backupInfos = bdb.getBackupInfos(); List<BackupInfo> restoreInfos;
assertThat(backupInfos.size()).
isEqualTo(0); bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
bdb.createNewBackup(true); bdb.put("abc".getBytes(), "def".getBytes());
backupInfos = bdb.getBackupInfos(); bdb.put("ghi".getBytes(), "jkl".getBytes());
assertThat(backupInfos.size()).
isEqualTo(1); backupInfos = bdb.getBackupInfos();
assertThat(backupInfos.size()).
// Retrieving backup infos twice shall not isEqualTo(0);
// lead to different results
List<BackupInfo> tmpBackupInfo = bdb.getBackupInfos(); bdb.createNewBackup(true);
assertThat(tmpBackupInfo.get(0).backupId()). backupInfos = bdb.getBackupInfos();
isEqualTo(backupInfos.get(0).backupId()); assertThat(backupInfos.size()).
assertThat(tmpBackupInfo.get(0).timestamp()). isEqualTo(1);
isEqualTo(backupInfos.get(0).timestamp());
assertThat(tmpBackupInfo.get(0).size()). // Retrieving backup infos twice shall not
isEqualTo(backupInfos.get(0).size()); // lead to different results
assertThat(tmpBackupInfo.get(0).numberFiles()). List<BackupInfo> tmpBackupInfo = bdb.getBackupInfos();
isEqualTo(backupInfos.get(0).numberFiles()); assertThat(tmpBackupInfo.get(0).backupId()).
isEqualTo(backupInfos.get(0).backupId());
// delete record after backup assertThat(tmpBackupInfo.get(0).timestamp()).
bdb.remove("abc".getBytes()); isEqualTo(backupInfos.get(0).timestamp());
byte[] value = bdb.get("abc".getBytes()); assertThat(tmpBackupInfo.get(0).size()).
assertThat(value).isNull(); isEqualTo(backupInfos.get(0).size());
bdb.close(); assertThat(tmpBackupInfo.get(0).numberFiles()).
isEqualTo(backupInfos.get(0).numberFiles());
// restore from backup
RestoreOptions ropt = new RestoreOptions(false); // delete record after backup
RestoreBackupableDB rdb = new RestoreBackupableDB(bopt); bdb.remove("abc".getBytes());
byte[] value = bdb.get("abc".getBytes());
// getting backup infos from restorable db should assertThat(value).isNull();
// lead to the same infos as from backupable db bdb.close();
restoreInfos = rdb.getBackupInfos();
assertThat(restoreInfos.size()). // restore from backup
isEqualTo(backupInfos.size()); ropt = new RestoreOptions(false);
assertThat(restoreInfos.get(0).backupId()). rdb = new RestoreBackupableDB(bopt);
isEqualTo(backupInfos.get(0).backupId());
assertThat(restoreInfos.get(0).timestamp()). // getting backup infos from restorable db should
isEqualTo(backupInfos.get(0).timestamp()); // lead to the same infos as from backupable db
assertThat(restoreInfos.get(0).size()). restoreInfos = rdb.getBackupInfos();
isEqualTo(backupInfos.get(0).size()); assertThat(restoreInfos.size()).
assertThat(restoreInfos.get(0).numberFiles()). isEqualTo(backupInfos.size());
isEqualTo(backupInfos.get(0).numberFiles()); assertThat(restoreInfos.get(0).backupId()).
isEqualTo(backupInfos.get(0).backupId());
rdb.restoreDBFromLatestBackup( assertThat(restoreInfos.get(0).timestamp()).
dbFolder.getRoot().getAbsolutePath(), isEqualTo(backupInfos.get(0).timestamp());
dbFolder.getRoot().getAbsolutePath(), assertThat(restoreInfos.get(0).size()).
ropt); isEqualTo(backupInfos.get(0).size());
// do nothing because there is only one backup assertThat(restoreInfos.get(0).numberFiles()).
rdb.purgeOldBackups(1); isEqualTo(backupInfos.get(0).numberFiles());
restoreInfos = rdb.getBackupInfos();
assertThat(restoreInfos.size()). rdb.restoreDBFromLatestBackup(
isEqualTo(1); dbFolder.getRoot().getAbsolutePath(),
rdb.dispose(); dbFolder.getRoot().getAbsolutePath(),
ropt.dispose(); ropt);
// do nothing because there is only one backup
// verify that backed up data contains deleted record rdb.purgeOldBackups(1);
bdb = BackupableDB.open(opt, bopt, restoreInfos = rdb.getBackupInfos();
dbFolder.getRoot().getAbsolutePath()); assertThat(restoreInfos.size()).
value = bdb.get("abc".getBytes()); isEqualTo(1);
assertThat(new String(value)). rdb.dispose();
isEqualTo("def"); ropt.dispose();
bdb.createNewBackup(false); // verify that backed up data contains deleted record
// after new backup there must be two backup infos bdb = BackupableDB.open(opt, bopt,
backupInfos = bdb.getBackupInfos(); dbFolder.getRoot().getAbsolutePath());
assertThat(backupInfos.size()). value = bdb.get("abc".getBytes());
isEqualTo(2); assertThat(new String(value)).
// deleting the backup must be possible using the isEqualTo("def");
// id provided by backup infos
bdb.deleteBackup(backupInfos.get(1).backupId()); bdb.createNewBackup(false);
// after deletion there should only be one info // after new backup there must be two backup infos
backupInfos = bdb.getBackupInfos(); backupInfos = bdb.getBackupInfos();
assertThat(backupInfos.size()). assertThat(backupInfos.size()).
isEqualTo(1); isEqualTo(2);
bdb.createNewBackup(false); // deleting the backup must be possible using the
bdb.createNewBackup(false); // id provided by backup infos
bdb.createNewBackup(false); bdb.deleteBackup(backupInfos.get(1).backupId());
backupInfos = bdb.getBackupInfos(); // after deletion there should only be one info
assertThat(backupInfos.size()). backupInfos = bdb.getBackupInfos();
isEqualTo(4); assertThat(backupInfos.size()).
// purge everything and keep two isEqualTo(1);
bdb.purgeOldBackups(2); bdb.createNewBackup(false);
// backup infos need to be two bdb.createNewBackup(false);
backupInfos = bdb.getBackupInfos(); bdb.createNewBackup(false);
assertThat(backupInfos.size()). backupInfos = bdb.getBackupInfos();
isEqualTo(2); assertThat(backupInfos.size()).
assertThat(backupInfos.get(0).backupId()). isEqualTo(4);
isEqualTo(4); // purge everything and keep two
assertThat(backupInfos.get(1).backupId()). bdb.purgeOldBackups(2);
isEqualTo(5); // backup infos need to be two
backupInfos = bdb.getBackupInfos();
opt.dispose(); assertThat(backupInfos.size()).
bopt.dispose(); isEqualTo(2);
bdb.close(); assertThat(backupInfos.get(0).backupId()).
isEqualTo(4);
assertThat(backupInfos.get(1).backupId()).
isEqualTo(5);
} finally {
if (opt != null) {
opt.dispose();
}
if (bopt != null) {
bopt.dispose();
}
if (bdb != null) {
bdb.close();
}
if (ropt != null) {
ropt.dispose();
}
if (rdb != null) {
rdb.dispose();
}
}
} }
} }

@ -54,113 +54,138 @@ public class ComparatorTest {
@Test @Test
public void builtinForwardComparator() public void builtinForwardComparator()
throws RocksDBException { throws RocksDBException {
Options options = new Options(); Options options = null;
options.setCreateIfMissing(true); RocksDB rocksDB = null;
options.setComparator(BuiltinComparator.BYTEWISE_COMPARATOR); RocksIterator rocksIterator = null;
RocksDB rocksDB = RocksDB.open(options, try {
dbFolder.getRoot().getAbsolutePath()); options = new Options();
options.setCreateIfMissing(true);
rocksDB.put("abc1".getBytes(), "abc1".getBytes()); options.setComparator(BuiltinComparator.BYTEWISE_COMPARATOR);
rocksDB.put("abc2".getBytes(), "abc2".getBytes()); rocksDB = RocksDB.open(options,
rocksDB.put("abc3".getBytes(), "abc3".getBytes()); dbFolder.getRoot().getAbsolutePath());
RocksIterator rocksIterator = rocksDB.newIterator(); rocksDB.put("abc1".getBytes(), "abc1".getBytes());
// Iterate over keys using a iterator rocksDB.put("abc2".getBytes(), "abc2".getBytes());
rocksIterator.seekToFirst(); rocksDB.put("abc3".getBytes(), "abc3".getBytes());
assertThat(rocksIterator.isValid()).isTrue();
assertThat(rocksIterator.key()).isEqualTo( rocksIterator = rocksDB.newIterator();
"abc1".getBytes()); // Iterate over keys using a iterator
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.seekToFirst();
"abc1".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
rocksIterator.next(); assertThat(rocksIterator.key()).isEqualTo(
assertThat(rocksIterator.isValid()).isTrue(); "abc1".getBytes());
assertThat(rocksIterator.key()).isEqualTo( assertThat(rocksIterator.value()).isEqualTo(
"abc2".getBytes()); "abc1".getBytes());
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.next();
"abc2".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
rocksIterator.next(); assertThat(rocksIterator.key()).isEqualTo(
assertThat(rocksIterator.isValid()).isTrue(); "abc2".getBytes());
assertThat(rocksIterator.key()).isEqualTo( assertThat(rocksIterator.value()).isEqualTo(
"abc3".getBytes()); "abc2".getBytes());
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.next();
"abc3".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
rocksIterator.next(); assertThat(rocksIterator.key()).isEqualTo(
assertThat(rocksIterator.isValid()).isFalse(); "abc3".getBytes());
// Get last one assertThat(rocksIterator.value()).isEqualTo(
rocksIterator.seekToLast(); "abc3".getBytes());
assertThat(rocksIterator.isValid()).isTrue(); rocksIterator.next();
assertThat(rocksIterator.key()).isEqualTo( assertThat(rocksIterator.isValid()).isFalse();
"abc3".getBytes()); // Get last one
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.seekToLast();
"abc3".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
// Seek for abc assertThat(rocksIterator.key()).isEqualTo(
rocksIterator.seek("abc".getBytes()); "abc3".getBytes());
assertThat(rocksIterator.isValid()).isTrue(); assertThat(rocksIterator.value()).isEqualTo(
assertThat(rocksIterator.key()).isEqualTo( "abc3".getBytes());
"abc1".getBytes()); // Seek for abc
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.seek("abc".getBytes());
"abc1".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
rocksIterator.dispose(); assertThat(rocksIterator.key()).isEqualTo(
rocksDB.close(); "abc1".getBytes());
options.dispose(); assertThat(rocksIterator.value()).isEqualTo(
"abc1".getBytes());
} finally {
if (rocksIterator != null) {
rocksIterator.dispose();
}
if (rocksDB != null) {
rocksDB.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test @Test
public void builtinReverseComparator() public void builtinReverseComparator()
throws RocksDBException { throws RocksDBException {
Options options = new Options(); Options options = null;
options.setCreateIfMissing(true); RocksDB rocksDB = null;
options.setComparator( RocksIterator rocksIterator = null;
BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR); try {
RocksDB rocksDB = RocksDB.open(options, options = new Options();
dbFolder.getRoot().getAbsolutePath()); options.setCreateIfMissing(true);
options.setComparator(
rocksDB.put("abc1".getBytes(), "abc1".getBytes()); BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR);
rocksDB.put("abc2".getBytes(), "abc2".getBytes()); rocksDB = RocksDB.open(options,
rocksDB.put("abc3".getBytes(), "abc3".getBytes()); dbFolder.getRoot().getAbsolutePath());
RocksIterator rocksIterator = rocksDB.newIterator(); rocksDB.put("abc1".getBytes(), "abc1".getBytes());
// Iterate over keys using a iterator rocksDB.put("abc2".getBytes(), "abc2".getBytes());
rocksIterator.seekToFirst(); rocksDB.put("abc3".getBytes(), "abc3".getBytes());
assertThat(rocksIterator.isValid()).isTrue();
assertThat(rocksIterator.key()).isEqualTo( rocksIterator = rocksDB.newIterator();
"abc3".getBytes()); // Iterate over keys using a iterator
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.seekToFirst();
"abc3".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
rocksIterator.next(); assertThat(rocksIterator.key()).isEqualTo(
assertThat(rocksIterator.isValid()).isTrue(); "abc3".getBytes());
assertThat(rocksIterator.key()).isEqualTo( assertThat(rocksIterator.value()).isEqualTo(
"abc2".getBytes()); "abc3".getBytes());
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.next();
"abc2".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
rocksIterator.next(); assertThat(rocksIterator.key()).isEqualTo(
assertThat(rocksIterator.isValid()).isTrue(); "abc2".getBytes());
assertThat(rocksIterator.key()).isEqualTo( assertThat(rocksIterator.value()).isEqualTo(
"abc1".getBytes()); "abc2".getBytes());
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.next();
"abc1".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
rocksIterator.next(); assertThat(rocksIterator.key()).isEqualTo(
assertThat(rocksIterator.isValid()).isFalse(); "abc1".getBytes());
// Get last one assertThat(rocksIterator.value()).isEqualTo(
rocksIterator.seekToLast(); "abc1".getBytes());
assertThat(rocksIterator.isValid()).isTrue(); rocksIterator.next();
assertThat(rocksIterator.key()).isEqualTo( assertThat(rocksIterator.isValid()).isFalse();
"abc1".getBytes()); // Get last one
assertThat(rocksIterator.value()).isEqualTo( rocksIterator.seekToLast();
"abc1".getBytes()); assertThat(rocksIterator.isValid()).isTrue();
// Will be invalid because abc is after abc1 assertThat(rocksIterator.key()).isEqualTo(
rocksIterator.seek("abc".getBytes()); "abc1".getBytes());
assertThat(rocksIterator.isValid()).isFalse(); assertThat(rocksIterator.value()).isEqualTo(
// Will be abc3 because the next one after abc999 "abc1".getBytes());
// is abc3 // Will be invalid because abc is after abc1
rocksIterator.seek("abc999".getBytes()); rocksIterator.seek("abc".getBytes());
assertThat(rocksIterator.key()).isEqualTo( assertThat(rocksIterator.isValid()).isFalse();
"abc3".getBytes()); // Will be abc3 because the next one after abc999
assertThat(rocksIterator.value()).isEqualTo( // is abc3
"abc3".getBytes()); rocksIterator.seek("abc999".getBytes());
rocksIterator.dispose(); assertThat(rocksIterator.key()).isEqualTo(
rocksDB.close(); "abc3".getBytes());
options.dispose(); assertThat(rocksIterator.value()).isEqualTo(
"abc3".getBytes());
} finally {
if (rocksIterator != null) {
rocksIterator.dispose();
}
if (rocksDB != null) {
rocksDB.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test @Test

@ -17,25 +17,32 @@ public class FilterTest {
@Test @Test
public void filter() { public void filter() {
Options options = new Options(); Options options = null;
// test table config try {
options.setTableFormatConfig(new BlockBasedTableConfig(). options = new Options();
setFilter(new BloomFilter())); // test table config
options.dispose(); options.setTableFormatConfig(new BlockBasedTableConfig().
System.gc(); setFilter(new BloomFilter()));
System.runFinalization(); options.dispose();
// new Bloom filter System.gc();
options = new Options(); System.runFinalization();
BlockBasedTableConfig blockConfig = new BlockBasedTableConfig(); // new Bloom filter
blockConfig.setFilter(new BloomFilter()); options = new Options();
options.setTableFormatConfig(blockConfig); BlockBasedTableConfig blockConfig = new BlockBasedTableConfig();
BloomFilter bloomFilter = new BloomFilter(10); blockConfig.setFilter(new BloomFilter());
blockConfig.setFilter(bloomFilter); options.setTableFormatConfig(blockConfig);
options.setTableFormatConfig(blockConfig); BloomFilter bloomFilter = new BloomFilter(10);
System.gc(); blockConfig.setFilter(bloomFilter);
System.runFinalization(); options.setTableFormatConfig(blockConfig);
blockConfig.setFilter(new BloomFilter(10, false)); System.gc();
options.setTableFormatConfig(blockConfig); System.runFinalization();
options.dispose(); blockConfig.setFilter(new BloomFilter(10, false));
options.setTableFormatConfig(blockConfig);
} finally {
if (options != null) {
options.dispose();
}
}
} }
} }

@ -26,6 +26,7 @@ public class KeyMayExistTest {
@Test @Test
public void keyMayExist() throws RocksDBException { public void keyMayExist() throws RocksDBException {
<<<<<<< HEAD
RocksDB db; RocksDB db;
DBOptions options = new DBOptions(); DBOptions options = new DBOptions();
options.setCreateIfMissing(true) options.setCreateIfMissing(true)
@ -49,23 +50,50 @@ public class KeyMayExistTest {
assertThat(exists).isTrue(); assertThat(exists).isTrue();
assertThat(retValue.toString()). assertThat(retValue.toString()).
isEqualTo("value"); isEqualTo("value");
=======
RocksDB db = null;
Options options = null;
try {
options = new Options();
options.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
// open database using cf names
List<String> cfNames = new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
cfNames.add("default");
cfNames.add("new_cf");
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
assertThat(columnFamilyHandleList.size()).
isEqualTo(2);
db.put("key".getBytes(), "value".getBytes());
// Test without column family
StringBuffer retValue = new StringBuffer();
boolean exists = db.keyMayExist("key".getBytes(), retValue);
assertThat(exists).isTrue();
assertThat(retValue.toString()).
isEqualTo("value");
>>>>>>> [RocksJava] Integrated review comments from D28209
// Test without column family but with readOptions // Test without column family but with readOptions
retValue = new StringBuffer(); retValue = new StringBuffer();
exists = db.keyMayExist(new ReadOptions(), "key".getBytes(), exists = db.keyMayExist(new ReadOptions(), "key".getBytes(),
retValue); retValue);
assertThat(exists).isTrue(); assertThat(exists).isTrue();
assertThat(retValue.toString()). assertThat(retValue.toString()).
isEqualTo("value"); isEqualTo("value");
// Test with column family // Test with column family
retValue = new StringBuffer(); retValue = new StringBuffer();
exists = db.keyMayExist(columnFamilyHandleList.get(0), "key".getBytes(), exists = db.keyMayExist(columnFamilyHandleList.get(0), "key".getBytes(),
retValue); retValue);
assertThat(exists).isTrue(); assertThat(exists).isTrue();
assertThat(retValue.toString()). assertThat(retValue.toString()).
isEqualTo("value"); isEqualTo("value");
<<<<<<< HEAD
// Test with column family and readOptions // Test with column family and readOptions
retValue = new StringBuffer(); retValue = new StringBuffer();
exists = db.keyMayExist(new ReadOptions(), exists = db.keyMayExist(new ReadOptions(),
@ -74,9 +102,27 @@ public class KeyMayExistTest {
assertThat(exists).isTrue(); assertThat(exists).isTrue();
assertThat(retValue.toString()). assertThat(retValue.toString()).
isEqualTo("value"); isEqualTo("value");
=======
// Test with column family and readOptions
retValue = new StringBuffer();
exists = db.keyMayExist(new ReadOptions(),
columnFamilyHandleList.get(0), "key".getBytes(),
retValue);
assertThat(exists).isTrue();
assertThat(retValue.toString()).
isEqualTo("value");
>>>>>>> [RocksJava] Integrated review comments from D28209
// KeyMayExist in CF1 must return false // KeyMayExist in CF1 must return false
assertThat(db.keyMayExist(columnFamilyHandleList.get(1), assertThat(db.keyMayExist(columnFamilyHandleList.get(1),
"key".getBytes(), retValue)).isFalse(); "key".getBytes(), retValue)).isFalse();
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
} }
} }

@ -18,94 +18,121 @@ public class MemTableTest {
new RocksMemoryResource(); new RocksMemoryResource();
@Test @Test
public void memTable() throws RocksDBException { public void hashSkipListMemTable() throws RocksDBException {
Options options = new Options(); Options options = null;
// Test HashSkipListMemTableConfig try {
HashSkipListMemTableConfig memTableConfig = options = new Options();
new HashSkipListMemTableConfig(); // Test HashSkipListMemTableConfig
assertThat(memTableConfig.bucketCount()). HashSkipListMemTableConfig memTableConfig =
isEqualTo(1000000); new HashSkipListMemTableConfig();
memTableConfig.setBucketCount(2000000); assertThat(memTableConfig.bucketCount()).
assertThat(memTableConfig.bucketCount()). isEqualTo(1000000);
isEqualTo(2000000); memTableConfig.setBucketCount(2000000);
assertThat(memTableConfig.height()). assertThat(memTableConfig.bucketCount()).
isEqualTo(4); isEqualTo(2000000);
memTableConfig.setHeight(5); assertThat(memTableConfig.height()).
assertThat(memTableConfig.height()). isEqualTo(4);
isEqualTo(5); memTableConfig.setHeight(5);
assertThat(memTableConfig.branchingFactor()). assertThat(memTableConfig.height()).
isEqualTo(4); isEqualTo(5);
memTableConfig.setBranchingFactor(6); assertThat(memTableConfig.branchingFactor()).
assertThat(memTableConfig.branchingFactor()). isEqualTo(4);
isEqualTo(6); memTableConfig.setBranchingFactor(6);
options.setMemTableConfig(memTableConfig); assertThat(memTableConfig.branchingFactor()).
options.dispose(); isEqualTo(6);
System.gc(); options.setMemTableConfig(memTableConfig);
System.runFinalization(); } finally {
// Test SkipList if (options != null) {
options = new Options(); options.dispose();
SkipListMemTableConfig skipMemTableConfig = }
new SkipListMemTableConfig(); }
assertThat(skipMemTableConfig.lookahead()). }
isEqualTo(0);
skipMemTableConfig.setLookahead(20); @Test
assertThat(skipMemTableConfig.lookahead()). public void skipListMemTable() throws RocksDBException {
isEqualTo(20); Options options = null;
options.setMemTableConfig(skipMemTableConfig); try {
options.dispose(); options = new Options();
System.gc(); SkipListMemTableConfig skipMemTableConfig =
System.runFinalization(); new SkipListMemTableConfig();
// Test HashLinkedListMemTableConfig assertThat(skipMemTableConfig.lookahead()).
options = new Options(); isEqualTo(0);
HashLinkedListMemTableConfig hashLinkedListMemTableConfig = skipMemTableConfig.setLookahead(20);
new HashLinkedListMemTableConfig(); assertThat(skipMemTableConfig.lookahead()).
assertThat(hashLinkedListMemTableConfig.bucketCount()). isEqualTo(20);
isEqualTo(50000); options.setMemTableConfig(skipMemTableConfig);
hashLinkedListMemTableConfig.setBucketCount(100000); options.dispose();
assertThat(hashLinkedListMemTableConfig.bucketCount()). } finally {
isEqualTo(100000); if (options != null) {
assertThat(hashLinkedListMemTableConfig.hugePageTlbSize()). options.dispose();
isEqualTo(0); }
hashLinkedListMemTableConfig.setHugePageTlbSize(1); }
assertThat(hashLinkedListMemTableConfig.hugePageTlbSize()). }
isEqualTo(1);
assertThat(hashLinkedListMemTableConfig. @Test
bucketEntriesLoggingThreshold()). public void hashLinkedListMemTable() throws RocksDBException {
isEqualTo(4096); Options options = null;
hashLinkedListMemTableConfig. try {
setBucketEntriesLoggingThreshold(200); options = new Options();
assertThat(hashLinkedListMemTableConfig. HashLinkedListMemTableConfig hashLinkedListMemTableConfig =
bucketEntriesLoggingThreshold()). new HashLinkedListMemTableConfig();
isEqualTo(200); assertThat(hashLinkedListMemTableConfig.bucketCount()).
assertThat(hashLinkedListMemTableConfig. isEqualTo(50000);
ifLogBucketDistWhenFlush()).isTrue(); hashLinkedListMemTableConfig.setBucketCount(100000);
hashLinkedListMemTableConfig. assertThat(hashLinkedListMemTableConfig.bucketCount()).
setIfLogBucketDistWhenFlush(false); isEqualTo(100000);
assertThat(hashLinkedListMemTableConfig. assertThat(hashLinkedListMemTableConfig.hugePageTlbSize()).
ifLogBucketDistWhenFlush()).isFalse(); isEqualTo(0);
assertThat(hashLinkedListMemTableConfig. hashLinkedListMemTableConfig.setHugePageTlbSize(1);
thresholdUseSkiplist()). assertThat(hashLinkedListMemTableConfig.hugePageTlbSize()).
isEqualTo(256); isEqualTo(1);
hashLinkedListMemTableConfig.setThresholdUseSkiplist(29); assertThat(hashLinkedListMemTableConfig.
assertThat(hashLinkedListMemTableConfig. bucketEntriesLoggingThreshold()).
thresholdUseSkiplist()). isEqualTo(4096);
isEqualTo(29); hashLinkedListMemTableConfig.
options.setMemTableConfig(hashLinkedListMemTableConfig); setBucketEntriesLoggingThreshold(200);
options.dispose(); assertThat(hashLinkedListMemTableConfig.
System.gc(); bucketEntriesLoggingThreshold()).
System.runFinalization(); isEqualTo(200);
// test VectorMemTableConfig assertThat(hashLinkedListMemTableConfig.
options = new Options(); ifLogBucketDistWhenFlush()).isTrue();
VectorMemTableConfig vectorMemTableConfig = hashLinkedListMemTableConfig.
new VectorMemTableConfig(); setIfLogBucketDistWhenFlush(false);
assertThat(vectorMemTableConfig.reservedSize()). assertThat(hashLinkedListMemTableConfig.
isEqualTo(0); ifLogBucketDistWhenFlush()).isFalse();
vectorMemTableConfig.setReservedSize(123); assertThat(hashLinkedListMemTableConfig.
assertThat(vectorMemTableConfig.reservedSize()). thresholdUseSkiplist()).
isEqualTo(123); isEqualTo(256);
options.setMemTableConfig(vectorMemTableConfig); hashLinkedListMemTableConfig.setThresholdUseSkiplist(29);
options.dispose(); assertThat(hashLinkedListMemTableConfig.
System.gc(); thresholdUseSkiplist()).
System.runFinalization(); isEqualTo(29);
options.setMemTableConfig(hashLinkedListMemTableConfig);
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void vectorMemTable() throws RocksDBException {
Options options = null;
try {
options = new Options();
VectorMemTableConfig vectorMemTableConfig =
new VectorMemTableConfig();
assertThat(vectorMemTableConfig.reservedSize()).
isEqualTo(0);
vectorMemTableConfig.setReservedSize(123);
assertThat(vectorMemTableConfig.reservedSize()).
isEqualTo(123);
options.setMemTableConfig(vectorMemTableConfig);
options.dispose();
} finally {
if (options != null) {
options.dispose();
}
}
} }
} }

@ -28,180 +28,227 @@ public class MergeTest {
@Test @Test
public void stringOption() public void stringOption()
throws InterruptedException, RocksDBException { throws InterruptedException, RocksDBException {
String db_path_string = RocksDB db = null;
dbFolder.getRoot().getAbsolutePath(); Options opt = null;
Options opt = new Options(); try {
opt.setCreateIfMissing(true); String db_path_string =
opt.setMergeOperatorName("stringappend"); dbFolder.getRoot().getAbsolutePath();
opt = new Options();
RocksDB db = RocksDB.open(opt, db_path_string); opt.setCreateIfMissing(true);
// writing aa under key opt.setMergeOperatorName("stringappend");
db.put("key".getBytes(), "aa".getBytes());
// merge bb under key db = RocksDB.open(opt, db_path_string);
db.merge("key".getBytes(), "bb".getBytes()); // writing aa under key
db.put("key".getBytes(), "aa".getBytes());
byte[] value = db.get("key".getBytes()); // merge bb under key
String strValue = new String(value); db.merge("key".getBytes(), "bb".getBytes());
db.close(); byte[] value = db.get("key".getBytes());
opt.dispose(); String strValue = new String(value);
assertThat(strValue).isEqualTo("aa,bb"); assertThat(strValue).isEqualTo("aa,bb");
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
} }
@Test @Test
public void cFStringOption() public void cFStringOption()
throws InterruptedException, RocksDBException { throws InterruptedException, RocksDBException {
DBOptions opt = new DBOptions(); RocksDB db = null;
String db_path_string = DBOptions opt = null;
dbFolder.getRoot().getAbsolutePath();
opt.setCreateIfMissing(true);
opt.setCreateMissingColumnFamilies(true);
List<ColumnFamilyDescriptor> cfDescr =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList = List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>(); new ArrayList<>();
cfDescr.add(new ColumnFamilyDescriptor("default", try {
new ColumnFamilyOptions().setMergeOperatorName( String db_path_string =
"stringappend"))); dbFolder.getRoot().getAbsolutePath();
cfDescr.add(new ColumnFamilyDescriptor("default", opt = new DBOptions();
new ColumnFamilyOptions().setMergeOperatorName( opt.setCreateIfMissing(true);
"stringappend"))); opt.setCreateMissingColumnFamilies(true);
RocksDB db = RocksDB.open(opt, db_path_string,
cfDescr, columnFamilyHandleList); List<ColumnFamilyDescriptor> cfDescriptors =
new ArrayList<>();
// writing aa under key cfDescriptors.add(new ColumnFamilyDescriptor("default",
db.put(columnFamilyHandleList.get(1), new ColumnFamilyOptions().setMergeOperatorName(
"cfkey".getBytes(), "aa".getBytes()); "stringappend")));
// merge bb under key cfDescriptors.add(new ColumnFamilyDescriptor("default",
db.merge(columnFamilyHandleList.get(1), new ColumnFamilyOptions().setMergeOperatorName(
"cfkey".getBytes(), "bb".getBytes()); "stringappend")));
db = RocksDB.open(opt, db_path_string,
byte[] value = db.get(columnFamilyHandleList.get(1), "cfkey".getBytes()); cfDescriptors, columnFamilyHandleList);
String strValue = new String(value);
// writing aa under key
for (ColumnFamilyHandle handle : columnFamilyHandleList) { db.put(columnFamilyHandleList.get(1),
handle.dispose(); "cfkey".getBytes(), "aa".getBytes());
// merge bb under key
db.merge(columnFamilyHandleList.get(1),
"cfkey".getBytes(), "bb".getBytes());
byte[] value = db.get(columnFamilyHandleList.get(1), "cfkey".getBytes());
String strValue = new String(value);
assertThat(strValue).isEqualTo("aa,bb");
} finally {
for (ColumnFamilyHandle handle : columnFamilyHandleList) {
handle.dispose();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
} }
db.close();
opt.dispose();
assertThat(strValue).isEqualTo("aa,bb");
} }
@Test @Test
public void operatorOption() public void operatorOption()
throws InterruptedException, RocksDBException { throws InterruptedException, RocksDBException {
String db_path_string = RocksDB db = null;
dbFolder.getRoot().getAbsolutePath(); Options opt = null;
Options opt = new Options(); try {
opt.setCreateIfMissing(true); String db_path_string =
dbFolder.getRoot().getAbsolutePath();
StringAppendOperator stringAppendOperator = new StringAppendOperator(); opt = new Options();
opt.setMergeOperator(stringAppendOperator); opt.setCreateIfMissing(true);
RocksDB db = RocksDB.open(opt, db_path_string); StringAppendOperator stringAppendOperator = new StringAppendOperator();
// Writing aa under key opt.setMergeOperator(stringAppendOperator);
db.put("key".getBytes(), "aa".getBytes());
db = RocksDB.open(opt, db_path_string);
// Writing bb under key // Writing aa under key
db.merge("key".getBytes(), "bb".getBytes()); db.put("key".getBytes(), "aa".getBytes());
byte[] value = db.get("key".getBytes()); // Writing bb under key
String strValue = new String(value); db.merge("key".getBytes(), "bb".getBytes());
db.close(); byte[] value = db.get("key".getBytes());
opt.dispose(); String strValue = new String(value);
assertThat(strValue).isEqualTo("aa,bb");
assertThat(strValue).isEqualTo("aa,bb");
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
} }
@Test @Test
public void cFOperatorOption() public void cFOperatorOption()
throws InterruptedException, RocksDBException { throws InterruptedException, RocksDBException {
DBOptions opt = new DBOptions(); RocksDB db = null;
String db_path_string = DBOptions opt = null;
dbFolder.getRoot().getAbsolutePath(); ColumnFamilyHandle columnFamilyHandle = null;
try {
opt.setCreateIfMissing(true); String db_path_string =
opt.setCreateMissingColumnFamilies(true); dbFolder.getRoot().getAbsolutePath();
StringAppendOperator stringAppendOperator = new StringAppendOperator(); opt = new DBOptions();
opt.setCreateIfMissing(true);
List<ColumnFamilyDescriptor> cfDescr = opt.setCreateMissingColumnFamilies(true);
new ArrayList<>(); StringAppendOperator stringAppendOperator = new StringAppendOperator();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>(); List<ColumnFamilyDescriptor> cfDescriptors =
cfDescr.add(new ColumnFamilyDescriptor("default", new ArrayList<>();
new ColumnFamilyOptions().setMergeOperator( List<ColumnFamilyHandle> columnFamilyHandleList =
stringAppendOperator))); new ArrayList<>();
cfDescr.add(new ColumnFamilyDescriptor("new_cf", cfDescriptors.add(new ColumnFamilyDescriptor("default",
new ColumnFamilyOptions().setMergeOperator( new ColumnFamilyOptions().setMergeOperator(
stringAppendOperator))); stringAppendOperator)));
RocksDB db = RocksDB.open(opt, db_path_string, cfDescriptors.add(new ColumnFamilyDescriptor("new_cf",
cfDescr, columnFamilyHandleList); new ColumnFamilyOptions().setMergeOperator(
// writing aa under key stringAppendOperator)));
db.put(columnFamilyHandleList.get(1), db = RocksDB.open(opt, db_path_string,
"cfkey".getBytes(), "aa".getBytes()); cfDescriptors, columnFamilyHandleList);
// merge bb under key
db.merge(columnFamilyHandleList.get(1), // writing aa under key
"cfkey".getBytes(), "bb".getBytes()); db.put(columnFamilyHandleList.get(1),
byte[] value = db.get(columnFamilyHandleList.get(1), "cfkey".getBytes()); "cfkey".getBytes(), "aa".getBytes());
String strValue = new String(value); // merge bb under key
db.merge(columnFamilyHandleList.get(1),
// Test also with createColumnFamily "cfkey".getBytes(), "bb".getBytes());
ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily( byte[] value = db.get(columnFamilyHandleList.get(1), "cfkey".getBytes());
new ColumnFamilyDescriptor("new_cf2", String strValue = new String(value);
new ColumnFamilyOptions().setMergeOperator(
new StringAppendOperator()))); // Test also with createColumnFamily
// writing xx under cfkey2 columnFamilyHandle = db.createColumnFamily(
db.put(columnFamilyHandle, "cfkey2".getBytes(), "xx".getBytes()); new ColumnFamilyDescriptor("new_cf2"));
// merge yy under cfkey2 // writing xx under cfkey2
db.merge(columnFamilyHandle, "cfkey2".getBytes(), "yy".getBytes()); db.put(columnFamilyHandle, "cfkey2".getBytes(), "xx".getBytes());
value = db.get(columnFamilyHandle, "cfkey2".getBytes()); // merge yy under cfkey2
String strValueTmpCf = new String(value); db.merge(columnFamilyHandle, "cfkey2".getBytes(), "yy".getBytes());
value = db.get(columnFamilyHandle, "cfkey2".getBytes());
columnFamilyHandle.dispose(); String strValueTmpCf = new String(value);
db.close();
opt.dispose(); columnFamilyHandle.dispose();
assertThat(strValue).isEqualTo("aa,bb"); assertThat(strValue).isEqualTo("aa,bb");
assertThat(strValueTmpCf).isEqualTo("xx,yy"); assertThat(strValueTmpCf).isEqualTo("xx,yy");
} finally {
if (columnFamilyHandle != null) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
} }
@Test @Test
public void operatorGcBehaviour() public void operatorGcBehaviour()
throws RocksDBException { throws RocksDBException {
String db_path_string = Options opt = null;
dbFolder.getRoot().getAbsolutePath(); RocksDB db = null;
Options opt = new Options(); try {
opt.setCreateIfMissing(true); String db_path_string =
StringAppendOperator stringAppendOperator = new StringAppendOperator(); dbFolder.getRoot().getAbsolutePath();
opt.setMergeOperator(stringAppendOperator); opt = new Options();
RocksDB db = RocksDB.open(opt, db_path_string); opt.setCreateIfMissing(true);
db.close(); StringAppendOperator stringAppendOperator = new StringAppendOperator();
opt.dispose(); opt.setMergeOperator(stringAppendOperator);
System.gc(); db = RocksDB.open(opt, db_path_string);
System.runFinalization(); db.close();
// test reuse opt.dispose();
opt = new Options(); System.gc();
opt.setMergeOperator(stringAppendOperator); System.runFinalization();
db = RocksDB.open(opt, db_path_string); // test reuse
db.close(); opt = new Options();
opt.dispose(); opt.setMergeOperator(stringAppendOperator);
System.gc(); db = RocksDB.open(opt, db_path_string);
System.runFinalization(); db.close();
// test param init opt.dispose();
opt = new Options(); System.gc();
opt.setMergeOperator(new StringAppendOperator()); System.runFinalization();
db = RocksDB.open(opt, db_path_string); // test param init
db.close(); opt = new Options();
opt.dispose(); opt.setMergeOperator(new StringAppendOperator());
System.gc(); db = RocksDB.open(opt, db_path_string);
System.runFinalization(); db.close();
// test replace one with another merge operator instance opt.dispose();
opt = new Options(); System.gc();
opt.setMergeOperator(stringAppendOperator); System.runFinalization();
StringAppendOperator newStringAppendOperator = new StringAppendOperator(); // test replace one with another merge operator instance
opt.setMergeOperator(newStringAppendOperator); opt = new Options();
db = RocksDB.open(opt, db_path_string); opt.setMergeOperator(stringAppendOperator);
db.close(); StringAppendOperator newStringAppendOperator = new StringAppendOperator();
opt.dispose(); opt.setMergeOperator(newStringAppendOperator);
db = RocksDB.open(opt, db_path_string);
db.close();
opt.dispose();
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
} }
} }

@ -21,281 +21,350 @@ public class OptionsTest {
@Test @Test
public void options() throws RocksDBException { public void options() throws RocksDBException {
Options opt = new Options(); Options opt = null;
Random rand = PlatformRandomHelper. try {
getPlatformSpecificRandomFactory(); opt = new Options();
Random rand = PlatformRandomHelper.
DBOptionsTest.testDBOptions(opt); getPlatformSpecificRandomFactory();
DBOptionsTest.testDBOptions(opt);
{ // WriteBufferSize test
long longValue = rand.nextLong(); { // WriteBufferSize test
opt.setWriteBufferSize(longValue); long longValue = rand.nextLong();
assert(opt.writeBufferSize() == longValue); opt.setWriteBufferSize(longValue);
} assert (opt.writeBufferSize() == longValue);
}
{ // MaxWriteBufferNumber test
int intValue = rand.nextInt(); { // MaxWriteBufferNumber test
opt.setMaxWriteBufferNumber(intValue); int intValue = rand.nextInt();
assert(opt.maxWriteBufferNumber() == intValue); opt.setMaxWriteBufferNumber(intValue);
} assert (opt.maxWriteBufferNumber() == intValue);
}
{ // MinWriteBufferNumberToMerge test
int intValue = rand.nextInt(); { // MinWriteBufferNumberToMerge test
opt.setMinWriteBufferNumberToMerge(intValue); int intValue = rand.nextInt();
assert(opt.minWriteBufferNumberToMerge() == intValue); opt.setMinWriteBufferNumberToMerge(intValue);
} assert (opt.minWriteBufferNumberToMerge() == intValue);
}
{ // NumLevels test
int intValue = rand.nextInt(); { // NumLevels test
opt.setNumLevels(intValue); int intValue = rand.nextInt();
assert(opt.numLevels() == intValue); opt.setNumLevels(intValue);
} assert (opt.numLevels() == intValue);
}
{ // LevelFileNumCompactionTrigger test
int intValue = rand.nextInt(); { // LevelFileNumCompactionTrigger test
opt.setLevelZeroFileNumCompactionTrigger(intValue); int intValue = rand.nextInt();
assert(opt.levelZeroFileNumCompactionTrigger() == intValue); opt.setLevelZeroFileNumCompactionTrigger(intValue);
} assert (opt.levelZeroFileNumCompactionTrigger() == intValue);
}
{ // LevelSlowdownWritesTrigger test
int intValue = rand.nextInt(); { // LevelSlowdownWritesTrigger test
opt.setLevelZeroSlowdownWritesTrigger(intValue); int intValue = rand.nextInt();
assert(opt.levelZeroSlowdownWritesTrigger() == intValue); opt.setLevelZeroSlowdownWritesTrigger(intValue);
} assert (opt.levelZeroSlowdownWritesTrigger() == intValue);
}
{ // LevelStopWritesTrigger test
int intValue = rand.nextInt(); { // LevelStopWritesTrigger test
opt.setLevelZeroStopWritesTrigger(intValue); int intValue = rand.nextInt();
assert(opt.levelZeroStopWritesTrigger() == intValue); opt.setLevelZeroStopWritesTrigger(intValue);
} assert (opt.levelZeroStopWritesTrigger() == intValue);
}
{ // MaxMemCompactionLevel test
int intValue = rand.nextInt(); { // MaxMemCompactionLevel test
opt.setMaxMemCompactionLevel(intValue); int intValue = rand.nextInt();
assert(opt.maxMemCompactionLevel() == intValue); opt.setMaxMemCompactionLevel(intValue);
} assert (opt.maxMemCompactionLevel() == intValue);
}
{ // TargetFileSizeBase test
long longValue = rand.nextLong(); { // TargetFileSizeBase test
opt.setTargetFileSizeBase(longValue); long longValue = rand.nextLong();
assert(opt.targetFileSizeBase() == longValue); opt.setTargetFileSizeBase(longValue);
} assert (opt.targetFileSizeBase() == longValue);
}
{ // TargetFileSizeMultiplier test
int intValue = rand.nextInt(); { // TargetFileSizeMultiplier test
opt.setTargetFileSizeMultiplier(intValue); int intValue = rand.nextInt();
assert(opt.targetFileSizeMultiplier() == intValue); opt.setTargetFileSizeMultiplier(intValue);
} assert (opt.targetFileSizeMultiplier() == intValue);
}
{ // MaxBytesForLevelBase test
long longValue = rand.nextLong(); { // MaxBytesForLevelBase test
opt.setMaxBytesForLevelBase(longValue); long longValue = rand.nextLong();
assert(opt.maxBytesForLevelBase() == longValue); opt.setMaxBytesForLevelBase(longValue);
} assert (opt.maxBytesForLevelBase() == longValue);
}
{ // MaxBytesForLevelMultiplier test
int intValue = rand.nextInt(); { // MaxBytesForLevelMultiplier test
opt.setMaxBytesForLevelMultiplier(intValue); int intValue = rand.nextInt();
assert(opt.maxBytesForLevelMultiplier() == intValue); opt.setMaxBytesForLevelMultiplier(intValue);
} assert (opt.maxBytesForLevelMultiplier() == intValue);
}
{ // ExpandedCompactionFactor test
int intValue = rand.nextInt(); { // ExpandedCompactionFactor test
opt.setExpandedCompactionFactor(intValue); int intValue = rand.nextInt();
assert(opt.expandedCompactionFactor() == intValue); opt.setExpandedCompactionFactor(intValue);
} assert (opt.expandedCompactionFactor() == intValue);
}
{ // SourceCompactionFactor test
int intValue = rand.nextInt(); { // SourceCompactionFactor test
opt.setSourceCompactionFactor(intValue); int intValue = rand.nextInt();
assert(opt.sourceCompactionFactor() == intValue); opt.setSourceCompactionFactor(intValue);
} assert (opt.sourceCompactionFactor() == intValue);
}
{ // MaxGrandparentOverlapFactor test
int intValue = rand.nextInt(); { // MaxGrandparentOverlapFactor test
opt.setMaxGrandparentOverlapFactor(intValue); int intValue = rand.nextInt();
assert(opt.maxGrandparentOverlapFactor() == intValue); opt.setMaxGrandparentOverlapFactor(intValue);
} assert (opt.maxGrandparentOverlapFactor() == intValue);
}
{ // SoftRateLimit test
double doubleValue = rand.nextDouble(); { // SoftRateLimit test
opt.setSoftRateLimit(doubleValue); double doubleValue = rand.nextDouble();
assert(opt.softRateLimit() == doubleValue); opt.setSoftRateLimit(doubleValue);
} assert (opt.softRateLimit() == doubleValue);
}
{ // HardRateLimit test
double doubleValue = rand.nextDouble(); { // HardRateLimit test
opt.setHardRateLimit(doubleValue); double doubleValue = rand.nextDouble();
assert(opt.hardRateLimit() == doubleValue); opt.setHardRateLimit(doubleValue);
} assert (opt.hardRateLimit() == doubleValue);
}
{ // RateLimitDelayMaxMilliseconds test
int intValue = rand.nextInt(); { // RateLimitDelayMaxMilliseconds test
opt.setRateLimitDelayMaxMilliseconds(intValue); int intValue = rand.nextInt();
assert(opt.rateLimitDelayMaxMilliseconds() == intValue); opt.setRateLimitDelayMaxMilliseconds(intValue);
} assert (opt.rateLimitDelayMaxMilliseconds() == intValue);
}
{ // ArenaBlockSize test
long longValue = rand.nextLong(); { // ArenaBlockSize test
opt.setArenaBlockSize(longValue); long longValue = rand.nextLong();
assert(opt.arenaBlockSize() == longValue); opt.setArenaBlockSize(longValue);
} assert (opt.arenaBlockSize() == longValue);
}
{ // DisableAutoCompactions test
boolean boolValue = rand.nextBoolean(); { // DisableAutoCompactions test
opt.setDisableAutoCompactions(boolValue); boolean boolValue = rand.nextBoolean();
assert(opt.disableAutoCompactions() == boolValue); opt.setDisableAutoCompactions(boolValue);
} assert (opt.disableAutoCompactions() == boolValue);
}
{ // PurgeRedundantKvsWhileFlush test
boolean boolValue = rand.nextBoolean(); { // PurgeRedundantKvsWhileFlush test
opt.setPurgeRedundantKvsWhileFlush(boolValue); boolean boolValue = rand.nextBoolean();
assert(opt.purgeRedundantKvsWhileFlush() == boolValue); opt.setPurgeRedundantKvsWhileFlush(boolValue);
} assert (opt.purgeRedundantKvsWhileFlush() == boolValue);
}
{ // VerifyChecksumsInCompaction test
boolean boolValue = rand.nextBoolean(); { // VerifyChecksumsInCompaction test
opt.setVerifyChecksumsInCompaction(boolValue); boolean boolValue = rand.nextBoolean();
assert(opt.verifyChecksumsInCompaction() == boolValue); opt.setVerifyChecksumsInCompaction(boolValue);
} assert (opt.verifyChecksumsInCompaction() == boolValue);
}
{ // FilterDeletes test
boolean boolValue = rand.nextBoolean(); { // FilterDeletes test
opt.setFilterDeletes(boolValue); boolean boolValue = rand.nextBoolean();
assert(opt.filterDeletes() == boolValue); opt.setFilterDeletes(boolValue);
} assert (opt.filterDeletes() == boolValue);
}
{ // MaxSequentialSkipInIterations test
long longValue = rand.nextLong(); { // MaxSequentialSkipInIterations test
opt.setMaxSequentialSkipInIterations(longValue); long longValue = rand.nextLong();
assert(opt.maxSequentialSkipInIterations() == longValue); opt.setMaxSequentialSkipInIterations(longValue);
} assert (opt.maxSequentialSkipInIterations() == longValue);
}
{ // InplaceUpdateSupport test
boolean boolValue = rand.nextBoolean(); { // InplaceUpdateSupport test
opt.setInplaceUpdateSupport(boolValue); boolean boolValue = rand.nextBoolean();
assert(opt.inplaceUpdateSupport() == boolValue); opt.setInplaceUpdateSupport(boolValue);
} assert (opt.inplaceUpdateSupport() == boolValue);
}
{ // InplaceUpdateNumLocks test
long longValue = rand.nextLong(); { // InplaceUpdateNumLocks test
opt.setInplaceUpdateNumLocks(longValue); long longValue = rand.nextLong();
assert(opt.inplaceUpdateNumLocks() == longValue); opt.setInplaceUpdateNumLocks(longValue);
} assert (opt.inplaceUpdateNumLocks() == longValue);
}
{ // MemtablePrefixBloomBits test
int intValue = rand.nextInt(); { // MemtablePrefixBloomBits test
opt.setMemtablePrefixBloomBits(intValue); int intValue = rand.nextInt();
assert(opt.memtablePrefixBloomBits() == intValue); opt.setMemtablePrefixBloomBits(intValue);
} assert (opt.memtablePrefixBloomBits() == intValue);
}
{ // MemtablePrefixBloomProbes test
int intValue = rand.nextInt(); { // MemtablePrefixBloomProbes test
opt.setMemtablePrefixBloomProbes(intValue); int intValue = rand.nextInt();
assert(opt.memtablePrefixBloomProbes() == intValue); opt.setMemtablePrefixBloomProbes(intValue);
} assert (opt.memtablePrefixBloomProbes() == intValue);
}
{ // BloomLocality test
int intValue = rand.nextInt(); { // BloomLocality test
opt.setBloomLocality(intValue); int intValue = rand.nextInt();
assert(opt.bloomLocality() == intValue); opt.setBloomLocality(intValue);
} assert (opt.bloomLocality() == intValue);
}
{ // MaxSuccessiveMerges test
long longValue = rand.nextLong(); { // MaxSuccessiveMerges test
opt.setMaxSuccessiveMerges(longValue); long longValue = rand.nextLong();
assert(opt.maxSuccessiveMerges() == longValue); opt.setMaxSuccessiveMerges(longValue);
assert (opt.maxSuccessiveMerges() == longValue);
}
{ // MinPartialMergeOperands test
int intValue = rand.nextInt();
opt.setMinPartialMergeOperands(intValue);
assert (opt.minPartialMergeOperands() == intValue);
}
} finally {
if (opt != null) {
opt.dispose();
}
} }
}
{ // MinPartialMergeOperands test @Test
int intValue = rand.nextInt(); public void rocksEnv() {
opt.setMinPartialMergeOperands(intValue); Options options = null;
assert(opt.minPartialMergeOperands() == intValue); try {
options = new Options();
RocksEnv rocksEnv = RocksEnv.getDefault();
options.setEnv(rocksEnv);
assertThat(options.getEnv()).isSameAs(rocksEnv);
} finally {
if (options != null) {
options.dispose();
}
} }
opt.dispose();
} }
@Test @Test
public void linkageOfPrepMethods() { public void linkageOfPrepMethods() {
Options options = new Options(); Options options = null;
options.optimizeUniversalStyleCompaction(); try {
options.optimizeUniversalStyleCompaction(4000); options = new Options();
options.optimizeLevelStyleCompaction(); options.optimizeUniversalStyleCompaction();
options.optimizeLevelStyleCompaction(3000); options.optimizeUniversalStyleCompaction(4000);
options.optimizeForPointLookup(10); options.optimizeLevelStyleCompaction();
options.prepareForBulkLoad(); options.optimizeLevelStyleCompaction(3000);
options.optimizeForPointLookup(10);
options.prepareForBulkLoad();
} finally {
if (options != null) {
options.dispose();
}
}
} }
@Test @Test
public void compressionTypes() { public void compressionTypes() {
Options options = new Options(); Options options = null;
for(CompressionType compressionType : try {
CompressionType.values()) { options = new Options();
options.setCompressionType(compressionType); for (CompressionType compressionType :
assertThat(options.compressionType()). CompressionType.values()) {
isEqualTo(compressionType); options.setCompressionType(compressionType);
assertThat(options.compressionType()).
isEqualTo(compressionType);
}
} finally {
if (options != null) {
options.dispose();
}
} }
options.dispose();
} }
@Test @Test
public void compactionStyles() { public void compactionStyles() {
Options options = new Options(); Options options = null;
for (CompactionStyle compactionStyle : try {
CompactionStyle.values()) { options = new Options();
options.setCompactionStyle(compactionStyle); for (CompactionStyle compactionStyle :
assertThat(options.compactionStyle()). CompactionStyle.values()) {
isEqualTo(compactionStyle); options.setCompactionStyle(compactionStyle);
assertThat(options.compactionStyle()).
isEqualTo(compactionStyle);
}
} finally {
if (options != null) {
options.dispose();
}
} }
options.dispose();
} }
@Test @Test
public void rateLimiterConfig() { public void rateLimiterConfig() {
Options options = new Options(); Options options = null;
RateLimiterConfig rateLimiterConfig = Options anotherOptions = null;
new GenericRateLimiterConfig(1000, 0, 1); RateLimiterConfig rateLimiterConfig;
options.setRateLimiterConfig(rateLimiterConfig); try {
options.dispose(); options = new Options();
// Test with parameter initialization rateLimiterConfig = new GenericRateLimiterConfig(1000, 0, 1);
Options anotherOptions = new Options(); options.setRateLimiterConfig(rateLimiterConfig);
anotherOptions.setRateLimiterConfig( // Test with parameter initialization
new GenericRateLimiterConfig(1000)); anotherOptions = new Options();
anotherOptions.dispose(); anotherOptions.setRateLimiterConfig(
new GenericRateLimiterConfig(1000));
} finally {
if (options != null) {
options.dispose();
}
if (anotherOptions != null) {
anotherOptions.dispose();
}
}
} }
@Test @Test
public void shouldSetTestPrefixExtractor() { public void shouldSetTestPrefixExtractor() {
Options options = new Options(); Options options = null;
options.useFixedLengthPrefixExtractor(100); try {
options.useFixedLengthPrefixExtractor(10); options = new Options();
options.dispose(); options.useFixedLengthPrefixExtractor(100);
options.useFixedLengthPrefixExtractor(10);
} finally {
if (options != null) {
options.dispose();
}
}
} }
@Test @Test
public void shouldTestMemTableFactoryName() public void shouldTestMemTableFactoryName()
throws RocksDBException { throws RocksDBException {
Options options = new Options(); Options options = null;
options.setMemTableConfig(new VectorMemTableConfig()); try {
assertThat(options.memTableFactoryName()). options = new Options();
isEqualTo("VectorRepFactory"); options.setMemTableConfig(new VectorMemTableConfig());
options.setMemTableConfig( assertThat(options.memTableFactoryName()).
new HashLinkedListMemTableConfig()); isEqualTo("VectorRepFactory");
assertThat(options.memTableFactoryName()). options.setMemTableConfig(
isEqualTo("HashLinkedListRepFactory"); new HashLinkedListMemTableConfig());
options.dispose(); assertThat(options.memTableFactoryName()).
isEqualTo("HashLinkedListRepFactory");
} finally {
if (options != null) {
options.dispose();
}
}
} }
@Test @Test
public void statistics() { public void statistics() {
Options options = new Options(); Options options = null;
Statistics statistics = options.createStatistics(). Options anotherOptions = null;
statisticsPtr(); try {
assertThat(statistics).isNotNull(); options = new Options();
Statistics statistics = options.createStatistics().
Options anotherOptions = new Options(); statisticsPtr();
statistics = anotherOptions.statisticsPtr(); assertThat(statistics).isNotNull();
assertThat(statistics).isNotNull(); anotherOptions = new Options();
statistics = anotherOptions.statisticsPtr();
assertThat(statistics).isNotNull();
} finally {
if (options != null) {
options.dispose();
}
if (anotherOptions != null) {
anotherOptions.dispose();
}
}
} }
} }

@ -26,203 +26,308 @@ public class ReadOnlyTest {
@Test @Test
public void readOnlyOpen() throws RocksDBException { public void readOnlyOpen() throws RocksDBException {
RocksDB db, db2, db3; RocksDB db = null;
RocksDB db2 = null;
RocksDB db3 = null;
Options options = null;
List<ColumnFamilyHandle> columnFamilyHandleList = List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>(); new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>(); new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList2 = List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList2 =
new ArrayList<>(); new ArrayList<>();
try {
Options options = new Options(); options = new Options();
options.setCreateIfMissing(true); options.setCreateIfMissing(true);
db = RocksDB.open(options, db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath()); dbFolder.getRoot().getAbsolutePath());
db.put("key".getBytes(), "value".getBytes()); db.put("key".getBytes(), "value".getBytes());
db2 = RocksDB.openReadOnly( db2 = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath()); dbFolder.getRoot().getAbsolutePath());
assertThat("value"). assertThat("value").
isEqualTo(new String(db2.get("key".getBytes()))); isEqualTo(new String(db2.get("key".getBytes())));
db.close(); db.close();
db2.close(); db2.close();
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add( cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions())); new ColumnFamilyOptions()));
db = RocksDB.open( db = RocksDB.open(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList); dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList);
columnFamilyHandleList.add(db.createColumnFamily( columnFamilyHandleList.add(db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions()))); new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions())));
columnFamilyHandleList.add(db.createColumnFamily( columnFamilyHandleList.add(db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions()))); new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions())));
db.put(columnFamilyHandleList.get(2), "key2".getBytes(), db.put(columnFamilyHandleList.get(2), "key2".getBytes(),
"value2".getBytes()); "value2".getBytes());
db2 = RocksDB.openReadOnly( db2 = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList); readOnlyColumnFamilyHandleList);
assertThat(db2.get("key2".getBytes())).isNull(); assertThat(db2.get("key2".getBytes())).isNull();
assertThat(db2.get(readOnlyColumnFamilyHandleList.get(0), "key2".getBytes())). assertThat(db2.get(readOnlyColumnFamilyHandleList.get(0), "key2".getBytes())).
isNull(); isNull();
cfDescriptors.clear();
cfDescriptors.clear(); cfDescriptors.add(
cfDescriptors.add( new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions()));
new ColumnFamilyOptions())); cfDescriptors.add(
cfDescriptors.add( new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions()));
new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions())); db3 = RocksDB.openReadOnly(
db3 = RocksDB.openReadOnly( dbFolder.getRoot().getAbsolutePath(), cfDescriptors, readOnlyColumnFamilyHandleList2);
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, assertThat(new String(db3.get(readOnlyColumnFamilyHandleList2.get(1),
readOnlyColumnFamilyHandleList2); "key2".getBytes()))).isEqualTo("value2");
assertThat(new String(db3.get(readOnlyColumnFamilyHandleList2.get(1), } finally {
"key2".getBytes()))).isEqualTo("value2"); if (db != null) {
db.close(); db.close();
db2.close(); }
db3.close(); if (db2 != null) {
options.dispose(); db2.close();
}
if (db3 != null) {
db3.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test(expected = RocksDBException.class) @Test(expected = RocksDBException.class)
public void failToWriteInReadOnly() throws RocksDBException { public void failToWriteInReadOnly() throws RocksDBException {
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); RocksDB db = null;
cfDescriptors.add( RocksDB rDb = null;
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, Options options = null;
new ColumnFamilyOptions())); try {
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
new ArrayList<>(); cfDescriptors.add(
Options options = new Options(); new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
options.setCreateIfMissing(true); new ColumnFamilyOptions()));
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
RocksDB db = RocksDB.open(options, new ArrayList<>();
dbFolder.getRoot().getAbsolutePath()); options = new Options();
db.close(); options.setCreateIfMissing(true);
RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, db = RocksDB.open(options,
readOnlyColumnFamilyHandleList); dbFolder.getRoot().getAbsolutePath());
// test that put fails in readonly mode db.close();
rDb.put("key".getBytes(), "value".getBytes()); rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
// test that put fails in readonly mode
rDb.put("key".getBytes(), "value".getBytes());
} finally {
if (db != null) {
db.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test(expected = RocksDBException.class) @Test(expected = RocksDBException.class)
public void failToCFWriteInReadOnly() throws RocksDBException { public void failToCFWriteInReadOnly() throws RocksDBException {
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); RocksDB db = null;
cfDescriptors.add( RocksDB rDb = null;
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, Options options = null;
new ColumnFamilyOptions())); try {
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
new ArrayList<>(); cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
Options options = new Options(); new ColumnFamilyOptions()));
options.setCreateIfMissing(true); List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
RocksDB db = RocksDB.open(options, options = new Options();
dbFolder.getRoot().getAbsolutePath()); options.setCreateIfMissing(true);
db.close();
RocksDB rDb = RocksDB.openReadOnly( db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, dbFolder.getRoot().getAbsolutePath());
readOnlyColumnFamilyHandleList); db.close();
rDb = RocksDB.openReadOnly(
rDb.put(readOnlyColumnFamilyHandleList.get(0), dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
"key".getBytes(), "value".getBytes()); readOnlyColumnFamilyHandleList);
rDb.put(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes(), "value".getBytes());
} finally {
if (db != null) {
db.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test(expected = RocksDBException.class) @Test(expected = RocksDBException.class)
public void failToRemoveInReadOnly() throws RocksDBException { public void failToRemoveInReadOnly() throws RocksDBException {
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); RocksDB db = null;
cfDescriptors.add( RocksDB rDb = null;
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, Options options = null;
new ColumnFamilyOptions())); try {
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
new ArrayList<>(); cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
Options options = new Options(); new ColumnFamilyOptions()));
options.setCreateIfMissing(true); List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath()); options = new Options();
db.close(); options.setCreateIfMissing(true);
RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, db = RocksDB.open(options,
readOnlyColumnFamilyHandleList); dbFolder.getRoot().getAbsolutePath());
db.close();
rDb.remove("key".getBytes()); rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
rDb.remove("key".getBytes());
} finally {
if (db != null) {
db.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test(expected = RocksDBException.class) @Test(expected = RocksDBException.class)
public void failToCFRemoveInReadOnly() throws RocksDBException { public void failToCFRemoveInReadOnly() throws RocksDBException {
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); RocksDB db = null;
cfDescriptors.add( RocksDB rDb = null;
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, Options options = null;
new ColumnFamilyOptions())); try {
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
new ArrayList<>(); cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
Options options = new Options(); new ColumnFamilyOptions()));
options.setCreateIfMissing(true); List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath()); options = new Options();
db.close(); options.setCreateIfMissing(true);
RocksDB rDb = RocksDB.openReadOnly( db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, dbFolder.getRoot().getAbsolutePath());
readOnlyColumnFamilyHandleList); db.close();
rDb.remove(readOnlyColumnFamilyHandleList.get(0), rDb = RocksDB.openReadOnly(
"key".getBytes()); dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
rDb.remove(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes());
} finally {
if (db != null) {
db.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test(expected = RocksDBException.class) @Test(expected = RocksDBException.class)
public void failToWriteBatchReadOnly() throws RocksDBException { public void failToWriteBatchReadOnly() throws RocksDBException {
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); RocksDB db = null;
cfDescriptors.add( RocksDB rDb = null;
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, Options options = null;
new ColumnFamilyOptions())); try {
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
new ArrayList<>(); cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
Options options = new Options(); new ColumnFamilyOptions()));
options.setCreateIfMissing(true); List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath()); options = new Options();
db.close(); options.setCreateIfMissing(true);
RocksDB rDb = RocksDB.openReadOnly( db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, dbFolder.getRoot().getAbsolutePath());
readOnlyColumnFamilyHandleList); db.close();
WriteBatch wb = new WriteBatch(); rDb = RocksDB.openReadOnly(
wb.put("key".getBytes(), "value".getBytes()); dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
rDb.write(new WriteOptions(), wb); readOnlyColumnFamilyHandleList);
WriteBatch wb = new WriteBatch();
wb.put("key".getBytes(), "value".getBytes());
rDb.write(new WriteOptions(), wb);
} finally {
if (db != null) {
db.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
} }
@Test(expected = RocksDBException.class) @Test(expected = RocksDBException.class)
public void failToCFWriteBatchReadOnly() throws RocksDBException { public void failToCFWriteBatchReadOnly() throws RocksDBException {
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); RocksDB db = null;
cfDescriptors.add( RocksDB rDb = null;
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, Options options = null;
new ColumnFamilyOptions())); WriteBatch wb = null;
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = try {
new ArrayList<>(); List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add(
Options options = new Options(); new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
options.setCreateIfMissing(true); new ColumnFamilyOptions()));
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
RocksDB db = RocksDB.open(options, new ArrayList<>();
dbFolder.getRoot().getAbsolutePath());
db.close(); options = new Options();
options.setCreateIfMissing(true);
RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, db = RocksDB.open(options,
readOnlyColumnFamilyHandleList); dbFolder.getRoot().getAbsolutePath());
db.close();
WriteBatch wb = new WriteBatch();
wb.put(readOnlyColumnFamilyHandleList.get(0), rDb = RocksDB.openReadOnly(
"key".getBytes(), "value".getBytes()); dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
rDb.write(new WriteOptions(), wb); readOnlyColumnFamilyHandleList);
wb = new WriteBatch();
wb.put(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes(), "value".getBytes());
rDb.write(new WriteOptions(), wb);
} finally {
if (db != null) {
db.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
if (wb != null) {
wb.dispose();
}
}
} }
} }

@ -25,32 +25,66 @@ public class ReadOptionsTest {
public ExpectedException exception = ExpectedException.none(); public ExpectedException exception = ExpectedException.none();
@Test @Test
public void readOptions() { public void verifyChecksum(){
ReadOptions opt = new ReadOptions(); ReadOptions opt = null;
Random rand = new Random(); try {
{ // VerifyChecksums test opt = new ReadOptions();
Random rand = new Random();
boolean boolValue = rand.nextBoolean(); boolean boolValue = rand.nextBoolean();
opt.setVerifyChecksums(boolValue); opt.setVerifyChecksums(boolValue);
assertThat(opt.verifyChecksums()).isEqualTo(boolValue); assertThat(opt.verifyChecksums()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
} }
}
{ // FillCache test @Test
public void fillCache(){
ReadOptions opt = null;
try {
opt = new ReadOptions();
Random rand = new Random();
boolean boolValue = rand.nextBoolean(); boolean boolValue = rand.nextBoolean();
opt.setFillCache(boolValue); opt.setFillCache(boolValue);
assertThat(opt.fillCache()).isEqualTo(boolValue); assertThat(opt.fillCache()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
} }
}
{ // Tailing test @Test
public void tailing(){
ReadOptions opt = null;
try {
opt = new ReadOptions();
Random rand = new Random();
boolean boolValue = rand.nextBoolean(); boolean boolValue = rand.nextBoolean();
opt.setTailing(boolValue); opt.setTailing(boolValue);
assertThat(opt.tailing()).isEqualTo(boolValue); assertThat(opt.tailing()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
} }
}
{ // Snapshot null test @Test
public void snapshot(){
ReadOptions opt = null;
try {
opt = new ReadOptions();
Random rand = new Random();
opt.setSnapshot(null); opt.setSnapshot(null);
assertThat(opt.snapshot()).isNull(); assertThat(opt.snapshot()).isNull();
} finally {
if (opt != null) {
opt.dispose();
}
} }
opt.dispose();
} }
@Test @Test

@ -0,0 +1,39 @@
// Copyright (c) 2014, 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.
package org.rocksdb.test;
import org.junit.ClassRule;
import org.junit.Test;
import org.rocksdb.RocksEnv;
import static org.assertj.core.api.Assertions.assertThat;
public class RocksEnvTest {
@ClassRule
public static final RocksMemoryResource rocksMemoryResource =
new RocksMemoryResource();
@Test
public void rocksEnv(){
RocksEnv rocksEnv = RocksEnv.getDefault();
rocksEnv.setBackgroundThreads(5);
// default rocksenv will always return zero for flush pool
// no matter what was set via setBackgroundThreads
assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.FLUSH_POOL)).
isEqualTo(0);
rocksEnv.setBackgroundThreads(5, RocksEnv.FLUSH_POOL);
// default rocksenv will always return zero for flush pool
// no matter what was set via setBackgroundThreads
assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.FLUSH_POOL)).
isEqualTo(0);
rocksEnv.setBackgroundThreads(5, RocksEnv.COMPACTION_POOL);
// default rocksenv will always return zero for compaction pool
// no matter what was set via setBackgroundThreads
assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.COMPACTION_POOL)).
isEqualTo(0);
}
}

@ -26,61 +26,83 @@ public class RocksIteratorTest {
@Test @Test
public void rocksIterator() throws RocksDBException { public void rocksIterator() throws RocksDBException {
RocksDB db; RocksDB db = null;
Options options = new Options(); Options options = null;
options.setCreateIfMissing(true) RocksIterator iterator = null;
.setCreateMissingColumnFamilies(true); try {
db = RocksDB.open(options, options = new Options();
dbFolder.getRoot().getAbsolutePath()); options.setCreateIfMissing(true)
db.put("key1".getBytes(), "value1".getBytes()); .setCreateMissingColumnFamilies(true);
db.put("key2".getBytes(), "value2".getBytes()); db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.put("key1".getBytes(), "value1".getBytes());
db.put("key2".getBytes(), "value2".getBytes());
RocksIterator iterator = db.newIterator(); iterator = db.newIterator();
iterator.seekToFirst(); iterator.seekToFirst();
assertThat(iterator.isValid()).isTrue(); assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("key1".getBytes()); assertThat(iterator.key()).isEqualTo("key1".getBytes());
assertThat(iterator.value()).isEqualTo("value1".getBytes()); assertThat(iterator.value()).isEqualTo("value1".getBytes());
iterator.next(); iterator.next();
assertThat(iterator.isValid()).isTrue(); assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("key2".getBytes()); assertThat(iterator.key()).isEqualTo("key2".getBytes());
assertThat(iterator.value()).isEqualTo("value2".getBytes()); assertThat(iterator.value()).isEqualTo("value2".getBytes());
iterator.next(); iterator.next();
assertThat(iterator.isValid()).isFalse(); assertThat(iterator.isValid()).isFalse();
iterator.seekToLast(); iterator.seekToLast();
iterator.prev(); iterator.prev();
assertThat(iterator.isValid()).isTrue(); assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("key1".getBytes()); assertThat(iterator.key()).isEqualTo("key1".getBytes());
assertThat(iterator.value()).isEqualTo("value1".getBytes()); assertThat(iterator.value()).isEqualTo("value1".getBytes());
iterator.seekToFirst(); iterator.seekToFirst();
iterator.seekToLast(); iterator.seekToLast();
assertThat(iterator.isValid()).isTrue(); assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("key2".getBytes()); assertThat(iterator.key()).isEqualTo("key2".getBytes());
assertThat(iterator.value()).isEqualTo("value2".getBytes()); assertThat(iterator.value()).isEqualTo("value2".getBytes());
iterator.status(); iterator.status();
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (iterator != null) {
iterator.dispose();
}
}
} }
@Test @Test
public void rocksIteratorGc() public void rocksIteratorGc()
throws RocksDBException { throws RocksDBException {
RocksDB db; RocksDB db = null;
Options options = new Options(); Options options = null;
options.setCreateIfMissing(true) try {
.setCreateMissingColumnFamilies(true); options = new Options();
db = RocksDB.open(options, options.setCreateIfMissing(true)
dbFolder.getRoot().getAbsolutePath()); .setCreateMissingColumnFamilies(true);
db.put("key".getBytes(), "value".getBytes()); db = RocksDB.open(options,
RocksIterator iter = db.newIterator(); dbFolder.getRoot().getAbsolutePath());
RocksIterator iter2 = db.newIterator(); db.put("key".getBytes(), "value".getBytes());
RocksIterator iter3 = db.newIterator(); db.newIterator();
iter = null; db.newIterator();
db.close(); RocksIterator iter3 = db.newIterator();
db = null; db.close();
iter2 = null; db = null;
System.gc(); System.gc();
System.runFinalization(); System.runFinalization();
iter3.dispose(); iter3.dispose();
System.gc(); System.gc();
System.runFinalization(); System.runFinalization();
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
} }
} }

@ -27,58 +27,71 @@ public class SnapshotTest {
@Test @Test
public void snapshots() throws RocksDBException { public void snapshots() throws RocksDBException {
RocksDB db; RocksDB db = null;
Options options = new Options(); Options options = null;
options.setCreateIfMissing(true); ReadOptions readOptions = null;
try {
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); options = new Options();
db.put("key".getBytes(), "value".getBytes()); options.setCreateIfMissing(true);
// Get new Snapshot of database
Snapshot snapshot = db.getSnapshot(); db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
ReadOptions readOptions = new ReadOptions(); db.put("key".getBytes(), "value".getBytes());
// set snapshot in ReadOptions // Get new Snapshot of database
readOptions.setSnapshot(snapshot); Snapshot snapshot = db.getSnapshot();
// retrieve key value pair readOptions = new ReadOptions();
assertThat(new String(db.get("key".getBytes()))). // set snapshot in ReadOptions
isEqualTo("value"); readOptions.setSnapshot(snapshot);
// retrieve key value pair created before // retrieve key value pair
// the snapshot was made assertThat(new String(db.get("key".getBytes()))).
assertThat(new String(db.get(readOptions, isEqualTo("value");
"key".getBytes()))).isEqualTo("value"); // retrieve key value pair created before
// add new key/value pair // the snapshot was made
db.put("newkey".getBytes(), "newvalue".getBytes()); assertThat(new String(db.get(readOptions,
// using no snapshot the latest db entries "key".getBytes()))).isEqualTo("value");
// will be taken into account // add new key/value pair
assertThat(new String(db.get("newkey".getBytes()))). db.put("newkey".getBytes(), "newvalue".getBytes());
isEqualTo("newvalue"); // using no snapshot the latest db entries
// snapshopot was created before newkey // will be taken into account
assertThat(db.get(readOptions, "newkey".getBytes())). assertThat(new String(db.get("newkey".getBytes()))).
isNull(); isEqualTo("newvalue");
// Retrieve snapshot from read options // snapshopot was created before newkey
Snapshot sameSnapshot = readOptions.snapshot(); assertThat(db.get(readOptions, "newkey".getBytes())).
readOptions.setSnapshot(sameSnapshot); isNull();
// results must be the same with new Snapshot // Retrieve snapshot from read options
// instance using the same native pointer Snapshot sameSnapshot = readOptions.snapshot();
assertThat(new String(db.get(readOptions, readOptions.setSnapshot(sameSnapshot);
"key".getBytes()))).isEqualTo("value"); // results must be the same with new Snapshot
// update key value pair to newvalue // instance using the same native pointer
db.put("key".getBytes(), "newvalue".getBytes()); assertThat(new String(db.get(readOptions,
// read with previously created snapshot will "key".getBytes()))).isEqualTo("value");
// read previous version of key value pair // update key value pair to newvalue
assertThat(new String(db.get(readOptions, db.put("key".getBytes(), "newvalue".getBytes());
"key".getBytes()))).isEqualTo("value"); // read with previously created snapshot will
// read for newkey using the snapshot must be // read previous version of key value pair
// null assertThat(new String(db.get(readOptions,
assertThat(db.get(readOptions, "newkey".getBytes())). "key".getBytes()))).isEqualTo("value");
isNull(); // read for newkey using the snapshot must be
// setting null to snapshot in ReadOptions leads // null
// to no Snapshot being used. assertThat(db.get(readOptions, "newkey".getBytes())).
readOptions.setSnapshot(null); isNull();
assertThat(new String(db.get(readOptions, // setting null to snapshot in ReadOptions leads
"newkey".getBytes()))).isEqualTo("newvalue"); // to no Snapshot being used.
// release Snapshot readOptions.setSnapshot(null);
db.releaseSnapshot(snapshot); assertThat(new String(db.get(readOptions,
// Close database "newkey".getBytes()))).isEqualTo("newvalue");
db.close(); // release Snapshot
db.releaseSnapshot(snapshot);
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (readOptions != null) {
readOptions.dispose();
}
}
} }
} }

@ -27,27 +27,35 @@ public class StatisticsCollectorTest {
@Test @Test
public void statisticsCollector() public void statisticsCollector()
throws InterruptedException, RocksDBException { throws InterruptedException, RocksDBException {
Options opt = new Options().createStatistics().setCreateIfMissing(true); Options opt = null;
Statistics stats = opt.statisticsPtr(); RocksDB db = null;
try {
RocksDB db = RocksDB.open(opt, opt = new Options().createStatistics().setCreateIfMissing(true);
dbFolder.getRoot().getAbsolutePath()); Statistics stats = opt.statisticsPtr();
StatsCallbackMock callback = new StatsCallbackMock(); db = RocksDB.open(opt,
StatsCollectorInput statsInput = new StatsCollectorInput(stats, callback); dbFolder.getRoot().getAbsolutePath());
StatisticsCollector statsCollector = new StatisticsCollector( StatsCallbackMock callback = new StatsCallbackMock();
Collections.singletonList(statsInput), 100); StatsCollectorInput statsInput = new StatsCollectorInput(stats, callback);
statsCollector.start();
StatisticsCollector statsCollector = new StatisticsCollector(
Thread.sleep(1000); Collections.singletonList(statsInput), 100);
statsCollector.start();
assertThat(callback.tickerCallbackCount).isGreaterThan(0);
assertThat(callback.histCallbackCount).isGreaterThan(0); Thread.sleep(1000);
statsCollector.shutDown(1000); assertThat(callback.tickerCallbackCount).isGreaterThan(0);
assertThat(callback.histCallbackCount).isGreaterThan(0);
db.close();
opt.dispose(); statsCollector.shutDown(1000);
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
} }
} }

@ -124,7 +124,7 @@ void Java_org_rocksdb_BackupableDBOptions_newBackupableDBOptions(
* Signature: (J)Ljava/lang/String; * Signature: (J)Ljava/lang/String;
*/ */
jstring Java_org_rocksdb_BackupableDBOptions_backupDir( jstring Java_org_rocksdb_BackupableDBOptions_backupDir(
JNIEnv* env, jobject jopt, jlong jhandle, jstring jpath) { JNIEnv* env, jobject jopt, jlong jhandle) {
auto bopt = reinterpret_cast<rocksdb::BackupableDBOptions*>(jhandle); auto bopt = reinterpret_cast<rocksdb::BackupableDBOptions*>(jhandle);
return env->NewStringUTF(bopt->backup_dir.c_str()); return env->NewStringUTF(bopt->backup_dir.c_str());
} }

@ -274,6 +274,17 @@ void Java_org_rocksdb_Options_setParanoidChecks(
static_cast<bool>(paranoid_checks); static_cast<bool>(paranoid_checks);
} }
/*
* Class: org_rocksdb_Options
* Method: setEnv
* Signature: (JJ)V
*/
void Java_org_rocksdb_Options_setEnv(
JNIEnv* env, jobject jobj, jlong jhandle, jlong jenv) {
reinterpret_cast<rocksdb::Options*>(jhandle)->env =
reinterpret_cast<rocksdb::Env*>(jenv);
}
/* /*
* Class: org_rocksdb_Options * Class: org_rocksdb_Options
* Method: setMaxTotalWalSize * Method: setMaxTotalWalSize

Loading…
Cancel
Save