diff --git a/java/Makefile b/java/Makefile index 21066b991..d3bd8d8d4 100644 --- a/java/Makefile +++ b/java/Makefile @@ -32,8 +32,8 @@ NATIVE_JAVA_CLASSES = org.rocksdb.AbstractComparator\ org.rocksdb.StringAppendOperator\ org.rocksdb.WriteBatch\ org.rocksdb.WriteBatch.Handler\ - org.rocksdb.WriteBatchInternal\ - org.rocksdb.WriteBatchTest\ + org.rocksdb.test.WriteBatchInternal\ + org.rocksdb.test.WriteBatchTest\ org.rocksdb.WriteOptions\ ROCKSDB_MAJOR = $(shell egrep "ROCKSDB_MAJOR.[0-9]" ../include/rocksdb/version.h | cut -d ' ' -f 3) @@ -43,18 +43,51 @@ ROCKSDB_PATCH = $(shell egrep "ROCKSDB_PATCH.[0-9]" ../include/rocksdb/version.h NATIVE_INCLUDE = ./include ARCH := $(shell getconf LONG_BIT) ROCKSDB_JAR = rocksdbjni-$(ROCKSDB_MAJOR).$(ROCKSDB_MINOR).$(ROCKSDB_PATCH)-linux$(ARCH).jar - ifeq ($(PLATFORM), OS_MACOSX) ROCKSDB_JAR = rocksdbjni-$(ROCKSDB_MAJOR).$(ROCKSDB_MINOR).$(ROCKSDB_PATCH)-osx.jar endif +JAVA_TESTS = org.rocksdb.test.AbstractComparatorTest\ + org.rocksdb.test.BackupableDBTest\ + org.rocksdb.test.BlockBasedTableConfigTest\ + org.rocksdb.test.ColumnFamilyOptionsTest\ + org.rocksdb.test.ColumnFamilyTest\ + org.rocksdb.test.ComparatorOptionsTest\ + org.rocksdb.test.ComparatorTest\ + org.rocksdb.test.DBOptionsTest\ + org.rocksdb.test.DirectComparatorTest\ + org.rocksdb.test.FilterTest\ + org.rocksdb.test.FlushTest\ + org.rocksdb.test.KeyMayExistTest\ + org.rocksdb.test.MemTableTest\ + org.rocksdb.test.MergeTest\ + org.rocksdb.test.MixedOptionsTest\ + org.rocksdb.test.OptionsTest\ + org.rocksdb.test.PlainTableConfigTest\ + org.rocksdb.test.ReadOnlyTest\ + org.rocksdb.test.ReadOptionsTest\ + org.rocksdb.test.RocksIteratorTest\ + org.rocksdb.test.SnapshotTest\ + org.rocksdb.test.StatisticsCollectorTest\ + org.rocksdb.test.WirteBatchHandlerTest\ + org.rocksdb.test.WriteBatchTest\ + +JAVA_TEST_LIBDIR = ./test-libs/ +JAVA_JUNIT_JAR = $(JAVA_TEST_LIBDIR)junit-4.12-beta-2.jar +JAVA_HAMCR_JAR = $(JAVA_TEST_LIBDIR)hamcrest-core-1.3.jar +JAVA_MOCKITO_JAR = $(JAVA_TEST_LIBDIR)mockito-all-1.9.5.jar +JAVA_CGLIB_JAR = $(JAVA_TEST_LIBDIR)cglib-2.2.2.jar +JAVA_ASSERTJ_JAR = $(JAVA_TEST_LIBDIR)assertj-core-1.7.0.jar +JAVA_TESTCLASSPATH = $(ROCKSDB_JAR):$(JAVA_JUNIT_JAR):$(JAVA_HAMCR_JAR):$(JAVA_MOCKITO_JAR):$(JAVA_CGLIB_JAR):$(JAVA_ASSERTJ_JAR):.:./* + clean: -find . -name "*.class" -exec rm {} \; -find . -name "hs*.log" -exec rm {} \; rm -rf javadoc/* + rm -rf test-libs/ javadocs: - mkdir -p javadoc; javadoc -d javadoc -sourcepath . -subpackages org + mkdir -p javadoc; javadoc -d javadoc -sourcepath . -subpackages org -exclude org.rocksdb.test java: javadocs javac org/rocksdb/util/*.java org/rocksdb/*.java @@ -76,33 +109,17 @@ column_family_sample: java java -ea -Djava.library.path=.:../ -cp ".:./*" -Xcheck:jni RocksDBColumnFamilySample /tmp/rocksdbjni @rm -rf /tmp/rocksdbjni -test: java - @rm -rf /tmp/rocksdbjni_* - javac org/rocksdb/test/*.java - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.WriteBatchTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.BackupableDBTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.BlockBasedTableConfigTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.DBOptionsTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ColumnFamilyTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ColumnFamilyOptionsTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.FilterTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.FlushTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.KeyMayExistTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.MemTableTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.MergeTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.MixedOptionsTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.OptionsTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.PlainTableConfigTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ReadOnlyTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ReadOptionsTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.RocksIteratorTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.SnapshotTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.StatisticsCollectorTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ComparatorOptionsTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ComparatorTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.DirectComparatorTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.WriteBatchHandlerTest - @rm -rf /tmp/rocksdbjni_* +resolve_test_deps: + mkdir -p "$(JAVA_TEST_LIBDIR)" + test -s "$(JAVA_JUNIT_JAR)" || curl -L -o $(JAVA_JUNIT_JAR) http://search.maven.org/remotecontent?filepath=junit/junit/4.12-beta-2/junit-4.12-beta-2.jar + test -s "$(JAVA_HAMCR_JAR)" || curl -L -o $(JAVA_HAMCR_JAR) http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar + test -s "$(JAVA_MOCKITO_JAR)" || curl -L -o "$(JAVA_MOCKITO_JAR)" http://search.maven.org/remotecontent?filepath=org/mockito/mockito-all/1.9.5/mockito-all-1.9.5.jar + test -s "$(JAVA_CGLIB_JAR)" || curl -L -o "$(JAVA_CGLIB_JAR)" http://search.maven.org/remotecontent?filepath=cglib/cglib/2.2.2/cglib-2.2.2.jar + test -s "$(JAVA_ASSERTJ_JAR)" || curl -L -o "$(JAVA_ASSERTJ_JAR)" http://central.maven.org/maven2/org/assertj/assertj-core/1.7.0/assertj-core-1.7.0.jar + +test: java resolve_test_deps + javac -cp $(JAVA_TESTCLASSPATH) org/rocksdb/test/*.java + java -ea -Djava.library.path=.:../ -cp "$(JAVA_TESTCLASSPATH)" org.junit.runner.JUnitCore $(JAVA_TESTS) db_bench: java javac org/rocksdb/benchmark/*.java diff --git a/java/org/rocksdb/ColumnFamilyHandle.java b/java/org/rocksdb/ColumnFamilyHandle.java index 92a4d7cef..ed8417728 100644 --- a/java/org/rocksdb/ColumnFamilyHandle.java +++ b/java/org/rocksdb/ColumnFamilyHandle.java @@ -30,9 +30,11 @@ public class ColumnFamilyHandle extends RocksObject { * before freeing the native handle.

*/ @Override protected void disposeInternal() { - assert(isInitialized()); - if (rocksDB_.isInitialized()) { - disposeInternal(nativeHandle_); + synchronized (rocksDB_) { + assert (isInitialized()); + if (rocksDB_.isInitialized()) { + disposeInternal(nativeHandle_); + } } } diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 5ebbc609e..730c1940d 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -363,8 +363,10 @@ public class RocksDB extends RocksObject { } @Override protected void disposeInternal() { - assert(isInitialized()); - disposeInternal(nativeHandle_); + synchronized (this) { + assert (isInitialized()); + disposeInternal(nativeHandle_); + } } /** @@ -1150,6 +1152,8 @@ public class RocksDB extends RocksObject { throws RocksDBException, IllegalArgumentException { // throws RocksDBException if something goes wrong dropColumnFamily(nativeHandle_, columnFamilyHandle.nativeHandle_); + // After the drop the native handle is not valid anymore + columnFamilyHandle.nativeHandle_ = 0; } /** diff --git a/java/org/rocksdb/RocksIterator.java b/java/org/rocksdb/RocksIterator.java index fee3f459d..b947b2c83 100644 --- a/java/org/rocksdb/RocksIterator.java +++ b/java/org/rocksdb/RocksIterator.java @@ -143,9 +143,11 @@ public class RocksIterator extends RocksObject { * before freeing the native handle.

*/ @Override protected void disposeInternal() { - assert(isInitialized()); - if (rocksDB_.isInitialized()) { - disposeInternal(nativeHandle_); + synchronized (rocksDB_) { + assert (isInitialized()); + if (rocksDB_.isInitialized()) { + disposeInternal(nativeHandle_); + } } } diff --git a/java/org/rocksdb/WriteBatch.java b/java/org/rocksdb/WriteBatch.java index 5bd1119da..3407033ab 100644 --- a/java/org/rocksdb/WriteBatch.java +++ b/java/org/rocksdb/WriteBatch.java @@ -221,13 +221,3 @@ public class WriteBatch extends RocksObject { private native void disposeInternal(long handle); } } - -/** - * Package-private class which provides java api to access - * c++ WriteBatchInternal. - */ -class WriteBatchInternal { - static native void setSequence(WriteBatch batch, long sn); - static native long sequence(WriteBatch batch); - static native void append(WriteBatch b1, WriteBatch b2); -} diff --git a/java/org/rocksdb/WriteBatchTest.java b/java/org/rocksdb/WriteBatchTest.java deleted file mode 100644 index 770cd85b8..000000000 --- a/java/org/rocksdb/WriteBatchTest.java +++ /dev/null @@ -1,124 +0,0 @@ -// 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. -// -// Copyright (c) 2011 The LevelDB Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. See the AUTHORS file for names of contributors. -package org.rocksdb; - -import java.util.*; -import java.io.UnsupportedEncodingException; - -/** - * This class mimics the db/write_batch_test.cc in the c++ rocksdb library. - */ -public class WriteBatchTest { - static { - RocksDB.loadLibrary(); - } - - public static void main(String args[]) { - System.out.println("Testing WriteBatchTest.Empty ==="); - Empty(); - - System.out.println("Testing WriteBatchTest.Multiple ==="); - Multiple(); - - System.out.println("Testing WriteBatchTest.Append ==="); - Append(); - - System.out.println("Testing WriteBatchTest.Blob ==="); - Blob(); - - // The following tests have not yet ported. - // Continue(); - // PutGatherSlices(); - - System.out.println("Passed all WriteBatchTest!"); - } - - static void Empty() { - WriteBatch batch = new WriteBatch(); - assert(batch.count() == 0); - } - - static void Multiple() { - 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")); - WriteBatchInternal.setSequence(batch, 100); - assert(100 == WriteBatchInternal.sequence(batch)); - assert(3 == batch.count()); - assert(("Put(baz, boo)@102" + - "Delete(box)@101" + - "Put(foo, bar)@100") - .equals(new String(getContents(batch), "US-ASCII"))); - } catch (UnsupportedEncodingException e) { - System.err.println(e); - assert(false); - } - } - - static void Append() { - WriteBatch b1 = new WriteBatch(); - WriteBatch b2 = new WriteBatch(); - WriteBatchInternal.setSequence(b1, 200); - WriteBatchInternal.setSequence(b2, 300); - WriteBatchInternal.append(b1, b2); - assert(getContents(b1).length == 0); - assert(b1.count() == 0); - try { - b2.put("a".getBytes("US-ASCII"), "va".getBytes("US-ASCII")); - WriteBatchInternal.append(b1, b2); - assert("Put(a, va)@200".equals(new String(getContents(b1), "US-ASCII"))); - assert(1 == b1.count()); - b2.clear(); - b2.put("b".getBytes("US-ASCII"), "vb".getBytes("US-ASCII")); - WriteBatchInternal.append(b1, b2); - assert(("Put(a, va)@200" + - "Put(b, vb)@201") - .equals(new String(getContents(b1), "US-ASCII"))); - assert(2 == b1.count()); - b2.remove("foo".getBytes("US-ASCII")); - WriteBatchInternal.append(b1, b2); - assert(("Put(a, va)@200" + - "Put(b, vb)@202" + - "Put(b, vb)@201" + - "Delete(foo)@203") - .equals(new String(getContents(b1), "US-ASCII"))); - assert(4 == b1.count()); - } catch (UnsupportedEncodingException e) { - System.err.println(e); - assert(false); - } - } - - static void Blob() { - WriteBatch batch = new WriteBatch(); - try { - 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")); - assert(5 == batch.count()); - assert(("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"))); - } catch (UnsupportedEncodingException e) { - System.err.println(e); - assert(false); - } - } - - static native byte[] getContents(WriteBatch batch); -} diff --git a/java/org/rocksdb/test/BackupableDBTest.java b/java/org/rocksdb/test/BackupableDBTest.java index 2115e9ca9..f0a6708c1 100644 --- a/java/org/rocksdb/test/BackupableDBTest.java +++ b/java/org/rocksdb/test/BackupableDBTest.java @@ -5,28 +5,41 @@ 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.List; public class BackupableDBTest { - static final String db_path = "/tmp/rocksdbjni_backupable_db_test"; - static final String backup_path = "/tmp/rocksdbjni_backupable_db_backup_test"; - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args) { + + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @Rule + public TemporaryFolder backupFolder = new TemporaryFolder(); + + @Test + public void shouldTestBackupableDb() { Options opt = new Options(); opt.setCreateIfMissing(true); - BackupableDBOptions bopt = new BackupableDBOptions(backup_path, false, + BackupableDBOptions bopt = new BackupableDBOptions( + backupFolder.getRoot().getAbsolutePath(), false, true, false, true, 0, 0); BackupableDB bdb = null; List backupInfos; List restoreInfos; try { - bdb = BackupableDB.open(opt, bopt, db_path); + bdb = BackupableDB.open(opt, bopt, + dbFolder.getRoot().getAbsolutePath()); bdb.put("abc".getBytes(), "def".getBytes()); bdb.put("ghi".getBytes(), "jkl".getBytes()); @@ -74,7 +87,9 @@ public class BackupableDBTest { assert(restoreInfos.get(0).numberFiles() == backupInfos.get(0).numberFiles()); - rdb.restoreDBFromLatestBackup(db_path, db_path, + rdb.restoreDBFromLatestBackup( + dbFolder.getRoot().getAbsolutePath(), + dbFolder.getRoot().getAbsolutePath(), ropt); // do nothing because there is only one backup rdb.purgeOldBackups(1); @@ -84,7 +99,8 @@ public class BackupableDBTest { ropt.dispose(); // verify that backed up data contains deleted record - bdb = BackupableDB.open(opt, bopt, db_path); + bdb = BackupableDB.open(opt, bopt, + dbFolder.getRoot().getAbsolutePath()); value = bdb.get("abc".getBytes()); assert(new String(value).equals("def")); @@ -110,7 +126,6 @@ public class BackupableDBTest { assert(backupInfos.size() == 2); assert(backupInfos.get(0).backupId() == 4); assert(backupInfos.get(1).backupId() == 5); - System.out.println("Backup and restore test passed"); } catch (RocksDBException e) { System.err.format("[ERROR]: %s%n", e); e.printStackTrace(); @@ -121,5 +136,6 @@ public class BackupableDBTest { bdb.close(); } } + System.out.println("Passed BackupableDBTest."); } } diff --git a/java/org/rocksdb/test/BlockBasedTableConfigTest.java b/java/org/rocksdb/test/BlockBasedTableConfigTest.java index 3f54d5a78..8c73915ee 100644 --- a/java/org/rocksdb/test/BlockBasedTableConfigTest.java +++ b/java/org/rocksdb/test/BlockBasedTableConfigTest.java @@ -5,13 +5,19 @@ package org.rocksdb.test; +import org.junit.ClassRule; +import org.junit.Test; import org.rocksdb.BlockBasedTableConfig; import org.rocksdb.ChecksumType; import org.rocksdb.IndexType; public class BlockBasedTableConfigTest { + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); - public static void main(String[] args) { + @Test + public void shouldTestBlockBasedTableConfig() { BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); blockBasedTableConfig.setNoBlockCache(true); @@ -42,6 +48,6 @@ public class BlockBasedTableConfigTest { == 4); blockBasedTableConfig.setCacheNumShardBits(5); assert(blockBasedTableConfig.cacheNumShardBits() == 5); - System.out.println("BlockBasedTableConfig test passed"); + System.out.println("Passed BlockBasedTableConfigTest."); } } diff --git a/java/org/rocksdb/test/ColumnFamilyTest.java b/java/org/rocksdb/test/ColumnFamilyTest.java index 350c4446c..e52eac589 100644 --- a/java/org/rocksdb/test/ColumnFamilyTest.java +++ b/java/org/rocksdb/test/ColumnFamilyTest.java @@ -9,16 +9,25 @@ import java.util.HashMap; import java.util.List; import java.util.ArrayList; import java.util.Map; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.rocksdb.*; public class ColumnFamilyTest { - static final String db_path = "/tmp/rocksdbjni_columnfamily_test"; - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args) { + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + @Test + public void shouldTestColumnFamilies() { + String db_path = dbFolder.getRoot().getAbsolutePath(); RocksDB db = null; Options options = new Options(); options.setCreateIfMissing(true); @@ -274,7 +283,6 @@ public class ColumnFamilyTest { assert(false); } - System.out.println("Passed ColumnFamilyTest"); // free cf handles before database close for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { columnFamilyHandle.dispose(); @@ -283,5 +291,6 @@ public class ColumnFamilyTest { db.close(); // be sure to dispose c++ pointers options.dispose(); + System.out.println("Passed ColumnFamilyTest."); } } diff --git a/java/org/rocksdb/test/ComparatorOptionsTest.java b/java/org/rocksdb/test/ComparatorOptionsTest.java index e25209392..21f4fc2a1 100644 --- a/java/org/rocksdb/test/ComparatorOptionsTest.java +++ b/java/org/rocksdb/test/ComparatorOptionsTest.java @@ -5,27 +5,30 @@ package org.rocksdb.test; +import org.junit.ClassRule; +import org.junit.Test; import org.rocksdb.ComparatorOptions; -import org.rocksdb.RocksDB; -import java.util.Random; +import static org.assertj.core.api.Assertions.assertThat; public class ComparatorOptionsTest { - static { - RocksDB.loadLibrary(); - } + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); - public static void main(String[] args) { + @Test + public void shouldTestComparatorOptions() { final ComparatorOptions copt = new ComparatorOptions(); - Random rand = new Random(); + + assertThat(copt).isNotNull(); { // UseAdaptiveMutex test copt.setUseAdaptiveMutex(true); - assert(copt.useAdaptiveMutex() == true); + assertThat(copt.useAdaptiveMutex()).isTrue(); copt.setUseAdaptiveMutex(false); - assert(copt.useAdaptiveMutex() == false); + assertThat(copt.useAdaptiveMutex()).isFalse(); } copt.dispose(); diff --git a/java/org/rocksdb/test/ComparatorTest.java b/java/org/rocksdb/test/ComparatorTest.java index 34d7c78df..d65a0653a 100644 --- a/java/org/rocksdb/test/ComparatorTest.java +++ b/java/org/rocksdb/test/ComparatorTest.java @@ -5,19 +5,26 @@ 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.io.IOException; import java.nio.file.FileSystems; public class ComparatorTest { - private static final String db_path = "/tmp/comparator_db"; - static { - RocksDB.loadLibrary(); - } + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); - public static void main(String[] args) throws IOException { + @Test + public void shouldTestComparator() throws IOException { final AbstractComparatorTest comparatorTest = new AbstractComparatorTest() { @Override @@ -38,7 +45,8 @@ public class ComparatorTest { }; // test the round-tripability of keys written and read with the Comparator - comparatorTest.testRoundtrip(FileSystems.getDefault().getPath(db_path)); + comparatorTest.testRoundtrip(FileSystems.getDefault().getPath( + dbFolder.getRoot().getAbsolutePath())); System.out.println("Passed ComparatorTest"); } diff --git a/java/org/rocksdb/test/DirectComparatorTest.java b/java/org/rocksdb/test/DirectComparatorTest.java index 9df06eb73..562038897 100644 --- a/java/org/rocksdb/test/DirectComparatorTest.java +++ b/java/org/rocksdb/test/DirectComparatorTest.java @@ -5,19 +5,25 @@ 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.io.IOException; import java.nio.file.FileSystems; public class DirectComparatorTest { - private static final String db_path = "/tmp/direct_comparator_db"; + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); - static { - RocksDB.loadLibrary(); - } + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); - public static void main(String[] args) throws IOException { + @Test + public void shouldTestDirectComparator() throws IOException { final AbstractComparatorTest comparatorTest = new AbstractComparatorTest() { @Override @@ -41,7 +47,8 @@ public class DirectComparatorTest { }; // test the round-tripability of keys written and read with the DirectComparator - comparatorTest.testRoundtrip(FileSystems.getDefault().getPath(db_path)); + comparatorTest.testRoundtrip(FileSystems.getDefault().getPath( + dbFolder.getRoot().getAbsolutePath())); System.out.println("Passed DirectComparatorTest"); } diff --git a/java/org/rocksdb/test/FilterTest.java b/java/org/rocksdb/test/FilterTest.java index fc4fabf56..3894167b0 100644 --- a/java/org/rocksdb/test/FilterTest.java +++ b/java/org/rocksdb/test/FilterTest.java @@ -5,13 +5,18 @@ package org.rocksdb.test; +import org.junit.ClassRule; +import org.junit.Test; import org.rocksdb.*; public class FilterTest { - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args) { + + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Test + public void shouldTestFilter() { Options options = new Options(); // test table config BlockBasedTableConfig blockConfig = new BlockBasedTableConfig(); @@ -37,6 +42,6 @@ public class FilterTest { blockConfig = null; System.gc(); System.runFinalization(); - System.out.println("Filter test passed"); + System.out.println("Passed FilterTest."); } } diff --git a/java/org/rocksdb/test/MemTableTest.java b/java/org/rocksdb/test/MemTableTest.java index 0d1e4d54a..0b1244fc2 100644 --- a/java/org/rocksdb/test/MemTableTest.java +++ b/java/org/rocksdb/test/MemTableTest.java @@ -5,13 +5,18 @@ package org.rocksdb.test; +import org.junit.ClassRule; +import org.junit.Test; import org.rocksdb.*; public class MemTableTest { - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args) { + + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Test + public void shouldTestMemTable() throws RocksDBException { Options options = new Options(); // Test HashSkipListMemTableConfig HashSkipListMemTableConfig memTableConfig = @@ -25,11 +30,7 @@ public class MemTableTest { assert(memTableConfig.branchingFactor() == 4); memTableConfig.setBranchingFactor(6); assert(memTableConfig.branchingFactor() == 6); - try { - options.setMemTableConfig(memTableConfig); - } catch (RocksDBException e) { - assert(false); - } + options.setMemTableConfig(memTableConfig); memTableConfig = null; options.dispose(); System.gc(); @@ -41,11 +42,7 @@ public class MemTableTest { assert(skipMemTableConfig.lookahead() == 0); skipMemTableConfig.setLookahead(20); assert(skipMemTableConfig.lookahead() == 20); - try { - options.setMemTableConfig(skipMemTableConfig); - } catch (RocksDBException e) { - assert(false); - } + options.setMemTableConfig(skipMemTableConfig); skipMemTableConfig = null; options.dispose(); System.gc(); @@ -67,21 +64,17 @@ public class MemTableTest { assert(hashLinkedListMemTableConfig. bucketEntriesLoggingThreshold() == 200); assert(hashLinkedListMemTableConfig. - ifLogBucketDistWhenFlush() == true); + ifLogBucketDistWhenFlush()); hashLinkedListMemTableConfig. setIfLogBucketDistWhenFlush(false); - assert(hashLinkedListMemTableConfig. - ifLogBucketDistWhenFlush() == false); + assert(!hashLinkedListMemTableConfig. + ifLogBucketDistWhenFlush()); assert(hashLinkedListMemTableConfig. thresholdUseSkiplist() == 256); hashLinkedListMemTableConfig.setThresholdUseSkiplist(29); assert(hashLinkedListMemTableConfig. thresholdUseSkiplist() == 29); - try { - options.setMemTableConfig(hashLinkedListMemTableConfig); - } catch (RocksDBException e) { - assert(false); - } + options.setMemTableConfig(hashLinkedListMemTableConfig); hashLinkedListMemTableConfig = null; options.dispose(); System.gc(); @@ -93,15 +86,11 @@ public class MemTableTest { assert(vectorMemTableConfig.reservedSize() == 0); vectorMemTableConfig.setReservedSize(123); assert(vectorMemTableConfig.reservedSize() == 123); - try { - options.setMemTableConfig(vectorMemTableConfig); - } catch (RocksDBException e) { - assert(false); - } + options.setMemTableConfig(vectorMemTableConfig); vectorMemTableConfig = null; options.dispose(); System.gc(); System.runFinalization(); - System.out.println("Mem-table test passed"); + System.out.println("Passed MemTableTest."); } } diff --git a/java/org/rocksdb/test/MergeTest.java b/java/org/rocksdb/test/MergeTest.java index d802559e1..31a3fe5cb 100644 --- a/java/org/rocksdb/test/MergeTest.java +++ b/java/org/rocksdb/test/MergeTest.java @@ -7,19 +7,33 @@ package org.rocksdb.test; import java.util.List; import java.util.ArrayList; + +import org.junit.AfterClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.rocksdb.*; public class MergeTest { - static final String db_path_string = "/tmp/rocksdbjni_mergestring_db"; - static final String db_cf_path_string = "/tmp/rocksdbjni_mergecfstring_db"; - static final String db_path_operator = "/tmp/rocksdbjni_mergeoperator_db"; - static { - RocksDB.loadLibrary(); + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @AfterClass + public static void printMergePass(){ + System.out.println("Passed MergeTest."); } - public static void testStringOption() + @Test + public void shouldTestStringOption() throws InterruptedException, RocksDBException { + String db_path_string = + dbFolder.getRoot().getAbsolutePath(); Options opt = new Options(); opt.setCreateIfMissing(true); opt.setMergeOperatorName("stringappend"); @@ -38,23 +52,26 @@ public class MergeTest { assert(strValue.equals("aa,bb")); } - public static void testCFStringOption() + @Test + public void shouldTestCFStringOption() throws InterruptedException, RocksDBException { DBOptions opt = new DBOptions(); + String db_path_string = + dbFolder.getRoot().getAbsolutePath(); opt.setCreateIfMissing(true); opt.setCreateMissingColumnFamilies(true); List cfDescr = - new ArrayList(); + new ArrayList<>(); List columnFamilyHandleList = - new ArrayList(); + new ArrayList<>(); cfDescr.add(new ColumnFamilyDescriptor("default", new ColumnFamilyOptions().setMergeOperatorName( "stringappend"))); cfDescr.add(new ColumnFamilyDescriptor("default", new ColumnFamilyOptions().setMergeOperatorName( "stringappend"))); - RocksDB db = RocksDB.open(opt, db_cf_path_string, + RocksDB db = RocksDB.open(opt, db_path_string, cfDescr, columnFamilyHandleList); // writing aa under key @@ -75,8 +92,11 @@ public class MergeTest { assert(strValue.equals("aa,bb")); } - public static void testOperatorOption() + @Test + public void shouldTestOperatorOption() throws InterruptedException, RocksDBException { + String db_path_string = + dbFolder.getRoot().getAbsolutePath(); Options opt = new Options(); opt.setCreateIfMissing(true); @@ -98,26 +118,29 @@ public class MergeTest { assert(strValue.equals("aa,bb")); } - public static void testCFOperatorOption() + @Test + public void shouldTestCFOperatorOption() throws InterruptedException, RocksDBException { DBOptions opt = new DBOptions(); + String db_path_string = + dbFolder.getRoot().getAbsolutePath(); + opt.setCreateIfMissing(true); opt.setCreateMissingColumnFamilies(true); StringAppendOperator stringAppendOperator = new StringAppendOperator(); List cfDescr = - new ArrayList(); + new ArrayList<>(); List columnFamilyHandleList = - new ArrayList(); + new ArrayList<>(); cfDescr.add(new ColumnFamilyDescriptor("default", new ColumnFamilyOptions().setMergeOperator( stringAppendOperator))); cfDescr.add(new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions().setMergeOperator( stringAppendOperator))); - RocksDB db = RocksDB.open(opt, db_path_operator, + RocksDB db = RocksDB.open(opt, db_path_string, cfDescr, columnFamilyHandleList); - // writing aa under key db.put(columnFamilyHandleList.get(1), "cfkey".getBytes(), "aa".getBytes()); @@ -139,14 +162,18 @@ public class MergeTest { value = db.get(columnFamilyHandle, "cfkey2".getBytes()); String strValueTmpCf = new String(value); + columnFamilyHandle.dispose(); db.close(); opt.dispose(); assert(strValue.equals("aa,bb")); assert(strValueTmpCf.equals("xx,yy")); } - public static void testOperatorGcBehaviour() + @Test + public void shouldTestOperatorGcBehaviour() throws RocksDBException { + String db_path_string = + dbFolder.getRoot().getAbsolutePath(); Options opt = new Options(); opt.setCreateIfMissing(true); StringAppendOperator stringAppendOperator = new StringAppendOperator(); @@ -185,14 +212,4 @@ public class MergeTest { System.gc(); System.runFinalization(); } - - public static void main(String[] args) - throws InterruptedException, RocksDBException { - testStringOption(); - testCFStringOption(); - testOperatorOption(); - testCFOperatorOption(); - testOperatorGcBehaviour(); - System.out.println("Passed MergeTest."); - } } diff --git a/java/org/rocksdb/test/PlainTableConfigTest.java b/java/org/rocksdb/test/PlainTableConfigTest.java index 888f35d81..f4cebb155 100644 --- a/java/org/rocksdb/test/PlainTableConfigTest.java +++ b/java/org/rocksdb/test/PlainTableConfigTest.java @@ -5,12 +5,19 @@ package org.rocksdb.test; +import org.junit.ClassRule; +import org.junit.Test; import org.rocksdb.EncodingType; import org.rocksdb.PlainTableConfig; public class PlainTableConfigTest { - public static void main(String[] args) { + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Test + public void shouldTestPlainTableConfig() { PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setKeySize(5); assert(plainTableConfig.keySize() == 5); @@ -29,6 +36,6 @@ public class PlainTableConfigTest { assert(plainTableConfig.fullScanMode()); plainTableConfig.setStoreIndexInFile(true); assert(plainTableConfig.storeIndexInFile()); - System.out.println("PlainTableConfig test passed"); + System.out.println("Passed PlainTableConfigTest."); } } diff --git a/java/org/rocksdb/test/PlatformRandomHelper.java b/java/org/rocksdb/test/PlatformRandomHelper.java index 7112fc4f1..d43f4a4f0 100644 --- a/java/org/rocksdb/test/PlatformRandomHelper.java +++ b/java/org/rocksdb/test/PlatformRandomHelper.java @@ -18,11 +18,11 @@ public class PlatformRandomHelper { * @return boolean value indicating if operating system is 64 Bit. */ public static boolean isOs64Bit(){ - boolean is64Bit = false; + boolean is64Bit; if (System.getProperty("os.name").contains("Windows")) { is64Bit = (System.getenv("ProgramFiles(x86)") != null); } else { - is64Bit = (System.getProperty("os.arch").indexOf("64") != -1); + is64Bit = (System.getProperty("os.arch").contains("64")); } return is64Bit; } diff --git a/java/org/rocksdb/test/ReadOnlyTest.java b/java/org/rocksdb/test/ReadOnlyTest.java index 21b5eb9ae..057d2d4b8 100644 --- a/java/org/rocksdb/test/ReadOnlyTest.java +++ b/java/org/rocksdb/test/ReadOnlyTest.java @@ -4,31 +4,41 @@ // 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; public class ReadOnlyTest { - static final String DB_PATH = "/tmp/rocksdbjni_readonly_test"; - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args){ + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @Test + public void shouldTestReadOnlyOpen() { RocksDB db = null, db2 = null, db3 = null; List columnFamilyHandleList = - new ArrayList(); + new ArrayList<>(); List db2ColumnFamilyHandleList = - new ArrayList(); + new ArrayList<>(); List db3ColumnFamilyHandleList = - new ArrayList(); + new ArrayList<>(); Options options = new Options(); options.setCreateIfMissing(true); try { - db = RocksDB.open(options, DB_PATH); + db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath()); db.put("key".getBytes(), "value".getBytes()); - db2 = RocksDB.openReadOnly(DB_PATH); + db2 = RocksDB.openReadOnly( + dbFolder.getRoot().getAbsolutePath()); assert("value".equals(new String(db2.get("key".getBytes())))); db.close(); db2.close(); @@ -38,7 +48,7 @@ public class ReadOnlyTest { new ArrayList(); cfNames.add(new ColumnFamilyDescriptor("default")); - db = RocksDB.open(DB_PATH, cfNames, columnFamilyHandleList); + db = RocksDB.open(dbFolder.getRoot().getAbsolutePath(), cfNames, columnFamilyHandleList); columnFamilyHandleList.add(db.createColumnFamily( new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions()))); columnFamilyHandleList.add(db.createColumnFamily( @@ -46,15 +56,16 @@ public class ReadOnlyTest { db.put(columnFamilyHandleList.get(2), "key2".getBytes(), "value2".getBytes()); - db2 = RocksDB.openReadOnly(DB_PATH, cfNames, db2ColumnFamilyHandleList); + db2 = RocksDB.openReadOnly( + dbFolder.getRoot().getAbsolutePath(), cfNames, db2ColumnFamilyHandleList); assert(db2.get("key2".getBytes())==null); assert(db2.get(columnFamilyHandleList.get(0), "key2".getBytes())==null); List cfNewName = - new ArrayList(); + new ArrayList<>(); cfNewName.add(new ColumnFamilyDescriptor("default")); cfNewName.add(new ColumnFamilyDescriptor("new_cf2")); - db3 = RocksDB.openReadOnly(DB_PATH, cfNewName, db3ColumnFamilyHandleList); + db3 = RocksDB.openReadOnly(dbFolder.getRoot().getAbsolutePath(), cfNewName, db3ColumnFamilyHandleList); assert(new String(db3.get(db3ColumnFamilyHandleList.get(1), "key2".getBytes())).equals("value2")); }catch (RocksDBException e){ @@ -125,6 +136,6 @@ public class ReadOnlyTest { columnFamilyHandle.dispose(); } db3.close(); - System.out.println("Passed ReadOnlyTest"); + System.out.println("Passed ReadOnlyTest."); } } diff --git a/java/org/rocksdb/test/ReadOptionsTest.java b/java/org/rocksdb/test/ReadOptionsTest.java index b3b5b2690..27d757a10 100644 --- a/java/org/rocksdb/test/ReadOptionsTest.java +++ b/java/org/rocksdb/test/ReadOptionsTest.java @@ -6,14 +6,20 @@ package org.rocksdb.test; import java.util.Random; + +import org.junit.ClassRule; +import org.junit.Test; import org.rocksdb.RocksDB; import org.rocksdb.ReadOptions; public class ReadOptionsTest { - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args) { + + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Test + public void shouldTestReadOptions() { ReadOptions opt = new ReadOptions(); Random rand = new Random(); { // VerifyChecksums test @@ -33,8 +39,7 @@ public class ReadOptionsTest { opt.setTailing(boolValue); assert(opt.tailing() == boolValue); } - opt.dispose(); - System.out.println("Passed ReadOptionsTest"); + System.out.println("Passed ReadOptionsTest."); } } diff --git a/java/org/rocksdb/test/RocksIteratorTest.java b/java/org/rocksdb/test/RocksIteratorTest.java index 1e2fa8c6d..7de27cad9 100644 --- a/java/org/rocksdb/test/RocksIteratorTest.java +++ b/java/org/rocksdb/test/RocksIteratorTest.java @@ -4,45 +4,46 @@ // of patent rights can be found in the PATENTS file in the same directory. package org.rocksdb.test; -import org.rocksdb.ColumnFamilyHandle; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.rocksdb.Options; import org.rocksdb.RocksDB; import org.rocksdb.RocksDBException; import org.rocksdb.RocksIterator; -import java.util.ArrayList; -import java.util.List; - public class RocksIteratorTest { - static final String DB_PATH = "/tmp/rocksdbjni_iterator_test"; - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args){ + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @Test + public void shouldTestRocksIteratorGc() + throws RocksDBException { RocksDB db; Options options = new Options(); options.setCreateIfMissing(true) .setCreateMissingColumnFamilies(true); - try { - db = RocksDB.open(options, DB_PATH); - db.put("key".getBytes(), "value".getBytes()); - RocksIterator iter = db.newIterator(); - RocksIterator iter2 = db.newIterator(); - RocksIterator iter3 = db.newIterator(); - iter = null; - db.close(); - db = null; - iter2 = null; - System.gc(); - System.runFinalization(); - System.out.println("Passed RocksIterator Test"); - iter3.dispose(); - System.gc(); - System.runFinalization(); - }catch (RocksDBException e){ - e.printStackTrace(); - assert(false); - } + db = RocksDB.open(options, + dbFolder.getRoot().getAbsolutePath()); + db.put("key".getBytes(), "value".getBytes()); + RocksIterator iter = db.newIterator(); + RocksIterator iter2 = db.newIterator(); + RocksIterator iter3 = db.newIterator(); + iter = null; + db.close(); + db = null; + iter2 = null; + System.gc(); + System.runFinalization(); + iter3.dispose(); + System.gc(); + System.runFinalization(); + System.out.println("Passed RocksIteratorTest."); } } diff --git a/java/org/rocksdb/test/RocksMemoryResource.java b/java/org/rocksdb/test/RocksMemoryResource.java new file mode 100644 index 000000000..eabbc822e --- /dev/null +++ b/java/org/rocksdb/test/RocksMemoryResource.java @@ -0,0 +1,21 @@ +package org.rocksdb.test; + +import org.junit.rules.ExternalResource; +import org.rocksdb.RocksDB; + +/** + * Resource to trigger garbage collection after each test + * run. + */ +public class RocksMemoryResource extends ExternalResource { + + static { + RocksDB.loadLibrary(); + } + + @Override + protected void after() { + System.gc(); + System.runFinalization(); + } +} \ No newline at end of file diff --git a/java/org/rocksdb/test/SnapshotTest.java b/java/org/rocksdb/test/SnapshotTest.java index 67d0a83ef..ad3546de3 100644 --- a/java/org/rocksdb/test/SnapshotTest.java +++ b/java/org/rocksdb/test/SnapshotTest.java @@ -4,84 +4,79 @@ // of patent rights can be found in the PATENTS file in the same directory. package org.rocksdb.test; -import java.util.ArrayList; -import java.util.List; - -import org.rocksdb.ColumnFamilyHandle; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.rocksdb.Options; import org.rocksdb.ReadOptions; import org.rocksdb.RocksDB; import org.rocksdb.RocksDBException; import org.rocksdb.Snapshot; -import org.rocksdb.WriteBatch; -import org.rocksdb.WriteOptions; +public class SnapshotTest { -public class SnapshotTest -{ - static final String DB_PATH = "/tmp/rocksdbjni_snapshot_test"; - static { - RocksDB.loadLibrary(); - } + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); - public static void main(String[] args){ - RocksDB db = null; + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @Test + public void shouldTestSnapshots() throws RocksDBException { + RocksDB db; Options options = new Options(); options.setCreateIfMissing(true); - try { - db = RocksDB.open(options, DB_PATH); - db.put("key".getBytes(), "value".getBytes()); - // Get new Snapshot of database - Snapshot snapshot = db.getSnapshot(); - ReadOptions readOptions = new ReadOptions(); - // set snapshot in ReadOptions - readOptions.setSnapshot(snapshot); - // retrieve key value pair - assert(new String(db.get("key".getBytes())) - .equals("value")); - // retrieve key value pair created before - // the snapshot was made - assert(new String(db.get(readOptions, - "key".getBytes())).equals("value")); - // add new key/value pair - db.put("newkey".getBytes(), "newvalue".getBytes()); - // using no snapshot the latest db entries - // will be taken into account - assert(new String(db.get("newkey".getBytes())) - .equals("newvalue")); - // snapshopot was created before newkey - assert(db.get(readOptions, "newkey".getBytes()) - == null); - // 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 - assert(new String(db.get(readOptions, - "key".getBytes())).equals("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 - assert(new String(db.get(readOptions, - "key".getBytes())).equals("value")); - // read for newkey using the snapshot must be - // null - assert(db.get(readOptions, "newkey".getBytes()) - == null); - // setting null to snapshot in ReadOptions leads - // to no Snapshot being used. - readOptions.setSnapshot(null); - assert(new String(db.get(readOptions, - "newkey".getBytes())).equals("newvalue")); - // release Snapshot - db.releaseSnapshot(snapshot); - // Close database - db.close(); - }catch (RocksDBException e){ - e.printStackTrace(); - assert(false); - } - System.out.println("Passed SnapshotTest"); + + db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); + db.put("key".getBytes(), "value".getBytes()); + // Get new Snapshot of database + Snapshot snapshot = db.getSnapshot(); + ReadOptions readOptions = new ReadOptions(); + // set snapshot in ReadOptions + readOptions.setSnapshot(snapshot); + // retrieve key value pair + assert(new String(db.get("key".getBytes())) + .equals("value")); + // retrieve key value pair created before + // the snapshot was made + assert(new String(db.get(readOptions, + "key".getBytes())).equals("value")); + // add new key/value pair + db.put("newkey".getBytes(), "newvalue".getBytes()); + // using no snapshot the latest db entries + // will be taken into account + assert(new String(db.get("newkey".getBytes())) + .equals("newvalue")); + // snapshopot was created before newkey + assert(db.get(readOptions, "newkey".getBytes()) + == null); + // 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 + assert(new String(db.get(readOptions, + "key".getBytes())).equals("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 + assert(new String(db.get(readOptions, + "key".getBytes())).equals("value")); + // read for newkey using the snapshot must be + // null + assert(db.get(readOptions, "newkey".getBytes()) + == null); + // setting null to snapshot in ReadOptions leads + // to no Snapshot being used. + readOptions.setSnapshot(null); + assert(new String(db.get(readOptions, + "newkey".getBytes())).equals("newvalue")); + // release Snapshot + db.releaseSnapshot(snapshot); + // Close database + db.close(); } } diff --git a/java/org/rocksdb/test/StatisticsCollectorTest.java b/java/org/rocksdb/test/StatisticsCollectorTest.java index 5298aa46a..b748c21ce 100644 --- a/java/org/rocksdb/test/StatisticsCollectorTest.java +++ b/java/org/rocksdb/test/StatisticsCollectorTest.java @@ -6,20 +6,32 @@ package org.rocksdb.test; import java.util.Collections; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.rocksdb.*; +import static org.assertj.core.api.Assertions.assertThat; + public class StatisticsCollectorTest { - static final String db_path = "/tmp/rocksdbjni_statistics_collector_test"; - static { - RocksDB.loadLibrary(); - } - public static void main(String[] args) + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @Test + public void shouldTestStatisticsCollector() throws InterruptedException, RocksDBException { Options opt = new Options().createStatistics().setCreateIfMissing(true); Statistics stats = opt.statisticsPtr(); - RocksDB db = RocksDB.open(opt, db_path); + RocksDB db = RocksDB.open(opt, + dbFolder.getRoot().getAbsolutePath()); StatsCallbackMock callback = new StatsCallbackMock(); StatsCollectorInput statsInput = new StatsCollectorInput(stats, callback); @@ -30,8 +42,8 @@ public class StatisticsCollectorTest { Thread.sleep(1000); - assert(callback.tickerCallbackCount > 0); - assert(callback.histCallbackCount > 0); + assertThat(callback.tickerCallbackCount).isGreaterThan(0); + assertThat(callback.histCallbackCount).isGreaterThan(0); statsCollector.shutDown(1000); diff --git a/java/org/rocksdb/test/WriteBatchTest.java b/java/org/rocksdb/test/WriteBatchTest.java new file mode 100644 index 000000000..72e0e464e --- /dev/null +++ b/java/org/rocksdb/test/WriteBatchTest.java @@ -0,0 +1,130 @@ +// 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. +// +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. +package org.rocksdb.test; + +import org.junit.AfterClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.rocksdb.WriteBatch; + +import java.io.UnsupportedEncodingException; + +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(); + */ +public class WriteBatchTest { + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @AfterClass + public static void printMergePass(){ + System.out.println("Passed WriteBatchTest."); + } + + @Test + public void shouldTestEmptyWriteBatch() { + WriteBatch batch = new WriteBatch(); + assertThat(batch.count()).isEqualTo(0); + } + + @Test + public void shouldTestMultipleBatchOperations() + 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")); + WriteBatchInternal.setSequence(batch, 100); + assertThat(WriteBatchInternal.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 shouldTestAppendOperation() + throws UnsupportedEncodingException { + WriteBatch b1 = new WriteBatch(); + WriteBatch b2 = new WriteBatch(); + WriteBatchInternal.setSequence(b1, 200); + WriteBatchInternal.setSequence(b2, 300); + WriteBatchInternal.append(b1, b2); + assertThat(getContents(b1).length).isEqualTo(0); + assertThat(b1.count()).isEqualTo(0); + b2.put("a".getBytes("US-ASCII"), "va".getBytes("US-ASCII")); + WriteBatchInternal.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")); + WriteBatchInternal.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")); + WriteBatchInternal.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 shouldTestBlobOperation() + 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"))); + } + + static native byte[] getContents(WriteBatch batch); +} + +/** + * Package-private class which provides java api to access + * c++ WriteBatchInternal. + */ +class WriteBatchInternal { + static native void setSequence(WriteBatch batch, long sn); + static native long sequence(WriteBatch batch); + static native void append(WriteBatch b1, WriteBatch b2); +} diff --git a/java/rocksjni.pom b/java/rocksjni.pom index 554357031..d8fe09fe9 100644 --- a/java/rocksjni.pom +++ b/java/rocksjni.pom @@ -1,34 +1,164 @@ - - 4.0.0 - RocksDB JNI - http://rocksdb.org/ - org.rocksdb - rocksdbjni - 3.6.0 - RocksDB fat jar that contains .so files for linux32 and linux64, and jnilib files for Mac OSX. - - - Apache License 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html - repo - - - - scm:git:git://github.com/dropwizard/metrics.git - scm:git:git@github.com:dropwizard/metrics.git - http://github.com/dropwizard/metrics/ - HEAD - - - - Facebook - help@facebook.com - America/New_York - - architect - - - + + 4.0.0 + RocksDB JNI + http://rocksdb.org/ + org.rocksdb + rocksdbjni + 3.6.0 + RocksDB fat jar that contains .so files for linux32 and linux64, and jnilib files + for Mac OSX. + + + + Apache License 2.0 + http://www.apache.org/licenses/LICENSE-2.0.html + repo + + + + scm:git:git://github.com/dropwizard/metrics.git + scm:git:git@github.com:dropwizard/metrics.git + http://github.com/dropwizard/metrics/ + HEAD + + + + Facebook + help@facebook.com + America/New_York + + architect + + + + + + UTF-8 + + + + + + ${project.basedir} + + + + ${project.basedir} + + **/* + + + + + ${project.basedir} + + + + ${project.basedir} + + *.so + *.jar + *.jnilib + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.7 + 1.7 + + + + *.java + + org/rocksdb/benchmark/*.java + + org/rocksdb/test/*.java + org/rocksdb/WriteBatchTest.java + + + + + default-testCompile + test-compile + + + + + %regex[org/rocksdb/[^WriteBatchTest].*java] + + *.java + + org/rocksdb/benchmark/*.java + + org/rocksdb/util/*.java + + + + org/rocksdb/test/*.java + + + + testCompile + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.17 + + ${argLine} + + + + org.jacoco + jacoco-maven-plugin + 0.7.1.201405082137 + + + + prepare-agent + + + + report + prepare-package + + report + + + + + + + + + + junit + junit + 4.12-beta-2 + test + + + org.assertj + assertj-core + 1.7.0 + test + + + org.mockito + mockito-all + 1.9.5 + + diff --git a/java/rocksjni/write_batch.cc b/java/rocksjni/write_batch.cc index f1d0a89d6..d243c87a0 100644 --- a/java/rocksjni/write_batch.cc +++ b/java/rocksjni/write_batch.cc @@ -9,8 +9,8 @@ #include "include/org_rocksdb_WriteBatch.h" #include "include/org_rocksdb_WriteBatch_Handler.h" -#include "include/org_rocksdb_WriteBatchInternal.h" -#include "include/org_rocksdb_WriteBatchTest.h" +#include "include/org_rocksdb_test_WriteBatchInternal.h" +#include "include/org_rocksdb_test_WriteBatchTest.h" #include "rocksjni/portal.h" #include "rocksjni/writebatchhandlerjnicallback.h" #include "rocksdb/db.h" @@ -257,11 +257,11 @@ void Java_org_rocksdb_WriteBatch_disposeInternal( } /* - * Class: org_rocksdb_WriteBatchInternal + * Class: org_rocksdb_test_WriteBatchInternal * Method: setSequence * Signature: (Lorg/rocksdb/WriteBatch;J)V */ -void Java_org_rocksdb_WriteBatchInternal_setSequence( +void Java_org_rocksdb_test_WriteBatchInternal_setSequence( JNIEnv* env, jclass jclazz, jobject jobj, jlong jsn) { rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj); assert(wb != nullptr); @@ -271,11 +271,11 @@ void Java_org_rocksdb_WriteBatchInternal_setSequence( } /* - * Class: org_rocksdb_WriteBatchInternal + * Class: org_rocksdb_test_WriteBatchInternal * Method: sequence * Signature: (Lorg/rocksdb/WriteBatch;)J */ -jlong Java_org_rocksdb_WriteBatchInternal_sequence( +jlong Java_org_rocksdb_test_WriteBatchInternal_sequence( JNIEnv* env, jclass jclazz, jobject jobj) { rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj); assert(wb != nullptr); @@ -284,11 +284,11 @@ jlong Java_org_rocksdb_WriteBatchInternal_sequence( } /* - * Class: org_rocksdb_WriteBatchInternal + * Class: org_rocksdb_test_WriteBatchInternal * Method: append * Signature: (Lorg/rocksdb/WriteBatch;Lorg/rocksdb/WriteBatch;)V */ -void Java_org_rocksdb_WriteBatchInternal_append( +void Java_org_rocksdb_test_WriteBatchInternal_append( JNIEnv* env, jclass jclazz, jobject jwb1, jobject jwb2) { rocksdb::WriteBatch* wb1 = rocksdb::WriteBatchJni::getHandle(env, jwb1); assert(wb1 != nullptr); @@ -321,11 +321,11 @@ void Java_org_rocksdb_WriteBatch_00024Handler_disposeInternal( } /* - * Class: org_rocksdb_WriteBatchTest + * Class: org_rocksdb_test_WriteBatchTest * Method: getContents * Signature: (Lorg/rocksdb/WriteBatch;)[B */ -jbyteArray Java_org_rocksdb_WriteBatchTest_getContents( +jbyteArray Java_org_rocksdb_test_WriteBatchTest_getContents( JNIEnv* env, jclass jclazz, jobject jobj) { rocksdb::WriteBatch* b = rocksdb::WriteBatchJni::getHandle(env, jobj); assert(b != nullptr);