[JNI] Improve the internal interface between java and c++ for basic db operations.

Summary:
Improve the internal interface between java and c++ for basic db operations
by including the RocksDB native handle (i.e., c++ pointer of rocksdb::DB)
as a input parameter of the internal interface.

This improvement reduces one JNI call per db operation from c++.

Test Plan: make test

Reviewers: haobo, sdong, dhruba

Reviewed By: sdong

CC: leveldb

Differential Revision: https://reviews.facebook.net/D17445
main
Yueh-Hsuan Chiang 11 years ago
parent da0887a3dc
commit 92d2766001
  1. 26
      java/org/rocksdb/RocksDB.java
  2. 55
      java/rocksjni/rocksjni.cc

@ -61,7 +61,7 @@ public class RocksDB {
* @param value the value associated with the specified key.
*/
public void put(byte[] key, byte[] value) throws RocksDBException {
put(key, key.length, value, value.length);
put(nativeHandle_, key, key.length, value, value.length);
}
/**
@ -72,7 +72,7 @@ public class RocksDB {
*/
public void put(WriteOptions writeOpts, byte[] key, byte[] value)
throws RocksDBException {
put(writeOpts.nativeHandle_, key, key.length, value, value.length);
put(nativeHandle_, writeOpts.nativeHandle_, key, key.length, value, value.length);
}
/**
@ -88,7 +88,7 @@ public class RocksDB {
* found.
*/
public int get(byte[] key, byte[] value) throws RocksDBException {
return get(key, key.length, value, value.length);
return get(nativeHandle_, key, key.length, value, value.length);
}
/**
@ -103,7 +103,7 @@ public class RocksDB {
* @see RocksDBException
*/
public byte[] get(byte[] key) throws RocksDBException {
return get(key, key.length);
return get(nativeHandle_, key, key.length);
}
/**
@ -112,7 +112,7 @@ public class RocksDB {
* did not exist in the database.
*/
public void remove(byte[] key) throws RocksDBException {
remove(key, key.length);
remove(nativeHandle_, key, key.length);
}
/**
@ -122,7 +122,7 @@ public class RocksDB {
*/
public void remove(WriteOptions writeOpt, byte[] key)
throws RocksDBException {
remove(writeOpt.nativeHandle_, key, key.length);
remove(nativeHandle_, writeOpt.nativeHandle_, key, key.length);
}
@Override protected void finalize() {
@ -141,20 +141,22 @@ public class RocksDB {
private native void open(
long optionsHandle, String path) throws RocksDBException;
private native void put(
byte[] key, int keyLen,
long handle, byte[] key, int keyLen,
byte[] value, int valueLen) throws RocksDBException;
private native void put(
long writeOptHandle, byte[] key, int keyLen,
long handle, long writeOptHandle,
byte[] key, int keyLen,
byte[] value, int valueLen) throws RocksDBException;
private native int get(
byte[] key, int keyLen,
long handle, byte[] key, int keyLen,
byte[] value, int valueLen) throws RocksDBException;
private native byte[] get(
byte[] key, int keyLen) throws RocksDBException;
long handle, byte[] key, int keyLen) throws RocksDBException;
private native void remove(
byte[] key, int keyLen) throws RocksDBException;
long handle, byte[] key, int keyLen) throws RocksDBException;
private native void remove(
long writeOptHandle, byte[] key, int keyLen) throws RocksDBException;
long handle, long writeOptHandle,
byte[] key, int keyLen) throws RocksDBException;
private native void close0();
private long nativeHandle_;

