Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
66 changes: 61 additions & 5 deletions leveldb/src/main/java/com/github/hf/leveldb/LevelDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,31 @@ public Iterator iterator() throws LevelDBClosedException {

/**
* Specifies a configuration to open the database with.
* </p>
* Defaults:
* <ul>
* <li>createIfMissing (true)</li>
* <li>paranoidChecks (false)</li>
* <li>reuseLogs (true)</li>
* <li>cacheSize (8MB)</li>
* <li>blockSize (4K)</li>
* <li>writeBufferSize (4MB)</li>
* <li>maxOpenFiles (1000)</li>
* </ul>
*/
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() {
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion leveldb/src/main/jni-prebuild/gen-java-headers
Original file line number Diff line number Diff line change
Expand Up @@ -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