Skip to content

Commit 5e797cf

Browse files
author
Ankit Gupta
committed
Change filter implementation
1 parent cea2be2 commit 5e797cf

File tree

7 files changed

+138
-29
lines changed

7 files changed

+138
-29
lines changed

java/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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
1+
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
22
NATIVE_INCLUDE = ./include
33
ROCKSDB_JAR = rocksdbjni.jar
44

java/RocksDBSample.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ public static void main(String[] args) {
3232
assert(db == null);
3333
}
3434

35+
Filter filter = new Filter(10);
3536
options.setCreateIfMissing(true)
3637
.createStatistics()
3738
.setWriteBufferSize(8 * SizeUnit.KB)
3839
.setMaxWriteBufferNumber(3)
3940
.setDisableSeekCompaction(true)
4041
.setBlockSize(64 * SizeUnit.KB)
4142
.setMaxBackgroundCompactions(10)
42-
.createBloomFilter(10);
43+
.setFilter(filter);
4344
Statistics stats = options.statisticsPtr();
4445

4546
assert(options.createIfMissing() == true);
@@ -225,5 +226,6 @@ public static void main(String[] args) {
225226
// be sure to dispose c++ pointers
226227
options.dispose();
227228
readOptions.dispose();
229+
filter.dispose();
228230
}
229231
}

java/org/rocksdb/Filter.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
6+
package org.rocksdb;
7+
8+
/**
9+
* Filters are stored in rocksdb and are consulted automatically
10+
* by rocksdb to decide whether or not to read some
11+
* information from disk. In many cases, a filter can cut down the
12+
* number of disk seeks form a handful to a single disk seek per
13+
* DB::Get() call.
14+
*
15+
* This function a new filter policy that uses a bloom filter
16+
* with approximately the specified number of bits per key.
17+
* A good value for bitsPerKey is 10, which yields a filter
18+
* with ~ 1% false positive rate.
19+
*/
20+
public class Filter {
21+
private long nativeHandle_;
22+
23+
public Filter(int bitsPerKey) {
24+
newFilter(bitsPerKey);
25+
}
26+
27+
public long getNativeHandle() {
28+
return nativeHandle_;
29+
}
30+
31+
/**
32+
* Deletes underlying C++ filter pointer.
33+
*/
34+
public synchronized void dispose() {
35+
if(nativeHandle_ != 0) {
36+
dispose0(nativeHandle_);
37+
}
38+
}
39+
40+
@Override protected void finalize() {
41+
dispose();
42+
}
43+
44+
private boolean isInitialized() {
45+
return (nativeHandle_ != 0);
46+
}
47+
48+
private native void newFilter(int bitsPerKey);
49+
private native void dispose0(long handle);
50+
}

java/org/rocksdb/Options.java

+5-15
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,14 @@ public long blockSize() {
145145
}
146146