@ -87,13 +87,13 @@ void rocksdb_put_helper(
/*
* Class: org_rocksdb_RocksDB
* Method: put
* Signature: ([BI[BI)V
* Signature: (J[BI[BI)V
*/
void Java_org_rocksdb_RocksDB_put___3BI_3BI(
JNIEnv* env, jobject jdb,
void Java_org_rocksdb_RocksDB_put__J_3BI_3BI(
JNIEnv* env, jobject jdb, jlong jdb_handle,
jbyteArray jkey, jint jkey_len,
jbyteArray jvalue, jint jvalue_len) {
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
static const rocksdb::WriteOptions default_write_options =
rocksdb::WriteOptions();
@ -105,13 +105,14 @@ void Java_org_rocksdb_RocksDB_put___3BI_3BI(
/*
* Class: org_rocksdb_RocksDB
* Method: put
* Signature: (J[BI[BI)V
* Signature: (JJ[BI[BI)V
*/
void Java_org_rocksdb_RocksDB_put__J_3BI_3BI(
JNIEnv* env, jobject jdb, jlong jwrite_options_handle,
void Java_org_rocksdb_RocksDB_put__JJ_3BI_3BI(
JNIEnv* env, jobject jdb,
jlong jdb_handle, jlong jwrite_options_handle,
jbyteArray jkey, jint jkey_len,
jbyteArray jvalue, jint jvalue_len) {
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(
jwrite_options_handle);
@ -126,11 +127,12 @@ void Java_org_rocksdb_RocksDB_put__J_3BI_3BI(
/*
* Class: org_rocksdb_RocksDB
* Method: get
* Signature: ([BI)[B
* Signature: (J[BI)[B
*/
jbyteArray Java_org_rocksdb_RocksDB_get___3BI(
JNIEnv* env, jobject jdb, jbyteArray jkey, jint jkey_len) {
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
jbyteArray Java_org_rocksdb_RocksDB_get__J_3BI(
JNIEnv* env, jobject jdb, jlong jdb_handle,
jbyteArray jkey, jint jkey_len) {
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
jboolean isCopy;
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
@ -166,16 +168,15 @@ jbyteArray Java_org_rocksdb_RocksDB_get___3BI(
/*
* Class: org_rocksdb_RocksDB
* Method: get
* Signature: ([BI[BI)I
* Signature: (J[BI[BI)I
*/
jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
JNIEnv* env, jobject jdb,
jint Java_org_rocksdb_RocksDB_get__J_3BI_3BI(
JNIEnv* env, jobject jdb, jlong jdb_handle,
jbyteArray jkey, jint jkey_len,
jbyteArray jvalue, jint jvalue_len) {
static const int kNotFound = -1;
static const int kStatusError = -2;
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
jboolean isCopy;
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
@ -240,15 +241,15 @@ void rocksdb_remove_helper(
return;
}
/*
* Class: org_rocksdb_RocksDB
* Method: remove
* Signature: ([BI)V
* Signature: (J[BI)V
*/
void Java_org_rocksdb_RocksDB_remove___3BI(
JNIEnv* env, jobject jdb, jbyteArray jkey, jint jkey_len) {
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
void Java_org_rocksdb_RocksDB_remove__J_3BI(
JNIEnv* env, jobject jdb, jlong jdb_handle,
jbyteArray jkey, jint jkey_len) {
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
static const rocksdb::WriteOptions default_write_options =
rocksdb::WriteOptions();
@ -258,12 +259,12 @@ void Java_org_rocksdb_RocksDB_remove___3BI(
/*
* Class: org_rocksdb_RocksDB
* Method: remove
* Signature: (J[BI)V
* Signature: (JJ[BI)V
*/
void Java_org_rocksdb_RocksDB_remove__J_3BI(
JNIEnv* env, jobject jdb, jlong jwrite_options,
jbyteArray jkey, jint jkey_len) {
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
void Java_org_rocksdb_RocksDB_remove__JJ_3BI(
JNIEnv* env, jobject jdb, jlong jdb_handle,
jlong jwrite_options, jbyteArray jkey, jint jkey_len) {
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(jwrite_options);
rocksdb_remove_helper(env, db, *write_options, jkey, jkey_len);

Loading…
Cancel
Save