2018-04-07 06:22:37 +02: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++ and enables
|
|
|
|
// calling C++ rocksdb::SstFileManager methods
|
|
|
|
// from Java side.
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_SstFileManager.h"
|
|
|
|
#include "rocksdb/sst_file_manager.h"
|
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: newSstFileManager
|
|
|
|
* Signature: (JJJDJ)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_SstFileManager_newSstFileManager(
|
2018-04-13 02:55:14 +02:00
|
|
|
JNIEnv* jnienv, jclass /*jcls*/, jlong jenv_handle, jlong jlogger_handle,
|
2018-04-07 06:22:37 +02:00
|
|
|
jlong jrate_bytes, jdouble jmax_trash_db_ratio,
|
|
|
|
jlong jmax_delete_chunk_bytes) {
|
|
|
|
auto* env = reinterpret_cast<rocksdb::Env*>(jenv_handle);
|
|
|
|
rocksdb::Status s;
|
|
|
|
rocksdb::SstFileManager* sst_file_manager = nullptr;
|
|
|
|
|
|
|
|
if (jlogger_handle != 0) {
|
|
|
|
auto* sptr_logger =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::Logger>*>(jlogger_handle);
|
|
|
|
sst_file_manager = rocksdb::NewSstFileManager(
|
|
|
|
env, *sptr_logger, "", jrate_bytes, true, &s, jmax_trash_db_ratio,
|
2018-04-07 06:22:37 +02:00
|
|
|
jmax_delete_chunk_bytes);
|
|
|
|
} else {
|
2018-04-13 02:55:14 +02:00
|
|
|
sst_file_manager = rocksdb::NewSstFileManager(env, nullptr, "", jrate_bytes,
|
|
|
|
true, &s, jmax_trash_db_ratio,
|
|
|
|
jmax_delete_chunk_bytes);
|
2018-04-07 06:22:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
if (sst_file_manager != nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
delete sst_file_manager;
|
2018-04-07 06:22:37 +02:00
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(jnienv, s);
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
|
|
|
new std::shared_ptr<rocksdb::SstFileManager>(sst_file_manager);
|
2018-04-07 06:22:37 +02:00
|
|
|
|
|
|
|
return reinterpret_cast<jlong>(sptr_sst_file_manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: setMaxAllowedSpaceUsage
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_SstFileManager_setMaxAllowedSpaceUsage(
|
2018-04-13 02:55:14 +02:00
|
|
|
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle,
|
|
|
|
jlong jmax_allowed_space) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
sptr_sst_file_manager->get()->SetMaxAllowedSpaceUsage(jmax_allowed_space);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: setCompactionBufferSize
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_SstFileManager_setCompactionBufferSize(
|
2018-04-13 02:55:14 +02:00
|
|
|
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle,
|
|
|
|
jlong jcompaction_buffer_size) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
|
|
|
sptr_sst_file_manager->get()->SetCompactionBufferSize(
|
|
|
|
jcompaction_buffer_size);
|
2018-04-07 06:22:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: isMaxAllowedSpaceReached
|
|
|
|
* Signature: (J)Z
|
|
|
|
*/
|
|
|
|
jboolean Java_org_rocksdb_SstFileManager_isMaxAllowedSpaceReached(
|
2018-04-13 02:55:14 +02:00
|
|
|
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
return sptr_sst_file_manager->get()->IsMaxAllowedSpaceReached();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: isMaxAllowedSpaceReachedIncludingCompactions
|
|
|
|
* Signature: (J)Z
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jboolean
|
|
|
|
Java_org_rocksdb_SstFileManager_isMaxAllowedSpaceReachedIncludingCompactions(
|
|
|
|
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
|
|
|
return sptr_sst_file_manager->get()
|
|
|
|
->IsMaxAllowedSpaceReachedIncludingCompactions();
|
2018-04-07 06:22:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: getTotalSize
|
|
|
|
* Signature: (J)J
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_SstFileManager_getTotalSize(JNIEnv* /*env*/,
|
|
|
|
jobject /*jobj*/,
|
|
|
|
jlong jhandle) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
return sptr_sst_file_manager->get()->GetTotalSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: getTrackedFiles
|
|
|
|
* Signature: (J)Ljava/util/Map;
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jobject Java_org_rocksdb_SstFileManager_getTrackedFiles(JNIEnv* env,
|
|
|
|
jobject /*jobj*/,
|
|
|
|
jlong jhandle) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
auto tracked_files = sptr_sst_file_manager->get()->GetTrackedFiles();
|
2018-04-13 02:55:14 +02:00
|
|
|
|
|
|
|
const jobject jtracked_files = rocksdb::HashMapJni::construct(
|
|
|
|
env, static_cast<uint32_t>(tracked_files.size()));
|
2018-04-07 06:22:37 +02:00
|
|
|
if (jtracked_files == nullptr) {
|
2018-04-13 02:55:14 +02:00
|
|
|
// exception occurred
|
|
|
|
return nullptr;
|
2018-04-07 06:22:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
const rocksdb::HashMapJni::FnMapKV<const std::string, const uint64_t>
|
|
|
|
fn_map_kv =
|
2018-07-10 00:17:38 +02:00
|
|
|
[env](const std::pair<const std::string, const uint64_t>& pair) {
|
2018-04-13 02:55:14 +02:00
|
|
|
const jstring jtracked_file_path =
|
|
|
|
env->NewStringUTF(pair.first.c_str());
|
|
|
|
if (jtracked_file_path == nullptr) {
|
2018-04-07 06:22:37 +02:00
|
|
|
// an error occurred
|
|
|
|
return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
|
2018-04-13 02:55:14 +02:00
|
|
|
}
|
|
|
|
const jobject jtracked_file_size =
|
|
|
|
rocksdb::LongJni::valueOf(env, pair.second);
|
|
|
|
if (jtracked_file_size == 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>(jtracked_file_path,
|
|
|
|
jtracked_file_size));
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!rocksdb::HashMapJni::putAll(env, jtracked_files, tracked_files.begin(),
|
|
|
|
tracked_files.end(), fn_map_kv)) {
|
2018-04-07 06:22:37 +02:00
|
|
|
// exception occcurred
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return jtracked_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: getDeleteRateBytesPerSecond
|
|
|
|
* Signature: (J)J
|
|
|
|
*/
|
|
|
|
jlong Java_org_rocksdb_SstFileManager_getDeleteRateBytesPerSecond(
|
2018-04-13 02:55:14 +02:00
|
|
|
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
return sptr_sst_file_manager->get()->GetDeleteRateBytesPerSecond();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: setDeleteRateBytesPerSecond
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_SstFileManager_setDeleteRateBytesPerSecond(
|
2018-04-13 02:55:14 +02:00
|
|
|
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle, jlong jdelete_rate) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
sptr_sst_file_manager->get()->SetDeleteRateBytesPerSecond(jdelete_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: getMaxTrashDBRatio
|
|
|
|
* Signature: (J)D
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jdouble Java_org_rocksdb_SstFileManager_getMaxTrashDBRatio(JNIEnv* /*env*/,
|
|
|
|
jobject /*jobj*/,
|
|
|
|
jlong jhandle) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
return sptr_sst_file_manager->get()->GetMaxTrashDBRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: setMaxTrashDBRatio
|
|
|
|
* Signature: (JD)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileManager_setMaxTrashDBRatio(JNIEnv* /*env*/,
|
|
|
|
jobject /*jobj*/,
|
|
|
|
jlong jhandle,
|
|
|
|
jdouble jratio) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
sptr_sst_file_manager->get()->SetMaxTrashDBRatio(jratio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileManager
|
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileManager_disposeInternal(JNIEnv* /*env*/,
|
|
|
|
jobject /*jobj*/,
|
|
|
|
jlong jhandle) {
|
2018-04-07 06:22:37 +02:00
|
|
|
auto* sptr_sst_file_manager =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<std::shared_ptr<rocksdb::SstFileManager>*>(jhandle);
|
2018-04-07 06:22:37 +02:00
|
|
|
delete sptr_sst_file_manager;
|
|
|
|
}
|