diff --git a/java/samples/src/main/java/RocksDBColumnFamilySample.java b/java/samples/src/main/java/RocksDBColumnFamilySample.java index 8d682928c..7dd0de9cc 100644 --- a/java/samples/src/main/java/RocksDBColumnFamilySample.java +++ b/java/samples/src/main/java/RocksDBColumnFamilySample.java @@ -22,73 +22,57 @@ 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())); - assert(columnFamilyHandle != null); - - } finally { - if (columnFamilyHandle != null) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - db = null; - } - if (options != null) { - options.dispose(); + new ColumnFamilyOptions()))) { + assert (columnFamilyHandle != null); } } // open DB with two column families - List columnFamilyDescriptors = new ArrayList<>(); + final List 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 columnFamilyHandles = new ArrayList<>(); - try { - db = RocksDB.open(new DBOptions(), db_path, - columnFamilyDescriptors, columnFamilyHandles); + final List columnFamilyHandles = new ArrayList<>(); + try(final DBOptions options = new DBOptions(); + final RocksDB db = RocksDB.open(options, db_path, + columnFamilyDescriptors, columnFamilyHandles)) { assert(db != null); - // put and get from non-default column family - db.put(columnFamilyHandles.get(0), new WriteOptions(), - "key".getBytes(), "value".getBytes()); - String value = new String(db.get(columnFamilyHandles.get(0), - "key".getBytes())); + try { + // put and get from non-default column family + db.put(columnFamilyHandles.get(0), new WriteOptions(), + "key".getBytes(), "value".getBytes()); + String value = new String(db.get(columnFamilyHandles.get(0), + "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()); - wb.remove(columnFamilyHandles.get(0), "key".getBytes()); - db.write(new WriteOptions(), wb); + // atomic write + 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(); - } - if (wb != null) { - wb.dispose(); + // drop column family + db.dropColumnFamily(columnFamilyHandles.get(1)); + } finally { + for (final ColumnFamilyHandle handle : columnFamilyHandles) { + handle.close(); + } } } } diff --git a/java/samples/src/main/java/RocksDBSample.java b/java/samples/src/main/java/RocksDBSample.java index 3ac17777d..de5bc26d5 100644 --- a/java/samples/src/main/java/RocksDBSample.java +++ b/java/samples/src/main/java/RocksDBSample.java @@ -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,287 +28,273 @@ 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); - assert(false); - } catch (RocksDBException e) { - System.out.format("caught the expceted exception -- %s\n", e); - assert(db == null); - } + try (final Options options = new Options(); + final Filter bloomFilter = new BloomFilter(10); + final ReadOptions readOptions = new ReadOptions() + .setFillCache(false)) { - try { - options.setCreateIfMissing(true) - .createStatistics() - .setWriteBufferSize(8 * SizeUnit.KB) - .setMaxWriteBufferNumber(3) - .setMaxBackgroundCompactions(10) - .setCompressionType(CompressionType.SNAPPY_COMPRESSION) - .setCompactionStyle(CompactionStyle.UNIVERSAL); - } catch (IllegalArgumentException e) { - assert(false); - } - - Statistics stats = options.statisticsPtr(); - - assert(options.createIfMissing() == true); - assert(options.writeBufferSize() == 8 * SizeUnit.KB); - assert(options.maxWriteBufferNumber() == 3); - assert(options.maxBackgroundCompactions() == 10); - assert(options.compressionType() == CompressionType.SNAPPY_COMPRESSION); - assert(options.compactionStyle() == CompactionStyle.UNIVERSAL); - - assert(options.memTableFactoryName().equals("SkipListFactory")); - options.setMemTableConfig( - new HashSkipListMemTableConfig() - .setHeight(4) - .setBranchingFactor(4) - .setBucketCount(2000000)); - assert(options.memTableFactoryName().equals("HashSkipListRepFactory")); - - options.setMemTableConfig( - new HashLinkedListMemTableConfig() - .setBucketCount(100000)); - assert(options.memTableFactoryName().equals("HashLinkedListRepFactory")); - - options.setMemTableConfig( - new VectorMemTableConfig().setReservedSize(10000)); - assert(options.memTableFactoryName().equals("VectorRepFactory")); - - options.setMemTableConfig(new SkipListMemTableConfig()); - assert(options.memTableFactoryName().equals("SkipListFactory")); - - options.setTableFormatConfig(new PlainTableConfig()); - // Plain-Table requires mmap read - options.setAllowMmapReads(true); - assert(options.tableFactoryName().equals("PlainTable")); - - options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000, - 10000, 10)); - options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000)); - - - Filter bloomFilter = new BloomFilter(10); - BlockBasedTableConfig table_options = new BlockBasedTableConfig(); - table_options.setBlockCacheSize(64 * SizeUnit.KB) - .setFilter(bloomFilter) - .setCacheNumShardBits(6) - .setBlockSizeDeviation(5) - .setBlockRestartInterval(10) - .setCacheIndexAndFilterBlocks(true) - .setHashIndexAllowCollision(false) - .setBlockCacheCompressedSize(64 * SizeUnit.KB) - .setBlockCacheCompressedNumShardBits(10); - - assert(table_options.blockCacheSize() == 64 * SizeUnit.KB); - assert(table_options.cacheNumShardBits() == 6); - assert(table_options.blockSizeDeviation() == 5); - assert(table_options.blockRestartInterval() == 10); - assert(table_options.cacheIndexAndFilterBlocks() == true); - assert(table_options.hashIndexAllowCollision() == false); - assert(table_options.blockCacheCompressedSize() == 64 * SizeUnit.KB); - assert(table_options.blockCacheCompressedNumShardBits() == 10); - - options.setTableFormatConfig(table_options); - assert(options.tableFactoryName().equals("BlockBasedTable")); - - try { - db = RocksDB.open(options, db_path); - db.put("hello".getBytes(), "world".getBytes()); - byte[] value = db.get("hello".getBytes()); - assert("world".equals(new String(value))); - String str = db.getProperty("rocksdb.stats"); - 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); - db.put("hello".getBytes(), "world".getBytes()); - byte[] value = db.get("hello".getBytes()); - System.out.format("Get('hello') = %s\n", - new String(value)); - - for (int i = 1; i <= 9; ++i) { - for (int j = 1; j <= 9; ++j) { - db.put(String.format("%dx%d", i, j).getBytes(), - String.format("%d", i * j).getBytes()); - } + try (final RocksDB db = RocksDB.open(options, db_path_not_found)) { + assert (false); + } catch (RocksDBException e) { + System.out.format("caught the expected exception -- %s\n", e); } - for (int i = 1; i <= 9; ++i) { - for (int j = 1; j <= 9; ++j) { - System.out.format("%s ", new String(db.get( - String.format("%dx%d", i, j).getBytes()))); - } - System.out.println(""); + try { + options.setCreateIfMissing(true) + .createStatistics() + .setWriteBufferSize(8 * SizeUnit.KB) + .setMaxWriteBufferNumber(3) + .setMaxBackgroundCompactions(10) + .setCompressionType(CompressionType.SNAPPY_COMPRESSION) + .setCompactionStyle(CompactionStyle.UNIVERSAL); + } catch (IllegalArgumentException e) { + assert (false); } - // write batch test - WriteOptions writeOpt = new WriteOptions(); - for (int i = 10; i <= 19; ++i) { - WriteBatch batch = new WriteBatch(); - for (int j = 10; j <= 19; ++j) { - batch.put(String.format("%dx%d", i, j).getBytes(), + Statistics stats = options.statisticsPtr(); + + assert (options.createIfMissing() == true); + assert (options.writeBufferSize() == 8 * SizeUnit.KB); + assert (options.maxWriteBufferNumber() == 3); + assert (options.maxBackgroundCompactions() == 10); + assert (options.compressionType() == CompressionType.SNAPPY_COMPRESSION); + assert (options.compactionStyle() == CompactionStyle.UNIVERSAL); + + assert (options.memTableFactoryName().equals("SkipListFactory")); + options.setMemTableConfig( + new HashSkipListMemTableConfig() + .setHeight(4) + .setBranchingFactor(4) + .setBucketCount(2000000)); + assert (options.memTableFactoryName().equals("HashSkipListRepFactory")); + + options.setMemTableConfig( + new HashLinkedListMemTableConfig() + .setBucketCount(100000)); + assert (options.memTableFactoryName().equals("HashLinkedListRepFactory")); + + options.setMemTableConfig( + new VectorMemTableConfig().setReservedSize(10000)); + assert (options.memTableFactoryName().equals("VectorRepFactory")); + + options.setMemTableConfig(new SkipListMemTableConfig()); + assert (options.memTableFactoryName().equals("SkipListFactory")); + + options.setTableFormatConfig(new PlainTableConfig()); + // Plain-Table requires mmap read + options.setAllowMmapReads(true); + assert (options.tableFactoryName().equals("PlainTable")); + + options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000, + 10000, 10)); + options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000)); + + final BlockBasedTableConfig table_options = new BlockBasedTableConfig(); + table_options.setBlockCacheSize(64 * SizeUnit.KB) + .setFilter(bloomFilter) + .setCacheNumShardBits(6) + .setBlockSizeDeviation(5) + .setBlockRestartInterval(10) + .setCacheIndexAndFilterBlocks(true) + .setHashIndexAllowCollision(false) + .setBlockCacheCompressedSize(64 * SizeUnit.KB) + .setBlockCacheCompressedNumShardBits(10); + + assert (table_options.blockCacheSize() == 64 * SizeUnit.KB); + assert (table_options.cacheNumShardBits() == 6); + assert (table_options.blockSizeDeviation() == 5); + assert (table_options.blockRestartInterval() == 10); + assert (table_options.cacheIndexAndFilterBlocks() == true); + assert (table_options.hashIndexAllowCollision() == false); + assert (table_options.blockCacheCompressedSize() == 64 * SizeUnit.KB); + assert (table_options.blockCacheCompressedNumShardBits() == 10); + + options.setTableFormatConfig(table_options); + assert (options.tableFactoryName().equals("BlockBasedTable")); + + 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))); + String str = db.getProperty("rocksdb.stats"); + assert (str != null && !str.equals("")); + } catch (RocksDBException e) { + System.out.format("[ERROR] caught the unexpceted exception -- %s\n", e); + assert (false); + } + + 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", + new String(value)); + + for (int i = 1; i <= 9; ++i) { + for (int j = 1; j <= 9; ++j) { + db.put(String.format("%dx%d", i, j).getBytes(), + String.format("%d", i * j).getBytes()); + } + } + + for (int i = 1; i <= 9; ++i) { + for (int j = 1; j <= 9; ++j) { + System.out.format("%s ", new String(db.get( + String.format("%dx%d", i, j).getBytes()))); + } + System.out.println(""); + } + + // write batch test + try (final WriteOptions writeOpt = new WriteOptions()) { + for (int i = 10; i <= 19; ++i) { + 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); + } + } } - db.write(writeOpt, batch); - batch.dispose(); - } - for (int i = 10; i <= 19; ++i) { - for (int j = 10; j <= 19; ++j) { - assert(new String( - db.get(String.format("%dx%d", i, j).getBytes())).equals( - String.format("%d", i * j))); - System.out.format("%s ", new String(db.get( - String.format("%dx%d", i, j).getBytes()))); + for (int i = 10; i <= 19; ++i) { + for (int j = 10; j <= 19; ++j) { + assert (new String( + db.get(String.format("%dx%d", i, j).getBytes())).equals( + String.format("%d", i * j))); + System.out.format("%s ", new String(db.get( + String.format("%dx%d", i, j).getBytes()))); + } + System.out.println(""); } - System.out.println(""); - } - writeOpt.dispose(); - value = db.get("1x1".getBytes()); - assert(value != null); - value = db.get("world".getBytes()); - assert(value == null); - value = db.get(readOptions, "world".getBytes()); - assert(value == null); + value = db.get("1x1".getBytes()); + assert (value != null); + value = db.get("world".getBytes()); + assert (value == null); + value = db.get(readOptions, "world".getBytes()); + assert (value == null); - byte[] testKey = "asdf".getBytes(); - byte[] testValue = - "asdfghjkl;'?> insufficientArray.length); - len = db.get("asdfjkl;".getBytes(), enoughArray); - assert(len == RocksDB.NOT_FOUND); - len = db.get(testKey, enoughArray); - assert(len == testValue.length); + byte[] insufficientArray = new byte[10]; + byte[] enoughArray = new byte[50]; + int len; + len = db.get(testKey, insufficientArray); + assert (len > insufficientArray.length); + len = db.get("asdfjkl;".getBytes(), enoughArray); + assert (len == RocksDB.NOT_FOUND); + len = db.get(testKey, enoughArray); + assert (len == testValue.length); - len = db.get(readOptions, testKey, insufficientArray); - assert(len > insufficientArray.length); - len = db.get(readOptions, "asdfjkl;".getBytes(), enoughArray); - assert(len == RocksDB.NOT_FOUND); - len = db.get(readOptions, testKey, enoughArray); - assert(len == testValue.length); + len = db.get(readOptions, testKey, insufficientArray); + assert (len > insufficientArray.length); + len = db.get(readOptions, "asdfjkl;".getBytes(), enoughArray); + assert (len == RocksDB.NOT_FOUND); + len = db.get(readOptions, testKey, enoughArray); + assert (len == testValue.length); - db.remove(testKey); - len = db.get(testKey, enoughArray); - assert(len == RocksDB.NOT_FOUND); + db.remove(testKey); + len = db.get(testKey, enoughArray); + assert (len == RocksDB.NOT_FOUND); - // repeat the test with WriteOptions - WriteOptions writeOpts = new WriteOptions(); - writeOpts.setSync(true); - writeOpts.setDisableWAL(true); - db.put(writeOpts, testKey, testValue); - len = db.get(testKey, enoughArray); - assert(len == testValue.length); - assert(new String(testValue).equals( - new String(enoughArray, 0, len))); - writeOpts.dispose(); - - try { - for (TickerType statsType : TickerType.values()) { - stats.getTickerCount(statsType); + // repeat the test with WriteOptions + try (final WriteOptions writeOpts = new WriteOptions()) { + writeOpts.setSync(true); + writeOpts.setDisableWAL(true); + db.put(writeOpts, testKey, testValue); + len = db.get(testKey, enoughArray); + assert (len == testValue.length); + assert (new String(testValue).equals( + new String(enoughArray, 0, len))); } - System.out.println("getTickerCount() passed."); - } catch (Exception e) { - System.out.println("Failed in call to getTickerCount()"); - assert(false); //Should never reach here. - } - try { - for (HistogramType histogramType : HistogramType.values()) { - HistogramData data = stats.geHistogramData(histogramType); + try { + for (TickerType statsType : TickerType.values()) { + stats.getTickerCount(statsType); + } + System.out.println("getTickerCount() passed."); + } catch (Exception e) { + System.out.println("Failed in call to getTickerCount()"); + assert (false); //Should never reach here. } - System.out.println("geHistogramData() passed."); - } catch (Exception e) { - System.out.println("Failed in call to geHistogramData()"); - assert(false); //Should never reach here. + + try { + for (HistogramType histogramType : HistogramType.values()) { + HistogramData data = stats.geHistogramData(histogramType); + } + System.out.println("geHistogramData() passed."); + } catch (Exception e) { + System.out.println("Failed in call to geHistogramData()"); + assert (false); //Should never reach here. + } + + try (final RocksIterator iterator = db.newIterator()) { + + boolean seekToFirstPassed = false; + for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { + iterator.status(); + assert (iterator.key() != null); + assert (iterator.value() != null); + seekToFirstPassed = true; + } + if (seekToFirstPassed) { + System.out.println("iterator seekToFirst tests passed."); + } + + boolean seekToLastPassed = false; + for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) { + iterator.status(); + assert (iterator.key() != null); + assert (iterator.value() != null); + seekToLastPassed = true; + } + + if (seekToLastPassed) { + System.out.println("iterator seekToLastPassed tests passed."); + } + + iterator.seekToFirst(); + iterator.seek(iterator.key()); + assert (iterator.key() != null); + assert (iterator.value() != null); + + System.out.println("iterator seek test passed."); + + } + System.out.println("iterator tests passed."); + + final List keys = new ArrayList<>(); + try (final RocksIterator iterator = db.newIterator()) { + for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) { + keys.add(iterator.key()); + } + } + + Map values = db.multiGet(keys); + assert (values.size() == keys.size()); + for (byte[] value1 : values.values()) { + assert (value1 != null); + } + + values = db.multiGet(new ReadOptions(), keys); + assert (values.size() == keys.size()); + for (byte[] value1 : values.values()) { + assert (value1 != null); + } + } catch (RocksDBException e) { + System.err.println(e); } - - RocksIterator iterator = db.newIterator(); - - boolean seekToFirstPassed = false; - for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { - iterator.status(); - assert(iterator.key() != null); - assert(iterator.value() != null); - seekToFirstPassed = true; - } - if(seekToFirstPassed) { - System.out.println("iterator seekToFirst tests passed."); - } - - boolean seekToLastPassed = false; - for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) { - iterator.status(); - assert(iterator.key() != null); - assert(iterator.value() != null); - seekToLastPassed = true; - } - - if(seekToLastPassed) { - System.out.println("iterator seekToLastPassed tests passed."); - } - - iterator.seekToFirst(); - iterator.seek(iterator.key()); - assert(iterator.key() != null); - assert(iterator.value() != null); - - System.out.println("iterator seek test passed."); - - iterator.dispose(); - System.out.println("iterator tests passed."); - - iterator = db.newIterator(); - List keys = new ArrayList(); - for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) { - keys.add(iterator.key()); - } - iterator.dispose(); - - Map values = db.multiGet(keys); - assert(values.size() == keys.size()); - for(byte[] value1 : values.values()) { - assert(value1 != null); - } - - values = db.multiGet(new ReadOptions(), keys); - assert(values.size() == keys.size()); - for(byte[] value1 : values.values()) { - assert(value1 != null); - } - } catch (RocksDBException e) { - System.err.println(e); } - if (db != null) { - db.close(); - } - // be sure to dispose c++ pointers - options.dispose(); - readOptions.dispose(); } } diff --git a/java/src/test/java/org/rocksdb/AbstractComparatorTest.java b/java/src/test/java/org/rocksdb/AbstractComparatorTest.java index bf8b3c0f7..db4b4d7d0 100644 --- a/java/src/test/java/org/rocksdb/AbstractComparatorTest.java +++ b/java/src/test/java/org/rocksdb/AbstractComparatorTest.java @@ -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,57 +40,43 @@ 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()); - 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) - i--; // generate a different key - } else { - db.put(key, "value".getBytes()); + 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()); + // 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(); - it.seekToFirst(); - int lastKey = Integer.MIN_VALUE; - int count = 0; - for (it.seekToFirst(); it.isValid(); it.next()) { - final int thisKey = byteToInt(it.key()); - assertThat(thisKey).isGreaterThan(lastKey); - lastKey = thisKey; - count++; - } - it.dispose(); - db.close(); - - assertThat(count).isEqualTo(ITERATIONS); - - } finally { - if (db != null) { - db.close(); - } - - if (opt != null) { - opt.dispose(); + 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; + for (it.seekToFirst(); it.isValid(); it.next()) { + final int thisKey = byteToInt(it.key()); + assertThat(thisKey).isGreaterThan(lastKey); + lastKey = thisKey; + count++; + } + assertThat(count).isEqualTo(ITERATIONS); } } } @@ -109,80 +96,75 @@ public abstract class AbstractComparatorTest { public void testRoundtripCf(final Path db_path) throws IOException, RocksDBException { - DBOptions opt = null; - RocksDB db = null; - List cfDescriptors = - new ArrayList<>(); - cfDescriptors.add(new ColumnFamilyDescriptor( - RocksDB.DEFAULT_COLUMN_FAMILY)); - cfDescriptors.add(new ColumnFamilyDescriptor("new_cf".getBytes(), - new ColumnFamilyOptions().setComparator( - getAscendingIntKeyComparator()))); - List cfHandles = new ArrayList<>(); - try { - opt = new DBOptions(). + try(final AbstractComparator comparator = getAscendingIntKeyComparator()) { + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes(), + new ColumnFamilyOptions().setComparator(comparator)) + ); + + final List 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; + // store 10,000 random integer keys + final int ITERATIONS = 10000; - db = RocksDB.open(opt, db_path.toString(), cfDescriptors, cfHandles); - assertThat(cfDescriptors.size()).isEqualTo(2); - assertThat(cfHandles.size()).isEqualTo(2); + try (final RocksDB db = RocksDB.open(opt, db_path.toString(), + cfDescriptors, cfHandles)) { + try { + assertThat(cfDescriptors.size()).isEqualTo(2); + assertThat(cfHandles.size()).isEqualTo(2); - final Random random = new Random(); - for (int i = 0; i < ITERATIONS; i++) { - final byte key[] = intToByte(random.nextInt()); - if (i > 0 && db.get(cfHandles.get(1), key) != null) { - // does key already exist (avoid duplicates) - i--; // generate a different key - } else { - db.put(cfHandles.get(1), key, "value".getBytes()); + final Random random = new Random(); + for (int i = 0; i < ITERATIONS; i++) { + final byte key[] = intToByte(random.nextInt()); + if (i > 0 && db.get(cfHandles.get(1), key) != null) { + // does key already exist (avoid duplicates) + i--; // generate a different key + } else { + db.put(cfHandles.get(1), key, "value".getBytes()); + } + } + } finally { + for (final ColumnFamilyHandle handle : cfHandles) { + handle.close(); + } + } + cfHandles.clear(); } - } - for (ColumnFamilyHandle handle : cfHandles) { - handle.dispose(); - } - cfHandles.clear(); - db.close(); - // re-open db and read from start to end - // integer keys should be in ascending - // order as defined by SimpleIntComparator - db = RocksDB.open(opt, db_path.toString(), cfDescriptors, cfHandles); - assertThat(cfDescriptors.size()).isEqualTo(2); - assertThat(cfHandles.size()).isEqualTo(2); - final RocksIterator it = db.newIterator(cfHandles.get(1)); - it.seekToFirst(); - int lastKey = Integer.MIN_VALUE; - int count = 0; - for (it.seekToFirst(); it.isValid(); it.next()) { - final int thisKey = byteToInt(it.key()); - assertThat(thisKey).isGreaterThan(lastKey); - lastKey = thisKey; - count++; - } + // re-open db and read from start to end + // integer keys should be in ascending + // order as defined by SimpleIntComparator + 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); - it.dispose(); - for (ColumnFamilyHandle handle : cfHandles) { - handle.dispose(); - } - cfHandles.clear(); - db.close(); - assertThat(count).isEqualTo(ITERATIONS); + it.seekToFirst(); + int lastKey = Integer.MIN_VALUE; + int count = 0; + for (it.seekToFirst(); it.isValid(); it.next()) { + final int thisKey = byteToInt(it.key()); + assertThat(thisKey).isGreaterThan(lastKey); + lastKey = thisKey; + count++; + } - } finally { - for (ColumnFamilyHandle handle : cfHandles) { - handle.dispose(); - } + assertThat(count).isEqualTo(ITERATIONS); - if (db != null) { - db.close(); - } - - if (opt != null) { - opt.dispose(); + } finally { + for (final ColumnFamilyHandle handle : cfHandles) { + handle.close(); + } + } + cfHandles.clear(); + } } } } diff --git a/java/src/test/java/org/rocksdb/BackupEngineTest.java b/java/src/test/java/org/rocksdb/BackupEngineTest.java index f010ff3ac..b50ddf499 100644 --- a/java/src/test/java/org/rocksdb/BackupEngineTest.java +++ b/java/src/test/java/org/rocksdb/BackupEngineTest.java @@ -28,148 +28,96 @@ 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()); + // Open empty database. + 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)) { - 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(); + final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) { + be.createNewBackup(db, false); + be.createNewBackup(db, true); + verifyNumberOfValidBackups(be, 2); } } } @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()); + // Open empty database. + 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( - backupFolder.getRoot().getAbsolutePath()); - try(final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) { - be.createNewBackup(db, false); - be.createNewBackup(db, true); - final List backupInfo = - verifyNumberOfValidBackups(be, 2); - // Delete the first backup - be.deleteBackup(backupInfo.get(0).backupId()); - final List newBackupInfo = - verifyNumberOfValidBackups(be, 1); + try(final BackupableDBOptions bopt = new BackupableDBOptions( + backupFolder.getRoot().getAbsolutePath()); + final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) { + be.createNewBackup(db, false); + be.createNewBackup(db, true); + final List backupInfo = + verifyNumberOfValidBackups(be, 2); + // Delete the first backup + be.deleteBackup(backupInfo.get(0).backupId()); + final List newBackupInfo = + verifyNumberOfValidBackups(be, 1); - // The second backup must remain. - 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(); + // The second backup must remain. + assertThat(newBackupInfo.get(0).backupId()). + isEqualTo(backupInfo.get(1).backupId()); } } } @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()); + // Open empty database. + 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( - backupFolder.getRoot().getAbsolutePath()); - try(final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) { - be.createNewBackup(db, false); - be.createNewBackup(db, true); - be.createNewBackup(db, true); - be.createNewBackup(db, true); - final List backupInfo = - verifyNumberOfValidBackups(be, 4); - // Delete everything except the latest backup - be.purgeOldBackups(1); - final List newBackupInfo = - verifyNumberOfValidBackups(be, 1); - // The latest backup must remain. - 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(); + try(final BackupableDBOptions bopt = new BackupableDBOptions( + backupFolder.getRoot().getAbsolutePath()); + final BackupEngine be = BackupEngine.open(opt.getEnv(), bopt)) { + be.createNewBackup(db, false); + be.createNewBackup(db, true); + be.createNewBackup(db, true); + be.createNewBackup(db, true); + final List backupInfo = + verifyNumberOfValidBackups(be, 4); + // Delete everything except the latest backup + be.purgeOldBackups(1); + final List newBackupInfo = + verifyNumberOfValidBackups(be, 1); + // The latest backup must remain. + assertThat(newBackupInfo.get(0).backupId()). + isEqualTo(backupInfo.get(3).backupId()); } } } @Test - public void restoreLatestBackup() - throws RocksDBException { - Options opt = null; - RocksDB db = null; - try { - opt = new Options().setCreateIfMissing(true); + public void restoreLatestBackup() throws RocksDBException { + try(final Options 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; + RocksDB db = null; try { - bopt = new BackupableDBOptions( + db = RocksDB.open(opt, + dbFolder.getRoot().getAbsolutePath()); + // Fill database with some test values + prepareDatabase(db); + + 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,51 +130,44 @@ public class BackupEngineTest { assertThat(new String(db.get("key2".getBytes()))).endsWith("V3"); db.close(); + db = null; verifyNumberOfValidBackups(be, 2); // restore db from latest backup - be.restoreDbFromLatestBackup(dbFolder.getRoot().getAbsolutePath(), - dbFolder.getRoot().getAbsolutePath(), - new RestoreOptions(false)); + try(final RestoreOptions ropts = new RestoreOptions(false)) { + be.restoreDbFromLatestBackup(dbFolder.getRoot().getAbsolutePath(), + 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(); + if(db != null) { + db.close(); } } - } finally { - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); - } } } @Test public void restoreFromBackup() throws RocksDBException { - Options opt = null; - 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(final Options opt = new Options().setCreateIfMissing(true)) { + RocksDB db = null; try { - bopt = new BackupableDBOptions( + // Open empty database. + db = RocksDB.open(opt, + dbFolder.getRoot().getAbsolutePath()); + // Fill database with some test values + prepareDatabase(db); + 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 = verifyNumberOfValidBackups(be, 2); + final List backupInfo = verifyNumberOfValidBackups(be, 2); // restore db from first backup be.restoreDbFromBackup(backupInfo.get(0).backupId(), dbFolder.getRoot().getAbsolutePath(), @@ -256,17 +198,10 @@ public class BackupEngineTest { assertThat(new String(db.get("key2".getBytes()))).endsWith("V1"); } } finally { - if(bopt != null) { - bopt.dispose(); + if(db != null) { + db.close(); } } - } finally { - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); - } } } diff --git a/java/src/test/java/org/rocksdb/BackupableDBOptionsTest.java b/java/src/test/java/org/rocksdb/BackupableDBOptionsTest.java index 44dc5b578..c3836ac9b 100644 --- a/java/src/test/java/org/rocksdb/BackupableDBOptionsTest.java +++ b/java/src/test/java/org/rocksdb/BackupableDBOptionsTest.java @@ -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); - options.backupDir(); + public void failBackupDirIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.backupDir(); + } } @Test - public void failSetShareTableFilesIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.setShareTableFiles(true); + public void failSetShareTableFilesIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.setShareTableFiles(true); + } } @Test - public void failShareTableFilesIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.shareTableFiles(); + public void failShareTableFilesIfDisposed() { + try (BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.shareTableFiles(); + } } @Test - public void failSetSyncIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.setSync(true); + public void failSetSyncIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.setSync(true); + } } @Test - public void failSyncIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.sync(); + public void failSyncIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.sync(); + } } @Test - public void failSetDestroyOldDataIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.setDestroyOldData(true); + public void failSetDestroyOldDataIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.setDestroyOldData(true); + } } @Test - public void failDestroyOldDataIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.destroyOldData(); + public void failDestroyOldDataIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.destroyOldData(); + } } @Test - public void failSetBackupLogFilesIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.setBackupLogFiles(true); + public void failSetBackupLogFilesIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.setBackupLogFiles(true); + } } @Test - public void failBackupLogFilesIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.backupLogFiles(); + public void failBackupLogFilesIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.backupLogFiles(); + } } @Test - public void failSetBackupRateLimitIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.setBackupRateLimit(1); + public void failSetBackupRateLimitIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.setBackupRateLimit(1); + } } @Test - public void failBackupRateLimitIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.backupRateLimit(); + public void failBackupRateLimitIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.backupRateLimit(); + } } @Test - public void failSetRestoreRateLimitIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.setRestoreRateLimit(1); + public void failSetRestoreRateLimitIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.setRestoreRateLimit(1); + } } @Test - public void failRestoreRateLimitIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.restoreRateLimit(); + public void failRestoreRateLimitIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.restoreRateLimit(); + } } @Test - public void failSetShareFilesWithChecksumIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.setShareFilesWithChecksum(true); + public void failSetShareFilesWithChecksumIfDisposed() { + try (final BackupableDBOptions options = + setupUninitializedBackupableDBOptions(exception)) { + options.setShareFilesWithChecksum(true); + } } @Test - public void failShareFilesWithChecksumIfDisposed(){ - BackupableDBOptions options = setupUninitializedBackupableDBOptions( - exception); - options.shareFilesWithChecksum(); + public void failShareFilesWithChecksumIfDisposed() { + 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; } diff --git a/java/src/test/java/org/rocksdb/BackupableDBTest.java b/java/src/test/java/org/rocksdb/BackupableDBTest.java index b5e2f129c..4675f4198 100644 --- a/java/src/test/java/org/rocksdb/BackupableDBTest.java +++ b/java/src/test/java/org/rocksdb/BackupableDBTest.java @@ -28,74 +28,48 @@ 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()); - // 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(); + 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); } } } @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()); - // Fill database with some test values - prepareDatabase(bdb); - // Create two backups - bdb.createNewBackup(false); - bdb.createNewBackup(true); - List backupInfo = - verifyNumberOfValidBackups(bdb, 2); - // Delete the first backup - bdb.deleteBackup(backupInfo.get(0).backupId()); - List 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(); + 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 = + verifyNumberOfValidBackups(bdb, 2); + // Delete the first backup + bdb.deleteBackup(backupInfo.get(0).backupId()); + final List newBackupInfo = + verifyNumberOfValidBackups(bdb, 1); + // The second backup must remain. + assertThat(newBackupInfo.get(0).backupId()). + isEqualTo(backupInfo.get(1).backupId()); } } } @@ -103,90 +77,61 @@ 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()); - // Fill database with some test values - prepareDatabase(bdb); - // Create two backups - bdb.createNewBackup(false); - bdb.createNewBackup(true); - List backupInfo = - verifyNumberOfValidBackups(bdb, 2); - // init RestoreBackupableDB - rdb = new RestoreBackupableDB(bopt); - // Delete the first backup - rdb.deleteBackup(backupInfo.get(0).backupId()); - // Fetch backup info using RestoreBackupableDB - List newBackupInfo = verifyNumberOfValidBackups(rdb, 1); - // 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(); + 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); + final List backupInfo = + verifyNumberOfValidBackups(bdb, 2); + // init RestoreBackupableDB + try (final RestoreBackupableDB rdb = new RestoreBackupableDB(bopt)) { + // Delete the first backup + rdb.deleteBackup(backupInfo.get(0).backupId()); + // Fetch backup info using RestoreBackupableDB + List newBackupInfo = verifyNumberOfValidBackups(rdb, 1); + // The second backup must remain. + assertThat(newBackupInfo.get(0).backupId()). + isEqualTo(backupInfo.get(1).backupId()); + } } } } @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()); - // Fill database with some test values - prepareDatabase(bdb); - // Create two backups - bdb.createNewBackup(false); - bdb.createNewBackup(true); - bdb.createNewBackup(true); - bdb.createNewBackup(true); - List backupInfo = - verifyNumberOfValidBackups(bdb, 4); - // Delete everything except the latest backup - bdb.purgeOldBackups(1); - List 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(); + 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); + bdb.createNewBackup(true); + bdb.createNewBackup(true); + final List backupInfo = + verifyNumberOfValidBackups(bdb, 4); + // Delete everything except the latest backup + bdb.purgeOldBackups(1); + final List newBackupInfo = + verifyNumberOfValidBackups(bdb, 1); + // The latest backup must remain. + assertThat(newBackupInfo.get(0).backupId()). + isEqualTo(backupInfo.get(3).backupId()); } } } @@ -194,58 +139,43 @@ 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()); - // Fill database with some test values - prepareDatabase(bdb); - // Create two backups - bdb.createNewBackup(false); - bdb.createNewBackup(true); - bdb.createNewBackup(true); - bdb.createNewBackup(true); - List infos = verifyNumberOfValidBackups(bdb, 4); - assertThat(infos.get(1).size()). - isEqualTo(infos.get(2).size()); - assertThat(infos.get(1).numberFiles()). - isEqualTo(infos.get(2).numberFiles()); - long maxTimeBeforePurge = Long.MIN_VALUE; - for (BackupInfo backupInfo : infos) { - if (maxTimeBeforePurge < backupInfo.timestamp()) { - maxTimeBeforePurge = backupInfo.timestamp(); + 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); + bdb.createNewBackup(true); + bdb.createNewBackup(true); + List infos = verifyNumberOfValidBackups(bdb, 4); + assertThat(infos.get(1).size()). + isEqualTo(infos.get(2).size()); + assertThat(infos.get(1).numberFiles()). + isEqualTo(infos.get(2).numberFiles()); + long maxTimeBeforePurge = Long.MIN_VALUE; + for (BackupInfo backupInfo : infos) { + if (maxTimeBeforePurge < backupInfo.timestamp()) { + maxTimeBeforePurge = backupInfo.timestamp(); + } + } + // init RestoreBackupableDB + try (final RestoreBackupableDB rdb = new RestoreBackupableDB(bopt)) { + // the same number of backups must + // exist using RestoreBackupableDB. + verifyNumberOfValidBackups(rdb, 4); + rdb.purgeOldBackups(1); + infos = verifyNumberOfValidBackups(rdb, 1); + assertThat(infos.get(0).timestamp()). + isEqualTo(maxTimeBeforePurge); } - } - // init RestoreBackupableDB - rdb = new RestoreBackupableDB(bopt); - // the same number of backups must - // exist using RestoreBackupableDB. - verifyNumberOfValidBackups(rdb, 4); - rdb.purgeOldBackups(1); - 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,58 +183,44 @@ 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()); - // Fill database with some test values - prepareDatabase(bdb); - bdb.createNewBackup(true); - verifyNumberOfValidBackups(bdb, 1); - bdb.put("key1".getBytes(), "valueV2".getBytes()); - bdb.put("key2".getBytes(), "valueV2".getBytes()); - bdb.createNewBackup(true); - verifyNumberOfValidBackups(bdb, 2); - bdb.put("key1".getBytes(), "valueV3".getBytes()); - 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(); + try (final BackupableDB bdb = BackupableDB.open(opt, bopt, + dbFolder.getRoot().getAbsolutePath())) { + // Fill database with some test values + prepareDatabase(bdb); + bdb.createNewBackup(true); + verifyNumberOfValidBackups(bdb, 1); + bdb.put("key1".getBytes(), "valueV2".getBytes()); + bdb.put("key2".getBytes(), "valueV2".getBytes()); + bdb.createNewBackup(true); + verifyNumberOfValidBackups(bdb, 2); + bdb.put("key1".getBytes(), "valueV3".getBytes()); + bdb.put("key2".getBytes(), "valueV3".getBytes()); + assertThat(new String(bdb.get("key1".getBytes()))).endsWith("V3"); + assertThat(new String(bdb.get("key2".getBytes()))).endsWith("V3"); + } // init RestoreBackupableDB - rdb = new RestoreBackupableDB(bopt); - verifyNumberOfValidBackups(rdb, 2); - // restore db from latest backup - rdb.restoreDBFromLatestBackup(dbFolder.getRoot().getAbsolutePath(), - dbFolder.getRoot().getAbsolutePath(), - new RestoreOptions(false)); + 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()); - // 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(); + 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"); } } } @@ -312,59 +228,44 @@ 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()); - // Fill database with some test values - prepareDatabase(bdb); - bdb.createNewBackup(true); - verifyNumberOfValidBackups(bdb, 1); - bdb.put("key1".getBytes(), "valueV2".getBytes()); - bdb.put("key2".getBytes(), "valueV2".getBytes()); - bdb.createNewBackup(true); - verifyNumberOfValidBackups(bdb, 2); - bdb.put("key1".getBytes(), "valueV3".getBytes()); - 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(); + try (final BackupableDB bdb = BackupableDB.open(opt, bopt, + dbFolder.getRoot().getAbsolutePath())) { + // Fill database with some test values + prepareDatabase(bdb); + bdb.createNewBackup(true); + verifyNumberOfValidBackups(bdb, 1); + bdb.put("key1".getBytes(), "valueV2".getBytes()); + bdb.put("key2".getBytes(), "valueV2".getBytes()); + bdb.createNewBackup(true); + verifyNumberOfValidBackups(bdb, 2); + bdb.put("key1".getBytes(), "valueV3".getBytes()); + bdb.put("key2".getBytes(), "valueV3".getBytes()); + assertThat(new String(bdb.get("key1".getBytes()))).endsWith("V3"); + assertThat(new String(bdb.get("key2".getBytes()))).endsWith("V3"); + } // init RestoreBackupableDB - rdb = new RestoreBackupableDB(bopt); - List backupInfo = verifyNumberOfValidBackups(rdb, 2); - // restore db from first backup - rdb.restoreDBFromBackup(backupInfo.get(0).backupId(), - dbFolder.getRoot().getAbsolutePath(), - dbFolder.getRoot().getAbsolutePath(), - new RestoreOptions(false)); + try (final RestoreBackupableDB rdb = new RestoreBackupableDB(bopt)) { + final List 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()); - // 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(); + 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"); } } } @@ -372,13 +273,13 @@ public class BackupableDBTest { /** * Verify backups. * - * @param bdb {@link BackupableDB} instance. + * @param bdb {@link BackupableDB} instance. * @param expectedNumberOfBackups numerical value * @throws RocksDBException thrown if an error occurs within the native - * part of the library. + * part of the library. */ - private List verifyNumberOfValidBackups(BackupableDB bdb, - int expectedNumberOfBackups) throws RocksDBException { + private List verifyNumberOfValidBackups(final BackupableDB bdb, + final int expectedNumberOfBackups) throws RocksDBException { // Verify that backups exist assertThat(bdb.getCorruptedBackups().length). isEqualTo(0); @@ -392,13 +293,13 @@ public class BackupableDBTest { /** * Verify backups. * - * @param rdb {@link RestoreBackupableDB} instance. + * @param rdb {@link RestoreBackupableDB} instance. * @param expectedNumberOfBackups numerical value * @throws RocksDBException thrown if an error occurs within the native - * part of the library. + * part of the library. */ private List verifyNumberOfValidBackups( - RestoreBackupableDB rdb, int expectedNumberOfBackups) + final RestoreBackupableDB rdb, final int expectedNumberOfBackups) throws RocksDBException { // Verify that backups exist assertThat(rdb.getCorruptedBackups().length). @@ -415,9 +316,9 @@ public class BackupableDBTest { * * @param db {@link RocksDB} instance. * @throws RocksDBException thrown if an error occurs within the native - * part of the library. + * 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()); diff --git a/java/src/test/java/org/rocksdb/BlockBasedTableConfigTest.java b/java/src/test/java/org/rocksdb/BlockBasedTableConfigTest.java index 2b1ce5ffa..ccb7b7625 100644 --- a/java/src/test/java/org/rocksdb/BlockBasedTableConfigTest.java +++ b/java/src/test/java/org/rocksdb/BlockBasedTableConfigTest.java @@ -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(); - } } } diff --git a/java/src/test/java/org/rocksdb/CheckPointTest.java b/java/src/test/java/org/rocksdb/CheckPointTest.java index 3081e585a..e79569fb8 100644 --- a/java/src/test/java/org/rocksdb/CheckPointTest.java +++ b/java/src/test/java/org/rocksdb/CheckPointTest.java @@ -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()); - db.put("key".getBytes(), "value".getBytes()); - 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, - checkpointFolder.getRoot().getAbsolutePath() + - "/snapshot1"); - assertThat(new String(db.get("key".getBytes()))). - isEqualTo("value"); - assertThat(db.get("key2".getBytes())).isNull(); - db.close(); - db = RocksDB.open(options, - checkpointFolder.getRoot().getAbsolutePath() + - "/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(); + try (final Options options = new Options(). + setCreateIfMissing(true)) { + + try (final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { + db.put("key".getBytes(), "value".getBytes()); + 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"); + } } - if (options != null) { - options.dispose(); + + try (final RocksDB db = RocksDB.open(options, + checkpointFolder.getRoot().getAbsolutePath() + + "/snapshot1")) { + assertThat(new String(db.get("key".getBytes()))). + isEqualTo("value"); + assertThat(db.get("key2".getBytes())).isNull(); } - if (checkpoint != null) { - checkpoint.dispose(); + + try (final RocksDB db = RocksDB.open(options, + checkpointFolder.getRoot().getAbsolutePath() + + "/snapshot2")) { + assertThat(new String(db.get("key".getBytes()))). + isEqualTo("value"); + assertThat(new String(db.get("key2".getBytes()))). + isEqualTo("value2"); } } } @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(); - Checkpoint.create(db); + 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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java b/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java index e0ebd67ac..e2ebf13d8 100644 --- a/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java +++ b/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java @@ -26,616 +26,386 @@ public class ColumnFamilyOptionsTest { @Test public void getColumnFamilyOptionsFromProps() { - ColumnFamilyOptions opt = null; - try { + Properties properties = new Properties(); + properties.put("write_buffer_size", "112"); + properties.put("max_write_buffer_number", "13"); + + try (final ColumnFamilyOptions opt = ColumnFamilyOptions. + getColumnFamilyOptionsFromProps(properties)) { // setup sample properties - Properties properties = new Properties(); - properties.put("write_buffer_size", "112"); - properties.put("max_write_buffer_number", "13"); - opt = ColumnFamilyOptions. - getColumnFamilyOptionsFromProps(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(); - properties.put("tomato", "1024"); - properties.put("burger", "2"); - opt = ColumnFamilyOptions. - getColumnFamilyOptionsFromProps(properties); + // setup sample properties + final Properties properties = new Properties(); + properties.put("tomato", "1024"); + properties.put("burger", "2"); + + 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() { - ColumnFamilyOptions.getColumnFamilyOptionsFromProps( - new Properties()); + try (final ColumnFamilyOptions opt = + ColumnFamilyOptions.getColumnFamilyOptionsFromProps( + 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,21 +413,16 @@ 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 compressionTypeList = new ArrayList<>(); - for (int i=0; i < columnFamilyOptions.numLevels(); i++) { + for (int i = 0; i < columnFamilyOptions.numLevels(); i++) { compressionTypeList.add(CompressionType.NO_COMPRESSION); } columnFamilyOptions.setCompressionPerLevel(compressionTypeList); @@ -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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/ColumnFamilyTest.java b/java/src/test/java/org/rocksdb/ColumnFamilyTest.java index af0d71e70..c5b4fe96a 100644 --- a/java/src/test/java/org/rocksdb/ColumnFamilyTest.java +++ b/java/src/test/java/org/rocksdb/ColumnFamilyTest.java @@ -25,430 +25,350 @@ 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 columnFamilyNames; - columnFamilyNames = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); + final List 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 { - options = new Options().setCreateIfMissing(true); + try (final Options options = new Options().setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { + final ColumnFamilyHandle cfh = db.getDefaultColumnFamily(); + try { + assertThat(cfh).isNotNull(); - db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); - cfh = db.getDefaultColumnFamily(); - assertThat(cfh).isNotNull(); + final byte[] key = "key".getBytes(); + final byte[] value = "value".getBytes(); - final byte[] key = "key".getBytes(); - final byte[] value = "value".getBytes(); + db.put(cfh, key, value); - db.put(cfh, key, value); + final byte[] actualValue = db.get(cfh, key); - final byte[] actualValue = db.get(cfh, key); - - assertThat(cfh).isNotNull(); - assertThat(actualValue).isEqualTo(value); - } finally { - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + assertThat(cfh).isNotNull(); + assertThat(actualValue).isEqualTo(value); + } finally { + cfh.close(); } } } @Test public void createColumnFamily() throws RocksDBException { - RocksDB db = null; - Options options = null; - ColumnFamilyHandle columnFamilyHandle = null; - 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 columnFamilyNames; - 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(); + 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 { + final List 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 { + columnFamilyHandle.close(); } } } @Test public void openWithColumnFamilies() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfNames = - new ArrayList<>(); - List 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())); + final List cfNames = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes()) + ); - db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), - cfNames, columnFamilyHandleList); - assertThat(columnFamilyHandleList.size()).isEqualTo(2); - db.put("dfkey1".getBytes(), "dfvalue".getBytes()); - db.put(columnFamilyHandleList.get(0), "dfkey2".getBytes(), - "dfvalue".getBytes()); - db.put(columnFamilyHandleList.get(1), "newcfkey1".getBytes(), - "newcfvalue".getBytes()); + final List columnFamilyHandleList = + new ArrayList<>(); - String retVal = new String(db.get(columnFamilyHandleList.get(1), - "newcfkey1".getBytes())); - assertThat(retVal).isEqualTo("newcfvalue"); - assertThat((db.get(columnFamilyHandleList.get(1), - "dfkey1".getBytes()))).isNull(); - db.remove(columnFamilyHandleList.get(1), "newcfkey1".getBytes()); - assertThat((db.get(columnFamilyHandleList.get(1), - "newcfkey1".getBytes()))).isNull(); - db.remove(columnFamilyHandleList.get(0), new WriteOptions(), - "dfkey2".getBytes()); - assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(), - "dfkey2".getBytes())).isNull(); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + // Test open database with column family names + try (final DBOptions options = new DBOptions() + .setCreateIfMissing(true) + .setCreateMissingColumnFamilies(true); + final RocksDB 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(), + "dfvalue".getBytes()); + db.put(columnFamilyHandleList.get(1), "newcfkey1".getBytes(), + "newcfvalue".getBytes()); + + String retVal = new String(db.get(columnFamilyHandleList.get(1), + "newcfkey1".getBytes())); + assertThat(retVal).isEqualTo("newcfvalue"); + assertThat((db.get(columnFamilyHandleList.get(1), + "dfkey1".getBytes()))).isNull(); + db.remove(columnFamilyHandleList.get(1), "newcfkey1".getBytes()); + assertThat((db.get(columnFamilyHandleList.get(1), + "newcfkey1".getBytes()))).isNull(); + db.remove(columnFamilyHandleList.get(0), new WriteOptions(), + "dfkey2".getBytes()); + assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(), + "dfkey2".getBytes())).isNull(); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test public void getWithOutValueAndCf() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfDescriptors = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - try { - options = new DBOptions(); - options.setCreateIfMissing(true); - options.setCreateMissingColumnFamilies(true); - // Test open database with column family names - cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); - db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), - cfDescriptors, columnFamilyHandleList); - db.put(columnFamilyHandleList.get(0), new WriteOptions(), - "key1".getBytes(), "value".getBytes()); - db.put("key2".getBytes(), "12345678".getBytes()); - 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); - assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); - assertThat(outValue).isEqualTo("value".getBytes()); - // found value which fits partially - getResult = db.get(columnFamilyHandleList.get(0), new ReadOptions(), - "key2".getBytes(), outValue); - assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); - assertThat(outValue).isEqualTo("12345".getBytes()); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + final List columnFamilyHandleList = new ArrayList<>(); + + // Test open database with column family names + 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()); + 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); + assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); + assertThat(outValue).isEqualTo("value".getBytes()); + // found value which fits partially + getResult = db.get(columnFamilyHandleList.get(0), new ReadOptions(), + "key2".getBytes(), outValue); + assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); + assertThat(outValue).isEqualTo("12345".getBytes()); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test public void createWriteDropColumnFamily() throws RocksDBException { - RocksDB db = null; - DBOptions opt = null; - ColumnFamilyHandle tmpColumnFamilyHandle = null; - List cfNames = - new ArrayList<>(); - List 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())); - db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); - db.dropColumnFamily(tmpColumnFamilyHandle); - tmpColumnFamilyHandle.dispose(); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (tmpColumnFamilyHandle != null) { - tmpColumnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List 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; + try { + tmpColumnFamilyHandle = db.createColumnFamily( + new ColumnFamilyDescriptor("tmpCF".getBytes(), + new ColumnFamilyOptions())); + db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); + db.dropColumnFamily(tmpColumnFamilyHandle); + } finally { + if (tmpColumnFamilyHandle != null) { + tmpColumnFamilyHandle.close(); + } + for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test public void writeBatch() throws RocksDBException { - RocksDB db = null; - DBOptions opt = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - try { - opt = new DBOptions(); - opt.setCreateIfMissing(true); - opt.setCreateMissingColumnFamilies(true); + try (final ColumnFamilyOptions defaultCfOptions = new ColumnFamilyOptions() + .setMergeOperator(new StringAppendOperator())) { + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, + defaultCfOptions), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List 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 { + writeBatch.put("key".getBytes(), "value".getBytes()); + writeBatch.put(db.getDefaultColumnFamily(), + "mergeKey".getBytes(), "merge".getBytes()); + writeBatch.merge(db.getDefaultColumnFamily(), "mergeKey".getBytes(), + "merge".getBytes()); + writeBatch.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), + "value".getBytes()); + writeBatch.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), + "value2".getBytes()); + writeBatch.remove("xyz".getBytes()); + writeBatch.remove(columnFamilyHandleList.get(1), "xyz".getBytes()); + db.write(writeOpt, writeBatch); - 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()); - writeBatch.merge(db.getDefaultColumnFamily(), "mergeKey".getBytes(), - "merge".getBytes()); - writeBatch.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), - "value".getBytes()); - writeBatch.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), - "value2".getBytes()); - 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), - "newcfkey".getBytes()))).isEqualTo("value"); - assertThat(new String(db.get(columnFamilyHandleList.get(1), - "newcfkey2".getBytes()))).isEqualTo("value2"); - assertThat(new String(db.get("key".getBytes()))).isEqualTo("value"); - // check if key is merged - assertThat(new String(db.get(db.getDefaultColumnFamily(), - "mergeKey".getBytes()))).isEqualTo("merge,merge"); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); + assertThat(db.get(columnFamilyHandleList.get(1), + "xyz".getBytes()) == null); + assertThat(new String(db.get(columnFamilyHandleList.get(1), + "newcfkey".getBytes()))).isEqualTo("value"); + assertThat(new String(db.get(columnFamilyHandleList.get(1), + "newcfkey2".getBytes()))).isEqualTo("value2"); + assertThat(new String(db.get("key".getBytes()))).isEqualTo("value"); + // check if key is merged + assertThat(new String(db.get(db.getDefaultColumnFamily(), + "mergeKey".getBytes()))).isEqualTo("merge,merge"); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } @Test public void iteratorOnColumnFamily() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - RocksIterator rocksIterator = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - try { - options = new DBOptions(); - options.setCreateIfMissing(true); - options.setCreateMissingColumnFamilies(true); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List 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 { - 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)); - rocksIterator.seekToFirst(); - Map refMap = new HashMap<>(); - refMap.put("newcfkey", "value"); - refMap.put("newcfkey2", "value2"); - int i = 0; - while (rocksIterator.isValid()) { - i++; - assertThat(refMap.get(new String(rocksIterator.key()))). - isEqualTo(new String(rocksIterator.value())); - 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(); - } - if (options != null) { - options.dispose(); + db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), + "value".getBytes()); + db.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), + "value2".getBytes()); + try (final RocksIterator rocksIterator = + db.newIterator(columnFamilyHandleList.get(1))) { + rocksIterator.seekToFirst(); + Map refMap = new HashMap<>(); + refMap.put("newcfkey", "value"); + refMap.put("newcfkey2", "value2"); + int i = 0; + while (rocksIterator.isValid()) { + i++; + assertThat(refMap.get(new String(rocksIterator.key()))). + isEqualTo(new String(rocksIterator.value())); + rocksIterator.next(); + } + assertThat(i).isEqualTo(2); + } + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test public void multiGet() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfDescriptors = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - try { - options = new DBOptions(); - options.setCreateIfMissing(true); - options.setCreateMissingColumnFamilies(true); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List 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 { + db.put(columnFamilyHandleList.get(0), "key".getBytes(), + "value".getBytes()); + db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), + "value".getBytes()); - 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()); - - List keys = Arrays.asList(new byte[][]{"key".getBytes(), "newcfkey".getBytes()}); - Map 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); - 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(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + final List keys = Arrays.asList(new byte[][]{ + "key".getBytes(), "newcfkey".getBytes() + }); + Map 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); + 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 (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test public void properties() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - 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), - "rocksdb.estimate-num-keys")).isGreaterThanOrEqualTo(0); - assertThat(db.getProperty("rocksdb.stats")).isNotNull(); - assertThat(db.getProperty(columnFamilyHandleList.get(0), - "rocksdb.sstables")).isNotNull(); - assertThat(db.getProperty(columnFamilyHandleList.get(1), - "rocksdb.estimate-num-keys")).isNotNull(); - assertThat(db.getProperty(columnFamilyHandleList.get(1), - "rocksdb.stats")).isNotNull(); - assertThat(db.getProperty(columnFamilyHandleList.get(1), - "rocksdb.sstables")).isNotNull(); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List 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 { + assertThat(db.getProperty("rocksdb.estimate-num-keys")). + isNotNull(); + assertThat(db.getLongProperty(columnFamilyHandleList.get(0), + "rocksdb.estimate-num-keys")).isGreaterThanOrEqualTo(0); + assertThat(db.getProperty("rocksdb.stats")).isNotNull(); + assertThat(db.getProperty(columnFamilyHandleList.get(0), + "rocksdb.sstables")).isNotNull(); + assertThat(db.getProperty(columnFamilyHandleList.get(1), + "rocksdb.estimate-num-keys")).isNotNull(); + assertThat(db.getProperty(columnFamilyHandleList.get(1), + "rocksdb.stats")).isNotNull(); + assertThat(db.getProperty(columnFamilyHandleList.get(1), + "rocksdb.sstables")).isNotNull(); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @@ -456,289 +376,230 @@ public class ColumnFamilyTest { @Test public void iterators() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - List 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 defRefMap = new HashMap<>(); - defRefMap.put("dfkey1", "dfvalue"); - defRefMap.put("key", "value"); - while (iter.isValid()) { - assertThat(defRefMap.get(new String(iter.key()))). - isEqualTo(new String(iter.value())); - iter.next(); - } - // iterate over new_cf key/value pairs - Map cfRefMap = new HashMap<>(); - cfRefMap.put("newcfkey", "value"); - cfRefMap.put("newcfkey2", "value2"); - iter = iterators.get(1); - iter.seekToFirst(); - while (iter.isValid()) { - assertThat(cfRefMap.get(new String(iter.key()))). - isEqualTo(new String(iter.value())); - iter.next(); - } - } finally { - if (iterators != null) { - for (RocksIterator rocksIterator : iterators) { - rocksIterator.dispose(); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List 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 iterators = null; + try { + iterators = db.newIterators(columnFamilyHandleList); + assertThat(iterators.size()).isEqualTo(2); + RocksIterator iter = iterators.get(0); + iter.seekToFirst(); + final Map defRefMap = new HashMap<>(); + defRefMap.put("dfkey1", "dfvalue"); + defRefMap.put("key", "value"); + while (iter.isValid()) { + assertThat(defRefMap.get(new String(iter.key()))). + isEqualTo(new String(iter.value())); + iter.next(); + } + // iterate over new_cf key/value pairs + final Map cfRefMap = new HashMap<>(); + cfRefMap.put("newcfkey", "value"); + cfRefMap.put("newcfkey2", "value2"); + iter = iterators.get(1); + iter.seekToFirst(); + while (iter.isValid()) { + assertThat(cfRefMap.get(new String(iter.key()))). + isEqualTo(new String(iter.value())); + iter.next(); + } + } finally { + if (iterators != null) { + for (final RocksIterator rocksIterator : iterators) { + rocksIterator.close(); + } + } + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); } - } - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); } } } @Test(expected = RocksDBException.class) public void failPutDisposedCF() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - 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()); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + try (final DBOptions options = new DBOptions() + .setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath(), + cfDescriptors, columnFamilyHandleList)) { + try { + db.dropColumnFamily(columnFamilyHandleList.get(1)); + db.put(columnFamilyHandleList.get(1), "key".getBytes(), + "value".getBytes()); + } finally { + for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test(expected = RocksDBException.class) public void failRemoveDisposedCF() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - 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(); - } - if (options != null) { - options.dispose(); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + try (final DBOptions options = new DBOptions() + .setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath(), + cfDescriptors, columnFamilyHandleList)) { + try { + db.dropColumnFamily(columnFamilyHandleList.get(1)); + db.remove(columnFamilyHandleList.get(1), "key".getBytes()); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test(expected = RocksDBException.class) public void failGetDisposedCF() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - 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(); - } - if (options != null) { - options.dispose(); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + try (final DBOptions options = new DBOptions() + .setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath(), cfDescriptors, + columnFamilyHandleList)) { + try { + db.dropColumnFamily(columnFamilyHandleList.get(1)); + db.get(columnFamilyHandleList.get(1), "key".getBytes()); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test(expected = RocksDBException.class) public void failMultiGetWithoutCorrectNumberOfCF() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - try { - options = new DBOptions(); - options.setCreateIfMissing(true); + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + try (final DBOptions options = new DBOptions() + .setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath(), cfDescriptors, + columnFamilyHandleList)) { + try { + final List keys = new ArrayList<>(); + keys.add("key".getBytes()); + keys.add("newcfkey".getBytes()); + final List cfCustomList = new ArrayList<>(); + db.multiGet(cfCustomList, keys); - 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 keys = new ArrayList<>(); - keys.add("key".getBytes()); - keys.add("newcfkey".getBytes()); - List cfCustomList = new ArrayList<>(); - db.multiGet(cfCustomList, keys); - - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @Test public void testByteCreateFolumnFamily() throws RocksDBException { - RocksDB db = null; - Options options = null; - 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 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(); - } - if (cf2 != null) { - cf2.dispose(); - } - if (cf3 != null) { - cf3.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 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 { + cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0)); + cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1)); + final List 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.close(); + } + if (cf2 != null) { + cf2.close(); + } + if (cf3 != null) { + cf3.close(); + } } } } @Test public void testCFNamesWithZeroBytes() throws RocksDBException { - RocksDB db = null; - Options options = null; ColumnFamilyHandle cf1 = null, cf2 = null; - 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 }; - cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0)); - cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1)); - List families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); - assertThat(families).contains("default".getBytes(), b0, b1); - } finally { - if (cf1 != null) { - cf1.dispose(); - } - if (cf2 != null) { - cf2.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()); + ) { + try { + 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)); + final List families = RocksDB.listColumnFamilies(options, + dbFolder.getRoot().getAbsolutePath()); + assertThat(families).contains("default".getBytes(), b0, b1); + } finally { + if (cf1 != null) { + cf1.close(); + } + if (cf2 != null) { + cf2.close(); + } } } } @Test public void testCFNameSimplifiedChinese() throws RocksDBException { - RocksDB db = null; - Options options = null; ColumnFamilyHandle columnFamilyHandle = null; - 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())); + try (final Options options = new Options().setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath()); + ) { + try { + final String simplifiedChinese = "\u7b80\u4f53\u5b57"; + columnFamilyHandle = db.createColumnFamily( + new ColumnFamilyDescriptor(simplifiedChinese.getBytes())); - List families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); - assertThat(families).contains("default".getBytes(), simplifiedChinese.getBytes()); - } finally { - if (columnFamilyHandle != null) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + final List families = RocksDB.listColumnFamilies(options, + dbFolder.getRoot().getAbsolutePath()); + assertThat(families).contains("default".getBytes(), + simplifiedChinese.getBytes()); + } finally { + if (columnFamilyHandle != null) { + columnFamilyHandle.close(); + } } } - - } } diff --git a/java/src/test/java/org/rocksdb/ComparatorOptionsTest.java b/java/src/test/java/org/rocksdb/ComparatorOptionsTest.java index 2a86515e3..fcdd09acb 100644 --- a/java/src/test/java/org/rocksdb/ComparatorOptionsTest.java +++ b/java/src/test/java/org/rocksdb/ComparatorOptionsTest.java @@ -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 + assertThat(copt).isNotNull(); + // UseAdaptiveMutex test copt.setUseAdaptiveMutex(true); assertThat(copt.useAdaptiveMutex()).isTrue(); copt.setUseAdaptiveMutex(false); assertThat(copt.useAdaptiveMutex()).isFalse(); } - - copt.dispose(); } } diff --git a/java/src/test/java/org/rocksdb/ComparatorTest.java b/java/src/test/java/org/rocksdb/ComparatorTest.java index d4cea0cb8..b34821844 100644 --- a/java/src/test/java/org/rocksdb/ComparatorTest.java +++ b/java/src/test/java/org/rocksdb/ComparatorTest.java @@ -79,66 +79,52 @@ 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()); + 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()); - rocksDB.put("abc1".getBytes(), "abc1".getBytes()); - rocksDB.put("abc2".getBytes(), "abc2".getBytes()); - rocksDB.put("abc3".getBytes(), "abc3".getBytes()); - - rocksIterator = rocksDB.newIterator(); - // Iterate over keys using a iterator - rocksIterator.seekToFirst(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc1".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc1".getBytes()); - rocksIterator.next(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc2".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc2".getBytes()); - rocksIterator.next(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc3".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc3".getBytes()); - rocksIterator.next(); - assertThat(rocksIterator.isValid()).isFalse(); - // Get last one - rocksIterator.seekToLast(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc3".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc3".getBytes()); - // Seek for abc - rocksIterator.seek("abc".getBytes()); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc1".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc1".getBytes()); - - } finally { - if (rocksIterator != null) { - rocksIterator.dispose(); - } - if (rocksDB != null) { - rocksDB.close(); - } - if (options != null) { - options.dispose(); + try(final RocksIterator rocksIterator = rocksDb.newIterator()) { + // Iterate over keys using a iterator + rocksIterator.seekToFirst(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc1".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc1".getBytes()); + rocksIterator.next(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc2".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc2".getBytes()); + rocksIterator.next(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc3".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc3".getBytes()); + rocksIterator.next(); + assertThat(rocksIterator.isValid()).isFalse(); + // Get last one + rocksIterator.seekToLast(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc3".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc3".getBytes()); + // Seek for abc + rocksIterator.seek("abc".getBytes()); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc1".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc1".getBytes()); } } } @@ -146,69 +132,56 @@ 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()); + 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()); + rocksDb.put("abc1".getBytes(), "abc1".getBytes()); + rocksDb.put("abc2".getBytes(), "abc2".getBytes()); + rocksDb.put("abc3".getBytes(), "abc3".getBytes()); - rocksIterator = rocksDB.newIterator(); - // Iterate over keys using a iterator - rocksIterator.seekToFirst(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc3".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc3".getBytes()); - rocksIterator.next(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc2".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc2".getBytes()); - rocksIterator.next(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc1".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc1".getBytes()); - rocksIterator.next(); - assertThat(rocksIterator.isValid()).isFalse(); - // Get last one - rocksIterator.seekToLast(); - assertThat(rocksIterator.isValid()).isTrue(); - assertThat(rocksIterator.key()).isEqualTo( - "abc1".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc1".getBytes()); - // Will be invalid because abc is after abc1 - rocksIterator.seek("abc".getBytes()); - assertThat(rocksIterator.isValid()).isFalse(); - // Will be abc3 because the next one after abc999 - // is abc3 - rocksIterator.seek("abc999".getBytes()); - assertThat(rocksIterator.key()).isEqualTo( - "abc3".getBytes()); - assertThat(rocksIterator.value()).isEqualTo( - "abc3".getBytes()); - } finally { - if (rocksIterator != null) { - rocksIterator.dispose(); - } - if (rocksDB != null) { - rocksDB.close(); - } - if (options != null) { - options.dispose(); + try (final RocksIterator rocksIterator = rocksDb.newIterator()) { + // Iterate over keys using a iterator + rocksIterator.seekToFirst(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc3".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc3".getBytes()); + rocksIterator.next(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc2".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc2".getBytes()); + rocksIterator.next(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc1".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc1".getBytes()); + rocksIterator.next(); + assertThat(rocksIterator.isValid()).isFalse(); + // Get last one + rocksIterator.seekToLast(); + assertThat(rocksIterator.isValid()).isTrue(); + assertThat(rocksIterator.key()).isEqualTo( + "abc1".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc1".getBytes()); + // Will be invalid because abc is after abc1 + rocksIterator.seek("abc".getBytes()); + assertThat(rocksIterator.isValid()).isFalse(); + // Will be abc3 because the next one after abc999 + // is abc3 + rocksIterator.seek("abc999".getBytes()); + assertThat(rocksIterator.key()).isEqualTo( + "abc3".getBytes()); + assertThat(rocksIterator.value()).isEqualTo( + "abc3".getBytes()); } } } diff --git a/java/src/test/java/org/rocksdb/CompressionOptionsTest.java b/java/src/test/java/org/rocksdb/CompressionOptionsTest.java index 2e2633524..51b7259f6 100644 --- a/java/src/test/java/org/rocksdb/CompressionOptionsTest.java +++ b/java/src/test/java/org/rocksdb/CompressionOptionsTest.java @@ -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)); diff --git a/java/src/test/java/org/rocksdb/DBOptionsTest.java b/java/src/test/java/org/rocksdb/DBOptionsTest.java index 7cb29a4a5..523e53784 100644 --- a/java/src/test/java/org/rocksdb/DBOptionsTest.java +++ b/java/src/test/java/org/rocksdb/DBOptionsTest.java @@ -24,547 +24,339 @@ public class DBOptionsTest { @Test public void getDBOptionsFromProps() { - DBOptions opt = null; - try { - // setup sample properties - Properties properties = new Properties(); - properties.put("allow_mmap_reads", "true"); - properties.put("bytes_per_sync", "13"); - opt = DBOptions.getDBOptionsFromProps(properties); + // setup sample properties + final Properties properties = new Properties(); + properties.put("allow_mmap_reads", "true"); + properties.put("bytes_per_sync", "13"); + 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(); - properties.put("tomato", "1024"); - properties.put("burger", "2"); - opt = DBOptions. - getDBOptionsFromProps(properties); + // setup sample properties + final Properties properties = new Properties(); + properties.put("tomato", "1024"); + properties.put("burger", "2"); + 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(); - Statistics statistics = options.createStatistics(). - statisticsPtr(); - assertThat(statistics).isNotNull(); + try(final DBOptions options = new DBOptions()) { + Statistics statistics = options.createStatistics(). + statisticsPtr(); + assertThat(statistics).isNotNull(); - DBOptions anotherOptions = new DBOptions(); - statistics = anotherOptions.statisticsPtr(); - assertThat(statistics).isNotNull(); + try(final DBOptions anotherOptions = new DBOptions()) { + statistics = anotherOptions.statisticsPtr(); + assertThat(statistics).isNotNull(); + } + } } } diff --git a/java/src/test/java/org/rocksdb/DirectSliceTest.java b/java/src/test/java/org/rocksdb/DirectSliceTest.java index 615adab38..2d3abea45 100644 --- a/java/src/test/java/org/rocksdb/DirectSliceTest.java +++ b/java/src/test/java/org/rocksdb/DirectSliceTest.java @@ -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); - buffer.put(data); - buffer.put(data.length, (byte)0); + 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); - buffer.put(data); - directSlice = new DirectSlice(buffer, 4); + final byte[] data = "Some text".getBytes(); + final ByteBuffer buffer = ByteBuffer.allocateDirect(data.length); + buffer.put(data); + 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 } } } diff --git a/java/src/test/java/org/rocksdb/FilterTest.java b/java/src/test/java/org/rocksdb/FilterTest.java index d5a1830b3..e5bb60fda 100644 --- a/java/src/test/java/org/rocksdb/FilterTest.java +++ b/java/src/test/java/org/rocksdb/FilterTest.java @@ -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); - blockConfig.setFilter(bloomFilter); - options.setTableFormatConfig(blockConfig); - System.gc(); - System.runFinalization(); - blockConfig.setFilter(new BloomFilter(10, false)); - options.setTableFormatConfig(blockConfig); + // new Bloom filter + final BlockBasedTableConfig blockConfig = new BlockBasedTableConfig(); + try(final Options options = new Options()) { - } finally { - if (options != null) { - options.dispose(); + try(final Filter bloomFilter = new BloomFilter()) { + blockConfig.setFilter(bloomFilter); + options.setTableFormatConfig(blockConfig); + } + + try(final Filter bloomFilter = new BloomFilter(10)) { + blockConfig.setFilter(bloomFilter); + options.setTableFormatConfig(blockConfig); + } + + try(final Filter bloomFilter = new BloomFilter(10, false)) { + blockConfig.setFilter(bloomFilter); + options.setTableFormatConfig(blockConfig); } } } diff --git a/java/src/test/java/org/rocksdb/FlushTest.java b/java/src/test/java/org/rocksdb/FlushTest.java index 094910f27..f3530292a 100644 --- a/java/src/test/java/org/rocksdb/FlushTest.java +++ b/java/src/test/java/org/rocksdb/FlushTest.java @@ -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()); - 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"); - 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(); - } - if (wOpt != null) { - wOpt.dispose(); - } + 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"); + db.flush(flushOptions); + assertThat(db.getProperty("rocksdb.num-entries-active-mem-table")) + .isEqualTo("0"); + } } } } diff --git a/java/src/test/java/org/rocksdb/InfoLogLevelTest.java b/java/src/test/java/org/rocksdb/InfoLogLevelTest.java index 71a032a0b..48ecfa16a 100644 --- a/java/src/test/java/org/rocksdb/InfoLogLevelTest.java +++ b/java/src/test/java/org/rocksdb/InfoLogLevelTest.java @@ -24,81 +24,52 @@ 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, + public void testFatalLogLevel() throws RocksDBException, IOException { - RocksDB db = null; - Options options = null; - try { - options = new Options(). - setCreateIfMissing(true). - setInfoLogLevel(InfoLogLevel.FATAL_LEVEL); + 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(). - setInfoLogLevel(InfoLogLevel.FATAL_LEVEL); - options = new Options(dbOptions, - new ColumnFamilyOptions()). - setCreateIfMissing(true); + try (final DBOptions dbOptions = new DBOptions(). + setInfoLogLevel(InfoLogLevel.FATAL_LEVEL); + 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(); - } } } @Test(expected = IllegalArgumentException.class) public void failIfIllegalByteValueProvided() { - InfoLogLevel.getInfoLogLevel((byte)-1); + InfoLogLevel.getInfoLogLevel((byte) -1); } @Test @@ -114,9 +85,10 @@ 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); + dbFolder.getRoot().getAbsolutePath() + "/LOG"))).split(separator); int first_non_header = lines.length; // Identify the last line of the header diff --git a/java/src/test/java/org/rocksdb/KeyMayExistTest.java b/java/src/test/java/org/rocksdb/KeyMayExistTest.java index a39ddbb21..bc341c9d2 100644 --- a/java/src/test/java/org/rocksdb/KeyMayExistTest.java +++ b/java/src/test/java/org/rocksdb/KeyMayExistTest.java @@ -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,70 +26,61 @@ public class KeyMayExistTest { @Test public void keyMayExist() throws RocksDBException { - RocksDB db = null; - DBOptions options = null; - List cfDescriptors = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - try { - options = new DBOptions(); - options.setCreateIfMissing(true) - .setCreateMissingColumnFamilies(true); - // open database using cf names + final List 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, - dbFolder.getRoot().getAbsolutePath(), - cfDescriptors, columnFamilyHandleList); - assertThat(columnFamilyHandleList.size()). - isEqualTo(2); - db.put("key".getBytes(), "value".getBytes()); - // Test without column family - StringBuffer retValue = new StringBuffer(); - boolean exists = db.keyMayExist("key".getBytes(), retValue); - assertThat(exists).isTrue(); - assertThat(retValue.toString()). - isEqualTo("value"); + final List 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 { + assertThat(columnFamilyHandleList.size()). + isEqualTo(2); + db.put("key".getBytes(), "value".getBytes()); + // Test without column family + StringBuffer retValue = new StringBuffer(); + boolean exists = db.keyMayExist("key".getBytes(), retValue); + assertThat(exists).isTrue(); + assertThat(retValue.toString()).isEqualTo("value"); - // Test without column family but with readOptions - retValue = new StringBuffer(); - exists = db.keyMayExist(new ReadOptions(), "key".getBytes(), - retValue); - assertThat(exists).isTrue(); - assertThat(retValue.toString()). - isEqualTo("value"); + // Test without column family but with readOptions + try (final ReadOptions readOptions = new ReadOptions()) { + retValue = new StringBuffer(); + exists = db.keyMayExist(readOptions, "key".getBytes(), retValue); + assertThat(exists).isTrue(); + 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"); + // Test with column family + retValue = new StringBuffer(); + exists = db.keyMayExist(columnFamilyHandleList.get(0), "key".getBytes(), + retValue); + assertThat(exists).isTrue(); + assertThat(retValue.toString()).isEqualTo("value"); - // Test with column family and readOptions - retValue = new StringBuffer(); - exists = db.keyMayExist(new ReadOptions(), - columnFamilyHandleList.get(0), "key".getBytes(), - retValue); - assertThat(exists).isTrue(); - assertThat(retValue.toString()). - isEqualTo("value"); + // Test with column family and readOptions + try (final ReadOptions readOptions = new ReadOptions()) { + retValue = new StringBuffer(); + exists = db.keyMayExist(readOptions, + columnFamilyHandleList.get(0), "key".getBytes(), + retValue); + assertThat(exists).isTrue(); + 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(); - } - if (options != null) { - options.dispose(); + // KeyMayExist in CF1 must return false + assertThat(db.keyMayExist(columnFamilyHandleList.get(1), + "key".getBytes(), retValue)).isFalse(); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } diff --git a/java/src/test/java/org/rocksdb/LoggerTest.java b/java/src/test/java/org/rocksdb/LoggerTest.java index 2eff3191a..3e6d359be 100644 --- a/java/src/test/java/org/rocksdb/LoggerTest.java +++ b/java/src/test/java/org/rocksdb/LoggerTest.java @@ -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,202 +20,165 @@ 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(). - setInfoLogLevel(InfoLogLevel.DEBUG_LEVEL). - setCreateIfMissing(true); - - // 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(); - } - }; - + 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 + @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()); - - // there should be more than zero received log messages in - // debug level. - assertThat(logMessageCounter.get()).isGreaterThan(0); - } finally { - if (db != null) { - db.close(); + 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); } } - logMessageCounter.set(0); } @Test public void fatalLogger() throws RocksDBException { - RocksDB db = null; - logMessageCounter.set(0); + final AtomicInteger logMessageCounter = new AtomicInteger(); + try (final Options options = new Options(). + setInfoLogLevel(InfoLogLevel.FATAL_LEVEL). + setCreateIfMissing(true); - try { - // Setup options - 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) { - @Override - protected void log(InfoLogLevel infoLogLevel, String logMsg) { - assertThat(logMsg).isNotNull(); - assertThat(logMsg.length()).isGreaterThan(0); - logMessageCounter.incrementAndGet(); - } - }; + final Logger logger = new Logger(options) { + // Create new logger with max log level passed by 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()); - - // there should be zero messages - // using fatal level as log level. - assertThat(logMessageCounter.get()).isEqualTo(0); - } finally { - if (db != null) { - db.close(); + 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); } } - logMessageCounter.set(0); } @Test public void dbOptionsLogger() throws RocksDBException { - RocksDB db = null; - Logger logger = null; - List cfHandles = new ArrayList<>(); - List cfDescriptors = new ArrayList<>(); - cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); - - logMessageCounter.set(0); - try { - // Setup options - final DBOptions options = new DBOptions(). - setInfoLogLevel(InfoLogLevel.FATAL_LEVEL). - setCreateIfMissing(true); - - // 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(); - } - }; - + 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 + @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); - // 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(); - } - if (db != null) { - db.close(); - } - if (logger != null) { - logger.dispose(); + + final List cfDescriptors = + Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + final List 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); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : cfHandles) { + columnFamilyHandle.close(); + } + } } } } @Test public void setInfoLogLevel() { - Logger logger = null; - try { - // Setup options - final Options options = new Options(). - setInfoLogLevel(InfoLogLevel.FATAL_LEVEL). - setCreateIfMissing(true); - - // 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(); - } - }; + 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 + @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(). - setInfoLogLevel(InfoLogLevel.FATAL_LEVEL). - setCreateIfMissing(true); - - // 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(); - } - }; + 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 + 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()); - // there should be zero messages - // using fatal level as log level. - assertThat(logMessageCounter.get()).isEqualTo(0); + try (final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { - // change log level to debug level - logger.setInfoLogLevel(InfoLogLevel.DEBUG_LEVEL); + // there should be zero messages + // using fatal level as log level. + assertThat(logMessageCounter.get()).isEqualTo(0); - db.put("key".getBytes(), "value".getBytes()); - db.flush(new FlushOptions().setWaitForFlush(true)); + // change log level to debug level + logger.setInfoLogLevel(InfoLogLevel.DEBUG_LEVEL); - // messages shall be received due to previous actions. - assertThat(logMessageCounter.get()).isNotEqualTo(0); + db.put("key".getBytes(), "value".getBytes()); + db.flush(new FlushOptions().setWaitForFlush(true)); - } finally { - if (db != null) { - db.close(); + // messages shall be received due to previous actions. + assertThat(logMessageCounter.get()).isNotEqualTo(0); } } - logMessageCounter.set(0); } } diff --git a/java/src/test/java/org/rocksdb/MemTableTest.java b/java/src/test/java/org/rocksdb/MemTableTest.java index b54f583d0..bbd5e2055 100644 --- a/java/src/test/java/org/rocksdb/MemTableTest.java +++ b/java/src/test/java/org/rocksdb/MemTableTest.java @@ -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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/MergeTest.java b/java/src/test/java/org/rocksdb/MergeTest.java index 9eec4e1eb..d38df3195 100644 --- a/java/src/test/java/org/rocksdb/MergeTest.java +++ b/java/src/test/java/org/rocksdb/MergeTest.java @@ -5,6 +5,7 @@ package org.rocksdb; +import java.util.Arrays; import java.util.List; import java.util.ArrayList; @@ -27,78 +28,60 @@ 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 columnFamilyHandleList = - new ArrayList<>(); - try { - String db_path_string = - dbFolder.getRoot().getAbsolutePath(); - opt = new DBOptions(); - opt.setCreateIfMissing(true); - opt.setCreateMissingColumnFamilies(true); - List 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 cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt1), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt2) + ); - // 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()); + final List 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()); + // merge bb under key + db.merge(columnFamilyHandleList.get(1), + "cfkey".getBytes(), "bb".getBytes()); - byte[] value = db.get(columnFamilyHandleList.get(1), "cfkey".getBytes()); - String strValue = new String(value); - assertThat(strValue).isEqualTo("aa,bb"); - } finally { - for (ColumnFamilyHandle handle : columnFamilyHandleList) { - handle.dispose(); - } - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); + byte[] value = db.get(columnFamilyHandleList.get(1), + "cfkey".getBytes()); + String strValue = new String(value); + assertThat(strValue).isEqualTo("aa,bb"); + } finally { + for (final ColumnFamilyHandle handle : columnFamilyHandleList) { + handle.close(); + } + } } } } @@ -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 cfDescriptors = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - try { - String db_path_string = - dbFolder.getRoot().getAbsolutePath(); - opt = new DBOptions(); - opt.setCreateIfMissing(true); - opt.setCreateMissingColumnFamilies(true); - StringAppendOperator stringAppendOperator = new StringAppendOperator(); + final StringAppendOperator stringAppendOperator = + new StringAppendOperator(); + try (final ColumnFamilyOptions cfOpt1 = new ColumnFamilyOptions() + .setMergeOperator(stringAppendOperator); + final ColumnFamilyOptions cfOpt2 = new ColumnFamilyOptions() + .setMergeOperator(stringAppendOperator) + ) { + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt1), + new ColumnFamilyDescriptor("new_cf".getBytes(), cfOpt2) + ); + final List 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()); + // merge bb under key + db.merge(columnFamilyHandleList.get(1), + "cfkey".getBytes(), "bb".getBytes()); + byte[] value = db.get(columnFamilyHandleList.get(1), + "cfkey".getBytes()); + String strValue = new String(value); - 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); + // Test also with createColumnFamily + try (final ColumnFamilyOptions cfHandleOpts = + new ColumnFamilyOptions() + .setMergeOperator(stringAppendOperator); + final ColumnFamilyHandle cfHandle = + db.createColumnFamily( + new ColumnFamilyDescriptor("new_cf2".getBytes(), + 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()); + value = db.get(cfHandle, "cfkey2".getBytes()); + String strValueTmpCf = new String(value); - // 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()); - String strValue = new String(value); - - // Test also with createColumnFamily - cfHandle = db.createColumnFamily( - new ColumnFamilyDescriptor("new_cf2".getBytes(), - new ColumnFamilyOptions().setMergeOperator(stringAppendOperator))); - // writing xx under cfkey2 - db.put(cfHandle, "cfkey2".getBytes(), "xx".getBytes()); - // merge yy under cfkey2 - 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(); - } - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); + assertThat(strValue).isEqualTo("aa,bb"); + assertThat(strValueTmpCf).isEqualTo("xx,yy"); + } + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } @@ -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(); - // test reuse - opt = new Options(); - opt.setMergeOperator(stringAppendOperator); - db = RocksDB.open(opt, db_path_string); - db.close(); - opt.dispose(); - System.gc(); - System.runFinalization(); - // 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(); - // test replace one with another merge operator instance - opt = new Options(); - opt.setMergeOperator(stringAppendOperator); - StringAppendOperator newStringAppendOperator = new StringAppendOperator(); + 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 + try (final Options opt = new Options() + .setMergeOperator(stringAppendOperator); + final RocksDB db = RocksDB.open(opt, + dbFolder.getRoot().getAbsolutePath())) { + //no-op + } + + // test param init + 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 + 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(); + nullStringInSetMergeOperatorByNameColumnFamilyOptions() { + try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) { opt.setMergeOperatorName(null); - } finally { - if (opt != null) { - opt.dispose(); - } } } } diff --git a/java/src/test/java/org/rocksdb/MixedOptionsTest.java b/java/src/test/java/org/rocksdb/MixedOptionsTest.java index a3090a1b1..bbe295719 100644 --- a/java/src/test/java/org/rocksdb/MixedOptionsTest.java +++ b/java/src/test/java/org/rocksdb/MixedOptionsTest.java @@ -19,38 +19,37 @@ 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())); - 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); - assertThat(options.tableFactoryName()).isEqualTo("PlainTable"); - // Free instances - options.dispose(); - options = null; - cfOptions.dispose(); - cfOptions = null; - dbOptions.dispose(); - dbOptions = null; - System.gc(); - System.runFinalization(); + 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 + try (final DBOptions dbOptions = new DBOptions(); + final Options options = new Options(dbOptions, cfOptions)) { + assertThat(options.tableFactoryName()).isEqualTo("PlainTable"); + // Free instances + } + } + // Test Optimize for statements - cfOptions = new ColumnFamilyOptions(); + try(final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions()) { cfOptions.optimizeUniversalStyleCompaction(); cfOptions.optimizeLevelStyleCompaction(); cfOptions.optimizeForPointLookup(1024); - options = new Options(); - options.optimizeLevelStyleCompaction(); - options.optimizeLevelStyleCompaction(400); - options.optimizeUniversalStyleCompaction(); - options.optimizeUniversalStyleCompaction(400); - options.optimizeForPointLookup(1024); - options.prepareForBulkLoad(); + try(final Options options = new Options()) { + options.optimizeLevelStyleCompaction(); + options.optimizeLevelStyleCompaction(400); + options.optimizeUniversalStyleCompaction(); + options.optimizeUniversalStyleCompaction(400); + options.optimizeForPointLookup(1024); + options.prepareForBulkLoad(); + } + } } } diff --git a/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java b/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java index 4e9ad27a2..519a28be4 100644 --- a/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java +++ b/java/src/test/java/org/rocksdb/NativeLibraryLoaderTest.java @@ -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(); diff --git a/java/src/test/java/org/rocksdb/OptionsTest.java b/java/src/test/java/org/rocksdb/OptionsTest.java index 6d11e6fa7..a588b5de7 100644 --- a/java/src/test/java/org/rocksdb/OptionsTest.java +++ b/java/src/test/java/org/rocksdb/OptionsTest.java @@ -8,6 +8,7 @@ package org.rocksdb; import java.util.ArrayList; import java.util.List; import java.util.Random; + import org.junit.ClassRule; import org.junit.Test; @@ -25,985 +26,599 @@ public class OptionsTest { @Test public void setIncreaseParallelism() { - Options opt = null; - try { - opt = new Options(); + try (final Options opt = new Options()) { final int threads = Runtime.getRuntime().availableProcessors() * 2; opt.setIncreaseParallelism(threads); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void writeBufferSize() throws RocksDBException { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setWriteBufferSize(longValue); assertThat(opt.writeBufferSize()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxWriteBufferNumber() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMaxWriteBufferNumber(intValue); assertThat(opt.maxWriteBufferNumber()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void minWriteBufferNumberToMerge() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMinWriteBufferNumberToMerge(intValue); assertThat(opt.minWriteBufferNumberToMerge()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void numLevels() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setNumLevels(intValue); assertThat(opt.numLevels()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void levelZeroFileNumCompactionTrigger() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setLevelZeroFileNumCompactionTrigger(intValue); assertThat(opt.levelZeroFileNumCompactionTrigger()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void levelZeroSlowdownWritesTrigger() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setLevelZeroSlowdownWritesTrigger(intValue); assertThat(opt.levelZeroSlowdownWritesTrigger()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void levelZeroStopWritesTrigger() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setLevelZeroStopWritesTrigger(intValue); assertThat(opt.levelZeroStopWritesTrigger()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void targetFileSizeBase() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setTargetFileSizeBase(longValue); assertThat(opt.targetFileSizeBase()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void targetFileSizeMultiplier() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setTargetFileSizeMultiplier(intValue); assertThat(opt.targetFileSizeMultiplier()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxBytesForLevelBase() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setMaxBytesForLevelBase(longValue); assertThat(opt.maxBytesForLevelBase()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void levelCompactionDynamicLevelBytes() { - Options opt = null; - try { - opt = new Options(); + try (final Options opt = new Options()) { final boolean boolValue = rand.nextBoolean(); opt.setLevelCompactionDynamicLevelBytes(boolValue); assertThat(opt.levelCompactionDynamicLevelBytes()) .isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxBytesForLevelMultiplier() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMaxBytesForLevelMultiplier(intValue); assertThat(opt.maxBytesForLevelMultiplier()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void expandedCompactionFactor() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setExpandedCompactionFactor(intValue); assertThat(opt.expandedCompactionFactor()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void sourceCompactionFactor() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setSourceCompactionFactor(intValue); assertThat(opt.sourceCompactionFactor()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxGrandparentOverlapFactor() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMaxGrandparentOverlapFactor(intValue); assertThat(opt.maxGrandparentOverlapFactor()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void softRateLimit() { - Options opt = null; - try { - opt = new Options(); - double doubleValue = rand.nextDouble(); + try (final Options opt = new Options()) { + final double doubleValue = rand.nextDouble(); opt.setSoftRateLimit(doubleValue); assertThat(opt.softRateLimit()).isEqualTo(doubleValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void hardRateLimit() { - Options opt = null; - try { - opt = new Options(); - double doubleValue = rand.nextDouble(); + try (final Options opt = new Options()) { + final double doubleValue = rand.nextDouble(); opt.setHardRateLimit(doubleValue); assertThat(opt.hardRateLimit()).isEqualTo(doubleValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void rateLimitDelayMaxMilliseconds() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + 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 { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setArenaBlockSize(longValue); assertThat(opt.arenaBlockSize()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void disableAutoCompactions() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setDisableAutoCompactions(boolValue); assertThat(opt.disableAutoCompactions()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void purgeRedundantKvsWhileFlush() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setPurgeRedundantKvsWhileFlush(boolValue); assertThat(opt.purgeRedundantKvsWhileFlush()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void verifyChecksumsInCompaction() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setVerifyChecksumsInCompaction(boolValue); assertThat(opt.verifyChecksumsInCompaction()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void filterDeletes() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setFilterDeletes(boolValue); assertThat(opt.filterDeletes()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxSequentialSkipInIterations() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setMaxSequentialSkipInIterations(longValue); assertThat(opt.maxSequentialSkipInIterations()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void inplaceUpdateSupport() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + 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 { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setInplaceUpdateNumLocks(longValue); assertThat(opt.inplaceUpdateNumLocks()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void memtablePrefixBloomBits() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMemtablePrefixBloomBits(intValue); assertThat(opt.memtablePrefixBloomBits()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void memtablePrefixBloomProbes() { - Options opt = null; - try { - int intValue = rand.nextInt(); - opt = new Options(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMemtablePrefixBloomProbes(intValue); assertThat(opt.memtablePrefixBloomProbes()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void bloomLocality() { - Options opt = null; - try { - int intValue = rand.nextInt(); - opt = new Options(); + try (final Options opt = new Options()) { + 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 { - Options opt = null; - try { - long longValue = rand.nextLong(); - opt = new Options(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setMaxSuccessiveMerges(longValue); assertThat(opt.maxSuccessiveMerges()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void minPartialMergeOperands() { - Options opt = null; - try { - int intValue = rand.nextInt(); - opt = new Options(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMinPartialMergeOperands(intValue); assertThat(opt.minPartialMergeOperands()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void optimizeFiltersForHits() { - Options opt = null; - try { - boolean aBoolean = rand.nextBoolean(); - opt = new Options(); + try (final Options opt = new Options()) { + final boolean aBoolean = rand.nextBoolean(); opt.setOptimizeFiltersForHits(aBoolean); assertThat(opt.optimizeFiltersForHits()).isEqualTo(aBoolean); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void createIfMissing() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setCreateIfMissing(boolValue); assertThat(opt.createIfMissing()). isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void createMissingColumnFamilies() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setCreateMissingColumnFamilies(boolValue); assertThat(opt.createMissingColumnFamilies()). isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void errorIfExists() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setErrorIfExists(boolValue); assertThat(opt.errorIfExists()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void paranoidChecks() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setParanoidChecks(boolValue); assertThat(opt.paranoidChecks()). isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxTotalWalSize() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setMaxTotalWalSize(longValue); assertThat(opt.maxTotalWalSize()). isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxOpenFiles() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMaxOpenFiles(intValue); assertThat(opt.maxOpenFiles()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void disableDataSync() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setDisableDataSync(boolValue); assertThat(opt.disableDataSync()). isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void useFsync() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setUseFsync(boolValue); assertThat(opt.useFsync()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void dbLogDir() { - Options opt = null; - try { - opt = new Options(); - String str = "path/to/DbLogDir"; + try (final Options opt = new Options()) { + final String str = "path/to/DbLogDir"; opt.setDbLogDir(str); assertThat(opt.dbLogDir()).isEqualTo(str); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void walDir() { - Options opt = null; - try { - opt = new Options(); - String str = "path/to/WalDir"; + try (final Options opt = new Options()) { + final String str = "path/to/WalDir"; opt.setWalDir(str); assertThat(opt.walDir()).isEqualTo(str); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void deleteObsoleteFilesPeriodMicros() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setDeleteObsoleteFilesPeriodMicros(longValue); assertThat(opt.deleteObsoleteFilesPeriodMicros()). isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxBackgroundCompactions() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMaxBackgroundCompactions(intValue); assertThat(opt.maxBackgroundCompactions()). isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxBackgroundFlushes() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setMaxBackgroundFlushes(intValue); assertThat(opt.maxBackgroundFlushes()). isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxLogFileSize() throws RocksDBException { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + 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 { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setLogFileTimeToRoll(longValue); assertThat(opt.logFileTimeToRoll()). isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void keepLogFileNum() throws RocksDBException { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setKeepLogFileNum(longValue); assertThat(opt.keepLogFileNum()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void maxManifestFileSize() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setMaxManifestFileSize(longValue); assertThat(opt.maxManifestFileSize()). isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void tableCacheNumshardbits() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setTableCacheNumshardbits(intValue); assertThat(opt.tableCacheNumshardbits()). isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void walSizeLimitMB() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setWalSizeLimitMB(longValue); assertThat(opt.walSizeLimitMB()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void walTtlSeconds() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + 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 { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setManifestPreallocationSize(longValue); assertThat(opt.manifestPreallocationSize()). isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void allowOsBuffer() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setAllowOsBuffer(boolValue); assertThat(opt.allowOsBuffer()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void allowMmapReads() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setAllowMmapReads(boolValue); assertThat(opt.allowMmapReads()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void allowMmapWrites() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setAllowMmapWrites(boolValue); assertThat(opt.allowMmapWrites()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void isFdCloseOnExec() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setIsFdCloseOnExec(boolValue); assertThat(opt.isFdCloseOnExec()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void statsDumpPeriodSec() { - Options opt = null; - try { - opt = new Options(); - int intValue = rand.nextInt(); + try (final Options opt = new Options()) { + final int intValue = rand.nextInt(); opt.setStatsDumpPeriodSec(intValue); assertThat(opt.statsDumpPeriodSec()).isEqualTo(intValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void adviseRandomOnOpen() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setAdviseRandomOnOpen(boolValue); assertThat(opt.adviseRandomOnOpen()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void useAdaptiveMutex() { - Options opt = null; - try { - opt = new Options(); - boolean boolValue = rand.nextBoolean(); + try (final Options opt = new Options()) { + final boolean boolValue = rand.nextBoolean(); opt.setUseAdaptiveMutex(boolValue); assertThat(opt.useAdaptiveMutex()).isEqualTo(boolValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void bytesPerSync() { - Options opt = null; - try { - opt = new Options(); - long longValue = rand.nextLong(); + try (final Options opt = new Options()) { + final long longValue = rand.nextLong(); opt.setBytesPerSync(longValue); assertThat(opt.bytesPerSync()).isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void env() { - Options options = null; - try { - options = new Options(); - Env env = Env.getDefault(); + try (final Options options = new Options(); + final Env env = Env.getDefault()) { options.setEnv(env); assertThat(options.getEnv()).isSameAs(env); - } finally { - if (options != null) { - options.dispose(); - } } } @Test public void linkageOfPrepMethods() { - Options options = null; - try { - options = new Options(); + try (final Options options = new Options()) { options.optimizeUniversalStyleCompaction(); options.optimizeUniversalStyleCompaction(4000); options.optimizeLevelStyleCompaction(); options.optimizeLevelStyleCompaction(3000); options.optimizeForPointLookup(10); options.prepareForBulkLoad(); - } finally { - if (options != null) { - options.dispose(); - } } } @Test public void compressionTypes() { - Options options = null; - try { - options = new Options(); - for (CompressionType compressionType : + try (final Options options = new Options()) { + for (final CompressionType compressionType : CompressionType.values()) { options.setCompressionType(compressionType); assertThat(options.compressionType()). @@ -1011,22 +626,17 @@ public class OptionsTest { assertThat(CompressionType.valueOf("NO_COMPRESSION")). isEqualTo(CompressionType.NO_COMPRESSION); } - } finally { - if (options != null) { - options.dispose(); - } } } @Test public void compressionPerLevel() { - ColumnFamilyOptions columnFamilyOptions = null; - try { - columnFamilyOptions = new ColumnFamilyOptions(); + try (final ColumnFamilyOptions columnFamilyOptions = + new ColumnFamilyOptions()) { assertThat(columnFamilyOptions.compressionPerLevel()).isEmpty(); List compressionTypeList = new ArrayList<>(); - for (int i=0; i < columnFamilyOptions.numLevels(); i++) { + for (int i = 0; i < columnFamilyOptions.numLevels(); i++) { compressionTypeList.add(CompressionType.NO_COMPRESSION); } columnFamilyOptions.setCompressionPerLevel(compressionTypeList); @@ -1035,18 +645,13 @@ public class OptionsTest { 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(); @@ -1066,19 +671,13 @@ public class OptionsTest { CompressionType.SNAPPY_COMPRESSION, CompressionType.LZ4_COMPRESSION); - } finally { - if (columnFamilyOptions != null) { - columnFamilyOptions.dispose(); - } } } @Test public void compactionStyles() { - Options options = null; - try { - options = new Options(); - for (CompactionStyle compactionStyle : + try (final Options options = new Options()) { + for (final CompactionStyle compactionStyle : CompactionStyle.values()) { options.setCompactionStyle(compactionStyle); assertThat(options.compactionStyle()). @@ -1086,18 +685,12 @@ public class OptionsTest { assertThat(CompactionStyle.valueOf("FIFO")). isEqualTo(CompactionStyle.FIFO); } - } finally { - if (options != null) { - options.dispose(); - } } } @Test public void maxTableFilesSizeFIFO() { - Options opt = null; - try { - opt = new Options(); + try (final Options opt = new Options()) { long longValue = rand.nextLong(); // Size has to be positive longValue = (longValue < 0) ? -longValue : longValue; @@ -1105,61 +698,36 @@ public class OptionsTest { opt.setMaxTableFilesSizeFIFO(longValue); assertThat(opt.maxTableFilesSizeFIFO()). isEqualTo(longValue); - } finally { - if (opt != null) { - opt.dispose(); - } } } @Test public void rateLimiterConfig() { - Options options = null; - Options anotherOptions = null; - RateLimiterConfig rateLimiterConfig; - try { - options = new Options(); - rateLimiterConfig = new GenericRateLimiterConfig(1000, 100 * 1000, 1); + try (final Options options = new Options(); + final Options anotherOptions = new Options()) { + final RateLimiterConfig rateLimiterConfig = + new GenericRateLimiterConfig(1000, 100 * 1000, 1); options.setRateLimiterConfig(rateLimiterConfig); // Test with parameter initialization - anotherOptions = new Options(); + anotherOptions.setRateLimiterConfig( new GenericRateLimiterConfig(1000)); - } finally { - if (options != null) { - options.dispose(); - } - if (anotherOptions != null) { - anotherOptions.dispose(); - } } } @Test public void shouldSetTestPrefixExtractor() { - Options options = null; - try { - options = new Options(); + try (final Options options = new Options()) { options.useFixedLengthPrefixExtractor(100); options.useFixedLengthPrefixExtractor(10); - } finally { - if (options != null) { - options.dispose(); - } } } @Test public void shouldSetTestCappedPrefixExtractor() { - Options options = null; - try { - options = new Options(); + try (final Options options = new Options()) { options.useCappedPrefixExtractor(100); options.useCappedPrefixExtractor(10); - } finally { - if (options != null) { - options.dispose(); - } } } @@ -1167,9 +735,7 @@ public class OptionsTest { @Test public void shouldTestMemTableFactoryName() throws RocksDBException { - Options options = null; - try { - options = new Options(); + try (final Options options = new Options()) { options.setMemTableConfig(new VectorMemTableConfig()); assertThat(options.memTableFactoryName()). isEqualTo("VectorRepFactory"); @@ -1177,31 +743,18 @@ public class OptionsTest { new HashLinkedListMemTableConfig()); assertThat(options.memTableFactoryName()). isEqualTo("HashLinkedListRepFactory"); - } finally { - if (options != null) { - options.dispose(); - } } } @Test public void statistics() { - Options options = null; - Options anotherOptions = null; - try { - options = new Options(); + try (final Options options = new Options()) { Statistics statistics = options.createStatistics(). statisticsPtr(); assertThat(statistics).isNotNull(); - anotherOptions = new Options(); - statistics = anotherOptions.statisticsPtr(); - assertThat(statistics).isNotNull(); - } finally { - if (options != null) { - options.dispose(); - } - if (anotherOptions != null) { - anotherOptions.dispose(); + try (final Options anotherOptions = new Options()) { + statistics = anotherOptions.statisticsPtr(); + assertThat(statistics).isNotNull(); } } } diff --git a/java/src/test/java/org/rocksdb/PlainTableConfigTest.java b/java/src/test/java/org/rocksdb/PlainTableConfigTest.java index b815cd058..05bd13863 100644 --- a/java/src/test/java/org/rocksdb/PlainTableConfigTest.java +++ b/java/src/test/java/org/rocksdb/PlainTableConfigTest.java @@ -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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/PlatformRandomHelper.java b/java/src/test/java/org/rocksdb/PlatformRandomHelper.java index e88a8951d..b437e7f97 100644 --- a/java/src/test/java/org/rocksdb/PlatformRandomHelper.java +++ b/java/src/test/java/org/rocksdb/PlatformRandomHelper.java @@ -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 { diff --git a/java/src/test/java/org/rocksdb/ReadOnlyTest.java b/java/src/test/java/org/rocksdb/ReadOnlyTest.java index 5cf2b32d4..d993c9148 100644 --- a/java/src/test/java/org/rocksdb/ReadOnlyTest.java +++ b/java/src/test/java/org/rocksdb/ReadOnlyTest.java @@ -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 columnFamilyHandleList = - new ArrayList<>(); - List readOnlyColumnFamilyHandleList = - new ArrayList<>(); - List 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()); - assertThat("value"). - isEqualTo(new String(db2.get("key".getBytes()))); - db.close(); - db2.close(); + try (final RocksDB db2 = RocksDB.openReadOnly( + dbFolder.getRoot().getAbsolutePath())) { + assertThat("value"). + isEqualTo(new String(db2.get("key".getBytes()))); + } + } - List cfDescriptors = new ArrayList<>(); - cfDescriptors.add( - new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, - new ColumnFamilyOptions())); + try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { + final List cfDescriptors = new ArrayList<>(); + cfDescriptors.add(new ColumnFamilyDescriptor( + RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)); - db = RocksDB.open( - dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList); - columnFamilyHandleList.add(db.createColumnFamily( - new ColumnFamilyDescriptor("new_cf".getBytes(), new ColumnFamilyOptions()))); - columnFamilyHandleList.add(db.createColumnFamily( - new ColumnFamilyDescriptor("new_cf2".getBytes(), new ColumnFamilyOptions()))); - db.put(columnFamilyHandleList.get(2), "key2".getBytes(), - "value2".getBytes()); + final List 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(), newCfOpts))); + columnFamilyHandleList.add(db.createColumnFamily( + new ColumnFamilyDescriptor("new_cf2".getBytes(), newCf2Opts))); + db.put(columnFamilyHandleList.get(2), "key2".getBytes(), + "value2".getBytes()); - db2 = RocksDB.openReadOnly( - dbFolder.getRoot().getAbsolutePath(), cfDescriptors, - readOnlyColumnFamilyHandleList); - assertThat(db2.get("key2".getBytes())).isNull(); - 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), - "key2".getBytes()))).isEqualTo("value2"); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db2 != null) { - db2.close(); - } - for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList2) { - columnFamilyHandle.dispose(); - } - if (db3 != null) { - db3.close(); - } - if (options != null) { - options.dispose(); + final List readOnlyColumnFamilyHandleList = + new ArrayList<>(); + try (final RocksDB db2 = RocksDB.openReadOnly( + dbFolder.getRoot().getAbsolutePath(), cfDescriptors, + 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())). + isNull(); + cfDescriptors.clear(); + cfDescriptors.add( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, + newCfOpts2)); + cfDescriptors.add(new ColumnFamilyDescriptor("new_cf2".getBytes(), + newCf2Opts2)); + + final List 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 (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList2) { + columnFamilyHandle.close(); + } + } + } + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList) { + columnFamilyHandle.close(); + } + } + } + } 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 cfDescriptors = new ArrayList<>(); - List 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 cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) + ); - db = RocksDB.open(options, - dbFolder.getRoot().getAbsolutePath()); - db.close(); - rDb = RocksDB.openReadOnly( + final List readOnlyColumnFamilyHandleList = + new ArrayList<>(); + try (final RocksDB rDb = RocksDB.openReadOnly( dbFolder.getRoot().getAbsolutePath(), cfDescriptors, - readOnlyColumnFamilyHandleList); - - // 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(); - } - if (rDb != null) { - rDb.close(); - } - if (options != null) { - options.dispose(); + readOnlyColumnFamilyHandleList)) { + try { + // test that put fails in readonly mode + rDb.put("key".getBytes(), "value".getBytes()); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } @Test(expected = RocksDBException.class) public void failToCFWriteInReadOnly() throws RocksDBException { - RocksDB db = null; - RocksDB rDb = null; - Options options = null; - List cfDescriptors = new ArrayList<>(); - List 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); - - db = RocksDB.open(options, - dbFolder.getRoot().getAbsolutePath()); - db.close(); - rDb = RocksDB.openReadOnly( + try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) + ); + final List readOnlyColumnFamilyHandleList = + new ArrayList<>(); + try (final RocksDB rDb = RocksDB.openReadOnly( dbFolder.getRoot().getAbsolutePath(), cfDescriptors, - readOnlyColumnFamilyHandleList); - - rDb.put(readOnlyColumnFamilyHandleList.get(0), - "key".getBytes(), "value".getBytes()); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (rDb != null) { - rDb.close(); - } - if (options != null) { - options.dispose(); + readOnlyColumnFamilyHandleList)) { + try { + rDb.put(readOnlyColumnFamilyHandleList.get(0), + "key".getBytes(), "value".getBytes()); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } @Test(expected = RocksDBException.class) public void failToRemoveInReadOnly() throws RocksDBException { - RocksDB db = null; - RocksDB rDb = null; - Options options = null; - List cfDescriptors = new ArrayList<>(); - List 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 cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) + ); - db = RocksDB.open(options, - dbFolder.getRoot().getAbsolutePath()); - db.close(); - rDb = RocksDB.openReadOnly( + final List readOnlyColumnFamilyHandleList = + new ArrayList<>(); + + try (final RocksDB rDb = RocksDB.openReadOnly( dbFolder.getRoot().getAbsolutePath(), cfDescriptors, - readOnlyColumnFamilyHandleList); - - rDb.remove("key".getBytes()); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (rDb != null) { - rDb.close(); - } - if (options != null) { - options.dispose(); + readOnlyColumnFamilyHandleList)) { + try { + rDb.remove("key".getBytes()); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } @Test(expected = RocksDBException.class) public void failToCFRemoveInReadOnly() throws RocksDBException { - RocksDB db = null; - RocksDB rDb = null; - Options options = null; - List cfDescriptors = new ArrayList<>(); - List 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 cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) + ); - db = RocksDB.open(options, - dbFolder.getRoot().getAbsolutePath()); - db.close(); - - rDb = RocksDB.openReadOnly( + final List readOnlyColumnFamilyHandleList = + new ArrayList<>(); + try (final RocksDB rDb = RocksDB.openReadOnly( dbFolder.getRoot().getAbsolutePath(), cfDescriptors, - readOnlyColumnFamilyHandleList); - - rDb.remove(readOnlyColumnFamilyHandleList.get(0), - "key".getBytes()); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (rDb != null) { - rDb.close(); - } - if (options != null) { - options.dispose(); + readOnlyColumnFamilyHandleList)) { + try { + rDb.remove(readOnlyColumnFamilyHandleList.get(0), + "key".getBytes()); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } @Test(expected = RocksDBException.class) public void failToWriteBatchReadOnly() throws RocksDBException { - RocksDB db = null; - RocksDB rDb = null; - Options options = null; - List cfDescriptors = new ArrayList<>(); - List readOnlyColumnFamilyHandleList = - new ArrayList<>(); - try { + try (final Options options = new Options().setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { + //no-op + } - cfDescriptors.add( - new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, - new ColumnFamilyOptions())); + try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) + ); - options = new Options(); - options.setCreateIfMissing(true); - - db = RocksDB.open(options, - dbFolder.getRoot().getAbsolutePath()); - db.close(); - - rDb = RocksDB.openReadOnly( + final List readOnlyColumnFamilyHandleList = + new ArrayList<>(); + try (final RocksDB rDb = RocksDB.openReadOnly( dbFolder.getRoot().getAbsolutePath(), cfDescriptors, readOnlyColumnFamilyHandleList); - - WriteBatch wb = new WriteBatch(); - wb.put("key".getBytes(), "value".getBytes()); - rDb.write(new WriteOptions(), wb); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (rDb != null) { - rDb.close(); - } - if (options != null) { - options.dispose(); + final WriteBatch wb = new WriteBatch(); + final WriteOptions wOpts = new WriteOptions()) { + try { + wb.put("key".getBytes(), "value".getBytes()); + rDb.write(wOpts, wb); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } @Test(expected = RocksDBException.class) public void failToCFWriteBatchReadOnly() throws RocksDBException { - RocksDB db = null; - RocksDB rDb = null; - Options options = null; - WriteBatch wb = null; - List cfDescriptors = new ArrayList<>(); - List readOnlyColumnFamilyHandleList = - new ArrayList<>(); - try { + try (final Options options = new Options().setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { + //no-op + } - cfDescriptors.add( - new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, - new ColumnFamilyOptions())); + try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { + final List cfDescriptors = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) + ); - - options = new Options(); - options.setCreateIfMissing(true); - - db = RocksDB.open(options, - dbFolder.getRoot().getAbsolutePath()); - db.close(); - - rDb = RocksDB.openReadOnly( + final List 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); - } finally { - for (ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (db != null) { - db.close(); - } - if (rDb != null) { - rDb.close(); - } - if (options != null) { - options.dispose(); - } - if (wb != null) { - wb.dispose(); + 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 (final ColumnFamilyHandle columnFamilyHandle : + readOnlyColumnFamilyHandleList) { + columnFamilyHandle.close(); + } + } } } } diff --git a/java/src/test/java/org/rocksdb/ReadOptionsTest.java b/java/src/test/java/org/rocksdb/ReadOptionsTest.java index df42cf0cd..a10f5ac7e 100644 --- a/java/src/test/java/org/rocksdb/ReadOptionsTest.java +++ b/java/src/test/java/org/rocksdb/ReadOptionsTest.java @@ -24,127 +24,111 @@ public class ReadOptionsTest { public ExpectedException exception = ExpectedException.none(); @Test - public void verifyChecksum(){ - ReadOptions opt = null; - try { - opt = new ReadOptions(); - Random rand = new Random(); - boolean boolValue = rand.nextBoolean(); + public void verifyChecksum() { + 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(); + public void fillCache() { + 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(); + public void tailing() { + 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(); + public void snapshot() { + 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); - readOptions.setVerifyChecksums(true); + public void failSetVerifyChecksumUninitialized() { + try (final ReadOptions readOptions = + setupUninitializedReadOptions(exception)) { + readOptions.setVerifyChecksums(true); + } } @Test - public void failVerifyChecksumUninitialized(){ - ReadOptions readOptions = setupUninitializedReadOptions( - exception); - readOptions.verifyChecksums(); + public void failVerifyChecksumUninitialized() { + try (final ReadOptions readOptions = + setupUninitializedReadOptions(exception)) { + readOptions.verifyChecksums(); + } } @Test - public void failSetFillCacheUninitialized(){ - ReadOptions readOptions = setupUninitializedReadOptions( - exception); - readOptions.setFillCache(true); + public void failSetFillCacheUninitialized() { + try (final ReadOptions readOptions = + setupUninitializedReadOptions(exception)) { + readOptions.setFillCache(true); + } } @Test - public void failFillCacheUninitialized(){ - ReadOptions readOptions = setupUninitializedReadOptions( - exception); - readOptions.fillCache(); + public void failFillCacheUninitialized() { + try (final ReadOptions readOptions = + setupUninitializedReadOptions(exception)) { + readOptions.fillCache(); + } } @Test - public void failSetTailingUninitialized(){ - ReadOptions readOptions = setupUninitializedReadOptions( - exception); - readOptions.setTailing(true); + public void failSetTailingUninitialized() { + try (final ReadOptions readOptions = + setupUninitializedReadOptions(exception)) { + readOptions.setTailing(true); + } } @Test - public void failTailingUninitialized(){ - ReadOptions readOptions = setupUninitializedReadOptions( - exception); - readOptions.tailing(); + public void failTailingUninitialized() { + try (final ReadOptions readOptions = + setupUninitializedReadOptions(exception)) { + readOptions.tailing(); + } } @Test - public void failSetSnapshotUninitialized(){ - ReadOptions readOptions = setupUninitializedReadOptions( - exception); - readOptions.setSnapshot(null); + public void failSetSnapshotUninitialized() { + try (final ReadOptions readOptions = + setupUninitializedReadOptions(exception)) { + readOptions.setSnapshot(null); + } } @Test - public void failSnapshotUninitialized(){ - ReadOptions readOptions = setupUninitializedReadOptions( - exception); - readOptions.snapshot(); + public void failSnapshotUninitialized() { + 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; } diff --git a/java/src/test/java/org/rocksdb/RocksDBTest.java b/java/src/test/java/org/rocksdb/RocksDBTest.java index cb1c1f77d..ceac69f5f 100644 --- a/java/src/test/java/org/rocksdb/RocksDBTest.java +++ b/java/src/test/java/org/rocksdb/RocksDBTest.java @@ -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(); - } - if (opt != null) { - opt.dispose(); - } + try (final RocksDB db = + RocksDB.open(dbFolder.getRoot().getAbsolutePath())) { + assertThat(db).isNotNull(); + } + } + + @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(); - wb1.put("key1".getBytes(), "aa".getBytes()); - wb1.merge("key1".getBytes(), "bb".getBytes()); - wb2 = new WriteBatch(); - wb2.put("key2".getBytes(), "xx".getBytes()); - wb2.merge("key2".getBytes(), "yy".getBytes()); - db.write(opts, wb1); - db.write(opts, wb2); + 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()); + + 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 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,66 +205,47 @@ 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(). - setCreateIfMissing(true). - setDisableAutoCompactions(true). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(4). - setWriteBufferSize(100<<10). - setLevelZeroFileNumCompactionTrigger(3). - setTargetFileSizeBase(200 << 10). - setTargetFileSizeMultiplier(1). - setMaxBytesForLevelBase(500 << 10). - setMaxBytesForLevelMultiplier(1). - setDisableAutoCompactions(false); - // open database - db = RocksDB.open(opt, - dbFolder.getRoot().getAbsolutePath()); + try (final Options opt = new Options(). + setCreateIfMissing(true). + setDisableAutoCompactions(true). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(4). + setWriteBufferSize(100 << 10). + setLevelZeroFileNumCompactionTrigger(3). + setTargetFileSizeBase(200 << 10). + setTargetFileSizeMultiplier(1). + setMaxBytesForLevelBase(500 << 10). + setMaxBytesForLevelMultiplier(1). + setDisableAutoCompactions(false); + 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,66 +253,53 @@ 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 columnFamilyHandles = - new ArrayList<>(); - try { - opt = new DBOptions(). - setCreateIfMissing(true). - setCreateMissingColumnFamilies(true); - List columnFamilyDescriptors = - new ArrayList<>(); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - RocksDB.DEFAULT_COLUMN_FAMILY)); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - "new_cf".getBytes(), - new ColumnFamilyOptions(). - setDisableAutoCompactions(true). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(4). - setWriteBufferSize(100 << 10). - setLevelZeroFileNumCompactionTrigger(3). - setTargetFileSizeBase(200 << 10). - setTargetFileSizeMultiplier(1). - setMaxBytesForLevelBase(500 << 10). - setMaxBytesForLevelMultiplier(1). - setDisableAutoCompactions(false))); + try ( + final DBOptions opt = new DBOptions(). + setCreateIfMissing(true). + setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions(). + setDisableAutoCompactions(true). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(4). + setWriteBufferSize(100 << 10). + setLevelZeroFileNumCompactionTrigger(3). + setTargetFileSizeBase(200 << 10). + setTargetFileSizeMultiplier(1). + setMaxBytesForLevelBase(500 << 10). + setMaxBytesForLevelMultiplier(1). + setDisableAutoCompactions(false) + ) { + final List 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 columnFamilyHandles = new ArrayList<>(); + try (final RocksDB db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, - columnFamilyHandles); - // fill database with key/value pairs - byte[] b = new byte[10000]; - for (int i = 0; i < 200; i++) { - rand.nextBytes(b); - db.put(columnFamilyHandles.get(1), - String.valueOf(i).getBytes(), b); - } - db.compactRange(columnFamilyHandles.get(1)); - } finally { - for (ColumnFamilyHandle handle : columnFamilyHandles) { - handle.dispose(); - } - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); + columnFamilyHandles)) { + try { + // fill database with key/value pairs + byte[] b = new byte[10000]; + for (int i = 0; i < 200; i++) { + rand.nextBytes(b); + db.put(columnFamilyHandles.get(1), + String.valueOf(i).getBytes(), b); + } + db.compactRange(columnFamilyHandles.get(1)); + } finally { + for (final ColumnFamilyHandle handle : columnFamilyHandles) { + handle.close(); + } + } } } } @@ -406,24 +307,20 @@ public class RocksDBTest { @Test public void compactRangeWithKeys() throws RocksDBException { - RocksDB db = null; - Options opt = null; - try { - opt = new Options(). - setCreateIfMissing(true). - setDisableAutoCompactions(true). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(4). - setWriteBufferSize(100<<10). - setLevelZeroFileNumCompactionTrigger(3). - setTargetFileSizeBase(200 << 10). - setTargetFileSizeMultiplier(1). - setMaxBytesForLevelBase(500 << 10). - setMaxBytesForLevelMultiplier(1). - setDisableAutoCompactions(false); - // open database - db = RocksDB.open(opt, - dbFolder.getRoot().getAbsolutePath()); + try (final Options opt = new Options(). + setCreateIfMissing(true). + setDisableAutoCompactions(true). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(4). + setWriteBufferSize(100 << 10). + setLevelZeroFileNumCompactionTrigger(3). + setTargetFileSizeBase(200 << 10). + setTargetFileSizeMultiplier(1). + setMaxBytesForLevelBase(500 << 10). + setMaxBytesForLevelMultiplier(1). + setDisableAutoCompactions(false); + 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,37 +328,27 @@ 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(). - setCreateIfMissing(true). - setDisableAutoCompactions(true). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(4). - setWriteBufferSize(100<<10). - setLevelZeroFileNumCompactionTrigger(3). - setTargetFileSizeBase(200 << 10). - setTargetFileSizeMultiplier(1). - setMaxBytesForLevelBase(500 << 10). - setMaxBytesForLevelMultiplier(1). - setDisableAutoCompactions(false); - // open database - db = RocksDB.open(opt, - dbFolder.getRoot().getAbsolutePath()); + try ( + final Options opt = new Options(). + setCreateIfMissing(true). + setDisableAutoCompactions(true). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(4). + setWriteBufferSize(100 << 10). + setLevelZeroFileNumCompactionTrigger(3). + setTargetFileSizeBase(200 << 10). + setTargetFileSizeMultiplier(1). + setMaxBytesForLevelBase(500 << 10). + setMaxBytesForLevelMultiplier(1). + setDisableAutoCompactions(false); + 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,67 +358,55 @@ 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 columnFamilyHandles = - new ArrayList<>(); - try { - opt = new DBOptions(). - setCreateIfMissing(true). - setCreateMissingColumnFamilies(true); - List columnFamilyDescriptors = - new ArrayList<>(); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - RocksDB.DEFAULT_COLUMN_FAMILY)); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - "new_cf".getBytes(), - new ColumnFamilyOptions(). - setDisableAutoCompactions(true). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(4). - setWriteBufferSize(100<<10). - setLevelZeroFileNumCompactionTrigger(3). - setTargetFileSizeBase(200 << 10). - setTargetFileSizeMultiplier(1). - setMaxBytesForLevelBase(500 << 10). - setMaxBytesForLevelMultiplier(1). - setDisableAutoCompactions(false))); + try (final DBOptions opt = new DBOptions(). + setCreateIfMissing(true). + setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions(). + setDisableAutoCompactions(true). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(4). + setWriteBufferSize(100 << 10). + setLevelZeroFileNumCompactionTrigger(3). + setTargetFileSizeBase(200 << 10). + setTargetFileSizeMultiplier(1). + setMaxBytesForLevelBase(500 << 10). + setMaxBytesForLevelMultiplier(1). + setDisableAutoCompactions(false) + ) { + final List 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 columnFamilyHandles = + new ArrayList<>(); + try (final RocksDB db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, - columnFamilyHandles); - // fill database with key/value pairs - byte[] b = new byte[10000]; - for (int i = 0; i < 200; i++) { - rand.nextBytes(b); - db.put(columnFamilyHandles.get(1), - String.valueOf(i).getBytes(), b); - } - db.compactRange(columnFamilyHandles.get(1), - "0".getBytes(), "201".getBytes()); - } finally { - for (ColumnFamilyHandle handle : columnFamilyHandles) { - handle.dispose(); - } - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); + columnFamilyHandles)) { + try { + // fill database with key/value pairs + byte[] b = new byte[10000]; + for (int i = 0; i < 200; i++) { + rand.nextBytes(b); + db.put(columnFamilyHandles.get(1), + String.valueOf(i).getBytes(), b); + } + db.compactRange(columnFamilyHandles.get(1), + "0".getBytes(), "201".getBytes()); + } finally { + for (final ColumnFamilyHandle handle : columnFamilyHandles) { + handle.close(); + } + } } } } @@ -539,54 +414,48 @@ public class RocksDBTest { @Test public void compactRangeWithKeysReduceColumnFamily() throws RocksDBException { - RocksDB db = null; - DBOptions opt = null; - List columnFamilyHandles = - new ArrayList<>(); - try { - opt = new DBOptions(). - setCreateIfMissing(true). - setCreateMissingColumnFamilies(true); - List columnFamilyDescriptors = - new ArrayList<>(); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - RocksDB.DEFAULT_COLUMN_FAMILY)); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - "new_cf".getBytes(), - new ColumnFamilyOptions(). - setDisableAutoCompactions(true). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(4). - setWriteBufferSize(100<<10). - setLevelZeroFileNumCompactionTrigger(3). - setTargetFileSizeBase(200 << 10). - setTargetFileSizeMultiplier(1). - setMaxBytesForLevelBase(500 << 10). - setMaxBytesForLevelMultiplier(1). - setDisableAutoCompactions(false))); + try (final DBOptions opt = new DBOptions(). + setCreateIfMissing(true). + setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions(). + setDisableAutoCompactions(true). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(4). + setWriteBufferSize(100 << 10). + setLevelZeroFileNumCompactionTrigger(3). + setTargetFileSizeBase(200 << 10). + setTargetFileSizeMultiplier(1). + setMaxBytesForLevelBase(500 << 10). + setMaxBytesForLevelMultiplier(1). + setDisableAutoCompactions(false) + ) { + final List columnFamilyDescriptors = + Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts) + ); + + final List columnFamilyHandles = new ArrayList<>(); // open database - db = RocksDB.open(opt, + try (final RocksDB db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, - columnFamilyHandles); - // fill database with key/value pairs - byte[] b = new byte[10000]; - for (int i = 0; i < 200; i++) { - rand.nextBytes(b); - db.put(columnFamilyHandles.get(1), - String.valueOf(i).getBytes(), b); - } - db.compactRange(columnFamilyHandles.get(1), "0".getBytes(), - "201".getBytes(), true, -1, 0); - } finally { - for (ColumnFamilyHandle handle : columnFamilyHandles) { - handle.dispose(); - } - if (db != null) { - db.close(); - } - if (opt != null) { - opt.dispose(); + columnFamilyHandles)) { + try { + // fill database with key/value pairs + byte[] b = new byte[10000]; + for (int i = 0; i < 200; i++) { + rand.nextBytes(b); + db.put(columnFamilyHandles.get(1), + String.valueOf(i).getBytes(), b); + } + db.compactRange(columnFamilyHandles.get(1), "0".getBytes(), + "201".getBytes(), true, -1, 0); + } finally { + for (final ColumnFamilyHandle handle : columnFamilyHandles) { + handle.close(); + } + } } } } @@ -594,35 +463,33 @@ 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; - final int L0_FILE_SIZE = - NUM_KEYS_PER_L0_FILE * (KEY_SIZE + VALUE_SIZE); - final int NUM_L0_FILES = 10; - final int TEST_SCALE = 5; - final int KEY_INTERVAL = 100; - opt = new Options(). - setCreateIfMissing(true). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(5). - // a slightly bigger write buffer than L0 file - // so that we can ensure manual flush always - // go before background flush happens. - setWriteBufferSize(L0_FILE_SIZE * 2). - // Disable auto L0 -> L1 compaction - setLevelZeroFileNumCompactionTrigger(20). - setTargetFileSizeBase(L0_FILE_SIZE * 100). - setTargetFileSizeMultiplier(1). - // To disable auto compaction - setMaxBytesForLevelBase(NUM_L0_FILES * L0_FILE_SIZE * 100). - setMaxBytesForLevelMultiplier(2). - setDisableAutoCompactions(true); - db = RocksDB.open(opt, - dbFolder.getRoot().getAbsolutePath()); + final int NUM_KEYS_PER_L0_FILE = 100; + final int KEY_SIZE = 20; + final int VALUE_SIZE = 300; + final int L0_FILE_SIZE = + NUM_KEYS_PER_L0_FILE * (KEY_SIZE + VALUE_SIZE); + final int NUM_L0_FILES = 10; + final int TEST_SCALE = 5; + final int KEY_INTERVAL = 100; + try (final Options opt = new Options(). + setCreateIfMissing(true). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(5). + // a slightly bigger write buffer than L0 file + // so that we can ensure manual flush always + // go before background flush happens. + setWriteBufferSize(L0_FILE_SIZE * 2). + // Disable auto L0 -> L1 compaction + setLevelZeroFileNumCompactionTrigger(20). + setTargetFileSizeBase(L0_FILE_SIZE * 100). + setTargetFileSizeMultiplier(1). + // To disable auto compaction + setMaxBytesForLevelBase(NUM_L0_FILES * L0_FILE_SIZE * 100). + setMaxBytesForLevelMultiplier(2). + setDisableAutoCompactions(true); + 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; @@ -634,7 +501,7 @@ public class RocksDBTest { rand.nextBytes(value); db.put(String.format("%020d", int_key).getBytes(), - value); + value); } db.flush(new FlushOptions().setWaitForFlush(true)); // Make sure we do create one more L0 files. @@ -665,141 +532,121 @@ 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 columnFamilyHandles = - new ArrayList<>(); - try { - final int NUM_KEYS_PER_L0_FILE = 100; - final int KEY_SIZE = 20; - final int VALUE_SIZE = 300; - final int L0_FILE_SIZE = - NUM_KEYS_PER_L0_FILE * (KEY_SIZE + VALUE_SIZE); - final int NUM_L0_FILES = 10; - final int TEST_SCALE = 5; - final int KEY_INTERVAL = 100; - opt = new DBOptions(). - setCreateIfMissing(true). - setCreateMissingColumnFamilies(true); - List columnFamilyDescriptors = - new ArrayList<>(); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - RocksDB.DEFAULT_COLUMN_FAMILY)); - columnFamilyDescriptors.add(new ColumnFamilyDescriptor( - "new_cf".getBytes(), - new ColumnFamilyOptions(). - setCompactionStyle(CompactionStyle.LEVEL). - setNumLevels(5). - // a slightly bigger write buffer than L0 file - // so that we can ensure manual flush always - // go before background flush happens. - setWriteBufferSize(L0_FILE_SIZE * 2). - // Disable auto L0 -> L1 compaction - setLevelZeroFileNumCompactionTrigger(20). - setTargetFileSizeBase(L0_FILE_SIZE * 100). - setTargetFileSizeMultiplier(1). - // To disable auto compaction - setMaxBytesForLevelBase(NUM_L0_FILES * L0_FILE_SIZE * 100). - setMaxBytesForLevelMultiplier(2). - setDisableAutoCompactions(true))); + final int NUM_KEYS_PER_L0_FILE = 100; + final int KEY_SIZE = 20; + final int VALUE_SIZE = 300; + final int L0_FILE_SIZE = + NUM_KEYS_PER_L0_FILE * (KEY_SIZE + VALUE_SIZE); + final int NUM_L0_FILES = 10; + final int TEST_SCALE = 5; + final int KEY_INTERVAL = 100; + + try (final DBOptions opt = new DBOptions(). + setCreateIfMissing(true). + setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions new_cf_opts = new ColumnFamilyOptions(). + setCompactionStyle(CompactionStyle.LEVEL). + setNumLevels(5). + // a slightly bigger write buffer than L0 file + // so that we can ensure manual flush always + // go before background flush happens. + setWriteBufferSize(L0_FILE_SIZE * 2). + // Disable auto L0 -> L1 compaction + setLevelZeroFileNumCompactionTrigger(20). + setTargetFileSizeBase(L0_FILE_SIZE * 100). + setTargetFileSizeMultiplier(1). + // To disable auto compaction + setMaxBytesForLevelBase(NUM_L0_FILES * L0_FILE_SIZE * 100). + setMaxBytesForLevelMultiplier(2). + setDisableAutoCompactions(true) + ) { + final List columnFamilyDescriptors = + Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes(), new_cf_opts) + ); + + final List columnFamilyHandles = new ArrayList<>(); // open database - db = RocksDB.open(opt, + try (final RocksDB db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, - columnFamilyHandles); - // fill database with key/value pairs - byte[] value = new byte[VALUE_SIZE]; - int int_key = 0; - for (int round = 0; round < 5; ++round) { - int initial_key = int_key; - for (int f = 1; f <= NUM_L0_FILES; ++f) { - for (int i = 0; i < NUM_KEYS_PER_L0_FILE; ++i) { - int_key += KEY_INTERVAL; - rand.nextBytes(value); + columnFamilyHandles)) { + try { + // fill database with key/value pairs + byte[] value = new byte[VALUE_SIZE]; + int int_key = 0; + for (int round = 0; round < 5; ++round) { + int initial_key = int_key; + for (int f = 1; f <= NUM_L0_FILES; ++f) { + for (int i = 0; i < NUM_KEYS_PER_L0_FILE; ++i) { + int_key += KEY_INTERVAL; + rand.nextBytes(value); - db.put(columnFamilyHandles.get(1), - String.format("%020d", int_key).getBytes(), - value); + db.put(columnFamilyHandles.get(1), + String.format("%020d", int_key).getBytes(), + value); + } + db.flush(new FlushOptions().setWaitForFlush(true), + columnFamilyHandles.get(1)); + // Make sure we do create one more L0 files. + assertThat( + db.getProperty(columnFamilyHandles.get(1), + "rocksdb.num-files-at-level0")). + isEqualTo("" + f); + } + + // Compact all L0 files we just created + db.compactRange( + columnFamilyHandles.get(1), + String.format("%020d", initial_key).getBytes(), + String.format("%020d", int_key - 1).getBytes()); + // Making sure there isn't any L0 files. + assertThat( + db.getProperty(columnFamilyHandles.get(1), + "rocksdb.num-files-at-level0")). + isEqualTo("0"); + // Making sure there are some L1 files. + // Here we only use != 0 instead of a specific number + // as we don't want the test make any assumption on + // how compaction works. + assertThat( + db.getProperty(columnFamilyHandles.get(1), + "rocksdb.num-files-at-level1")). + isNotEqualTo("0"); + // Because we only compacted those keys we issued + // in this round, there shouldn't be any L1 -> L2 + // compaction. So we expect zero L2 files here. + assertThat( + db.getProperty(columnFamilyHandles.get(1), + "rocksdb.num-files-at-level2")). + isEqualTo("0"); + } + } finally { + for (final ColumnFamilyHandle handle : columnFamilyHandles) { + handle.close(); } - db.flush(new FlushOptions().setWaitForFlush(true), - columnFamilyHandles.get(1)); - // Make sure we do create one more L0 files. - assertThat( - db.getProperty(columnFamilyHandles.get(1), - "rocksdb.num-files-at-level0")). - isEqualTo("" + f); } - - // Compact all L0 files we just created - db.compactRange( - columnFamilyHandles.get(1), - String.format("%020d", initial_key).getBytes(), - String.format("%020d", int_key - 1).getBytes()); - // Making sure there isn't any L0 files. - assertThat( - db.getProperty(columnFamilyHandles.get(1), - "rocksdb.num-files-at-level0")). - isEqualTo("0"); - // Making sure there are some L1 files. - // Here we only use != 0 instead of a specific number - // as we don't want the test make any assumption on - // how compaction works. - assertThat( - db.getProperty(columnFamilyHandles.get(1), - "rocksdb.num-files-at-level1")). - isNotEqualTo("0"); - // Because we only compacted those keys we issued - // in this round, there shouldn't be any L1 -> L2 - // compaction. So we expect zero L2 files here. - assertThat( - db.getProperty(columnFamilyHandles.get(1), - "rocksdb.num-files-at-level2")). - isEqualTo("0"); - } - } finally { - for (ColumnFamilyHandle handle : columnFamilyHandles) { - handle.dispose(); - } - 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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/RocksEnvTest.java b/java/src/test/java/org/rocksdb/RocksEnvTest.java index a051a3562..d89570aad 100644 --- a/java/src/test/java/org/rocksdb/RocksEnvTest.java +++ b/java/src/test/java/org/rocksdb/RocksEnvTest.java @@ -17,22 +17,23 @@ public class RocksEnvTest { new RocksMemoryResource(); @Test - public void rocksEnv(){ - Env rocksEnv = RocksEnv.getDefault(); - rocksEnv.setBackgroundThreads(5); - // default rocksenv will always return zero for flush pool - // no matter what was set via setBackgroundThreads - assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.FLUSH_POOL)). - isEqualTo(0); - rocksEnv.setBackgroundThreads(5, RocksEnv.FLUSH_POOL); - // default rocksenv will always return zero for flush pool - // no matter what was set via setBackgroundThreads - assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.FLUSH_POOL)). - isEqualTo(0); - rocksEnv.setBackgroundThreads(5, RocksEnv.COMPACTION_POOL); - // default rocksenv will always return zero for compaction pool - // no matter what was set via setBackgroundThreads - assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.COMPACTION_POOL)). - isEqualTo(0); + public void rocksEnv() { + 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 + assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.FLUSH_POOL)). + isEqualTo(0); + rocksEnv.setBackgroundThreads(5, RocksEnv.FLUSH_POOL); + // default rocksenv will always return zero for flush pool + // no matter what was set via setBackgroundThreads + assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.FLUSH_POOL)). + isEqualTo(0); + rocksEnv.setBackgroundThreads(5, RocksEnv.COMPACTION_POOL); + // default rocksenv will always return zero for compaction pool + // no matter what was set via setBackgroundThreads + assertThat(rocksEnv.getThreadPoolQueueLen(RocksEnv.COMPACTION_POOL)). + isEqualTo(0); + } } } diff --git a/java/src/test/java/org/rocksdb/RocksIteratorTest.java b/java/src/test/java/org/rocksdb/RocksIteratorTest.java index eb841d3e6..4471df9cc 100644 --- a/java/src/test/java/org/rocksdb/RocksIteratorTest.java +++ b/java/src/test/java/org/rocksdb/RocksIteratorTest.java @@ -22,50 +22,36 @@ 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) - .setCreateMissingColumnFamilies(true); - db = RocksDB.open(options, - dbFolder.getRoot().getAbsolutePath()); + try (final Options options = new Options() + .setCreateIfMissing(true) + .setCreateMissingColumnFamilies(true); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { db.put("key1".getBytes(), "value1".getBytes()); db.put("key2".getBytes(), "value2".getBytes()); - iterator = db.newIterator(); - - iterator.seekToFirst(); - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo("key1".getBytes()); - assertThat(iterator.value()).isEqualTo("value1".getBytes()); - iterator.next(); - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo("key2".getBytes()); - assertThat(iterator.value()).isEqualTo("value2".getBytes()); - iterator.next(); - assertThat(iterator.isValid()).isFalse(); - iterator.seekToLast(); - iterator.prev(); - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo("key1".getBytes()); - assertThat(iterator.value()).isEqualTo("value1".getBytes()); - iterator.seekToFirst(); - iterator.seekToLast(); - assertThat(iterator.isValid()).isTrue(); - 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(); + try (final RocksIterator iterator = db.newIterator()) { + iterator.seekToFirst(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("key1".getBytes()); + assertThat(iterator.value()).isEqualTo("value1".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("key2".getBytes()); + assertThat(iterator.value()).isEqualTo("value2".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isFalse(); + iterator.seekToLast(); + iterator.prev(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("key1".getBytes()); + assertThat(iterator.value()).isEqualTo("value1".getBytes()); + iterator.seekToFirst(); + iterator.seekToLast(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("key2".getBytes()); + assertThat(iterator.value()).isEqualTo("value2".getBytes()); + iterator.status(); } } } diff --git a/java/src/test/java/org/rocksdb/RocksMemEnvTest.java b/java/src/test/java/org/rocksdb/RocksMemEnvTest.java index 7530e51b1..141f7f850 100644 --- a/java/src/test/java/org/rocksdb/RocksMemEnvTest.java +++ b/java/src/test/java/org/rocksdb/RocksMemEnvTest.java @@ -33,73 +33,55 @@ 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]); + } - // write key/value pairs using MemEnv - for (int i=0; i < keys.length; i++) { - db.put(keys[i], values[i]); + // read key/value pairs using MemEnv + for (int i = 0; i < keys.length; i++) { + assertThat(db.get(keys[i])).isEqualTo(values[i]); + } + + // Check iterator access + try (final RocksIterator iterator = db.newIterator()) { + iterator.seekToFirst(); + for (int i = 0; i < keys.length; i++) { + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo(keys[i]); + assertThat(iterator.value()).isEqualTo(values[i]); + iterator.next(); + } + // reached end of database + assertThat(iterator.isValid()).isFalse(); + } + + // flush + db.flush(flushOptions); + + // read key/value pairs after flush using MemEnv + for (int i = 0; i < keys.length; i++) { + assertThat(db.get(keys[i])).isEqualTo(values[i]); + } } - // read key/value pairs using MemEnv - for (int i=0; i < keys.length; i++) { - assertThat(db.get(keys[i])).isEqualTo(values[i]); - } - - // Check iterator access - RocksIterator iterator = db.newIterator(); - iterator.seekToFirst(); - for (int i=0; i < keys.length; i++) { - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo(keys[i]); - assertThat(iterator.value()).isEqualTo(values[i]); - iterator.next(); - } - // reached end of database - assertThat(iterator.isValid()).isFalse(); - iterator.dispose(); - - // flush - db.flush(flushOptions); - - // read key/value pairs after flush using MemEnv - 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"); - // 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(); + 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]); + } } } } @@ -125,27 +107,22 @@ 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++) { + for (int i = 0; i < keys.length; i++) { db.put(keys[i], values[i]); otherDb.put(otherKeys[i], values[i]); } // verify key/value pairs after flush using MemEnv - for (int i=0; i < keys.length; i++) { + for (int i = 0; i < keys.length; i++) { // verify db assertThat(db.get(otherKeys[i])).isNull(); assertThat(db.get(keys[i])).isEqualTo(values[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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/RocksMemoryResource.java b/java/src/test/java/org/rocksdb/RocksMemoryResource.java index de9ba0d6b..6fd1c7e66 100644 --- a/java/src/test/java/org/rocksdb/RocksMemoryResource.java +++ b/java/src/test/java/org/rocksdb/RocksMemoryResource.java @@ -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 { diff --git a/java/src/test/java/org/rocksdb/SliceTest.java b/java/src/test/java/org/rocksdb/SliceTest.java index 51f542fa5..952c9ab86 100644 --- a/java/src/test/java/org/rocksdb/SliceTest.java +++ b/java/src/test/java/org/rocksdb/SliceTest.java @@ -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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/SnapshotTest.java b/java/src/test/java/org/rocksdb/SnapshotTest.java index 19e4c5021..581bae50b 100644 --- a/java/src/test/java/org/rocksdb/SnapshotTest.java +++ b/java/src/test/java/org/rocksdb/SnapshotTest.java @@ -22,195 +22,147 @@ 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(); - assertThat(snapshot.getSequenceNumber()).isGreaterThan(0); - assertThat(snapshot.getSequenceNumber()).isEqualTo(1); - readOptions = new ReadOptions(); - // set snapshot in ReadOptions - readOptions.setSnapshot(snapshot); - // retrieve key value pair - assertThat(new String(db.get("key".getBytes()))). - isEqualTo("value"); - // retrieve key value pair created before - // the snapshot was made - assertThat(new String(db.get(readOptions, - "key".getBytes()))).isEqualTo("value"); - // add new key/value pair - db.put("newkey".getBytes(), "newvalue".getBytes()); - // using no snapshot the latest db entries - // will be taken into account - assertThat(new String(db.get("newkey".getBytes()))). - isEqualTo("newvalue"); - // snapshopot was created before newkey - assertThat(db.get(readOptions, "newkey".getBytes())). - isNull(); - // Retrieve snapshot from read options - Snapshot sameSnapshot = readOptions.snapshot(); - readOptions.setSnapshot(sameSnapshot); - // results must be the same with new Snapshot - // instance using the same native pointer - assertThat(new String(db.get(readOptions, - "key".getBytes()))).isEqualTo("value"); - // update key value pair to newvalue - db.put("key".getBytes(), "newvalue".getBytes()); - // read with previously created snapshot will - // read previous version of key value pair - assertThat(new String(db.get(readOptions, - "key".getBytes()))).isEqualTo("value"); - // read for newkey using the snapshot must be - // null - assertThat(db.get(readOptions, "newkey".getBytes())). - isNull(); - // setting null to snapshot in ReadOptions leads - // to no Snapshot being used. - readOptions.setSnapshot(null); - assertThat(new String(db.get(readOptions, - "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(); + try (final Snapshot snapshot = db.getSnapshot()) { + assertThat(snapshot.getSequenceNumber()).isGreaterThan(0); + assertThat(snapshot.getSequenceNumber()).isEqualTo(1); + 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"); + // retrieve key value pair created before + // the snapshot was made + assertThat(new String(db.get(readOptions, + "key".getBytes()))).isEqualTo("value"); + // add new key/value pair + db.put("newkey".getBytes(), "newvalue".getBytes()); + // using no snapshot the latest db entries + // will be taken into account + assertThat(new String(db.get("newkey".getBytes()))). + isEqualTo("newvalue"); + // snapshopot was created before newkey + assertThat(db.get(readOptions, "newkey".getBytes())). + isNull(); + // Retrieve snapshot from read options + try (final Snapshot sameSnapshot = readOptions.snapshot()) { + readOptions.setSnapshot(sameSnapshot); + // results must be the same with new Snapshot + // instance using the same native pointer + assertThat(new String(db.get(readOptions, + "key".getBytes()))).isEqualTo("value"); + // update key value pair to newvalue + db.put("key".getBytes(), "newvalue".getBytes()); + // read with previously created snapshot will + // read previous version of key value pair + assertThat(new String(db.get(readOptions, + "key".getBytes()))).isEqualTo("value"); + // read for newkey using the snapshot must be + // null + assertThat(db.get(readOptions, "newkey".getBytes())). + isNull(); + // setting null to snapshot in ReadOptions leads + // to no Snapshot being used. + readOptions.setSnapshot(null); + assertThat(new String(db.get(readOptions, + "newkey".getBytes()))).isEqualTo("newvalue"); + // release Snapshot + db.releaseSnapshot(snapshot); + } + } } } } @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); - db.put("key2".getBytes(), "value2".getBytes()); + 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(); - iterator.seekToFirst(); - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo("key".getBytes()); - iterator.next(); - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo("key2".getBytes()); - iterator.next(); - assertThat(iterator.isValid()).isFalse(); + // iterate over current state of db + try (final RocksIterator iterator = db.newIterator()) { + iterator.seekToFirst(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("key".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("key2".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isFalse(); + } - // iterate using a snapshot - snapshotIterator = db.newIterator(readOptions); - snapshotIterator.seekToFirst(); - assertThat(snapshotIterator.isValid()).isTrue(); - assertThat(snapshotIterator.key()).isEqualTo("key".getBytes()); - snapshotIterator.next(); - assertThat(snapshotIterator.isValid()).isFalse(); + // iterate using a snapshot + 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(); + // release Snapshot + db.releaseSnapshot(snapshot); } } } @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); - db.put("key2".getBytes(), "value2".getBytes()); + 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()); - iterator.seekToFirst(); - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo("key".getBytes()); - iterator.next(); - assertThat(iterator.isValid()).isTrue(); - assertThat(iterator.key()).isEqualTo("key2".getBytes()); - iterator.next(); - assertThat(iterator.isValid()).isFalse(); + // iterate over current state of column family + try (final RocksIterator iterator = db.newIterator( + db.getDefaultColumnFamily())) { + iterator.seekToFirst(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("key".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isTrue(); + 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); - snapshotIterator.seekToFirst(); - assertThat(snapshotIterator.isValid()).isTrue(); - assertThat(snapshotIterator.key()).isEqualTo("key".getBytes()); - snapshotIterator.next(); - assertThat(snapshotIterator.isValid()).isFalse(); + // iterate using a snapshot on default column family + try (final RocksIterator snapshotIterator = db.newIterator( + db.getDefaultColumnFamily(), 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(); + // release Snapshot + db.releaseSnapshot(snapshot); + } } } } diff --git a/java/src/test/java/org/rocksdb/StatisticsCollectorTest.java b/java/src/test/java/org/rocksdb/StatisticsCollectorTest.java index 0feaa4237..9f014d1d3 100644 --- a/java/src/test/java/org/rocksdb/StatisticsCollectorTest.java +++ b/java/src/test/java/org/rocksdb/StatisticsCollectorTest.java @@ -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(); + try (final Options opt = new Options() + .createStatistics() + .setCreateIfMissing(true); + final RocksDB db = RocksDB.open(opt, + dbFolder.getRoot().getAbsolutePath())) { + final Statistics stats = opt.statisticsPtr(); - db = RocksDB.open(opt, - dbFolder.getRoot().getAbsolutePath()); + final StatsCallbackMock callback = new StatsCallbackMock(); + final StatsCollectorInput statsInput = + new StatsCollectorInput(stats, callback); - StatsCallbackMock callback = new StatsCallbackMock(); - StatsCollectorInput statsInput = new StatsCollectorInput(stats, callback); - - StatisticsCollector statsCollector = new StatisticsCollector( + 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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/TransactionLogIteratorTest.java b/java/src/test/java/org/rocksdb/TransactionLogIteratorTest.java index 1de2efdea..b619258ec 100644 --- a/java/src/test/java/org/rocksdb/TransactionLogIteratorTest.java +++ b/java/src/test/java/org/rocksdb/TransactionLogIteratorTest.java @@ -17,43 +17,27 @@ 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); + try (final Options options = new Options() + .setCreateIfMissing(true) + .setWalTtlSeconds(1000) + .setWalSizeLimitMB(10); + final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { - db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); - - for (int i = 0; i < numberOfPuts; i++){ + for (int i = 0; i < numberOfPuts; i++) { db.put(String.valueOf(i).getBytes(), String.valueOf(i).getBytes()); } @@ -65,117 +49,89 @@ 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()); + } + // the latest sequence number is 10 because + // (5 + 5) puts were written beforehand + assertThat(db.getLatestSequenceNumber()). + isEqualTo(numberOfPuts + numberOfPuts); - for (int i = 0; i < numberOfPuts; i++){ - db.put(cfHandle, String.valueOf(i).getBytes(), - String.valueOf(i).getBytes()); - } - // the latest sequence number is 10 because - // (5 + 5) puts were written beforehand - assertThat(db.getLatestSequenceNumber()). - isEqualTo(numberOfPuts + numberOfPuts); + // Get updates since the beginning + try (final TransactionLogIterator transactionLogIterator = + db.getUpdatesSince(0)) { + assertThat(transactionLogIterator.isValid()).isTrue(); + transactionLogIterator.status(); - // Get updates since the beginning - transactionLogIterator = db.getUpdatesSince(0); - assertThat(transactionLogIterator.isValid()).isTrue(); - transactionLogIterator.status(); - - // The first sequence number is 1 - 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(); + // The first sequence number is 1 + final TransactionLogIterator.BatchResult batchResult = + transactionLogIterator.getBatch(); + assertThat(batchResult.sequenceNumber()).isEqualTo(1); + } } } } @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); + 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 = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); db.put("key1".getBytes(), "value1".getBytes()); // Get updates since the beginning - transactionLogIterator = db.getUpdatesSince(0); - transactionLogIterator.status(); - assertThat(transactionLogIterator.isValid()).isTrue(); - transactionLogIterator.next(); - assertThat(transactionLogIterator.isValid()).isFalse(); - transactionLogIterator.status(); - db.put("key2".getBytes(), "value2".getBytes()); - 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 { - 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()); - 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()); - assertThat(db.getLatestSequenceNumber()).isEqualTo(numberOfKeys); - - transactionLogIterator = db.getUpdatesSince(0); - for (int i = 0; i < numberOfKeys; i++) { + try (final TransactionLogIterator transactionLogIterator = + db.getUpdatesSince(0)) { transactionLogIterator.status(); assertThat(transactionLogIterator.isValid()).isTrue(); transactionLogIterator.next(); + assertThat(transactionLogIterator.isValid()).isFalse(); + transactionLogIterator.status(); + db.put("key2".getBytes(), "value2".getBytes()); + transactionLogIterator.next(); + transactionLogIterator.status(); + assertThat(transactionLogIterator.isValid()).isTrue(); } - } finally { - if (transactionLogIterator != null) { - transactionLogIterator.dispose(); + } + } + + @Test + public void transactionLogIteratorCheckAfterRestart() + throws RocksDBException { + final int numberOfKeys = 2; + 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)); + } - if (db != null) { - db.close(); - } - if (options != null) { - options.dispose(); + + // reopen + try (final RocksDB db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath())) { + assertThat(db.getLatestSequenceNumber()).isEqualTo(numberOfKeys); + + try (final TransactionLogIterator transactionLogIterator = + db.getUpdatesSince(0)) { + for (int i = 0; i < numberOfKeys; i++) { + transactionLogIterator.status(); + assertThat(transactionLogIterator.isValid()).isTrue(); + transactionLogIterator.next(); + } + } } } } diff --git a/java/src/test/java/org/rocksdb/TtlDBTest.java b/java/src/test/java/org/rocksdb/TtlDBTest.java index 934363a87..6539ea4e0 100644 --- a/java/src/test/java/org/rocksdb/TtlDBTest.java +++ b/java/src/test/java/org/rocksdb/TtlDBTest.java @@ -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,108 +27,74 @@ 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 cfNames = - new ArrayList<>(); - List columnFamilyHandleList = - new ArrayList<>(); - cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); - cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); - List 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 cfNames = Arrays.asList( + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf".getBytes()) + ); + final List ttlValues = Arrays.asList(0, 1); - try { - dbOptions = new DBOptions(). - setCreateMissingColumnFamilies(true). - setCreateIfMissing(true); - ttlDB = TtlDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(), - cfNames, columnFamilyHandleList, ttlValues, false); + final List 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 { + ttlDB.put("key".getBytes(), "value".getBytes()); + assertThat(ttlDB.get("key".getBytes())). + isEqualTo("value".getBytes()); + ttlDB.put(columnFamilyHandleList.get(1), "key".getBytes(), + "value".getBytes()); + assertThat(ttlDB.get(columnFamilyHandleList.get(1), + "key".getBytes())).isEqualTo("value".getBytes()); + TimeUnit.SECONDS.sleep(2); - ttlDB.put("key".getBytes(), "value".getBytes()); - assertThat(ttlDB.get("key".getBytes())). - isEqualTo("value".getBytes()); - ttlDB.put(columnFamilyHandleList.get(1), "key".getBytes(), - "value".getBytes()); - assertThat(ttlDB.get(columnFamilyHandleList.get(1), - "key".getBytes())).isEqualTo("value".getBytes()); - TimeUnit.SECONDS.sleep(2); + ttlDB.compactRange(); + ttlDB.compactRange(columnFamilyHandleList.get(1)); - ttlDB.compactRange(); - ttlDB.compactRange(columnFamilyHandleList.get(1)); - - assertThat(ttlDB.get("key".getBytes())).isNotNull(); - assertThat(ttlDB.get(columnFamilyHandleList.get(1), - "key".getBytes())).isNull(); - - - } finally { - for (ColumnFamilyHandle columnFamilyHandle : - columnFamilyHandleList) { - columnFamilyHandle.dispose(); - } - if (ttlDB != null) { - ttlDB.close(); - } - if (dbOptions != null) { - dbOptions.dispose(); + assertThat(ttlDB.get("key".getBytes())).isNotNull(); + assertThat(ttlDB.get(columnFamilyHandleList.get(1), + "key".getBytes())).isNull(); + } finally { + for (final ColumnFamilyHandle columnFamilyHandle : + columnFamilyHandleList) { + columnFamilyHandle.close(); + } } } } @@ -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, - dbFolder.getRoot().getAbsolutePath()); - columnFamilyHandle = ttlDB.createColumnFamilyWithTtl( - new ColumnFamilyDescriptor("new_cf".getBytes()), 1); + try (final Options options = new Options().setCreateIfMissing(true); + final TtlDB ttlDB = TtlDB.open(options, + dbFolder.getRoot().getAbsolutePath()); + 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(); - } } } } diff --git a/java/src/test/java/org/rocksdb/WriteBatchHandlerTest.java b/java/src/test/java/org/rocksdb/WriteBatchHandlerTest.java index 257ef6438..35c63f2af 100644 --- a/java/src/test/java/org/rocksdb/WriteBatchHandlerTest.java +++ b/java/src/test/java/org/rocksdb/WriteBatchHandlerTest.java @@ -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>> testEvents = new ArrayList<>(); - testEvents.add(new Tuple<>(Action.DELETE, - new Tuple("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(null, "log1".getBytes()))); - testEvents.add(new Tuple<>(Action.MERGE, - new Tuple<>("k2".getBytes(), "v22".getBytes()))); - testEvents.add(new Tuple<>(Action.DELETE, - new Tuple("k3".getBytes(), null))); + // setup test data + final List>> testEvents = Arrays.asList( + new Tuple<>(Action.DELETE, + new Tuple("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(null, "log1".getBytes())), + new Tuple<>(Action.MERGE, + new Tuple<>("k2".getBytes(), "v22".getBytes())), + new Tuple<>(Action.DELETE, + new Tuple("k3".getBytes(), null)) + ); - // load test data to the write batch - batch = new WriteBatch(); + // load test data to the write batch + try (final WriteBatch batch = new WriteBatch()) { for (final Tuple> testEvent : testEvents) { final Tuple data = testEvent.value; switch (testEvent.key) { @@ -67,29 +65,27 @@ public class WriteBatchHandlerTest { } } - // attempt to read test data back from the WriteBatch by iterating with a handler - handler = new CapturingWriteBatchHandler(); - batch.iterate(handler); + // 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>> actualEvents = handler.getEvents(); - assertThat(testEvents.size()).isSameAs(actualEvents.size()); + // compare the results to the test data + final List>> 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(); + for (int i = 0; i < testEvents.size(); i++) { + assertThat(equals(testEvents.get(i), actualEvents.get(i))).isTrue(); + } } } } - private static boolean equals(final Tuple> expected, - final Tuple> actual) { + private static boolean equals( + final Tuple> expected, + final Tuple> 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>> events = new ArrayList<>(); + private final List>> 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(key, null))); + events.add(new Tuple<>(Action.DELETE, + new Tuple(key, null))); } @Override public void logData(final byte[] blob) { - events.add(new Tuple<>(Action.LOG, new Tuple(null, blob))); + events.add(new Tuple<>(Action.LOG, + new Tuple(null, blob))); } } } diff --git a/java/src/test/java/org/rocksdb/WriteBatchTest.java b/java/src/test/java/org/rocksdb/WriteBatchTest.java index b7fb50533..fdfb02444 100644 --- a/java/src/test/java/org/rocksdb/WriteBatchTest.java +++ b/java/src/test/java/org/rocksdb/WriteBatchTest.java @@ -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. - * + *

