Refactored tests to use try-with-resources

main
Adam Retter 9 years ago
parent f8e02c7825
commit 0f2d2fcff6
  1. 56
      java/samples/src/main/java/RocksDBColumnFamilySample.java
  2. 58
      java/samples/src/main/java/RocksDBSample.java
  3. 100
      java/src/test/java/org/rocksdb/AbstractComparatorTest.java
  4. 135
      java/src/test/java/org/rocksdb/BackupEngineTest.java
  5. 170
      java/src/test/java/org/rocksdb/BackupableDBOptionsTest.java
  6. 219
      java/src/test/java/org/rocksdb/BackupableDBTest.java
  7. 24
      java/src/test/java/org/rocksdb/BlockBasedTableConfigTest.java
  8. 63
      java/src/test/java/org/rocksdb/CheckPointTest.java
  9. 445
      java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java
  10. 617
      java/src/test/java/org/rocksdb/ColumnFamilyTest.java
  11. 7
      java/src/test/java/org/rocksdb/ComparatorOptionsTest.java
  12. 73
      java/src/test/java/org/rocksdb/ComparatorTest.java
  13. 5
      java/src/test/java/org/rocksdb/CompressionOptionsTest.java
  14. 388
      java/src/test/java/org/rocksdb/DBOptionsTest.java
  15. 64
      java/src/test/java/org/rocksdb/DirectSliceTest.java
  16. 32
      java/src/test/java/org/rocksdb/FilterTest.java
  17. 46
      java/src/test/java/org/rocksdb/FlushTest.java
  18. 50
      java/src/test/java/org/rocksdb/InfoLogLevelTest.java
  19. 58
      java/src/test/java/org/rocksdb/KeyMayExistTest.java
  20. 126
      java/src/test/java/org/rocksdb/LoggerTest.java
  21. 34
      java/src/test/java/org/rocksdb/MemTableTest.java
  22. 283
      java/src/test/java/org/rocksdb/MergeTest.java
  23. 29
      java/src/test/java/org/rocksdb/MixedOptionsTest.java
  24. 2
      java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java
  25. 737
      java/src/test/java/org/rocksdb/OptionsTest.java
  26. 10
      java/src/test/java/org/rocksdb/PlainTableConfigTest.java
  27. 2
      java/src/test/java/org/rocksdb/PlatformRandomHelper.java
  28. 396
      java/src/test/java/org/rocksdb/ReadOnlyTest.java
  29. 88
      java/src/test/java/org/rocksdb/ReadOptionsTest.java
  30. 425
      java/src/test/java/org/rocksdb/RocksDBTest.java
  31. 3
      java/src/test/java/org/rocksdb/RocksEnvTest.java
  32. 24
      java/src/test/java/org/rocksdb/RocksIteratorTest.java
  33. 96
      java/src/test/java/org/rocksdb/RocksMemEnvTest.java
  34. 4
      java/src/test/java/org/rocksdb/RocksMemoryResource.java
  35. 68
      java/src/test/java/org/rocksdb/SliceTest.java
  36. 112
      java/src/test/java/org/rocksdb/SnapshotTest.java
  37. 32
      java/src/test/java/org/rocksdb/StatisticsCollectorTest.java
  38. 134
      java/src/test/java/org/rocksdb/TransactionLogIteratorTest.java
  39. 120
      java/src/test/java/org/rocksdb/TtlDBTest.java
  40. 63
      java/src/test/java/org/rocksdb/WriteBatchHandlerTest.java
  41. 30
      java/src/test/java/org/rocksdb/WriteBatchTest.java
  42. 92
      java/src/test/java/org/rocksdb/WriteBatchWithIndexTest.java
  43. 3
      java/src/test/java/org/rocksdb/WriteOptionsTest.java

@ -22,48 +22,35 @@ public class RocksDBColumnFamilySample {
String db_path = args[0];
System.out.println("RocksDBColumnFamilySample");
RocksDB db = null;
Options options = null;
ColumnFamilyHandle columnFamilyHandle = null;
WriteBatch wb = null;
try {
options = new Options().setCreateIfMissing(true);
db = RocksDB.open(options, db_path);
try(final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options, db_path)) {
assert(db != null);
// create column family
columnFamilyHandle = db.createColumnFamily(
try(final ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf".getBytes(),
new ColumnFamilyOptions()));
new ColumnFamilyOptions()))) {
assert (columnFamilyHandle != null);
} finally {
if (columnFamilyHandle != null) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
db = null;
}
if (options != null) {
options.dispose();
}
}
// open DB with two column families
List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>();
final List<ColumnFamilyDescriptor> columnFamilyDescriptors =
new ArrayList<>();
// have to open default column family
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions()));
// open the new one, too
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
"new_cf".getBytes(), new ColumnFamilyOptions()));
List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
try {
db = RocksDB.open(new DBOptions(), db_path,
columnFamilyDescriptors, columnFamilyHandles);
final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
try(final DBOptions options = new DBOptions();
final RocksDB db = RocksDB.open(options, db_path,
columnFamilyDescriptors, columnFamilyHandles)) {
assert(db != null);
try {
// put and get from non-default column family
db.put(columnFamilyHandles.get(0), new WriteOptions(),
"key".getBytes(), "value".getBytes());
@ -71,24 +58,21 @@ public class RocksDBColumnFamilySample {
"key".getBytes()));
// atomic write
wb = new WriteBatch();
wb.put(columnFamilyHandles.get(0), "key2".getBytes(), "value2".getBytes());
wb.put(columnFamilyHandles.get(1), "key3".getBytes(), "value3".getBytes());
try (final WriteBatch wb = new WriteBatch()) {
wb.put(columnFamilyHandles.get(0), "key2".getBytes(),
"value2".getBytes());
wb.put(columnFamilyHandles.get(1), "key3".getBytes(),
"value3".getBytes());
wb.remove(columnFamilyHandles.get(0), "key".getBytes());
db.write(new WriteOptions(), wb);
}
// drop column family
db.dropColumnFamily(columnFamilyHandles.get(1));
} finally {
for (ColumnFamilyHandle handle : columnFamilyHandles){
handle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle handle : columnFamilyHandles) {
handle.close();
}
if (wb != null) {
wb.dispose();
}
}
}

