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
|
|
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// This file implements the "bridge" between Java and C++ and enables
|
|
|
|
// calling c++ rocksdb::DB methods from Java side.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <jni.h>
|
|
|
|
#include <string>
|
2014-04-25 22:57:20 +02:00
|
|
|
#include <vector>
|
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 "include/org_rocksdb_RocksDB.h"
|
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
#include "rocksdb/db.h"
|
2014-04-14 22:42:36 +02:00
|
|
|
#include "rocksdb/cache.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
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Open
|
|
|
|
|
2014-04-14 22:42:36 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: open
|
|
|
|
* Signature: (JLjava/lang/String;)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_open(
|
2014-08-25 23:22:55 +02:00
|
|
|
JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path) {
|
2014-04-14 22:42:36 +02:00
|
|
|
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
|
|
|
|
rocksdb::DB* db = nullptr;
|
2014-04-02 01:59:05 +02:00
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
|
2014-04-14 22:42:36 +02:00
|
|
|
rocksdb::Status s = rocksdb::DB::Open(*opt, db_path, &db);
|
2014-04-02 01:59:05 +02:00
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
2014-04-14 22:42:36 +02:00
|
|
|
rocksdb::RocksDBJni::setHandle(env, jdb, db);
|
2014-04-02 01:59:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Put
|
|
|
|
|
|
|
|
void rocksdb_put_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::WriteOptions& write_options,
|
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
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
jbyteArray jvalue, jint jvalue_len) {
|
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, 0);
|
|
|
|
jbyte* value = env->GetByteArrayElements(jvalue, 0);
|
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char*>(value), jvalue_len);
|
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
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
rocksdb::Status s = db->Put(write_options, key_slice, value_slice);
|
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
|
|
|
|
|
|
|
// trigger java unref on key and value.
|
|
|
|
// by passing JNI_ABORT, it will simply release the reference without
|
|
|
|
// copying the result back to the java byte array.
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: put
|
2014-04-03 07:23:04 +02:00
|
|
|
* Signature: (J[BI[BI)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2014-04-03 07:23:04 +02:00
|
|
|
void Java_org_rocksdb_RocksDB_put__J_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
2014-04-02 22:14:55 +02:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
jbyteArray jvalue, jint jvalue_len) {
|
2014-04-03 07:23:04 +02:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
|
|
|
|
|
|
|
rocksdb_put_helper(env, db, default_write_options,
|
|
|
|
jkey, jkey_len,
|
|
|
|
jvalue, jvalue_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: put
|
2014-04-03 07:23:04 +02:00
|
|
|
* Signature: (JJ[BI[BI)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2014-04-03 07:23:04 +02:00
|
|
|
void Java_org_rocksdb_RocksDB_put__JJ_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb,
|
|
|
|
jlong jdb_handle, jlong jwrite_options_handle,
|
2014-04-02 22:14:55 +02:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
jbyteArray jvalue, jint jvalue_len) {
|
2014-04-03 07:23:04 +02:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
jwrite_options_handle);
|
|
|
|
|
|
|
|
rocksdb_put_helper(env, db, *write_options,
|
|
|
|
jkey, jkey_len,
|
|
|
|
jvalue, jvalue_len);
|
|
|
|
}
|
|
|
|
|
2014-04-09 09:48:20 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Write
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: write
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_write(
|
|
|
|
JNIEnv* env, jobject jdb,
|
|
|
|
jlong jwrite_options_handle, jlong jbatch_handle) {
|
|
|
|
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
jwrite_options_handle);
|
|
|
|
auto batch = reinterpret_cast<rocksdb::WriteBatch*>(jbatch_handle);
|
|
|
|
|
|
|
|
rocksdb::Status s = db->Write(*write_options, batch);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Get
|
|
|
|
|
2014-04-22 00:52:59 +02:00
|
|
|
jbyteArray rocksdb_get_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::ReadOptions& read_opt,
|
2014-04-03 07:23:04 +02:00
|
|
|
jbyteArray jkey, jint jkey_len) {
|
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
|
|
|
jboolean isCopy;
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
|
|
|
|
rocksdb::Slice key_slice(
|
|
|
|
reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
|
|
|
|
std::string value;
|
|
|
|
rocksdb::Status s = db->Get(
|
2014-04-22 00:52:59 +02:00
|
|
|
read_opt, key_slice, &value);
|
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
|
|
|
|
|
|
|
// trigger java unref on key.
|
|
|
|
// by passing JNI_ABORT, it will simply release the reference without
|
|
|
|
// copying the result back to the java byte array.
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
|
|
|
|
if (s.IsNotFound()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
jbyteArray jvalue = env->NewByteArray(value.size());
|
|
|
|
env->SetByteArrayRegion(
|
|
|
|
jvalue, 0, value.size(),
|
|
|
|
reinterpret_cast<const jbyte*>(value.c_str()));
|
|
|
|
return jvalue;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
2014-04-22 00:52:59 +02:00
|
|
|
* Signature: (J[BI)[B
|
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
|
|
|
*/
|
2014-04-22 00:52:59 +02:00
|
|
|
jbyteArray Java_org_rocksdb_RocksDB_get__J_3BI(
|
2014-04-03 07:23:04 +02:00
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
2014-04-22 00:52:59 +02:00
|
|
|
jbyteArray jkey, jint jkey_len) {
|
|
|
|
return rocksdb_get_helper(env,
|
|
|
|
reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
rocksdb::ReadOptions(),
|
|
|
|
jkey, jkey_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
|
|
|
* Signature: (JJ[BI)[B
|
|
|
|
*/
|
|
|
|
jbyteArray Java_org_rocksdb_RocksDB_get__JJ_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle, jlong jropt_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len) {
|
|
|
|
return rocksdb_get_helper(env,
|
|
|
|
reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
*reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle),
|
|
|
|
jkey, jkey_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
jint rocksdb_get_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::ReadOptions& read_options,
|
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
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
jbyteArray jvalue, jint jvalue_len) {
|
2014-03-30 07:00:52 +02:00
|
|
|
static const int kNotFound = -1;
|
|
|
|
static const int kStatusError = -2;
|
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
|
|
|
|
2014-04-03 08:54:50 +02:00
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, 0);
|
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
|
|
|
rocksdb::Slice key_slice(
|
|
|
|
reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
|
|
|
|
// TODO(yhchiang): we might save one memory allocation here by adding
|
|
|
|
// a DB::Get() function which takes preallocated jbyte* as input.
|
|
|
|
std::string cvalue;
|
|
|
|
rocksdb::Status s = db->Get(
|
2014-04-22 00:52:59 +02:00
|
|
|
read_options, key_slice, &cvalue);
|
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
|
|
|
|
|
|
|
// trigger java unref on key.
|
|
|
|
// by passing JNI_ABORT, it will simply release the reference without
|
|
|
|
// copying the result back to the java byte array.
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
|
|
|
|
if (s.IsNotFound()) {
|
2014-03-30 07:00:52 +02:00
|
|
|
return kNotFound;
|
|
|
|
} else if (!s.ok()) {
|
|
|
|
// Here since we are throwing a Java exception from c++ side.
|
|
|
|
// As a result, c++ does not know calling this function will in fact
|
|
|
|
// throwing an exception. As a result, the execution flow will
|
|
|
|
// not stop here, and codes after this throw will still be
|
|
|
|
// executed.
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
|
|
|
|
// Return a dummy const value to avoid compilation error, although
|
|
|
|
// java side might not have a chance to get the return value :)
|
|
|
|
return kStatusError;
|
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
|
|
|
}
|
|
|
|
|
2014-03-30 07:00:52 +02:00
|
|
|
int cvalue_len = static_cast<int>(cvalue.size());
|
|
|
|
int length = std::min(jvalue_len, cvalue_len);
|
|
|
|
|
2014-04-03 08:54:50 +02:00
|
|
|
env->SetByteArrayRegion(
|
|
|
|
jvalue, 0, length,
|
|
|
|
reinterpret_cast<const jbyte*>(cvalue.c_str()));
|
|
|
|
return cvalue_len;
|
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
|
|
|
}
|
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
jobject multi_get_helper(JNIEnv* env, jobject jdb, rocksdb::DB* db,
|
|
|
|
const rocksdb::ReadOptions& rOpt, jobject jkey_list, jint jkeys_count) {
|
|
|
|
std::vector<rocksdb::Slice> keys;
|
|
|
|
std::vector<jbyte*> keys_to_free;
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// get iterator
|
|
|
|
jobject iteratorObj = env->CallObjectMethod(
|
|
|
|
jkey_list, rocksdb::ListJni::getIteratorMethod(env));
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// iterate over keys and convert java byte array to slice
|
|
|
|
while(env->CallBooleanMethod(
|
|
|
|
iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) {
|
|
|
|
jbyteArray jkey = (jbyteArray) env->CallObjectMethod(
|
|
|
|
iteratorObj, rocksdb::ListJni::getNextMethod(env));
|
|
|
|
jint key_length = env->GetArrayLength(jkey);
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
jbyte* key = new jbyte[key_length];
|
|
|
|
env->GetByteArrayRegion(jkey, 0, key_length, key);
|
|
|
|
// store allocated jbyte to free it after multiGet call
|
2014-04-26 05:59:16 +02:00
|
|
|
keys_to_free.push_back(key);
|
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
rocksdb::Slice key_slice(
|
|
|
|
reinterpret_cast<char*>(key), key_length);
|
|
|
|
keys.push_back(key_slice);
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
std::vector<std::string> values;
|
|
|
|
std::vector<rocksdb::Status> s = db->MultiGet(rOpt, keys, &values);
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// Don't reuse class pointer
|
2014-04-26 05:59:16 +02:00
|
|
|
jclass jclazz = env->FindClass("java/util/ArrayList");
|
2014-04-25 22:57:20 +02:00
|
|
|
jmethodID mid = rocksdb::ListJni::getArrayListConstructorMethodId(
|
2014-04-26 05:59:16 +02:00
|
|
|
env, jclazz);
|
2014-04-25 22:57:20 +02:00
|
|
|
jobject jvalue_list = env->NewObject(jclazz, mid, jkeys_count);
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// insert in java list
|
|
|
|
for(std::vector<rocksdb::Status>::size_type i = 0; i != s.size(); i++) {
|
|
|
|
if(s[i].ok()) {
|
|
|
|
jbyteArray jvalue = env->NewByteArray(values[i].size());
|
|
|
|
env->SetByteArrayRegion(
|
|
|
|
jvalue, 0, values[i].size(),
|
|
|
|
reinterpret_cast<const jbyte*>(values[i].c_str()));
|
|
|
|
env->CallBooleanMethod(
|
|
|
|
jvalue_list, rocksdb::ListJni::getListAddMethodId(env), jvalue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
env->CallBooleanMethod(
|
|
|
|
jvalue_list, rocksdb::ListJni::getListAddMethodId(env), nullptr);
|
|
|
|
}
|
|
|
|
}
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
// free up allocated byte arrays
|
|
|
|
for(std::vector<jbyte*>::size_type i = 0; i != keys_to_free.size(); i++) {
|
|
|
|
delete[] keys_to_free[i];
|
|
|
|
}
|
|
|
|
keys_to_free.clear();
|
2014-04-26 05:59:16 +02:00
|
|
|
|
2014-04-25 22:57:20 +02:00
|
|
|
return jvalue_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: multiGet
|
|
|
|
* Signature: (JLjava/util/List;I)Ljava/util/List;
|
|
|
|
*/
|
2014-04-26 07:39:33 +02:00
|
|
|
jobject Java_org_rocksdb_RocksDB_multiGet__JLjava_util_List_2I(
|
2014-04-25 22:57:20 +02:00
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jobject jkey_list, jint jkeys_count) {
|
|
|
|
return multi_get_helper(env, jdb, reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
rocksdb::ReadOptions(), jkey_list, jkeys_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: multiGet
|
|
|
|
* Signature: (JJLjava/util/List;I)Ljava/util/List;
|
|
|
|
*/
|
2014-04-26 07:39:33 +02:00
|
|
|
jobject Java_org_rocksdb_RocksDB_multiGet__JJLjava_util_List_2I(
|
2014-04-25 22:57:20 +02:00
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jlong jropt_handle, jobject jkey_list, jint jkeys_count) {
|
|
|
|
return multi_get_helper(env, jdb, reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
*reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle), jkey_list,
|
|
|
|
jkeys_count);
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:52:59 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
|
|
|
* Signature: (J[BI[BI)I
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_RocksDB_get__J_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
jbyteArray jvalue, jint jvalue_len) {
|
|
|
|
return rocksdb_get_helper(env,
|
|
|
|
reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
rocksdb::ReadOptions(),
|
|
|
|
jkey, jkey_len, jvalue, jvalue_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
|
|
|
* Signature: (JJ[BI[BI)I
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_RocksDB_get__JJ_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle, jlong jropt_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
jbyteArray jvalue, jint jvalue_len) {
|
|
|
|
return rocksdb_get_helper(env,
|
|
|
|
reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
*reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle),
|
|
|
|
jkey, jkey_len, jvalue, jvalue_len);
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Delete()
|
|
|
|
void rocksdb_remove_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::WriteOptions& write_options,
|
|
|
|
jbyteArray jkey, jint jkey_len) {
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, 0);
|
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
|
|
|
|
rocksdb::Status s = db->Delete(write_options, key_slice);
|
|
|
|
|
|
|
|
// trigger java unref on key and value.
|
|
|
|
// by passing JNI_ABORT, it will simply release the reference without
|
|
|
|
// copying the result back to the java byte array.
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: remove
|
2014-04-03 07:23:04 +02:00
|
|
|
* Signature: (J[BI)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2014-04-03 07:23:04 +02:00
|
|
|
void Java_org_rocksdb_RocksDB_remove__J_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
|
|
|
|
|
|
|
rocksdb_remove_helper(env, db, default_write_options, jkey, jkey_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: remove
|
2014-04-03 07:23:04 +02:00
|
|
|
* Signature: (JJ[BI)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2014-04-03 07:23:04 +02:00
|
|
|
void Java_org_rocksdb_RocksDB_remove__JJ_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jlong jwrite_options, jbyteArray jkey, jint jkey_len) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(jwrite_options);
|
|
|
|
|
|
|
|
rocksdb_remove_helper(env, db, *write_options, jkey, jkey_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::~DB()
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
[Java] Generalize dis-own native handle and refine dispose framework.
Summary:
1. Move disOwnNativeHandle() function from RocksDB to RocksObject
to allow other RocksObject to use disOwnNativeHandle() when its
ownership of native handle has been transferred.
2. RocksObject now has an abstract implementation of dispose(),
which does the following two things. First, it checks whether
both isOwningNativeHandle() and isInitialized() return true.
If so, it will call the protected abstract function dispose0(),
which all the subclasses of RocksObject should implement. Second,
it sets nativeHandle_ = 0. This redesign ensure all subclasses
of RocksObject have the same dispose behavior.
3. All subclasses of RocksObject now should implement dispose0()
instead of dispose(), and dispose0() will be called only when
isInitialized() returns true.
Test Plan:
make rocksdbjava
make jtest
Reviewers: dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett, haobo
Reviewed By: haobo
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D18801
2014-05-29 03:16:29 +02:00
|
|
|
* Method: disposeInternal
|
2014-05-01 10:44:46 +02:00
|
|
|
* Signature: (J)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
|
|
|
*/
|
[Java] Generalize dis-own native handle and refine dispose framework.
Summary:
1. Move disOwnNativeHandle() function from RocksDB to RocksObject
to allow other RocksObject to use disOwnNativeHandle() when its
ownership of native handle has been transferred.
2. RocksObject now has an abstract implementation of dispose(),
which does the following two things. First, it checks whether
both isOwningNativeHandle() and isInitialized() return true.
If so, it will call the protected abstract function dispose0(),
which all the subclasses of RocksObject should implement. Second,
it sets nativeHandle_ = 0. This redesign ensure all subclasses
of RocksObject have the same dispose behavior.
3. All subclasses of RocksObject now should implement dispose0()
instead of dispose(), and dispose0() will be called only when
isInitialized() returns true.
Test Plan:
make rocksdbjava
make jtest
Reviewers: dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett, haobo
Reviewed By: haobo
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D18801
2014-05-29 03:16:29 +02:00
|
|
|
void Java_org_rocksdb_RocksDB_disposeInternal(
|
2014-05-01 10:44:46 +02:00
|
|
|
JNIEnv* env, jobject java_db, jlong jhandle) {
|
[Java] Generalize dis-own native handle and refine dispose framework.
Summary:
1. Move disOwnNativeHandle() function from RocksDB to RocksObject
to allow other RocksObject to use disOwnNativeHandle() when its
ownership of native handle has been transferred.
2. RocksObject now has an abstract implementation of dispose(),
which does the following two things. First, it checks whether
both isOwningNativeHandle() and isInitialized() return true.
If so, it will call the protected abstract function dispose0(),
which all the subclasses of RocksObject should implement. Second,
it sets nativeHandle_ = 0. This redesign ensure all subclasses
of RocksObject have the same dispose behavior.
3. All subclasses of RocksObject now should implement dispose0()
instead of dispose(), and dispose0() will be called only when
isInitialized() returns true.
Test Plan:
make rocksdbjava
make jtest
Reviewers: dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett, haobo
Reviewed By: haobo
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D18801
2014-05-29 03:16:29 +02:00
|
|
|
delete reinterpret_cast<rocksdb::DB*>(jhandle);
|
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
|
|
|
}
|
2014-04-19 12:26:22 +02:00
|
|
|
|
2014-04-19 22:21:06 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: iterator0
|
|
|
|
* Signature: (J)J
|
|
|
|
*/
|
2014-04-19 12:26:22 +02:00
|
|
|
jlong Java_org_rocksdb_RocksDB_iterator0(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
rocksdb::Iterator* iterator = db->NewIterator(rocksdb::ReadOptions());
|
|
|
|
return reinterpret_cast<jlong>(iterator);
|
2014-04-19 22:05:21 +02:00
|
|
|
}
|
2014-09-24 20:43:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getProperty0
|
|
|
|
* Signature: (JLjava/lang/String;I)Ljava/lang/String;
|
|
|
|
*/
|
|
|
|
jstring Java_org_rocksdb_RocksDB_getProperty0(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jstring jproperty,
|
|
|
|
jint jproperty_len) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
|
|
|
|
const char* property = env->GetStringUTFChars(jproperty, 0);
|
|
|
|
rocksdb::Slice property_slice(property, jproperty_len);
|
|
|
|
|
|
|
|
std::string property_value;
|
|
|
|
bool retCode = db->GetProperty(property_slice, &property_value);
|
|
|
|
env->ReleaseStringUTFChars(jproperty, property);
|
|
|
|
|
|
|
|
if (!retCode) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, rocksdb::Status::NotFound());
|
|
|
|
}
|
|
|
|
|
|
|
|
return env->NewStringUTF(property_value.data());
|
|
|
|
}
|