Skip to content

Commit dc4b27a

Browse files
author
Ankit Gupta
committed
Add bloom filters
1 parent af6ad11 commit dc4b27a

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

java/RocksDBSample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public static void main(String[] args) {
3838
.setMaxWriteBufferNumber(3)
3939
.setDisableSeekCompaction(true)
4040
.setBlockSize(64 * SizeUnit.KB)
41-
.setMaxBackgroundCompactions(10);
41+
.setMaxBackgroundCompactions(10)
42+
.createBloomFilter(10);
4243
Statistics stats = options.statisticsPtr();
4344

4445
assert(options.createIfMissing() == true);

java/org/rocksdb/Options.java

+24
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ public long blockSize() {
143143
assert(isInitialized());
144144
return blockSize(nativeHandle_);
145145
}
146+
147+
/**
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.
160+
* @return the instance of the current Options.
161+
* @see RocksDB.open()
162+
*/
163+
public Options createBloomFilter(int bitsPerKey) {
164+
assert(isInitialized());
165+
createBloomFilter0(nativeHandle_, bitsPerKey);
166+
return this;
167+
}
146168

147169
/*
148170
* Disable compaction triggered by seek.
@@ -1237,6 +1259,8 @@ private native void setMaxBackgroundCompactions(
12371259

12381260
private native void useFixedLengthPrefixExtractor(
12391261
long handle, int prefixLength);
1262+
1263+
private native void createBloomFilter0(long handle, int bitsPerKey);
12401264

12411265
long nativeHandle_;
12421266
long cacheSize_;

java/rocksjni/options.cc

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "rocksdb/memtablerep.h"
2222
#include "rocksdb/table.h"
2323
#include "rocksdb/slice_transform.h"
24+
#include "rocksdb/filter_policy.h"
2425

2526
/*
2627
* Class: org_rocksdb_Options
@@ -119,6 +120,23 @@ jlong Java_org_rocksdb_Options_statisticsPtr(
119120
return reinterpret_cast<jlong>(st);
120121
}
121122

123+
/*
124+
* Class: org_rocksdb_Options
125+
* Method: createBloomFilter0
126+
* Signature: (JI)V
127+
*/
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);
138+
}
139+
122140
/*
123141
* Class: org_rocksdb_Options
124142
* Method: maxWriteBufferNumber

0 commit comments

Comments
 (0)