From dc4b27ac486dbc3e7ef7c02881d23495fb6307b8 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Mon, 21 Apr 2014 20:25:30 -0700 Subject: [PATCH 1/8] Add bloom filters --- java/RocksDBSample.java | 3 ++- java/org/rocksdb/Options.java | 24 ++++++++++++++++++++++++ java/rocksjni/options.cc | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/java/RocksDBSample.java b/java/RocksDBSample.java index 940b4bd408..57e9aa6e74 100644 --- a/java/RocksDBSample.java +++ b/java/RocksDBSample.java @@ -38,7 +38,8 @@ public class RocksDBSample { .setMaxWriteBufferNumber(3) .setDisableSeekCompaction(true) .setBlockSize(64 * SizeUnit.KB) - .setMaxBackgroundCompactions(10); + .setMaxBackgroundCompactions(10) + .createBloomFilter(10); Statistics stats = options.statisticsPtr(); assert(options.createIfMissing() == true); diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index 287ef29fa5..95081eb750 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -143,6 +143,28 @@ public class Options { assert(isInitialized()); return blockSize(nativeHandle_); } + + /** + * Filters are stored in rocksdb and are consulted automatically + * by rocksdb to decide whether or not to read some + * information from disk. In many cases, a filter can cut down the + * number of disk seeks form a handful to a single disk seek per + * DB::Get() call. + * + * This function a new filter policy that uses a bloom filter + * with approximately the specified number of bits per key. + * A good value for bitsPerKey is 10, which yields a filter + * with ~ 1% false positive rate. + * + * @param Bits per key for bloom filter. + * @return the instance of the current Options. + * @see RocksDB.open() + */ + public Options createBloomFilter(int bitsPerKey) { + assert(isInitialized()); + createBloomFilter0(nativeHandle_, bitsPerKey); + return this; + } /* * Disable compaction triggered by seek. @@ -1237,6 +1259,8 @@ public class Options { private native void useFixedLengthPrefixExtractor( long handle, int prefixLength); + + private native void createBloomFilter0(long handle, int bitsPerKey); long nativeHandle_; long cacheSize_; diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 99b6b605e8..f11f6961bd 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -21,6 +21,7 @@ #include "rocksdb/memtablerep.h" #include "rocksdb/table.h" #include "rocksdb/slice_transform.h" +#include "rocksdb/filter_policy.h" /* * Class: org_rocksdb_Options @@ -119,6 +120,23 @@ jlong Java_org_rocksdb_Options_statisticsPtr( return reinterpret_cast(st); } +/* + * Class: org_rocksdb_Options + * Method: createBloomFilter0 + * Signature: (JI)V + */ +void Java_org_rocksdb_Options_createBloomFilter0( + JNIEnv* env, jobject jobj, jlong jhandle, jint jbits_per_key) { + rocksdb::Options* opt = reinterpret_cast(jhandle); + + // Delete previously allocated pointer + if(opt->filter_policy) { + delete opt->filter_policy; + } + + opt->filter_policy = rocksdb::NewBloomFilterPolicy(jbits_per_key); +} + /* * Class: org_rocksdb_Options * Method: maxWriteBufferNumber From cea2be20b66344514fc7cd42b1d06fe269985994 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Mon, 21 Apr 2014 20:27:09 -0700 Subject: [PATCH 2/8] Fix formatting --- java/org/rocksdb/Options.java | 14 +++++++------- java/org/rocksdb/ReadOptions.java | 1 - java/rocksjni/options.cc | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index 95081eb750..c05e0d5e4b 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -143,19 +143,19 @@ public class Options { assert(isInitialized()); return blockSize(nativeHandle_); } - + /** * Filters are stored in rocksdb and are consulted automatically * by rocksdb to decide whether or not to read some * information from disk. In many cases, a filter can cut down the * number of disk seeks form a handful to a single disk seek per * DB::Get() call. - * - * This function a new filter policy that uses a bloom filter - * with approximately the specified number of bits per key. - * A good value for bitsPerKey is 10, which yields a filter + * + * This function a new filter policy that uses a bloom filter + * with approximately the specified number of bits per key. + * A good value for bitsPerKey is 10, which yields a filter * with ~ 1% false positive rate. - * + * * @param Bits per key for bloom filter. * @return the instance of the current Options. * @see RocksDB.open() @@ -1259,7 +1259,7 @@ public class Options { private native void useFixedLengthPrefixExtractor( long handle, int prefixLength); - + private native void createBloomFilter0(long handle, int bitsPerKey); long nativeHandle_; diff --git a/java/org/rocksdb/ReadOptions.java b/java/org/rocksdb/ReadOptions.java index c9ab547cc5..aead12ec86 100644 --- a/java/org/rocksdb/ReadOptions.java +++ b/java/org/rocksdb/ReadOptions.java @@ -162,4 +162,3 @@ public class ReadOptions { return nativeHandle_ != 0; } } - diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index f11f6961bd..29108e44ce 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -128,12 +128,12 @@ jlong Java_org_rocksdb_Options_statisticsPtr( void Java_org_rocksdb_Options_createBloomFilter0( JNIEnv* env, jobject jobj, jlong jhandle, jint jbits_per_key) { rocksdb::Options* opt = reinterpret_cast(jhandle); - + // Delete previously allocated pointer if(opt->filter_policy) { delete opt->filter_policy; } - + opt->filter_policy = rocksdb::NewBloomFilterPolicy(jbits_per_key); } From 5e797cf0dd399bdb9c1588198482f3b3dd97b84d Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Mon, 21 Apr 2014 23:56:19 -0700 Subject: [PATCH 3/8] Change filter implementation --- java/Makefile | 2 +- java/RocksDBSample.java | 4 ++- java/org/rocksdb/Filter.java | 50 +++++++++++++++++++++++++++++++++++ java/org/rocksdb/Options.java | 20 ++++---------- java/rocksjni/filter.cc | 39 +++++++++++++++++++++++++++ java/rocksjni/options.cc | 18 +++++-------- java/rocksjni/portal.h | 34 ++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 29 deletions(-) create mode 100644 java/org/rocksdb/Filter.java create mode 100644 java/rocksjni/filter.cc diff --git a/java/Makefile b/java/Makefile index 5311af14f5..dd8b4b6fe8 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 org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.Iterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.ReadOptions +NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.Iterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.ReadOptions org.rocksdb.Filter NATIVE_INCLUDE = ./include ROCKSDB_JAR = rocksdbjni.jar diff --git a/java/RocksDBSample.java b/java/RocksDBSample.java index 57e9aa6e74..4bedc592c3 100644 --- a/java/RocksDBSample.java +++ b/java/RocksDBSample.java @@ -32,6 +32,7 @@ public class RocksDBSample { assert(db == null); } + Filter filter = new Filter(10); options.setCreateIfMissing(true) .createStatistics() .setWriteBufferSize(8 * SizeUnit.KB) @@ -39,7 +40,7 @@ public class RocksDBSample { .setDisableSeekCompaction(true) .setBlockSize(64 * SizeUnit.KB) .setMaxBackgroundCompactions(10) - .createBloomFilter(10); + .setFilter(filter); Statistics stats = options.statisticsPtr(); assert(options.createIfMissing() == true); @@ -225,5 +226,6 @@ public class RocksDBSample { // be sure to dispose c++ pointers options.dispose(); readOptions.dispose(); + filter.dispose(); } } diff --git a/java/org/rocksdb/Filter.java b/java/org/rocksdb/Filter.java new file mode 100644 index 0000000000..cf6544870c --- /dev/null +++ b/java/org/rocksdb/Filter.java @@ -0,0 +1,50 @@ +// 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; + +/** + * Filters are stored in rocksdb and are consulted automatically + * by rocksdb to decide whether or not to read some + * information from disk. In many cases, a filter can cut down the + * number of disk seeks form a handful to a single disk seek per + * DB::Get() call. + * + * This function a new filter policy that uses a bloom filter + * with approximately the specified number of bits per key. + * A good value for bitsPerKey is 10, which yields a filter + * with ~ 1% false positive rate. + */ +public class Filter { + private long nativeHandle_; + + public Filter(int bitsPerKey) { + newFilter(bitsPerKey); + } + + public long getNativeHandle() { + return nativeHandle_; + } + + /** + * Deletes underlying C++ filter pointer. + */ + public synchronized void dispose() { + if(nativeHandle_ != 0) { + dispose0(nativeHandle_); + } + } + + @Override protected void finalize() { + dispose(); + } + + private boolean isInitialized() { + return (nativeHandle_ != 0); + } + + private native void newFilter(int bitsPerKey); + private native void dispose0(long handle); +} \ No newline at end of file diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index c05e0d5e4b..aee5f18519 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -145,24 +145,14 @@ public class Options { } /** - * Filters are stored in rocksdb and are consulted automatically - * by rocksdb to decide whether or not to read some - * information from disk. In many cases, a filter can cut down the - * number of disk seeks form a handful to a single disk seek per - * DB::Get() call. - * - * This function a new filter policy that uses a bloom filter - * with approximately the specified number of bits per key. - * A good value for bitsPerKey is 10, which yields a filter - * with ~ 1% false positive rate. - * - * @param Bits per key for bloom filter. + * Use the specified filter policy to reduce disk reads. + * @param Filter policy java instance. * @return the instance of the current Options. * @see RocksDB.open() */ - public Options createBloomFilter(int bitsPerKey) { + public Options setFilter(Filter filter) { assert(isInitialized()); - createBloomFilter0(nativeHandle_, bitsPerKey); + setFilter0(nativeHandle_, filter.getNativeHandle()); return this; } @@ -1260,7 +1250,7 @@ public class Options { private native void useFixedLengthPrefixExtractor( long handle, int prefixLength); - private native void createBloomFilter0(long handle, int bitsPerKey); + private native void setFilter0(long optHandle, long fpHandle); long nativeHandle_; long cacheSize_; diff --git a/java/rocksjni/filter.cc b/java/rocksjni/filter.cc new file mode 100644 index 0000000000..c363a851a4 --- /dev/null +++ b/java/rocksjni/filter.cc @@ -0,0 +1,39 @@ +// 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. +// +// This file implements the "bridge" between Java and C++ for rocksdb::Filter. + +#include +#include +#include +#include + +#include "include/org_rocksdb_Filter.h" +#include "rocksjni/portal.h" +#include "rocksdb/filter_policy.h" + +/* + * Class: org_rocksdb_Filter + * Method: newFilter + * Signature: (I)V + */ +void Java_org_rocksdb_Filter_newFilter( + JNIEnv* env, jobject jobj, jint bits_per_key) { + const rocksdb::FilterPolicy* fp = rocksdb::NewBloomFilterPolicy(bits_per_key); + rocksdb::FilterJni::setHandle(env, jobj, fp); +} + +/* + * Class: org_rocksdb_Filter + * Method: dispose0 + * Signature: (J)V + */ +void Java_org_rocksdb_Filter_dispose0( + JNIEnv* env, jobject jobj, jlong handle) { + auto fp = reinterpret_cast(handle); + delete fp; + + rocksdb::FilterJni::setHandle(env, jobj, nullptr); +} \ No newline at end of file diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 29108e44ce..dbb10e0c8a 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -122,19 +122,13 @@ jlong Java_org_rocksdb_Options_statisticsPtr( /* * Class: org_rocksdb_Options - * Method: createBloomFilter0 - * Signature: (JI)V + * Method: setFilter0 + * Signature: (JJ)V */ -void Java_org_rocksdb_Options_createBloomFilter0( - JNIEnv* env, jobject jobj, jlong jhandle, jint jbits_per_key) { - rocksdb::Options* opt = reinterpret_cast(jhandle); - - // Delete previously allocated pointer - if(opt->filter_policy) { - delete opt->filter_policy; - } - - opt->filter_policy = rocksdb::NewBloomFilterPolicy(jbits_per_key); +void Java_org_rocksdb_Options_setFilter0( + JNIEnv* env, jobject jobj, jlong jopt_handle, jlong jfp_handle) { + reinterpret_cast(jopt_handle)->filter_policy = + reinterpret_cast(jfp_handle); } /* diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index 0b485e2e36..61edb8bc43 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -12,6 +12,7 @@ #include #include "rocksdb/db.h" +#include "rocksdb/filter_policy.h" #include "utilities/backupable_db.h" namespace rocksdb { @@ -281,5 +282,38 @@ class IteratorJni { reinterpret_cast(op)); } }; + +class FilterJni { + public: + // Get the java class id of org.rocksdb.Filter. + static jclass getJClass(JNIEnv* env) { + static jclass jclazz = env->FindClass("org/rocksdb/Filter"); + assert(jclazz != nullptr); + return jclazz; + } + + // Get the field id of the member variable of org.rocksdb.Filter + // that stores the pointer to rocksdb::Iterator. + static jfieldID getHandleFieldID(JNIEnv* env) { + static jfieldID fid = env->GetFieldID( + getJClass(env), "nativeHandle_", "J"); + assert(fid != nullptr); + return fid; + } + + // Get the pointer to rocksdb::Filter. + static rocksdb::FilterPolicy* getHandle(JNIEnv* env, jobject jobj) { + return reinterpret_cast( + env->GetLongField(jobj, getHandleFieldID(env))); + } + + // Pass the rocksdb::Filter pointer to the java side. + static void setHandle( + JNIEnv* env, jobject jobj, const rocksdb::FilterPolicy* op) { + env->SetLongField( + jobj, getHandleFieldID(env), + reinterpret_cast(op)); + } +}; } // namespace rocksdb #endif // JAVA_ROCKSJNI_PORTAL_H_ From 677b0d6d3fafac727beef58666c4ceee907ad322 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Tue, 22 Apr 2014 00:04:56 -0700 Subject: [PATCH 4/8] Refactor filter impl --- java/org/rocksdb/Filter.java | 16 ++++++---------- java/org/rocksdb/Options.java | 4 ++-- java/rocksjni/filter.cc | 2 +- java/rocksjni/options.cc | 4 ++-- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/java/org/rocksdb/Filter.java b/java/org/rocksdb/Filter.java index cf6544870c..a0e16eb4d7 100644 --- a/java/org/rocksdb/Filter.java +++ b/java/org/rocksdb/Filter.java @@ -12,22 +12,18 @@ package org.rocksdb; * number of disk seeks form a handful to a single disk seek per * DB::Get() call. * - * This function a new filter policy that uses a bloom filter + * This class creates a new filter policy that uses a bloom filter * with approximately the specified number of bits per key. * A good value for bitsPerKey is 10, which yields a filter - * with ~ 1% false positive rate. + * with ~ 1% false positive rate. */ public class Filter { private long nativeHandle_; - + public Filter(int bitsPerKey) { newFilter(bitsPerKey); } - - public long getNativeHandle() { - return nativeHandle_; - } - + /** * Deletes underlying C++ filter pointer. */ @@ -44,7 +40,7 @@ public class Filter { private boolean isInitialized() { return (nativeHandle_ != 0); } - + private native void newFilter(int bitsPerKey); private native void dispose0(long handle); -} \ No newline at end of file +} diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index aee5f18519..ff289b7768 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -152,7 +152,7 @@ public class Options { */ public Options setFilter(Filter filter) { assert(isInitialized()); - setFilter0(nativeHandle_, filter.getNativeHandle()); + setFilter0(nativeHandle_, filter); return this; } @@ -1250,7 +1250,7 @@ public class Options { private native void useFixedLengthPrefixExtractor( long handle, int prefixLength); - private native void setFilter0(long optHandle, long fpHandle); + private native void setFilter0(long optHandle, Filter fp); long nativeHandle_; long cacheSize_; diff --git a/java/rocksjni/filter.cc b/java/rocksjni/filter.cc index c363a851a4..caee92c824 100644 --- a/java/rocksjni/filter.cc +++ b/java/rocksjni/filter.cc @@ -36,4 +36,4 @@ void Java_org_rocksdb_Filter_dispose0( delete fp; rocksdb::FilterJni::setHandle(env, jobj, nullptr); -} \ No newline at end of file +} diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index dbb10e0c8a..d9aef3d741 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -126,9 +126,9 @@ jlong Java_org_rocksdb_Options_statisticsPtr( * Signature: (JJ)V */ void Java_org_rocksdb_Options_setFilter0( - JNIEnv* env, jobject jobj, jlong jopt_handle, jlong jfp_handle) { + JNIEnv* env, jobject jobj, jlong jopt_handle, jobject jfp) { reinterpret_cast(jopt_handle)->filter_policy = - reinterpret_cast(jfp_handle); + rocksdb::FilterJni::getHandle(env, jfp); } /* From 89cb481aa16ccfbed8f64018d1a98710c3907141 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Tue, 22 Apr 2014 00:09:40 -0700 Subject: [PATCH 5/8] Fix doc --- java/rocksjni/filter.cc | 3 ++- java/rocksjni/portal.h | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/java/rocksjni/filter.cc b/java/rocksjni/filter.cc index caee92c824..dd65cf42d6 100644 --- a/java/rocksjni/filter.cc +++ b/java/rocksjni/filter.cc @@ -3,7 +3,8 @@ // 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. // -// This file implements the "bridge" between Java and C++ for rocksdb::Filter. +// This file implements the "bridge" between Java and C++ for +// rocksdb::FilterPolicy. #include #include diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index 61edb8bc43..4c44443297 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -285,7 +285,7 @@ class IteratorJni { class FilterJni { public: - // Get the java class id of org.rocksdb.Filter. + // Get the java class id of org.rocksdb.FilterPolicy. static jclass getJClass(JNIEnv* env) { static jclass jclazz = env->FindClass("org/rocksdb/Filter"); assert(jclazz != nullptr); @@ -293,7 +293,7 @@ class FilterJni { } // Get the field id of the member variable of org.rocksdb.Filter - // that stores the pointer to rocksdb::Iterator. + // that stores the pointer to rocksdb::FilterPolicy. static jfieldID getHandleFieldID(JNIEnv* env) { static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); @@ -301,13 +301,13 @@ class FilterJni { return fid; } - // Get the pointer to rocksdb::Filter. + // Get the pointer to rocksdb::FilterPolicy. static rocksdb::FilterPolicy* getHandle(JNIEnv* env, jobject jobj) { return reinterpret_cast( env->GetLongField(jobj, getHandleFieldID(env))); } - // Pass the rocksdb::Filter pointer to the java side. + // Pass the rocksdb::FilterPolicy pointer to the java side. static void setHandle( JNIEnv* env, jobject jobj, const rocksdb::FilterPolicy* op) { env->SetLongField( From 2214fd8a1506ab0444bea5c4ed931a9b9dcce5e8 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Tue, 22 Apr 2014 08:58:43 -0700 Subject: [PATCH 6/8] Refactor filter impl --- java/Makefile | 2 +- java/RocksDBSample.java | 2 +- java/org/rocksdb/BloomFilter.java | 23 +++++++++++++++++++++++ java/org/rocksdb/Filter.java | 13 +++++-------- java/rocksjni/filter.cc | 7 ++++--- 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 java/org/rocksdb/BloomFilter.java diff --git a/java/Makefile b/java/Makefile index dd8b4b6fe8..1a199e1dfc 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 org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.Iterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.ReadOptions org.rocksdb.Filter +NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.Iterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.ReadOptions org.rocksdb.Filter org.rocksdb.BloomFilter NATIVE_INCLUDE = ./include ROCKSDB_JAR = rocksdbjni.jar diff --git a/java/RocksDBSample.java b/java/RocksDBSample.java index 4bedc592c3..7e59747229 100644 --- a/java/RocksDBSample.java +++ b/java/RocksDBSample.java @@ -32,7 +32,7 @@ public class RocksDBSample { assert(db == null); } - Filter filter = new Filter(10); + Filter filter = new BloomFilter(10); options.setCreateIfMissing(true) .createStatistics() .setWriteBufferSize(8 * SizeUnit.KB) diff --git a/java/org/rocksdb/BloomFilter.java b/java/org/rocksdb/BloomFilter.java new file mode 100644 index 0000000000..a1cd4a9cec --- /dev/null +++ b/java/org/rocksdb/BloomFilter.java @@ -0,0 +1,23 @@ +// 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; + +public class BloomFilter extends Filter { + private final int bitsPerKey_; + + public BloomFilter(int bitsPerKey) { + super(); + bitsPerKey_ = bitsPerKey; + createNewFilter(); + } + + @Override + protected void createNewFilter() { + createNewFilter0(bitsPerKey_); + } + + private native void createNewFilter0(int bitsKeyKey); +} \ No newline at end of file diff --git a/java/org/rocksdb/Filter.java b/java/org/rocksdb/Filter.java index a0e16eb4d7..de7049a4dc 100644 --- a/java/org/rocksdb/Filter.java +++ b/java/org/rocksdb/Filter.java @@ -17,12 +17,10 @@ package org.rocksdb; * A good value for bitsPerKey is 10, which yields a filter * with ~ 1% false positive rate. */ -public class Filter { - private long nativeHandle_; - - public Filter(int bitsPerKey) { - newFilter(bitsPerKey); - } +public abstract class Filter { + protected long nativeHandle_ = 0; + + protected abstract void createNewFilter(); /** * Deletes underlying C++ filter pointer. @@ -37,10 +35,9 @@ public class Filter { dispose(); } - private boolean isInitialized() { + protected boolean isInitialized() { return (nativeHandle_ != 0); } - private native void newFilter(int bitsPerKey); private native void dispose0(long handle); } diff --git a/java/rocksjni/filter.cc b/java/rocksjni/filter.cc index dd65cf42d6..7ef959814e 100644 --- a/java/rocksjni/filter.cc +++ b/java/rocksjni/filter.cc @@ -12,15 +12,16 @@ #include #include "include/org_rocksdb_Filter.h" +#include "include/org_rocksdb_BloomFilter.h" #include "rocksjni/portal.h" #include "rocksdb/filter_policy.h" /* - * Class: org_rocksdb_Filter - * Method: newFilter + * Class: org_rocksdb_BloomFilter + * Method: createNewFilter0 * Signature: (I)V */ -void Java_org_rocksdb_Filter_newFilter( +void Java_org_rocksdb_BloomFilter_createNewFilter0( JNIEnv* env, jobject jobj, jint bits_per_key) { const rocksdb::FilterPolicy* fp = rocksdb::NewBloomFilterPolicy(bits_per_key); rocksdb::FilterJni::setHandle(env, jobj, fp); From 7a5106fbeabbb30be8e4f4a3a21d610dea64e418 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Tue, 22 Apr 2014 09:01:57 -0700 Subject: [PATCH 7/8] Add doc --- java/org/rocksdb/BloomFilter.java | 14 ++++++++++++++ java/org/rocksdb/Filter.java | 5 ----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/java/org/rocksdb/BloomFilter.java b/java/org/rocksdb/BloomFilter.java index a1cd4a9cec..f0f935a202 100644 --- a/java/org/rocksdb/BloomFilter.java +++ b/java/org/rocksdb/BloomFilter.java @@ -5,12 +5,26 @@ package org.rocksdb; +/** + * This class creates a new filter policy that uses a bloom filter + * with approximately the specified number of bits per key. + * A good value for bitsPerKey is 10, which yields a filter + * with ~ 1% false positive rate. + * + * Default value of bits per key is 10. + */ public class BloomFilter extends Filter { + private static final int DEFAULT_BITS_PER_KEY = 10; private final int bitsPerKey_; + + public BloomFilter() { + this(DEFAULT_BITS_PER_KEY); + } public BloomFilter(int bitsPerKey) { super(); bitsPerKey_ = bitsPerKey; + createNewFilter(); } diff --git a/java/org/rocksdb/Filter.java b/java/org/rocksdb/Filter.java index de7049a4dc..186822abd0 100644 --- a/java/org/rocksdb/Filter.java +++ b/java/org/rocksdb/Filter.java @@ -11,11 +11,6 @@ package org.rocksdb; * information from disk. In many cases, a filter can cut down the * number of disk seeks form a handful to a single disk seek per * DB::Get() call. - * - * This class creates a new filter policy that uses a bloom filter - * with approximately the specified number of bits per key. - * A good value for bitsPerKey is 10, which yields a filter - * with ~ 1% false positive rate. */ public abstract class Filter { protected long nativeHandle_ = 0; From dd9f6f0a3174db51c2d74789bfe1f5603efe1509 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Tue, 22 Apr 2014 10:51:39 -0700 Subject: [PATCH 8/8] Fix formatting --- java/org/rocksdb/BloomFilter.java | 14 +++++++------- java/org/rocksdb/Filter.java | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/java/org/rocksdb/BloomFilter.java b/java/org/rocksdb/BloomFilter.java index f0f935a202..9c4913a8c6 100644 --- a/java/org/rocksdb/BloomFilter.java +++ b/java/org/rocksdb/BloomFilter.java @@ -10,28 +10,28 @@ package org.rocksdb; * with approximately the specified number of bits per key. * A good value for bitsPerKey is 10, which yields a filter * with ~ 1% false positive rate. - * + * * Default value of bits per key is 10. */ public class BloomFilter extends Filter { private static final int DEFAULT_BITS_PER_KEY = 10; private final int bitsPerKey_; - + public BloomFilter() { this(DEFAULT_BITS_PER_KEY); } - + public BloomFilter(int bitsPerKey) { super(); bitsPerKey_ = bitsPerKey; - + createNewFilter(); } - + @Override protected void createNewFilter() { createNewFilter0(bitsPerKey_); } - + private native void createNewFilter0(int bitsKeyKey); -} \ No newline at end of file +} diff --git a/java/org/rocksdb/Filter.java b/java/org/rocksdb/Filter.java index 186822abd0..0de392ac6e 100644 --- a/java/org/rocksdb/Filter.java +++ b/java/org/rocksdb/Filter.java @@ -14,7 +14,7 @@ package org.rocksdb; */ public abstract class Filter { protected long nativeHandle_ = 0; - + protected abstract void createNewFilter(); /**