2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
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 22:19:21 +01:00
|
|
|
// 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 21:30:06 +02:00
|
|
|
#include <limits>
|
2014-08-21 22:55:51 +02:00
|
|
|
#include <string>
|
2014-10-26 23:23:06 +01:00
|
|
|
#include <vector>
|
2014-08-21 22:55:51 +02: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 22:19:21 +01:00
|
|
|
#include "rocksdb/db.h"
|
2014-04-22 08:56:19 +02:00
|
|
|
#include "rocksdb/filter_policy.h"
|
2014-10-09 23:16:41 +02:00
|
|
|
#include "rocksdb/status.h"
|
2014-07-23 20:15:14 +02:00
|
|
|
#include "rocksdb/utilities/backupable_db.h"
|
2015-01-03 15:52:07 +01:00
|
|
|
#include "rocksdb/utilities/write_batch_with_index.h"
|
2014-08-03 22:11:44 +02:00
|
|
|
#include "rocksjni/comparatorjnicallback.h"
|
2015-02-10 21:59:40 +01:00
|
|
|
#include "rocksjni/loggerjnicallback.h"
|
2014-10-23 17:19:38 +02:00
|
|
|
#include "rocksjni/writebatchhandlerjnicallback.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 22:19:21 +01:00
|
|
|
|
2016-01-28 15:44:31 +01:00
|
|
|
// Remove macro on windows
|
|
|
|
#ifdef DELETE
|
|
|
|
#undef DELETE
|
|
|
|
#endif
|
|
|
|
|
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 22:19:21 +01:00
|
|
|
namespace rocksdb {
|
|
|
|
|
2015-03-12 22:25:57 +01:00
|
|
|
// Detect if jlong overflows size_t
|
2014-10-09 23:16:41 +02:00
|
|
|
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 21:30:06 +02:00
|
|
|
}
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// Native class template
|
|
|
|
template<class PTR, class DERIVED> class RocksDBNativeClass {
|
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 22:19:21 +01:00
|
|
|
public:
|
2015-01-24 23:38:43 +01:00
|
|
|
// Get the java class id
|
|
|
|
static jclass getJClass(JNIEnv* env, const char* jclazz_name) {
|
|
|
|
jclass jclazz = env->FindClass(jclazz_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 22:19:21 +01:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// Get the field id of the member variable to store
|
|
|
|
// the ptr
|
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 22:19:21 +01:00
|
|
|
static jfieldID getHandleFieldID(JNIEnv* env) {
|
2014-07-09 18:06:40 +02:00
|
|
|
static jfieldID fid = env->GetFieldID(
|
2015-01-24 23:38:43 +01:00
|
|
|
DERIVED::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 22:19:21 +01:00
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// Get the pointer from Java
|
|
|
|
static PTR getHandle(JNIEnv* env, jobject jobj) {
|
|
|
|
return reinterpret_cast<PTR>(
|
|
|
|
env->GetLongField(jobj, getHandleFieldID(env)));
|
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 22:19:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// Pass the pointer to the java side.
|
|
|
|
static void setHandle(JNIEnv* env, jobject jdb, PTR ptr) {
|
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 22:19:21 +01:00
|
|
|
env->SetLongField(
|
|
|
|
jdb, getHandleFieldID(env),
|
2015-01-24 23:38:43 +01:00
|
|
|
reinterpret_cast<jlong>(ptr));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-12 22:25:57 +01:00
|
|
|
// Java Exception template
|
|
|
|
template<class DERIVED> class RocksDBJavaException {
|
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 22:19:21 +01:00
|
|
|
public:
|
2015-03-12 22:25:57 +01:00
|
|
|
// Get the java class id
|
|
|
|
static jclass getJClass(JNIEnv* env, const char* jclazz_name) {
|
|
|
|
jclass jclazz = env->FindClass(jclazz_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 22:19:21 +01:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and throw a java exception by converting the input
|
2015-03-12 22:25:57 +01:00
|
|
|
// Status.
|
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 22:19:21 +01:00
|
|
|
//
|
|
|
|
// 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 18:06:40 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2015-03-12 22:25:57 +01:00
|
|
|
DERIVED::getJClass(env), "<init>", "(Ljava/lang/String;)V");
|
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 22:19:21 +01:00
|
|
|
assert(mid != nullptr);
|
|
|
|
|
2015-03-12 22:25:57 +01:00
|
|
|
env->Throw((jthrowable)env->NewObject(DERIVED::getJClass(env),
|
|
|
|
mid, msg));
|
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 22:19:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-12 22:25:57 +01:00
|
|
|
// The portal class for org.rocksdb.RocksDB
|
|
|
|
class RocksDBJni : public RocksDBNativeClass<rocksdb::DB*, RocksDBJni> {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.RocksDB.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
return RocksDBNativeClass::getJClass(env, "org/rocksdb/RocksDB");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The portal class for org.rocksdb.RocksDBException
|
|
|
|
class RocksDBExceptionJni :
|
|
|
|
public RocksDBJavaException<RocksDBExceptionJni> {
|
|
|
|
public:
|
|
|
|
// Get the java class id of java.lang.IllegalArgumentException
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
return RocksDBJavaException::getJClass(env,
|
|
|
|
"org/rocksdb/RocksDBException");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The portal class for java.lang.IllegalArgumentException
|
|
|
|
class IllegalArgumentExceptionJni :
|
|
|
|
public RocksDBJavaException<IllegalArgumentExceptionJni> {
|
|
|
|
public:
|
|
|
|
// Get the java class id of java.lang.IllegalArgumentException
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
return RocksDBJavaException::getJClass(env,
|
|
|
|
"java/lang/IllegalArgumentException");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.Options
|
2015-01-28 21:54:01 +01:00
|
|
|
class OptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::Options*, OptionsJni> {
|
2014-04-02 01:59:05 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env, "org/rocksdb/Options");
|
2014-04-02 01:59:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.DBOptions
|
2015-01-28 21:54:01 +01:00
|
|
|
class DBOptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::DBOptions*, DBOptionsJni> {
|
2014-10-28 22:38:08 +01:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env, "org/rocksdb/DBOptions");
|
2014-10-28 22:38:08 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-31 23:39:14 +01:00
|
|
|
class ColumnFamilyDescriptorJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.ColumnFamilyDescriptor
|
|
|
|
static jclass getColumnFamilyDescriptorClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/ColumnFamilyDescriptor");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method id of columnFamilyName
|
|
|
|
static jmethodID getColumnFamilyNameMethod(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getColumnFamilyDescriptorClass(env),
|
2014-12-18 23:43:14 +01:00
|
|
|
"columnFamilyName", "()[B");
|
2014-10-31 23:39:14 +01:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method id of columnFamilyOptions
|
|
|
|
static jmethodID getColumnFamilyOptionsMethod(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getColumnFamilyDescriptorClass(env),
|
|
|
|
"columnFamilyOptions", "()Lorg/rocksdb/ColumnFamilyOptions;");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.ColumnFamilyOptions
|
|
|
|
class ColumnFamilyOptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::ColumnFamilyOptions*, ColumnFamilyOptionsJni> {
|
2014-10-28 22:38:08 +01:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/ColumnFamilyOptions");
|
2014-10-28 22:38:08 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.WriteOptions
|
|
|
|
class WriteOptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::WriteOptions*, WriteOptionsJni> {
|
2014-04-02 22:14:55 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/WriteOptions");
|
2014-04-02 22:14:55 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.ReadOptions
|
|
|
|
class ReadOptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::ReadOptions*, ReadOptionsJni> {
|
2014-04-22 00:52:59 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/ReadOptions");
|
2014-04-22 00:52:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.ReadOptions
|
|
|
|
class WriteBatchJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::WriteBatch*, WriteBatchJni> {
|
2014-04-02 22:14:55 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/WriteBatch");
|
2014-04-02 22:14:55 +02:00
|
|
|
}
|
|
|
|
};
|
2014-04-17 06:38:33 +02:00
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.WriteBatch.Handler
|
|
|
|
class WriteBatchHandlerJni : public RocksDBNativeClass<
|
|
|
|
const rocksdb::WriteBatchHandlerJniCallback*,
|
|
|
|
WriteBatchHandlerJni> {
|
2014-10-23 17:19:38 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/WriteBatch$Handler");
|
2014-10-23 17:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `put` of org.rocksdb.WriteBatch.Handler.
|
|
|
|
static jmethodID getPutMethodId(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getJClass(env), "put", "([B[B)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `merge` of org.rocksdb.WriteBatch.Handler.
|
|
|
|
static jmethodID getMergeMethodId(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getJClass(env), "merge", "([B[B)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `delete` of org.rocksdb.WriteBatch.Handler.
|
|
|
|
static jmethodID getDeleteMethodId(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getJClass(env), "delete", "([B)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `logData` of org.rocksdb.WriteBatch.Handler.
|
|
|
|
static jmethodID getLogDataMethodId(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getJClass(env), "logData", "([B)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `shouldContinue` of org.rocksdb.WriteBatch.Handler.
|
|
|
|
static jmethodID getContinueMethodId(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getJClass(env), "shouldContinue", "()Z");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.WriteBatchWithIndex
|
|
|
|
class WriteBatchWithIndexJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::WriteBatchWithIndex*, WriteBatchWithIndexJni> {
|
2015-01-03 15:52:07 +01:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/WriteBatch");
|
2015-01-03 15:52:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-17 06:38:33 +02:00
|
|
|
class HistogramDataJni {
|
|
|
|
public:
|
|
|
|
static jmethodID getConstructorMethodId(JNIEnv* env, jclass jclazz) {
|
2014-07-09 18:09:08 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(jclazz, "<init>", "(DDDDD)V");
|
2014-04-17 06:38:33 +02:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
2014-04-18 19:47:03 +02:00
|
|
|
}
|
|
|
|
};
|
2014-05-14 07:22:21 +02:00
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.WriteBatchWithIndex
|
|
|
|
class BackupableDBOptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::BackupableDBOptions*, BackupableDBOptionsJni> {
|
2014-04-18 02:28:51 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/BackupableDBOptions");
|
2014-04-17 06:38:33 +02:00
|
|
|
}
|
|
|
|
};
|
2014-04-19 12:26:22 +02:00
|
|
|
|
2015-08-05 01:07:17 +02:00
|
|
|
class BackupEngineJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::BackupEngine*, BackupEngineJni> {
|
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/BackupEngine");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.RocksIterator
|
|
|
|
class IteratorJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::Iterator*, IteratorJni> {
|
2014-04-19 12:26:22 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/RocksIterator");
|
2014-04-19 12:26:22 +02:00
|
|
|
}
|
|
|
|
};
|
2014-04-22 08:56:19 +02:00
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.Filter
|
|
|
|
class FilterJni : public RocksDBNativeClass<
|
|
|
|
std::shared_ptr<rocksdb::FilterPolicy>*, FilterJni> {
|
2014-04-22 08:56:19 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/Filter");
|
2014-10-13 10:34:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.ColumnFamilyHandle
|
|
|
|
class ColumnFamilyHandleJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::ColumnFamilyHandle*, ColumnFamilyHandleJni> {
|
2014-10-13 10:34:52 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/ColumnFamilyHandle");
|
2014-04-22 08:56:19 +02:00
|
|
|
}
|
|
|
|
};
|
2014-04-25 22:57:20 +02:00
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.FlushOptions
|
|
|
|
class FlushOptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::FlushOptions*, FlushOptionsJni> {
|
2014-11-09 20:09:39 +01:00
|
|
|
public:
|
2015-01-24 23:38:43 +01:00
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/FlushOptions");
|
|
|
|
}
|
2014-11-09 20:09:39 +01:00
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.ComparatorOptions
|
|
|
|
class ComparatorOptionsJni : public RocksDBNativeClass<
|
|
|
|
rocksdb::ComparatorJniCallbackOptions*, ComparatorOptionsJni> {
|
2014-08-21 22:55:51 +02:00
|
|
|
public:
|
2015-01-24 23:38:43 +01:00
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/ComparatorOptions");
|
|
|
|
}
|
2014-08-15 14:34:10 +02:00
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.AbstractComparator
|
|
|
|
class AbstractComparatorJni : public RocksDBNativeClass<
|
|
|
|
const rocksdb::BaseComparatorJniCallback*,
|
|
|
|
AbstractComparatorJni> {
|
2014-08-03 22:11:44 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/AbstractComparator");
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 22:55:51 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env),
|
|
|
|
"compare",
|
|
|
|
"(Lorg/rocksdb/AbstractSlice;Lorg/rocksdb/AbstractSlice;)I");
|
2014-08-03 22:11:44 +02:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `findShortestSeparator` of org.rocksdb.Comparator.
|
|
|
|
static jmethodID getFindShortestSeparatorMethodId(JNIEnv* env) {
|
2014-08-21 22:55:51 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env),
|
|
|
|
"findShortestSeparator",
|
|
|
|
"(Ljava/lang/String;Lorg/rocksdb/AbstractSlice;)Ljava/lang/String;");
|
2014-08-03 22:11:44 +02:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method `findShortSuccessor` of org.rocksdb.Comparator.
|
|
|
|
static jmethodID getFindShortSuccessorMethodId(JNIEnv* env) {
|
2014-08-21 22:55:51 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env),
|
|
|
|
"findShortSuccessor",
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/String;");
|
2014-08-03 22:11:44 +02:00
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-24 23:38:43 +01:00
|
|
|
// The portal class for org.rocksdb.AbstractSlice
|
|
|
|
class AbstractSliceJni : public RocksDBNativeClass<
|
|
|
|
const rocksdb::Slice*, AbstractSliceJni> {
|
2014-08-03 22:11:44 +02:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
2015-01-24 23:38:43 +01:00
|
|
|
return RocksDBNativeClass::getJClass(env,
|
|
|
|
"org/rocksdb/AbstractSlice");
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 22:57:20 +02:00
|
|
|
class ListJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of java.util.List.
|
|
|
|
static jclass getListClass(JNIEnv* env) {
|
2014-07-08 06:45:02 +02:00
|
|
|
jclass jclazz = env->FindClass("java/util/List");
|
2014-04-25 22:57:20 +02:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// Get the java class id of java.util.ArrayList.
|
|
|
|
static jclass getArrayListClass(JNIEnv* env) {
|
2014-07-08 06:45:02 +02:00
|
|
|
jclass jclazz = env->FindClass("java/util/ArrayList");
|
2014-04-25 22:57:20 +02:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java class id of java.util.Iterator.
|
|
|
|
static jclass getIteratorClass(JNIEnv* env) {
|
2014-07-08 06:45:02 +02:00
|
|
|
jclass jclazz = env->FindClass("java/util/Iterator");
|
2014-04-25 22:57:20 +02:00
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// Get the java method id of java.util.List.iterator().
|
|
|
|
static jmethodID getIteratorMethod(JNIEnv* env) {
|
2014-07-09 18:06:40 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 22:57:20 +02:00
|
|
|
getListClass(env), "iterator", "()Ljava/util/Iterator;");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// Get the java method id of java.util.Iterator.hasNext().
|
|
|
|
static jmethodID getHasNextMethod(JNIEnv* env) {
|
2014-07-09 18:06:40 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 22:57:20 +02:00
|
|
|
getIteratorClass(env), "hasNext", "()Z");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// Get the java method id of java.util.Iterator.next().
|
|
|
|
static jmethodID getNextMethod(JNIEnv* env) {
|
2014-07-09 18:06:40 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 22:57:20 +02:00
|
|
|
getIteratorClass(env), "next", "()Ljava/lang/Object;");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// Get the java method id of arrayList constructor.
|
|
|
|
static jmethodID getArrayListConstructorMethodId(JNIEnv* env, jclass jclazz) {
|
2014-07-09 18:06:40 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 22:57:20 +02:00
|
|
|
jclazz, "<init>", "(I)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// Get the java method id of java.util.List.add().
|
|
|
|
static jmethodID getListAddMethodId(JNIEnv* env) {
|
2014-07-09 18:06:40 +02:00
|
|
|
static jmethodID mid = env->GetMethodID(
|
2014-04-25 22:57:20 +02:00
|
|
|
getListClass(env), "add", "(Ljava/lang/Object;)Z");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
};
|
2014-08-03 22:11:44 +02:00
|
|
|
|
2015-03-19 23:50:45 +01:00
|
|
|
class ByteJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of java.lang.Byte.
|
|
|
|
static jclass getByteClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("java/lang/Byte");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the java method id of java.lang.Byte.byteValue.
|
|
|
|
static jmethodID getByteValueMethod(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getByteClass(env), "byteValue", "()B");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-26 23:23:06 +01:00
|
|
|
class BackupInfoJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.BackupInfo.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/BackupInfo");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jobject construct0(JNIEnv* env, uint32_t backup_id, int64_t timestamp,
|
|
|
|
uint64_t size, uint32_t number_files) {
|
|
|
|
static jmethodID mid = env->GetMethodID(getJClass(env), "<init>",
|
|
|
|
"(IJJI)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return env->NewObject(getJClass(env), mid,
|
|
|
|
backup_id, timestamp, size, number_files);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BackupInfoListJni {
|
|
|
|
public:
|
|
|
|
static jobject getBackupInfo(JNIEnv* env,
|
|
|
|
std::vector<BackupInfo> backup_infos) {
|
|
|
|
jclass jclazz = env->FindClass("java/util/ArrayList");
|
|
|
|
jmethodID mid = rocksdb::ListJni::getArrayListConstructorMethodId(
|
|
|
|
env, jclazz);
|
|
|
|
jobject jbackup_info_handle_list = env->NewObject(jclazz, mid,
|
|
|
|
backup_infos.size());
|
|
|
|
// insert in java list
|
|
|
|
for (std::vector<rocksdb::BackupInfo>::size_type i = 0;
|
|
|
|
i != backup_infos.size(); i++) {
|
|
|
|
rocksdb::BackupInfo backup_info = backup_infos[i];
|
|
|
|
jobject obj = rocksdb::BackupInfoJni::construct0(env,
|
|
|
|
backup_info.backup_id,
|
|
|
|
backup_info.timestamp,
|
|
|
|
backup_info.size,
|
|
|
|
backup_info.number_files);
|
|
|
|
env->CallBooleanMethod(jbackup_info_handle_list,
|
|
|
|
rocksdb::ListJni::getListAddMethodId(env), obj);
|
|
|
|
}
|
|
|
|
return jbackup_info_handle_list;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-03 16:23:24 +01:00
|
|
|
class WBWIRocksIteratorJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.WBWIRocksIterator.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
static jclass jclazz = env->FindClass("org/rocksdb/WBWIRocksIterator");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jfieldID getWriteEntryField(JNIEnv* env) {
|
|
|
|
static jfieldID fid =
|
|
|
|
env->GetFieldID(getJClass(env), "entry",
|
|
|
|
"Lorg/rocksdb/WBWIRocksIterator$WriteEntry;");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jobject getWriteEntry(JNIEnv* env, jobject jwbwi_rocks_iterator) {
|
|
|
|
jobject jwe =
|
|
|
|
env->GetObjectField(jwbwi_rocks_iterator, getWriteEntryField(env));
|
|
|
|
assert(jwe != nullptr);
|
|
|
|
return jwe;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class WriteTypeJni {
|
|
|
|
public:
|
|
|
|
// Get the PUT enum field of org.rocksdb.WBWIRocksIterator.WriteType
|
|
|
|
static jobject PUT(JNIEnv* env) {
|
|
|
|
return getEnum(env, "PUT");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the MERGE enum field of org.rocksdb.WBWIRocksIterator.WriteType
|
|
|
|
static jobject MERGE(JNIEnv* env) {
|
|
|
|
return getEnum(env, "MERGE");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the DELETE enum field of org.rocksdb.WBWIRocksIterator.WriteType
|
|
|
|
static jobject DELETE(JNIEnv* env) {
|
|
|
|
return getEnum(env, "DELETE");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the LOG enum field of org.rocksdb.WBWIRocksIterator.WriteType
|
|
|
|
static jobject LOG(JNIEnv* env) {
|
|
|
|
return getEnum(env, "LOG");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Get the java class id of org.rocksdb.WBWIRocksIterator.WriteType.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/WBWIRocksIterator$WriteType");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an enum field of org.rocksdb.WBWIRocksIterator.WriteType
|
|
|
|
static jobject getEnum(JNIEnv* env, const char name[]) {
|
|
|
|
jclass jclazz = getJClass(env);
|
|
|
|
jfieldID jfid =
|
|
|
|
env->GetStaticFieldID(jclazz, name,
|
|
|
|
"Lorg/rocksdb/WBWIRocksIterator$WriteType;");
|
|
|
|
assert(jfid != nullptr);
|
|
|
|
return env->GetStaticObjectField(jclazz, jfid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class WriteEntryJni {
|
|
|
|
public:
|
|
|
|
// Get the java class id of org.rocksdb.WBWIRocksIterator.WriteEntry.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
static jclass jclazz =
|
|
|
|
env->FindClass("org/rocksdb/WBWIRocksIterator$WriteEntry");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setWriteType(JNIEnv* env, jobject jwrite_entry,
|
|
|
|
WriteType write_type) {
|
|
|
|
jobject jwrite_type;
|
|
|
|
switch (write_type) {
|
|
|
|
case kPutRecord:
|
|
|
|
jwrite_type = WriteTypeJni::PUT(env);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kMergeRecord:
|
|
|
|
jwrite_type = WriteTypeJni::MERGE(env);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kDeleteRecord:
|
|
|
|
jwrite_type = WriteTypeJni::DELETE(env);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kLogDataRecord:
|
|
|
|
jwrite_type = WriteTypeJni::LOG(env);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
jwrite_type = nullptr;
|
|
|
|
}
|
|
|
|
assert(jwrite_type != nullptr);
|
|
|
|
env->SetObjectField(jwrite_entry, getWriteTypeField(env), jwrite_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setKey(JNIEnv* env, jobject jwrite_entry,
|
|
|
|
const rocksdb::Slice* slice) {
|
|
|
|
jobject jkey = env->GetObjectField(jwrite_entry, getKeyField(env));
|
|
|
|
AbstractSliceJni::setHandle(env, jkey, slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setValue(JNIEnv* env, jobject jwrite_entry,
|
|
|
|
const rocksdb::Slice* slice) {
|
|
|
|
jobject jvalue = env->GetObjectField(jwrite_entry, getValueField(env));
|
|
|
|
AbstractSliceJni::setHandle(env, jvalue, slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static jfieldID getWriteTypeField(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "type", "Lorg/rocksdb/WBWIRocksIterator$WriteType;");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jfieldID getKeyField(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "key", "Lorg/rocksdb/DirectSlice;");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jfieldID getValueField(JNIEnv* env) {
|
|
|
|
static jfieldID fid = env->GetFieldID(
|
|
|
|
getJClass(env), "value", "Lorg/rocksdb/DirectSlice;");
|
|
|
|
assert(fid != nullptr);
|
|
|
|
return fid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-10 21:59:40 +01:00
|
|
|
class InfoLogLevelJni {
|
|
|
|
public:
|
|
|
|
// Get the DEBUG_LEVEL enum field of org.rocksdb.InfoLogLevel
|
|
|
|
static jobject DEBUG_LEVEL(JNIEnv* env) {
|
|
|
|
return getEnum(env, "DEBUG_LEVEL");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the INFO_LEVEL enum field of org.rocksdb.InfoLogLevel
|
|
|
|
static jobject INFO_LEVEL(JNIEnv* env) {
|
|
|
|
return getEnum(env, "INFO_LEVEL");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the WARN_LEVEL enum field of org.rocksdb.InfoLogLevel
|
|
|
|
static jobject WARN_LEVEL(JNIEnv* env) {
|
|
|
|
return getEnum(env, "WARN_LEVEL");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ERROR_LEVEL enum field of org.rocksdb.InfoLogLevel
|
|
|
|
static jobject ERROR_LEVEL(JNIEnv* env) {
|
|
|
|
return getEnum(env, "ERROR_LEVEL");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the FATAL_LEVEL enum field of org.rocksdb.InfoLogLevel
|
|
|
|
static jobject FATAL_LEVEL(JNIEnv* env) {
|
|
|
|
return getEnum(env, "FATAL_LEVEL");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Get the java class id of org.rocksdb.WBWIRocksIterator.WriteType.
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
jclass jclazz = env->FindClass("org/rocksdb/InfoLogLevel");
|
|
|
|
assert(jclazz != nullptr);
|
|
|
|
return jclazz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an enum field of org.rocksdb.WBWIRocksIterator.WriteType
|
|
|
|
static jobject getEnum(JNIEnv* env, const char name[]) {
|
|
|
|
jclass jclazz = getJClass(env);
|
|
|
|
jfieldID jfid =
|
|
|
|
env->GetStaticFieldID(jclazz, name,
|
|
|
|
"Lorg/rocksdb/InfoLogLevel;");
|
|
|
|
assert(jfid != nullptr);
|
|
|
|
return env->GetStaticObjectField(jclazz, jfid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-10 23:16:21 +01:00
|
|
|
// The portal class for org.rocksdb.Logger
|
|
|
|
class LoggerJni : public RocksDBNativeClass<
|
|
|
|
std::shared_ptr<rocksdb::LoggerJniCallback>*, LoggerJni> {
|
2015-02-10 21:59:40 +01:00
|
|
|
public:
|
|
|
|
static jclass getJClass(JNIEnv* env) {
|
|
|
|
return RocksDBNativeClass::getJClass(env,
|
2015-03-10 23:16:21 +01:00
|
|
|
"org/rocksdb/Logger");
|
2015-02-10 21:59:40 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 23:16:21 +01:00
|
|
|
// Get the java method `name` of org.rocksdb.Logger.
|
2015-02-10 21:59:40 +01:00
|
|
|
static jmethodID getLogMethodId(JNIEnv* env) {
|
|
|
|
static jmethodID mid = env->GetMethodID(
|
|
|
|
getJClass(env), "log",
|
|
|
|
"(Lorg/rocksdb/InfoLogLevel;Ljava/lang/String;)V");
|
|
|
|
assert(mid != nullptr);
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
class JniUtil {
|
2014-08-21 22:55:51 +02:00
|
|
|
public:
|
2015-01-03 18:52:17 +01:00
|
|
|
/*
|
2014-08-03 22:11:44 +02: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;
|
|
|
|
}
|
2015-01-03 18:52:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for operations on a key and value
|
|
|
|
* for example WriteBatch->Put
|
|
|
|
*
|
|
|
|
* TODO(AR) could be extended to cover returning rocksdb::Status
|
|
|
|
* from `op` and used for RocksDB->Put etc.
|
|
|
|
*/
|
|
|
|
static void kv_op(
|
|
|
|
std::function<void(rocksdb::Slice, rocksdb::Slice)> op,
|
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
|
|
|
|
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
|
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
|
|
|
|
jentry_value_len);
|
|
|
|
|
|
|
|
op(key_slice, value_slice);
|
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for operations on a key
|
|
|
|
* for example WriteBatch->Delete
|
|
|
|
*
|
|
|
|
* TODO(AR) could be extended to cover returning rocksdb::Status
|
|
|
|
* from `op` and used for RocksDB->Delete etc.
|
|
|
|
*/
|
|
|
|
static void k_op(
|
|
|
|
std::function<void(rocksdb::Slice)> op,
|
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len) {
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
|
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
|
|
|
|
op(key_slice);
|
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
}
|
2014-08-03 22:11:44 +02: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 22:19:21 +01:00
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // JAVA_ROCKSJNI_PORTAL_H_
|