From 0806801dc848a219dba3ea21ed1442348abf12fd Mon Sep 17 00:00:00 2001 From: Sagar Vemuri Date: Tue, 26 Sep 2017 16:27:28 -0700 Subject: [PATCH] DestroyDB API Summary: Expose DestroyDB API in RocksJava. Closes https://github.com/facebook/rocksdb/pull/2934 Differential Revision: D5914775 Pulled By: sagar0 fbshipit-source-id: 84af6ea0d2bccdcfb9fe8c07b2f87373f0d5bab6 --- java/rocksjni/rocksjni.cc | 27 +++++++++++++++++++ java/src/main/java/org/rocksdb/RocksDB.java | 17 ++++++++++++ .../test/java/org/rocksdb/RocksDBTest.java | 24 +++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/java/rocksjni/rocksjni.cc b/java/rocksjni/rocksjni.cc index a08a459714..53d845b63b 100644 --- a/java/rocksjni/rocksjni.cc +++ b/java/rocksjni/rocksjni.cc @@ -2196,3 +2196,30 @@ void Java_org_rocksdb_RocksDB_ingestExternalFile( rocksdb::RocksDBExceptionJni::ThrowNew(env, s); } } + +/* + * Class: org_rocksdb_RocksDB + * Method: destroyDB + * Signature: (Ljava/lang/String;J)V + */ +void Java_org_rocksdb_RocksDB_destroyDB( + JNIEnv* env, jclass jcls, jstring jdb_path, jlong joptions_handle) { + const char* db_path = env->GetStringUTFChars(jdb_path, nullptr); + if(db_path == nullptr) { + // exception thrown: OutOfMemoryError + return; + } + + auto* options = reinterpret_cast(joptions_handle); + if (options == nullptr) { + rocksdb::RocksDBExceptionJni::ThrowNew(env, + rocksdb::Status::InvalidArgument("Invalid Options.")); + } + + rocksdb::Status s = rocksdb::DestroyDB(db_path, *options); + env->ReleaseStringUTFChars(jdb_path, db_path); + + if (!s.ok()) { + rocksdb::RocksDBExceptionJni::ThrowNew(env, s); + } +} diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index eda0950990..592c7f9ad2 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -2174,6 +2174,21 @@ public class RocksDB extends RocksObject { filePathList.size(), ingestExternalFileOptions.nativeHandle_); } + /** + * Static method to destroy the contents of the specified database. + * Be very careful using this method. + * + * @param path the path to the Rocksdb database. + * @param options {@link org.rocksdb.Options} instance. + * + * @throws RocksDBException thrown if error happens in underlying + * native library. + */ + public static void destroyDB(final String path, final Options options) + throws RocksDBException { + destroyDB(path, options.nativeHandle_); + } + /** * Private constructor. * @@ -2380,5 +2395,7 @@ public class RocksDB extends RocksObject { private native void ingestExternalFile(long handle, long cfHandle, String[] filePathList, int filePathListLen, long ingest_external_file_options_handle) throws RocksDBException; + private native static void destroyDB(final String path, + final long optionsHandle) throws RocksDBException; protected DBOptionsInterface options_; } diff --git a/java/src/test/java/org/rocksdb/RocksDBTest.java b/java/src/test/java/org/rocksdb/RocksDBTest.java index 89894746d2..01f56d9b5b 100644 --- a/java/src/test/java/org/rocksdb/RocksDBTest.java +++ b/java/src/test/java/org/rocksdb/RocksDBTest.java @@ -763,4 +763,28 @@ public class RocksDBTest { } } } + + @Test + public void destroyDB() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true)) { + String dbPath = dbFolder.getRoot().getAbsolutePath(); + try (final RocksDB db = RocksDB.open(options, dbPath)) { + db.put("key1".getBytes(), "value".getBytes()); + } + assertThat(dbFolder.getRoot().exists()).isTrue(); + RocksDB.destroyDB(dbPath, options); + assertThat(dbFolder.getRoot().exists()).isFalse(); + } + } + + @Test(expected = RocksDBException.class) + public void destroyDBFailIfOpen() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true)) { + String dbPath = dbFolder.getRoot().getAbsolutePath(); + try (final RocksDB db = RocksDB.open(options, dbPath)) { + // Fails as the db is open and locked. + RocksDB.destroyDB(dbPath, options); + } + } + } }