2018-03-02 19:22:38 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
//
|
|
|
|
// This file implements the "bridge" between Java and C++
|
2020-02-20 21:07:53 +01:00
|
|
|
// for ROCKSDB_NAMESPACE::TransactionDB.
|
2018-03-02 19:22:38 +01:00
|
|
|
|
2022-03-01 18:02:15 +01:00
|
|
|
#include "rocksdb/utilities/transaction_db.h"
|
|
|
|
|
2018-03-02 19:22:38 +01:00
|
|
|
#include <jni.h>
|
2022-03-01 18:02:15 +01:00
|
|
|
|
2018-03-02 19:22:38 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_TransactionDB.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/utilities/transaction.h"
|
2022-03-01 18:02:15 +01:00
|
|
|
#include "rocksjni/cplusplus_to_java_convert.h"
|
2018-03-02 19:22:38 +01:00
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: open
|
|
|
|
* Signature: (JJLjava/lang/String;)J
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_TransactionDB_open__JJLjava_lang_String_2(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv* env, jclass, jlong joptions_handle,
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong jtxn_db_options_handle, jstring jdb_path) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* options =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::Options*>(joptions_handle);
|
2018-03-02 19:22:38 +01:00
|
|
|
auto* txn_db_options =
|
2020-02-20 21:07:53 +01:00
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDBOptions*>(
|
|
|
|
jtxn_db_options_handle);
|
|
|
|
ROCKSDB_NAMESPACE::TransactionDB* tdb = nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, nullptr);
|
|
|
|
if (db_path == nullptr) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::Status s = ROCKSDB_NAMESPACE::TransactionDB::Open(
|
|
|
|
*options, *txn_db_options, db_path, &tdb);
|
2018-03-02 19:22:38 +01:00
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
|
|
|
|
if (s.ok()) {
|
2022-03-01 18:02:15 +01:00
|
|
|
return GET_CPLUSPLUS_POINTER(tdb);
|
2018-03-02 19:22:38 +01:00
|
|
|
} else {
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
2018-03-02 19:22:38 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: open
|
|
|
|
* Signature: (JJLjava/lang/String;[[B[J)[J
|
|
|
|
*/
|
|
|
|
jlongArray Java_org_rocksdb_TransactionDB_open__JJLjava_lang_String_2_3_3B_3J(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv* env, jclass, jlong jdb_options_handle,
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong jtxn_db_options_handle, jstring jdb_path, jobjectArray jcolumn_names,
|
2018-03-02 19:22:38 +01:00
|
|
|
jlongArray jcolumn_options_handles) {
|
|
|
|
const char* db_path = env->GetStringUTFChars(jdb_path, nullptr);
|
|
|
|
if (db_path == nullptr) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsize len_cols = env->GetArrayLength(jcolumn_names);
|
|
|
|
if (env->EnsureLocalCapacity(len_cols) != 0) {
|
|
|
|
// out of memory
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
jlong* jco = env->GetLongArrayElements(jcolumn_options_handles, nullptr);
|
|
|
|
if (jco == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
std::vector<ROCKSDB_NAMESPACE::ColumnFamilyDescriptor> column_families;
|
2018-03-02 19:22:38 +01:00
|
|
|
for (int i = 0; i < len_cols; i++) {
|
|
|
|
const jobject jcn = env->GetObjectArrayElement(jcolumn_names, i);
|
|
|
|
if (env->ExceptionCheck()) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
env->ReleaseLongArrayElements(jcolumn_options_handles, jco, JNI_ABORT);
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
const jbyteArray jcn_ba = reinterpret_cast<jbyteArray>(jcn);
|
|
|
|
jbyte* jcf_name = env->GetByteArrayElements(jcn_ba, nullptr);
|
|
|
|
if (jcf_name == nullptr) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->DeleteLocalRef(jcn);
|
|
|
|
env->ReleaseLongArrayElements(jcolumn_options_handles, jco, JNI_ABORT);
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int jcf_name_len = env->GetArrayLength(jcn_ba);
|
|
|
|
if (env->EnsureLocalCapacity(jcf_name_len) != 0) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// out of memory
|
|
|
|
env->ReleaseByteArrayElements(jcn_ba, jcf_name, JNI_ABORT);
|
|
|
|
env->DeleteLocalRef(jcn);
|
|
|
|
env->ReleaseLongArrayElements(jcolumn_options_handles, jco, JNI_ABORT);
|
|
|
|
env->ReleaseStringUTFChars(jdb_path, db_path);
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
const std::string cf_name(reinterpret_cast<char*>(jcf_name), jcf_name_len);
|
2020-02-20 21:07:53 +01:00
|
|
|
const ROCKSDB_NAMESPACE::ColumnFamilyOptions* cf_options =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyOptions*>(jco[i]);
|
2018-03-02 19:22:38 +01:00
|
|
|
column_families.push_back(
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::ColumnFamilyDescriptor(cf_name, *cf_options));
|
2018-03-02 19:22:38 +01:00
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jcn_ba, jcf_name, JNI_ABORT);
|
|
|
|
env->DeleteLocalRef(jcn);
|
|
|
|
}
|
|
|
|
env->ReleaseLongArrayElements(jcolumn_options_handles, jco, JNI_ABORT);
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* db_options =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::DBOptions*>(jdb_options_handle);
|
2018-03-02 19:22:38 +01:00
|
|
|
auto* txn_db_options =
|
2020-02-20 21:07:53 +01:00
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDBOptions*>(
|
|
|
|
jtxn_db_options_handle);
|
|
|
|
std::vector<ROCKSDB_NAMESPACE::ColumnFamilyHandle*> handles;
|
|
|
|
ROCKSDB_NAMESPACE::TransactionDB* tdb = nullptr;
|
|
|
|
const ROCKSDB_NAMESPACE::Status s = ROCKSDB_NAMESPACE::TransactionDB::Open(
|
2018-04-13 02:55:14 +02:00
|
|
|
*db_options, *txn_db_options, db_path, column_families, &handles, &tdb);
|
2018-03-02 19:22:38 +01:00
|
|
|
|
|
|
|
// check if open operation was successful
|
|
|
|
if (s.ok()) {
|
|
|
|
const jsize resultsLen = 1 + len_cols; // db handle + column family handles
|
|
|
|
std::unique_ptr<jlong[]> results =
|
|
|
|
std::unique_ptr<jlong[]>(new jlong[resultsLen]);
|
2022-03-01 18:02:15 +01:00
|
|
|
results[0] = GET_CPLUSPLUS_POINTER(tdb);
|
2018-03-02 19:22:38 +01:00
|
|
|
for (int i = 1; i <= len_cols; i++) {
|
2022-03-01 18:02:15 +01:00
|
|
|
results[i] = GET_CPLUSPLUS_POINTER(handles[i - 1]);
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jlongArray jresults = env->NewLongArray(resultsLen);
|
|
|
|
if (jresults == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
env->SetLongArrayRegion(jresults, 0, resultsLen, results.get());
|
|
|
|
if (env->ExceptionCheck()) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
env->DeleteLocalRef(jresults);
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
return jresults;
|
|
|
|
} else {
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
2018-03-02 19:22:38 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:36:38 +01:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_TransactionDB_disposeInternal(
|
|
|
|
JNIEnv*, jobject, jlong jhandle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2019-02-22 23:36:38 +01:00
|
|
|
assert(txn_db != nullptr);
|
|
|
|
delete txn_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: closeDatabase
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_TransactionDB_closeDatabase(
|
|
|
|
JNIEnv* env, jclass, jlong jhandle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2019-02-22 23:36:38 +01:00
|
|
|
assert(txn_db != nullptr);
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::Status s = txn_db->Close();
|
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
2019-02-22 23:36:38 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 19:22:38 +01:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: beginTransaction
|
|
|
|
* Signature: (JJ)J
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_TransactionDB_beginTransaction__JJ(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv*, jobject, jlong jhandle, jlong jwrite_options_handle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2018-03-02 19:22:38 +01:00
|
|
|
auto* write_options =
|
2020-02-20 21:07:53 +01:00
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::WriteOptions*>(jwrite_options_handle);
|
|
|
|
ROCKSDB_NAMESPACE::Transaction* txn =
|
|
|
|
txn_db->BeginTransaction(*write_options);
|
2022-03-01 18:02:15 +01:00
|
|
|
return GET_CPLUSPLUS_POINTER(txn);
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: beginTransaction
|
|
|
|
* Signature: (JJJ)J
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_TransactionDB_beginTransaction__JJJ(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv*, jobject, jlong jhandle, jlong jwrite_options_handle,
|
|
|
|
jlong jtxn_options_handle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2018-03-02 19:22:38 +01:00
|
|
|
auto* write_options =
|
2020-02-20 21:07:53 +01:00
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::WriteOptions*>(jwrite_options_handle);
|
|
|
|
auto* txn_options = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionOptions*>(
|
|
|
|
jtxn_options_handle);
|
|
|
|
ROCKSDB_NAMESPACE::Transaction* txn =
|
2018-03-02 19:22:38 +01:00
|
|
|
txn_db->BeginTransaction(*write_options, *txn_options);
|
2022-03-01 18:02:15 +01:00
|
|
|
return GET_CPLUSPLUS_POINTER(txn);
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: beginTransaction_withOld
|
|
|
|
* Signature: (JJJ)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_TransactionDB_beginTransaction_1withOld__JJJ(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv*, jobject, jlong jhandle, jlong jwrite_options_handle,
|
|
|
|
jlong jold_txn_handle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2018-03-02 19:22:38 +01:00
|
|
|
auto* write_options =
|
2020-02-20 21:07:53 +01:00
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::WriteOptions*>(jwrite_options_handle);
|
|
|
|
auto* old_txn =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::Transaction*>(jold_txn_handle);
|
|
|
|
ROCKSDB_NAMESPACE::TransactionOptions txn_options;
|
|
|
|
ROCKSDB_NAMESPACE::Transaction* txn =
|
2018-03-02 19:22:38 +01:00
|
|
|
txn_db->BeginTransaction(*write_options, txn_options, old_txn);
|
|
|
|
|
|
|
|
// RocksJava relies on the assumption that
|
|
|
|
// we do not allocate a new Transaction object
|
|
|
|
// when providing an old_txn
|
|
|
|
assert(txn == old_txn);
|
|
|
|
|
2022-03-01 18:02:15 +01:00
|
|
|
return GET_CPLUSPLUS_POINTER(txn);
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: beginTransaction_withOld
|
|
|
|
* Signature: (JJJJ)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_TransactionDB_beginTransaction_1withOld__JJJJ(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv*, jobject, jlong jhandle, jlong jwrite_options_handle,
|
|
|
|
jlong jtxn_options_handle, jlong jold_txn_handle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2018-03-02 19:22:38 +01:00
|
|
|
auto* write_options =
|
2020-02-20 21:07:53 +01:00
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::WriteOptions*>(jwrite_options_handle);
|
|
|
|
auto* txn_options = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionOptions*>(
|
|
|
|
jtxn_options_handle);
|
|
|
|
auto* old_txn =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::Transaction*>(jold_txn_handle);
|
|
|
|
ROCKSDB_NAMESPACE::Transaction* txn =
|
2018-04-13 02:55:14 +02:00
|
|
|
txn_db->BeginTransaction(*write_options, *txn_options, old_txn);
|
2018-03-02 19:22:38 +01:00
|
|
|
|
|
|
|
// RocksJava relies on the assumption that
|
|
|
|
// we do not allocate a new Transaction object
|
|
|
|
// when providing an old_txn
|
|
|
|
assert(txn == old_txn);
|
|
|
|
|
2022-03-01 18:02:15 +01:00
|
|
|
return GET_CPLUSPLUS_POINTER(txn);
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: getTransactionByName
|
|
|
|
* Signature: (JLjava/lang/String;)J
|
|
|
|
*/
|
2019-02-22 23:36:38 +01:00
|
|
|
jlong Java_org_rocksdb_TransactionDB_getTransactionByName(
|
|
|
|
JNIEnv* env, jobject, jlong jhandle, jstring jname) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2018-03-02 19:22:38 +01:00
|
|
|
const char* name = env->GetStringUTFChars(jname, nullptr);
|
|
|
|
if (name == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return 0;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::Transaction* txn = txn_db->GetTransactionByName(name);
|
2018-03-02 19:22:38 +01:00
|
|
|
env->ReleaseStringUTFChars(jname, name);
|
2022-03-01 18:02:15 +01:00
|
|
|
return GET_CPLUSPLUS_POINTER(txn);
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: getAllPreparedTransactions
|
|
|
|
* Signature: (J)[J
|
|
|
|
*/
|
|
|
|
jlongArray Java_org_rocksdb_TransactionDB_getAllPreparedTransactions(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv* env, jobject, jlong jhandle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
|
|
|
std::vector<ROCKSDB_NAMESPACE::Transaction*> txns;
|
2018-03-02 19:22:38 +01:00
|
|
|
txn_db->GetAllPreparedTransactions(&txns);
|
|
|
|
|
|
|
|
const size_t size = txns.size();
|
|
|
|
assert(size < UINT32_MAX); // does it fit in a jint?
|
|
|
|
|
|
|
|
const jsize len = static_cast<jsize>(size);
|
2018-07-18 21:24:07 +02:00
|
|
|
std::vector<jlong> tmp(len);
|
2018-03-02 19:22:38 +01:00
|
|
|
for (jsize i = 0; i < len; ++i) {
|
2022-03-01 18:02:15 +01:00
|
|
|
tmp[i] = GET_CPLUSPLUS_POINTER(txns[i]);
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jlongArray jtxns = env->NewLongArray(len);
|
|
|
|
if (jtxns == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
2018-07-18 21:24:07 +02:00
|
|
|
env->SetLongArrayRegion(jtxns, 0, len, tmp.data());
|
2018-03-02 19:22:38 +01:00
|
|
|
if (env->ExceptionCheck()) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
env->DeleteLocalRef(jtxns);
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return jtxns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: getLockStatusData
|
|
|
|
* Signature: (J)Ljava/util/Map;
|
|
|
|
*/
|
2019-02-22 23:36:38 +01:00
|
|
|
jobject Java_org_rocksdb_TransactionDB_getLockStatusData(
|
|
|
|
JNIEnv* env, jobject, jlong jhandle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
|
|
|
const std::unordered_multimap<uint32_t, ROCKSDB_NAMESPACE::KeyLockInfo>
|
2018-04-13 02:55:14 +02:00
|
|
|
lock_status_data = txn_db->GetLockStatusData();
|
2020-02-20 21:07:53 +01:00
|
|
|
const jobject jlock_status_data = ROCKSDB_NAMESPACE::HashMapJni::construct(
|
2018-04-13 02:55:14 +02:00
|
|
|
env, static_cast<uint32_t>(lock_status_data.size()));
|
2018-03-02 19:22:38 +01:00
|
|
|
if (jlock_status_data == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception occurred
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
const ROCKSDB_NAMESPACE::HashMapJni::FnMapKV<
|
|
|
|
const int32_t, const ROCKSDB_NAMESPACE::KeyLockInfo, jobject, jobject>
|
2018-04-13 02:55:14 +02:00
|
|
|
fn_map_kv =
|
2020-02-20 21:07:53 +01:00
|
|
|
[env](const std::pair<const int32_t,
|
|
|
|
const ROCKSDB_NAMESPACE::KeyLockInfo>& pair) {
|
2018-04-13 02:55:14 +02:00
|
|
|
const jobject jlong_column_family_id =
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::LongJni::valueOf(env, pair.first);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jlong_column_family_id == nullptr) {
|
2018-03-02 19:22:38 +01:00
|
|
|
// an error occurred
|
|
|
|
return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
|
2018-04-13 02:55:14 +02:00
|
|
|
}
|
|
|
|
const jobject jkey_lock_info =
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::KeyLockInfoJni::construct(env, pair.second);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jkey_lock_info == nullptr) {
|
|
|
|
// an error occurred
|
|
|
|
return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
|
|
|
|
}
|
|
|
|
return std::unique_ptr<std::pair<jobject, jobject>>(
|
|
|
|
new std::pair<jobject, jobject>(jlong_column_family_id,
|
|
|
|
jkey_lock_info));
|
|
|
|
};
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
if (!ROCKSDB_NAMESPACE::HashMapJni::putAll(
|
|
|
|
env, jlock_status_data, lock_status_data.begin(),
|
|
|
|
lock_status_data.end(), fn_map_kv)) {
|
2018-03-02 19:22:38 +01:00
|
|
|
// exception occcurred
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return jlock_status_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-04-13 02:55:14 +02:00
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: getDeadlockInfoBuffer
|
|
|
|
* Signature: (J)[Lorg/rocksdb/TransactionDB/DeadlockPath;
|
|
|
|
*/
|
2018-03-02 19:22:38 +01:00
|
|
|
jobjectArray Java_org_rocksdb_TransactionDB_getDeadlockInfoBuffer(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jhandle) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
|
|
|
const std::vector<ROCKSDB_NAMESPACE::DeadlockPath> deadlock_info_buffer =
|
2018-03-02 19:22:38 +01:00
|
|
|
txn_db->GetDeadlockInfoBuffer();
|
|
|
|
|
|
|
|
const jsize deadlock_info_buffer_len =
|
|
|
|
static_cast<jsize>(deadlock_info_buffer.size());
|
2020-02-20 21:07:53 +01:00
|
|
|
jobjectArray jdeadlock_info_buffer = env->NewObjectArray(
|
|
|
|
deadlock_info_buffer_len,
|
|
|
|
ROCKSDB_NAMESPACE::DeadlockPathJni::getJClass(env), nullptr);
|
2018-03-02 19:22:38 +01:00
|
|
|
if (jdeadlock_info_buffer == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
jsize jdeadlock_info_buffer_offset = 0;
|
|
|
|
|
|
|
|
auto buf_end = deadlock_info_buffer.end();
|
2018-04-13 02:55:14 +02:00
|
|
|
for (auto buf_it = deadlock_info_buffer.begin(); buf_it != buf_end;
|
|
|
|
++buf_it) {
|
2020-02-20 21:07:53 +01:00
|
|
|
const ROCKSDB_NAMESPACE::DeadlockPath deadlock_path = *buf_it;
|
|
|
|
const std::vector<ROCKSDB_NAMESPACE::DeadlockInfo> deadlock_infos =
|
2018-04-13 02:55:14 +02:00
|
|
|
deadlock_path.path;
|
2018-03-02 19:22:38 +01:00
|
|
|
const jsize deadlock_infos_len =
|
|
|
|
static_cast<jsize>(deadlock_info_buffer.size());
|
2018-04-13 02:55:14 +02:00
|
|
|
jobjectArray jdeadlock_infos = env->NewObjectArray(
|
2020-02-20 21:07:53 +01:00
|
|
|
deadlock_infos_len, ROCKSDB_NAMESPACE::DeadlockInfoJni::getJClass(env),
|
|
|
|
nullptr);
|
2018-03-02 19:22:38 +01:00
|
|
|
if (jdeadlock_infos == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->DeleteLocalRef(jdeadlock_info_buffer);
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
jsize jdeadlock_infos_offset = 0;
|
|
|
|
|
|
|
|
auto infos_end = deadlock_infos.end();
|
2018-04-13 02:55:14 +02:00
|
|
|
for (auto infos_it = deadlock_infos.begin(); infos_it != infos_end;
|
|
|
|
++infos_it) {
|
2020-02-20 21:07:53 +01:00
|
|
|
const ROCKSDB_NAMESPACE::DeadlockInfo deadlock_info = *infos_it;
|
|
|
|
const jobject jdeadlock_info =
|
|
|
|
ROCKSDB_NAMESPACE::TransactionDBJni::newDeadlockInfo(
|
|
|
|
env, jobj, deadlock_info.m_txn_id, deadlock_info.m_cf_id,
|
|
|
|
deadlock_info.m_waiting_key, deadlock_info.m_exclusive);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jdeadlock_info == nullptr) {
|
2018-03-02 19:22:38 +01:00
|
|
|
// exception occcurred
|
|
|
|
env->DeleteLocalRef(jdeadlock_info_buffer);
|
|
|
|
return nullptr;
|
2018-04-13 02:55:14 +02:00
|
|
|
}
|
|
|
|
env->SetObjectArrayElement(jdeadlock_infos, jdeadlock_infos_offset++,
|
|
|
|
jdeadlock_info);
|
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException or
|
|
|
|
// ArrayStoreException
|
|
|
|
env->DeleteLocalRef(jdeadlock_info);
|
2018-03-02 19:22:38 +01:00
|
|
|
env->DeleteLocalRef(jdeadlock_info_buffer);
|
|
|
|
return nullptr;
|
2018-04-13 02:55:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
const jobject jdeadlock_path =
|
|
|
|
ROCKSDB_NAMESPACE::DeadlockPathJni::construct(
|
|
|
|
env, jdeadlock_infos, deadlock_path.limit_exceeded);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (jdeadlock_path == nullptr) {
|
|
|
|
// exception occcurred
|
|
|
|
env->DeleteLocalRef(jdeadlock_info_buffer);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
env->SetObjectArrayElement(jdeadlock_info_buffer,
|
|
|
|
jdeadlock_info_buffer_offset++, jdeadlock_path);
|
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException or ArrayStoreException
|
|
|
|
env->DeleteLocalRef(jdeadlock_path);
|
|
|
|
env->DeleteLocalRef(jdeadlock_info_buffer);
|
|
|
|
return nullptr;
|
2018-03-02 19:22:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jdeadlock_info_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-04-13 02:55:14 +02:00
|
|
|
* Class: org_rocksdb_TransactionDB
|
|
|
|
* Method: setDeadlockInfoBufferSize
|
|
|
|
* Signature: (JI)V
|
|
|
|
*/
|
2018-03-02 19:22:38 +01:00
|
|
|
void Java_org_rocksdb_TransactionDB_setDeadlockInfoBufferSize(
|
2019-02-22 23:36:38 +01:00
|
|
|
JNIEnv*, jobject, jlong jhandle, jint jdeadlock_info_buffer_size) {
|
2020-02-20 21:07:53 +01:00
|
|
|
auto* txn_db = reinterpret_cast<ROCKSDB_NAMESPACE::TransactionDB*>(jhandle);
|
2018-03-02 19:22:38 +01:00
|
|
|
txn_db->SetDeadlockInfoBufferSize(jdeadlock_info_buffer_size);
|
|
|
|
}
|