147147
/**
148-
* Filters are stored in rocksdb and are consulted automatically
149-
* by rocksdb to decide whether or not to read some
150-
* information from disk. In many cases, a filter can cut down the
151-
* number of disk seeks form a handful to a single disk seek per
152-
* DB::Get() call.
153-
*
154-
* This function a new filter policy that uses a bloom filter
155-
* with approximately the specified number of bits per key.
156-
* A good value for bitsPerKey is 10, which yields a filter
157-
* with ~ 1% false positive rate.
158-
*
159-
* @param Bits per key for bloom filter.
148+
* Use the specified filter policy to reduce disk reads.
149+
* @param Filter policy java instance.
160150
* @return the instance of the current Options.
161151
* @see RocksDB.open()
162152
*/
163-
public Options createBloomFilter(int bitsPerKey) {
153+
public Options setFilter(Filter filter) {
164154
assert(isInitialized());
165-
createBloomFilter0(nativeHandle_, bitsPerKey);
155+
setFilter0(nativeHandle_, filter.getNativeHandle());
166156
return this;
167157
}
168158

@@ -1260,7 +1250,7 @@ private native void setMaxBackgroundCompactions(
12601250
private native void useFixedLengthPrefixExtractor(
12611251
long handle, int prefixLength);
12621252

1263-
private native void createBloomFilter0(long handle, int bitsPerKey);
1253+
private native void setFilter0(long optHandle, long fpHandle);
12641254

12651255
long nativeHandle_;
12661256
long cacheSize_;

java/rocksjni/filter.cc

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
//
6+
// This file implements the "bridge" between Java and C++ for rocksdb::Filter.
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <jni.h>
11+
#include <string>
12+
13+
#include "include/org_rocksdb_Filter.h"
14+
#include "rocksjni/portal.h"
15+
#include "rocksdb/filter_policy.h"
16+
17+
/*
18+
* Class: org_rocksdb_Filter
19+
* Method: newFilter
20+
* Signature: (I)V
21+
*/
22+
void Java_org_rocksdb_Filter_newFilter(
23+
JNIEnv* env, jobject jobj, jint bits_per_key) {
24+
const rocksdb::FilterPolicy* fp = rocksdb::NewBloomFilterPolicy(bits_per_key);
25+
rocksdb::FilterJni::setHandle(env, jobj, fp);
26+
}
27+
28+
/*
29+
* Class: org_rocksdb_Filter
30+
* Method: dispose0
31+
* Signature: (J)V
32+
*/
33+
void Java_org_rocksdb_Filter_dispose0(
34+
JNIEnv* env, jobject jobj, jlong handle) {
35+
auto fp = reinterpret_cast<rocksdb::FilterPolicy*>(handle);
36+
delete fp;
37+
38+
rocksdb::FilterJni::setHandle(env, jobj, nullptr);
39+
}

java/rocksjni/options.cc

+6-12
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,13 @@ jlong Java_org_rocksdb_Options_statisticsPtr(
122122

123123
/*
124124
* Class: org_rocksdb_Options
125-
* Method: createBloomFilter0
126-
* Signature: (JI)V
125+
* Method: setFilter0
126+
* Signature: (JJ)V
127127
*/
128-
void Java_org_rocksdb_Options_createBloomFilter0(
129-
JNIEnv* env, jobject jobj, jlong jhandle, jint jbits_per_key) {
130-
rocksdb::Options* opt = reinterpret_cast<rocksdb::Options*>(jhandle);
131-
132-
// Delete previously allocated pointer
133-
if(opt->filter_policy) {
134-
delete opt->filter_policy;
135-
}
136-
137-
opt->filter_policy = rocksdb::NewBloomFilterPolicy(jbits_per_key);
128+
void Java_org_rocksdb_Options_setFilter0(
129+
JNIEnv* env, jobject jobj, jlong jopt_handle, jlong jfp_handle) {
130+
reinterpret_cast<rocksdb::Options*>(jopt_handle)->filter_policy =
131+
reinterpret_cast<rocksdb::FilterPolicy*>(jfp_handle);
138132
}
139133

140134
/*

java/rocksjni/portal.h

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <jni.h>
1414
#include "rocksdb/db.h"
15+
#include "rocksdb/filter_policy.h"
1516
#include "utilities/backupable_db.h"
1617

1718
namespace rocksdb {
@@ -281,5 +282,38 @@ class IteratorJni {
281282
reinterpret_cast<jlong>(op));
282283
}
283284
};
285+
286+
class FilterJni {
287+
public:
288+
// Get the java class id of org.rocksdb.Filter.
289+
static jclass getJClass(JNIEnv* env) {
290+
static jclass jclazz = env->FindClass("org/rocksdb/Filter");
291+
assert(jclazz != nullptr);
292+
return jclazz;
293+
}
294+
295+
// Get the field id of the member variable of org.rocksdb.Filter
296+
// that stores the pointer to rocksdb::Iterator.
297+
static jfieldID getHandleFieldID(JNIEnv* env) {
298+
static jfieldID fid = env->GetFieldID(
299+
getJClass(env), "nativeHandle_", "J");
300+
assert(fid != nullptr);
301+
return fid;
302+
}
303+
304+
// Get the pointer to rocksdb::Filter.
305+
static rocksdb::FilterPolicy* getHandle(JNIEnv* env, jobject jobj) {
306+
return reinterpret_cast<rocksdb::FilterPolicy*>(
307+
env->GetLongField(jobj, getHandleFieldID(env)));
308+
}
309+
310+
// Pass the rocksdb::Filter pointer to the java side.
311+
static void setHandle(
312+
JNIEnv* env, jobject jobj, const rocksdb::FilterPolicy* op) {
313+
env->SetLongField(
314+
jobj, getHandleFieldID(env),
315+
reinterpret_cast<jlong>(op));
316+
}
317+
};
284318
} // namespace rocksdb
285319
#endif // JAVA_ROCKSJNI_PORTAL_H_

0 commit comments

Comments
 (0)