Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
2014-03-28 21:19:21 +00:00
|
|
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// This file implements the "bridge" between Java and C++ and enables
|
|
|
|
// calling c++ rocksdb::DB methods from Java side.
|
|
|
|
|
2015-01-30 20:05:45 +00:00
|
|
|
#include <jni.h>
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
2014-03-28 21:19:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-01-25 21:05:29 +00:00
|
|
|
#include <memory>
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
2014-03-28 21:19:21 +00:00
|
|
|
#include <string>
|
2014-04-25 20:57:20 +00: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 21:19:21 +00:00
|
|
|
|
|
|
|
#include "include/org_rocksdb_RocksDB.h"
|
|
|
|
#include "rocksdb/db.h"
|
2014-04-14 20:42:36 +00:00
|
|
|
#include "rocksdb/cache.h"
|
2015-01-25 21:05:29 +00:00
|
|
|
#include "rocksdb/types.h"
|
2015-01-30 20:05:45 +00:00
|
|
|
#include "rocksjni/portal.h"
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
2014-03-28 21:19:21 +00:00
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Open
|
|
|
|
|
2014-04-14 20:42:36 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: open
|
|
|
|
* Signature: (JLjava/lang/String;)V
|
|
|
|
*/
|
2014-10-13 08:34:52 +00:00
|
|
|
void Java_org_rocksdb_RocksDB_open__JLjava_lang_String_2(
|
2014-08-25 21:22:55 +00:00
|
|
|
JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path) {
|
2014-04-14 20:42:36 +00:00
|
|
|
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
|
|
|
|
rocksdb::DB* db = nullptr;
|
2014-04-01 23:59:05 +00:00
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
|
2014-04-14 20:42:36 +00:00
|
|
|
rocksdb::Status s = rocksdb::DB::Open(*opt, db_path, &db);
|
2014-04-01 23:59:05 +00:00
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
2014-04-14 20:42:36 +00:00
|
|
|
rocksdb::RocksDBJni::setHandle(env, jdb, db);
|
2014-04-01 23:59:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: openROnly
|
|
|
|
* Signature: (JLjava/lang/String;)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_openROnly__JLjava_lang_String_2(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path) {
|
|
|
|
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
|
|
|
|
rocksdb::DB* db = nullptr;
|
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
|
|
|
|
rocksdb::Status s = rocksdb::DB::OpenForReadOnly(*opt,
|
|
|
|
db_path, &db);
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
rocksdb::RocksDBJni::setHandle(env, jdb, db);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: openROnly
|
|
|
|
* Signature: (JLjava/lang/String;Ljava/util/List;I)Ljava/util/List;
|
|
|
|
*/
|
|
|
|
jobject
|
|
|
|
Java_org_rocksdb_RocksDB_openROnly__JLjava_lang_String_2Ljava_util_List_2I(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path,
|
2014-10-31 22:39:14 +00:00
|
|
|
jobject jcfdesc_list, jint jcfdesc_count) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
|
|
|
|
rocksdb::DB* db = nullptr;
|
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
|
|
|
|
|
2014-12-18 22:43:14 +00:00
|
|
|
std::vector<jbyte*> cfnames_to_free;
|
|
|
|
std::vector<jbyteArray> jcfnames_for_free;
|
2014-10-13 08:34:52 +00:00
|
|
|
|
|
|
|
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
|
|
|
|
std::vector<rocksdb::ColumnFamilyHandle* > handles;
|
2014-10-31 22:39:14 +00:00
|
|
|
// get iterator for ColumnFamilyDescriptors
|
2014-10-13 08:34:52 +00:00
|
|
|
jobject iteratorObj = env->CallObjectMethod(
|
2014-10-31 22:39:14 +00:00
|
|
|
jcfdesc_list, rocksdb::ListJni::getIteratorMethod(env));
|
2014-10-13 08:34:52 +00:00
|
|
|
|
2014-10-31 22:39:14 +00:00
|
|
|
// iterate over ColumnFamilyDescriptors
|
2014-10-13 08:34:52 +00:00
|
|
|
while (env->CallBooleanMethod(
|
|
|
|
iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) {
|
2014-10-31 22:39:14 +00:00
|
|
|
// get ColumnFamilyDescriptor
|
|
|
|
jobject jcf_descriptor = env->CallObjectMethod(iteratorObj,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ListJni::getNextMethod(env));
|
2014-10-31 22:39:14 +00:00
|
|
|
// get ColumnFamilyName
|
2014-12-18 22:43:14 +00:00
|
|
|
jbyteArray byteArray = static_cast<jbyteArray>(env->CallObjectMethod(
|
|
|
|
jcf_descriptor,
|
2014-10-31 22:39:14 +00:00
|
|
|
rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyNameMethod(
|
2015-01-10 19:52:03 +00:00
|
|
|
env)));
|
2014-10-31 22:39:14 +00:00
|
|
|
// get CF Options
|
|
|
|
jobject jcf_opt_obj = env->CallObjectMethod(jcf_descriptor,
|
|
|
|
rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyOptionsMethod(
|
2015-01-10 19:52:03 +00:00
|
|
|
env));
|
2014-10-31 22:39:14 +00:00
|
|
|
rocksdb::ColumnFamilyOptions* cfOptions =
|
|
|
|
rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj);
|
|
|
|
|
2014-12-18 22:43:14 +00:00
|
|
|
jbyte* cfname = env->GetByteArrayElements(byteArray, 0);
|
2015-02-03 19:28:33 +00:00
|
|
|
const int len = env->GetArrayLength(byteArray);
|
2014-10-13 08:34:52 +00:00
|
|
|
|
|
|
|
// free allocated cfnames after call to open
|
|
|
|
cfnames_to_free.push_back(cfname);
|
2014-12-18 22:43:14 +00:00
|
|
|
jcfnames_for_free.push_back(byteArray);
|
|
|
|
column_families.push_back(rocksdb::ColumnFamilyDescriptor(
|
2015-02-03 19:28:33 +00:00
|
|
|
std::string(reinterpret_cast<char *>(cfname), len), *cfOptions));
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::Status s = rocksdb::DB::OpenForReadOnly(*opt,
|
|
|
|
db_path, column_families, &handles, &db);
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
// free jbyte allocations
|
|
|
|
for (std::vector<jbyte*>::size_type i = 0;
|
|
|
|
i != cfnames_to_free.size(); i++) {
|
|
|
|
// free cfnames
|
2014-12-18 22:43:14 +00:00
|
|
|
env->ReleaseByteArrayElements(jcfnames_for_free[i], cfnames_to_free[i], 0);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if open operation was successful
|
|
|
|
if (s.ok()) {
|
|
|
|
rocksdb::RocksDBJni::setHandle(env, jdb, db);
|
2014-11-06 22:12:36 +00:00
|
|
|
jclass jListClazz = env->FindClass("java/util/ArrayList");
|
|
|
|
jmethodID midList = rocksdb::ListJni::getArrayListConstructorMethodId(
|
|
|
|
env, jListClazz);
|
|
|
|
jobject jcfhandle_list = env->NewObject(jListClazz,
|
|
|
|
midList, handles.size());
|
2014-10-13 08:34:52 +00:00
|
|
|
// insert in java list
|
|
|
|
for (std::vector<rocksdb::ColumnFamilyHandle*>::size_type i = 0;
|
|
|
|
i != handles.size(); i++) {
|
|
|
|
// jlong must be converted to Long due to collections restrictions
|
2014-11-06 22:12:36 +00:00
|
|
|
jclass jLongClazz = env->FindClass("java/lang/Long");
|
|
|
|
jmethodID midLong = env->GetMethodID(jLongClazz, "<init>", "(J)V");
|
|
|
|
jobject obj = env->NewObject(jLongClazz, midLong,
|
2014-10-13 08:34:52 +00:00
|
|
|
reinterpret_cast<jlong>(handles[i]));
|
|
|
|
env->CallBooleanMethod(jcfhandle_list,
|
|
|
|
rocksdb::ListJni::getListAddMethodId(env), obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return jcfhandle_list;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
2014-11-06 22:12:36 +00:00
|
|
|
return nullptr;
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: open
|
|
|
|
* Signature: (JLjava/lang/String;Ljava/util/List;I)Ljava/util/List;
|
|
|
|
*/
|
|
|
|
jobject Java_org_rocksdb_RocksDB_open__JLjava_lang_String_2Ljava_util_List_2I(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path,
|
2014-10-31 22:39:14 +00:00
|
|
|
jobject jcfdesc_list, jint jcfdesc_count) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
|
|
|
|
rocksdb::DB* db = nullptr;
|
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
|
|
|
|
|
2014-12-18 22:43:14 +00:00
|
|
|
std::vector<jbyte*> cfnames_to_free;
|
|
|
|
std::vector<jbyteArray> jcfnames_for_free;
|
2014-10-13 08:34:52 +00:00
|
|
|
|
|
|
|
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
|
|
|
|
std::vector<rocksdb::ColumnFamilyHandle* > handles;
|
2014-10-31 22:39:14 +00:00
|
|
|
// get iterator for ColumnFamilyDescriptors
|
2014-10-13 08:34:52 +00:00
|
|
|
jobject iteratorObj = env->CallObjectMethod(
|
2014-10-31 22:39:14 +00:00
|
|
|
jcfdesc_list, rocksdb::ListJni::getIteratorMethod(env));
|
2014-10-13 08:34:52 +00:00
|
|
|
|
2014-10-31 22:39:14 +00:00
|
|
|
// iterate over ColumnFamilyDescriptors
|
2014-10-13 08:34:52 +00:00
|
|
|
while (env->CallBooleanMethod(
|
|
|
|
iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) {
|
2014-10-31 22:39:14 +00:00
|
|
|
// get ColumnFamilyDescriptor
|
|
|
|
jobject jcf_descriptor = env->CallObjectMethod(iteratorObj,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ListJni::getNextMethod(env));
|
2014-10-31 22:39:14 +00:00
|
|
|
// get ColumnFamilyName
|
2014-12-18 22:43:14 +00:00
|
|
|
jbyteArray byteArray = static_cast<jbyteArray>(env->CallObjectMethod(
|
|
|
|
jcf_descriptor,
|
2014-10-31 22:39:14 +00:00
|
|
|
rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyNameMethod(
|
2015-01-10 19:52:03 +00:00
|
|
|
env)));
|
2014-10-31 22:39:14 +00:00
|
|
|
// get CF Options
|
|
|
|
jobject jcf_opt_obj = env->CallObjectMethod(jcf_descriptor,
|
|
|
|
rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyOptionsMethod(
|
2015-01-10 19:52:03 +00:00
|
|
|
env));
|
2014-10-31 22:39:14 +00:00
|
|
|
rocksdb::ColumnFamilyOptions* cfOptions =
|
|
|
|
rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj);
|
|
|
|
|
2014-12-18 22:43:14 +00:00
|
|
|
jbyte* cfname = env->GetByteArrayElements(byteArray, 0);
|
2015-02-03 19:28:33 +00:00
|
|
|
const int len = env->GetArrayLength(byteArray);
|
2014-10-13 08:34:52 +00:00
|
|
|
|
|
|
|
// free allocated cfnames after call to open
|
|
|
|
cfnames_to_free.push_back(cfname);
|
2014-12-18 22:43:14 +00:00
|
|
|
jcfnames_for_free.push_back(byteArray);
|
|
|
|
column_families.push_back(rocksdb::ColumnFamilyDescriptor(
|
2015-02-03 19:28:33 +00:00
|
|
|
std::string(reinterpret_cast<char *>(cfname), len), *cfOptions));
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::Status s = rocksdb::DB::Open(*opt, db_path, column_families,
|
|
|
|
&handles, &db);
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
// free jbyte allocations
|
|
|
|
for (std::vector<jbyte*>::size_type i = 0;
|
|
|
|
i != cfnames_to_free.size(); i++) {
|
|
|
|
// free cfnames
|
2014-12-18 22:43:14 +00:00
|
|
|
env->ReleaseByteArrayElements(jcfnames_for_free[i], cfnames_to_free[i], 0);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if open operation was successful
|
|
|
|
if (s.ok()) {
|
|
|
|
rocksdb::RocksDBJni::setHandle(env, jdb, db);
|
2014-11-06 22:12:36 +00:00
|
|
|
jclass jListClazz = env->FindClass("java/util/ArrayList");
|
|
|
|
jmethodID midList = rocksdb::ListJni::getArrayListConstructorMethodId(
|
|
|
|
env, jListClazz);
|
|
|
|
jobject jcfhandle_list = env->NewObject(jListClazz,
|
|
|
|
midList, handles.size());
|
2014-10-13 08:34:52 +00:00
|
|
|
// insert in java list
|
|
|
|
for (std::vector<rocksdb::ColumnFamilyHandle*>::size_type i = 0;
|
|
|
|
i != handles.size(); i++) {
|
|
|
|
// jlong must be converted to Long due to collections restrictions
|
2014-11-06 22:12:36 +00:00
|
|
|
jclass jLongClazz = env->FindClass("java/lang/Long");
|
|
|
|
jmethodID midLong = env->GetMethodID(jLongClazz, "<init>", "(J)V");
|
|
|
|
jobject obj = env->NewObject(jLongClazz, midLong,
|
2014-10-13 08:34:52 +00:00
|
|
|
reinterpret_cast<jlong>(handles[i]));
|
|
|
|
env->CallBooleanMethod(jcfhandle_list,
|
|
|
|
rocksdb::ListJni::getListAddMethodId(env), obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return jcfhandle_list;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
2014-11-06 22:12:36 +00:00
|
|
|
return nullptr;
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::ListColumnFamilies
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: listColumnFamilies
|
|
|
|
* Signature: (JLjava/lang/String;)Ljava/util/List;
|
|
|
|
*/
|
|
|
|
jobject Java_org_rocksdb_RocksDB_listColumnFamilies(
|
|
|
|
JNIEnv* env, jclass jclazz, jlong jopt_handle, jstring jdb_path) {
|
|
|
|
std::vector<std::string> column_family_names;
|
|
|
|
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
|
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
|
|
|
|
jobject jvalue_list = nullptr;
|
|
|
|
|
|
|
|
rocksdb::Status s = rocksdb::DB::ListColumnFamilies(*opt, db_path,
|
|
|
|
&column_family_names);
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
if (s.ok()) {
|
|
|
|
// Don't reuse class pointer
|
2014-11-06 22:12:36 +00:00
|
|
|
jclass jListClazz = env->FindClass("java/util/ArrayList");
|
2014-10-13 08:34:52 +00:00
|
|
|
jmethodID mid = rocksdb::ListJni::getArrayListConstructorMethodId(env,
|
2014-11-06 22:12:36 +00:00
|
|
|
jListClazz);
|
|
|
|
jvalue_list = env->NewObject(jListClazz, mid, column_family_names.size());
|
2014-10-13 08:34:52 +00:00
|
|
|
|
|
|
|
for (std::vector<std::string>::size_type i = 0;
|
|
|
|
i < column_family_names.size(); i++) {
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jcf_value =
|
2014-11-11 21:47:22 +00:00
|
|
|
env->NewByteArray(static_cast<jsize>(column_family_names[i].size()));
|
|
|
|
env->SetByteArrayRegion(
|
|
|
|
jcf_value, 0, static_cast<jsize>(column_family_names[i].size()),
|
2015-02-03 19:28:33 +00:00
|
|
|
reinterpret_cast<const jbyte*>(column_family_names[i].data()));
|
2014-10-13 08:34:52 +00:00
|
|
|
env->CallBooleanMethod(jvalue_list,
|
2014-11-06 22:12:36 +00:00
|
|
|
rocksdb::ListJni::getListAddMethodId(env), jcf_value);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return jvalue_list;
|
|
|
|
}
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Put
|
|
|
|
|
|
|
|
void rocksdb_put_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::WriteOptions& write_options,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle, jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_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 21:19:21 +00:00
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, 0);
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyte* value = env->GetByteArrayElements(jentry_value, 0);
|
2014-04-02 20:14:55 +00:00
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
2014-11-06 22:12:36 +00:00
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
|
|
|
|
jentry_value_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 21:19:21 +00:00
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::Status s;
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
s = db->Put(write_options, cf_handle, key_slice, value_slice);
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
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 21:19:21 +00: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);
|
2014-11-06 22:12:36 +00:00
|
|
|
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
2014-03-28 21:19:21 +00:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: put
|
2014-04-03 05:23:04 +00:00
|
|
|
* Signature: (J[BI[BI)V
|
2014-04-02 20:14:55 +00:00
|
|
|
*/
|
2014-04-03 05:23:04 +00:00
|
|
|
void Java_org_rocksdb_RocksDB_put__J_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
2014-04-02 20:14:55 +00:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-04-03 05:23:04 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2014-04-02 20:14:55 +00:00
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb_put_helper(env, db, default_write_options, nullptr,
|
2014-04-02 20:14:55 +00:00
|
|
|
jkey, jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jentry_value, jentry_value_len);
|
2014-04-02 20:14:55 +00:00
|
|
|
}
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: put
|
|
|
|
* Signature: (J[BI[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_put__J_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
rocksdb_put_helper(env, db, default_write_options, cf_handle,
|
2014-11-06 22:12:36 +00:00
|
|
|
jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-10-13 08:34:52 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 20:14:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: put
|
2014-04-03 05:23:04 +00:00
|
|
|
* Signature: (JJ[BI[BI)V
|
2014-04-02 20:14:55 +00:00
|
|
|
*/
|
2014-04-03 05:23:04 +00:00
|
|
|
void Java_org_rocksdb_RocksDB_put__JJ_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb,
|
|
|
|
jlong jdb_handle, jlong jwrite_options_handle,
|
2014-04-02 20:14:55 +00:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-04-03 05:23:04 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2014-04-02 20:14:55 +00:00
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
jwrite_options_handle);
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb_put_helper(env, db, *write_options, nullptr,
|
2014-04-02 20:14:55 +00:00
|
|
|
jkey, jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jentry_value, jentry_value_len);
|
2014-04-02 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: put
|
|
|
|
* Signature: (JJ[BI[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_put__JJ_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb,
|
|
|
|
jlong jdb_handle, jlong jwrite_options_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
jwrite_options_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
rocksdb_put_helper(env, db, *write_options, cf_handle,
|
2014-11-06 22:12:36 +00:00
|
|
|
jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-10-13 08:34:52 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 07:48:20 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Write
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
2015-01-03 20:19:03 +00:00
|
|
|
* Method: write0
|
2014-04-09 07:48:20 +00:00
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
2015-01-03 20:19:03 +00:00
|
|
|
void Java_org_rocksdb_RocksDB_write0(
|
2014-04-09 07:48:20 +00:00
|
|
|
JNIEnv* env, jobject jdb,
|
2015-01-03 20:19:03 +00:00
|
|
|
jlong jwrite_options_handle, jlong jwb_handle) {
|
2014-04-09 07:48:20 +00:00
|
|
|
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
2015-01-03 20:19:03 +00:00
|
|
|
auto* write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
jwrite_options_handle);
|
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
|
|
|
|
|
|
|
rocksdb::Status s = db->Write(*write_options, wb);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: write1
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_write1(
|
|
|
|
JNIEnv* env, jobject jdb,
|
|
|
|
jlong jwrite_options_handle, jlong jwbwi_handle) {
|
|
|
|
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
|
|
|
auto* write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
2014-04-09 07:48:20 +00:00
|
|
|
jwrite_options_handle);
|
2015-01-03 20:19:03 +00:00
|
|
|
auto* wbwi = reinterpret_cast<rocksdb::WriteBatchWithIndex*>(jwbwi_handle);
|
|
|
|
auto* wb = wbwi->GetWriteBatch();
|
2014-04-09 07:48:20 +00:00
|
|
|
|
2015-01-03 20:19:03 +00:00
|
|
|
rocksdb::Status s = db->Write(*write_options, wb);
|
2014-04-09 07:48:20 +00:00
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::KeyMayExist
|
|
|
|
jboolean key_may_exist_helper(JNIEnv* env, rocksdb::DB* db,
|
|
|
|
const rocksdb::ReadOptions& read_opt,
|
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle, jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jobject jstring_buffer) {
|
2014-10-13 08:34:52 +00:00
|
|
|
std::string value;
|
|
|
|
bool value_found = false;
|
|
|
|
jboolean isCopy;
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
|
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
2014-11-01 00:02:12 +00:00
|
|
|
bool keyMayExist;
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
keyMayExist = db->KeyMayExist(read_opt, cf_handle, key_slice,
|
|
|
|
&value, &value_found);
|
|
|
|
} else {
|
|
|
|
keyMayExist = db->KeyMayExist(read_opt, key_slice,
|
|
|
|
&value, &value_found);
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
if (value_found && !value.empty()) {
|
2014-11-06 22:12:36 +00:00
|
|
|
jclass clazz = env->GetObjectClass(jstring_buffer);
|
2014-10-13 08:34:52 +00:00
|
|
|
jmethodID mid = env->GetMethodID(clazz, "append",
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/StringBuffer;");
|
|
|
|
jstring new_value_str = env->NewStringUTF(value.c_str());
|
2014-11-06 22:12:36 +00:00
|
|
|
env->CallObjectMethod(jstring_buffer, mid, new_value_str);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
2014-11-01 00:02:12 +00:00
|
|
|
return static_cast<jboolean>(keyMayExist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: keyMayExist
|
|
|
|
* Signature: ([BILjava/lang/StringBuffer;)Z
|
|
|
|
*/
|
|
|
|
jboolean Java_org_rocksdb_RocksDB_keyMayExist___3BILjava_lang_StringBuffer_2(
|
|
|
|
JNIEnv* env, jobject jdb, jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jobject jstring_buffer) {
|
2014-11-01 00:02:12 +00:00
|
|
|
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
|
|
|
return key_may_exist_helper(env, db, rocksdb::ReadOptions(),
|
2014-11-06 22:12:36 +00:00
|
|
|
nullptr, jkey, jkey_len, jstring_buffer);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: keyMayExist
|
|
|
|
* Signature: ([BIJLjava/lang/StringBuffer;)Z
|
|
|
|
*/
|
|
|
|
jboolean Java_org_rocksdb_RocksDB_keyMayExist___3BIJLjava_lang_StringBuffer_2(
|
|
|
|
JNIEnv* env, jobject jdb, jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jlong jcf_handle, jobject jstring_buffer) {
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(
|
|
|
|
jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
return key_may_exist_helper(env, db, rocksdb::ReadOptions(),
|
2014-11-06 22:12:36 +00:00
|
|
|
cf_handle, jkey, jkey_len, jstring_buffer);
|
2014-10-13 08:34:52 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-01 00:02:12 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: keyMayExist
|
|
|
|
* Signature: (J[BILjava/lang/StringBuffer;)Z
|
|
|
|
*/
|
|
|
|
jboolean Java_org_rocksdb_RocksDB_keyMayExist__J_3BILjava_lang_StringBuffer_2(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jread_options_handle,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jkey, jint jkey_len, jobject jstring_buffer) {
|
2014-11-01 00:02:12 +00:00
|
|
|
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
|
|
|
auto& read_options = *reinterpret_cast<rocksdb::ReadOptions*>(
|
|
|
|
jread_options_handle);
|
|
|
|
return key_may_exist_helper(env, db, read_options,
|
2014-11-06 22:12:36 +00:00
|
|
|
nullptr, jkey, jkey_len, jstring_buffer);
|
2014-11-01 00:02:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: keyMayExist
|
|
|
|
* Signature: (J[BIJLjava/lang/StringBuffer;)Z
|
|
|
|
*/
|
|
|
|
jboolean Java_org_rocksdb_RocksDB_keyMayExist__J_3BIJLjava_lang_StringBuffer_2(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jread_options_handle,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jkey, jint jkey_len, jlong jcf_handle, jobject jstring_buffer) {
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
|
|
|
|
auto& read_options = *reinterpret_cast<rocksdb::ReadOptions*>(
|
|
|
|
jread_options_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(
|
|
|
|
jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
return key_may_exist_helper(env, db, read_options, cf_handle,
|
2014-11-06 22:12:36 +00:00
|
|
|
jkey, jkey_len, jstring_buffer);
|
2014-10-13 08:34:52 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Get
|
|
|
|
|
2014-04-21 22:52:59 +00:00
|
|
|
jbyteArray rocksdb_get_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::ReadOptions& read_opt,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* column_family_handle, 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 21:19:21 +00:00
|
|
|
jboolean isCopy;
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, &isCopy);
|
|
|
|
rocksdb::Slice key_slice(
|
|
|
|
reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
|
|
|
|
std::string value;
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::Status s;
|
|
|
|
if (column_family_handle != nullptr) {
|
|
|
|
s = db->Get(read_opt, column_family_handle, key_slice, &value);
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
s = db->Get(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 21:19:21 +00: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()) {
|
2014-11-11 21:47:22 +00:00
|
|
|
jbyteArray jret_value = env->NewByteArray(static_cast<jsize>(value.size()));
|
|
|
|
env->SetByteArrayRegion(jret_value, 0, static_cast<jsize>(value.size()),
|
|
|
|
reinterpret_cast<const jbyte*>(value.c_str()));
|
2014-11-06 22:12:36 +00:00
|
|
|
return jret_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 21:19:21 +00:00
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
2014-04-21 22:52:59 +00: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 21:19:21 +00:00
|
|
|
*/
|
2014-04-21 22:52:59 +00:00
|
|
|
jbyteArray Java_org_rocksdb_RocksDB_get__J_3BI(
|
2014-04-03 05:23:04 +00:00
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
2014-04-21 22:52:59 +00:00
|
|
|
jbyteArray jkey, jint jkey_len) {
|
|
|
|
return rocksdb_get_helper(env,
|
|
|
|
reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ReadOptions(), nullptr,
|
2014-04-21 22:52:59 +00:00
|
|
|
jkey, jkey_len);
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
|
|
|
* Signature: (J[BIJ)[B
|
|
|
|
*/
|
|
|
|
jbyteArray Java_org_rocksdb_RocksDB_get__J_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
|
|
|
|
auto db_handle = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
return rocksdb_get_helper(env, db_handle, rocksdb::ReadOptions(),
|
|
|
|
cf_handle, jkey, jkey_len);
|
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
// will never be evaluated
|
|
|
|
return env->NewByteArray(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 22:52:59 +00:00
|
|
|
/*
|
|
|
|
* 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),
|
2014-10-13 08:34:52 +00:00
|
|
|
*reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle), nullptr,
|
2014-04-21 22:52:59 +00:00
|
|
|
jkey, jkey_len);
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
|
|
|
* Signature: (JJ[BIJ)[B
|
|
|
|
*/
|
|
|
|
jbyteArray Java_org_rocksdb_RocksDB_get__JJ_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle, jlong jropt_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
|
|
|
|
auto db_handle = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto& ro_opt = *reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
return rocksdb_get_helper(env, db_handle, ro_opt, cf_handle,
|
|
|
|
jkey, jkey_len);
|
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
// will never be evaluated
|
|
|
|
return env->NewByteArray(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 22:52:59 +00:00
|
|
|
jint rocksdb_get_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::ReadOptions& read_options,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* column_family_handle, jbyteArray jkey,
|
2014-11-06 22:12:36 +00:00
|
|
|
jint jkey_len, jbyteArray jentry_value, jint jentry_value_len) {
|
2014-03-30 05:00:52 +00: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 21:19:21 +00:00
|
|
|
|
2014-04-03 06:54:50 +00: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 21:19:21 +00: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;
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::Status s;
|
|
|
|
if (column_family_handle != nullptr) {
|
|
|
|
s = db->Get(read_options, column_family_handle, key_slice, &cvalue);
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
s = db->Get(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 21:19:21 +00: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 05:00:52 +00: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 21:19:21 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 05:00:52 +00:00
|
|
|
int cvalue_len = static_cast<int>(cvalue.size());
|
2014-11-06 22:12:36 +00:00
|
|
|
int length = std::min(jentry_value_len, cvalue_len);
|
2014-03-30 05:00:52 +00:00
|
|
|
|
2014-04-03 06:54:50 +00:00
|
|
|
env->SetByteArrayRegion(
|
2014-11-06 22:12:36 +00:00
|
|
|
jentry_value, 0, length,
|
2014-04-03 06:54:50 +00:00
|
|
|
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 21:19:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
// cf multi get
|
2014-04-25 20:57:20 +00:00
|
|
|
jobject multi_get_helper(JNIEnv* env, jobject jdb, rocksdb::DB* db,
|
2014-10-13 08:34:52 +00:00
|
|
|
const rocksdb::ReadOptions& rOpt, jobject jkey_list, jint jkeys_count,
|
|
|
|
jobject jcfhandle_list) {
|
2014-04-25 20:57:20 +00:00
|
|
|
std::vector<rocksdb::Slice> keys;
|
|
|
|
std::vector<jbyte*> keys_to_free;
|
2014-10-13 08:34:52 +00:00
|
|
|
std::vector<rocksdb::ColumnFamilyHandle*> cf_handles;
|
|
|
|
|
|
|
|
if (jcfhandle_list != nullptr) {
|
|
|
|
// get cf iterator
|
|
|
|
jobject cfIteratorObj = env->CallObjectMethod(
|
|
|
|
jcfhandle_list, rocksdb::ListJni::getIteratorMethod(env));
|
|
|
|
|
|
|
|
// iterate over keys and convert java byte array to slice
|
|
|
|
while (env->CallBooleanMethod(
|
|
|
|
cfIteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) {
|
|
|
|
jobject jobj = (jbyteArray) env->CallObjectMethod(
|
|
|
|
cfIteratorObj, rocksdb::ListJni::getNextMethod(env));
|
|
|
|
rocksdb::ColumnFamilyHandle* cfHandle =
|
|
|
|
rocksdb::ColumnFamilyHandleJni::getHandle(env, jobj);
|
|
|
|
cf_handles.push_back(cfHandle);
|
|
|
|
}
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
// Process key list
|
2014-04-25 20:57:20 +00:00
|
|
|
// get iterator
|
|
|
|
jobject iteratorObj = env->CallObjectMethod(
|
|
|
|
jkey_list, rocksdb::ListJni::getIteratorMethod(env));
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// iterate over keys and convert java byte array to slice
|
2014-10-09 21:16:41 +00:00
|
|
|
while (env->CallBooleanMethod(
|
2014-04-25 20:57:20 +00:00
|
|
|
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 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00: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 03:59:16 +00:00
|
|
|
keys_to_free.push_back(key);
|
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
rocksdb::Slice key_slice(
|
|
|
|
reinterpret_cast<char*>(key), key_length);
|
|
|
|
keys.push_back(key_slice);
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
std::vector<std::string> values;
|
2014-10-13 08:34:52 +00:00
|
|
|
std::vector<rocksdb::Status> s;
|
|
|
|
if (cf_handles.size() == 0) {
|
|
|
|
s = db->MultiGet(rOpt, keys, &values);
|
|
|
|
} else {
|
|
|
|
s = db->MultiGet(rOpt, cf_handles, keys, &values);
|
|
|
|
}
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// Don't reuse class pointer
|
2014-04-26 03:59:16 +00:00
|
|
|
jclass jclazz = env->FindClass("java/util/ArrayList");
|
2014-04-25 20:57:20 +00:00
|
|
|
jmethodID mid = rocksdb::ListJni::getArrayListConstructorMethodId(
|
2014-04-26 03:59:16 +00:00
|
|
|
env, jclazz);
|
2014-04-25 20:57:20 +00:00
|
|
|
jobject jvalue_list = env->NewObject(jclazz, mid, jkeys_count);
|
2014-04-26 03:59:16 +00:00
|
|
|
|
2014-04-25 20:57:20 +00:00
|
|
|
// insert in java list
|
2014-10-09 21:16:41 +00:00
|
|
|
for (std::vector<rocksdb::Status>::size_type i = 0; i != s.size(); i++) {
|
|
|
|
if (s[i].ok()) {
|
2014-11-11 21:47:22 +00:00
|
|
|
jbyteArray jentry_value =
|
|
|
|
env->NewByteArray(static_cast<jsize>(values[i].size()));
|
2014-04-25 20:57:20 +00:00
|
|
|
env->SetByteArrayRegion(
|
2014-11-11 21:47:22 +00:00
|
|
|
jentry_value, 0, static_cast<jsize>(values[i].size()),
|
2014-04-25 20:57:20 +00:00
|
|
|
reinterpret_cast<const jbyte*>(values[i].c_str()));
|
|
|
|
env->CallBooleanMethod(
|
2014-11-06 22:12:36 +00:00
|
|
|
jvalue_list, rocksdb::ListJni::getListAddMethodId(env),
|
|
|
|
jentry_value);
|
2014-10-09 21:16:41 +00:00
|
|
|
} else {
|
2014-04-25 20:57:20 +00:00
|
|
|
env->CallBooleanMethod(
|
|
|
|
jvalue_list, rocksdb::ListJni::getListAddMethodId(env), nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// free up allocated byte arrays
|
2014-10-09 21:16:41 +00:00
|
|
|
for (std::vector<jbyte*>::size_type i = 0; i != keys_to_free.size(); i++) {
|
2014-04-25 20:57:20 +00:00
|
|
|
delete[] keys_to_free[i];
|
|
|
|
}
|
|
|
|
keys_to_free.clear();
|
|
|
|
return jvalue_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: multiGet
|
|
|
|
* Signature: (JLjava/util/List;I)Ljava/util/List;
|
|
|
|
*/
|
2014-04-26 05:39:33 +00:00
|
|
|
jobject Java_org_rocksdb_RocksDB_multiGet__JLjava_util_List_2I(
|
2014-04-25 20:57:20 +00: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),
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ReadOptions(), jkey_list, jkeys_count, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: multiGet
|
|
|
|
* Signature: (JLjava/util/List;ILjava/util/List;)Ljava/util/List;
|
|
|
|
*/
|
|
|
|
jobject
|
|
|
|
Java_org_rocksdb_RocksDB_multiGet__JLjava_util_List_2ILjava_util_List_2(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jobject jkey_list, jint jkeys_count, jobject jcfhandle_list) {
|
|
|
|
return multi_get_helper(env, jdb, reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
rocksdb::ReadOptions(), jkey_list, jkeys_count, jcfhandle_list);
|
2014-04-25 20:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: multiGet
|
|
|
|
* Signature: (JJLjava/util/List;I)Ljava/util/List;
|
|
|
|
*/
|
2014-04-26 05:39:33 +00:00
|
|
|
jobject Java_org_rocksdb_RocksDB_multiGet__JJLjava_util_List_2I(
|
2014-04-25 20:57:20 +00: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,
|
2014-10-13 08:34:52 +00:00
|
|
|
jkeys_count, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: multiGet
|
|
|
|
* Signature: (JJLjava/util/List;ILjava/util/List;)Ljava/util/List;
|
|
|
|
*/
|
|
|
|
jobject
|
|
|
|
Java_org_rocksdb_RocksDB_multiGet__JJLjava_util_List_2ILjava_util_List_2(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jlong jropt_handle, jobject jkey_list, jint jkeys_count,
|
|
|
|
jobject jcfhandle_list) {
|
|
|
|
return multi_get_helper(env, jdb, reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
*reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle), jkey_list,
|
|
|
|
jkeys_count, jcfhandle_list);
|
2014-04-25 20:57:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 22:52:59 +00: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,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-04-21 22:52:59 +00:00
|
|
|
return rocksdb_get_helper(env,
|
|
|
|
reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ReadOptions(), nullptr,
|
2014-11-06 22:12:36 +00:00
|
|
|
jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-04-21 22:52:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
|
|
|
* Signature: (J[BI[BIJ)I
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_RocksDB_get__J_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto db_handle = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
return rocksdb_get_helper(env, db_handle, rocksdb::ReadOptions(), cf_handle,
|
2014-11-06 22:12:36 +00:00
|
|
|
jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-10-13 08:34:52 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
// will never be evaluated
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 22:52:59 +00:00
|
|
|
/*
|
|
|
|
* 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,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-04-21 22:52:59 +00:00
|
|
|
return rocksdb_get_helper(env,
|
|
|
|
reinterpret_cast<rocksdb::DB*>(jdb_handle),
|
|
|
|
*reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle),
|
2014-11-06 22:12:36 +00:00
|
|
|
nullptr, jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-04-21 22:52:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: get
|
|
|
|
* Signature: (JJ[BI[BIJ)I
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_RocksDB_get__JJ_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle, jlong jropt_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto db_handle = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto& ro_opt = *reinterpret_cast<rocksdb::ReadOptions*>(jropt_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
return rocksdb_get_helper(env, db_handle, ro_opt, cf_handle, jkey,
|
2014-11-06 22:12:36 +00:00
|
|
|
jkey_len, jentry_value, jentry_value_len);
|
2014-10-13 08:34:52 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
// will never be evaluated
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 20:14:55 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Delete()
|
|
|
|
void rocksdb_remove_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::WriteOptions& write_options,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle, jbyteArray jkey, jint jkey_len) {
|
2014-04-02 20:14:55 +00:00
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, 0);
|
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::Status s;
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
s = db->Delete(write_options, cf_handle, key_slice);
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
s = db->Delete(write_options, key_slice);
|
|
|
|
}
|
2014-04-02 20:14:55 +00: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);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: remove
|
2014-04-03 05:23:04 +00:00
|
|
|
* Signature: (J[BI)V
|
2014-04-02 20:14:55 +00:00
|
|
|
*/
|
2014-04-03 05:23:04 +00: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 20:14:55 +00:00
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb_remove_helper(env, db, default_write_options, nullptr,
|
|
|
|
jkey, jkey_len);
|
|
|
|
}
|
2014-04-02 20:14:55 +00:00
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: remove
|
|
|
|
* Signature: (J[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_remove__J_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
rocksdb_remove_helper(env, db, default_write_options, cf_handle,
|
|
|
|
jkey, jkey_len);
|
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
2014-04-02 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: remove
|
2014-10-13 08:34:52 +00:00
|
|
|
* Signature: (JJ[BIJ)V
|
2014-04-02 20:14:55 +00:00
|
|
|
*/
|
2014-04-03 05:23:04 +00: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 20:14:55 +00:00
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(jwrite_options);
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb_remove_helper(env, db, *write_options, nullptr, jkey, jkey_len);
|
2014-04-02 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: remove
|
|
|
|
* Signature: (JJ[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_remove__JJ_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jlong jwrite_options, jbyteArray jkey, jint jkey_len,
|
|
|
|
jlong jcf_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(jwrite_options);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
rocksdb_remove_helper(env, db, *write_options, cf_handle, jkey, jkey_len);
|
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
|
|
|
}
|
2014-09-16 20:58:49 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Merge
|
|
|
|
|
|
|
|
void rocksdb_merge_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::WriteOptions& write_options,
|
2014-10-29 17:40:44 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle, jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-09-16 20:58:49 +00:00
|
|
|
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, 0);
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyte* value = env->GetByteArrayElements(jentry_value, 0);
|
2014-09-16 20:58:49 +00:00
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
2014-11-06 22:12:36 +00:00
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
|
|
|
|
jentry_value_len);
|
2014-09-16 20:58:49 +00:00
|
|
|
|
2014-10-29 17:40:44 +00:00
|
|
|
rocksdb::Status s;
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
s = db->Merge(write_options, cf_handle, key_slice, value_slice);
|
|
|
|
} else {
|
|
|
|
s = db->Merge(write_options, key_slice, value_slice);
|
|
|
|
}
|
2014-09-16 20:58:49 +00: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);
|
2014-11-06 22:12:36 +00:00
|
|
|
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
|
2014-09-16 20:58:49 +00:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: merge
|
|
|
|
* Signature: (J[BI[BI)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_merge__J_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-09-16 20:58:49 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
|
|
|
|
|
|
|
rocksdb_merge_helper(env, db, default_write_options,
|
2014-11-06 22:12:36 +00:00
|
|
|
nullptr, jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-10-29 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: merge
|
|
|
|
* Signature: (J[BI[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_merge__J_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-29 17:40:44 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
static const rocksdb::WriteOptions default_write_options =
|
|
|
|
rocksdb::WriteOptions();
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
rocksdb_merge_helper(env, db, default_write_options,
|
2014-11-06 22:12:36 +00:00
|
|
|
cf_handle, jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-10-29 17:40:44 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
2014-09-16 20:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: merge
|
|
|
|
* Signature: (JJ[BI[BI)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_merge__JJ_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jdb,
|
|
|
|
jlong jdb_handle, jlong jwrite_options_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-09-16 20:58:49 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
jwrite_options_handle);
|
|
|
|
|
|
|
|
rocksdb_merge_helper(env, db, *write_options,
|
2014-11-06 22:12:36 +00:00
|
|
|
nullptr, jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-10-29 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: merge
|
|
|
|
* Signature: (JJ[BI[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_merge__JJ_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jdb,
|
|
|
|
jlong jdb_handle, jlong jwrite_options_handle,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-29 17:40:44 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto write_options = reinterpret_cast<rocksdb::WriteOptions*>(
|
|
|
|
jwrite_options_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
rocksdb_merge_helper(env, db, *write_options,
|
2014-11-06 22:12:36 +00:00
|
|
|
cf_handle, jkey, jkey_len, jentry_value, jentry_value_len);
|
2014-10-29 17:40:44 +00:00
|
|
|
} else {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument("Invalid ColumnFamilyHandle."));
|
|
|
|
}
|
2014-09-16 20:58:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 21:19:21 +00: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 01:16:29 +00:00
|
|
|
* Method: disposeInternal
|
2014-05-01 08:44:46 +00: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 21:19:21 +00: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 01:16:29 +00:00
|
|
|
void Java_org_rocksdb_RocksDB_disposeInternal(
|
2014-05-01 08:44:46 +00: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 01:16:29 +00: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 21:19:21 +00:00
|
|
|
}
|
2014-04-19 10:26:22 +00:00
|
|
|
|
2015-01-16 22:35:21 +00:00
|
|
|
jlong rocksdb_iterator_helper(
|
|
|
|
rocksdb::DB* db, rocksdb::ReadOptions read_options,
|
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle) {
|
|
|
|
rocksdb::Iterator* iterator = nullptr;
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
iterator = db->NewIterator(read_options, cf_handle);
|
|
|
|
} else {
|
|
|
|
iterator = db->NewIterator(read_options);
|
|
|
|
}
|
|
|
|
return reinterpret_cast<jlong>(iterator);
|
|
|
|
}
|
|
|
|
|
2014-04-19 20:21:06 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
2015-01-16 22:35:21 +00:00
|
|
|
* Method: iterator
|
2014-04-19 20:21:06 +00:00
|
|
|
* Signature: (J)J
|
|
|
|
*/
|
2015-01-16 22:35:21 +00:00
|
|
|
jlong Java_org_rocksdb_RocksDB_iterator__J(
|
2014-04-19 10:26:22 +00:00
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
2015-01-16 22:35:21 +00:00
|
|
|
return rocksdb_iterator_helper(db, rocksdb::ReadOptions(),
|
|
|
|
nullptr);
|
2014-04-19 20:05:21 +00:00
|
|
|
}
|
2014-09-24 18:43:35 +00:00
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
2015-01-16 22:35:21 +00:00
|
|
|
* Method: iterator
|
2014-10-13 08:34:52 +00:00
|
|
|
* Signature: (JJ)J
|
|
|
|
*/
|
2015-01-16 22:35:21 +00:00
|
|
|
jlong Java_org_rocksdb_RocksDB_iterator__JJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle,
|
|
|
|
jlong jread_options_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
auto& read_options = *reinterpret_cast<rocksdb::ReadOptions*>(
|
|
|
|
jread_options_handle);
|
|
|
|
return rocksdb_iterator_helper(db, read_options,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: iteratorCF
|
|
|
|
* Signature: (JJ)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_iteratorCF__JJ(
|
2014-10-13 08:34:52 +00:00
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jlong jcf_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
2015-01-16 22:35:21 +00:00
|
|
|
return rocksdb_iterator_helper(db, rocksdb::ReadOptions(),
|
|
|
|
cf_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: iteratorCF
|
|
|
|
* Signature: (JJJ)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_iteratorCF__JJJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jlong jcf_handle,
|
|
|
|
jlong jread_options_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
auto& read_options = *reinterpret_cast<rocksdb::ReadOptions*>(
|
|
|
|
jread_options_handle);
|
|
|
|
return rocksdb_iterator_helper(db, read_options,
|
|
|
|
cf_handle);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: iterators
|
2015-01-16 22:35:21 +00:00
|
|
|
* Signature: (JLjava/util/List;J)[J
|
2014-10-13 08:34:52 +00:00
|
|
|
*/
|
|
|
|
jlongArray Java_org_rocksdb_RocksDB_iterators(
|
2015-01-16 22:35:21 +00:00
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jobject jcfhandle_list,
|
|
|
|
jlong jread_options_handle) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
2015-01-16 22:35:21 +00:00
|
|
|
auto& read_options = *reinterpret_cast<rocksdb::ReadOptions*>(
|
|
|
|
jread_options_handle);
|
2014-10-13 08:34:52 +00:00
|
|
|
std::vector<rocksdb::ColumnFamilyHandle*> cf_handles;
|
|
|
|
std::vector<rocksdb::Iterator*> iterators;
|
|
|
|
|
|
|
|
if (jcfhandle_list != nullptr) {
|
|
|
|
// get cf iterator
|
|
|
|
jobject cfIteratorObj = env->CallObjectMethod(
|
|
|
|
jcfhandle_list, rocksdb::ListJni::getIteratorMethod(env));
|
|
|
|
|
|
|
|
// iterate over keys and convert java byte array to slice
|
|
|
|
while (env->CallBooleanMethod(
|
|
|
|
cfIteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) {
|
|
|
|
jobject jobj = (jbyteArray) env->CallObjectMethod(
|
|
|
|
cfIteratorObj, rocksdb::ListJni::getNextMethod(env));
|
|
|
|
rocksdb::ColumnFamilyHandle* cfHandle =
|
|
|
|
rocksdb::ColumnFamilyHandleJni::getHandle(env, jobj);
|
|
|
|
cf_handles.push_back(cfHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 22:35:21 +00:00
|
|
|
rocksdb::Status s = db->NewIterators(read_options,
|
2014-10-13 08:34:52 +00:00
|
|
|
cf_handles, &iterators);
|
|
|
|
if (s.ok()) {
|
2014-11-11 21:47:22 +00:00
|
|
|
jlongArray jLongArray =
|
|
|
|
env->NewLongArray(static_cast<jsize>(iterators.size()));
|
|
|
|
for (std::vector<rocksdb::Iterator*>::size_type i = 0; i < iterators.size();
|
|
|
|
i++) {
|
|
|
|
env->SetLongArrayRegion(jLongArray, static_cast<jsize>(i), 1,
|
|
|
|
reinterpret_cast<const jlong*>(&iterators[i]));
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
return jLongArray;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
return env->NewLongArray(0);
|
|
|
|
}
|
|
|
|
|
2014-12-28 16:37:57 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getDefaultColumnFamily
|
|
|
|
* Signature: (J)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_getDefaultColumnFamily(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jdb_handle) {
|
|
|
|
auto* db_handle = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto* cf_handle = db_handle->DefaultColumnFamily();
|
|
|
|
return reinterpret_cast<jlong>(cf_handle);
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: createColumnFamily
|
2014-10-31 22:39:14 +00:00
|
|
|
* Signature: (JLorg/rocksdb/ColumnFamilyDescriptor;)J;
|
2014-10-13 08:34:52 +00:00
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_createColumnFamily(
|
2014-10-31 22:39:14 +00:00
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jobject jcf_descriptor) {
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* handle;
|
|
|
|
auto db_handle = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2014-10-31 22:39:14 +00:00
|
|
|
|
2014-12-18 22:43:14 +00:00
|
|
|
// get ColumnFamilyName
|
|
|
|
jbyteArray byteArray = static_cast<jbyteArray>(env->CallObjectMethod(
|
|
|
|
jcf_descriptor,
|
2014-10-31 22:39:14 +00:00
|
|
|
rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyNameMethod(
|
2015-01-10 19:52:03 +00:00
|
|
|
env)));
|
2014-10-31 22:39:14 +00:00
|
|
|
// get CF Options
|
|
|
|
jobject jcf_opt_obj = env->CallObjectMethod(jcf_descriptor,
|
|
|
|
rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyOptionsMethod(
|
2015-01-10 19:52:03 +00:00
|
|
|
env));
|
2014-10-31 22:39:14 +00:00
|
|
|
rocksdb::ColumnFamilyOptions* cfOptions =
|
|
|
|
rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj);
|
|
|
|
|
2014-12-18 22:43:14 +00:00
|
|
|
jbyte* cfname = env->GetByteArrayElements(byteArray, 0);
|
2015-02-03 19:28:33 +00:00
|
|
|
const int len = env->GetArrayLength(byteArray);
|
2015-01-14 23:26:32 +00:00
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::Status s = db_handle->CreateColumnFamily(
|
2015-02-03 19:28:33 +00:00
|
|
|
*cfOptions, std::string(reinterpret_cast<char *>(cfname), len), &handle);
|
2014-12-18 22:43:14 +00:00
|
|
|
env->ReleaseByteArrayElements(byteArray, cfname, 0);
|
2014-10-13 08:34:52 +00:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return reinterpret_cast<jlong>(handle);
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: dropColumnFamily
|
|
|
|
* Signature: (JJ)V;
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_dropColumnFamily(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle, jlong jcf_handle) {
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
auto db_handle = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
rocksdb::Status s = db_handle->DropColumnFamily(cf_handle);
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 18:48:44 +00:00
|
|
|
/*
|
|
|
|
* Method: getSnapshot
|
|
|
|
* Signature: (J)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_getSnapshot(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
const rocksdb::Snapshot* snapshot = db->GetSnapshot();
|
|
|
|
return reinterpret_cast<jlong>(snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Method: releaseSnapshot
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_releaseSnapshot(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jlong snapshot_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
auto snapshot = reinterpret_cast<rocksdb::Snapshot*>(snapshot_handle);
|
|
|
|
db->ReleaseSnapshot(snapshot);
|
|
|
|
}
|
|
|
|
|
2014-09-24 18:43:35 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getProperty0
|
|
|
|
* Signature: (JLjava/lang/String;I)Ljava/lang/String;
|
|
|
|
*/
|
2014-10-13 08:34:52 +00:00
|
|
|
jstring Java_org_rocksdb_RocksDB_getProperty0__JLjava_lang_String_2I(
|
2014-09-24 18:43:35 +00:00
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jstring jproperty,
|
|
|
|
jint jproperty_len) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
2014-10-09 21:16:41 +00:00
|
|
|
|
2014-09-24 18:43:35 +00:00
|
|
|
const char* property = env->GetStringUTFChars(jproperty, 0);
|
|
|
|
rocksdb::Slice property_slice(property, jproperty_len);
|
2014-10-09 21:16:41 +00:00
|
|
|
|
2014-09-24 18:43:35 +00:00
|
|
|
std::string property_value;
|
|
|
|
bool retCode = db->GetProperty(property_slice, &property_value);
|
|
|
|
env->ReleaseStringUTFChars(jproperty, property);
|
2014-10-09 21:16:41 +00:00
|
|
|
|
2014-09-24 18:43:35 +00:00
|
|
|
if (!retCode) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, rocksdb::Status::NotFound());
|
|
|
|
}
|
2014-10-09 21:16:41 +00:00
|
|
|
|
2014-09-24 18:43:35 +00:00
|
|
|
return env->NewStringUTF(property_value.data());
|
|
|
|
}
|
2014-10-13 08:34:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getProperty0
|
|
|
|
* Signature: (JJLjava/lang/String;I)Ljava/lang/String;
|
|
|
|
*/
|
|
|
|
jstring Java_org_rocksdb_RocksDB_getProperty0__JJLjava_lang_String_2I(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jlong jcf_handle,
|
|
|
|
jstring jproperty, jint jproperty_len) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
|
|
|
|
const char* property = env->GetStringUTFChars(jproperty, 0);
|
|
|
|
rocksdb::Slice property_slice(property, jproperty_len);
|
|
|
|
|
|
|
|
std::string property_value;
|
|
|
|
bool retCode = db->GetProperty(cf_handle, property_slice, &property_value);
|
|
|
|
env->ReleaseStringUTFChars(jproperty, property);
|
|
|
|
|
|
|
|
if (!retCode) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, rocksdb::Status::NotFound());
|
|
|
|
}
|
|
|
|
|
|
|
|
return env->NewStringUTF(property_value.data());
|
|
|
|
}
|
2014-11-09 19:09:39 +00:00
|
|
|
|
2014-11-10 11:55:58 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getLongProperty
|
|
|
|
* Signature: (JLjava/lang/String;I)L;
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_getLongProperty__JLjava_lang_String_2I(
|
|
|
|
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);
|
|
|
|
|
|
|
|
uint64_t property_value = 0;
|
|
|
|
bool retCode = db->GetIntProperty(property_slice, &property_value);
|
|
|
|
env->ReleaseStringUTFChars(jproperty, property);
|
|
|
|
|
|
|
|
if (!retCode) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, rocksdb::Status::NotFound());
|
|
|
|
}
|
|
|
|
return property_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getLongProperty
|
|
|
|
* Signature: (JJLjava/lang/String;I)L;
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_getLongProperty__JJLjava_lang_String_2I(
|
|
|
|
JNIEnv* env, jobject jdb, jlong db_handle, jlong jcf_handle,
|
|
|
|
jstring jproperty, jint jproperty_len) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
|
|
|
|
const char* property = env->GetStringUTFChars(jproperty, 0);
|
|
|
|
rocksdb::Slice property_slice(property, jproperty_len);
|
|
|
|
|
|
|
|
uint64_t property_value;
|
|
|
|
bool retCode = db->GetIntProperty(cf_handle, property_slice, &property_value);
|
|
|
|
env->ReleaseStringUTFChars(jproperty, property);
|
|
|
|
|
|
|
|
if (!retCode) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, rocksdb::Status::NotFound());
|
|
|
|
}
|
|
|
|
return property_value;
|
|
|
|
}
|
|
|
|
|
2014-11-09 19:09:39 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::Flush
|
|
|
|
|
|
|
|
void rocksdb_flush_helper(
|
|
|
|
JNIEnv* env, rocksdb::DB* db, const rocksdb::FlushOptions& flush_options,
|
|
|
|
rocksdb::ColumnFamilyHandle* column_family_handle) {
|
|
|
|
rocksdb::Status s;
|
|
|
|
if (column_family_handle != nullptr) {
|
|
|
|
s = db->Flush(flush_options, column_family_handle);
|
|
|
|
} else {
|
|
|
|
s = db->Flush(flush_options);
|
|
|
|
}
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: flush
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_flush__JJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jlong jflush_options) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto flush_options = reinterpret_cast<rocksdb::FlushOptions*>(jflush_options);
|
|
|
|
rocksdb_flush_helper(env, db, *flush_options, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: flush
|
|
|
|
* Signature: (JJJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_flush__JJJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jlong jflush_options, jlong jcf_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto flush_options = reinterpret_cast<rocksdb::FlushOptions*>(jflush_options);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
rocksdb_flush_helper(env, db, *flush_options, cf_handle);
|
|
|
|
}
|
2014-11-17 22:29:52 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::CompactRange - Full
|
|
|
|
|
|
|
|
void rocksdb_compactrange_helper(JNIEnv* env, rocksdb::DB* db,
|
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle, jboolean jreduce_level,
|
|
|
|
jint jtarget_level, jint jtarget_path_id) {
|
|
|
|
|
|
|
|
rocksdb::Status s;
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
s = db->CompactRange(cf_handle, nullptr, nullptr, jreduce_level,
|
|
|
|
jtarget_level, static_cast<uint32_t>(jtarget_path_id));
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
s = db->CompactRange(nullptr, nullptr, jreduce_level,
|
|
|
|
jtarget_level, static_cast<uint32_t>(jtarget_path_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: compactRange0
|
|
|
|
* Signature: (JZII)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_compactRange0__JZII(JNIEnv* env,
|
|
|
|
jobject jdb, jlong jdb_handle, jboolean jreduce_level,
|
|
|
|
jint jtarget_level, jint jtarget_path_id) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
rocksdb_compactrange_helper(env, db, nullptr, jreduce_level,
|
|
|
|
jtarget_level, jtarget_path_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: compactRange
|
|
|
|
* Signature: (JZIIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_compactRange__JZIIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle,
|
|
|
|
jboolean jreduce_level, jint jtarget_level,
|
|
|
|
jint jtarget_path_id, jlong jcf_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
rocksdb_compactrange_helper(env, db, cf_handle, jreduce_level,
|
|
|
|
jtarget_level, jtarget_path_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::CompactRange - Range
|
|
|
|
|
|
|
|
void rocksdb_compactrange_helper(JNIEnv* env, rocksdb::DB* db,
|
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle, jbyteArray jbegin, jint jbegin_len,
|
|
|
|
jbyteArray jend, jint jend_len, jboolean jreduce_level, jint jtarget_level,
|
|
|
|
jint jtarget_path_id) {
|
|
|
|
|
|
|
|
jbyte* begin = env->GetByteArrayElements(jbegin, 0);
|
|
|
|
jbyte* end = env->GetByteArrayElements(jend, 0);
|
|
|
|
const rocksdb::Slice begin_slice(reinterpret_cast<char*>(begin), jbegin_len);
|
|
|
|
const rocksdb::Slice end_slice(reinterpret_cast<char*>(end), jend_len);
|
|
|
|
|
|
|
|
rocksdb::Status s;
|
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
s = db->CompactRange(cf_handle, &begin_slice, &end_slice, jreduce_level,
|
|
|
|
jtarget_level, static_cast<uint32_t>(jtarget_path_id));
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
s = db->CompactRange(&begin_slice, &end_slice, jreduce_level,
|
|
|
|
jtarget_level, static_cast<uint32_t>(jtarget_path_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jbegin, begin, JNI_ABORT);
|
|
|
|
env->ReleaseByteArrayElements(jend, end, JNI_ABORT);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: compactRange0
|
|
|
|
* Signature: (J[BI[BIZII)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_compactRange0__J_3BI_3BIZII(JNIEnv* env,
|
|
|
|
jobject jdb, jlong jdb_handle, jbyteArray jbegin, jint jbegin_len,
|
|
|
|
jbyteArray jend, jint jend_len, jboolean jreduce_level,
|
|
|
|
jint jtarget_level, jint jtarget_path_id) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
rocksdb_compactrange_helper(env, db, nullptr, jbegin, jbegin_len,
|
|
|
|
jend, jend_len, jreduce_level, jtarget_level, jtarget_path_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: compactRange
|
|
|
|
* Signature: (JJ[BI[BIZII)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_compactRange__J_3BI_3BIZIIJ(
|
|
|
|
JNIEnv* env, jobject jdb, jlong jdb_handle, jbyteArray jbegin,
|
|
|
|
jint jbegin_len, jbyteArray jend, jint jend_len,
|
|
|
|
jboolean jreduce_level, jint jtarget_level,
|
|
|
|
jint jtarget_path_id, jlong jcf_handle) {
|
|
|
|
auto db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
rocksdb_compactrange_helper(env, db, cf_handle, jbegin, jbegin_len,
|
|
|
|
jend, jend_len, jreduce_level, jtarget_level, jtarget_path_id);
|
|
|
|
}
|
2015-01-25 21:05:29 +00:00
|
|
|
|
2015-01-25 21:46:11 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::GetLatestSequenceNumber
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getLatestSequenceNumber
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_getLatestSequenceNumber(JNIEnv* env,
|
|
|
|
jobject jdb, jlong jdb_handle) {
|
2015-01-25 21:47:29 +00:00
|
|
|
auto* db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2015-01-25 21:46:11 +00:00
|
|
|
return db->GetLatestSequenceNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB enable/disable file deletions
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: enableFileDeletions
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_disableFileDeletions(JNIEnv* env,
|
|
|
|
jobject jdb, jlong jdb_handle) {
|
2015-01-25 21:47:29 +00:00
|
|
|
auto* db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2015-01-25 21:46:11 +00:00
|
|
|
rocksdb::Status s = db->DisableFileDeletions();
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: enableFileDeletions
|
|
|
|
* Signature: (JZ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_RocksDB_enableFileDeletions(JNIEnv* env,
|
|
|
|
jobject jdb, jlong jdb_handle, jboolean jforce) {
|
2015-01-25 21:47:29 +00:00
|
|
|
auto* db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2015-01-25 21:46:11 +00:00
|
|
|
rocksdb::Status s = db->EnableFileDeletions(jforce);
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 21:05:29 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// rocksdb::DB::GetUpdatesSince
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_RocksDB
|
|
|
|
* Method: getUpdatesSince
|
|
|
|
* Signature: (JJ)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_RocksDB_getUpdatesSince(JNIEnv* env,
|
|
|
|
jobject jdb, jlong jdb_handle, jlong jsequence_number) {
|
2015-01-25 21:47:29 +00:00
|
|
|
auto* db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
|
2015-01-25 21:05:29 +00:00
|
|
|
rocksdb::SequenceNumber sequence_number =
|
|
|
|
static_cast<rocksdb::SequenceNumber>(jsequence_number);
|
|
|
|
std::unique_ptr<rocksdb::TransactionLogIterator> iter;
|
|
|
|
rocksdb::Status s = db->GetUpdatesSince(sequence_number, &iter);
|
|
|
|
if (s.ok()) {
|
|
|
|
return reinterpret_cast<jlong>(iter.release());
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
return 0;
|
|
|
|
}
|