* Not ported yet: - * + *

* Continue(); * PutGatherSlices(); */ @@ -36,77 +36,83 @@ public class WriteBatchTest { @Test public void emptyWriteBatch() { - WriteBatch batch = new WriteBatch(); - assertThat(batch.count()).isEqualTo(0); + try (final WriteBatch batch = new WriteBatch()) { + assertThat(batch.count()).isEqualTo(0); + } } @Test public void multipleBatchOperations() throws UnsupportedEncodingException { - 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(). - isEqualTo(100); - assertThat(batch.count()).isEqualTo(3); - assertThat(new String(getContents(batch), "US-ASCII")). - isEqualTo("Put(baz, boo)@102" + - "Delete(box)@101" + - "Put(foo, bar)@100"); + 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(). + isEqualTo(100); + assertThat(batch.count()).isEqualTo(3); + assertThat(new String(getContents(batch), "US-ASCII")). + isEqualTo("Put(baz, boo)@102" + + "Delete(box)@101" + + "Put(foo, bar)@100"); + } } @Test public void testAppendOperation() throws UnsupportedEncodingException { - WriteBatch b1 = new WriteBatch(); - WriteBatch b2 = new WriteBatch(); - WriteBatchTestInternalHelper.setSequence(b1, 200); - WriteBatchTestInternalHelper.setSequence(b2, 300); - WriteBatchTestInternalHelper.append(b1, b2); - assertThat(getContents(b1).length).isEqualTo(0); - 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(b1.count()).isEqualTo(1); - b2.clear(); - b2.put("b".getBytes("US-ASCII"), "vb".getBytes("US-ASCII")); - WriteBatchTestInternalHelper.append(b1, b2); - assertThat(("Put(a, va)@200" + - "Put(b, vb)@201") - .equals(new String(getContents(b1), "US-ASCII"))); - assertThat(b1.count()).isEqualTo(2); - b2.remove("foo".getBytes("US-ASCII")); - WriteBatchTestInternalHelper.append(b1, b2); - assertThat(("Put(a, va)@200" + - "Put(b, vb)@202" + - "Put(b, vb)@201" + - "Delete(foo)@203") - .equals(new String(getContents(b1), "US-ASCII"))); - assertThat(b1.count()).isEqualTo(4); + try (final WriteBatch b1 = new WriteBatch(); + final WriteBatch b2 = new WriteBatch()) { + WriteBatchTestInternalHelper.setSequence(b1, 200); + WriteBatchTestInternalHelper.setSequence(b2, 300); + WriteBatchTestInternalHelper.append(b1, b2); + assertThat(getContents(b1).length).isEqualTo(0); + 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(b1.count()).isEqualTo(1); + b2.clear(); + b2.put("b".getBytes("US-ASCII"), "vb".getBytes("US-ASCII")); + WriteBatchTestInternalHelper.append(b1, b2); + assertThat(("Put(a, va)@200" + + "Put(b, vb)@201") + .equals(new String(getContents(b1), "US-ASCII"))); + assertThat(b1.count()).isEqualTo(2); + b2.remove("foo".getBytes("US-ASCII")); + WriteBatchTestInternalHelper.append(b1, b2); + assertThat(("Put(a, va)@200" + + "Put(b, vb)@202" + + "Put(b, vb)@201" + + "Delete(foo)@203") + .equals(new String(getContents(b1), "US-ASCII"))); + assertThat(b1.count()).isEqualTo(4); + } } @Test public void blobOperation() throws UnsupportedEncodingException { - 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")); - batch.putLogData("blob1".getBytes("US-ASCII")); - batch.remove("k2".getBytes("US-ASCII")); - batch.putLogData("blob2".getBytes("US-ASCII")); - batch.merge("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII")); - assertThat(batch.count()).isEqualTo(5); - assertThat(("Merge(foo, bar)@4" + - "Put(k1, v1)@0" + - "Delete(k2)@3" + - "Put(k2, v2)@1" + - "Put(k3, v3)@2") - .equals(new String(getContents(batch), "US-ASCII"))); + 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")); + batch.putLogData("blob1".getBytes("US-ASCII")); + batch.remove("k2".getBytes("US-ASCII")); + batch.putLogData("blob2".getBytes("US-ASCII")); + batch.merge("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII")); + assertThat(batch.count()).isEqualTo(5); + assertThat(("Merge(foo, bar)@4" + + "Put(k1, v1)@0" + + "Delete(k2)@3" + + "Put(k2, v2)@1" + + "Put(k3, v3)@2") + .equals(new String(getContents(batch), "US-ASCII"))); + } } static byte[] getContents(final WriteBatch wb) { @@ -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); } diff --git a/java/src/test/java/org/rocksdb/WriteBatchWithIndexTest.java b/java/src/test/java/org/rocksdb/WriteBatchWithIndexTest.java index 08cac9bce..74558fc2e 100644 --- a/java/src/test/java/org/rocksdb/WriteBatchWithIndexTest.java +++ b/java/src/test/java/org/rocksdb/WriteBatchWithIndexTest.java @@ -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,169 +87,121 @@ 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"; - final String k2 = "key2"; - final String v2 = "value2"; - final String k3 = "key3"; - final String v3 = "value3"; - final byte[] k1b = k1.getBytes(); - final byte[] v1b = v1.getBytes(); - final byte[] k2b = k2.getBytes(); - final byte[] v2b = v2.getBytes(); - final byte[] k3b = k3.getBytes(); - final byte[] v3b = v3.getBytes(); + final String k1 = "key1"; + final String v1 = "value1"; + final String k2 = "key2"; + final String v2 = "value2"; + final String k3 = "key3"; + final String v3 = "value3"; + final byte[] k1b = k1.getBytes(); + final byte[] v1b = v1.getBytes(); + final byte[] k2b = k2.getBytes(); + final byte[] v2b = v2.getBytes(); + final byte[] k3b = k3.getBytes(); + final byte[] v3b = v3.getBytes(); - //add put records - wbwi.put(k1b, v1b); - wbwi.put(k2b, v2b); - wbwi.put(k3b, v3b); + //add put records + wbwi.put(k1b, v1b); + wbwi.put(k2b, v2b); + wbwi.put(k3b, v3b); - //add a deletion record - final String k4 = "key4"; - final byte[] k4b = k4.getBytes(); - wbwi.remove(k4b); + //add a deletion record + final String k4 = "key4"; + final byte[] k4b = k4.getBytes(); + wbwi.remove(k4b); - WBWIRocksIterator.WriteEntry[] expected = { - new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, - new DirectSlice(k1), new DirectSlice(v1)), - new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, - new DirectSlice(k2), new DirectSlice(v2)), - new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, - new DirectSlice(k3), new DirectSlice(v3)), - new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.DELETE, - new DirectSlice(k4), DirectSlice.NONE) - }; + final WBWIRocksIterator.WriteEntry[] expected = { + new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, + new DirectSlice(k1), new DirectSlice(v1)), + new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, + new DirectSlice(k2), new DirectSlice(v2)), + new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, + new DirectSlice(k3), new DirectSlice(v3)), + new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.DELETE, + 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}; - //direct access - seek to key offsets - final int[] testOffsets = {2, 0, 1, 3}; + for (int i = 0; i < testOffsets.length; i++) { + final int testOffset = testOffsets[i]; + final byte[] key = toArray(expected[testOffset].getKey().data()); - for(int i = 0; i < testOffsets.length; i++) { - final int testOffset = testOffsets[i]; - final byte[] key = toArray(expected[testOffset].getKey().data()); + it.seek(key); + assertThat(it.isValid()).isTrue(); - it.seek(key); - assertThat(it.isValid()).isTrue(); + final WBWIRocksIterator.WriteEntry entry = it.entry(); + assertThat(entry.equals(expected[testOffset])).isTrue(); + } - final WBWIRocksIterator.WriteEntry entry = it.entry(); - assertThat(entry.equals(expected[testOffset])).isTrue(); - } + //forward iterative access + int i = 0; + for (it.seekToFirst(); it.isValid(); it.next()) { + assertThat(it.entry().equals(expected[i++])).isTrue(); + } - //forward iterative access - int i = 0; - for(it.seekToFirst(); it.isValid(); it.next()) { - assertThat(it.entry().equals(expected[i++])).isTrue(); - } - - //reverse iterative access - i = expected.length - 1; - for(it.seekToLast(); it.isValid(); it.prev()) { - assertThat(it.entry().equals(expected[i--])).isTrue(); - } - - } finally { - if(it != null) { - it.dispose(); + //reverse iterative access + i = expected.length - 1; + for (it.seekToLast(); it.isValid(); it.prev()) { + assertThat(it.entry().equals(expected[i--])).isTrue(); + } } } } @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); - //add zero byte value - wbwi.put(zeroByteValue, zeroByteValue); + final ByteBuffer buffer = ByteBuffer.allocateDirect(zeroByteValue.length); + buffer.put(zeroByteValue); - ByteBuffer buffer = ByteBuffer.allocateDirect(zeroByteValue.length); - buffer.put(zeroByteValue); + WBWIRocksIterator.WriteEntry[] expected = { + new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, + new DirectSlice(buffer, zeroByteValue.length), + new DirectSlice(buffer, zeroByteValue.length)) + }; - WBWIRocksIterator.WriteEntry[] expected = { - new WBWIRocksIterator.WriteEntry(WBWIRocksIterator.WriteType.PUT, - new DirectSlice(buffer, zeroByteValue.length), - new DirectSlice(buffer, zeroByteValue.length)) - }; - WBWIRocksIterator it = null; - try { - 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(); + try (final WBWIRocksIterator it = wbwi.newIterator()) { + it.seekToFirst(); + assertThat(it.entry().equals(expected[0])).isTrue(); + assertThat(it.entry().hashCode() == expected[0].hashCode()).isTrue(); } } } diff --git a/java/src/test/java/org/rocksdb/WriteOptionsTest.java b/java/src/test/java/org/rocksdb/WriteOptionsTest.java index 333a76194..c6af5c818 100644 --- a/java/src/test/java/org/rocksdb/WriteOptionsTest.java +++ b/java/src/test/java/org/rocksdb/WriteOptionsTest.java @@ -17,15 +17,16 @@ public class WriteOptionsTest { new RocksMemoryResource(); @Test - public void writeOptions(){ - WriteOptions writeOptions = new WriteOptions(); - writeOptions.setDisableWAL(true); - assertThat(writeOptions.disableWAL()).isTrue(); - writeOptions.setDisableWAL(false); - assertThat(writeOptions.disableWAL()).isFalse(); - writeOptions.setSync(true); - assertThat(writeOptions.sync()).isTrue(); - writeOptions.setSync(false); - assertThat(writeOptions.sync()).isFalse(); + public void writeOptions() { + try (final WriteOptions writeOptions = new WriteOptions()) { + writeOptions.setDisableWAL(true); + assertThat(writeOptions.disableWAL()).isTrue(); + writeOptions.setDisableWAL(false); + assertThat(writeOptions.disableWAL()).isFalse(); + writeOptions.setSync(true); + assertThat(writeOptions.sync()).isTrue(); + writeOptions.setSync(false); + assertThat(writeOptions.sync()).isFalse(); + } } }