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
This commit is contained in:
Sagar Vemuri 2017-09-26 16:27:28 -07:00 committed by Facebook Github Bot
parent aa67bae6cf
commit 0806801dc8
3 changed files with 68 additions and 0 deletions

View File

@ -2196,3 +2196,30 @@ void Java_org_rocksdb_RocksDB_ingestExternalFile(
rocksdb::RocksDBExceptionJni::ThrowNew(env, s); 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<rocksdb::Options*>(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);
}
}

View File

@ -2174,6 +2174,21 @@ public class RocksDB extends RocksObject {
filePathList.size(), ingestExternalFileOptions.nativeHandle_); 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. * Private constructor.
* *
@ -2380,5 +2395,7 @@ public class RocksDB extends RocksObject {
private native void ingestExternalFile(long handle, long cfHandle, private native void ingestExternalFile(long handle, long cfHandle,
String[] filePathList, int filePathListLen, String[] filePathList, int filePathListLen,
long ingest_external_file_options_handle) throws RocksDBException; long ingest_external_file_options_handle) throws RocksDBException;
private native static void destroyDB(final String path,
final long optionsHandle) throws RocksDBException;
protected DBOptionsInterface options_; protected DBOptionsInterface options_;
} }

View File

@ -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);
}
}
}
} }