diff --git a/java/Makefile b/java/Makefile index 0f971433f..3899fd067 100644 --- a/java/Makefile +++ b/java/Makefile @@ -1,4 +1,4 @@ -NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions +NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions org.rocksdb.Statistics NATIVE_INCLUDE = ./include ROCKSDB_JAR = rocksdbjni.jar diff --git a/java/RocksDBSample.java b/java/RocksDBSample.java index 293e84fd4..242e2a041 100644 --- a/java/RocksDBSample.java +++ b/java/RocksDBSample.java @@ -34,11 +34,13 @@ public class RocksDBSample { } options.setCreateIfMissing(true) + .createStatistics() .setWriteBufferSize(8 * SizeUnit.KB) .setMaxWriteBufferNumber(3) .setDisableSeekCompaction(true) .setBlockSize(64 * SizeUnit.KB) .setMaxBackgroundCompactions(10); + Statistics stats = new Statistics(options.statisticsPtr()); assert(options.createIfMissing() == true); assert(options.writeBufferSize() == 8 * SizeUnit.KB); @@ -120,13 +122,25 @@ public class RocksDBSample { assert(new String(testValue).equals( new String(enoughArray, 0, len))); writeOpts.dispose(); + + try { + for(StatisticsType statsType : StatisticsType.values()) { + stats.getTickerCount(statsType); + } + System.out.println("getTickerCount() passed."); + } + catch(Exception e) { + System.out.println("Failed in call to getTickerCount()"); + assert(false); //Should never reach here. + } + } catch (RocksDBException e) { System.err.println(e); } if (db != null) { db.close(); } - // be sure to dispose c++ pointer + // be sure to dispose c++ pointers options.dispose(); } } diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index 13c05bc3f..9c76d4ac4 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -200,6 +200,35 @@ public class Options { return maxBackgroundCompactions(nativeHandle_); } + /* + * Creates statistics object which collects metrics about database operations. + Statistics objects should not be shared between DB instances as + it does not use any locks to prevent concurrent updates. + * + * @return the instance of the current Options. + * @see RocksDB.open() + */ + public Options createStatistics() { + assert(isInitialized()); + createStatistics(nativeHandle_); + return this; + } + + /* + * Pointer to statistics object. Should only be called after statistics has + * been created by createStatistics() call. + * + * @see createStatistics() + */ + public long statisticsPtr() { + assert(isInitialized()); + + long statsPtr = statisticsPtr(nativeHandle_); + assert(statsPtr != 0); + + return statsPtr; + } + /** * Set the amount of cache in bytes that will be used by RocksDB. * If cacheSize is non-positive, then cache will not be used. @@ -223,7 +252,7 @@ public class Options { * in the c++ side. */ public synchronized void dispose() { - if (nativeHandle_ != 0) { + if (isInitialized()) { dispose0(); } } @@ -249,6 +278,8 @@ public class Options { private native void setMaxBackgroundCompactions( long handle, int maxBackgroundCompactions); private native int maxBackgroundCompactions(long handle); + private native void createStatistics(long optHandle); + private native long statisticsPtr(long optHandle); long nativeHandle_; long cacheSize_; diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 896c37012..f9452b72e 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -5,7 +5,6 @@ package org.rocksdb; -import java.lang.*; import java.util.*; import java.io.Closeable; import java.io.IOException; diff --git a/java/org/rocksdb/RocksDBException.java b/java/org/rocksdb/RocksDBException.java index e426e03ee..acc93669e 100644 --- a/java/org/rocksdb/RocksDBException.java +++ b/java/org/rocksdb/RocksDBException.java @@ -5,7 +5,6 @@ package org.rocksdb; -import java.lang.*; import java.util.*; /** diff --git a/java/org/rocksdb/WriteBatch.java b/java/org/rocksdb/WriteBatch.java index fdd9ef28c..451d82409 100644 --- a/java/org/rocksdb/WriteBatch.java +++ b/java/org/rocksdb/WriteBatch.java @@ -5,7 +5,6 @@ package org.rocksdb; -import java.lang.*; import java.util.*; /** @@ -118,4 +117,3 @@ class WriteBatchInternal { 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 index 283caca65..e54f94675 100644 --- a/java/org/rocksdb/WriteBatchTest.java +++ b/java/org/rocksdb/WriteBatchTest.java @@ -9,7 +9,6 @@ package org.rocksdb; import java.util.*; -import java.lang.*; import java.io.UnsupportedEncodingException; /** diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index e9d3e48ed..4f39fd51b 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -9,12 +9,14 @@ #include #include #include +#include #include "include/org_rocksdb_Options.h" #include "include/org_rocksdb_WriteOptions.h" #include "rocksjni/portal.h" #include "rocksdb/db.h" #include "rocksdb/options.h" +#include "rocksdb/statistics.h" /* * Class: org_rocksdb_Options @@ -35,7 +37,7 @@ void Java_org_rocksdb_Options_dispose0(JNIEnv* env, jobject jobj) { rocksdb::Options* op = rocksdb::OptionsJni::getHandle(env, jobj); delete op; - rocksdb::OptionsJni::setHandle(env, jobj, op); + rocksdb::OptionsJni::setHandle(env, jobj, nullptr); } /* @@ -91,6 +93,27 @@ void Java_org_rocksdb_Options_setMaxWriteBufferNumber( jmax_write_buffer_number; } +/* + * Class: org_rocksdb_Options + * Method: createStatistics + * Signature: (J)V + */ +void Java_org_rocksdb_Options_createStatistics( + JNIEnv* env, jobject jobj, jlong jOptHandle) { + reinterpret_cast(jOptHandle)->statistics = + rocksdb::CreateDBStatistics(); +} + +/* + * Class: org_rocksdb_Options + * Method: statisticsPtr + * Signature: (J)J + */ +jlong Java_org_rocksdb_Options_statisticsPtr( + JNIEnv* env, jobject jobj, jlong jOptHandle) { + auto st = reinterpret_cast(jOptHandle)->statistics.get(); + return reinterpret_cast(st); +} /* * Class: org_rocksdb_Options @@ -235,5 +258,3 @@ jboolean Java_org_rocksdb_WriteOptions_disableWAL( JNIEnv* env, jobject jwrite_options, jlong jhandle) { return reinterpret_cast(jhandle)->disableWAL; } - - diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index 5b0524aec..ab354bd38 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -12,6 +12,7 @@ #include #include "rocksdb/db.h" +#include "rocksdb/statistics.h" namespace rocksdb { diff --git a/java/rocksjni/write_batch.cc b/java/rocksjni/write_batch.cc index a3564d536..9d8ba1074 100644 --- a/java/rocksjni/write_batch.cc +++ b/java/rocksjni/write_batch.cc @@ -262,4 +262,3 @@ jbyteArray Java_org_rocksdb_WriteBatchTest_getContents( return jstate; } -