[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
This commit is contained in:
Yueh-Hsuan Chiang 2014-04-02 22:23:04 -07:00
parent da0887a3dc
commit 92d2766001
2 changed files with 43 additions and 40 deletions

View File

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

View File

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