mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
This commit is contained in:
81
java/rocksjni/portal.h
Normal file
81
java/rocksjni/portal.h
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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 is designed for caching those frequently used IDs and provide
|
||||
// efficient portal (i.e, a set of static functions) to access java code
|
||||
// from c++.
|
||||
|
||||
#ifndef JAVA_ROCKSJNI_PORTAL_H_
|
||||
#define JAVA_ROCKSJNI_PORTAL_H_
|
||||
|
||||
#include <jni.h>
|
||||
#include "rocksdb/db.h"
|
||||
|
||||
namespace rocksdb {
|
||||
|
||||
// The portal class for org.rocksdb.RocksDB
|
||||
class RocksDBJni {
|
||||
public:
|
||||
// Get the java class id of org.rocksdb.RocksDB.
|
||||
static jclass getJClass(JNIEnv* env) {
|
||||
static jclass jclazz = env->FindClass("org/rocksdb/RocksDB");
|
||||
assert(jclazz != nullptr);
|
||||
return jclazz;
|
||||
}
|
||||
|
||||
// Get the field id of the member variable of org.rocksdb.RocksDB
|
||||
// that stores the pointer to rocksdb::DB.
|
||||
static jfieldID getHandleFieldID(JNIEnv* env) {
|
||||
static jfieldID fid = env->GetFieldID(
|
||||
getJClass(env), "nativeHandle", "J");
|
||||
assert(fid != nullptr);
|
||||
return fid;
|
||||
}
|
||||
|
||||
// Get the pointer to rocksdb::DB of the specified org.rocksdb.RocksDB.
|
||||
static rocksdb::DB* getHandle(JNIEnv* env, jobject jdb) {
|
||||
return reinterpret_cast<rocksdb::DB*>(
|
||||
env->GetLongField(jdb, getHandleFieldID(env)));
|
||||
}
|
||||
|
||||
// Pass the rocksdb::DB pointer to the java side.
|
||||
static void setHandle(JNIEnv* env, jobject jdb, rocksdb::DB* db) {
|
||||
env->SetLongField(
|
||||
jdb, getHandleFieldID(env),
|
||||
reinterpret_cast<jlong>(db));
|
||||
}
|
||||
};
|
||||
|
||||
// The portal class for org.rocksdb.RocksDBException
|
||||
class RocksDBExceptionJni {
|
||||
public:
|
||||
// Get the jclass of org.rocksdb.RocksDBException
|
||||
static jclass getJClass(JNIEnv* env) {
|
||||
static jclass jclazz = env->FindClass("org/rocksdb/RocksDBException");
|
||||
assert(jclazz != nullptr);
|
||||
return jclazz;
|
||||
}
|
||||
|
||||
// Create and throw a java exception by converting the input
|
||||
// Status to an RocksDBException.
|
||||
//
|
||||
// In case s.ok() is true, then this function will not throw any
|
||||
// exception.
|
||||
static void ThrowNew(JNIEnv* env, Status s) {
|
||||
if (s.ok()) {
|
||||
return;
|
||||
}
|
||||
jstring msg = env->NewStringUTF(s.ToString().c_str());
|
||||
// get the constructor id of org.rocksdb.RocksDBException
|
||||
static jmethodID mid = env->GetMethodID(
|
||||
getJClass(env), "<init>", "(Ljava/lang/String;)V");
|
||||
assert(mid != nullptr);
|
||||
|
||||
env->Throw((jthrowable)env->NewObject(getJClass(env), mid, msg));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rocksdb
|
||||
#endif // JAVA_ROCKSJNI_PORTAL_H_
|
||||
177
java/rocksjni/rocksjni.cc
Normal file
177
java/rocksjni/rocksjni.cc
Normal file
@@ -0,0 +1,177 @@
|
||||
// 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++ and enables
|
||||
// calling c++ rocksdb::DB methods from Java side.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <jni.h>
|
||||
#include <string>
|
||||
|
||||
#include "include/org_rocksdb_RocksDB.h"
|
||||
#include "rocksjni/portal.h"
|
||||
#include "rocksdb/db.h"
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_RocksDB
|
||||
* Method: open0
|
||||
* Signature: (Ljava/lang/String;)V
|
||||
*/
|
||||
void Java_org_rocksdb_RocksDB_open0(
|
||||
JNIEnv* env, jobject java_db, jstring jdb_path) {
|
||||
rocksdb::DB* db;
|
||||
rocksdb::Options options;
|
||||
options.create_if_missing = true;
|
||||
|
||||
jboolean isCopy = false;
|
||||
const char* db_path = env->GetStringUTFChars(jdb_path, &isCopy);
|
||||
rocksdb::Status s = rocksdb::DB::Open(options, db_path, &db);
|
||||
env->ReleaseStringUTFChars(jdb_path, db_path);
|
||||
|
||||
if (s.ok()) {
|
||||
rocksdb::RocksDBJni::setHandle(env, java_db, db);
|
||||
return;
|
||||
}
|
||||
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_RocksDB
|
||||
* Method: put
|
||||
* Signature: ([BI[BI)V
|
||||
*/
|
||||
void Java_org_rocksdb_RocksDB_put(
|
||||
JNIEnv* env, jobject jdb,
|
||||
jbyteArray jkey, jint jkey_len,
|
||||
jbyteArray jvalue, jint jvalue_len) {
|
||||
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
||||
|
||||
jboolean isCopy;
|
||||
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
|
||||
jbyte* value = env->GetByteArrayElements(jvalue, &isCopy);
|
||||
rocksdb::Slice key_slice(
|
||||
reinterpret_cast<char*>(key), jkey_len);
|
||||
rocksdb::Slice value_slice(
|
||||
reinterpret_cast<char*>(value), jvalue_len);
|
||||
|
||||
rocksdb::Status s = db->Put(
|
||||
rocksdb::WriteOptions(), key_slice, value_slice);
|
||||
|
||||
// trigger java unref on key and value.
|
||||
// by passing JNI_ABORT, it will simply release the reference without
|
||||
// copying the result back to the java byte array.
|
||||
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
||||
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
|
||||
|
||||
if (s.ok()) {
|
||||
return;
|
||||
}
|
||||
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_RocksDB
|
||||
* Method: get
|
||||
* Signature: ([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);
|
||||
|
||||
jboolean isCopy;
|
||||
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
|
||||
rocksdb::Slice key_slice(
|
||||
reinterpret_cast<char*>(key), jkey_len);
|
||||
|
||||
std::string value;
|
||||
rocksdb::Status s = db->Get(
|
||||
rocksdb::ReadOptions(),
|
||||
key_slice, &value);
|
||||
|
||||
// trigger java unref on key.
|
||||
// by passing JNI_ABORT, it will simply release the reference without
|
||||
// copying the result back to the java byte array.
|
||||
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
||||
|
||||
if (s.IsNotFound()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (s.ok()) {
|
||||
jbyteArray jvalue = env->NewByteArray(value.size());
|
||||
env->SetByteArrayRegion(
|
||||
jvalue, 0, value.size(),
|
||||
reinterpret_cast<const jbyte*>(value.c_str()));
|
||||
return jvalue;
|
||||
}
|
||||
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_RocksDB
|
||||
* Method: get
|
||||
* Signature: ([BI[BI)I
|
||||
*/
|
||||
jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
|
||||
JNIEnv* env, jobject jdb,
|
||||
jbyteArray jkey, jint jkey_len,
|
||||
jbyteArray jvalue, jint jvalue_len) {
|
||||
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
||||
|
||||
jboolean isCopy;
|
||||
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
|
||||
jbyte* value = env->GetByteArrayElements(jvalue, &isCopy);
|
||||
rocksdb::Slice key_slice(
|
||||
reinterpret_cast<char*>(key), jkey_len);
|
||||
|
||||
// TODO(yhchiang): we might save one memory allocation here by adding
|
||||
// a DB::Get() function which takes preallocated jbyte* as input.
|
||||
std::string cvalue;
|
||||
rocksdb::Status s = db->Get(
|
||||
rocksdb::ReadOptions(), key_slice, &cvalue);
|
||||
|
||||
// trigger java unref on key.
|
||||
// by passing JNI_ABORT, it will simply release the reference without
|
||||
// copying the result back to the java byte array.
|
||||
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
||||
|
||||
if (s.IsNotFound()) {
|
||||
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
|
||||
return -1;
|
||||
} else if (s.ok()) {
|
||||
int cvalue_len = static_cast<int>(cvalue.size());
|
||||
int length = cvalue_len;
|
||||
// currently we prevent overflowing.
|
||||
if (length > jvalue_len) {
|
||||
length = jvalue_len;
|
||||
}
|
||||
memcpy(value, cvalue.c_str(), length);
|
||||
env->ReleaseByteArrayElements(jvalue, value, JNI_COMMIT);
|
||||
if (cvalue_len > length) {
|
||||
return static_cast<jint>(cvalue.size());
|
||||
}
|
||||
return length;
|
||||
}
|
||||
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_RocksDB
|
||||
* Method: close0
|
||||
* Signature: ()V
|
||||
*/
|
||||
void Java_org_rocksdb_RocksDB_close0(
|
||||
JNIEnv* env, jobject java_db) {
|
||||
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, java_db);
|
||||
delete db;
|
||||
db = nullptr;
|
||||
|
||||
rocksdb::RocksDBJni::setHandle(env, java_db, db);
|
||||
}
|
||||
Reference in New Issue
Block a user