From 2214fd8a1506ab0444bea5c4ed931a9b9dcce5e8 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Tue, 22 Apr 2014 08:58:43 -0700 Subject: [PATCH] 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 dd8b4b6fe..1a199e1df 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 4bedc592c..7e5974722 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 000000000..a1cd4a9ce --- /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 a0e16eb4d..de7049a4d 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 dd65cf42d..7ef959814 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);