From 63d74d2bfac888d6a09aa29b560b88bf8ca692a4 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Sun, 6 Apr 2014 09:33:31 -0700 Subject: [PATCH] Expose JNI layer for these options: block_size, max_write_buffer_number, write_buffer_size, disable_seek_compaction, max_background_compactions. --- java/RocksDBSample.java | 13 +++++ java/org/rocksdb/Options.java | 107 +++++++++++++++++++++++++++++++++- java/rocksjni/options.cc | 105 +++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+), 3 deletions(-) diff --git a/java/RocksDBSample.java b/java/RocksDBSample.java index e6421778c..8a1a3ccbc 100644 --- a/java/RocksDBSample.java +++ b/java/RocksDBSample.java @@ -33,6 +33,19 @@ public class RocksDBSample { } options.setCreateIfMissing(true); + options.setWriteBufferSize(8*1024); + options.setMaxWriteBufferNumber(3); + options.setDisableSeekCompaction(true); + options.setBlockSize(64*1024); + options.setMaxBackgroundCompactions(10); + + assert(options.createIfMissing() == true); + assert(options.writeBufferSize() == 8192); + assert(options.maxWriteBufferNumber() == 3); + assert(options.disableSeekCompaction() == true); + assert(options.blockSize() == 65536); + assert(options.maxBackgroundCompactions() == 10); + try { db = RocksDB.open(options, db_path_not_found); db.put("hello".getBytes(), "world".getBytes()); diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index af759ae8d..94498cead 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -34,7 +34,7 @@ public class Options { * @see RocksDB::Open() */ public void setCreateIfMissing(boolean flag) { - assert(nativeHandle_ != 0); + checkInitialization(); setCreateIfMissing(nativeHandle_, flag); } @@ -45,10 +45,97 @@ public class Options { * @return return true if the create_if_missing flag is set to true. * @see setCreateIfMissing() */ - public boolean craeteIfMissing() { - assert(nativeHandle_ != 0); + public boolean createIfMissing() { + checkInitialization(); return createIfMissing(nativeHandle_); } + + /** + * Amount of data to build up in memory (backed by an unsorted log + * on disk) before converting to a sorted on-disk file. + * + * Larger values increase performance, especially during bulk loads. + * Up to max_write_buffer_number write buffers may be held in memory + * at the same time, so you may wish to adjust this parameter + * to control memory usage. + * + * Also, a larger write buffer will result in a longer recovery time + * the next time the database is opened. + * + * Default: 4MB + * @param size of write buffer + * @see RocksDB::Open() + */ + public void setWriteBufferSize(int writeBufferSize) { + checkInitialization(); + setWriteBufferSize(nativeHandle_, writeBufferSize); + } + + /** + * Return size of write buffer size. + * + * @return size of write buffer. + * @see setWriteBufferSize() + */ + public int writeBufferSize() { + checkInitialization(); + return writeBufferSize(nativeHandle_); + } + + /** + * The maximum number of write buffers that are built up in memory. + * The default is 2, so that when 1 write buffer is being flushed to + * storage, new writes can continue to the other write buffer. + * Default: 2 + * + * @param maximum number of write buffers + * @see RocksDB::Open() + */ + public void setMaxWriteBufferNumber(int maxWriteBufferNumber) { + checkInitialization(); + setMaxWriteBufferNumber(nativeHandle_, maxWriteBufferNumber); + } + + /** + * Returns maximum number of write buffers. + * + * @return maximum number of write buffers. + * @see setMaxWriteBufferNumber() + */ + public int maxWriteBufferNumber() { + checkInitialization(); + return maxWriteBufferNumber(nativeHandle_); + } + + public void setBlockSize(int blockSize) { + checkInitialization(); + setBlockSize(nativeHandle_, blockSize); + } + + public int blockSize() { + checkInitialization(); + return blockSize(nativeHandle_); + } + + public void setDisableSeekCompaction(boolean disableSeekCompaction) { + checkInitialization(); + setDisableSeekCompaction(nativeHandle_, disableSeekCompaction); + } + + public boolean disableSeekCompaction() { + checkInitialization(); + return disableSeekCompaction(nativeHandle_); + } + + public void setMaxBackgroundCompactions(int maxBackgroundCompactions) { + checkInitialization(); + setMaxBackgroundCompactions(nativeHandle_, maxBackgroundCompactions); + } + + public int maxBackgroundCompactions() { + checkInitialization(); + return maxBackgroundCompactions(nativeHandle_); + } /** * Release the memory allocated for the current instance @@ -59,11 +146,25 @@ public class Options { dispose0(); } } + + private void checkInitialization() { + assert(nativeHandle_ != 0); + } private native void newOptions(); private native void dispose0(); private native void setCreateIfMissing(long handle, boolean flag); private native boolean createIfMissing(long handle); + private native void setWriteBufferSize(long handle, int writeBufferSize); + private native int writeBufferSize(long handle); + private native void setMaxWriteBufferNumber(long handle, int maxWriteBufferNumber); + private native int maxWriteBufferNumber(long handle); + private native void setBlockSize(long handle, int blockSize); + private native int blockSize(long handle); + private native void setDisableSeekCompaction(long handle, boolean disableSeekCompaction); + private native boolean disableSeekCompaction(long handle); + private native void setMaxBackgroundCompactions(long handle, int maxBackgroundCompactions); + private native int maxBackgroundCompactions(long handle); long nativeHandle_; } diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 69224f6d0..50ea53e39 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -58,6 +58,111 @@ jboolean Java_org_rocksdb_Options_createIfMissing( return reinterpret_cast(jhandle)->create_if_missing; } +/* + * Class: org_rocksdb_Options + * Method: setWriteBufferSize + * Signature: (JZ)I + */ +void Java_org_rocksdb_Options_setWriteBufferSize( + JNIEnv* env, jobject jobj, jlong jhandle, jint jwrite_buffer_size) { + reinterpret_cast(jhandle)->write_buffer_size = + static_cast(jwrite_buffer_size); +} + + +/* + * Class: org_rocksdb_Options + * Method: writeBufferSize + * Signature: (J)I + */ +jint Java_org_rocksdb_Options_writeBufferSize( + JNIEnv* env, jobject jobj, jlong jhandle) { + return reinterpret_cast(jhandle)->write_buffer_size; +} + +/* + * Class: org_rocksdb_Options + * Method: setMaxWriteBufferNumber + * Signature: (JI)V + */ +void Java_org_rocksdb_Options_setMaxWriteBufferNumber( + JNIEnv* env, jobject jobj, jlong jhandle, jint jmax_write_buffer_number) { + reinterpret_cast(jhandle)->max_write_buffer_number = jmax_write_buffer_number; +} + + +/* + * Class: org_rocksdb_Options + * Method: maxWriteBufferNumber + * Signature: (J)I + */ +jint Java_org_rocksdb_Options_maxWriteBufferNumber( + JNIEnv* env, jobject jobj, jlong jhandle) { + return reinterpret_cast(jhandle)->max_write_buffer_number; +} + +/* + * Class: org_rocksdb_Options + * Method: setBlockSize + * Signature: (JI)V + */ +void Java_org_rocksdb_Options_setBlockSize( + JNIEnv* env, jobject jobj, jlong jhandle, jint jblock_size) { + reinterpret_cast(jhandle)->block_size = + static_cast(jblock_size); +} + +/* + * Class: org_rocksdb_Options + * Method: blockSize + * Signature: (J)I + */ +jint Java_org_rocksdb_Options_blockSize( + JNIEnv* env, jobject jobj, jlong jhandle) { + return reinterpret_cast(jhandle)->block_size; +} + +/* + * Class: org_rocksdb_Options + * Method: setDisableSeekCompaction + * Signature: (JZ)V + */ +void Java_org_rocksdb_Options_setDisableSeekCompaction( + JNIEnv* env, jobject jobj, jlong jhandle, jboolean jdisable_seek_compaction) { + reinterpret_cast(jhandle)->disable_seek_compaction = jdisable_seek_compaction; +} + +/* + * Class: org_rocksdb_Options + * Method: disableSeekCompaction + * Signature: (J)Z + */ +jboolean Java_org_rocksdb_Options_disableSeekCompaction( + JNIEnv* env, jobject jobj, jlong jhandle) { + return reinterpret_cast(jhandle)->disable_seek_compaction; +} + +/* + * Class: org_rocksdb_Options + * Method: setMaxBackgroundCompactions + * Signature: (JI)V + */ +void Java_org_rocksdb_Options_setMaxBackgroundCompactions( + JNIEnv* env, jobject jobj, jlong jhandle, jint jmax_background_compactions) { + reinterpret_cast(jhandle)->max_background_compactions = jmax_background_compactions; +} + +/* + * Class: org_rocksdb_Options + * Method: maxBackgroundCompactions + * Signature: (J)I + */ +jint Java_org_rocksdb_Options_maxBackgroundCompactions( + JNIEnv* env, jobject jobj, jlong jhandle) { + return reinterpret_cast(jhandle)->max_background_compactions; +} + + ////////////////////////////////////////////////////////////////////////////// // WriteOptions