2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-11-17 18:22:44 +00:00
|
|
|
//
|
|
|
|
// This file implements the "bridge" between Java and C++ and enables
|
2020-02-20 20:07:53 +00:00
|
|
|
// calling c++ ROCKSDB_NAMESPACE::TtlDB methods.
|
2014-11-17 18:22:44 +00:00
|
|
|
// from Java side.
|
|
|
|
|
2015-01-13 21:56:37 +00:00
|
|
|
#include <jni.h>
|
2014-11-17 18:22:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-04-13 00:55:14 +00:00
|
|
|
#include <memory>
|
2014-11-17 18:22:44 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_TtlDB.h"
|
|
|
|
#include "rocksdb/utilities/db_ttl.h"
|
2015-01-13 21:56:37 +00:00
|
|
|
#include "rocksjni/portal.h"
|
2014-11-17 18:22:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TtlDB
|
|
|
|
* Method: open
|
2016-01-20 17:05:41 +00:00
|
|
|
* Signature: (JLjava/lang/String;IZ)J
|
2014-11-17 18:22:44 +00:00
|
|
|
*/
|
2019-02-22 22:36:38 +00:00
|
|
|
jlong Java_org_rocksdb_TtlDB_open(
|
|
|
|
JNIEnv* env, jclass, jlong joptions_handle, jstring jdb_path, jint jttl,
|
|
|
|
jboolean jread_only) {
|
2017-02-28 00:26:12 +00:00
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (db_path == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* opt = reinterpret_cast<ROCKSDB_NAMESPACE::Options*>(joptions_handle);
|
|
|
|
ROCKSDB_NAMESPACE::DBWithTTL* db = nullptr;
|
|
|
|
ROCKSDB_NAMESPACE::Status s =
|
|
|
|
ROCKSDB_NAMESPACE::DBWithTTL::Open(*opt, db_path, &db, jttl, jread_only);
|
2014-11-17 18:22:44 +00:00
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
|
|
|
|
// as TTLDB extends RocksDB on the java side, we can reuse
|
|
|
|
// the RocksDB portal here.
|
|
|
|
if (s.ok()) {
|
2016-01-20 17:05:41 +00:00
|
|
|
return reinterpret_cast<jlong>(db);
|
|
|
|
} else {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
2016-01-20 17:05:41 +00:00
|
|
|
return 0;
|
2014-11-17 18:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-21 22:38:17 +00:00
|
|
|
|
2015-01-13 21:56:37 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TtlDB
|
2015-01-17 22:28:54 +00:00
|
|
|
* Method: openCF
|
2016-01-20 17:05:41 +00:00
|
|
|
* Signature: (JLjava/lang/String;[[B[J[IZ)[J
|
2015-01-13 21:56:37 +00:00
|
|
|
*/
|
2019-02-22 22:36:38 +00:00
|
|
|
jlongArray Java_org_rocksdb_TtlDB_openCF(
|
|
|
|
JNIEnv* env, jclass, jlong jopt_handle, jstring jdb_path,
|
|
|
|
jobjectArray jcolumn_names, jlongArray jcolumn_options,
|
|
|
|
jintArray jttls, jboolean jread_only) {
|
2017-02-28 00:26:12 +00:00
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (db_path == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-20 17:05:41 +00:00
|
|
|
|
2017-02-28 00:26:12 +00:00
|
|
|
const jsize len_cols = env->GetArrayLength(jcolumn_names);
|
|
|
|
jlong* jco = env->GetLongArrayElements(jcolumn_options, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (jco == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-20 17:05:41 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
std::vector<ROCKSDB_NAMESPACE::ColumnFamilyDescriptor> column_families;
|
2017-02-28 00:26:12 +00:00
|
|
|
jboolean has_exception = JNI_FALSE;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::JniUtil::byteStrings<std::string>(
|
2018-04-13 00:55:14 +00:00
|
|
|
env, jcolumn_names,
|
|
|
|
[](const char* str_data, const size_t str_len) {
|
|
|
|
return std::string(str_data, str_len);
|
|
|
|
},
|
|
|
|
[&jco, &column_families](size_t idx, std::string cf_name) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::ColumnFamilyOptions* cf_options =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyOptions*>(jco[idx]);
|
2018-04-13 00:55:14 +00:00
|
|
|
column_families.push_back(
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::ColumnFamilyDescriptor(cf_name, *cf_options));
|
2018-04-13 00:55:14 +00:00
|
|
|
},
|
|
|
|
&has_exception);
|
2016-01-20 17:05:41 +00:00
|
|
|
|
|
|
|
env->ReleaseLongArrayElements(jcolumn_options, jco, JNI_ABORT);
|
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
if (has_exception == JNI_TRUE) {
|
2017-06-05 18:23:31 +00:00
|
|
|
// exception occurred
|
2017-02-28 00:26:12 +00:00
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-20 17:05:41 +00:00
|
|
|
|
|
|
|
std::vector<int32_t> ttl_values;
|
2017-02-28 00:26:12 +00:00
|
|
|
jint* jttlv = env->GetIntArrayElements(jttls, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (jttlv == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const jsize len_ttls = env->GetArrayLength(jttls);
|
2018-04-13 00:55:14 +00:00
|
|
|
for (jsize i = 0; i < len_ttls; i++) {
|
2016-01-20 17:05:41 +00:00
|
|
|
ttl_values.push_back(jttlv[i]);
|
2015-01-13 21:56:37 +00:00
|
|
|
}
|
2016-01-20 17:05:41 +00:00
|
|
|
env->ReleaseIntArrayElements(jttls, jttlv, JNI_ABORT);
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* opt = reinterpret_cast<ROCKSDB_NAMESPACE::DBOptions*>(jopt_handle);
|
|
|
|
std::vector<ROCKSDB_NAMESPACE::ColumnFamilyHandle*> handles;
|
|
|
|
ROCKSDB_NAMESPACE::DBWithTTL* db = nullptr;
|
|
|
|
ROCKSDB_NAMESPACE::Status s = ROCKSDB_NAMESPACE::DBWithTTL::Open(
|
2018-04-13 00:55:14 +00:00
|
|
|
*opt, db_path, column_families, &handles, &db, ttl_values, jread_only);
|
2015-01-13 21:56:37 +00:00
|
|
|
|
2017-02-28 00:26:12 +00:00
|
|
|
// we have now finished with db_path
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
|
2015-01-13 21:56:37 +00:00
|
|
|
// check if open operation was successful
|
|
|
|
if (s.ok()) {
|
2018-04-13 00:55:14 +00:00
|
|
|
const jsize resultsLen = 1 + len_cols; // db handle + column family handles
|
2016-07-26 20:18:31 +00:00
|
|
|
std::unique_ptr<jlong[]> results =
|
|
|
|
std::unique_ptr<jlong[]>(new jlong[resultsLen]);
|
2016-01-20 17:05:41 +00:00
|
|
|
results[0] = reinterpret_cast<jlong>(db);
|
2018-04-13 00:55:14 +00:00
|
|
|
for (int i = 1; i <= len_cols; i++) {
|
2016-01-20 17:05:41 +00:00
|
|
|
results[i] = reinterpret_cast<jlong>(handles[i - 1]);
|
2015-01-13 21:56:37 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 17:05:41 +00:00
|
|
|
jlongArray jresults = env->NewLongArray(resultsLen);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (jresults == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-22 17:17:10 +00:00
|
|
|
env->SetLongArrayRegion(jresults, 0, resultsLen, results.get());
|
2018-04-13 00:55:14 +00:00
|
|
|
if (env->ExceptionCheck()) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
env->DeleteLocalRef(jresults);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:05:41 +00:00
|
|
|
return jresults;
|
|
|
|
} else {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
2016-01-20 17:05:41 +00:00
|
|
|
return NULL;
|
2015-01-13 21:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 22:36:38 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TtlDB
|
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_TtlDB_disposeInternal(
|
|
|
|
JNIEnv*, jobject, jlong jhandle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* ttl_db = reinterpret_cast<ROCKSDB_NAMESPACE::DBWithTTL*>(jhandle);
|
2019-02-22 22:36:38 +00:00
|
|
|
assert(ttl_db != nullptr);
|
|
|
|
delete ttl_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TtlDB
|
|
|
|
* Method: closeDatabase
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_TtlDB_closeDatabase(
|
|
|
|
JNIEnv* /* env */, jclass, jlong /* jhandle */) {
|
2020-02-20 20:07:53 +00:00
|
|
|
// auto* ttl_db = reinterpret_cast<ROCKSDB_NAMESPACE::DBWithTTL*>(jhandle);
|
|
|
|
// assert(ttl_db != nullptr);
|
|
|
|
// ROCKSDB_NAMESPACE::Status s = ttl_db->Close();
|
|
|
|
// ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
2019-02-22 22:36:38 +00:00
|
|
|
|
|
|
|
//TODO(AR) this is disabled until https://github.com/facebook/rocksdb/issues/4818 is resolved!
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:38:17 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TtlDB
|
|
|
|
* Method: createColumnFamilyWithTtl
|
2016-02-01 20:00:40 +00:00
|
|
|
* Signature: (JLorg/rocksdb/ColumnFamilyDescriptor;[BJI)J;
|
2014-11-21 22:38:17 +00:00
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_TtlDB_createColumnFamilyWithTtl(
|
2019-02-22 22:36:38 +00:00
|
|
|
JNIEnv* env, jobject, jlong jdb_handle, jbyteArray jcolumn_name,
|
2018-04-13 00:55:14 +00:00
|
|
|
jlong jcolumn_options, jint jttl) {
|
2017-02-28 00:26:12 +00:00
|
|
|
jbyte* cfname = env->GetByteArrayElements(jcolumn_name, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (cfname == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const jsize len = env->GetArrayLength(jcolumn_name);
|
2016-02-01 20:00:40 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* cfOptions = reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyOptions*>(
|
|
|
|
jcolumn_options);
|
2015-01-31 13:43:21 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* db_handle = reinterpret_cast<ROCKSDB_NAMESPACE::DBWithTTL*>(jdb_handle);
|
|
|
|
ROCKSDB_NAMESPACE::ColumnFamilyHandle* handle;
|
|
|
|
ROCKSDB_NAMESPACE::Status s = db_handle->CreateColumnFamilyWithTtl(
|
2018-04-13 00:55:14 +00:00
|
|
|
*cfOptions, std::string(reinterpret_cast<char*>(cfname), len), &handle,
|
|
|
|
jttl);
|
2017-02-28 00:26:12 +00:00
|
|
|
|
2016-02-01 20:00:40 +00:00
|
|
|
env->ReleaseByteArrayElements(jcolumn_name, cfname, 0);
|
2014-11-21 22:38:17 +00:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return reinterpret_cast<jlong>(handle);
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
2014-11-21 22:38:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|