From 74057d6d2d9fd288c0bb454cad786055714c3b32 Mon Sep 17 00:00:00 2001 From: fyrz Date: Fri, 7 Nov 2014 23:31:38 +0100 Subject: [PATCH] [RocksJava] Improved tests within RocksJava --- java/Makefile | 1 + java/org/rocksdb/RocksDB.java | 1 + java/org/rocksdb/RocksEnv.java | 3 +- .../test/BlockBasedTableConfigTest.java | 108 +++- java/org/rocksdb/test/DBOptionsTest.java | 379 ++++++++++++-- java/org/rocksdb/test/EnvironmentTest.java | 20 +- java/org/rocksdb/test/OptionsTest.java | 488 +++++++++++++++++- .../rocksdb/test/PlainTableConfigTest.java | 54 +- java/org/rocksdb/test/ReadOptionsTest.java | 10 +- java/org/rocksdb/test/RocksDBTest.java | 282 ++++++++++ java/org/rocksdb/test/SizeUnitTest.java | 28 + 11 files changed, 1302 insertions(+), 72 deletions(-) create mode 100644 java/org/rocksdb/test/RocksDBTest.java create mode 100644 java/org/rocksdb/test/SizeUnitTest.java diff --git a/java/Makefile b/java/Makefile index 3c7b8bb1a..99664c6ef 100644 --- a/java/Makefile +++ b/java/Makefile @@ -66,6 +66,7 @@ JAVA_TESTS = org.rocksdb.test.AbstractComparatorTest\ org.rocksdb.test.PlainTableConfigTest\ org.rocksdb.test.ReadOnlyTest\ org.rocksdb.test.ReadOptionsTest\ + org.rocksdb.test.RocksDBTest\ org.rocksdb.test.RocksEnvTest\ org.rocksdb.test.RocksIteratorTest\ org.rocksdb.test.SnapshotTest\ diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 730c1940d..5fcfd2ff4 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -103,6 +103,7 @@ public class RocksDB extends RocksObject { // This allows to use the rocksjni default Options instead of // the c++ one. Options options = new Options(); + options.setCreateIfMissing(true); return open(options, path); } diff --git a/java/org/rocksdb/RocksEnv.java b/java/org/rocksdb/RocksEnv.java index 5bbf4fb3d..bb19eb732 100644 --- a/java/org/rocksdb/RocksEnv.java +++ b/java/org/rocksdb/RocksEnv.java @@ -18,6 +18,7 @@ public class RocksEnv extends RocksObject { static { default_env_ = new RocksEnv(getDefaultEnvInternal()); + } private static native long getDefaultEnvInternal(); @@ -101,7 +102,7 @@ public class RocksEnv extends RocksObject { * {@link RocksObject} must implement to release their associated C++ * resource. */ - protected void disposeInternal() { + @Override protected void disposeInternal() { disposeInternal(nativeHandle_); } private native void disposeInternal(long handle); diff --git a/java/org/rocksdb/test/BlockBasedTableConfigTest.java b/java/org/rocksdb/test/BlockBasedTableConfigTest.java index 143a3fa14..241429542 100644 --- a/java/org/rocksdb/test/BlockBasedTableConfigTest.java +++ b/java/org/rocksdb/test/BlockBasedTableConfigTest.java @@ -18,65 +18,145 @@ public class BlockBasedTableConfigTest { new RocksMemoryResource(); @Test - public void blockBasedTableConfig() { - BlockBasedTableConfig blockBasedTableConfig = - new BlockBasedTableConfig(); + public void noBlockCache() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setNoBlockCache(true); assertThat(blockBasedTableConfig.noBlockCache()).isTrue(); + } + + @Test + public void blockCacheSize() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setBlockCacheSize(8 * 1024); assertThat(blockBasedTableConfig.blockCacheSize()). isEqualTo(8 * 1024); + } + + @Test + public void blockSizeDeviation() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setBlockSizeDeviation(12); assertThat(blockBasedTableConfig.blockSizeDeviation()). isEqualTo(12); + } + + @Test + public void blockRestartInterval() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setBlockRestartInterval(15); assertThat(blockBasedTableConfig.blockRestartInterval()). isEqualTo(15); + } + + @Test + public void wholeKeyFiltering() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setWholeKeyFiltering(false); assertThat(blockBasedTableConfig.wholeKeyFiltering()). isFalse(); + } + + @Test + public void cacheIndexAndFilterBlocks() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setCacheIndexAndFilterBlocks(true); assertThat(blockBasedTableConfig.cacheIndexAndFilterBlocks()). isTrue(); + + } + + @Test + public void hashIndexAllowCollision() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setHashIndexAllowCollision(false); assertThat(blockBasedTableConfig.hashIndexAllowCollision()). isFalse(); + } + + @Test + public void blockCacheCompressedSize() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setBlockCacheCompressedSize(40); assertThat(blockBasedTableConfig.blockCacheCompressedSize()). isEqualTo(40); + } + + @Test + public void checksumType() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setChecksumType(ChecksumType.kNoChecksum); blockBasedTableConfig.setChecksumType(ChecksumType.kxxHash); assertThat(blockBasedTableConfig.checksumType().equals( ChecksumType.kxxHash)); + } + + @Test + public void indexType() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); + assertThat(IndexType.values().length).isEqualTo(2); blockBasedTableConfig.setIndexType(IndexType.kHashSearch); assertThat(blockBasedTableConfig.indexType().equals( IndexType.kHashSearch)); + assertThat(IndexType.valueOf("kBinarySearch")).isNotNull(); + blockBasedTableConfig.setIndexType(IndexType.valueOf("kBinarySearch")); + assertThat(blockBasedTableConfig.indexType().equals( + IndexType.kBinarySearch)); + } + + @Test + public void blockCacheCompressedNumShardBits() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setBlockCacheCompressedNumShardBits(4); assertThat(blockBasedTableConfig.blockCacheCompressedNumShardBits()). isEqualTo(4); + } + + @Test + public void cacheNumShardBits() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setCacheNumShardBits(5); assertThat(blockBasedTableConfig.cacheNumShardBits()). isEqualTo(5); + } + + @Test + public void blockSize() { + BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setBlockSize(10); assertThat(blockBasedTableConfig.blockSize()).isEqualTo(10); } + @Test public void blockBasedTableWithFilter() { - Options options = new Options(); - options.setTableFormatConfig( - new BlockBasedTableConfig().setFilter( - new BloomFilter(10))); - assertThat(options.tableFactoryName()). - isEqualTo("BlockBasedTable"); + Options options = null; + try { + options = new Options(); + 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 = new Options(); - options.setTableFormatConfig( - new BlockBasedTableConfig().setFilter(null)); - assertThat(options.tableFactoryName()). - isEqualTo("BlockBasedTable"); + Options options = null; + try { + options = new Options(); + options.setTableFormatConfig( + new BlockBasedTableConfig().setFilter(null)); + assertThat(options.tableFactoryName()). + isEqualTo("BlockBasedTable"); + } finally { + if (options != null) { + options.dispose(); + } + } } } diff --git a/java/org/rocksdb/test/DBOptionsTest.java b/java/org/rocksdb/test/DBOptionsTest.java index 529a9b09b..9a15658e7 100644 --- a/java/org/rocksdb/test/DBOptionsTest.java +++ b/java/org/rocksdb/test/DBOptionsTest.java @@ -19,227 +19,508 @@ public class DBOptionsTest { public static final RocksMemoryResource rocksMemoryResource = new RocksMemoryResource(); - @Test - public void dbOptions() throws RocksDBException { - testDBOptions(new DBOptions()); - } + public static final Random rand = PlatformRandomHelper. + getPlatformSpecificRandomFactory(); - static void testDBOptions(DBOptionsInterface opt) throws RocksDBException { - Random rand = PlatformRandomHelper. - getPlatformSpecificRandomFactory(); - { // CreateIfMissing test + @Test + public void createIfMissing() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setCreateIfMissing(boolValue); assertThat(opt.createIfMissing()). isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // CreateMissingColumnFamilies test + @Test + public void createMissingColumnFamilies() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setCreateMissingColumnFamilies(boolValue); assertThat(opt.createMissingColumnFamilies()). isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // ErrorIfExists test + @Test + public void errorIfExists() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setErrorIfExists(boolValue); assertThat(opt.errorIfExists()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // ParanoidChecks test + @Test + public void paranoidChecks() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setParanoidChecks(boolValue); assertThat(opt.paranoidChecks()). isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { - // MaxTotalWalSize test + @Test + public void maxTotalWalSize() { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setMaxTotalWalSize(longValue); assertThat(opt.maxTotalWalSize()). isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // MaxOpenFiles test + @Test + public void maxOpenFiles() { + DBOptions opt = null; + try { + opt = new DBOptions(); int intValue = rand.nextInt(); opt.setMaxOpenFiles(intValue); assertThat(opt.maxOpenFiles()).isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // DisableDataSync test + @Test + public void disableDataSync() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setDisableDataSync(boolValue); assertThat(opt.disableDataSync()). isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // UseFsync test + @Test + public void useFsync() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setUseFsync(boolValue); assertThat(opt.useFsync()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // DbLogDir test + @Test + public void dbLogDir() { + DBOptions opt = null; + try { + opt = new DBOptions(); String str = "path/to/DbLogDir"; opt.setDbLogDir(str); assertThat(opt.dbLogDir()).isEqualTo(str); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // WalDir test + @Test + public void walDir() { + DBOptions opt = null; + try { + opt = new DBOptions(); String str = "path/to/WalDir"; opt.setWalDir(str); assertThat(opt.walDir()).isEqualTo(str); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // DeleteObsoleteFilesPeriodMicros test + @Test + public void deleteObsoleteFilesPeriodMicros() { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setDeleteObsoleteFilesPeriodMicros(longValue); assertThat(opt.deleteObsoleteFilesPeriodMicros()). isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // MaxBackgroundCompactions test + @Test + public void maxBackgroundCompactions() { + DBOptions opt = null; + try { + opt = new DBOptions(); int intValue = rand.nextInt(); opt.setMaxBackgroundCompactions(intValue); assertThat(opt.maxBackgroundCompactions()). isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // MaxBackgroundFlushes test + @Test + public void maxBackgroundFlushes() { + DBOptions opt = null; + try { + opt = new DBOptions(); int intValue = rand.nextInt(); opt.setMaxBackgroundFlushes(intValue); assertThat(opt.maxBackgroundFlushes()). isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // MaxLogFileSize test + @Test + public void maxLogFileSize() throws RocksDBException { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setMaxLogFileSize(longValue); assertThat(opt.maxLogFileSize()).isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // LogFileTimeToRoll test + @Test + public void logFileTimeToRoll() throws RocksDBException { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setLogFileTimeToRoll(longValue); assertThat(opt.logFileTimeToRoll()). isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // KeepLogFileNum test + @Test + public void keepLogFileNum() throws RocksDBException { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setKeepLogFileNum(longValue); assertThat(opt.keepLogFileNum()).isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // MaxManifestFileSize test + @Test + public void maxManifestFileSize() { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setMaxManifestFileSize(longValue); assertThat(opt.maxManifestFileSize()). isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // TableCacheNumshardbits test + @Test + public void tableCacheNumshardbits() { + DBOptions opt = null; + try { + opt = new DBOptions(); int intValue = rand.nextInt(); opt.setTableCacheNumshardbits(intValue); assertThat(opt.tableCacheNumshardbits()). isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // TableCacheRemoveScanCountLimit test + @Test + public void tableCacheRemoveScanCountLimit() { + DBOptions opt = null; + try { + opt = new DBOptions(); int intValue = rand.nextInt(); opt.setTableCacheRemoveScanCountLimit(intValue); assertThat(opt.tableCacheRemoveScanCountLimit()). isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // WalSizeLimitMB test + @Test + public void walSizeLimitMB() { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setWalSizeLimitMB(longValue); assertThat(opt.walSizeLimitMB()).isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // WalTtlSeconds test + @Test + public void walTtlSeconds() { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setWalTtlSeconds(longValue); assertThat(opt.walTtlSeconds()).isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // ManifestPreallocationSize test + @Test + public void manifestPreallocationSize() throws RocksDBException { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setManifestPreallocationSize(longValue); assertThat(opt.manifestPreallocationSize()). isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // AllowOsBuffer test + @Test + public void allowOsBuffer() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setAllowOsBuffer(boolValue); assertThat(opt.allowOsBuffer()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // AllowMmapReads test + @Test + public void allowMmapReads() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setAllowMmapReads(boolValue); assertThat(opt.allowMmapReads()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // AllowMmapWrites test + @Test + public void allowMmapWrites() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setAllowMmapWrites(boolValue); assertThat(opt.allowMmapWrites()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // IsFdCloseOnExec test + @Test + public void isFdCloseOnExec() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setIsFdCloseOnExec(boolValue); assertThat(opt.isFdCloseOnExec()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // SkipLogErrorOnRecovery test + @Test + public void skipLogErrorOnRecovery() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setSkipLogErrorOnRecovery(boolValue); assertThat(opt.skipLogErrorOnRecovery()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // StatsDumpPeriodSec test + @Test + public void statsDumpPeriodSec() { + DBOptions opt = null; + try { + opt = new DBOptions(); int intValue = rand.nextInt(); opt.setStatsDumpPeriodSec(intValue); assertThat(opt.statsDumpPeriodSec()).isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // AdviseRandomOnOpen test + @Test + public void adviseRandomOnOpen() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setAdviseRandomOnOpen(boolValue); assertThat(opt.adviseRandomOnOpen()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // UseAdaptiveMutex test + @Test + public void useAdaptiveMutex() { + DBOptions opt = null; + try { + opt = new DBOptions(); boolean boolValue = rand.nextBoolean(); opt.setUseAdaptiveMutex(boolValue); assertThat(opt.useAdaptiveMutex()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } } + } - { // BytesPerSync test + @Test + public void bytesPerSync() { + DBOptions opt = null; + try { + opt = new DBOptions(); long longValue = rand.nextLong(); opt.setBytesPerSync(longValue); assertThat(opt.bytesPerSync()).isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } } } @Test public void rateLimiterConfig() { - DBOptions options = new DBOptions(); - RateLimiterConfig rateLimiterConfig = - new GenericRateLimiterConfig(1000, 0, 1); - options.setRateLimiterConfig(rateLimiterConfig); - options.dispose(); - // Test with parameter initialization - DBOptions anotherOptions = new DBOptions(); - anotherOptions.setRateLimiterConfig( - new GenericRateLimiterConfig(1000)); - anotherOptions.dispose(); + DBOptions options = null; + DBOptions anotherOptions = null; + try { + options = new DBOptions(); + RateLimiterConfig rateLimiterConfig = + new GenericRateLimiterConfig(1000, 0, 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 diff --git a/java/org/rocksdb/test/EnvironmentTest.java b/java/org/rocksdb/test/EnvironmentTest.java index c6542afed..b5af069da 100644 --- a/java/org/rocksdb/test/EnvironmentTest.java +++ b/java/org/rocksdb/test/EnvironmentTest.java @@ -1,3 +1,7 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. package org.rocksdb.test; import org.junit.Test; @@ -17,6 +21,7 @@ public class EnvironmentTest { @Test public void mac32() { setEnvironmentClassFields("mac", "32"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".jnilib"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -28,6 +33,7 @@ public class EnvironmentTest { @Test public void mac64() { setEnvironmentClassFields("mac", "64"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".jnilib"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -40,6 +46,7 @@ public class EnvironmentTest { public void nix32() { // Linux setEnvironmentClassFields("Linux", "32"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".so"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -48,6 +55,7 @@ public class EnvironmentTest { isEqualTo("librocksdbjni.so"); // UNIX setEnvironmentClassFields("Unix", "32"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".so"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -56,6 +64,7 @@ public class EnvironmentTest { isEqualTo("librocksdbjni.so"); // AIX setEnvironmentClassFields("aix", "32"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".so"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -67,6 +76,7 @@ public class EnvironmentTest { @Test public void nix64() { setEnvironmentClassFields("Linux", "x64"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".so"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -75,6 +85,7 @@ public class EnvironmentTest { isEqualTo("librocksdbjni.so"); // UNIX setEnvironmentClassFields("Unix", "x64"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".so"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -83,6 +94,7 @@ public class EnvironmentTest { isEqualTo("librocksdbjni.so"); // AIX setEnvironmentClassFields("aix", "x64"); + assertThat(Environment.isWindows()).isFalse(); assertThat(Environment.getJniLibraryExtension()). isEqualTo(".so"); assertThat(Environment.getJniLibraryName("rocksdb")). @@ -91,8 +103,14 @@ public class EnvironmentTest { isEqualTo("librocksdbjni.so"); } + @Test + public void detectWindows(){ + setEnvironmentClassFields("win", "x64"); + assertThat(Environment.isWindows()).isTrue(); + } + @Test(expected = UnsupportedOperationException.class) - public void failLinuxJniLibraryName(){ + public void failWinJniLibraryName(){ setEnvironmentClassFields("win", "x64"); Environment.getJniLibraryName("rocksdb"); } diff --git a/java/org/rocksdb/test/OptionsTest.java b/java/org/rocksdb/test/OptionsTest.java index 24dd5081c..a7241e822 100644 --- a/java/org/rocksdb/test/OptionsTest.java +++ b/java/org/rocksdb/test/OptionsTest.java @@ -19,14 +19,14 @@ public class OptionsTest { public static final RocksMemoryResource rocksMemoryResource = new RocksMemoryResource(); + public static final Random rand = PlatformRandomHelper. + getPlatformSpecificRandomFactory(); + @Test public void options() throws RocksDBException { Options opt = null; try { opt = new Options(); - Random rand = PlatformRandomHelper. - getPlatformSpecificRandomFactory(); - DBOptionsTest.testDBOptions(opt); { // WriteBufferSize test long longValue = rand.nextLong(); @@ -220,6 +220,484 @@ public class OptionsTest { } } + @Test + public void createIfMissing() { + Options opt = null; + try { + opt = new Options(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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"; + 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"; + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + opt.setTableCacheNumshardbits(intValue); + assertThat(opt.tableCacheNumshardbits()). + isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } + } + } + + @Test + public void tableCacheRemoveScanCountLimit() { + Options opt = null; + try { + opt = new Options(); + int intValue = rand.nextInt(); + opt.setTableCacheRemoveScanCountLimit(intValue); + assertThat(opt.tableCacheRemoveScanCountLimit()). + isEqualTo(intValue); + } finally { + if (opt != null) { + opt.dispose(); + } + } + } + + @Test + public void walSizeLimitMB() { + Options opt = null; + try { + opt = new Options(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + opt.setIsFdCloseOnExec(boolValue); + assertThat(opt.isFdCloseOnExec()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } + } + } + + @Test + public void skipLogErrorOnRecovery() { + Options opt = null; + try { + opt = new Options(); + boolean boolValue = rand.nextBoolean(); + opt.setSkipLogErrorOnRecovery(boolValue); + assertThat(opt.skipLogErrorOnRecovery()).isEqualTo(boolValue); + } finally { + if (opt != null) { + opt.dispose(); + } + } + } + + @Test + public void statsDumpPeriodSec() { + Options opt = null; + try { + opt = new Options(); + 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(); + 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(); + 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(); + opt.setBytesPerSync(longValue); + assertThat(opt.bytesPerSync()).isEqualTo(longValue); + } finally { + if (opt != null) { + opt.dispose(); + } + } + } + @Test public void rocksEnv() { Options options = null; @@ -263,6 +741,8 @@ public class OptionsTest { options.setCompressionType(compressionType); assertThat(options.compressionType()). isEqualTo(compressionType); + assertThat(CompressionType.valueOf("NO_COMPRESSION")). + isEqualTo(CompressionType.NO_COMPRESSION); } } finally { if (options != null) { @@ -281,6 +761,8 @@ public class OptionsTest { options.setCompactionStyle(compactionStyle); assertThat(options.compactionStyle()). isEqualTo(compactionStyle); + assertThat(CompactionStyle.valueOf("FIFO")). + isEqualTo(CompactionStyle.FIFO); } } finally { if (options != null) { diff --git a/java/org/rocksdb/test/PlainTableConfigTest.java b/java/org/rocksdb/test/PlainTableConfigTest.java index abd2cda12..72347e7d4 100644 --- a/java/org/rocksdb/test/PlainTableConfigTest.java +++ b/java/org/rocksdb/test/PlainTableConfigTest.java @@ -8,6 +8,7 @@ package org.rocksdb.test; import org.junit.ClassRule; import org.junit.Test; import org.rocksdb.EncodingType; +import org.rocksdb.Options; import org.rocksdb.PlainTableConfig; import static org.assertj.core.api.Assertions.assertThat; @@ -19,30 +20,79 @@ public class PlainTableConfigTest { new RocksMemoryResource(); @Test - public void plainTableConfig() { + public void keySize() { PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setKeySize(5); assertThat(plainTableConfig.keySize()). isEqualTo(5); + } + + @Test + public void bloomBitsPerKey() { + PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setBloomBitsPerKey(11); assertThat(plainTableConfig.bloomBitsPerKey()). isEqualTo(11); + } + + @Test + public void hashTableRatio() { + PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setHashTableRatio(0.95); assertThat(plainTableConfig.hashTableRatio()). isEqualTo(0.95); + } + + @Test + public void indexSparseness() { + PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setIndexSparseness(18); assertThat(plainTableConfig.indexSparseness()). isEqualTo(18); + } + + @Test + public void hugePageTlbSize() { + PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setHugePageTlbSize(1); assertThat(plainTableConfig.hugePageTlbSize()). isEqualTo(1); + } + + @Test + public void encodingType() { + PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setEncodingType(EncodingType.kPrefix); assertThat(plainTableConfig.encodingType()).isEqualTo( EncodingType.kPrefix); + } + + @Test + public void fullScanMode() { + PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setFullScanMode(true); - assertThat(plainTableConfig.fullScanMode()).isTrue(); + assertThat(plainTableConfig.fullScanMode()).isTrue(); } + + @Test + public void storeIndexInFile() { + PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setStoreIndexInFile(true); assertThat(plainTableConfig.storeIndexInFile()). isTrue(); } + + @Test + public void plainTableConfig() { + Options opt = null; + try { + opt = new Options(); + PlainTableConfig plainTableConfig = new PlainTableConfig(); + opt.setTableFormatConfig(plainTableConfig); + assertThat(opt.tableFactoryName()).isEqualTo("PlainTable"); + } finally { + if (opt != null) { + opt.dispose(); + } + } + } } diff --git a/java/org/rocksdb/test/ReadOptionsTest.java b/java/org/rocksdb/test/ReadOptionsTest.java index 80ea765c5..2cf1584a1 100644 --- a/java/org/rocksdb/test/ReadOptionsTest.java +++ b/java/org/rocksdb/test/ReadOptionsTest.java @@ -77,7 +77,6 @@ public class ReadOptionsTest { ReadOptions opt = null; try { opt = new ReadOptions(); - Random rand = new Random(); opt.setSnapshot(null); assertThat(opt.snapshot()).isNull(); } finally { @@ -88,12 +87,19 @@ public class ReadOptionsTest { } @Test - public void failVerifyChecksumUninitialized(){ + public void failSetVerifyChecksumUninitialized(){ ReadOptions readOptions = setupUninitializedReadOptions( exception); readOptions.setVerifyChecksums(true); } + @Test + public void failVerifyChecksumUninitialized(){ + ReadOptions readOptions = setupUninitializedReadOptions( + exception); + readOptions.verifyChecksums(); + } + @Test public void failSetFillCacheUninitialized(){ ReadOptions readOptions = setupUninitializedReadOptions( diff --git a/java/org/rocksdb/test/RocksDBTest.java b/java/org/rocksdb/test/RocksDBTest.java new file mode 100644 index 000000000..4f51e8b97 --- /dev/null +++ b/java/org/rocksdb/test/RocksDBTest.java @@ -0,0 +1,282 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. +package org.rocksdb.test; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.rocksdb.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RocksDBTest { + + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @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(); + } + } + } + + @Test + public void put() throws RocksDBException { + RocksDB db = null; + WriteOptions opt = null; + try { + db = RocksDB.open(dbFolder.getRoot().getAbsolutePath()); + 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); + 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()); + db.put("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("key1".getBytes(), outValue); + assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); + assertThat(outValue).isEqualTo("value".getBytes()); + // found value which fits partially + 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(); + db.put("key1".getBytes(), "value".getBytes()); + db.put("key2".getBytes(), "12345678".getBytes()); + byte[] outValue = new byte[5]; + // not found value + int getResult = db.get(rOpt, "keyNotFound".getBytes(), + outValue); + assertThat(getResult).isEqualTo(RocksDB.NOT_FOUND); + // found value which fits in outValue + getResult = db.get(rOpt, "key1".getBytes(), outValue); + assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); + assertThat(outValue).isEqualTo("value".getBytes()); + // found value which fits partially + 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 { + RocksDB db = null; + ReadOptions rOpt = null; + try { + db = RocksDB.open(dbFolder.getRoot().getAbsolutePath()); + rOpt = new ReadOptions(); + db.put("key1".getBytes(), "value".getBytes()); + db.put("key2".getBytes(), "12345678".getBytes()); + List lookupKeys = new ArrayList() {{ + add("key1".getBytes()); + add("key2".getBytes()); + }}; + Map results = db.multiGet(lookupKeys); + assertThat(results).isNotNull(); + assertThat(results.values()).isNotNull(); + assertThat(results.values()). + contains("value".getBytes(), "12345678".getBytes()); + // test same method with ReadOptions + results = db.multiGet(rOpt, lookupKeys); + assertThat(results).isNotNull(); + assertThat(results.values()).isNotNull(); + assertThat(results.values()). + contains("value".getBytes(), "12345678".getBytes()); + + // remove existing key + lookupKeys.remove("key2".getBytes()); + // add non existing key + lookupKeys.add("key3".getBytes()); + results = db.multiGet(lookupKeys); + assertThat(results).isNotNull(); + assertThat(results.values()).isNotNull(); + assertThat(results.values()). + contains("value".getBytes()); + // test same call with readOptions + results = db.multiGet(rOpt, lookupKeys); + assertThat(results).isNotNull(); + 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()); + db.put("key1".getBytes(), "value".getBytes()); + assertThat(db.get("key1".getBytes())).isEqualTo( + "value".getBytes()); + // merge key1 with another value portion + db.merge("key1".getBytes(), "value2".getBytes()); + assertThat(db.get("key1".getBytes())).isEqualTo( + "value,value2".getBytes()); + // merge key1 with another value portion + db.merge(wOpt, "key1".getBytes(), "value3".getBytes()); + assertThat(db.get("key1".getBytes())).isEqualTo( + "value,value2,value3".getBytes()); + // merge on non existent key shall insert the value + 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()); + db.put("key1".getBytes(), "value".getBytes()); + db.put("key2".getBytes(), "12345678".getBytes()); + assertThat(db.get("key1".getBytes())).isEqualTo( + "value".getBytes()); + assertThat(db.get("key2".getBytes())).isEqualTo( + "12345678".getBytes()); + db.remove("key1".getBytes()); + db.remove(wOpt, "key2".getBytes()); + assertThat(db.get("key1".getBytes())).isNull(); + assertThat(db.get("key2".getBytes())).isNull(); + } finally { + if (db != null) { + db.close(); + } + } + } +} diff --git a/java/org/rocksdb/test/SizeUnitTest.java b/java/org/rocksdb/test/SizeUnitTest.java new file mode 100644 index 000000000..16f636267 --- /dev/null +++ b/java/org/rocksdb/test/SizeUnitTest.java @@ -0,0 +1,28 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. +package org.rocksdb.test; + +import org.junit.Test; +import org.rocksdb.util.SizeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SizeUnitTest { + + public static final long COMPUTATION_UNIT = 1024L; + + @Test + public void sizeUnit() { + assertThat(SizeUnit.KB).isEqualTo(COMPUTATION_UNIT); + assertThat(SizeUnit.MB).isEqualTo( + SizeUnit.KB * COMPUTATION_UNIT); + assertThat(SizeUnit.GB).isEqualTo( + SizeUnit.MB * COMPUTATION_UNIT); + assertThat(SizeUnit.TB).isEqualTo( + SizeUnit.GB * COMPUTATION_UNIT); + assertThat(SizeUnit.PB).isEqualTo( + SizeUnit.TB * COMPUTATION_UNIT); + } +}