@ -8,8 +8,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import org.rocksdb.*;
import org.rocksdb.util.SizeUnit;
import java.io.IOException;
public class RocksDBSample {
@ -26,14 +28,15 @@ public class RocksDBSample {
String db_path_not_found = db_path + "_not_found";
System.out.println("RocksDBSample");
RocksDB db = null;
Options options = new Options();
try {
db = RocksDB.open(options, db_path_not_found);
try (final Options options = new Options();
final Filter bloomFilter = new BloomFilter(10);
final ReadOptions readOptions = new ReadOptions()
.setFillCache(false)) {
try (final RocksDB db = RocksDB.open(options, db_path_not_found)) {
assert (false);
} catch (RocksDBException e) {
System.out.format("caught the expceted exception -- %s\n", e);
assert(db == null);
System.out.format("caught the expected exception -- %s\n", e);
}
try {
@ -86,9 +89,7 @@ public class RocksDBSample {
10000, 10));
options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000));
Filter bloomFilter = new BloomFilter(10);
BlockBasedTableConfig table_options = new BlockBasedTableConfig();
final BlockBasedTableConfig table_options = new BlockBasedTableConfig();
table_options.setBlockCacheSize(64 * SizeUnit.KB)
.setFilter(bloomFilter)
.setCacheNumShardBits(6)
@ -111,8 +112,7 @@ public class RocksDBSample {
options.setTableFormatConfig(table_options);
assert (options.tableFactoryName().equals("BlockBasedTable"));
try {
db = RocksDB.open(options, db_path);
try (final RocksDB db = RocksDB.open(options, db_path)) {
db.put("hello".getBytes(), "world".getBytes());
byte[] value = db.get("hello".getBytes());
assert ("world".equals(new String(value)));
@ -120,17 +120,10 @@ public class RocksDBSample {
assert (str != null && !str.equals(""));
} catch (RocksDBException e) {
System.out.format("[ERROR] caught the unexpceted exception -- %s\n", e);
assert(db == null);
assert (false);
}
// be sure to release the c++ pointer
db.close();
ReadOptions readOptions = new ReadOptions();
readOptions.setFillCache(false);
try {
db = RocksDB.open(options, db_path);
try (final RocksDB db = RocksDB.open(options, db_path)) {
db.put("hello".getBytes(), "world".getBytes());
byte[] value = db.get("hello".getBytes());
System.out.format("Get('hello') = %s\n",
@ -152,15 +145,16 @@ public class RocksDBSample {
}
// write batch test
WriteOptions writeOpt = new WriteOptions();
try (final WriteOptions writeOpt = new WriteOptions()) {
for (int i = 10; i <= 19; ++i) {
WriteBatch batch = new WriteBatch();
try (final WriteBatch batch = new WriteBatch()) {
for (int j = 10; j <= 19; ++j) {
batch.put(String.format("%dx%d", i, j).getBytes(),
String.format("%d", i * j).getBytes());
}
db.write(writeOpt, batch);
batch.dispose();
}
}
}
for (int i = 10; i <= 19; ++i) {
for (int j = 10; j <= 19; ++j) {
@ -172,7 +166,6 @@ public class RocksDBSample {
}
System.out.println("");
}
writeOpt.dispose();
value = db.get("1x1".getBytes());
assert (value != null);
@ -216,7 +209,7 @@ public class RocksDBSample {
assert (len == RocksDB.NOT_FOUND);
// repeat the test with WriteOptions
WriteOptions writeOpts = new WriteOptions();
try (final WriteOptions writeOpts = new WriteOptions()) {
writeOpts.setSync(true);
writeOpts.setDisableWAL(true);
db.put(writeOpts, testKey, testValue);
@ -224,7 +217,7 @@ public class RocksDBSample {
assert (len == testValue.length);
assert (new String(testValue).equals(
new String(enoughArray, 0, len)));
writeOpts.dispose();
}
try {
for (TickerType statsType : TickerType.values()) {
@ -246,7 +239,7 @@ public class RocksDBSample {
assert (false); //Should never reach here.
}
RocksIterator iterator = db.newIterator();
try (final RocksIterator iterator = db.newIterator()) {
boolean seekToFirstPassed = false;
for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
@ -278,15 +271,15 @@ public class RocksDBSample {
System.out.println("iterator seek test passed.");
iterator.dispose();
}
System.out.println("iterator tests passed.");
iterator = db.newIterator();
List<byte[]> keys = new ArrayList<byte[]>();
final List<byte[]> keys = new ArrayList<>();
try (final RocksIterator iterator = db.newIterator()) {
for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) {
keys.add(iterator.key());
}
iterator.dispose();
}
Map<byte[], byte[]> values = db.multiGet(keys);
assert (values.size() == keys.size());
@ -302,11 +295,6 @@ public class RocksDBSample {
} catch (RocksDBException e) {
System.err.println(e);
}
if (db != null) {
db.close();
}
// be sure to dispose c++ pointers
options.dispose();
readOptions.dispose();
}
}

@ -8,6 +8,7 @@ package org.rocksdb;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@ -39,36 +40,33 @@ public abstract class AbstractComparatorTest {
*
* @throws java.io.IOException if IO error happens.
*/
public void testRoundtrip(final Path db_path) throws IOException, RocksDBException {
Options opt = null;
RocksDB db = null;
try {
opt = new Options();
opt.setCreateIfMissing(true);
opt.setComparator(getAscendingIntKeyComparator());
public void testRoundtrip(final Path db_path) throws IOException,
RocksDBException {
try (final AbstractComparator comparator = getAscendingIntKeyComparator();
final Options opt = new Options()
.setCreateIfMissing(true)
.setComparator(comparator)) {
// store 10,000 random integer keys
final int ITERATIONS = 10000;
db = RocksDB.open(opt, db_path.toString());
try (final RocksDB db = RocksDB.open(opt, db_path.toString())) {
final Random random = new Random();
for (int i = 0; i < ITERATIONS; i++) {
final byte key[] = intToByte(random.nextInt());
if (i > 0 && db.get(key) != null) { // does key already exist (avoid duplicates)
// does key already exist (avoid duplicates)
if (i > 0 && db.get(key) != null) {
i--; // generate a different key
} else {
db.put(key, "value".getBytes());
}
}
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());
final RocksIterator it = db.newIterator();
try (final RocksDB db = RocksDB.open(opt, db_path.toString());
final RocksIterator it = db.newIterator()) {
it.seekToFirst();
int lastKey = Integer.MIN_VALUE;
int count = 0;
@ -78,18 +76,7 @@ public abstract class AbstractComparatorTest {
lastKey = thisKey;
count++;
}
it.dispose();
db.close();
assertThat(count).isEqualTo(ITERATIONS);
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -109,25 +96,25 @@ public abstract class AbstractComparatorTest {
public void testRoundtripCf(final Path db_path) throws IOException,
RocksDBException {
DBOptions opt = null;
RocksDB db = null;
List<ColumnFamilyDescriptor> cfDescriptors =
new ArrayList<>();
cfDescriptors.add(new ColumnFamilyDescriptor(
RocksDB.DEFAULT_COLUMN_FAMILY));
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf".getBytes(),
new ColumnFamilyOptions().setComparator(
getAscendingIntKeyComparator())));
List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
try {
opt = new DBOptions().
try(final AbstractComparator comparator = getAscendingIntKeyComparator()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes(),
new ColumnFamilyOptions().setComparator(comparator))
);
final List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
try (final DBOptions opt = new DBOptions().
setCreateIfMissing(true).
setCreateMissingColumnFamilies(true);
setCreateMissingColumnFamilies(true)) {
// store 10,000 random integer keys
final int ITERATIONS = 10000;
db = RocksDB.open(opt, db_path.toString(), cfDescriptors, cfHandles);
try (final RocksDB db = RocksDB.open(opt, db_path.toString(),
cfDescriptors, cfHandles)) {
try {
assertThat(cfDescriptors.size()).isEqualTo(2);
assertThat(cfHandles.size()).isEqualTo(2);
@ -141,19 +128,24 @@ public abstract class AbstractComparatorTest {
db.put(cfHandles.get(1), key, "value".getBytes());
}
}
for (ColumnFamilyHandle handle : cfHandles) {
handle.dispose();
} finally {
for (final ColumnFamilyHandle handle : cfHandles) {
handle.close();
}
}
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);
try (final RocksDB db = RocksDB.open(opt, db_path.toString(),
cfDescriptors, cfHandles);
final RocksIterator it = db.newIterator(cfHandles.get(1))) {
try {
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;
@ -164,25 +156,15 @@ public abstract class AbstractComparatorTest {
count++;
}
it.dispose();
for (ColumnFamilyHandle handle : cfHandles) {
handle.dispose();
}
cfHandles.clear();
db.close();
assertThat(count).isEqualTo(ITERATIONS);
} finally {
for (ColumnFamilyHandle handle : cfHandles) {
handle.dispose();
for (final ColumnFamilyHandle handle : cfHandles) {
handle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
cfHandles.clear();
}
}
}
}

@ -28,57 +28,37 @@ public class BackupEngineTest {
@Test
public void backupDb() throws RocksDBException {
Options opt = null;
RocksDB db = null;
try {
opt = new Options().setCreateIfMissing(true);
// Open empty database.
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
try(final Options opt = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(db);
// Create two backups
BackupableDBOptions bopt = null;
try {
bopt = new BackupableDBOptions(
try(final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try(final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
be.createNewBackup(db, false);
be.createNewBackup(db, true);
verifyNumberOfValidBackups(be, 2);
}
} finally {
if(bopt != null) {
bopt.dispose();
}
}
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void deleteBackup() throws RocksDBException {
Options opt = null;
RocksDB db = null;
try {
opt = new Options().setCreateIfMissing(true);
// Open empty database.
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
try(final Options opt = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(db);
// Create two backups
BackupableDBOptions bopt = null;
try {
bopt = new BackupableDBOptions(
try(final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try(final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
be.createNewBackup(db, false);
be.createNewBackup(db, true);
final List<BackupInfo> backupInfo =
@ -92,38 +72,21 @@ public class BackupEngineTest {
assertThat(newBackupInfo.get(0).backupId()).
isEqualTo(backupInfo.get(1).backupId());
}
} finally {
if(bopt != null) {
bopt.dispose();
}
}
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void purgeOldBackups() throws RocksDBException {
Options opt = null;
RocksDB db = null;
try {
opt = new Options().setCreateIfMissing(true);
// Open empty database.
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
try(final Options opt = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(db);
// Create four backups
BackupableDBOptions bopt = null;
try {
bopt = new BackupableDBOptions(
try(final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try(final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
be.createNewBackup(db, false);
be.createNewBackup(db, true);
be.createNewBackup(db, true);
@ -138,38 +101,23 @@ public class BackupEngineTest {
assertThat(newBackupInfo.get(0).backupId()).
isEqualTo(backupInfo.get(3).backupId());
}
} finally {
if(bopt != null) {
bopt.dispose();
}
}
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void restoreLatestBackup()
throws RocksDBException {
Options opt = null;
public void restoreLatestBackup() throws RocksDBException {
try(final Options opt = new Options().setCreateIfMissing(true)) {
// Open empty database.
RocksDB db = null;
try {
opt = new Options().setCreateIfMissing(true);
// Open empty database.
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
// Fill database with some test values
prepareDatabase(db);
BackupableDBOptions bopt = null;
try {
bopt = new BackupableDBOptions(
try (final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
be.createNewBackup(db, true);
verifyNumberOfValidBackups(be, 1);
db.put("key1".getBytes(), "valueV2".getBytes());
@ -182,30 +130,26 @@ public class BackupEngineTest {
assertThat(new String(db.get("key2".getBytes()))).endsWith("V3");
db.close();
db = null;
verifyNumberOfValidBackups(be, 2);
// restore db from latest backup
try(final RestoreOptions ropts = new RestoreOptions(false)) {
be.restoreDbFromLatestBackup(dbFolder.getRoot().getAbsolutePath(),
dbFolder.getRoot().getAbsolutePath(),
new RestoreOptions(false));
dbFolder.getRoot().getAbsolutePath(), ropts);
}
// Open database again.
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath());
// Values must have suffix V2 because of restoring latest backup.
assertThat(new String(db.get("key1".getBytes()))).endsWith("V2");
assertThat(new String(db.get("key2".getBytes()))).endsWith("V2");
}
} finally {
if(bopt != null) {
bopt.dispose();
}
}
} finally {
if(db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -213,20 +157,17 @@ public class BackupEngineTest {
@Test
public void restoreFromBackup()
throws RocksDBException {
Options opt = null;
try(final Options opt = new Options().setCreateIfMissing(true)) {
RocksDB db = null;
try {
opt = new Options().setCreateIfMissing(true);
// Open empty database.
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
// Fill database with some test values
prepareDatabase(db);
BackupableDBOptions bopt = null;
try {
bopt = new BackupableDBOptions(
try (final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) {
be.createNewBackup(db, true);
verifyNumberOfValidBackups(be, 1);
db.put("key1".getBytes(), "valueV2".getBytes());
@ -240,9 +181,10 @@ public class BackupEngineTest {
//close the database
db.close();
db = null;
//restore the backup
List<BackupInfo> backupInfo = verifyNumberOfValidBackups(be, 2);
final List<BackupInfo> backupInfo = verifyNumberOfValidBackups(be, 2);
// restore db from first backup
be.restoreDbFromBackup(backupInfo.get(0).backupId(),
dbFolder.getRoot().getAbsolutePath(),
@ -255,17 +197,10 @@ public class BackupEngineTest {
assertThat(new String(db.get("key1".getBytes()))).endsWith("V1");
assertThat(new String(db.get("key2".getBytes()))).endsWith("V1");
}
} finally {
if(bopt != null) {
bopt.dispose();
}
}
} finally {
if(db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}

@ -16,7 +16,8 @@ import org.junit.rules.ExpectedException;
public class BackupableDBOptionsTest {
private final static String ARBITRARY_PATH = System.getProperty("java.io.tmpdir");
private final static String ARBITRARY_PATH =
System.getProperty("java.io.tmpdir");
@ClassRule
public static final RocksMemoryResource rocksMemoryResource =
@ -30,87 +31,61 @@ public class BackupableDBOptionsTest {
@Test
public void backupDir() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH)) {
assertThat(backupableDBOptions.backupDir()).
isEqualTo(ARBITRARY_PATH);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void shareTableFiles() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
boolean value = rand.nextBoolean();
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH)) {
final boolean value = rand.nextBoolean();
backupableDBOptions.setShareTableFiles(value);
assertThat(backupableDBOptions.shareTableFiles()).
isEqualTo(value);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void sync() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
boolean value = rand.nextBoolean();
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH)) {
final boolean value = rand.nextBoolean();
backupableDBOptions.setSync(value);
assertThat(backupableDBOptions.sync()).isEqualTo(value);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void destroyOldData() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
boolean value = rand.nextBoolean();
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH);) {
final boolean value = rand.nextBoolean();
backupableDBOptions.setDestroyOldData(value);
assertThat(backupableDBOptions.destroyOldData()).
isEqualTo(value);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void backupLogFiles() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
boolean value = rand.nextBoolean();
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH)) {
final boolean value = rand.nextBoolean();
backupableDBOptions.setBackupLogFiles(value);
assertThat(backupableDBOptions.backupLogFiles()).
isEqualTo(value);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void backupRateLimit() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
long value = Math.abs(rand.nextLong());
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH)) {
final long value = Math.abs(rand.nextLong());
backupableDBOptions.setBackupRateLimit(value);
assertThat(backupableDBOptions.backupRateLimit()).
isEqualTo(value);
@ -118,19 +93,14 @@ public class BackupableDBOptionsTest {
backupableDBOptions.setBackupRateLimit(-1);
assertThat(backupableDBOptions.backupRateLimit()).
isEqualTo(0);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void restoreRateLimit() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
long value = Math.abs(rand.nextLong());
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH)) {
final long value = Math.abs(rand.nextLong());
backupableDBOptions.setRestoreRateLimit(value);
assertThat(backupableDBOptions.restoreRateLimit()).
isEqualTo(value);
@ -138,145 +108,153 @@ public class BackupableDBOptionsTest {
backupableDBOptions.setRestoreRateLimit(-1);
assertThat(backupableDBOptions.restoreRateLimit()).
isEqualTo(0);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void shareFilesWithChecksum() {
BackupableDBOptions backupableDBOptions = null;
try {
backupableDBOptions = new BackupableDBOptions(ARBITRARY_PATH);
try (final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH)) {
boolean value = rand.nextBoolean();
backupableDBOptions.setShareFilesWithChecksum(value);
assertThat(backupableDBOptions.shareFilesWithChecksum()).
isEqualTo(value);
} finally {
if (backupableDBOptions != null) {
backupableDBOptions.dispose();
}
}
}
@Test
public void failBackupDirIsNull() {
exception.expect(IllegalArgumentException.class);
new BackupableDBOptions(null);
try (final BackupableDBOptions opts = new BackupableDBOptions(null)) {
//no-op
}
}
@Test
public void failBackupDirIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.backupDir();
}
}
@Test
public void failSetShareTableFilesIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.setShareTableFiles(true);
}
}
@Test
public void failShareTableFilesIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.shareTableFiles();
}
}
@Test
public void failSetSyncIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.setSync(true);
}
}
@Test
public void failSyncIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.sync();
}
}
@Test
public void failSetDestroyOldDataIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.setDestroyOldData(true);
}
}
@Test
public void failDestroyOldDataIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.destroyOldData();
}
}
@Test
public void failSetBackupLogFilesIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.setBackupLogFiles(true);
}
}
@Test
public void failBackupLogFilesIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.backupLogFiles();
}
}
@Test
public void failSetBackupRateLimitIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.setBackupRateLimit(1);
}
}
@Test
public void failBackupRateLimitIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.backupRateLimit();
}
}
@Test
public void failSetRestoreRateLimitIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.setRestoreRateLimit(1);
}
}
@Test
public void failRestoreRateLimitIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.restoreRateLimit();
}
}
@Test
public void failSetShareFilesWithChecksumIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.setShareFilesWithChecksum(true);
}
}
@Test
public void failShareFilesWithChecksumIfDisposed() {
BackupableDBOptions options = setupUninitializedBackupableDBOptions(
exception);
try (final BackupableDBOptions options =
setupUninitializedBackupableDBOptions(exception)) {
options.shareFilesWithChecksum();
}
}
private BackupableDBOptions setupUninitializedBackupableDBOptions(
ExpectedException exception) {
BackupableDBOptions backupableDBOptions =
final BackupableDBOptions backupableDBOptions =
new BackupableDBOptions(ARBITRARY_PATH);
backupableDBOptions.dispose();
backupableDBOptions.close();
exception.expect(AssertionError.class);
return backupableDBOptions;
}

@ -28,51 +28,34 @@ public class BackupableDBTest {
@Test
public void backupDb() throws RocksDBException {
Options opt = null;
BackupableDBOptions bopt = null;
BackupableDB bdb = null;
try {
opt = new Options().setCreateIfMissing(true);
bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options().setCreateIfMissing(true);
final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath())) {
assertThat(bopt.backupDir()).isEqualTo(
backupFolder.getRoot().getAbsolutePath());
// Open empty database.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(bdb);
// Create two backups
bdb.createNewBackup(false);
bdb.createNewBackup(true);
verifyNumberOfValidBackups(bdb, 2);
} finally {
if (bdb != null) {
bdb.close();
}
if (bopt != null) {
bopt.dispose();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void deleteBackup() throws RocksDBException {
Options opt = null;
BackupableDBOptions bopt = null;
BackupableDB bdb = null;
try {
opt = new Options().setCreateIfMissing(true);
bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options().setCreateIfMissing(true);
final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath())) {
assertThat(bopt.backupDir()).isEqualTo(
backupFolder.getRoot().getAbsolutePath());
// Open empty database.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(bdb);
// Create two backups
@ -82,20 +65,11 @@ public class BackupableDBTest {
verifyNumberOfValidBackups(bdb, 2);
// Delete the first backup
bdb.deleteBackup(backupInfo.get(0).backupId());
List<BackupInfo> newBackupInfo =
final List<BackupInfo> newBackupInfo =
verifyNumberOfValidBackups(bdb, 1);
// The second backup must remain.
assertThat(newBackupInfo.get(0).backupId()).
isEqualTo(backupInfo.get(1).backupId());
} finally {
if (bdb != null) {
bdb.close();
}
if (bopt != null) {
bopt.dispose();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -103,28 +77,23 @@ public class BackupableDBTest {
@Test
public void deleteBackupWithRestoreBackupableDB()
throws RocksDBException {
Options opt = null;
BackupableDBOptions bopt = null;
BackupableDB bdb = null;
RestoreBackupableDB rdb = null;
try {
opt = new Options().setCreateIfMissing(true);
bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options().setCreateIfMissing(true);
final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath())) {
assertThat(bopt.backupDir()).isEqualTo(
backupFolder.getRoot().getAbsolutePath());
// Open empty database.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(bdb);
// Create two backups
bdb.createNewBackup(false);
bdb.createNewBackup(true);
List<BackupInfo> backupInfo =
final List<BackupInfo> backupInfo =
verifyNumberOfValidBackups(bdb, 2);
// init RestoreBackupableDB
rdb = new RestoreBackupableDB(bopt);
try (final RestoreBackupableDB rdb = new RestoreBackupableDB(bopt)) {
// Delete the first backup
rdb.deleteBackup(backupInfo.get(0).backupId());
// Fetch backup info using RestoreBackupableDB
@ -132,36 +101,21 @@ public class BackupableDBTest {
// The second backup must remain.
assertThat(newBackupInfo.get(0).backupId()).
isEqualTo(backupInfo.get(1).backupId());
} finally {
if (bdb != null) {
bdb.close();
}
if (rdb != null) {
rdb.dispose();
}
if (bopt != null) {
bopt.dispose();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void purgeOldBackups() throws RocksDBException {
Options opt = null;
BackupableDBOptions bopt = null;
BackupableDB bdb = null;
try {
opt = new Options().setCreateIfMissing(true);
bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options().setCreateIfMissing(true);
final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath())) {
assertThat(bopt.backupDir()).isEqualTo(
backupFolder.getRoot().getAbsolutePath());
// Open empty database.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(bdb);
// Create two backups
@ -169,24 +123,15 @@ public class BackupableDBTest {
bdb.createNewBackup(true);
bdb.createNewBackup(true);
bdb.createNewBackup(true);
List<BackupInfo> backupInfo =
final List<BackupInfo> backupInfo =
verifyNumberOfValidBackups(bdb, 4);
// Delete everything except the latest backup
bdb.purgeOldBackups(1);
List<BackupInfo> newBackupInfo =
final List<BackupInfo> newBackupInfo =
verifyNumberOfValidBackups(bdb, 1);
// The latest backup must remain.
assertThat(newBackupInfo.get(0).backupId()).
isEqualTo(backupInfo.get(3).backupId());
} finally {
if (bdb != null) {
bdb.close();
}
if (bopt != null) {
bopt.dispose();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -194,19 +139,15 @@ public class BackupableDBTest {
@Test
public void purgeOldBackupsWithRestoreBackupableDb()
throws RocksDBException {
Options opt = null;
BackupableDBOptions bopt = null;
BackupableDB bdb = null;
RestoreBackupableDB rdb = null;
try {
opt = new Options().setCreateIfMissing(true);
bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options().setCreateIfMissing(true);
final BackupableDBOptions bopt =
new BackupableDBOptions(backupFolder.getRoot().getAbsolutePath())
) {
assertThat(bopt.backupDir()).isEqualTo(
backupFolder.getRoot().getAbsolutePath());
// Open empty database.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(bdb);
// Create two backups
@ -226,7 +167,7 @@ public class BackupableDBTest {
}
}
// init RestoreBackupableDB
rdb = new RestoreBackupableDB(bopt);
try (final RestoreBackupableDB rdb = new RestoreBackupableDB(bopt)) {
// the same number of backups must
// exist using RestoreBackupableDB.
verifyNumberOfValidBackups(rdb, 4);
@ -234,18 +175,7 @@ public class BackupableDBTest {
infos = verifyNumberOfValidBackups(rdb, 1);
assertThat(infos.get(0).timestamp()).
isEqualTo(maxTimeBeforePurge);
} finally {
if (bdb != null) {
bdb.close();
}
if (rdb != null) {
rdb.dispose();
}
if (bopt != null) {
bopt.dispose();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -253,19 +183,15 @@ public class BackupableDBTest {
@Test
public void restoreLatestBackup()
throws RocksDBException {
Options opt = null;
BackupableDBOptions bopt = null;
BackupableDB bdb = null;
RestoreBackupableDB rdb = null;
try {
opt = new Options().setCreateIfMissing(true);
bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options().setCreateIfMissing(true);
final BackupableDBOptions bopt =
new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath())) {
assertThat(bopt.backupDir()).isEqualTo(
backupFolder.getRoot().getAbsolutePath());
// Open empty database.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(bdb);
bdb.createNewBackup(true);
@ -278,33 +204,23 @@ public class BackupableDBTest {
bdb.put("key2".getBytes(), "valueV3".getBytes());
assertThat(new String(bdb.get("key1".getBytes()))).endsWith("V3");
assertThat(new String(bdb.get("key2".getBytes()))).endsWith("V3");
bdb.close();
}
// init RestoreBackupableDB
rdb = new RestoreBackupableDB(bopt);
try (final RestoreBackupableDB rdb = new RestoreBackupableDB(bopt)) {
verifyNumberOfValidBackups(rdb, 2);
// restore db from latest backup
rdb.restoreDBFromLatestBackup(dbFolder.getRoot().getAbsolutePath(),
dbFolder.getRoot().getAbsolutePath(),
new RestoreOptions(false));
}
// Open database again.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Values must have suffix V2 because of restoring latest backup.
assertThat(new String(bdb.get("key1".getBytes()))).endsWith("V2");
assertThat(new String(bdb.get("key2".getBytes()))).endsWith("V2");
} finally {
if (bdb != null) {
bdb.close();
}
if (rdb != null) {
rdb.dispose();
}
if (bopt != null) {
bopt.dispose();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -312,19 +228,14 @@ public class BackupableDBTest {
@Test
public void restoreFromBackup()
throws RocksDBException {
Options opt = null;
BackupableDBOptions bopt = null;
BackupableDB bdb = null;
RestoreBackupableDB rdb = null;
try {
opt = new Options().setCreateIfMissing(true);
bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options().setCreateIfMissing(true);
final BackupableDBOptions bopt = new BackupableDBOptions(
backupFolder.getRoot().getAbsolutePath())) {
assertThat(bopt.backupDir()).isEqualTo(
backupFolder.getRoot().getAbsolutePath());
// Open empty database.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Fill database with some test values
prepareDatabase(bdb);
bdb.createNewBackup(true);
@ -337,34 +248,24 @@ public class BackupableDBTest {
bdb.put("key2".getBytes(), "valueV3".getBytes());
assertThat(new String(bdb.get("key1".getBytes()))).endsWith("V3");
assertThat(new String(bdb.get("key2".getBytes()))).endsWith("V3");
bdb.close();
}
// init RestoreBackupableDB
rdb = new RestoreBackupableDB(bopt);
List<BackupInfo> backupInfo = verifyNumberOfValidBackups(rdb, 2);
try (final RestoreBackupableDB rdb = new RestoreBackupableDB(bopt)) {
final List<BackupInfo> backupInfo = verifyNumberOfValidBackups(rdb, 2);
// restore db from first backup
rdb.restoreDBFromBackup(backupInfo.get(0).backupId(),
dbFolder.getRoot().getAbsolutePath(),
dbFolder.getRoot().getAbsolutePath(),
new RestoreOptions(false));
}
// Open database again.
bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath());
try (final BackupableDB bdb = BackupableDB.open(opt, bopt,
dbFolder.getRoot().getAbsolutePath())) {
// Values must have suffix V2 because of restoring latest backup.
assertThat(new String(bdb.get("key1".getBytes()))).endsWith("V1");
assertThat(new String(bdb.get("key2".getBytes()))).endsWith("V1");
} finally {
if (bdb != null) {
bdb.close();
}
if (rdb != null) {
rdb.dispose();
}
if (bopt != null) {
bopt.dispose();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -377,8 +278,8 @@ public class BackupableDBTest {
* @throws RocksDBException thrown if an error occurs within the native
* part of the library.
*/
private List<BackupInfo> verifyNumberOfValidBackups(BackupableDB bdb,
int expectedNumberOfBackups) throws RocksDBException {
private List<BackupInfo> verifyNumberOfValidBackups(final BackupableDB bdb,
final int expectedNumberOfBackups) throws RocksDBException {
// Verify that backups exist
assertThat(bdb.getCorruptedBackups().length).
isEqualTo(0);
@ -398,7 +299,7 @@ public class BackupableDBTest {
* part of the library.
*/
private List<BackupInfo> verifyNumberOfValidBackups(
RestoreBackupableDB rdb, int expectedNumberOfBackups)
final RestoreBackupableDB rdb, final int expectedNumberOfBackups)
throws RocksDBException {
// Verify that backups exist
assertThat(rdb.getCorruptedBackups().length).
@ -417,7 +318,7 @@ public class BackupableDBTest {
* @throws RocksDBException thrown if an error occurs within the native
* part of the library.
*/
private void prepareDatabase(RocksDB db)
private void prepareDatabase(final RocksDB db)
throws RocksDBException {
db.put("key1".getBytes(), "valueV1".getBytes());
db.put("key2".getBytes(), "valueV1".getBytes());

@ -131,34 +131,20 @@ public class BlockBasedTableConfigTest {
@Test
public void blockBasedTableWithFilter() {
Options options = null;
try {
options = new Options();
options.setTableFormatConfig(
new BlockBasedTableConfig().setFilter(
new BloomFilter(10)));
try(final Options options = new Options()
.setTableFormatConfig(new BlockBasedTableConfig()
.setFilter(new BloomFilter(10)))) {
assertThat(options.tableFactoryName()).
isEqualTo("BlockBasedTable");
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void blockBasedTableWithoutFilter() {
Options options = null;
try {
options = new Options();
options.setTableFormatConfig(
new BlockBasedTableConfig().setFilter(null));
try(final Options options = new Options().setTableFormatConfig(
new BlockBasedTableConfig().setFilter(null))) {
assertThat(options.tableFactoryName()).
isEqualTo("BlockBasedTable");
} finally {
if (options != null) {
options.dispose();
}
}
}

@ -22,76 +22,61 @@ public class CheckPointTest {
@Test
public void checkPoint() throws RocksDBException {
RocksDB db = null;
Options options = null;
Checkpoint checkpoint = null;
try {
options = new Options().
setCreateIfMissing(true);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options().
setCreateIfMissing(true)) {
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put("key".getBytes(), "value".getBytes());
checkpoint = Checkpoint.create(db);
try (final Checkpoint checkpoint = Checkpoint.create(db)) {
checkpoint.createCheckpoint(checkpointFolder.
getRoot().getAbsolutePath() + "/snapshot1");
db.put("key2".getBytes(), "value2".getBytes());
checkpoint.createCheckpoint(checkpointFolder.
getRoot().getAbsolutePath() + "/snapshot2");
db.close();
db = RocksDB.open(options,
}
}
try (final RocksDB db = RocksDB.open(options,
checkpointFolder.getRoot().getAbsolutePath() +
"/snapshot1");
"/snapshot1")) {
assertThat(new String(db.get("key".getBytes()))).
isEqualTo("value");
assertThat(db.get("key2".getBytes())).isNull();
db.close();
db = RocksDB.open(options,
}
try (final RocksDB db = RocksDB.open(options,
checkpointFolder.getRoot().getAbsolutePath() +
"/snapshot2");
"/snapshot2")) {
assertThat(new String(db.get("key".getBytes()))).
isEqualTo("value");
assertThat(new String(db.get("key2".getBytes()))).
isEqualTo("value2");
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (checkpoint != null) {
checkpoint.dispose();
}
}
}
@Test(expected = IllegalArgumentException.class)
public void failIfDbIsNull() {
Checkpoint.create(null);
try (final Checkpoint checkpoint = Checkpoint.create(null)) {
}
}
@Test(expected = IllegalStateException.class)
public void failIfDbNotInitialized() throws RocksDBException {
RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
db.dispose();
try (final RocksDB db = RocksDB.open(
dbFolder.getRoot().getAbsolutePath())) {
db.close();
Checkpoint.create(db);
}
}
@Test(expected = RocksDBException.class)
public void failWithIllegalPath() throws RocksDBException {
RocksDB db = null;
Checkpoint checkpoint = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
checkpoint = Checkpoint.create(db);
try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
final Checkpoint checkpoint = Checkpoint.create(db)) {
checkpoint.createCheckpoint("/Z:///:\\C:\\TZ/-");
} finally {
if (db != null) {
db.close();
}
if (checkpoint != null) {
checkpoint.dispose();
}
}
}
}

@ -26,616 +26,386 @@ public class ColumnFamilyOptionsTest {
@Test
public void getColumnFamilyOptionsFromProps() {
ColumnFamilyOptions opt = null;
try {
// setup sample properties
Properties properties = new Properties();
properties.put("write_buffer_size", "112");
properties.put("max_write_buffer_number", "13");
opt = ColumnFamilyOptions.
getColumnFamilyOptionsFromProps(properties);
try (final ColumnFamilyOptions opt = ColumnFamilyOptions.
getColumnFamilyOptionsFromProps(properties)) {
// setup sample properties
assertThat(opt).isNotNull();
assertThat(String.valueOf(opt.writeBufferSize())).
isEqualTo(properties.get("write_buffer_size"));
assertThat(String.valueOf(opt.maxWriteBufferNumber())).
isEqualTo(properties.get("max_write_buffer_number"));
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void failColumnFamilyOptionsFromPropsWithIllegalValue() {
ColumnFamilyOptions opt = null;
try {
// setup sample properties
Properties properties = new Properties();
final Properties properties = new Properties();
properties.put("tomato", "1024");
properties.put("burger", "2");
opt = ColumnFamilyOptions.
getColumnFamilyOptionsFromProps(properties);
try (final ColumnFamilyOptions opt =
ColumnFamilyOptions.getColumnFamilyOptionsFromProps(properties)) {
assertThat(opt).isNull();
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test(expected = IllegalArgumentException.class)
public void failColumnFamilyOptionsFromPropsWithNullValue() {
ColumnFamilyOptions.getColumnFamilyOptionsFromProps(null);
try (final ColumnFamilyOptions opt =
ColumnFamilyOptions.getColumnFamilyOptionsFromProps(null)) {
}
}
@Test(expected = IllegalArgumentException.class)
public void failColumnFamilyOptionsFromPropsWithEmptyProps() {
try (final ColumnFamilyOptions opt =
ColumnFamilyOptions.getColumnFamilyOptionsFromProps(
new Properties());
new Properties())) {
}
}
@Test
public void writeBufferSize() throws RocksDBException {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
long longValue = rand.nextLong();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final long longValue = rand.nextLong();
opt.setWriteBufferSize(longValue);
assertThat(opt.writeBufferSize()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void maxWriteBufferNumber() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setMaxWriteBufferNumber(intValue);
assertThat(opt.maxWriteBufferNumber()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void minWriteBufferNumberToMerge() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setMinWriteBufferNumberToMerge(intValue);
assertThat(opt.minWriteBufferNumberToMerge()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void numLevels() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setNumLevels(intValue);
assertThat(opt.numLevels()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void levelZeroFileNumCompactionTrigger() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setLevelZeroFileNumCompactionTrigger(intValue);
assertThat(opt.levelZeroFileNumCompactionTrigger()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void levelZeroSlowdownWritesTrigger() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setLevelZeroSlowdownWritesTrigger(intValue);
assertThat(opt.levelZeroSlowdownWritesTrigger()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void levelZeroStopWritesTrigger() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setLevelZeroStopWritesTrigger(intValue);
assertThat(opt.levelZeroStopWritesTrigger()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void targetFileSizeBase() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
long longValue = rand.nextLong();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final long longValue = rand.nextLong();
opt.setTargetFileSizeBase(longValue);
assertThat(opt.targetFileSizeBase()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void targetFileSizeMultiplier() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setTargetFileSizeMultiplier(intValue);
assertThat(opt.targetFileSizeMultiplier()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void maxBytesForLevelBase() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
long longValue = rand.nextLong();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final long longValue = rand.nextLong();
opt.setMaxBytesForLevelBase(longValue);
assertThat(opt.maxBytesForLevelBase()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void levelCompactionDynamicLevelBytes() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setLevelCompactionDynamicLevelBytes(boolValue);
assertThat(opt.levelCompactionDynamicLevelBytes())
.isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void maxBytesForLevelMultiplier() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setMaxBytesForLevelMultiplier(intValue);
assertThat(opt.maxBytesForLevelMultiplier()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void expandedCompactionFactor() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setExpandedCompactionFactor(intValue);
assertThat(opt.expandedCompactionFactor()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void sourceCompactionFactor() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setSourceCompactionFactor(intValue);
assertThat(opt.sourceCompactionFactor()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void maxGrandparentOverlapFactor() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setMaxGrandparentOverlapFactor(intValue);
assertThat(opt.maxGrandparentOverlapFactor()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void softRateLimit() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
double doubleValue = rand.nextDouble();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final double doubleValue = rand.nextDouble();
opt.setSoftRateLimit(doubleValue);
assertThat(opt.softRateLimit()).isEqualTo(doubleValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void hardRateLimit() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
double doubleValue = rand.nextDouble();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final double doubleValue = rand.nextDouble();
opt.setHardRateLimit(doubleValue);
assertThat(opt.hardRateLimit()).isEqualTo(doubleValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void rateLimitDelayMaxMilliseconds() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setRateLimitDelayMaxMilliseconds(intValue);
assertThat(opt.rateLimitDelayMaxMilliseconds()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void arenaBlockSize() throws RocksDBException {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
long longValue = rand.nextLong();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final long longValue = rand.nextLong();
opt.setArenaBlockSize(longValue);
assertThat(opt.arenaBlockSize()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void disableAutoCompactions() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
boolean boolValue = rand.nextBoolean();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setDisableAutoCompactions(boolValue);
assertThat(opt.disableAutoCompactions()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void purgeRedundantKvsWhileFlush() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
boolean boolValue = rand.nextBoolean();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setPurgeRedundantKvsWhileFlush(boolValue);
assertThat(opt.purgeRedundantKvsWhileFlush()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void verifyChecksumsInCompaction() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
boolean boolValue = rand.nextBoolean();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setVerifyChecksumsInCompaction(boolValue);
assertThat(opt.verifyChecksumsInCompaction()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void filterDeletes() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
boolean boolValue = rand.nextBoolean();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setFilterDeletes(boolValue);
assertThat(opt.filterDeletes()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void maxSequentialSkipInIterations() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
long longValue = rand.nextLong();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final long longValue = rand.nextLong();
opt.setMaxSequentialSkipInIterations(longValue);
assertThat(opt.maxSequentialSkipInIterations()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void inplaceUpdateSupport() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
boolean boolValue = rand.nextBoolean();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setInplaceUpdateSupport(boolValue);
assertThat(opt.inplaceUpdateSupport()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void inplaceUpdateNumLocks() throws RocksDBException {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
long longValue = rand.nextLong();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final long longValue = rand.nextLong();
opt.setInplaceUpdateNumLocks(longValue);
assertThat(opt.inplaceUpdateNumLocks()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void memtablePrefixBloomBits() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
int intValue = rand.nextInt();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setMemtablePrefixBloomBits(intValue);
assertThat(opt.memtablePrefixBloomBits()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void memtablePrefixBloomProbes() {
ColumnFamilyOptions opt = null;
try {
int intValue = rand.nextInt();
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setMemtablePrefixBloomProbes(intValue);
assertThat(opt.memtablePrefixBloomProbes()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void bloomLocality() {
ColumnFamilyOptions opt = null;
try {
int intValue = rand.nextInt();
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setBloomLocality(intValue);
assertThat(opt.bloomLocality()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void maxSuccessiveMerges() throws RocksDBException {
ColumnFamilyOptions opt = null;
try {
long longValue = rand.nextLong();
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final long longValue = rand.nextLong();
opt.setMaxSuccessiveMerges(longValue);
assertThat(opt.maxSuccessiveMerges()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void minPartialMergeOperands() {
ColumnFamilyOptions opt = null;
try {
int intValue = rand.nextInt();
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final int intValue = rand.nextInt();
opt.setMinPartialMergeOperands(intValue);
assertThat(opt.minPartialMergeOperands()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void optimizeFiltersForHits() {
ColumnFamilyOptions opt = null;
try {
boolean aBoolean = rand.nextBoolean();
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
final boolean aBoolean = rand.nextBoolean();
opt.setOptimizeFiltersForHits(aBoolean);
assertThat(opt.optimizeFiltersForHits()).isEqualTo(aBoolean);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void memTable() throws RocksDBException {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
opt.setMemTableConfig(new HashLinkedListMemTableConfig());
assertThat(opt.memTableFactoryName()).
isEqualTo("HashLinkedListRepFactory");
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void comparator() throws RocksDBException {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
opt.setComparator(BuiltinComparator.BYTEWISE_COMPARATOR);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void linkageOfPrepMethods() {
ColumnFamilyOptions options = null;
try {
options = new ColumnFamilyOptions();
try (final ColumnFamilyOptions options = new ColumnFamilyOptions()) {
options.optimizeUniversalStyleCompaction();
options.optimizeUniversalStyleCompaction(4000);
options.optimizeLevelStyleCompaction();
options.optimizeLevelStyleCompaction(3000);
options.optimizeForPointLookup(10);
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void shouldSetTestPrefixExtractor() {
ColumnFamilyOptions options = null;
try {
options = new ColumnFamilyOptions();
try (final ColumnFamilyOptions options = new ColumnFamilyOptions()) {
options.useFixedLengthPrefixExtractor(100);
options.useFixedLengthPrefixExtractor(10);
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void shouldSetTestCappedPrefixExtractor() {
ColumnFamilyOptions options = null;
try {
options = new ColumnFamilyOptions();
try (final ColumnFamilyOptions options = new ColumnFamilyOptions()) {
options.useCappedPrefixExtractor(100);
options.useCappedPrefixExtractor(10);
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void compressionTypes() {
ColumnFamilyOptions columnFamilyOptions = null;
try {
columnFamilyOptions = new ColumnFamilyOptions();
for (CompressionType compressionType :
try (final ColumnFamilyOptions columnFamilyOptions
= new ColumnFamilyOptions()) {
for (final CompressionType compressionType :
CompressionType.values()) {
columnFamilyOptions.setCompressionType(compressionType);
assertThat(columnFamilyOptions.compressionType()).
@ -643,18 +413,13 @@ public class ColumnFamilyOptionsTest {
assertThat(CompressionType.valueOf("NO_COMPRESSION")).
isEqualTo(CompressionType.NO_COMPRESSION);
}
} finally {
if (columnFamilyOptions != null) {
columnFamilyOptions.dispose();
}
}
}
@Test
public void compressionPerLevel() {
ColumnFamilyOptions columnFamilyOptions = null;
try {
columnFamilyOptions = new ColumnFamilyOptions();
try (final ColumnFamilyOptions columnFamilyOptions
= new ColumnFamilyOptions()) {
assertThat(columnFamilyOptions.compressionPerLevel()).isEmpty();
List<CompressionType> compressionTypeList = new ArrayList<>();
for (int i = 0; i < columnFamilyOptions.numLevels(); i++) {
@ -666,18 +431,13 @@ public class ColumnFamilyOptionsTest {
assertThat(compressionType).isEqualTo(
CompressionType.NO_COMPRESSION);
}
} finally {
if (columnFamilyOptions != null) {
columnFamilyOptions.dispose();
}
}
}
@Test
public void differentCompressionsPerLevel() {
ColumnFamilyOptions columnFamilyOptions = null;
try {
columnFamilyOptions = new ColumnFamilyOptions();
try (final ColumnFamilyOptions columnFamilyOptions
= new ColumnFamilyOptions()) {
columnFamilyOptions.setNumLevels(3);
assertThat(columnFamilyOptions.compressionPerLevel()).isEmpty();
@ -697,38 +457,27 @@ public class ColumnFamilyOptionsTest {
CompressionType.SNAPPY_COMPRESSION,
CompressionType.LZ4_COMPRESSION);
} finally {
if (columnFamilyOptions != null) {
columnFamilyOptions.dispose();
}
}
}
@Test
public void compactionStyles() {
ColumnFamilyOptions ColumnFamilyOptions = null;
try {
ColumnFamilyOptions = new ColumnFamilyOptions();
for (CompactionStyle compactionStyle :
try (final ColumnFamilyOptions columnFamilyOptions
= new ColumnFamilyOptions()) {
for (final CompactionStyle compactionStyle :
CompactionStyle.values()) {
ColumnFamilyOptions.setCompactionStyle(compactionStyle);
assertThat(ColumnFamilyOptions.compactionStyle()).
columnFamilyOptions.setCompactionStyle(compactionStyle);
assertThat(columnFamilyOptions.compactionStyle()).
isEqualTo(compactionStyle);
assertThat(CompactionStyle.valueOf("FIFO")).
isEqualTo(CompactionStyle.FIFO);
}
} finally {
if (ColumnFamilyOptions != null) {
ColumnFamilyOptions.dispose();
}
}
}
@Test
public void maxTableFilesSizeFIFO() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
long longValue = rand.nextLong();
// Size has to be positive
longValue = (longValue < 0) ? -longValue : longValue;
@ -736,10 +485,6 @@ public class ColumnFamilyOptionsTest {
opt.setMaxTableFilesSizeFIFO(longValue);
assertThat(opt.maxTableFilesSizeFIFO()).
isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
}

@ -25,43 +25,26 @@ public class ColumnFamilyTest {
@Test
public void listColumnFamilies() throws RocksDBException {
RocksDB db = null;
Options options = null;
try {
options = new Options();
options.setCreateIfMissing(true);
DBOptions dbOptions = new DBOptions();
dbOptions.setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
// Test listColumnFamilies
List<byte[]> columnFamilyNames;
columnFamilyNames = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath());
final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies(options,
dbFolder.getRoot().getAbsolutePath());
assertThat(columnFamilyNames).isNotNull();
assertThat(columnFamilyNames.size()).isGreaterThan(0);
assertThat(columnFamilyNames.size()).isEqualTo(1);
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default");
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void defaultColumnFamily() throws RocksDBException {
RocksDB db = null;
Options options = null;
ColumnFamilyHandle cfh;
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
final ColumnFamilyHandle cfh = db.getDefaultColumnFamily();
try {
options = new Options().setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
cfh = db.getDefaultColumnFamily();
assertThat(cfh).isNotNull();
final byte[] key = "key".getBytes();
@ -74,66 +57,52 @@ public class ColumnFamilyTest {
assertThat(cfh).isNotNull();
assertThat(actualValue).isEqualTo(value);
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
cfh.close();
}
}
}
@Test
public void createColumnFamily() throws RocksDBException {
RocksDB db = null;
Options options = null;
ColumnFamilyHandle columnFamilyHandle = null;
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
final ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf".getBytes(),
new ColumnFamilyOptions()));
try {
options = new Options();
options.setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
columnFamilyHandle = db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf".getBytes(), new ColumnFamilyOptions()));
List<byte[]> columnFamilyNames;
columnFamilyNames = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath());
final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies(
options, dbFolder.getRoot().getAbsolutePath());
assertThat(columnFamilyNames).isNotNull();
assertThat(columnFamilyNames.size()).isGreaterThan(0);
assertThat(columnFamilyNames.size()).isEqualTo(2);
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default");
assertThat(new String(columnFamilyNames.get(1))).isEqualTo("new_cf");
} finally {
if (columnFamilyHandle != null) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
columnFamilyHandle.close();
}
}
}
@Test
public void openWithColumnFamilies() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
final List<ColumnFamilyDescriptor> cfNames = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes())
);
final List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
try {
options = new DBOptions();
options.setCreateIfMissing(true);
options.setCreateMissingColumnFamilies(true);
// Test open database with column family names
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfNames,
columnFamilyHandleList)) {
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
try {
assertThat(columnFamilyHandleList.size()).isEqualTo(2);
db.put("dfkey1".getBytes(), "dfvalue".getBytes());
db.put(columnFamilyHandleList.get(0), "dfkey2".getBytes(),
@ -154,43 +123,38 @@ public class ColumnFamilyTest {
assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(),
"dfkey2".getBytes())).isNull();
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void getWithOutValueAndCf() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfDescriptors =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
try {
options = new DBOptions();
options.setCreateIfMissing(true);
options.setCreateMissingColumnFamilies(true);
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
// Test open database with column family names
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList);
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
columnFamilyHandleList)) {
try {
db.put(columnFamilyHandleList.get(0), new WriteOptions(),
"key1".getBytes(), "value".getBytes());
db.put("key2".getBytes(), "12345678".getBytes());
byte[] outValue = new byte[5];
final byte[] outValue = new byte[5];
// not found value
int getResult = db.get("keyNotFound".getBytes(), outValue);
assertThat(getResult).isEqualTo(RocksDB.NOT_FOUND);
// found value which fits in outValue
getResult = db.get(columnFamilyHandleList.get(0), "key1".getBytes(), outValue);
getResult = db.get(columnFamilyHandleList.get(0), "key1".getBytes(),
outValue);
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND);
assertThat(outValue).isEqualTo("value".getBytes());
// found value which fits partially
@ -199,79 +163,62 @@ public class ColumnFamilyTest {
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND);
assertThat(outValue).isEqualTo("12345".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void createWriteDropColumnFamily() throws RocksDBException {
RocksDB db = null;
DBOptions opt = null;
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
columnFamilyHandleList)) {
ColumnFamilyHandle tmpColumnFamilyHandle = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
try {
opt = new DBOptions();
opt.setCreateIfMissing(true);
opt.setCreateMissingColumnFamilies(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
tmpColumnFamilyHandle = db.createColumnFamily(
new ColumnFamilyDescriptor("tmpCF".getBytes(), new ColumnFamilyOptions()));
new ColumnFamilyDescriptor("tmpCF".getBytes(),
new ColumnFamilyOptions()));
db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes());
db.dropColumnFamily(tmpColumnFamilyHandle);
tmpColumnFamilyHandle.dispose();
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (tmpColumnFamilyHandle != null) {
tmpColumnFamilyHandle.dispose();
tmpColumnFamilyHandle.close();
}
if (db != null) {
db.close();
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void writeBatch() throws RocksDBException {
RocksDB db = null;
DBOptions opt = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
try (final ColumnFamilyOptions defaultCfOptions = new ColumnFamilyOptions()
.setMergeOperator(new StringAppendOperator())) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
defaultCfOptions),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList);
final WriteBatch writeBatch = new WriteBatch();
final WriteOptions writeOpt = new WriteOptions()) {
try {
opt = new DBOptions();
opt.setCreateIfMissing(true);
opt.setCreateMissingColumnFamilies(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions().setMergeOperator(new StringAppendOperator())));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
WriteBatch writeBatch = new WriteBatch();
WriteOptions writeOpt = new WriteOptions();
writeBatch.put("key".getBytes(), "value".getBytes());
writeBatch.put(db.getDefaultColumnFamily(),
"mergeKey".getBytes(), "merge".getBytes());
@ -284,7 +231,7 @@ public class ColumnFamilyTest {
writeBatch.remove("xyz".getBytes());
writeBatch.remove(columnFamilyHandleList.get(1), "xyz".getBytes());
db.write(writeOpt, writeBatch);
writeBatch.dispose();
assertThat(db.get(columnFamilyHandleList.get(1),
"xyz".getBytes()) == null);
assertThat(new String(db.get(columnFamilyHandleList.get(1),
@ -296,43 +243,35 @@ public class ColumnFamilyTest {
assertThat(new String(db.get(db.getDefaultColumnFamily(),
"mergeKey".getBytes()))).isEqualTo("merge,merge");
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void iteratorOnColumnFamily() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
RocksIterator rocksIterator = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList)) {
try {
options = new DBOptions();
options.setCreateIfMissing(true);
options.setCreateMissingColumnFamilies(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(),
"value".getBytes());
db.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(),
"value2".getBytes());
rocksIterator = db.newIterator(
columnFamilyHandleList.get(1));
try (final RocksIterator rocksIterator =
db.newIterator(columnFamilyHandleList.get(1))) {
rocksIterator.seekToFirst();
Map<String, String> refMap = new HashMap<>();
refMap.put("newcfkey", "value");
@ -345,88 +284,73 @@ public class ColumnFamilyTest {
rocksIterator.next();
}
assertThat(i).isEqualTo(2);
rocksIterator.dispose();
} finally {
if (rocksIterator != null) {
rocksIterator.dispose();
}
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void multiGet() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfDescriptors =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList)) {
try {
options = new DBOptions();
options.setCreateIfMissing(true);
options.setCreateMissingColumnFamilies(true);
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList);
db.put(columnFamilyHandleList.get(0), "key".getBytes(), "value".getBytes());
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), "value".getBytes());
db.put(columnFamilyHandleList.get(0), "key".getBytes(),
"value".getBytes());
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(),
"value".getBytes());
List<byte[]> keys = Arrays.asList(new byte[][]{"key".getBytes(), "newcfkey".getBytes()});
Map<byte[], byte[]> retValues = db.multiGet(columnFamilyHandleList, keys);
final List<byte[]> keys = Arrays.asList(new byte[][]{
"key".getBytes(), "newcfkey".getBytes()
});
Map<byte[], byte[]> retValues = db.multiGet(columnFamilyHandleList,
keys);
assertThat(retValues.size()).isEqualTo(2);
assertThat(new String(retValues.get(keys.get(0))))
.isEqualTo("value");
assertThat(new String(retValues.get(keys.get(1))))
.isEqualTo("value");
retValues = db.multiGet(new ReadOptions(), columnFamilyHandleList, keys);
retValues = db.multiGet(new ReadOptions(), columnFamilyHandleList,
keys);
assertThat(retValues.size()).isEqualTo(2);
assertThat(new String(retValues.get(keys.get(0))))
.isEqualTo("value");
assertThat(new String(retValues.get(keys.get(1))))
.isEqualTo("value");
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void properties() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList)) {
try {
options = new DBOptions();
options.setCreateIfMissing(true);
options.setCreateMissingColumnFamilies(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
assertThat(db.getProperty("rocksdb.estimate-num-keys")).
isNotNull();
assertThat(db.getLongProperty(columnFamilyHandleList.get(0),
@ -441,14 +365,10 @@ public class ColumnFamilyTest {
assertThat(db.getProperty(columnFamilyHandleList.get(1),
"rocksdb.sstables")).isNotNull();
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@ -456,28 +376,23 @@ public class ColumnFamilyTest {
@Test
public void iterators() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
columnFamilyHandleList)) {
List<RocksIterator> iterators = null;
try {
options = new DBOptions();
options.setCreateIfMissing(true);
options.setCreateMissingColumnFamilies(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
iterators = db.newIterators(columnFamilyHandleList);
assertThat(iterators.size()).isEqualTo(2);
RocksIterator iter = iterators.get(0);
iter.seekToFirst();
Map<String, String> defRefMap = new HashMap<>();
final Map<String, String> defRefMap = new HashMap<>();
defRefMap.put("dfkey1", "dfvalue");
defRefMap.put("key", "value");
while (iter.isValid()) {
@ -486,7 +401,7 @@ public class ColumnFamilyTest {
iter.next();
}
// iterate over new_cf key/value pairs
Map<String, String> cfRefMap = new HashMap<>();
final Map<String, String> cfRefMap = new HashMap<>();
cfRefMap.put("newcfkey", "value");
cfRefMap.put("newcfkey2", "value2");
iter = iterators.get(1);
@ -498,247 +413,193 @@ public class ColumnFamilyTest {
}
} finally {
if (iterators != null) {
for (RocksIterator rocksIterator : iterators) {
rocksIterator.dispose();
}
for (final RocksIterator rocksIterator : iterators) {
rocksIterator.close();
}
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failPutDisposedCF() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList)) {
try {
options = new DBOptions();
options.setCreateIfMissing(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
db.dropColumnFamily(columnFamilyHandleList.get(1));
db.put(columnFamilyHandleList.get(1), "key".getBytes(), "value".getBytes());
db.put(columnFamilyHandleList.get(1), "key".getBytes(),
"value".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failRemoveDisposedCF() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList)) {
try {
options = new DBOptions();
options.setCreateIfMissing(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
db.dropColumnFamily(columnFamilyHandleList.get(1));
db.remove(columnFamilyHandleList.get(1), "key".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failGetDisposedCF() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
columnFamilyHandleList)) {
try {
options = new DBOptions();
options.setCreateIfMissing(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
db.dropColumnFamily(columnFamilyHandleList.get(1));
db.get(columnFamilyHandleList.get(1), "key".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failMultiGetWithoutCorrectNumberOfCF() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes()));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
columnFamilyHandleList)) {
try {
options = new DBOptions();
options.setCreateIfMissing(true);
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList);
List<byte[]> keys = new ArrayList<>();
final List<byte[]> keys = new ArrayList<>();
keys.add("key".getBytes());
keys.add("newcfkey".getBytes());
List<ColumnFamilyHandle> cfCustomList = new ArrayList<>();
final List<ColumnFamilyHandle> cfCustomList = new ArrayList<>();
db.multiGet(cfCustomList, keys);
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void testByteCreateFolumnFamily() throws RocksDBException {
RocksDB db = null;
Options options = null;
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())
) {
final byte[] b0 = new byte[]{(byte) 0x00};
final byte[] b1 = new byte[]{(byte) 0x01};
final byte[] b2 = new byte[]{(byte) 0x02};
ColumnFamilyHandle cf1 = null, cf2 = null, cf3 = null;
try {
options = new Options().setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
byte[] b0 = new byte[] { (byte)0x00 };
byte[] b1 = new byte[] { (byte)0x01 };
byte[] b2 = new byte[] { (byte)0x02 };
cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0));
cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1));
List<byte[]> families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath());
final List<byte[]> families = RocksDB.listColumnFamilies(options,
dbFolder.getRoot().getAbsolutePath());
assertThat(families).contains("default".getBytes(), b0, b1);
cf3 = db.createColumnFamily(new ColumnFamilyDescriptor(b2));
} finally {
if (cf1 != null) {
cf1.dispose();
cf1.close();
}
if (cf2 != null) {
cf2.dispose();
cf2.close();
}
if (cf3 != null) {
cf3.dispose();
cf3.close();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void testCFNamesWithZeroBytes() throws RocksDBException {
RocksDB db = null;
Options options = null;
ColumnFamilyHandle cf1 = null, cf2 = null;
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
) {
try {
options = new Options().setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
byte[] b0 = new byte[] { 0, 0 };
byte[] b1 = new byte[] { 0, 1 };
final byte[] b0 = new byte[]{0, 0};
final byte[] b1 = new byte[]{0, 1};
cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0));
cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1));
List<byte[]> families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath());
final List<byte[]> families = RocksDB.listColumnFamilies(options,
dbFolder.getRoot().getAbsolutePath());
assertThat(families).contains("default".getBytes(), b0, b1);
} finally {
if (cf1 != null) {
cf1.dispose();
cf1.close();
}
if (cf2 != null) {
cf2.dispose();
}
if (db != null) {
db.close();
cf2.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void testCFNameSimplifiedChinese() throws RocksDBException {
RocksDB db = null;
Options options = null;
ColumnFamilyHandle columnFamilyHandle = null;
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
) {
try {
options = new Options().setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
final String simplifiedChinese = "\u7b80\u4f53\u5b57";
columnFamilyHandle = db.createColumnFamily(
new ColumnFamilyDescriptor(simplifiedChinese.getBytes()));
List<byte[]> families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath());
assertThat(families).contains("default".getBytes(), simplifiedChinese.getBytes());
final List<byte[]> families = RocksDB.listColumnFamilies(options,
dbFolder.getRoot().getAbsolutePath());
assertThat(families).contains("default".getBytes(),
simplifiedChinese.getBytes());
} finally {
if (columnFamilyHandle != null) {
columnFamilyHandle.dispose();
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
}

@ -18,18 +18,15 @@ public class ComparatorOptionsTest {
@Test
public void comparatorOptions() {
final ComparatorOptions copt = new ComparatorOptions();
try(final ComparatorOptions copt = new ComparatorOptions()) {
assertThat(copt).isNotNull();
{ // UseAdaptiveMutex test
// UseAdaptiveMutex test
copt.setUseAdaptiveMutex(true);
assertThat(copt.useAdaptiveMutex()).isTrue();
copt.setUseAdaptiveMutex(false);
assertThat(copt.useAdaptiveMutex()).isFalse();
}
copt.dispose();
}
}

@ -79,21 +79,17 @@ public class ComparatorTest {
@Test
public void builtinForwardComparator()
throws RocksDBException {
Options options = null;
RocksDB rocksDB = null;
RocksIterator rocksIterator = null;
try {
options = new Options();
options.setCreateIfMissing(true);
options.setComparator(BuiltinComparator.BYTEWISE_COMPARATOR);
rocksDB = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
rocksDB.put("abc1".getBytes(), "abc1".getBytes());
rocksDB.put("abc2".getBytes(), "abc2".getBytes());
rocksDB.put("abc3".getBytes(), "abc3".getBytes());
rocksIterator = rocksDB.newIterator();
try (final Options options = new Options()
.setCreateIfMissing(true)
.setComparator(BuiltinComparator.BYTEWISE_COMPARATOR);
final RocksDB rocksDb = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())
) {
rocksDb.put("abc1".getBytes(), "abc1".getBytes());
rocksDb.put("abc2".getBytes(), "abc2".getBytes());
rocksDb.put("abc3".getBytes(), "abc3".getBytes());
try(final RocksIterator rocksIterator = rocksDb.newIterator()) {
// Iterate over keys using a iterator
rocksIterator.seekToFirst();
assertThat(rocksIterator.isValid()).isTrue();
@ -129,16 +125,6 @@ public class ComparatorTest {
"abc1".getBytes());
assertThat(rocksIterator.value()).isEqualTo(
"abc1".getBytes());
} finally {
if (rocksIterator != null) {
rocksIterator.dispose();
}
if (rocksDB != null) {
rocksDB.close();
}
if (options != null) {
options.dispose();
}
}
}
@ -146,22 +132,18 @@ public class ComparatorTest {
@Test
public void builtinReverseComparator()
throws RocksDBException {
Options options = null;
RocksDB rocksDB = null;
RocksIterator rocksIterator = null;
try {
options = new Options();
options.setCreateIfMissing(true);
options.setComparator(
BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR);
rocksDB = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
rocksDB.put("abc1".getBytes(), "abc1".getBytes());
rocksDB.put("abc2".getBytes(), "abc2".getBytes());
rocksDB.put("abc3".getBytes(), "abc3".getBytes());
rocksIterator = rocksDB.newIterator();
try (final Options options = new Options()
.setCreateIfMissing(true)
.setComparator(BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR);
final RocksDB rocksDb = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())
) {
rocksDb.put("abc1".getBytes(), "abc1".getBytes());
rocksDb.put("abc2".getBytes(), "abc2".getBytes());
rocksDb.put("abc3".getBytes(), "abc3".getBytes());
try (final RocksIterator rocksIterator = rocksDb.newIterator()) {
// Iterate over keys using a iterator
rocksIterator.seekToFirst();
assertThat(rocksIterator.isValid()).isTrue();
@ -200,15 +182,6 @@ public class ComparatorTest {
"abc3".getBytes());
assertThat(rocksIterator.value()).isEqualTo(
"abc3".getBytes());
} finally {
if (rocksIterator != null) {
rocksIterator.dispose();
}
if (rocksDB != null) {
rocksDB.close();
}
if (options != null) {
options.dispose();
}
}
}

@ -8,11 +8,10 @@ package org.rocksdb;
import org.junit.Test;
public class CompressionOptionsTest
{
public class CompressionOptionsTest {
@Test
public void getCompressionType() {
for (CompressionType compressionType : CompressionType.values()) {
for (final CompressionType compressionType : CompressionType.values()) {
String libraryName = compressionType.getLibraryName();
compressionType.equals(CompressionType.getCompressionType(
libraryName));

@ -24,547 +24,339 @@ public class DBOptionsTest {
@Test
public void getDBOptionsFromProps() {
DBOptions opt = null;
try {
// setup sample properties
Properties properties = new Properties();
final Properties properties = new Properties();
properties.put("allow_mmap_reads", "true");
properties.put("bytes_per_sync", "13");
opt = DBOptions.getDBOptionsFromProps(properties);
try(final DBOptions opt = DBOptions.getDBOptionsFromProps(properties)) {
assertThat(opt).isNotNull();
assertThat(String.valueOf(opt.allowMmapReads())).
isEqualTo(properties.get("allow_mmap_reads"));
assertThat(String.valueOf(opt.bytesPerSync())).
isEqualTo(properties.get("bytes_per_sync"));
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void failDBOptionsFromPropsWithIllegalValue() {
DBOptions opt = null;
try {
// setup sample properties
Properties properties = new Properties();
final Properties properties = new Properties();
properties.put("tomato", "1024");
properties.put("burger", "2");
opt = DBOptions.
getDBOptionsFromProps(properties);
try(final DBOptions opt = DBOptions.getDBOptionsFromProps(properties)) {
assertThat(opt).isNull();
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test(expected = IllegalArgumentException.class)
public void failDBOptionsFromPropsWithNullValue() {
DBOptions.getDBOptionsFromProps(null);
try(final DBOptions opt = DBOptions.getDBOptionsFromProps(null)) {
//no-op
}
}
@Test(expected = IllegalArgumentException.class)
public void failDBOptionsFromPropsWithEmptyProps() {
DBOptions.getDBOptionsFromProps(
new Properties());
try(final DBOptions opt = DBOptions.getDBOptionsFromProps(
new Properties())) {
//no-op
}
}
@Test
public void setIncreaseParallelism() {
DBOptions opt = null;
try {
opt = new DBOptions();
try(final DBOptions opt = new DBOptions()) {
final int threads = Runtime.getRuntime().availableProcessors() * 2;
opt.setIncreaseParallelism(threads);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void createIfMissing() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setCreateIfMissing(boolValue);
assertThat(opt.createIfMissing()).
isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.createIfMissing()).isEqualTo(boolValue);
}
}
@Test
public void createMissingColumnFamilies() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setCreateMissingColumnFamilies(boolValue);
assertThat(opt.createMissingColumnFamilies()).
isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.createMissingColumnFamilies()).isEqualTo(boolValue);
}
}
@Test
public void errorIfExists() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setErrorIfExists(boolValue);
assertThat(opt.errorIfExists()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void paranoidChecks() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setParanoidChecks(boolValue);
assertThat(opt.paranoidChecks()).
isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.paranoidChecks()).isEqualTo(boolValue);
}
}
@Test
public void maxTotalWalSize() {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setMaxTotalWalSize(longValue);
assertThat(opt.maxTotalWalSize()).
isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.maxTotalWalSize()).isEqualTo(longValue);
}
}
@Test
public void maxOpenFiles() {
DBOptions opt = null;
try {
opt = new DBOptions();
int intValue = rand.nextInt();
try(final DBOptions opt = new DBOptions()) {
final int intValue = rand.nextInt();
opt.setMaxOpenFiles(intValue);
assertThat(opt.maxOpenFiles()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void disableDataSync() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setDisableDataSync(boolValue);
assertThat(opt.disableDataSync()).
isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.disableDataSync()).isEqualTo(boolValue);
}
}
@Test
public void useFsync() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setUseFsync(boolValue);
assertThat(opt.useFsync()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void dbLogDir() {
DBOptions opt = null;
try {
opt = new DBOptions();
String str = "path/to/DbLogDir";
try(final DBOptions opt = new DBOptions()) {
final String str = "path/to/DbLogDir";
opt.setDbLogDir(str);
assertThat(opt.dbLogDir()).isEqualTo(str);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void walDir() {
DBOptions opt = null;
try {
opt = new DBOptions();
String str = "path/to/WalDir";
try(final DBOptions opt = new DBOptions()) {
final String str = "path/to/WalDir";
opt.setWalDir(str);
assertThat(opt.walDir()).isEqualTo(str);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void deleteObsoleteFilesPeriodMicros() {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setDeleteObsoleteFilesPeriodMicros(longValue);
assertThat(opt.deleteObsoleteFilesPeriodMicros()).
isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.deleteObsoleteFilesPeriodMicros()).isEqualTo(longValue);
}
}
@Test
public void maxBackgroundCompactions() {
DBOptions opt = null;
try {
opt = new DBOptions();
int intValue = rand.nextInt();
try(final DBOptions opt = new DBOptions()) {
final int intValue = rand.nextInt();
opt.setMaxBackgroundCompactions(intValue);
assertThat(opt.maxBackgroundCompactions()).
isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.maxBackgroundCompactions()).isEqualTo(intValue);
}
}
@Test
public void maxBackgroundFlushes() {
DBOptions opt = null;
try {
opt = new DBOptions();
int intValue = rand.nextInt();
try(final DBOptions opt = new DBOptions()) {
final int intValue = rand.nextInt();
opt.setMaxBackgroundFlushes(intValue);
assertThat(opt.maxBackgroundFlushes()).
isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.maxBackgroundFlushes()).isEqualTo(intValue);
}
}
@Test
public void maxLogFileSize() throws RocksDBException {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setMaxLogFileSize(longValue);
assertThat(opt.maxLogFileSize()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void logFileTimeToRoll() throws RocksDBException {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setLogFileTimeToRoll(longValue);
assertThat(opt.logFileTimeToRoll()).
isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.logFileTimeToRoll()).isEqualTo(longValue);
}
}
@Test
public void keepLogFileNum() throws RocksDBException {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setKeepLogFileNum(longValue);
assertThat(opt.keepLogFileNum()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void maxManifestFileSize() {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setMaxManifestFileSize(longValue);
assertThat(opt.maxManifestFileSize()).
isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.maxManifestFileSize()).isEqualTo(longValue);
}
}
@Test
public void tableCacheNumshardbits() {
DBOptions opt = null;
try {
opt = new DBOptions();
int intValue = rand.nextInt();
try(final DBOptions opt = new DBOptions()) {
final int intValue = rand.nextInt();
opt.setTableCacheNumshardbits(intValue);
assertThat(opt.tableCacheNumshardbits()).
isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.tableCacheNumshardbits()).isEqualTo(intValue);
}
}
@Test
public void walSizeLimitMB() {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setWalSizeLimitMB(longValue);
assertThat(opt.walSizeLimitMB()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void walTtlSeconds() {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setWalTtlSeconds(longValue);
assertThat(opt.walTtlSeconds()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void manifestPreallocationSize() throws RocksDBException {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setManifestPreallocationSize(longValue);
assertThat(opt.manifestPreallocationSize()).
isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
assertThat(opt.manifestPreallocationSize()).isEqualTo(longValue);
}
}
@Test
public void allowOsBuffer() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setAllowOsBuffer(boolValue);
assertThat(opt.allowOsBuffer()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void allowMmapReads() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setAllowMmapReads(boolValue);
assertThat(opt.allowMmapReads()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void allowMmapWrites() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setAllowMmapWrites(boolValue);
assertThat(opt.allowMmapWrites()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void isFdCloseOnExec() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setIsFdCloseOnExec(boolValue);
assertThat(opt.isFdCloseOnExec()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void statsDumpPeriodSec() {
DBOptions opt = null;
try {
opt = new DBOptions();
int intValue = rand.nextInt();
try(final DBOptions opt = new DBOptions()) {
final int intValue = rand.nextInt();
opt.setStatsDumpPeriodSec(intValue);
assertThat(opt.statsDumpPeriodSec()).isEqualTo(intValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void adviseRandomOnOpen() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setAdviseRandomOnOpen(boolValue);
assertThat(opt.adviseRandomOnOpen()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void useAdaptiveMutex() {
DBOptions opt = null;
try {
opt = new DBOptions();
boolean boolValue = rand.nextBoolean();
try(final DBOptions opt = new DBOptions()) {
final boolean boolValue = rand.nextBoolean();
opt.setUseAdaptiveMutex(boolValue);
assertThat(opt.useAdaptiveMutex()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void bytesPerSync() {
DBOptions opt = null;
try {
opt = new DBOptions();
long longValue = rand.nextLong();
try(final DBOptions opt = new DBOptions()) {
final long longValue = rand.nextLong();
opt.setBytesPerSync(longValue);
assertThat(opt.bytesPerSync()).isEqualTo(longValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void rateLimiterConfig() {
DBOptions options = null;
DBOptions anotherOptions = null;
try {
options = new DBOptions();
RateLimiterConfig rateLimiterConfig =
try(final DBOptions options = new DBOptions();
final DBOptions anotherOptions = new DBOptions()) {
final RateLimiterConfig rateLimiterConfig =
new GenericRateLimiterConfig(1000, 100 * 1000, 1);
options.setRateLimiterConfig(rateLimiterConfig);
// Test with parameter initialization
anotherOptions = new DBOptions();
anotherOptions.setRateLimiterConfig(
new GenericRateLimiterConfig(1000));
} finally {
if (options != null) {
options.dispose();
}
if (anotherOptions != null) {
anotherOptions.dispose();
}
}
}
@Test
public void statistics() {
DBOptions options = new DBOptions();
try(final DBOptions options = new DBOptions()) {
Statistics statistics = options.createStatistics().
statisticsPtr();
assertThat(statistics).isNotNull();
DBOptions anotherOptions = new DBOptions();
try(final DBOptions anotherOptions = new DBOptions()) {
statistics = anotherOptions.statisticsPtr();
assertThat(statistics).isNotNull();
}
}
}
}

@ -18,11 +18,8 @@ public class DirectSliceTest {
@Test
public void directSlice() {
DirectSlice directSlice = null;
DirectSlice otherSlice = null;
try {
directSlice = new DirectSlice("abc");
otherSlice = new DirectSlice("abc");
try(final DirectSlice directSlice = new DirectSlice("abc");
final DirectSlice otherSlice = new DirectSlice("abc")) {
assertThat(directSlice.toString()).isEqualTo("abc");
// clear first slice
directSlice.clear();
@ -32,75 +29,46 @@ public class DirectSliceTest {
// remove prefix
otherSlice.removePrefix(1);
assertThat(otherSlice.toString()).isEqualTo("bc");
} finally {
if (directSlice != null) {
directSlice.dispose();
}
if (otherSlice != null) {
otherSlice.dispose();
}
}
}
@Test
public void directSliceWithByteBuffer() {
DirectSlice directSlice = null;
try {
byte[] data = "Some text".getBytes();
ByteBuffer buffer = ByteBuffer.allocateDirect(data.length + 1);
final byte[] data = "Some text".getBytes();
final ByteBuffer buffer = ByteBuffer.allocateDirect(data.length + 1);
buffer.put(data);
buffer.put(data.length, (byte)0);
directSlice = new DirectSlice(buffer);
try(final DirectSlice directSlice = new DirectSlice(buffer)) {
assertThat(directSlice.toString()).isEqualTo("Some text");
} finally {
if (directSlice != null) {
directSlice.dispose();
}
}
}
@Test
public void directSliceWithByteBufferAndLength() {
DirectSlice directSlice = null;
try {
byte[] data = "Some text".getBytes();
ByteBuffer buffer = ByteBuffer.allocateDirect(data.length);
final byte[] data = "Some text".getBytes();
final ByteBuffer buffer = ByteBuffer.allocateDirect(data.length);
buffer.put(data);
directSlice = new DirectSlice(buffer, 4);
try(final DirectSlice directSlice = new DirectSlice(buffer, 4)) {
assertThat(directSlice.toString()).isEqualTo("Some");
} finally {
if (directSlice != null) {
directSlice.dispose();
}
}
}
@Test(expected = AssertionError.class)
public void directSliceInitWithoutDirectAllocation() {
DirectSlice directSlice = null;
try {
byte[] data = "Some text".getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
directSlice = new DirectSlice(buffer);
} finally {
if (directSlice != null) {
directSlice.dispose();
}
final byte[] data = "Some text".getBytes();
final ByteBuffer buffer = ByteBuffer.wrap(data);
try(final DirectSlice directSlice = new DirectSlice(buffer)) {
//no-op
}
}
@Test(expected = AssertionError.class)
public void directSlicePrefixInitWithoutDirectAllocation() {
DirectSlice directSlice = null;
try {
byte[] data = "Some text".getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
directSlice = new DirectSlice(buffer, 4);
} finally {
if (directSlice != null) {
directSlice.dispose();
}
final byte[] data = "Some text".getBytes();
final ByteBuffer buffer = ByteBuffer.wrap(data);
try(final DirectSlice directSlice = new DirectSlice(buffer, 4)) {
//no-op
}
}
}

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

@ -22,44 +22,28 @@ public class FlushTest {
@Test
public void flush() throws RocksDBException {
RocksDB db = null;
Options options = null;
WriteOptions wOpt = null;
FlushOptions flushOptions = null;
try {
options = new Options();
// Setup options
options.setCreateIfMissing(true);
options.setMaxWriteBufferNumber(10);
options.setMinWriteBufferNumberToMerge(10);
wOpt = new WriteOptions();
flushOptions = new FlushOptions();
flushOptions.setWaitForFlush(true);
try(final Options options = new Options()
.setCreateIfMissing(true)
.setMaxWriteBufferNumber(10)
.setMinWriteBufferNumberToMerge(10);
final WriteOptions wOpt = new WriteOptions()
.setDisableWAL(true);
final FlushOptions flushOptions = new FlushOptions()
.setWaitForFlush(true)) {
assertThat(flushOptions.waitForFlush()).isTrue();
wOpt.setDisableWAL(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try(final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put(wOpt, "key1".getBytes(), "value1".getBytes());
db.put(wOpt, "key2".getBytes(), "value2".getBytes());
db.put(wOpt, "key3".getBytes(), "value3".getBytes());
db.put(wOpt, "key4".getBytes(), "value4".getBytes());
assertThat(db.getProperty("rocksdb.num-entries-active-mem-table")).isEqualTo("4");
assertThat(db.getProperty("rocksdb.num-entries-active-mem-table"))
.isEqualTo("4");
db.flush(flushOptions);
assertThat(db.getProperty("rocksdb.num-entries-active-mem-table")).
isEqualTo("0");
} finally {
if (flushOptions != null) {
flushOptions.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
assertThat(db.getProperty("rocksdb.num-entries-active-mem-table"))
.isEqualTo("0");
}
if (wOpt != null) {
wOpt.dispose();
}
}
}
}

@ -24,75 +24,46 @@ public class InfoLogLevelTest {
@Test
public void testInfoLogLevel() throws RocksDBException,
IOException {
RocksDB db = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db =
RocksDB.open(dbFolder.getRoot().getAbsolutePath())) {
db.put("key".getBytes(), "value".getBytes());
assertThat(getLogContentsWithoutHeader()).isNotEmpty();
} finally {
if (db != null) {
db.close();
}
}
}
@Test
public void testFatalLogLevel() throws RocksDBException,
IOException {
RocksDB db = null;
Options options = null;
try {
options = new Options().
try (final Options options = new Options().
setCreateIfMissing(true).
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
assertThat(options.infoLogLevel()).
isEqualTo(InfoLogLevel.FATAL_LEVEL);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.put("key".getBytes(), "value".getBytes());
// As InfoLogLevel is set to FATAL_LEVEL, here we expect the log
// content to be empty.
assertThat(getLogContentsWithoutHeader()).isEmpty();
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void testFatalLogLevelWithDBOptions()
throws RocksDBException, IOException {
RocksDB db = null;
Options options = null;
DBOptions dbOptions = null;
try {
dbOptions = new DBOptions().
try (final DBOptions dbOptions = new DBOptions().
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
options = new Options(dbOptions,
final Options options = new Options(dbOptions,
new ColumnFamilyOptions()).
setCreateIfMissing(true);
final RocksDB db =
RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
assertThat(dbOptions.infoLogLevel()).
isEqualTo(InfoLogLevel.FATAL_LEVEL);
assertThat(options.infoLogLevel()).
isEqualTo(InfoLogLevel.FATAL_LEVEL);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.put("key".getBytes(), "value".getBytes());
assertThat(getLogContentsWithoutHeader()).isEmpty();
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (dbOptions != null) {
dbOptions.dispose();
}
}
}
@ -114,7 +85,8 @@ public class InfoLogLevelTest {
* @throws IOException if file is not found.
*/
private String getLogContentsWithoutHeader() throws IOException {
final String separator = Environment.isWindows() ? "\n" : System.getProperty("line.separator");
final String separator = Environment.isWindows() ?
"\n" : System.getProperty("line.separator");
final String[] lines = new String(readAllBytes(get(
dbFolder.getRoot().getAbsolutePath() + "/LOG"))).split(separator);

@ -10,6 +10,7 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ -25,23 +26,19 @@ public class KeyMayExistTest {
@Test
public void keyMayExist() throws RocksDBException {
RocksDB db = null;
DBOptions options = null;
List<ColumnFamilyDescriptor> cfDescriptors =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
try {
options = new DBOptions();
options.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
// open database using cf names
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes())
);
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
db = RocksDB.open(options,
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions options = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList);
cfDescriptors, columnFamilyHandleList)) {
try {
assertThat(columnFamilyHandleList.size()).
isEqualTo(2);
db.put("key".getBytes(), "value".getBytes());
@ -49,46 +46,41 @@ public class KeyMayExistTest {
StringBuffer retValue = new StringBuffer();
boolean exists = db.keyMayExist("key".getBytes(), retValue);
assertThat(exists).isTrue();
assertThat(retValue.toString()).
isEqualTo("value");
assertThat(retValue.toString()).isEqualTo("value");
// Test without column family but with readOptions
try (final ReadOptions readOptions = new ReadOptions()) {
retValue = new StringBuffer();
exists = db.keyMayExist(new ReadOptions(), "key".getBytes(),
retValue);
exists = db.keyMayExist(readOptions, "key".getBytes(), retValue);
assertThat(exists).isTrue();
assertThat(retValue.toString()).
isEqualTo("value");
assertThat(retValue.toString()).isEqualTo("value");
}
// Test with column family
retValue = new StringBuffer();
exists = db.keyMayExist(columnFamilyHandleList.get(0), "key".getBytes(),
retValue);
assertThat(exists).isTrue();
assertThat(retValue.toString()).
isEqualTo("value");
assertThat(retValue.toString()).isEqualTo("value");
// Test with column family and readOptions
try (final ReadOptions readOptions = new ReadOptions()) {
retValue = new StringBuffer();
exists = db.keyMayExist(new ReadOptions(),
exists = db.keyMayExist(readOptions,
columnFamilyHandleList.get(0), "key".getBytes(),
retValue);
assertThat(exists).isTrue();
assertThat(retValue.toString()).
isEqualTo("value");
assertThat(retValue.toString()).isEqualTo("value");
}
// KeyMayExist in CF1 must return false
assertThat(db.keyMayExist(columnFamilyHandleList.get(1),
"key".getBytes(), retValue)).isFalse();
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
}
}

@ -6,6 +6,7 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@ -19,183 +20,151 @@ public class LoggerTest {
@Rule
public TemporaryFolder dbFolder = new TemporaryFolder();
private AtomicInteger logMessageCounter = new AtomicInteger();
@Test
public void customLogger() throws RocksDBException {
RocksDB db = null;
logMessageCounter.set(0);
try {
// Setup options
final Options options = new Options().
final AtomicInteger logMessageCounter = new AtomicInteger();
try (final Options options = new Options().
setInfoLogLevel(InfoLogLevel.DEBUG_LEVEL).
setCreateIfMissing(true);
final Logger logger = new Logger(options) {
// Create new logger with max log level passed by options
Logger logger = new Logger(options) {
@Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
assertThat(logMsg).isNotNull();
assertThat(logMsg.length()).isGreaterThan(0);
logMessageCounter.incrementAndGet();
}
};
}
) {
// Set custom logger to options
options.setLogger(logger);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
// there should be more than zero received log messages in
// debug level.
assertThat(logMessageCounter.get()).isGreaterThan(0);
} finally {
if (db != null) {
db.close();
}
}
logMessageCounter.set(0);
}
@Test
public void fatalLogger() throws RocksDBException {
RocksDB db = null;
logMessageCounter.set(0);
try {
// Setup options
final Options options = new Options().
final AtomicInteger logMessageCounter = new AtomicInteger();
try (final Options options = new Options().
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL).
setCreateIfMissing(true);
final Logger logger = new Logger(options) {
// Create new logger with max log level passed by options
Logger logger = new Logger(options) {
@Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
assertThat(logMsg).isNotNull();
assertThat(logMsg.length()).isGreaterThan(0);
logMessageCounter.incrementAndGet();
}
};
}
) {
// Set custom logger to options
options.setLogger(logger);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
// there should be zero messages
// using fatal level as log level.
assertThat(logMessageCounter.get()).isEqualTo(0);
} finally {
if (db != null) {
db.close();
}
}
logMessageCounter.set(0);
}
@Test
public void dbOptionsLogger() throws RocksDBException {
RocksDB db = null;
Logger logger = null;
List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
logMessageCounter.set(0);
try {
// Setup options
final DBOptions options = new DBOptions().
final AtomicInteger logMessageCounter = new AtomicInteger();
try (final DBOptions options = new DBOptions().
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL).
setCreateIfMissing(true);
final Logger logger = new Logger(options) {
// Create new logger with max log level passed by options
logger = new Logger(options) {
@Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
assertThat(logMsg).isNotNull();
assertThat(logMsg.length()).isGreaterThan(0);
logMessageCounter.incrementAndGet();
}
};
}
) {
// Set custom logger to options
options.setLogger(logger);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, cfHandles);
final List<ColumnFamilyDescriptor> cfDescriptors =
Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
final List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, cfHandles)) {
try {
// there should be zero messages
// using fatal level as log level.
assertThat(logMessageCounter.get()).isEqualTo(0);
logMessageCounter.set(0);
} finally {
for (ColumnFamilyHandle columnFamilyHandle : cfHandles) {
columnFamilyHandle.dispose();
for (final ColumnFamilyHandle columnFamilyHandle : cfHandles) {
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
if (logger != null) {
logger.dispose();
}
}
}
@Test
public void setInfoLogLevel() {
Logger logger = null;
try {
// Setup options
final Options options = new Options().
final AtomicInteger logMessageCounter = new AtomicInteger();
try (final Options options = new Options().
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL).
setCreateIfMissing(true);
final Logger logger = new Logger(options) {
// Create new logger with max log level passed by options
logger = new Logger(options) {
@Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
assertThat(logMsg).isNotNull();
assertThat(logMsg.length()).isGreaterThan(0);
logMessageCounter.incrementAndGet();
}
};
}
) {
assertThat(logger.infoLogLevel()).
isEqualTo(InfoLogLevel.FATAL_LEVEL);
logger.setInfoLogLevel(InfoLogLevel.DEBUG_LEVEL);
assertThat(logger.infoLogLevel()).
isEqualTo(InfoLogLevel.DEBUG_LEVEL);
} finally {
if (logger != null) {
logger.dispose();
}
}
}
@Test
public void changeLogLevelAtRuntime() throws RocksDBException {
RocksDB db = null;
logMessageCounter.set(0);
try {
// Setup options
final Options options = new Options().
final AtomicInteger logMessageCounter = new AtomicInteger();
try (final Options options = new Options().
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL).
setCreateIfMissing(true);
// Create new logger with max log level passed by options
Logger logger = new Logger(options) {
final Logger logger = new Logger(options) {
@Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
assertThat(logMsg).isNotNull();
assertThat(logMsg.length()).isGreaterThan(0);
logMessageCounter.incrementAndGet();
}
};
}
) {
// Set custom logger to options
options.setLogger(logger);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
// there should be zero messages
// using fatal level as log level.
@ -209,12 +178,7 @@ public class LoggerTest {
// messages shall be received due to previous actions.
assertThat(logMessageCounter.get()).isNotEqualTo(0);
} finally {
if (db != null) {
db.close();
}
}
logMessageCounter.set(0);
}
}

@ -18,9 +18,7 @@ public class MemTableTest {
@Test
public void hashSkipListMemTable() throws RocksDBException {
Options options = null;
try {
options = new Options();
try(final Options options = new Options()) {
// Test HashSkipListMemTableConfig
HashSkipListMemTableConfig memTableConfig =
new HashSkipListMemTableConfig();
@ -40,18 +38,12 @@ public class MemTableTest {
assertThat(memTableConfig.branchingFactor()).
isEqualTo(6);
options.setMemTableConfig(memTableConfig);
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void skipListMemTable() throws RocksDBException {
Options options = null;
try {
options = new Options();
try(final Options options = new Options()) {
SkipListMemTableConfig skipMemTableConfig =
new SkipListMemTableConfig();
assertThat(skipMemTableConfig.lookahead()).
@ -60,19 +52,12 @@ public class MemTableTest {
assertThat(skipMemTableConfig.lookahead()).
isEqualTo(20);
options.setMemTableConfig(skipMemTableConfig);
options.dispose();
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void hashLinkedListMemTable() throws RocksDBException {
Options options = null;
try {
options = new Options();
try(final Options options = new Options()) {
HashLinkedListMemTableConfig hashLinkedListMemTableConfig =
new HashLinkedListMemTableConfig();
assertThat(hashLinkedListMemTableConfig.bucketCount()).
@ -107,18 +92,12 @@ public class MemTableTest {
thresholdUseSkiplist()).
isEqualTo(29);
options.setMemTableConfig(hashLinkedListMemTableConfig);
} finally {
if (options != null) {
options.dispose();
}
}
}
@Test
public void vectorMemTable() throws RocksDBException {
Options options = null;
try {
options = new Options();
try(final Options options = new Options()) {
VectorMemTableConfig vectorMemTableConfig =
new VectorMemTableConfig();
assertThat(vectorMemTableConfig.reservedSize()).
@ -127,11 +106,6 @@ public class MemTableTest {
assertThat(vectorMemTableConfig.reservedSize()).
isEqualTo(123);
options.setMemTableConfig(vectorMemTableConfig);
options.dispose();
} finally {
if (options != null) {
options.dispose();
}
}
}
}

@ -5,6 +5,7 @@
package org.rocksdb;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
@ -27,59 +28,44 @@ public class MergeTest {
@Test
public void stringOption()
throws InterruptedException, RocksDBException {
RocksDB db = null;
Options opt = null;
try {
String db_path_string =
dbFolder.getRoot().getAbsolutePath();
opt = new Options();
opt.setCreateIfMissing(true);
opt.setMergeOperatorName("stringappend");
db = RocksDB.open(opt, db_path_string);
try (final Options opt = new Options()
.setCreateIfMissing(true)
.setMergeOperatorName("stringappend");
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// writing aa under key
db.put("key".getBytes(), "aa".getBytes());
// merge bb under key
db.merge("key".getBytes(), "bb".getBytes());
byte[] value = db.get("key".getBytes());
String strValue = new String(value);
final byte[] value = db.get("key".getBytes());
final String strValue = new String(value);
assertThat(strValue).isEqualTo("aa,bb");
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void cFStringOption()
throws InterruptedException, RocksDBException {
RocksDB db = null;
DBOptions opt = null;
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
try {
String db_path_string =
dbFolder.getRoot().getAbsolutePath();
opt = new DBOptions();
opt.setCreateIfMissing(true);
opt.setCreateMissingColumnFamilies(true);
List<ColumnFamilyDescriptor> cfDescriptors =
new ArrayList<>();
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions().setMergeOperatorName(
"stringappend")));
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions().setMergeOperatorName(
"stringappend")));
db = RocksDB.open(opt, db_path_string,
cfDescriptors, columnFamilyHandleList);
try (final ColumnFamilyOptions cfOpt1 = new ColumnFamilyOptions()
.setMergeOperatorName("stringappend");
final ColumnFamilyOptions cfOpt2 = new ColumnFamilyOptions()
.setMergeOperatorName("stringappend")
) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt1),
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt2)
);
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions opt = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
columnFamilyHandleList)) {
try {
// writing aa under key
db.put(columnFamilyHandleList.get(1),
"cfkey".getBytes(), "aa".getBytes());
@ -87,18 +73,15 @@ public class MergeTest {
db.merge(columnFamilyHandleList.get(1),
"cfkey".getBytes(), "bb".getBytes());
byte[] value = db.get(columnFamilyHandleList.get(1), "cfkey".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();
for (final ColumnFamilyHandle handle : columnFamilyHandleList) {
handle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -106,99 +89,85 @@ public class MergeTest {
@Test
public void operatorOption()
throws InterruptedException, RocksDBException {
RocksDB db = null;
Options opt = null;
try {
String db_path_string =
dbFolder.getRoot().getAbsolutePath();
opt = new Options();
opt.setCreateIfMissing(true);
StringAppendOperator stringAppendOperator = new StringAppendOperator();
opt.setMergeOperator(stringAppendOperator);
db = RocksDB.open(opt, db_path_string);
final StringAppendOperator stringAppendOperator =
new StringAppendOperator();
try (final Options opt = new Options()
.setCreateIfMissing(true)
.setMergeOperator(stringAppendOperator);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// Writing aa under key
db.put("key".getBytes(), "aa".getBytes());
// Writing bb under key
db.merge("key".getBytes(), "bb".getBytes());
byte[] value = db.get("key".getBytes());
String strValue = new String(value);
final byte[] value = db.get("key".getBytes());
final String strValue = new String(value);
assertThat(strValue).isEqualTo("aa,bb");
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void cFOperatorOption()
throws InterruptedException, RocksDBException {
RocksDB db = null;
DBOptions opt = null;
ColumnFamilyHandle cfHandle = null;
List<ColumnFamilyDescriptor> cfDescriptors =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
final StringAppendOperator stringAppendOperator =
new StringAppendOperator();
try (final ColumnFamilyOptions cfOpt1 = new ColumnFamilyOptions()
.setMergeOperator(stringAppendOperator);
final ColumnFamilyOptions cfOpt2 = new ColumnFamilyOptions()
.setMergeOperator(stringAppendOperator)
) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt1),
new ColumnFamilyDescriptor("new_cf".getBytes(), cfOpt2)
);
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions opt = new DBOptions()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
columnFamilyHandleList)
) {
try {
String db_path_string =
dbFolder.getRoot().getAbsolutePath();
opt = new DBOptions();
opt.setCreateIfMissing(true);
opt.setCreateMissingColumnFamilies(true);
StringAppendOperator stringAppendOperator = new StringAppendOperator();
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions().setMergeOperator(
stringAppendOperator)));
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf".getBytes(),
new ColumnFamilyOptions().setMergeOperator(
stringAppendOperator)));
db = RocksDB.open(opt, db_path_string,
cfDescriptors, columnFamilyHandleList);
// writing aa under key
db.put(columnFamilyHandleList.get(1),
"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());
byte[] value = db.get(columnFamilyHandleList.get(1),
"cfkey".getBytes());
String strValue = new String(value);
// Test also with createColumnFamily
cfHandle = db.createColumnFamily(
try (final ColumnFamilyOptions cfHandleOpts =
new ColumnFamilyOptions()
.setMergeOperator(stringAppendOperator);
final ColumnFamilyHandle cfHandle =
db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf2".getBytes(),
new ColumnFamilyOptions().setMergeOperator(stringAppendOperator)));
cfHandleOpts))
) {
// writing xx under cfkey2
db.put(cfHandle, "cfkey2".getBytes(), "xx".getBytes());
// merge yy under cfkey2
db.merge(cfHandle, new WriteOptions(), "cfkey2".getBytes(), "yy".getBytes());
db.merge(cfHandle, new WriteOptions(), "cfkey2".getBytes(),
"yy".getBytes());
value = db.get(cfHandle, "cfkey2".getBytes());
String strValueTmpCf = new String(value);
assertThat(strValue).isEqualTo("aa,bb");
assertThat(strValueTmpCf).isEqualTo("xx,yy");
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (cfHandle != null) {
cfHandle.dispose();
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -206,97 +175,67 @@ public class MergeTest {
@Test
public void operatorGcBehaviour()
throws RocksDBException {
Options opt = null;
RocksDB db = null;
try {
String db_path_string =
dbFolder.getRoot().getAbsolutePath();
opt = new Options();
opt.setCreateIfMissing(true);
StringAppendOperator stringAppendOperator = new StringAppendOperator();
opt.setMergeOperator(stringAppendOperator);
db = RocksDB.open(opt, db_path_string);
db.close();
opt.dispose();
System.gc();
System.runFinalization();
final StringAppendOperator stringAppendOperator
= new StringAppendOperator();
try (final Options opt = new Options()
.setCreateIfMissing(true)
.setMergeOperator(stringAppendOperator);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
// test reuse
opt = new Options();
opt.setMergeOperator(stringAppendOperator);
db = RocksDB.open(opt, db_path_string);
db.close();
opt.dispose();
System.gc();
System.runFinalization();
try (final Options opt = new Options()
.setMergeOperator(stringAppendOperator);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
// test param init
opt = new Options();
opt.setMergeOperator(new StringAppendOperator());
db = RocksDB.open(opt, db_path_string);
db.close();
opt.dispose();
System.gc();
System.runFinalization();
try (final Options opt = new Options()
.setMergeOperator(new StringAppendOperator());
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
// test replace one with another merge operator instance
opt = new Options();
opt.setMergeOperator(stringAppendOperator);
StringAppendOperator newStringAppendOperator = new StringAppendOperator();
try (final Options opt = new Options()
.setMergeOperator(stringAppendOperator)) {
final StringAppendOperator newStringAppendOperator
= new StringAppendOperator();
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();
try (final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
}
}
@Test
public void emptyStringInSetMergeOperatorByName() {
Options opt = null;
ColumnFamilyOptions cOpt = null;
try {
opt = new Options();
cOpt = new ColumnFamilyOptions();
opt.setMergeOperatorName("");
cOpt.setMergeOperatorName("");
} finally {
if (opt != null) {
opt.dispose();
}
if (cOpt != null) {
cOpt.dispose();
}
try (final Options opt = new Options()
.setMergeOperatorName("");
final ColumnFamilyOptions cOpt = new ColumnFamilyOptions()
.setMergeOperatorName("")) {
//no-op
}
}
@Test(expected = IllegalArgumentException.class)
public void nullStringInSetMergeOperatorByNameOptions() {
Options opt = null;
try {
opt = new Options();
try (final Options opt = new Options()) {
opt.setMergeOperatorName(null);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test(expected = IllegalArgumentException.class)
public void
nullStringInSetMergeOperatorByNameColumnFamilyOptions() {
ColumnFamilyOptions opt = null;
try {
opt = new ColumnFamilyOptions();
try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
opt.setMergeOperatorName(null);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
}

@ -19,33 +19,30 @@ public class MixedOptionsTest {
@Test
public void mixedOptionsTest(){
// Set a table factory and check the names
ColumnFamilyOptions cfOptions = new ColumnFamilyOptions();
cfOptions.setTableFormatConfig(new BlockBasedTableConfig().
setFilter(new BloomFilter()));
try(final Filter bloomFilter = new BloomFilter();
final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions()
.setTableFormatConfig(
new BlockBasedTableConfig().setFilter(bloomFilter))
) {
assertThat(cfOptions.tableFactoryName()).isEqualTo(
"BlockBasedTable");
cfOptions.setTableFormatConfig(new PlainTableConfig());
assertThat(cfOptions.tableFactoryName()).isEqualTo("PlainTable");
// Initialize a dbOptions object from cf options and
// db options
DBOptions dbOptions = new DBOptions();
Options options = new Options(dbOptions, cfOptions);
try (final DBOptions dbOptions = new DBOptions();
final Options options = new Options(dbOptions, cfOptions)) {
assertThat(options.tableFactoryName()).isEqualTo("PlainTable");
// Free instances
options.dispose();
options = null;
cfOptions.dispose();
cfOptions = null;
dbOptions.dispose();
dbOptions = null;
System.gc();
System.runFinalization();
}
}
// Test Optimize for statements
cfOptions = new ColumnFamilyOptions();
try(final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions()) {
cfOptions.optimizeUniversalStyleCompaction();
cfOptions.optimizeLevelStyleCompaction();
cfOptions.optimizeForPointLookup(1024);
options = new Options();
try(final Options options = new Options()) {
options.optimizeLevelStyleCompaction();
options.optimizeLevelStyleCompaction(400);
options.optimizeUniversalStyleCompaction();
@ -54,3 +51,5 @@ public class MixedOptionsTest {
options.prepareForBulkLoad();
}
}
}
}

@ -23,7 +23,7 @@ public class NativeLibraryLoaderTest {
public void tempFolder() throws IOException {
NativeLibraryLoader.getInstance().loadLibraryFromJarToTemp(
temporaryFolder.getRoot().getAbsolutePath());
Path path = Paths.get(temporaryFolder.getRoot().getAbsolutePath(),
final Path path = Paths.get(temporaryFolder.getRoot().getAbsolutePath(),
Environment.getJniLibraryFileName("rocksdb"));
assertThat(Files.exists(path)).isTrue();
assertThat(Files.isReadable(path)).isTrue();

File diff suppressed because it is too large Load Diff

@ -80,16 +80,10 @@ public class PlainTableConfigTest {
@Test
public void plainTableConfig() {
Options opt = null;
try {
opt = new Options();
PlainTableConfig plainTableConfig = new PlainTableConfig();
try(final Options opt = new Options()) {
final PlainTableConfig plainTableConfig = new PlainTableConfig();
opt.setTableFormatConfig(plainTableConfig);
assertThat(opt.tableFactoryName()).isEqualTo("PlainTable");
} finally {
if (opt != null) {
opt.dispose();
}
}
}
}

@ -18,7 +18,7 @@ public class PlatformRandomHelper {
* @return boolean value indicating if operating system is 64 Bit.
*/
public static boolean isOs64Bit(){
boolean is64Bit;
final boolean is64Bit;
if (System.getProperty("os.name").contains("Windows")) {
is64Bit = (System.getenv("ProgramFiles(x86)") != null);
} else {

@ -10,6 +10,7 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ -25,340 +26,279 @@ public class ReadOnlyTest {
@Test
public void readOnlyOpen() throws RocksDBException {
RocksDB db = null;
RocksDB db2 = null;
RocksDB db3 = null;
Options options = null;
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList2 =
new ArrayList<>();
try {
options = new Options();
options.setCreateIfMissing(true);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options()
.setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put("key".getBytes(), "value".getBytes());
db2 = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db2 = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath())) {
assertThat("value").
isEqualTo(new String(db2.get("key".getBytes())));
db.close();
db2.close();
}
}
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add(new ColumnFamilyDescriptor(
RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts));
db = RocksDB.open(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList);
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, columnFamilyHandleList)) {
try (final ColumnFamilyOptions newCfOpts = new ColumnFamilyOptions();
final ColumnFamilyOptions newCf2Opts = new ColumnFamilyOptions()
) {
columnFamilyHandleList.add(db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf".getBytes(), new ColumnFamilyOptions())));
new ColumnFamilyDescriptor("new_cf".getBytes(), newCfOpts)));
columnFamilyHandleList.add(db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf2".getBytes(), new ColumnFamilyOptions())));
new ColumnFamilyDescriptor("new_cf2".getBytes(), newCf2Opts)));
db.put(columnFamilyHandleList.get(2), "key2".getBytes(),
"value2".getBytes());
db2 = RocksDB.openReadOnly(
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try (final RocksDB db2 = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
readOnlyColumnFamilyHandleList)) {
try (final ColumnFamilyOptions newCfOpts2 =
new ColumnFamilyOptions();
final ColumnFamilyOptions newCf2Opts2 =
new ColumnFamilyOptions()
) {
assertThat(db2.get("key2".getBytes())).isNull();
assertThat(db2.get(readOnlyColumnFamilyHandleList.get(0), "key2".getBytes())).
assertThat(db2.get(readOnlyColumnFamilyHandleList.get(0),
"key2".getBytes())).
isNull();
cfDescriptors.clear();
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
cfDescriptors.add(
new ColumnFamilyDescriptor("new_cf2".getBytes(), new ColumnFamilyOptions()));
db3 = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, readOnlyColumnFamilyHandleList2);
assertThat(new String(db3.get(readOnlyColumnFamilyHandleList2.get(1),
newCfOpts2));
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf2".getBytes(),
newCf2Opts2));
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList2
= new ArrayList<>();
try (final RocksDB db3 = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList2)) {
try {
assertThat(new String(db3.get(
readOnlyColumnFamilyHandleList2.get(1),
"key2".getBytes()))).isEqualTo("value2");
} finally {
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
columnFamilyHandle.dispose();
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList2) {
columnFamilyHandle.close();
}
if (db != null) {
db.close();
}
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db2 != null) {
db2.close();
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList2) {
columnFamilyHandle.dispose();
}
if (db3 != null) {
db3.close();
}
if (options != null) {
options.dispose();
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
}
}
}
}
@Test(expected = RocksDBException.class)
public void failToWriteInReadOnly() throws RocksDBException {
RocksDB db = null;
RocksDB rDb = null;
Options options = null;
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try {
try (final Options options = new Options()
.setCreateIfMissing(true)) {
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
}
options = new Options();
options.setCreateIfMissing(true);
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)
);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.close();
rDb = RocksDB.openReadOnly(
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
readOnlyColumnFamilyHandleList)) {
try {
// test that put fails in readonly mode
rDb.put("key".getBytes(), "value".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failToCFWriteInReadOnly() throws RocksDBException {
RocksDB db = null;
RocksDB rDb = null;
Options options = null;
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try {
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
options = new Options();
options.setCreateIfMissing(true);
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.close();
rDb = RocksDB.openReadOnly(
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)
);
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
readOnlyColumnFamilyHandleList)) {
try {
rDb.put(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes(), "value".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failToRemoveInReadOnly() throws RocksDBException {
RocksDB db = null;
RocksDB rDb = null;
Options options = null;
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try {
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
options = new Options();
options.setCreateIfMissing(true);
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)
);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.close();
rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList)) {
try {
rDb.remove("key".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failToCFRemoveInReadOnly() throws RocksDBException {
RocksDB db = null;
RocksDB rDb = null;
Options options = null;
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try {
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
options = new Options();
options.setCreateIfMissing(true);
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.close();
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)
);
rDb = RocksDB.openReadOnly(
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
readOnlyColumnFamilyHandleList)) {
try {
rDb.remove(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes());
} finally {
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failToWriteBatchReadOnly() throws RocksDBException {
RocksDB db = null;
RocksDB rDb = null;
Options options = null;
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try {
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
options = new Options();
options.setCreateIfMissing(true);
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.close();
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)
);
rDb = RocksDB.openReadOnly(
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
WriteBatch wb = new WriteBatch();
final WriteBatch wb = new WriteBatch();
final WriteOptions wOpts = new WriteOptions()) {
try {
wb.put("key".getBytes(), "value".getBytes());
rDb.write(new WriteOptions(), wb);
rDb.write(wOpts, wb);
} finally {
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
if (rDb != null) {
rDb.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void failToCFWriteBatchReadOnly() throws RocksDBException {
RocksDB db = null;
RocksDB rDb = null;
Options options = null;
WriteBatch wb = null;
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try {
cfDescriptors.add(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
new ColumnFamilyOptions()));
options = new Options();
options.setCreateIfMissing(true);
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
}
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.close();
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)
);
rDb = RocksDB.openReadOnly(
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList =
new ArrayList<>();
try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList);
wb = new WriteBatch();
wb.put(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes(), "value".getBytes());
rDb.write(new WriteOptions(), wb);
final WriteBatch wb = new WriteBatch();
final WriteOptions wOpts = new WriteOptions()) {
try {
wb.put(readOnlyColumnFamilyHandleList.get(0), "key".getBytes(),
"value".getBytes());
rDb.write(wOpts, wb);
} finally {
for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (db != null) {
db.close();
}
if (rDb != null) {
rDb.close();
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
if (options != null) {
options.dispose();
}
if (wb != null) {
wb.dispose();
}
}
}

@ -25,126 +25,110 @@ public class ReadOptionsTest {
@Test
public void verifyChecksum() {
ReadOptions opt = null;
try {
opt = new ReadOptions();
Random rand = new Random();
boolean boolValue = rand.nextBoolean();
try (final ReadOptions opt = new ReadOptions()) {
final Random rand = new Random();
final boolean boolValue = rand.nextBoolean();
opt.setVerifyChecksums(boolValue);
assertThat(opt.verifyChecksums()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void fillCache() {
ReadOptions opt = null;
try {
opt = new ReadOptions();
Random rand = new Random();
boolean boolValue = rand.nextBoolean();
try (final ReadOptions opt = new ReadOptions()) {
final Random rand = new Random();
final boolean boolValue = rand.nextBoolean();
opt.setFillCache(boolValue);
assertThat(opt.fillCache()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void tailing() {
ReadOptions opt = null;
try {
opt = new ReadOptions();
Random rand = new Random();
boolean boolValue = rand.nextBoolean();
try (final ReadOptions opt = new ReadOptions()) {
final Random rand = new Random();
final boolean boolValue = rand.nextBoolean();
opt.setTailing(boolValue);
assertThat(opt.tailing()).isEqualTo(boolValue);
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void snapshot() {
ReadOptions opt = null;
try {
opt = new ReadOptions();
try (final ReadOptions opt = new ReadOptions()) {
opt.setSnapshot(null);
assertThat(opt.snapshot()).isNull();
} finally {
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void failSetVerifyChecksumUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.setVerifyChecksums(true);
}
}
@Test
public void failVerifyChecksumUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.verifyChecksums();
}
}
@Test
public void failSetFillCacheUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.setFillCache(true);
}
}
@Test
public void failFillCacheUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.fillCache();
}
}
@Test
public void failSetTailingUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.setTailing(true);
}
}
@Test
public void failTailingUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.tailing();
}
}
@Test
public void failSetSnapshotUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.setSnapshot(null);
}
}
@Test
public void failSnapshotUninitialized() {
ReadOptions readOptions = setupUninitializedReadOptions(
exception);
try (final ReadOptions readOptions =
setupUninitializedReadOptions(exception)) {
readOptions.snapshot();
}
}
private ReadOptions setupUninitializedReadOptions(
ExpectedException exception) {
ReadOptions readOptions = new ReadOptions();
readOptions.dispose();
final ReadOptions readOptions = new ReadOptions();
readOptions.close();
exception.expect(AssertionError.class);
return readOptions;
}

@ -27,96 +27,65 @@ public class RocksDBTest {
@Test
public void open() throws RocksDBException {
RocksDB db = null;
Options opt = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
db.close();
opt = new Options();
opt.setCreateIfMissing(true);
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath());
} finally {
if (db != null) {
db.close();
try (final RocksDB db =
RocksDB.open(dbFolder.getRoot().getAbsolutePath())) {
assertThat(db).isNotNull();
}
if (opt != null) {
opt.dispose();
}
@Test
public void open_opt() throws RocksDBException {
try (final Options opt = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
assertThat(db).isNotNull();
}
}
@Test
public void put() throws RocksDBException {
RocksDB db = null;
WriteOptions opt = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
final WriteOptions opt = new WriteOptions()) {
db.put("key1".getBytes(), "value".getBytes());
opt = new WriteOptions();
db.put(opt, "key2".getBytes(), "12345678".getBytes());
assertThat(db.get("key1".getBytes())).isEqualTo(
"value".getBytes());
assertThat(db.get("key2".getBytes())).isEqualTo(
"12345678".getBytes());
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void write() throws RocksDBException {
RocksDB db = null;
Options options = null;
WriteBatch wb1 = null;
WriteBatch wb2 = null;
WriteOptions opts = null;
try {
options = new Options().
setMergeOperator(new StringAppendOperator()).
setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
opts = new WriteOptions();
wb1 = new WriteBatch();
try (final Options options = new Options().setMergeOperator(
new StringAppendOperator()).setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
final WriteOptions opts = new WriteOptions()) {
try (final WriteBatch wb1 = new WriteBatch()) {
wb1.put("key1".getBytes(), "aa".getBytes());
wb1.merge("key1".getBytes(), "bb".getBytes());
wb2 = new WriteBatch();
try (final WriteBatch wb2 = new WriteBatch()) {
wb2.put("key2".getBytes(), "xx".getBytes());
wb2.merge("key2".getBytes(), "yy".getBytes());
db.write(opts, wb1);
db.write(opts, wb2);
}
}
assertThat(db.get("key1".getBytes())).isEqualTo(
"aa,bb".getBytes());
assertThat(db.get("key2".getBytes())).isEqualTo(
"xx,yy".getBytes());
} finally {
if (db != null) {
db.close();
}
if (wb1 != null) {
wb1.dispose();
}
if (wb2 != null) {
wb2.dispose();
}
if (options != null) {
options.dispose();
}
if (opts != null) {
opts.dispose();
}
}
}
@Test
public void getWithOutValue() throws RocksDBException {
RocksDB db = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db =
RocksDB.open(dbFolder.getRoot().getAbsolutePath())) {
db.put("key1".getBytes(), "value".getBytes());
db.put("key2".getBytes(), "12345678".getBytes());
byte[] outValue = new byte[5];
@ -131,20 +100,13 @@ public class RocksDBTest {
getResult = db.get("key2".getBytes(), outValue);
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND);
assertThat(outValue).isEqualTo("12345".getBytes());
} finally {
if (db != null) {
db.close();
}
}
}
@Test
public void getWithOutValueReadOptions() throws RocksDBException {
RocksDB db = null;
ReadOptions rOpt = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
rOpt = new ReadOptions();
try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
final ReadOptions rOpt = new ReadOptions()) {
db.put("key1".getBytes(), "value".getBytes());
db.put("key2".getBytes(), "12345678".getBytes());
byte[] outValue = new byte[5];
@ -160,23 +122,13 @@ public class RocksDBTest {
getResult = db.get(rOpt, "key2".getBytes(), outValue);
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND);
assertThat(outValue).isEqualTo("12345".getBytes());
} finally {
if (db != null) {
db.close();
}
if (rOpt != null) {
rOpt.dispose();
}
}
}
@Test
public void multiGet() throws RocksDBException, InterruptedException {
RocksDB db = null;
ReadOptions rOpt = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
rOpt = new ReadOptions();
try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
final ReadOptions rOpt = new ReadOptions()) {
db.put("key1".getBytes(), "value".getBytes());
db.put("key2".getBytes(), "12345678".getBytes());
List<byte[]> lookupKeys = new ArrayList<>();
@ -209,27 +161,18 @@ public class RocksDBTest {
assertThat(results.values()).isNotNull();
assertThat(results.values()).
contains("value".getBytes());
} finally {
if (db != null) {
db.close();
}
if (rOpt != null) {
rOpt.dispose();
}
}
}
@Test
public void merge() throws RocksDBException {
RocksDB db = null;
Options opt = null;
WriteOptions wOpt;
try {
opt = new Options().
setCreateIfMissing(true).
setMergeOperator(new StringAppendOperator());
wOpt = new WriteOptions();
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath());
try (final Options opt = new Options()
.setCreateIfMissing(true)
.setMergeOperator(new StringAppendOperator());
final WriteOptions wOpt = new WriteOptions();
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())
) {
db.put("key1".getBytes(), "value".getBytes());
assertThat(db.get("key1".getBytes())).isEqualTo(
"value".getBytes());
@ -245,23 +188,13 @@ public class RocksDBTest {
db.merge(wOpt, "key2".getBytes(), "xxxx".getBytes());
assertThat(db.get("key2".getBytes())).isEqualTo(
"xxxx".getBytes());
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void remove() throws RocksDBException {
RocksDB db = null;
WriteOptions wOpt;
try {
wOpt = new WriteOptions();
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
final WriteOptions wOpt = new WriteOptions()) {
db.put("key1".getBytes(), "value".getBytes());
db.put("key2".getBytes(), "12345678".getBytes());
assertThat(db.get("key1".getBytes())).isEqualTo(
@ -272,52 +205,34 @@ public class RocksDBTest {
db.remove(wOpt, "key2".getBytes());
assertThat(db.get("key1".getBytes())).isNull();
assertThat(db.get("key2".getBytes())).isNull();
} finally {
if (db != null) {
db.close();
}
}
}
@Test
public void getIntProperty() throws RocksDBException {
RocksDB db = null;
Options options = null;
WriteOptions wOpt = null;
try {
options = new Options();
wOpt = new WriteOptions();
// Setup options
options.setCreateIfMissing(true);
options.setMaxWriteBufferNumber(10);
options.setMinWriteBufferNumberToMerge(10);
wOpt.setDisableWAL(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (
final Options options = new Options()
.setCreateIfMissing(true)
.setMaxWriteBufferNumber(10)
.setMinWriteBufferNumberToMerge(10);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
final WriteOptions wOpt = new WriteOptions().setDisableWAL(true)
) {
db.put(wOpt, "key1".getBytes(), "value1".getBytes());
db.put(wOpt, "key2".getBytes(), "value2".getBytes());
db.put(wOpt, "key3".getBytes(), "value3".getBytes());
db.put(wOpt, "key4".getBytes(), "value4".getBytes());
assertThat(db.getLongProperty("rocksdb.num-entries-active-mem-table")).isGreaterThan(0);
assertThat(db.getLongProperty("rocksdb.cur-size-active-mem-table")).isGreaterThan(0);
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (wOpt != null) {
wOpt.dispose();
}
assertThat(db.getLongProperty("rocksdb.num-entries-active-mem-table"))
.isGreaterThan(0);
assertThat(db.getLongProperty("rocksdb.cur-size-active-mem-table"))
.isGreaterThan(0);
}
}
@Test
public void fullCompactRange() throws RocksDBException {
RocksDB db = null;
Options opt = null;
try {
opt = new Options().
try (final Options opt = new Options().
setCreateIfMissing(true).
setDisableAutoCompactions(true).
setCompactionStyle(CompactionStyle.LEVEL).
@ -329,9 +244,8 @@ public class RocksDBTest {
setMaxBytesForLevelBase(500 << 10).
setMaxBytesForLevelMultiplier(1).
setDisableAutoCompactions(false);
// open database
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// fill database with key/value pairs
byte[] b = new byte[10000];
for (int i = 0; i < 200; i++) {
@ -339,34 +253,17 @@ public class RocksDBTest {
db.put((String.valueOf(i)).getBytes(), b);
}
db.compactRange();
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void fullCompactRangeColumnFamily()
throws RocksDBException {
RocksDB db = null;
DBOptions opt = null;
List<ColumnFamilyHandle> columnFamilyHandles =
new ArrayList<>();
try {
opt = new DBOptions().
try (
final DBOptions opt = new DBOptions().
setCreateIfMissing(true).
setCreateMissingColumnFamilies(true);
List<ColumnFamilyDescriptor> columnFamilyDescriptors =
new ArrayList<>();
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
RocksDB.DEFAULT_COLUMN_FAMILY));
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
"new_cf".getBytes(),
new ColumnFamilyOptions().
final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions().
setDisableAutoCompactions(true).
setCompactionStyle(CompactionStyle.LEVEL).
setNumLevels(4).
@ -376,12 +273,20 @@ public class RocksDBTest {
setTargetFileSizeMultiplier(1).
setMaxBytesForLevelBase(500 << 10).
setMaxBytesForLevelMultiplier(1).
setDisableAutoCompactions(false)));
setDisableAutoCompactions(false)
) {
final List<ColumnFamilyDescriptor> columnFamilyDescriptors =
Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts));
// open database
db = RocksDB.open(opt,
final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
try (final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath(),
columnFamilyDescriptors,
columnFamilyHandles);
columnFamilyHandles)) {
try {
// fill database with key/value pairs
byte[] b = new byte[10000];
for (int i = 0; i < 200; i++) {
@ -391,14 +296,10 @@ public class RocksDBTest {
}
db.compactRange(columnFamilyHandles.get(1));
} finally {
for (ColumnFamilyHandle handle : columnFamilyHandles) {
handle.dispose();
for (final ColumnFamilyHandle handle : columnFamilyHandles) {
handle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -406,10 +307,7 @@ public class RocksDBTest {
@Test
public void compactRangeWithKeys()
throws RocksDBException {
RocksDB db = null;
Options opt = null;
try {
opt = new Options().
try (final Options opt = new Options().
setCreateIfMissing(true).
setDisableAutoCompactions(true).
setCompactionStyle(CompactionStyle.LEVEL).
@ -421,9 +319,8 @@ public class RocksDBTest {
setMaxBytesForLevelBase(500 << 10).
setMaxBytesForLevelMultiplier(1).
setDisableAutoCompactions(false);
// open database
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// fill database with key/value pairs
byte[] b = new byte[10000];
for (int i = 0; i < 200; i++) {
@ -431,23 +328,14 @@ public class RocksDBTest {
db.put((String.valueOf(i)).getBytes(), b);
}
db.compactRange("0".getBytes(), "201".getBytes());
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void compactRangeWithKeysReduce()
throws RocksDBException {
RocksDB db = null;
Options opt = null;
try {
opt = new Options().
try (
final Options opt = new Options().
setCreateIfMissing(true).
setDisableAutoCompactions(true).
setCompactionStyle(CompactionStyle.LEVEL).
@ -459,9 +347,8 @@ public class RocksDBTest {
setMaxBytesForLevelBase(500 << 10).
setMaxBytesForLevelMultiplier(1).
setDisableAutoCompactions(false);
// open database
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())) {
// fill database with key/value pairs
byte[] b = new byte[10000];
for (int i = 0; i < 200; i++) {
@ -471,34 +358,16 @@ public class RocksDBTest {
db.flush(new FlushOptions().setWaitForFlush(true));
db.compactRange("0".getBytes(), "201".getBytes(),
true, -1, 0);
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void compactRangeWithKeysColumnFamily()
throws RocksDBException {
RocksDB db = null;
DBOptions opt = null;
List<ColumnFamilyHandle> columnFamilyHandles =
new ArrayList<>();
try {
opt = new DBOptions().
try (final DBOptions opt = new DBOptions().
setCreateIfMissing(true).
setCreateMissingColumnFamilies(true);
List<ColumnFamilyDescriptor> columnFamilyDescriptors =
new ArrayList<>();
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
RocksDB.DEFAULT_COLUMN_FAMILY));
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
"new_cf".getBytes(),
new ColumnFamilyOptions().
final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions().
setDisableAutoCompactions(true).
setCompactionStyle(CompactionStyle.LEVEL).
setNumLevels(4).
@ -508,12 +377,22 @@ public class RocksDBTest {
setTargetFileSizeMultiplier(1).
setMaxBytesForLevelBase(500 << 10).
setMaxBytesForLevelMultiplier(1).
setDisableAutoCompactions(false)));
setDisableAutoCompactions(false)
) {
final List<ColumnFamilyDescriptor> columnFamilyDescriptors =
Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts)
);
// open database
db = RocksDB.open(opt,
final List<ColumnFamilyHandle> columnFamilyHandles =
new ArrayList<>();
try (final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath(),
columnFamilyDescriptors,
columnFamilyHandles);
columnFamilyHandles)) {
try {
// fill database with key/value pairs
byte[] b = new byte[10000];
for (int i = 0; i < 200; i++) {
@ -524,14 +403,10 @@ public class RocksDBTest {
db.compactRange(columnFamilyHandles.get(1),
"0".getBytes(), "201".getBytes());
} finally {
for (ColumnFamilyHandle handle : columnFamilyHandles) {
handle.dispose();
for (final ColumnFamilyHandle handle : columnFamilyHandles) {
handle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -539,21 +414,10 @@ public class RocksDBTest {
@Test
public void compactRangeWithKeysReduceColumnFamily()
throws RocksDBException {
RocksDB db = null;
DBOptions opt = null;
List<ColumnFamilyHandle> columnFamilyHandles =
new ArrayList<>();
try {
opt = new DBOptions().
try (final DBOptions opt = new DBOptions().
setCreateIfMissing(true).
setCreateMissingColumnFamilies(true);
List<ColumnFamilyDescriptor> columnFamilyDescriptors =
new ArrayList<>();
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
RocksDB.DEFAULT_COLUMN_FAMILY));
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
"new_cf".getBytes(),
new ColumnFamilyOptions().
final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions().
setDisableAutoCompactions(true).
setCompactionStyle(CompactionStyle.LEVEL).
setNumLevels(4).
@ -563,12 +427,21 @@ public class RocksDBTest {
setTargetFileSizeMultiplier(1).
setMaxBytesForLevelBase(500 << 10).
setMaxBytesForLevelMultiplier(1).
setDisableAutoCompactions(false)));
setDisableAutoCompactions(false)
) {
final List<ColumnFamilyDescriptor> columnFamilyDescriptors =
Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts)
);
final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
// open database
db = RocksDB.open(opt,
try (final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath(),
columnFamilyDescriptors,
columnFamilyHandles);
columnFamilyHandles)) {
try {
// fill database with key/value pairs
byte[] b = new byte[10000];
for (int i = 0; i < 200; i++) {
@ -579,14 +452,10 @@ public class RocksDBTest {
db.compactRange(columnFamilyHandles.get(1), "0".getBytes(),
"201".getBytes(), true, -1, 0);
} finally {
for (ColumnFamilyHandle handle : columnFamilyHandles) {
handle.dispose();
for (final ColumnFamilyHandle handle : columnFamilyHandles) {
handle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@ -594,9 +463,6 @@ public class RocksDBTest {
@Test
public void compactRangeToLevel()
throws RocksDBException, InterruptedException {
RocksDB db = null;
Options opt = null;
try {
final int NUM_KEYS_PER_L0_FILE = 100;
final int KEY_SIZE = 20;
final int VALUE_SIZE = 300;
@ -605,7 +471,7 @@ public class RocksDBTest {
final int NUM_L0_FILES = 10;
final int TEST_SCALE = 5;
final int KEY_INTERVAL = 100;
opt = new Options().
try (final Options opt = new Options().
setCreateIfMissing(true).
setCompactionStyle(CompactionStyle.LEVEL).
setNumLevels(5).
@ -621,8 +487,9 @@ public class RocksDBTest {
setMaxBytesForLevelBase(NUM_L0_FILES * L0_FILE_SIZE * 100).
setMaxBytesForLevelMultiplier(2).
setDisableAutoCompactions(true);
db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath());
final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath())
) {
// fill database with key/value pairs
byte[] value = new byte[VALUE_SIZE];
int int_key = 0;
@ -665,24 +532,12 @@ public class RocksDBTest {
db.getProperty("rocksdb.num-files-at-level2")).
isEqualTo("0");
}
} finally {
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void compactRangeToLevelColumnFamily()
throws RocksDBException {
RocksDB db = null;
DBOptions opt = null;
List<ColumnFamilyHandle> columnFamilyHandles =
new ArrayList<>();
try {
final int NUM_KEYS_PER_L0_FILE = 100;
final int KEY_SIZE = 20;
final int VALUE_SIZE = 300;
@ -691,16 +546,11 @@ public class RocksDBTest {
final int NUM_L0_FILES = 10;
final int TEST_SCALE = 5;
final int KEY_INTERVAL = 100;
opt = new DBOptions().
try (final DBOptions opt = new DBOptions().
setCreateIfMissing(true).
setCreateMissingColumnFamilies(true);
List<ColumnFamilyDescriptor> columnFamilyDescriptors =
new ArrayList<>();
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
RocksDB.DEFAULT_COLUMN_FAMILY));
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
"new_cf".getBytes(),
new ColumnFamilyOptions().
final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions().
setCompactionStyle(CompactionStyle.LEVEL).
setNumLevels(5).
// a slightly bigger write buffer than L0 file
@ -714,12 +564,21 @@ public class RocksDBTest {
// To disable auto compaction
setMaxBytesForLevelBase(NUM_L0_FILES * L0_FILE_SIZE * 100).
setMaxBytesForLevelMultiplier(2).
setDisableAutoCompactions(true)));
setDisableAutoCompactions(true)
) {
final List<ColumnFamilyDescriptor> columnFamilyDescriptors =
Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts)
);
final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
// open database
db = RocksDB.open(opt,
try (final RocksDB db = RocksDB.open(opt,
dbFolder.getRoot().getAbsolutePath(),
columnFamilyDescriptors,
columnFamilyHandles);
columnFamilyHandles)) {
try {
// fill database with key/value pairs
byte[] value = new byte[VALUE_SIZE];
int int_key = 0;
@ -770,36 +629,24 @@ public class RocksDBTest {
isEqualTo("0");
}
} finally {
for (ColumnFamilyHandle handle : columnFamilyHandles) {
handle.dispose();
for (final ColumnFamilyHandle handle : columnFamilyHandles) {
handle.close();
}
if (db != null) {
db.close();
}
if (opt != null) {
opt.dispose();
}
}
}
@Test
public void enableDisableFileDeletions() throws RocksDBException {
RocksDB db = null;
Options options = null;
try {
options = new Options().setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())
) {
db.disableFileDeletions();
db.enableFileDeletions(false);
db.disableFileDeletions();
db.enableFileDeletions(true);
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
}

@ -18,7 +18,7 @@ public class RocksEnvTest {
@Test
public void rocksEnv() {
Env rocksEnv = RocksEnv.getDefault();
try (final Env rocksEnv = RocksEnv.getDefault()) {
rocksEnv.setBackgroundThreads(5);
// default rocksenv will always return zero for flush pool
// no matter what was set via setBackgroundThreads
@ -36,3 +36,4 @@ public class RocksEnvTest {
isEqualTo(0);
}
}
}

@ -22,20 +22,15 @@ public class RocksIteratorTest {
@Test
public void rocksIterator() throws RocksDBException {
RocksDB db = null;
Options options = null;
RocksIterator iterator = null;
try {
options = new Options();
options.setCreateIfMissing(true)
try (final Options options = new Options()
.setCreateIfMissing(true)
.setCreateMissingColumnFamilies(true);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put("key1".getBytes(), "value1".getBytes());
db.put("key2".getBytes(), "value2".getBytes());
iterator = db.newIterator();
try (final RocksIterator iterator = db.newIterator()) {
iterator.seekToFirst();
assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("key1".getBytes());
@ -57,15 +52,6 @@ public class RocksIteratorTest {
assertThat(iterator.key()).isEqualTo("key2".getBytes());
assertThat(iterator.value()).isEqualTo("value2".getBytes());
iterator.status();
} finally {
if (iterator != null) {
iterator.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}

@ -33,19 +33,14 @@ public class RocksMemEnvTest {
"baz".getBytes()
};
Env env = null;
Options options = null;
RocksDB db = null;
FlushOptions flushOptions = null;
try {
env = new RocksMemEnv();
options = new Options().
setCreateIfMissing(true).
setEnv(env);
flushOptions = new FlushOptions().
setWaitForFlush(true);
db = RocksDB.open(options, "dir/db");
try (final Env env = new RocksMemEnv();
final Options options = new Options()
.setCreateIfMissing(true)
.setEnv(env);
final FlushOptions flushOptions = new FlushOptions()
.setWaitForFlush(true);
) {
try (final RocksDB db = RocksDB.open(options, "dir/db")) {
// write key/value pairs using MemEnv
for (int i = 0; i < keys.length; i++) {
db.put(keys[i], values[i]);
@ -57,7 +52,7 @@ public class RocksMemEnvTest {
}
// Check iterator access
RocksIterator iterator = db.newIterator();
try (final RocksIterator iterator = db.newIterator()) {
iterator.seekToFirst();
for (int i = 0; i < keys.length; i++) {
assertThat(iterator.isValid()).isTrue();
@ -67,7 +62,7 @@ public class RocksMemEnvTest {
}
// reached end of database
assertThat(iterator.isValid()).isFalse();
iterator.dispose();
}
// flush
db.flush(flushOptions);
@ -76,30 +71,17 @@ public class RocksMemEnvTest {
for (int i = 0; i < keys.length; i++) {
assertThat(db.get(keys[i])).isEqualTo(values[i]);
}
}
db.close();
options.setCreateIfMissing(false);
// After reopen the values shall still be in the mem env.
// as long as the env is not freed.
db = RocksDB.open(options, "dir/db");
try (final RocksDB db = RocksDB.open(options, "dir/db")) {
// read key/value pairs using MemEnv
for (int i = 0; i < keys.length; i++) {
assertThat(db.get(keys[i])).isEqualTo(values[i]);
}
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (flushOptions != null) {
flushOptions.dispose();
}
if (env != null) {
env.dispose();
}
}
}
@ -125,18 +107,13 @@ public class RocksMemEnvTest {
"baz".getBytes()
};
Env env = null;
Options options = null;
RocksDB db = null, otherDb = null;
try {
env = new RocksMemEnv();
options = new Options().
setCreateIfMissing(true).
setEnv(env);
db = RocksDB.open(options, "dir/db");
otherDb = RocksDB.open(options, "dir/otherDb");
try (final Env env = new RocksMemEnv();
final Options options = new Options()
.setCreateIfMissing(true)
.setEnv(env);
final RocksDB db = RocksDB.open(options, "dir/db");
final RocksDB otherDb = RocksDB.open(options, "dir/otherDb")
) {
// write key/value pairs using MemEnv
// to db and to otherDb.
for (int i = 0; i < keys.length; i++) {
@ -154,43 +131,18 @@ public class RocksMemEnvTest {
assertThat(otherDb.get(keys[i])).isNull();
assertThat(otherDb.get(otherKeys[i])).isEqualTo(values[i]);
}
} finally {
if (db != null) {
db.close();
}
if (otherDb != null) {
otherDb.close();
}
if (options != null) {
options.dispose();
}
if (env != null) {
env.dispose();
}
}
}
@Test(expected = RocksDBException.class)
public void createIfMissingFalse() throws RocksDBException {
Env env = null;
Options options = null;
RocksDB db = null;
try {
env = new RocksMemEnv();
options = new Options().
setCreateIfMissing(false).
setEnv(env);
try (final Env env = new RocksMemEnv();
final Options options = new Options()
.setCreateIfMissing(false)
.setEnv(env);
final RocksDB db = RocksDB.open(options, "db/dir")) {
// shall throw an exception because db dir does not
// exist.
db = RocksDB.open(options, "db/dir");
} finally {
if (options != null) {
options.dispose();
}
if (env != null) {
env.dispose();
}
}
}
}

@ -5,7 +5,11 @@ import org.junit.rules.ExternalResource;
/**
* Resource to trigger garbage collection after each test
* run.
*
* @deprecated Will be removed with the implementation of
* {@link RocksObject#finalize()}
*/
@Deprecated
public class RocksMemoryResource extends ExternalResource {
static {

@ -17,89 +17,45 @@ public class SliceTest {
@Test
public void slice() {
Slice slice = null;
Slice otherSlice = null;
Slice thirdSlice = null;
try {
slice = new Slice("testSlice");
try (final Slice slice = new Slice("testSlice")) {
assertThat(slice.empty()).isFalse();
assertThat(slice.size()).isEqualTo(9);
assertThat(slice.data()).isEqualTo("testSlice".getBytes());
}
otherSlice = new Slice("otherSlice".getBytes());
try (final Slice otherSlice = new Slice("otherSlice".getBytes())) {
assertThat(otherSlice.data()).isEqualTo("otherSlice".getBytes());
}
thirdSlice = new Slice("otherSlice".getBytes(), 5);
try (final Slice thirdSlice = new Slice("otherSlice".getBytes(), 5)) {
assertThat(thirdSlice.data()).isEqualTo("Slice".getBytes());
} finally {
if (slice != null) {
slice.dispose();
}
if (otherSlice != null) {
otherSlice.dispose();
}
if (thirdSlice != null) {
thirdSlice.dispose();
}
}
}
@Test
public void sliceEquals() {
Slice slice = null;
Slice slice2 = null;
try {
slice = new Slice("abc");
slice2 = new Slice("abc");
try (final Slice slice = new Slice("abc");
final Slice slice2 = new Slice("abc")) {
assertThat(slice.equals(slice2)).isTrue();
assertThat(slice.hashCode() == slice2.hashCode()).isTrue();
} finally {
if (slice != null) {
slice.dispose();
}
if (slice2 != null) {
slice2.dispose();
}
}
}
@Test
public void sliceStartWith() {
Slice slice = null;
Slice match = null;
Slice noMatch = null;
try {
slice = new Slice("matchpoint");
match = new Slice("mat");
noMatch = new Slice("nomatch");
//assertThat(slice.startsWith(match)).isTrue();
try (final Slice slice = new Slice("matchpoint");
final Slice match = new Slice("mat");
final Slice noMatch = new Slice("nomatch")) {
assertThat(slice.startsWith(match)).isTrue();
assertThat(slice.startsWith(noMatch)).isFalse();
} finally {
if (slice != null) {
slice.dispose();
}
if (match != null) {
match.dispose();
}
if (noMatch != null) {
noMatch.dispose();
}
}
}
@Test
public void sliceToString() {
Slice slice = null;
try {
slice = new Slice("stringTest");
try (final Slice slice = new Slice("stringTest")) {
assertThat(slice.toString()).isEqualTo("stringTest");
assertThat(slice.toString(true)).isNotEqualTo("");
} finally {
if (slice != null) {
slice.dispose();
}
}
}
}

@ -22,23 +22,18 @@ public class SnapshotTest {
@Test
public void snapshots() throws RocksDBException {
RocksDB db = null;
Options options = null;
ReadOptions readOptions = null;
try {
options = new Options();
options.setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put("key".getBytes(), "value".getBytes());
// Get new Snapshot of database
Snapshot snapshot = db.getSnapshot();
try (final Snapshot snapshot = db.getSnapshot()) {
assertThat(snapshot.getSequenceNumber()).isGreaterThan(0);
assertThat(snapshot.getSequenceNumber()).isEqualTo(1);
readOptions = new ReadOptions();
try (final ReadOptions readOptions = new ReadOptions()) {
// set snapshot in ReadOptions
readOptions.setSnapshot(snapshot);
// retrieve key value pair
assertThat(new String(db.get("key".getBytes()))).
isEqualTo("value");
@ -56,7 +51,7 @@ public class SnapshotTest {
assertThat(db.get(readOptions, "newkey".getBytes())).
isNull();
// Retrieve snapshot from read options
Snapshot sameSnapshot = readOptions.snapshot();
try (final Snapshot sameSnapshot = readOptions.snapshot()) {
readOptions.setSnapshot(sameSnapshot);
// results must be the same with new Snapshot
// instance using the same native pointer
@ -79,42 +74,28 @@ public class SnapshotTest {
"newkey".getBytes()))).isEqualTo("newvalue");
// release Snapshot
db.releaseSnapshot(snapshot);
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (readOptions != null) {
readOptions.dispose();
}
}
}
@Test
public void iteratorWithSnapshot() throws RocksDBException {
RocksDB db = null;
Options options = null;
ReadOptions readOptions = null;
RocksIterator iterator = null;
RocksIterator snapshotIterator = null;
try {
options = new Options();
options.setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put("key".getBytes(), "value".getBytes());
// Get new Snapshot of database
Snapshot snapshot = db.getSnapshot();
readOptions = new ReadOptions();
// set snapshot in ReadOptions
readOptions.setSnapshot(snapshot);
try (final Snapshot snapshot = db.getSnapshot();
final ReadOptions readOptions =
new ReadOptions().setSnapshot(snapshot)) {
db.put("key2".getBytes(), "value2".getBytes());
// iterate over current state of db
iterator = db.newIterator();
try (final RocksIterator iterator = db.newIterator()) {
iterator.seekToFirst();
assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("key".getBytes());
@ -123,59 +104,43 @@ public class SnapshotTest {
assertThat(iterator.key()).isEqualTo("key2".getBytes());
iterator.next();
assertThat(iterator.isValid()).isFalse();
}
// iterate using a snapshot
snapshotIterator = db.newIterator(readOptions);
try (final RocksIterator snapshotIterator =
db.newIterator(readOptions)) {
snapshotIterator.seekToFirst();
assertThat(snapshotIterator.isValid()).isTrue();
assertThat(snapshotIterator.key()).isEqualTo("key".getBytes());
snapshotIterator.next();
assertThat(snapshotIterator.isValid()).isFalse();
}
// release Snapshot
db.releaseSnapshot(snapshot);
} finally {
if (iterator != null) {
iterator.dispose();
}
if (snapshotIterator != null) {
snapshotIterator.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (readOptions != null) {
readOptions.dispose();
}
}
}
@Test
public void iteratorWithSnapshotOnColumnFamily() throws RocksDBException {
RocksDB db = null;
Options options = null;
ReadOptions readOptions = null;
RocksIterator iterator = null;
RocksIterator snapshotIterator = null;
try {
try (final Options options = new Options()
.setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
options = new Options();
options.setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
db.put("key".getBytes(), "value".getBytes());
// Get new Snapshot of database
Snapshot snapshot = db.getSnapshot();
readOptions = new ReadOptions();
// set snapshot in ReadOptions
readOptions.setSnapshot(snapshot);
try (final Snapshot snapshot = db.getSnapshot();
final ReadOptions readOptions = new ReadOptions()
.setSnapshot(snapshot)) {
db.put("key2".getBytes(), "value2".getBytes());
// iterate over current state of column family
iterator = db.newIterator(db.getDefaultColumnFamily());
try (final RocksIterator iterator = db.newIterator(
db.getDefaultColumnFamily())) {
iterator.seekToFirst();
assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("key".getBytes());
@ -184,10 +149,11 @@ public class SnapshotTest {
assertThat(iterator.key()).isEqualTo("key2".getBytes());
iterator.next();
assertThat(iterator.isValid()).isFalse();
}
// iterate using a snapshot on default column family
snapshotIterator = db.newIterator(db.getDefaultColumnFamily(),
readOptions);
try (final RocksIterator snapshotIterator = db.newIterator(
db.getDefaultColumnFamily(), readOptions)) {
snapshotIterator.seekToFirst();
assertThat(snapshotIterator.isValid()).isTrue();
assertThat(snapshotIterator.key()).isEqualTo("key".getBytes());
@ -196,21 +162,7 @@ public class SnapshotTest {
// release Snapshot
db.releaseSnapshot(snapshot);
} finally {
if (iterator != null) {
iterator.dispose();
}
if (snapshotIterator != null) {
snapshotIterator.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
if (readOptions != null) {
readOptions.dispose();
}
}
}

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

@ -17,41 +17,25 @@ public class TransactionLogIteratorTest {
@Test
public void transactionLogIterator() throws RocksDBException {
RocksDB db = null;
Options options = null;
TransactionLogIterator transactionLogIterator = null;
try {
options = new Options().
setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
transactionLogIterator = db.getUpdatesSince(0);
} finally {
if (transactionLogIterator != null) {
transactionLogIterator.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
try (final Options options = new Options()
.setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
final TransactionLogIterator transactionLogIterator =
db.getUpdatesSince(0)) {
//no-op
}
}
@Test
public void getBatch() throws RocksDBException {
final int numberOfPuts = 5;
RocksDB db = null;
Options options = null;
ColumnFamilyHandle cfHandle = null;
TransactionLogIterator transactionLogIterator = null;
try {
options = new Options().
setCreateIfMissing(true).
setWalTtlSeconds(1000).
setWalSizeLimitMB(10);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options()
.setCreateIfMissing(true)
.setWalTtlSeconds(1000)
.setWalSizeLimitMB(10);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
for (int i = 0; i < numberOfPuts; i++) {
db.put(String.valueOf(i).getBytes(),
@ -65,9 +49,8 @@ public class TransactionLogIteratorTest {
isEqualTo(numberOfPuts);
// insert 5 writes into a cf
cfHandle = db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf".getBytes()));
try (final ColumnFamilyHandle cfHandle = db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf".getBytes()))) {
for (int i = 0; i < numberOfPuts; i++) {
db.put(cfHandle, String.valueOf(i).getBytes(),
String.valueOf(i).getBytes());
@ -78,45 +61,34 @@ public class TransactionLogIteratorTest {
isEqualTo(numberOfPuts + numberOfPuts);
// Get updates since the beginning
transactionLogIterator = db.getUpdatesSince(0);
try (final TransactionLogIterator transactionLogIterator =
db.getUpdatesSince(0)) {
assertThat(transactionLogIterator.isValid()).isTrue();
transactionLogIterator.status();
// The first sequence number is 1
TransactionLogIterator.BatchResult batchResult =
final TransactionLogIterator.BatchResult batchResult =
transactionLogIterator.getBatch();
assertThat(batchResult.sequenceNumber()).isEqualTo(1);
} finally {
if (transactionLogIterator != null) {
transactionLogIterator.dispose();
}
if (cfHandle != null) {
cfHandle.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void transactionLogIteratorStallAtLastRecord() throws RocksDBException {
RocksDB db = null;
Options options = null;
TransactionLogIterator transactionLogIterator = null;
try {
options = new Options().
setCreateIfMissing(true).
setWalTtlSeconds(1000).
setWalSizeLimitMB(10);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
public void transactionLogIteratorStallAtLastRecord()
throws RocksDBException {
try (final Options options = new Options()
.setCreateIfMissing(true)
.setWalTtlSeconds(1000)
.setWalSizeLimitMB(10);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put("key1".getBytes(), "value1".getBytes());
// Get updates since the beginning
transactionLogIterator = db.getUpdatesSince(0);
try (final TransactionLogIterator transactionLogIterator =
db.getUpdatesSince(0)) {
transactionLogIterator.status();
assertThat(transactionLogIterator.isValid()).isTrue();
transactionLogIterator.next();
@ -126,56 +98,40 @@ public class TransactionLogIteratorTest {
transactionLogIterator.next();
transactionLogIterator.status();
assertThat(transactionLogIterator.isValid()).isTrue();
} finally {
if (transactionLogIterator != null) {
transactionLogIterator.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void transactionLogIteratorCheckAfterRestart() throws RocksDBException {
public void transactionLogIteratorCheckAfterRestart()
throws RocksDBException {
final int numberOfKeys = 2;
RocksDB db = null;
Options options = null;
TransactionLogIterator transactionLogIterator = null;
try {
options = new Options().
setCreateIfMissing(true).
setWalTtlSeconds(1000).
setWalSizeLimitMB(10);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options()
.setCreateIfMissing(true)
.setWalTtlSeconds(1000)
.setWalSizeLimitMB(10)) {
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
db.put("key1".getBytes(), "value1".getBytes());
db.put("key2".getBytes(), "value2".getBytes());
db.flush(new FlushOptions().setWaitForFlush(true));
}
// reopen
db.close();
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
assertThat(db.getLatestSequenceNumber()).isEqualTo(numberOfKeys);
transactionLogIterator = db.getUpdatesSince(0);
try (final TransactionLogIterator transactionLogIterator =
db.getUpdatesSince(0)) {
for (int i = 0; i < numberOfKeys; i++) {
transactionLogIterator.status();
assertThat(transactionLogIterator.isValid()).isTrue();
transactionLogIterator.next();
}
} finally {
if (transactionLogIterator != null) {
transactionLogIterator.dispose();
}
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}

@ -11,6 +11,7 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -26,81 +27,54 @@ public class TtlDBTest {
public TemporaryFolder dbFolder = new TemporaryFolder();
@Test
public void ttlDBOpen() throws RocksDBException,
InterruptedException {
Options options = null;
TtlDB ttlDB = null;
try {
options = new Options().
setCreateIfMissing(true).
setMaxGrandparentOverlapFactor(0);
ttlDB = TtlDB.open(options,
dbFolder.getRoot().getAbsolutePath());
public void ttlDBOpen() throws RocksDBException, InterruptedException {
try (final Options options = new Options()
.setCreateIfMissing(true)
.setMaxGrandparentOverlapFactor(0);
final TtlDB ttlDB = TtlDB.open(options,
dbFolder.getRoot().getAbsolutePath())
) {
ttlDB.put("key".getBytes(), "value".getBytes());
assertThat(ttlDB.get("key".getBytes())).
isEqualTo("value".getBytes());
assertThat(ttlDB.get("key".getBytes())).isNotNull();
} finally {
if (ttlDB != null) {
ttlDB.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void ttlDBOpenWithTtl() throws RocksDBException,
InterruptedException {
Options options = null;
TtlDB ttlDB = null;
try {
options = new Options().
setCreateIfMissing(true).
setMaxGrandparentOverlapFactor(0);
ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath(),
1, false);
public void ttlDBOpenWithTtl() throws RocksDBException, InterruptedException {
try (final Options options = new Options()
.setCreateIfMissing(true)
.setMaxGrandparentOverlapFactor(0);
final TtlDB ttlDB = TtlDB.open(options,
dbFolder.getRoot().getAbsolutePath(), 1, false);
) {
ttlDB.put("key".getBytes(), "value".getBytes());
assertThat(ttlDB.get("key".getBytes())).
isEqualTo("value".getBytes());
TimeUnit.SECONDS.sleep(2);
ttlDB.compactRange();
assertThat(ttlDB.get("key".getBytes())).isNull();
} finally {
if (ttlDB != null) {
ttlDB.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void ttlDbOpenWithColumnFamilies() throws RocksDBException, InterruptedException {
DBOptions dbOptions = null;
TtlDB ttlDB = null;
List<ColumnFamilyDescriptor> cfNames =
new ArrayList<>();
List<ColumnFamilyHandle> columnFamilyHandleList =
new ArrayList<>();
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
List<Integer> ttlValues = new ArrayList<>();
// Default column family with infinite lifetime
ttlValues.add(0);
// new column family with 1 second ttl
ttlValues.add(1);
public void ttlDbOpenWithColumnFamilies() throws RocksDBException,
InterruptedException {
final List<ColumnFamilyDescriptor> cfNames = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes())
);
final List<Integer> ttlValues = Arrays.asList(0, 1);
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions dbOptions = new DBOptions()
.setCreateMissingColumnFamilies(true)
.setCreateIfMissing(true);
final TtlDB ttlDB = TtlDB.open(dbOptions,
dbFolder.getRoot().getAbsolutePath(), cfNames,
columnFamilyHandleList, ttlValues, false)) {
try {
dbOptions = new DBOptions().
setCreateMissingColumnFamilies(true).
setCreateIfMissing(true);
ttlDB = TtlDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(),
cfNames, columnFamilyHandleList, ttlValues, false);
ttlDB.put("key".getBytes(), "value".getBytes());
assertThat(ttlDB.get("key".getBytes())).
isEqualTo("value".getBytes());
@ -116,18 +90,11 @@ public class TtlDBTest {
assertThat(ttlDB.get("key".getBytes())).isNotNull();
assertThat(ttlDB.get(columnFamilyHandleList.get(1),
"key".getBytes())).isNull();
} finally {
for (ColumnFamilyHandle columnFamilyHandle :
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.dispose();
}
if (ttlDB != null) {
ttlDB.close();
columnFamilyHandle.close();
}
if (dbOptions != null) {
dbOptions.dispose();
}
}
}
@ -135,15 +102,12 @@ public class TtlDBTest {
@Test
public void createTtlColumnFamily() throws RocksDBException,
InterruptedException {
Options options = null;
TtlDB ttlDB = null;
ColumnFamilyHandle columnFamilyHandle = null;
try {
options = new Options().setCreateIfMissing(true);
ttlDB = TtlDB.open(options,
try (final Options options = new Options().setCreateIfMissing(true);
final TtlDB ttlDB = TtlDB.open(options,
dbFolder.getRoot().getAbsolutePath());
columnFamilyHandle = ttlDB.createColumnFamilyWithTtl(
new ColumnFamilyDescriptor("new_cf".getBytes()), 1);
final ColumnFamilyHandle columnFamilyHandle =
ttlDB.createColumnFamilyWithTtl(
new ColumnFamilyDescriptor("new_cf".getBytes()), 1)) {
ttlDB.put(columnFamilyHandle, "key".getBytes(),
"value".getBytes());
assertThat(ttlDB.get(columnFamilyHandle, "key".getBytes())).
@ -151,16 +115,6 @@ public class TtlDBTest {
TimeUnit.SECONDS.sleep(2);
ttlDB.compactRange(columnFamilyHandle);
assertThat(ttlDB.get(columnFamilyHandle, "key".getBytes())).isNull();
} finally {
if (columnFamilyHandle != null) {
columnFamilyHandle.dispose();
}
if (ttlDB != null) {
ttlDB.close();
}
if (options != null) {
options.dispose();
}
}
}
}

@ -23,28 +23,26 @@ public class WriteBatchHandlerTest {
@Test
public void writeBatchHandler() throws IOException, RocksDBException {
WriteBatch batch = null;
CapturingWriteBatchHandler handler = null;
try {
// setup test data
final List<Tuple<Action, Tuple<byte[], byte[]>>> testEvents = new ArrayList<>();
testEvents.add(new Tuple<>(Action.DELETE,
new Tuple<byte[], byte[]>("k0".getBytes(), null)));
testEvents.add(new Tuple<>(Action.PUT,
new Tuple<>("k1".getBytes(), "v1".getBytes())));
testEvents.add(new Tuple<>(Action.PUT,
new Tuple<>("k2".getBytes(), "v2".getBytes())));
testEvents.add(new Tuple<>(Action.PUT,
new Tuple<>("k3".getBytes(), "v3".getBytes())));
testEvents.add(new Tuple<>(Action.LOG,
new Tuple<byte[], byte[]>(null, "log1".getBytes())));
testEvents.add(new Tuple<>(Action.MERGE,
new Tuple<>("k2".getBytes(), "v22".getBytes())));
testEvents.add(new Tuple<>(Action.DELETE,
new Tuple<byte[], byte[]>("k3".getBytes(), null)));
final List<Tuple<Action, Tuple<byte[], byte[]>>> testEvents = Arrays.asList(
new Tuple<>(Action.DELETE,
new Tuple<byte[], byte[]>("k0".getBytes(), null)),
new Tuple<>(Action.PUT,
new Tuple<>("k1".getBytes(), "v1".getBytes())),
new Tuple<>(Action.PUT,
new Tuple<>("k2".getBytes(), "v2".getBytes())),
new Tuple<>(Action.PUT,
new Tuple<>("k3".getBytes(), "v3".getBytes())),
new Tuple<>(Action.LOG,
new Tuple<byte[], byte[]>(null, "log1".getBytes())),
new Tuple<>(Action.MERGE,
new Tuple<>("k2".getBytes(), "v22".getBytes())),
new Tuple<>(Action.DELETE,
new Tuple<byte[], byte[]>("k3".getBytes(), null))
);
// load test data to the write batch
batch = new WriteBatch();
try (final WriteBatch batch = new WriteBatch()) {
for (final Tuple<Action, Tuple<byte[], byte[]>> testEvent : testEvents) {
final Tuple<byte[], byte[]> data = testEvent.value;
switch (testEvent.key) {
@ -67,28 +65,26 @@ public class WriteBatchHandlerTest {
}
}
// attempt to read test data back from the WriteBatch by iterating with a handler
handler = new CapturingWriteBatchHandler();
// attempt to read test data back from the WriteBatch by iterating
// with a handler
try (final CapturingWriteBatchHandler handler =
new CapturingWriteBatchHandler()) {
batch.iterate(handler);
// compare the results to the test data
final List<Tuple<Action, Tuple<byte[], byte[]>>> actualEvents = handler.getEvents();
final List<Tuple<Action, Tuple<byte[], byte[]>>> actualEvents =
handler.getEvents();
assertThat(testEvents.size()).isSameAs(actualEvents.size());
for (int i = 0; i < testEvents.size(); i++) {
assertThat(equals(testEvents.get(i), actualEvents.get(i))).isTrue();
}
} finally {
if (handler != null) {
handler.dispose();
}
if (batch != null) {
batch.dispose();
}
}
}
private static boolean equals(final Tuple<Action, Tuple<byte[], byte[]>> expected,
private static boolean equals(
final Tuple<Action, Tuple<byte[], byte[]>> expected,
final Tuple<Action, Tuple<byte[], byte[]>> actual) {
if (!expected.key.equals(actual.key)) {
return false;
@ -136,7 +132,8 @@ public class WriteBatchHandlerTest {
*/
private static class CapturingWriteBatchHandler extends WriteBatch.Handler {
private final List<Tuple<Action, Tuple<byte[], byte[]>>> events = new ArrayList<>();
private final List<Tuple<Action, Tuple<byte[], byte[]>>> events
= new ArrayList<>();
/**
* Returns a copy of the current events list
@ -159,12 +156,14 @@ public class WriteBatchHandlerTest {
@Override
public void delete(final byte[] key) {
events.add(new Tuple<>(Action.DELETE, new Tuple<byte[], byte[]>(key, null)));
events.add(new Tuple<>(Action.DELETE,
new Tuple<byte[], byte[]>(key, null)));
}
@Override
public void logData(final byte[] blob) {
events.add(new Tuple<>(Action.LOG, new Tuple<byte[], byte[]>(null, blob)));
events.add(new Tuple<>(Action.LOG,
new Tuple<byte[], byte[]>(null, blob)));
}
}
}

@ -20,9 +20,9 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* This class mimics the db/write_batch_test.cc
* in the c++ rocksdb library.
*
* <p/>
* Not ported yet:
*
* <p/>
* Continue();
* PutGatherSlices();
*/
@ -36,17 +36,19 @@ public class WriteBatchTest {
@Test
public void emptyWriteBatch() {
WriteBatch batch = new WriteBatch();
try (final WriteBatch batch = new WriteBatch()) {
assertThat(batch.count()).isEqualTo(0);
}
}
@Test
public void multipleBatchOperations()
throws UnsupportedEncodingException {
WriteBatch batch = new WriteBatch();
try (WriteBatch batch = new WriteBatch()) {
batch.put("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII"));
batch.remove("box".getBytes("US-ASCII"));
batch.put("baz".getBytes("US-ASCII"), "boo".getBytes("US-ASCII"));
WriteBatchTestInternalHelper.setSequence(batch, 100);
assertThat(WriteBatchTestInternalHelper.sequence(batch)).
isNotNull().
@ -57,12 +59,13 @@ public class WriteBatchTest {
"Delete(box)@101" +
"Put(foo, bar)@100");
}
}
@Test
public void testAppendOperation()
throws UnsupportedEncodingException {
WriteBatch b1 = new WriteBatch();
WriteBatch b2 = new WriteBatch();
try (final WriteBatch b1 = new WriteBatch();
final WriteBatch b2 = new WriteBatch()) {
WriteBatchTestInternalHelper.setSequence(b1, 200);
WriteBatchTestInternalHelper.setSequence(b2, 300);
WriteBatchTestInternalHelper.append(b1, b2);
@ -70,7 +73,8 @@ public class WriteBatchTest {
assertThat(b1.count()).isEqualTo(0);
b2.put("a".getBytes("US-ASCII"), "va".getBytes("US-ASCII"));
WriteBatchTestInternalHelper.append(b1, b2);
assertThat("Put(a, va)@200".equals(new String(getContents(b1), "US-ASCII")));
assertThat("Put(a, va)@200".equals(new String(getContents(b1),
"US-ASCII")));
assertThat(b1.count()).isEqualTo(1);
b2.clear();
b2.put("b".getBytes("US-ASCII"), "vb".getBytes("US-ASCII"));
@ -88,11 +92,12 @@ public class WriteBatchTest {
.equals(new String(getContents(b1), "US-ASCII")));
assertThat(b1.count()).isEqualTo(4);
}
}
@Test
public void blobOperation()
throws UnsupportedEncodingException {
WriteBatch batch = new WriteBatch();
try (final WriteBatch batch = new WriteBatch()) {
batch.put("k1".getBytes("US-ASCII"), "v1".getBytes("US-ASCII"));
batch.put("k2".getBytes("US-ASCII"), "v2".getBytes("US-ASCII"));
batch.put("k3".getBytes("US-ASCII"), "v3".getBytes("US-ASCII"));
@ -108,6 +113,7 @@ public class WriteBatchTest {
"Put(k3, v3)@2")
.equals(new String(getContents(batch), "US-ASCII")));
}
}
static byte[] getContents(final WriteBatch wb) {
return getContents(wb.nativeHandle_);
@ -133,7 +139,11 @@ class WriteBatchTestInternalHelper {
append(wb1.nativeHandle_, wb2.nativeHandle_);
}
private static native void setSequence(final long writeBatchHandle, final long sn);
private static native void setSequence(final long writeBatchHandle,
final long sn);
private static native long sequence(final long writeBatchHandle);
private static native void append(final long writeBatchHandle1, final long writeBatchHandle2);
private static native void append(final long writeBatchHandle1,
final long writeBatchHandle2);
}

@ -32,13 +32,9 @@ public class WriteBatchWithIndexTest {
@Test
public void readYourOwnWrites() throws RocksDBException {
RocksDB db = null;
Options options = null;
try {
options = new Options();
// Setup options
options.setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
final byte[] k1 = "key1".getBytes();
final byte[] v1 = "value1".getBytes();
@ -48,13 +44,9 @@ public class WriteBatchWithIndexTest {
db.put(k1, v1);
db.put(k2, v2);
final WriteBatchWithIndex wbwi = new WriteBatchWithIndex(true);
RocksIterator base = null;
RocksIterator it = null;
try {
base = db.newIterator();
it = wbwi.newIteratorWithBase(base);
try (final WriteBatchWithIndex wbwi = new WriteBatchWithIndex(true);
final RocksIterator base = db.newIterator();
final RocksIterator it = wbwi.newIteratorWithBase(base)) {
it.seek(k1);
assertThat(it.isValid()).isTrue();
@ -95,72 +87,36 @@ public class WriteBatchWithIndexTest {
assertThat(it.isValid()).isTrue();
assertThat(it.key()).isEqualTo(k1);
assertThat(it.value()).isEqualTo(v1Other);
} finally {
if (it != null) {
it.dispose();
}
if (base != null) {
base.dispose();
}
}
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void write_writeBatchWithIndex() throws RocksDBException {
RocksDB db = null;
Options options = null;
try {
options = new Options();
// Setup options
options.setCreateIfMissing(true);
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());
try (final Options options = new Options().setCreateIfMissing(true);
final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
final byte[] k1 = "key1".getBytes();
final byte[] v1 = "value1".getBytes();
final byte[] k2 = "key2".getBytes();
final byte[] v2 = "value2".getBytes();
WriteBatchWithIndex wbwi = null;
try {
wbwi = new WriteBatchWithIndex();
try (final WriteBatchWithIndex wbwi = new WriteBatchWithIndex()) {
wbwi.put(k1, v1);
wbwi.put(k2, v2);
db.write(new WriteOptions(), wbwi);
} finally {
if(wbwi != null) {
wbwi.dispose();
}
}
assertThat(db.get(k1)).isEqualTo(v1);
assertThat(db.get(k2)).isEqualTo(v2);
} finally {
if (db != null) {
db.close();
}
if (options != null) {
options.dispose();
}
}
}
@Test
public void iterator() throws RocksDBException {
final WriteBatchWithIndex wbwi = new WriteBatchWithIndex(true);
try (final WriteBatchWithIndex wbwi = new WriteBatchWithIndex(true)) {
final String k1 = "key1";
final String v1 = "value1";
@ -185,7 +141,7 @@ public class WriteBatchWithIndexTest {
final byte[] k4b = k4.getBytes();
wbwi.remove(k4b);
WBWIRocksIterator.WriteEntry[] expected = {
final WBWIRocksIterator.WriteEntry[] expected = {
new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT,
new DirectSlice(k1), new DirectSlice(v1)),
new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT,
@ -196,10 +152,7 @@ public class WriteBatchWithIndexTest {
new DirectSlice(k4), DirectSlice.NONE)
};
WBWIRocksIterator it = null;
try {
it = wbwi.newIterator();
try (final WBWIRocksIterator it = wbwi.newIterator()) {
//direct access - seek to key offsets
final int[] testOffsets = {2, 0, 1, 3};
@ -225,23 +178,18 @@ public class WriteBatchWithIndexTest {
for (it.seekToLast(); it.isValid(); it.prev()) {
assertThat(it.entry().equals(expected[i--])).isTrue();
}
} finally {
if(it != null) {
it.dispose();
}
}
}
@Test
public void zeroByteTests() {
final WriteBatchWithIndex wbwi = new WriteBatchWithIndex(true);
byte[] zeroByteValue = new byte[] { 0, 0 };
try (final WriteBatchWithIndex wbwi = new WriteBatchWithIndex(true)) {
final byte[] zeroByteValue = new byte[]{0, 0};
//add zero byte value
wbwi.put(zeroByteValue, zeroByteValue);
ByteBuffer buffer = ByteBuffer.allocateDirect(zeroByteValue.length);
final ByteBuffer buffer = ByteBuffer.allocateDirect(zeroByteValue.length);
buffer.put(zeroByteValue);
WBWIRocksIterator.WriteEntry[] expected = {
@ -249,15 +197,11 @@ public class WriteBatchWithIndexTest {
new DirectSlice(buffer, zeroByteValue.length),
new DirectSlice(buffer, zeroByteValue.length))
};
WBWIRocksIterator it = null;
try {
it = wbwi.newIterator();
try (final WBWIRocksIterator it = wbwi.newIterator()) {
it.seekToFirst();
assertThat(it.entry().equals(expected[0])).isTrue();
assertThat(it.entry().hashCode() == expected[0].hashCode()).isTrue();
} finally {
if(it != null) {
it.dispose();
}
}
}

@ -18,7 +18,7 @@ public class WriteOptionsTest {
@Test
public void writeOptions() {
WriteOptions writeOptions = new WriteOptions();
try (final WriteOptions writeOptions = new WriteOptions()) {
writeOptions.setDisableWAL(true);
assertThat(writeOptions.disableWAL()).isTrue();
writeOptions.setDisableWAL(false);
@ -29,3 +29,4 @@ public class WriteOptionsTest {
assertThat(writeOptions.sync()).isFalse();
}
}
}

Loading…
Cancel
Save