From 01207d721034007d8f564eff9dc855a329f2d91d Mon Sep 17 00:00:00 2001 From: Stojan Dimitrovski Date: Sun, 16 Oct 2016 12:31:58 +0200 Subject: [PATCH] Add v1.19 configuration options Specifically: - error_if_exists - reuse_logs - paranoid_checks - max_open_files For details, see: https://github.com/google/leveldb/blob/v1.19/include/leveldb/options.h --- .../leveldb/test/nat/NativeOpenCloseTest.java | 23 ++++++- .../java/com/github/hf/leveldb/LevelDB.java | 66 +++++++++++++++++-- .../leveldb/implementation/NativeLevelDB.java | 6 +- ...hf_leveldb_implementation_NativeLevelDB.cc | 17 +++-- ..._hf_leveldb_implementation_NativeLevelDB.h | 4 +- .../src/main/jni-prebuild/gen-java-headers | 2 +- 6 files changed, 104 insertions(+), 14 deletions(-) diff --git a/leveldb/src/androidTest/java/com/github/hf/leveldb/test/nat/NativeOpenCloseTest.java b/leveldb/src/androidTest/java/com/github/hf/leveldb/test/nat/NativeOpenCloseTest.java index 8987846..245f408 100644 --- a/leveldb/src/androidTest/java/com/github/hf/leveldb/test/nat/NativeOpenCloseTest.java +++ b/leveldb/src/androidTest/java/com/github/hf/leveldb/test/nat/NativeOpenCloseTest.java @@ -90,8 +90,29 @@ public void testTwiceOpenADatabase() throws Exception { assertTrue(threw); ndbA.close(); - ndbA.close(); assertTrue(dbFile.exists()); } + + public void testExceptionIfFound() throws Exception { + assertFalse(dbFile.exists()); + + boolean threw = false; + + NativeLevelDB ndbA = new NativeLevelDB(dbFile.getAbsolutePath(), LevelDB.configure().createIfMissing(true).exceptionIfExists(true)); + + assertTrue(dbFile.exists()); + + try { + NativeLevelDB ndbB = new NativeLevelDB(dbFile.getAbsolutePath(), LevelDB.configure().createIfMissing(true).exceptionIfExists(true)); + } catch (LevelDBException e) { + threw = true; + } + + assertTrue(dbFile.exists()); + + ndbA.close(); + + assertTrue(threw); + } } diff --git a/leveldb/src/main/java/com/github/hf/leveldb/LevelDB.java b/leveldb/src/main/java/com/github/hf/leveldb/LevelDB.java index b4170a0..541fdad 100644 --- a/leveldb/src/main/java/com/github/hf/leveldb/LevelDB.java +++ b/leveldb/src/main/java/com/github/hf/leveldb/LevelDB.java @@ -319,15 +319,31 @@ public Iterator iterator() throws LevelDBClosedException { /** * Specifies a configuration to open the database with. + *

