Change filter implementation

This commit is contained in:
Ankit Gupta
2014-04-21 23:56:19 -07:00
parent cea2be20b6
commit 5e797cf0dd
7 changed files with 138 additions and 29 deletions

39
java/rocksjni/filter.cc Normal file
View File

@@ -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 <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include <string>
#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<rocksdb::FilterPolicy*>(handle);
delete fp;
rocksdb::FilterJni::setHandle(env, jobj, nullptr);
}

View File

@@ -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<rocksdb::Options*>(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<rocksdb::Options*>(jopt_handle)->filter_policy =
reinterpret_cast<rocksdb::FilterPolicy*>(jfp_handle);
}
/*

View File

@@ -12,6 +12,7 @@
#include <jni.h>
#include "rocksdb/db.h"
#include "rocksdb/filter_policy.h"
#include "utilities/backupable_db.h"
namespace rocksdb {
@@ -281,5 +282,38 @@ class IteratorJni {
reinterpret_cast<jlong>(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<rocksdb::FilterPolicy*>(
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<jlong>(op));
}
};
} // namespace rocksdb
#endif // JAVA_ROCKSJNI_PORTAL_H_