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
2014-03-28 21:19:21 +00:00
|
|
|
// 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"
|
2014-04-15 04:06:13 +00:00
|
|
|
#include "rocksdb/statistics.h"
|
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
2014-03-28 21:19:21 +00:00
|
|
|
|
|
|
|
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(
|
2014-04-01 04:46:10 +00:00
|
|
|
getJClass(env), "nativeHandle_", "J");
|
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
2014-03-28 21:19:21 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-01 23:59:05 +00:00
|
|
|
class OptionsJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.Options.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
static jclass jclazz = env->FindClass("org/rocksdb/Options");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.Options
|
|
|
|
// that stores the pointer to rocksdb::Options
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to rocksdb::Options
|
|
|
|
static rocksdb::Options* getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<rocksdb::Options*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rocksdb::Options pointer to the java side.
|
|
|
|
static void setHandle(JNIEnv* env, jobject jobj, rocksdb::Options* op) {
|
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
class WriteOptionsJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.WriteOptions.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
static jclass jclazz = env->FindClass("org/rocksdb/WriteOptions");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.WriteOptions
|
|
|
|
// that stores the pointer to rocksdb::WriteOptions
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to rocksdb::WriteOptions
|
|
|
|
static rocksdb::WriteOptions* getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rocksdb::WriteOptions pointer to the java side.
|
|
|
|
static void setHandle(JNIEnv* env, jobject jobj, rocksdb::WriteOptions* op) {
|
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class WriteBatchJni {
|
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
static jclass jclazz = env->FindClass("org/rocksdb/WriteBatch");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to rocksdb::WriteBatch of the specified
|
|
|
|
// org.rocksdb.WriteBatch.
|
|
|
|
static rocksdb::WriteBatch* getHandle(JNIEnv* env, jobject jwb) {
|
|
|
|
return reinterpret_cast<rocksdb::WriteBatch*>(
|
|
|
|
env->GetLongField(jwb, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rocksdb::WriteBatch pointer to the java side.
|
|
|
|
static void setHandle(JNIEnv* env, jobject jwb, rocksdb::WriteBatch* wb) {
|
|
|
|
env->SetLongField(
|
|
|
|
jwb, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(wb));
|
|
|
|
}
|
|
|
|
};
|
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
2014-03-28 21:19:21 +00:00
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // JAVA_ROCKSJNI_PORTAL_H_
|