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).
|
2015-08-04 23:07:17 +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::BackupEngine methods from the Java side.
|
2015-08-04 23:07:17 +00:00
|
|
|
|
|
|
|
#include <jni.h>
|
2022-01-27 23:44:23 +00:00
|
|
|
|
2015-08-04 23:07:17 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_BackupEngine.h"
|
2022-01-27 23:44:23 +00:00
|
|
|
#include "rocksdb/utilities/backup_engine.h"
|
2022-03-01 17:02:15 +00:00
|
|
|
#include "rocksjni/cplusplus_to_java_convert.h"
|
2015-08-04 23:07:17 +00:00
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: open
|
2016-01-20 17:05:41 +00:00
|
|
|
* Signature: (JJ)J
|
2015-08-04 23:07:17 +00:00
|
|
|
*/
|
2018-04-13 00:55:14 +00:00
|
|
|
jlong Java_org_rocksdb_BackupEngine_open(JNIEnv* env, jclass /*jcls*/,
|
|
|
|
jlong env_handle,
|
2022-01-27 23:44:23 +00:00
|
|
|
jlong backup_engine_options_handle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* rocks_env = reinterpret_cast<ROCKSDB_NAMESPACE::Env*>(env_handle);
|
2022-01-27 23:44:23 +00:00
|
|
|
auto* backup_engine_options =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngineOptions*>(
|
|
|
|
backup_engine_options_handle);
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::BackupEngine* backup_engine;
|
|
|
|
auto status = ROCKSDB_NAMESPACE::BackupEngine::Open(
|
2022-01-27 23:44:23 +00:00
|
|
|
rocks_env, *backup_engine_options, &backup_engine);
|
2015-08-04 23:07:17 +00:00
|
|
|
|
|
|
|
if (status.ok()) {
|
2022-03-01 17:02:15 +00:00
|
|
|
return GET_CPLUSPLUS_POINTER(backup_engine);
|
2016-01-20 17:05:41 +00:00
|
|
|
} else {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2016-01-20 17:05:41 +00:00
|
|
|
return 0;
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: createNewBackup
|
|
|
|
* Signature: (JJZ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_BackupEngine_createNewBackup(
|
2024-01-25 20:36:30 +00:00
|
|
|
JNIEnv* env, jclass /*jbe*/, jlong jbe_handle, jlong db_handle,
|
2015-08-04 23:07:17 +00:00
|
|
|
jboolean jflush_before_backup) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(db_handle);
|
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
2018-04-13 00:55:14 +00:00
|
|
|
auto status = backup_engine->CreateNewBackup(
|
|
|
|
db, static_cast<bool>(jflush_before_backup));
|
2015-08-04 23:07:17 +00:00
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2018-07-11 22:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: createNewBackupWithMetadata
|
|
|
|
* Signature: (JJLjava/lang/String;Z)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_BackupEngine_createNewBackupWithMetadata(
|
2024-01-25 20:36:30 +00:00
|
|
|
JNIEnv* env, jclass /*jbe*/, jlong jbe_handle, jlong db_handle,
|
2018-07-11 22:44:52 +00:00
|
|
|
jstring japp_metadata, jboolean jflush_before_backup) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(db_handle);
|
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
2018-07-11 22:44:52 +00:00
|
|
|
|
|
|
|
jboolean has_exception = JNI_FALSE;
|
2020-02-20 20:07:53 +00:00
|
|
|
std::string app_metadata = ROCKSDB_NAMESPACE::JniUtil::copyStdString(
|
|
|
|
env, japp_metadata, &has_exception);
|
2018-07-11 22:44:52 +00:00
|
|
|
if (has_exception == JNI_TRUE) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(
|
2018-07-14 05:40:23 +00:00
|
|
|
env, "Could not copy jstring to std::string");
|
2018-07-11 22:44:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto status = backup_engine->CreateNewBackupWithMetadata(
|
|
|
|
db, app_metadata, static_cast<bool>(jflush_before_backup));
|
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: getBackupInfo
|
|
|
|
* Signature: (J)Ljava/util/List;
|
|
|
|
*/
|
2018-04-13 00:55:14 +00:00
|
|
|
jobject Java_org_rocksdb_BackupEngine_getBackupInfo(JNIEnv* env,
|
2024-01-25 20:36:30 +00:00
|
|
|
jclass /*jcls*/,
|
2018-04-13 00:55:14 +00:00
|
|
|
jlong jbe_handle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
|
|
|
std::vector<ROCKSDB_NAMESPACE::BackupInfo> backup_infos;
|
2015-08-04 23:07:17 +00:00
|
|
|
backup_engine->GetBackupInfo(&backup_infos);
|
2020-02-20 20:07:53 +00:00
|
|
|
return ROCKSDB_NAMESPACE::BackupInfoListJni::getBackupInfo(env, backup_infos);
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: getCorruptedBackups
|
|
|
|
* Signature: (J)[I
|
|
|
|
*/
|
2018-04-13 00:55:14 +00:00
|
|
|
jintArray Java_org_rocksdb_BackupEngine_getCorruptedBackups(JNIEnv* env,
|
2024-01-25 20:36:30 +00:00
|
|
|
jclass /*jcls*/,
|
2018-04-13 00:55:14 +00:00
|
|
|
jlong jbe_handle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
|
|
|
std::vector<ROCKSDB_NAMESPACE::BackupID> backup_ids;
|
2015-08-04 23:07:17 +00:00
|
|
|
backup_engine->GetCorruptedBackups(&backup_ids);
|
|
|
|
// store backupids in int array
|
2016-01-28 14:44:31 +00:00
|
|
|
std::vector<jint> int_backup_ids(backup_ids.begin(), backup_ids.end());
|
2018-04-13 00:55:14 +00:00
|
|
|
|
2015-08-04 23:07:17 +00:00
|
|
|
// Store ints in java array
|
|
|
|
// Its ok to loose precision here (64->32)
|
2016-01-28 14:44:31 +00:00
|
|
|
jsize ret_backup_ids_size = static_cast<jsize>(backup_ids.size());
|
2017-02-28 00:26:12 +00:00
|
|
|
jintArray ret_backup_ids = env->NewIntArray(ret_backup_ids_size);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (ret_backup_ids == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-04 23:07:17 +00:00
|
|
|
env->SetIntArrayRegion(ret_backup_ids, 0, ret_backup_ids_size,
|
2018-04-13 00:55:14 +00:00
|
|
|
int_backup_ids.data());
|
2015-08-04 23:07:17 +00:00
|
|
|
return ret_backup_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: garbageCollect
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
2024-01-25 20:36:30 +00:00
|
|
|
void Java_org_rocksdb_BackupEngine_garbageCollect(JNIEnv* env, jclass /*jbe*/,
|
2018-04-13 00:55:14 +00:00
|
|
|
jlong jbe_handle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
2015-08-04 23:07:17 +00:00
|
|
|
auto status = backup_engine->GarbageCollect();
|
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: purgeOldBackups
|
|
|
|
* Signature: (JI)V
|
|
|
|
*/
|
2024-01-25 20:36:30 +00:00
|
|
|
void Java_org_rocksdb_BackupEngine_purgeOldBackups(JNIEnv* env, jclass /*jbe*/,
|
2018-04-13 00:55:14 +00:00
|
|
|
jlong jbe_handle,
|
|
|
|
jint jnum_backups_to_keep) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
2018-04-13 00:55:14 +00:00
|
|
|
auto status = backup_engine->PurgeOldBackups(
|
|
|
|
static_cast<uint32_t>(jnum_backups_to_keep));
|
2015-08-04 23:07:17 +00:00
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: deleteBackup
|
|
|
|
* Signature: (JI)V
|
|
|
|
*/
|
2024-01-25 20:36:30 +00:00
|
|
|
void Java_org_rocksdb_BackupEngine_deleteBackup(JNIEnv* env, jclass /*jbe*/,
|
2018-04-13 00:55:14 +00:00
|
|
|
jlong jbe_handle,
|
|
|
|
jint jbackup_id) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
|
|
|
auto status = backup_engine->DeleteBackup(
|
|
|
|
static_cast<ROCKSDB_NAMESPACE::BackupID>(jbackup_id));
|
2015-08-04 23:07:17 +00:00
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: restoreDbFromBackup
|
|
|
|
* Signature: (JILjava/lang/String;Ljava/lang/String;J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_BackupEngine_restoreDbFromBackup(
|
2024-01-25 20:36:30 +00:00
|
|
|
JNIEnv* env, jclass /*jbe*/, jlong jbe_handle, jint jbackup_id,
|
2015-08-04 23:07:17 +00:00
|
|
|
jstring jdb_dir, jstring jwal_dir, jlong jrestore_options_handle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
2017-02-28 00:26:12 +00:00
|
|
|
const char* db_dir = env->GetStringUTFChars(jdb_dir, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (db_dir == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char* wal_dir = env->GetStringUTFChars(jwal_dir, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (wal_dir == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->ReleaseStringUTFChars(jdb_dir, db_dir);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* restore_options = reinterpret_cast<ROCKSDB_NAMESPACE::RestoreOptions*>(
|
|
|
|
jrestore_options_handle);
|
2018-04-13 00:55:14 +00:00
|
|
|
auto status = backup_engine->RestoreDBFromBackup(
|
2020-02-20 20:07:53 +00:00
|
|
|
static_cast<ROCKSDB_NAMESPACE::BackupID>(jbackup_id), db_dir, wal_dir,
|
2018-04-13 00:55:14 +00:00
|
|
|
*restore_options);
|
2017-02-28 00:26:12 +00:00
|
|
|
|
2015-08-04 23:07:17 +00:00
|
|
|
env->ReleaseStringUTFChars(jwal_dir, wal_dir);
|
|
|
|
env->ReleaseStringUTFChars(jdb_dir, db_dir);
|
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: restoreDbFromLatestBackup
|
|
|
|
* Signature: (JLjava/lang/String;Ljava/lang/String;J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_BackupEngine_restoreDbFromLatestBackup(
|
2024-01-25 20:36:30 +00:00
|
|
|
JNIEnv* env, jclass /*jbe*/, jlong jbe_handle, jstring jdb_dir,
|
2015-08-04 23:07:17 +00:00
|
|
|
jstring jwal_dir, jlong jrestore_options_handle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* backup_engine =
|
|
|
|
reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
2017-02-28 00:26:12 +00:00
|
|
|
const char* db_dir = env->GetStringUTFChars(jdb_dir, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (db_dir == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char* wal_dir = env->GetStringUTFChars(jwal_dir, nullptr);
|
2018-04-13 00:55:14 +00:00
|
|
|
if (wal_dir == nullptr) {
|
2017-02-28 00:26:12 +00:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->ReleaseStringUTFChars(jdb_dir, db_dir);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* restore_options = reinterpret_cast<ROCKSDB_NAMESPACE::RestoreOptions*>(
|
|
|
|
jrestore_options_handle);
|
2018-04-13 00:55:14 +00:00
|
|
|
auto status = backup_engine->RestoreDBFromLatestBackup(db_dir, wal_dir,
|
|
|
|
*restore_options);
|
2017-02-28 00:26:12 +00:00
|
|
|
|
2015-08-04 23:07:17 +00:00
|
|
|
env->ReleaseStringUTFChars(jwal_dir, wal_dir);
|
|
|
|
env->ReleaseStringUTFChars(jdb_dir, db_dir);
|
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_BackupEngine
|
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
2024-01-25 20:36:30 +00:00
|
|
|
void Java_org_rocksdb_BackupEngine_disposeInternalJni(JNIEnv* /*env*/,
|
|
|
|
jclass /*jcls*/,
|
|
|
|
jlong jbe_handle) {
|
2020-02-20 20:07:53 +00:00
|
|
|
auto* be = reinterpret_cast<ROCKSDB_NAMESPACE::BackupEngine*>(jbe_handle);
|
2017-02-28 00:26:12 +00:00
|
|
|
assert(be != nullptr);
|
|
|
|
delete be;
|
2015-08-04 23:07:17 +00:00
|
|
|
}
|