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>
|
2014-09-17 19:30:06 +00:00
|
|
|
#include <limits>
|
2014-08-21 20:55:51 +00:00
|
|
|
#include <string>
|
|
|
|
|
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
|
|
|
#include "rocksdb/db.h"
|
2014-04-22 06:56:19 +00:00
|
|
|
#include "rocksdb/filter_policy.h"
|
2014-10-09 21:16:41 +00:00
|
|
|
#include "rocksdb/status.h"
|
2014-07-23 18:15:14 +00:00
|
|
|
#include "rocksdb/utilities/backupable_db.h"
|
2014-08-03 20:11:44 +00:00
|
|
|
#include "rocksjni/comparatorjnicallback.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 {
|
|
|
|
|
2014-10-09 21:16:41 +00:00
|
|
|
// detect if jlong overflows size_t
|
|
|
|
inline Status check_if_jlong_fits_size_t(const jlong& jvalue) {
|
|
|
|
Status s = Status::OK();
|
|
|
|
if (static_cast<uint64_t>(jvalue) > std::numeric_limits<size_t>::max()) {
|
|
|
|
s = Status::InvalidArgument(Slice("jlong overflows 32 bit value."));
|
|
|
|
}
|
|
|
|
return s;
|
2014-09-17 19:30:06 +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
2014-03-28 21:19:21 +00:00
|
|
|
// The portal class for org.rocksdb.RocksDB
|
|
|
|
class RocksDBJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.RocksDB.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/RocksDB");
|
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(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) {
|
2014-07-09 16:06:40 +00:00
|
|
|
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) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/RocksDBException");
|
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(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
|
2014-07-09 16:06:40 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
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
|
|
|
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) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/Options");
|
2014-04-01 23:59:05 +00:00
|
|
|
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) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2014-04-01 23:59:05 +00:00
|
|
|
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) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/WriteOptions");
|
2014-04-02 20:14:55 +00:00
|
|
|
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) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2014-04-02 20:14:55 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-21 22:52:59 +00:00
|
|
|
|
|
|
|
class ReadOptionsJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.ReadOptions.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/ReadOptions");
|
2014-04-21 22:52:59 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.ReadOptions
|
|
|
|
// that stores the pointer to rocksdb::ReadOptions
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2014-04-21 22:52:59 +00:00
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to rocksdb::ReadOptions
|
|
|
|
static rocksdb::ReadOptions* getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<rocksdb::ReadOptions*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rocksdb::ReadOptions pointer to the java side.
|
|
|
|
static void setHandle(JNIEnv* env, jobject jobj,
|
|
|
|
rocksdb::ReadOptions* op) {
|
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
class WriteBatchJni {
|
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/WriteBatch");
|
2014-04-02 20:14:55 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2014-04-02 20:14:55 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
2014-04-17 04:38:33 +00:00
|
|
|
|
|
|
|
class HistogramDataJni {
|
|
|
|
public:
|
|
|
|
static jmethodID getConstructorMethodId(JNIEnv* env, jclass jclazz) {
|
2014-07-09 16:09:08 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(jclazz, "<init>", "(DDDDD)V");
|
2014-04-17 04:38:33 +00:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
2014-04-18 17:47:03 +00:00
|
|
|
}
|
|
|
|
};
|
2014-05-14 05:22:21 +00:00
|
|
|
|
2014-04-18 00:28:51 +00:00
|
|
|
class BackupableDBOptionsJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.BackupableDBOptions.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/BackupableDBOptions");
|
2014-04-18 00:28:51 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.BackupableDBOptions
|
|
|
|
// that stores the pointer to rocksdb::BackupableDBOptions
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2014-04-18 00:28:51 +00:00
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to rocksdb::BackupableDBOptions
|
|
|
|
static rocksdb::BackupableDBOptions* getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<rocksdb::BackupableDBOptions*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rocksdb::BackupableDBOptions pointer to the java side.
|
|
|
|
static void setHandle(
|
|
|
|
JNIEnv* env, jobject jobj, rocksdb::BackupableDBOptions* op) {
|
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
2014-04-17 04:38:33 +00:00
|
|
|
}
|
|
|
|
};
|
2014-04-19 10:26:22 +00:00
|
|
|
|
|
|
|
class IteratorJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.Iteartor.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/RocksIterator");
|
2014-04-19 10:26:22 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.Iterator
|
2014-04-19 20:21:06 +00:00
|
|
|
// that stores the pointer to rocksdb::Iterator.
|
2014-04-19 10:26:22 +00:00
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2014-04-19 10:26:22 +00:00
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
2014-04-19 20:21:06 +00:00
|
|
|
// Get the pointer to rocksdb::Iterator.
|
2014-04-19 10:26:22 +00:00
|
|
|
static rocksdb::Iterator* getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<rocksdb::Iterator*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rocksdb::Iterator pointer to the java side.
|
|
|
|
static void setHandle(
|
|
|
|
JNIEnv* env, jobject jobj, rocksdb::Iterator* op) {
|
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
|
|
|
}
|
|
|
|
};
|
2014-04-22 06:56:19 +00:00
|
|
|
|
|
|
|
class FilterJni {
|
|
|
|
public:
|
2014-04-22 07:09:40 +00:00
|
|
|
// Get the java class id of org.rocksdb.FilterPolicy.
|
2014-04-22 06:56:19 +00:00
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/Filter");
|
2014-04-22 06:56:19 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.Filter
|
2014-04-22 07:09:40 +00:00
|
|
|
// that stores the pointer to rocksdb::FilterPolicy.
|
2014-04-22 06:56:19 +00:00
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2014-04-22 06:56:19 +00:00
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:09:40 +00:00
|
|
|
// Get the pointer to rocksdb::FilterPolicy.
|
2014-10-14 16:38:50 +00:00
|
|
|
static std::shared_ptr<rocksdb::FilterPolicy>* getHandle(
|
|
|
|
JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast
|
|
|
|
<std::shared_ptr<rocksdb::FilterPolicy> *>(
|
2014-04-22 06:56:19 +00:00
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:09:40 +00:00
|
|
|
// Pass the rocksdb::FilterPolicy pointer to the java side.
|
2014-04-22 06:56:19 +00:00
|
|
|
static void setHandle(
|
2014-10-14 16:38:50 +00:00
|
|
|
JNIEnv* env, jobject jobj, std::shared_ptr<rocksdb::FilterPolicy>* op) {
|
2014-04-22 06:56:19 +00:00
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ColumnFamilyHandleJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.ColumnFamilyHandle.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/ColumnFamilyHandle");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.ColumnFamilyHandle.
|
|
|
|
// that stores the pointer to rocksdb::ColumnFamilyHandle.
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to rocksdb::ColumnFamilyHandle.
|
|
|
|
static rocksdb::ColumnFamilyHandle* getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<rocksdb::ColumnFamilyHandle*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rocksdb::ColumnFamilyHandle pointer to the java side.
|
|
|
|
static void setHandle(
|
|
|
|
JNIEnv* env, jobject jobj, const rocksdb::ColumnFamilyHandle* op) {
|
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
2014-04-22 06:56:19 +00:00
|
|
|
}
|
|
|
|
};
|
2014-04-25 20:57:20 +00:00
|
|
|
|
2014-08-15 12:34:10 +00:00
|
|
|
class ComparatorOptionsJni {
|
2014-08-21 20:55:51 +00:00
|
|
|
public:
|
2014-08-15 12:34:10 +00:00
|
|
|
// Get the java class id of org.rocksdb.ComparatorOptions.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/ComparatorOptions");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.ComparatorOptions
|
|
|
|
// that stores the pointer to rocksdb::ComparatorJniCallbackOptions.
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the ComparatorJniCallbackOptions pointer to the java side.
|
|
|
|
static void setHandle(
|
2014-08-21 20:55:51 +00:00
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
const rocksdb::ComparatorJniCallbackOptions* op) {
|
2014-08-15 12:34:10 +00:00
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-03 20:11:44 +00:00
|
|
|
class AbstractComparatorJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.Comparator.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/AbstractComparator");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.Comparator
|
|
|
|
// that stores the pointer to rocksdb::Comparator.
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `name` of org.rocksdb.Comparator.
|
|
|
|
static jmethodID getNameMethodId(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getJClass(env), "name", "()Ljava/lang/String;");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `compare` of org.rocksdb.Comparator.
|
|
|
|
static jmethodID getCompareMethodId(JNIEnv* env) {
|
2014-08-21 20:55:51 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env),
|
|
|
|
"compare",
|
|
|
|
"(Lorg/rocksdb/AbstractSlice;Lorg/rocksdb/AbstractSlice;)I");
|
2014-08-03 20:11:44 +00:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `findShortestSeparator` of org.rocksdb.Comparator.
|
|
|
|
static jmethodID getFindShortestSeparatorMethodId(JNIEnv* env) {
|
2014-08-21 20:55:51 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env),
|
|
|
|
"findShortestSeparator",
|
|
|
|
"(Ljava/lang/String;Lorg/rocksdb/AbstractSlice;)Ljava/lang/String;");
|
2014-08-03 20:11:44 +00:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `findShortSuccessor` of org.rocksdb.Comparator.
|
|
|
|
static jmethodID getFindShortSuccessorMethodId(JNIEnv* env) {
|
2014-08-21 20:55:51 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env),
|
|
|
|
"findShortSuccessor",
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/String;");
|
2014-08-03 20:11:44 +00:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to ComparatorJniCallback.
|
2014-08-21 20:55:51 +00:00
|
|
|
static rocksdb::BaseComparatorJniCallback* getHandle(
|
|
|
|
JNIEnv* env, jobject jobj) {
|
2014-08-03 20:11:44 +00:00
|
|
|
return reinterpret_cast<rocksdb::BaseComparatorJniCallback*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the ComparatorJniCallback pointer to the java side.
|
|
|
|
static void setHandle(
|
2014-08-21 20:55:51 +00:00
|
|
|
JNIEnv* env, jobject jobj, const rocksdb::BaseComparatorJniCallback* op) {
|
2014-08-03 20:11:44 +00:00
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class AbstractSliceJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.Slice.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/AbstractSlice");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the field id of the member variable of org.rocksdb.Slice
|
|
|
|
// that stores the pointer to rocksdb::Slice.
|
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "nativeHandle_", "J");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pointer to Slice.
|
|
|
|
static rocksdb::Slice* getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<rocksdb::Slice*>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the Slice pointer to the java side.
|
|
|
|
static void setHandle(
|
|
|
|
JNIEnv* env, jobject jobj, const rocksdb::Slice* op) {
|
|
|
|
env->SetLongField(
|
|
|
|
jobj, getHandleFieldID(env),
|
|
|
|
reinterpret_cast<jlong>(op));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SliceJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.Slice.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/Slice");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jobject construct0(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env), "<init>", "()V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return env->NewObject(getJClass(env), mid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DirectSliceJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.DirectSlice.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/DirectSlice");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jobject construct0(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env), "<init>", "()V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return env->NewObject(getJClass(env), mid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
class ListJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of java.util.List.
|
|
|
|
static jclass getListClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("java/util/List");
|
2014-04-25 20:57:20 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// Get the java class id of java.util.ArrayList.
|
|
|
|
static jclass getArrayListClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("java/util/ArrayList");
|
2014-04-25 20:57:20 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java class id of java.util.Iterator.
|
|
|
|
static jclass getIteratorClass(JNIEnv* env) {
|
2014-07-08 04:45:02 +00:00
|
|
|
jclass jclazz = env->FindClass("java/util/Iterator");
|
2014-04-25 20:57:20 +00:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// Get the java method id of java.util.List.iterator().
|
|
|
|
static jmethodID getIteratorMethod(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 20:57:20 +00:00
|
|
|
getListClass(env), "iterator", "()Ljava/util/Iterator;");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// Get the java method id of java.util.Iterator.hasNext().
|
|
|
|
static jmethodID getHasNextMethod(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 20:57:20 +00:00
|
|
|
getIteratorClass(env), "hasNext", "()Z");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// Get the java method id of java.util.Iterator.next().
|
|
|
|
static jmethodID getNextMethod(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 20:57:20 +00:00
|
|
|
getIteratorClass(env), "next", "()Ljava/lang/Object;");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// Get the java method id of arrayList constructor.
|
|
|
|
static jmethodID getArrayListConstructorMethodId(JNIEnv* env, jclass jclazz) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 20:57:20 +00:00
|
|
|
jclazz, "<init>", "(I)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// Get the java method id of java.util.List.add().
|
|
|
|
static jmethodID getListAddMethodId(JNIEnv* env) {
|
2014-07-09 16:06:40 +00:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 20:57:20 +00:00
|
|
|
getListClass(env), "add", "(Ljava/lang/Object;)Z");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
};
|
2014-08-03 20:11:44 +00:00
|
|
|
|
|
|
|
class JniUtil {
|
2014-08-21 20:55:51 +00:00
|
|
|
public:
|
2014-08-03 20:11:44 +00:00
|
|
|
/**
|
|
|
|
* Copies a jstring to a std::string
|
|
|
|
* and releases the original jstring
|
|
|
|
*/
|
|
|
|
static std::string copyString(JNIEnv* env, jstring js) {
|
|
|
|
const char *utf = env->GetStringUTFChars(js, NULL);
|
|
|
|
std::string name(utf);
|
|
|
|
env->ReleaseStringUTFChars(js, utf);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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_
|