From e87973cde12596657c665c44a86e412f6759389a Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Sun, 18 May 2014 22:40:48 -0700 Subject: [PATCH] Removing code from portal.h for setting handle of RestoreOptions and RestoreBackupableDB --- java/org/rocksdb/BackupableDBOptions.java | 22 +++---- java/org/rocksdb/RestoreBackupableDB.java | 29 ++++----- java/org/rocksdb/RestoreOptions.java | 15 ++--- java/org/rocksdb/test/BackupableDBTest.java | 4 +- java/rocksjni/portal.h | 66 --------------------- java/rocksjni/restorejni.cc | 38 ++++++------ 6 files changed, 54 insertions(+), 120 deletions(-) diff --git a/java/org/rocksdb/BackupableDBOptions.java b/java/org/rocksdb/BackupableDBOptions.java index 8bcc2cf18..0989d39a8 100644 --- a/java/org/rocksdb/BackupableDBOptions.java +++ b/java/org/rocksdb/BackupableDBOptions.java @@ -11,26 +11,26 @@ package org.rocksdb; * * Note that dispose() must be called before an Options instance * become out-of-scope to release the allocated memory in c++. - * + * * @param path Where to keep the backup files. Has to be different than dbname. Best to set this to dbname_ + "/backups" - * @param shareTableFiles If share_table_files == true, backup will assume that - * table files with same name have the same contents. This enables - * incremental backups and avoids unnecessary data copies. If - * share_table_files == false, each backup will be on its own and will not + * @param shareTableFiles If share_table_files == true, backup will assume that + * table files with same name have the same contents. This enables + * incremental backups and avoids unnecessary data copies. If + * share_table_files == false, each backup will be on its own and will not * share any data with other backups. default: true - * @param sync If sync == true, we can guarantee you'll get consistent backup - * even on a machine crash/reboot. Backup process is slower with sync + * @param sync If sync == true, we can guarantee you'll get consistent backup + * even on a machine crash/reboot. Backup process is slower with sync * enabled. If sync == false, we don't guarantee anything on machine reboot. * However, chances are some of the backups are consistent. Default: true - * @param destroyOldData If true, it will delete whatever backups there are + * @param destroyOldData If true, it will delete whatever backups there are * already. Default: false * @param backupLogFiles If false, we won't backup log files. This option can be - * useful for backing up in-memory databases where log file are persisted, + * useful for backing up in-memory databases where log file are persisted, * but table files are in memory. Default: true - * @param backupRateLimit Max bytes that can be transferred in a second during + * @param backupRateLimit Max bytes that can be transferred in a second during * backup. If 0, go as fast as you can. Default: 0 - * @param restoreRateLimit Max bytes that can be transferred in a second during + * @param restoreRateLimit Max bytes that can be transferred in a second during * restore. If 0, go as fast as you can. Default: 0 */ public class BackupableDBOptions extends RocksObject { diff --git a/java/org/rocksdb/RestoreBackupableDB.java b/java/org/rocksdb/RestoreBackupableDB.java index f6a4ed0ab..8c6dbf531 100644 --- a/java/org/rocksdb/RestoreBackupableDB.java +++ b/java/org/rocksdb/RestoreBackupableDB.java @@ -7,24 +7,24 @@ package org.rocksdb; /** * This class is used to access information about backups and restore from them. - * - * Note that dispose() must be called before this instance become out-of-scope + * + * Note that dispose() must be called before this instance become out-of-scope * to release the allocated memory in c++. - * + * * @param options Instance of BackupableDBOptions. */ public class RestoreBackupableDB extends RocksObject { public RestoreBackupableDB(BackupableDBOptions options) { super(); - newRestoreBackupableDB(options.nativeHandle_); + nativeHandle_ = newRestoreBackupableDB(options.nativeHandle_); } - + /** * Restore from backup with backup_id * IMPORTANT -- if options_.share_table_files == true and you restore DB * from some backup that is not the latest, and you start creating new * backups from the new DB, they will probably fail. - * + * * Example: Let's say you have backups 1, 2, 3, 4, 5 and you restore 3. * If you add new data to the DB and try creating a new backup now, the * database will diverge from backups 4 and 5 and the new backup will fail. @@ -36,7 +36,7 @@ public class RestoreBackupableDB extends RocksObject { restoreDBFromBackup0(nativeHandle_, backupId, dbDir, walDir, restoreOptions.nativeHandle_); } - + /** * Restore from the latest backup. */ @@ -45,25 +45,25 @@ public class RestoreBackupableDB extends RocksObject { restoreDBFromLatestBackup0(nativeHandle_, dbDir, walDir, restoreOptions.nativeHandle_); } - + /** * Deletes old backups, keeping latest numBackupsToKeep alive. - * + * * @param Number of latest backups to keep */ public void purgeOldBackups(int numBackupsToKeep) throws RocksDBException { purgeOldBackups0(nativeHandle_, numBackupsToKeep); } - + /** * Deletes a specific backup. - * + * * @param ID of backup to delete. */ public void deleteBackup(long backupId) throws RocksDBException { deleteBackup0(nativeHandle_, backupId); } - + /** * Release the memory allocated for the current instance * in the c++ side. @@ -71,10 +71,11 @@ public class RestoreBackupableDB extends RocksObject { @Override public synchronized void dispose() { if (isInitialized()) { dispose(nativeHandle_); + nativeHandle_ = 0; } } - - private native void newRestoreBackupableDB(long options); + + private native long newRestoreBackupableDB(long options); private native void restoreDBFromBackup0(long nativeHandle, long backupId, String dbDir, String walDir, long restoreOptions) throws RocksDBException; private native void restoreDBFromLatestBackup0(long nativeHandle, diff --git a/java/org/rocksdb/RestoreOptions.java b/java/org/rocksdb/RestoreOptions.java index 51ffcbc73..0321c3b9c 100644 --- a/java/org/rocksdb/RestoreOptions.java +++ b/java/org/rocksdb/RestoreOptions.java @@ -8,21 +8,21 @@ package org.rocksdb; /** * RestoreOptions to control the behavior of restore. * - * Note that dispose() must be called before this instance become out-of-scope + * Note that dispose() must be called before this instance become out-of-scope * to release the allocated memory in c++. - * + * * @param If true, restore won't overwrite the existing log files in wal_dir. It * will also move all log files from archive directory to wal_dir. Use this - * option in combination with BackupableDBOptions::backup_log_files = false + * option in combination with BackupableDBOptions::backup_log_files = false * for persisting in-memory databases. * Default: false */ public class RestoreOptions extends RocksObject { public RestoreOptions(boolean keepLogFiles) { super(); - newRestoreOptions(keepLogFiles); + nativeHandle_ = newRestoreOptions(keepLogFiles); } - + /** * Release the memory allocated for the current instance * in the c++ side. @@ -30,9 +30,10 @@ public class RestoreOptions extends RocksObject { @Override public synchronized void dispose() { if (isInitialized()) { dispose(nativeHandle_); + nativeHandle_ = 0; } } - - private native void newRestoreOptions(boolean keepLogFiles); + + private native long newRestoreOptions(boolean keepLogFiles); private native void dispose(long handle); } diff --git a/java/org/rocksdb/test/BackupableDBTest.java b/java/org/rocksdb/test/BackupableDBTest.java index 3ef88dc8c..9d3a64c06 100644 --- a/java/org/rocksdb/test/BackupableDBTest.java +++ b/java/org/rocksdb/test/BackupableDBTest.java @@ -36,10 +36,12 @@ public class BackupableDBTest { bdb.close(); // restore from backup + RestoreOptions ropt = new RestoreOptions(false); RestoreBackupableDB rdb = new RestoreBackupableDB(bopt); rdb.restoreDBFromLatestBackup(db_path, db_path, - new RestoreOptions(false)); + ropt); rdb.dispose(); + ropt.dispose(); // verify that backed up data contains deleted record bdb = BackupableDB.open(opt, bopt, db_path); diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index 181b67705..b4f874330 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -251,72 +251,6 @@ class BackupableDBOptionsJni { } }; -class RestoreOptionsJni { - public: - // Get the java class id of org.rocksdb.RestoreOptions. - static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/RestoreOptions"); - assert(jclazz != nullptr); - return jclazz; - } - - // Get the field id of the member variable of org.rocksdb.RestoreOptions - // that stores the pointer to rocksdb::RestoreOptions - static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( - getJClass(env), "nativeHandle_", "J"); - assert(fid != nullptr); - return fid; - } - - // Get the pointer to rocksdb::RestoreOptions - static rocksdb::RestoreOptions* getHandle(JNIEnv* env, jobject jobj) { - return reinterpret_cast( - env->GetLongField(jobj, getHandleFieldID(env))); - } - - // Pass the rocksdb::RestoreOptions pointer to the java side. - static void setHandle( - JNIEnv* env, jobject jobj, rocksdb::RestoreOptions* op) { - env->SetLongField( - jobj, getHandleFieldID(env), - reinterpret_cast(op)); - } -}; - -class RestoreBackupableDBJni { - public: - // Get the java class id of org.rocksdb.RestoreBackupableDB. - static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/RestoreBackupableDB"); - assert(jclazz != nullptr); - return jclazz; - } - - // Get the field id of the member variable of org.rocksdb.RestoreBackupableDB - // that stores the pointer to rocksdb::RestoreBackupableDB - static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( - getJClass(env), "nativeHandle_", "J"); - assert(fid != nullptr); - return fid; - } - - // Get the pointer to rocksdb::RestoreBackupableDB - static rocksdb::RestoreBackupableDB* getHandle(JNIEnv* env, jobject jobj) { - return reinterpret_cast( - env->GetLongField(jobj, getHandleFieldID(env))); - } - - // Pass the rocksdb::RestoreBackupableDB pointer to the java side. - static void setHandle( - JNIEnv* env, jobject jobj, rocksdb::RestoreBackupableDB* op) { - env->SetLongField( - jobj, getHandleFieldID(env), - reinterpret_cast(op)); - } -}; - class IteratorJni { public: // Get the java class id of org.rocksdb.Iteartor. diff --git a/java/rocksjni/restorejni.cc b/java/rocksjni/restorejni.cc index d351d4066..a6ade81a7 100644 --- a/java/rocksjni/restorejni.cc +++ b/java/rocksjni/restorejni.cc @@ -4,7 +4,7 @@ // 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::RestoreBackupableDB and rocksdb::RestoreOptions methods +// calling c++ rocksdb::RestoreBackupableDB and rocksdb::RestoreOptions methods // from Java side. #include @@ -20,12 +20,12 @@ /* * Class: org_rocksdb_RestoreOptions * Method: newRestoreOptions - * Signature: (Z)V + * Signature: (Z)J */ -void Java_org_rocksdb_RestoreOptions_newRestoreOptions(JNIEnv* env, +jlong Java_org_rocksdb_RestoreOptions_newRestoreOptions(JNIEnv* env, jobject jobj, jboolean keep_log_files) { auto ropt = new rocksdb::RestoreOptions(keep_log_files); - rocksdb::RestoreOptionsJni::setHandle(env, jobj, ropt); + return reinterpret_cast(ropt); } /* @@ -38,20 +38,18 @@ void Java_org_rocksdb_RestoreOptions_dispose(JNIEnv* env, jobject jobj, auto ropt = reinterpret_cast(jhandle); assert(ropt); delete ropt; - - rocksdb::RestoreOptionsJni::setHandle(env, jobj, nullptr); } /* * Class: org_rocksdb_RestoreBackupableDB * Method: newRestoreBackupableDB - * Signature: (J)V + * Signature: (J)J */ -void Java_org_rocksdb_RestoreBackupableDB_newRestoreBackupableDB(JNIEnv* env, +jlong Java_org_rocksdb_RestoreBackupableDB_newRestoreBackupableDB(JNIEnv* env, jobject jobj, jlong jopt_handle) { auto opt = reinterpret_cast(jopt_handle); auto rdb = new rocksdb::RestoreBackupableDB(rocksdb::Env::Default(), *opt); - rocksdb::RestoreBackupableDBJni::setHandle(env, jobj, rdb); + return reinterpret_cast(rdb); } /* @@ -63,17 +61,17 @@ void Java_org_rocksdb_RestoreBackupableDB_restoreDBFromBackup0(JNIEnv* env, jobject jobj, jlong jhandle, jlong jbackup_id, jstring jdb_dir, jstring jwal_dir, jlong jopt_handle) { auto opt = reinterpret_cast(jopt_handle); - + const char* cdb_dir = env->GetStringUTFChars(jdb_dir, 0); const char* cwal_dir = env->GetStringUTFChars(jwal_dir, 0); - + auto rdb = reinterpret_cast(jhandle); rocksdb::Status s = rdb->RestoreDBFromBackup(jbackup_id, cdb_dir, cwal_dir, *opt); - + env->ReleaseStringUTFChars(jdb_dir, cdb_dir); env->ReleaseStringUTFChars(jwal_dir, cwal_dir); - + if(!s.ok()) { rocksdb::RocksDBExceptionJni::ThrowNew(env, s); } @@ -88,17 +86,17 @@ void Java_org_rocksdb_RestoreBackupableDB_restoreDBFromLatestBackup0( JNIEnv* env, jobject jobj, jlong jhandle, jstring jdb_dir, jstring jwal_dir, jlong jopt_handle) { auto opt = reinterpret_cast(jopt_handle); - + const char* cdb_dir = env->GetStringUTFChars(jdb_dir, 0); const char* cwal_dir = env->GetStringUTFChars(jwal_dir, 0); - + auto rdb = reinterpret_cast(jhandle); rocksdb::Status s = rdb->RestoreDBFromLatestBackup(cdb_dir, cwal_dir, *opt); - + env->ReleaseStringUTFChars(jdb_dir, cdb_dir); env->ReleaseStringUTFChars(jwal_dir, cwal_dir); - + if(!s.ok()) { rocksdb::RocksDBExceptionJni::ThrowNew(env, s); } @@ -113,7 +111,7 @@ void Java_org_rocksdb_RestoreBackupableDB_purgeOldBackups0(JNIEnv* env, jobject jobj, jlong jhandle, jint jnum_backups_to_keep) { auto rdb = reinterpret_cast(jhandle); rocksdb::Status s = rdb->PurgeOldBackups(jnum_backups_to_keep); - + if(!s.ok()) { rocksdb::RocksDBExceptionJni::ThrowNew(env, s); } @@ -128,7 +126,7 @@ void Java_org_rocksdb_RestoreBackupableDB_deleteBackup0(JNIEnv* env, jobject jobj, jlong jhandle, jlong jbackup_id) { auto rdb = reinterpret_cast(jhandle); rocksdb::Status s = rdb->DeleteBackup(jbackup_id); - + if(!s.ok()) { rocksdb::RocksDBExceptionJni::ThrowNew(env, s); } @@ -144,6 +142,4 @@ void Java_org_rocksdb_RestoreBackupableDB_dispose(JNIEnv* env, jobject jobj, auto ropt = reinterpret_cast(jhandle); assert(ropt); delete ropt; - - rocksdb::RestoreBackupableDBJni::setHandle(env, jobj, nullptr); }