+ * Defaults: + * */ public static final class Configuration { - private boolean createIfMissing; - private int cacheSize; - private int blockSize; - private int writeBufferSize; + private boolean createIfMissing = true; + private boolean paranoidChecks = false; + private boolean reuseLogs = true; + private boolean exceptionIfExists = false; + + private int cacheSize = 8 * 1024 * 1024; + private int blockSize = 4 * 1024; + private int writeBufferSize = 4 * 1024 * 1024; + private int maxOpenFiles = 1000; private Configuration() { - createIfMissing = true; + // No-op. } public boolean createIfMissing() { @@ -369,5 +385,45 @@ public Configuration writeBufferSize(int writeBufferSize) { return this; } + + public Configuration paranoidChecks(boolean paranoidChecks) { + this.paranoidChecks = paranoidChecks; + + return this; + } + + public boolean paranoidChecks() { + return paranoidChecks; + } + + public int maxOpenFiles() { + return maxOpenFiles; + } + + public Configuration maxOpenFiles(int maxOpenFiles) { + this.maxOpenFiles = maxOpenFiles; + + return this; + } + + public boolean reuseLogs() { + return reuseLogs; + } + + public Configuration reuseLogs(boolean reuseLogs) { + this.reuseLogs = reuseLogs; + + return this; + } + + public Configuration exceptionIfExists(boolean exceptionIfExists) { + this.exceptionIfExists = exceptionIfExists; + + return this; + } + + public boolean exceptionIfExists() { + return exceptionIfExists; + } } } diff --git a/leveldb/src/main/java/com/github/hf/leveldb/implementation/NativeLevelDB.java b/leveldb/src/main/java/com/github/hf/leveldb/implementation/NativeLevelDB.java index 346a3d5..109f012 100644 --- a/leveldb/src/main/java/com/github/hf/leveldb/implementation/NativeLevelDB.java +++ b/leveldb/src/main/java/com/github/hf/leveldb/implementation/NativeLevelDB.java @@ -83,9 +83,13 @@ public NativeLevelDB(String path, Configuration configuration) throws LevelDBExc } ndb = nopen(configuration.createIfMissing(), + configuration.paranoidChecks(), + configuration.reuseLogs(), + configuration.exceptionIfExists(), configuration.cacheSize(), configuration.blockSize(), configuration.writeBufferSize(), + configuration.maxOpenFiles(), path); setPath(path); @@ -375,7 +379,7 @@ protected void checkIfClosed() throws LevelDBClosedException { * @return the nat structure pointer * @throws LevelDBException */ - private static native long nopen(boolean createIfMissing, int cacheSize, int blockSize, int writeBufferSize, String path) throws LevelDBException; + private static native long nopen(boolean createIfMissing, boolean paranoidChecks, boolean reuseLogs, boolean exceptionIfExists, int cacheSize, int blockSize, int writeBufferSize, int maxOpenFiles, String path) throws LevelDBException; /** * Natively closes pointers and memory. Pointer is unchecked. diff --git a/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.cc b/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.cc index 11b28ae..0ceeea7 100644 --- a/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.cc +++ b/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.cc @@ -88,8 +88,9 @@ void throwExceptionFromStatus(JNIEnv *env, leveldb::Status &status) { } } + JNIEXPORT jlong JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nopen -(JNIEnv *env, jclass cself, jboolean createIfMissing, jint cacheSize, jint blockSize, jint writeBufferSize, jstring path) { +(JNIEnv *env, jclass cself, jboolean createIfMissing, jboolean paranoidChecks, jboolean reuseLogs, jboolean exceptionIfExists, jint cacheSize, jint blockSize, jint writeBufferSize, jint maxOpenFiles, jstring path) { const char *nativePath = env->GetStringUTFChars(path, 0); @@ -106,18 +107,26 @@ JNIEXPORT jlong JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_ options.create_if_missing = createIfMissing == JNI_TRUE; options.info_log = logger; + options.paranoid_checks = paranoidChecks == JNI_TRUE; + options.reuse_logs = reuseLogs == JNI_TRUE; + options.error_if_exists = exceptionIfExists == JNI_TRUE; + if (cache != NULL) { options.block_cache = cache; } - if (blockSize != 0) { + if (blockSize > 0) { options.block_size = (size_t) blockSize; } - if (writeBufferSize != 0) { + if (writeBufferSize > 0) { options.write_buffer_size = (size_t) writeBufferSize; } + if (maxOpenFiles > 0) { + options.max_open_files = (int) maxOpenFiles; + } + leveldb::Status status = leveldb::DB::Open(options, nativePath, &db); env->ReleaseStringUTFChars(path, nativePath); @@ -346,4 +355,4 @@ JNIEXPORT void JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_n leveldb::DB* db = holder->db; db->ReleaseSnapshot((leveldb::Snapshot*) nsnapshot); -} \ No newline at end of file +} diff --git a/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.h b/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.h index d2a2838..92e8088 100644 --- a/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.h +++ b/leveldb/src/main/jni-prebuild/com_github_hf_leveldb_implementation_NativeLevelDB.h @@ -10,10 +10,10 @@ extern "C" { /* * Class: com_github_hf_leveldb_implementation_NativeLevelDB * Method: nopen - * Signature: (ZIIILjava/lang/String;)J + * Signature: (ZZZZIIIILjava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_com_github_hf_leveldb_implementation_NativeLevelDB_nopen - (JNIEnv *, jclass, jboolean, jint, jint, jint, jstring); + (JNIEnv *, jclass, jboolean, jboolean, jboolean, jboolean, jint, jint, jint, jint, jstring); /* * Class: com_github_hf_leveldb_implementation_NativeLevelDB diff --git a/leveldb/src/main/jni-prebuild/gen-java-headers b/leveldb/src/main/jni-prebuild/gen-java-headers index a254593..c46e549 100755 --- a/leveldb/src/main/jni-prebuild/gen-java-headers +++ b/leveldb/src/main/jni-prebuild/gen-java-headers @@ -5,4 +5,4 @@ JAVA_SOURCES_DIR=$JNI_PREBUILD_DIR/../java NATIVE_SOURCES='com.github.hf.leveldb.implementation.NativeLevelDB com.github.hf.leveldb.implementation.NativeWriteBatch com.github.hf.leveldb.implementation.NativeIterator' -/usr/java/latest/bin/javah -d $JNI_PREBUILD_DIR -classpath $JAVA_SOURCES_DIR $NATIVE_SOURCES +javah -d $JNI_PREBUILD_DIR -classpath $JAVA_SOURCES_DIR $NATIVE_SOURCES