提交 a73383e8 编写于 作者: Y Yueh-Hsuan Chiang

Minor fix in rocksdb jni library, RocksDB now does not implement Closeable.

Summary:
* [java] RocksDB now does not implement Closeable.
* [java] RocksDB.close() is now synchronized.
* [c++] Fix a bug in rocksjni.cc that does not release a java reference before
        throwing an exception.

Test Plan:
make jni
make jtest

Reviewers: haobo, sdong

Reviewed By: haobo

CC: leveldb

Differential Revision: https://reviews.facebook.net/D17355
上级 8e81caf0
......@@ -21,9 +21,10 @@ public class RocksDBSample {
String db_path = args[0];
System.out.println("RocksDBSample");
RocksDB db = null;
try {
RocksDB db = RocksDB.open(db_path);
db = RocksDB.open(db_path);
db.put("hello".getBytes(), "world".getBytes());
byte[] value = db.get("hello".getBytes());
System.out.format("Get('hello') = %s\n",
......@@ -67,13 +68,11 @@ public class RocksDBSample {
assert(len == RocksDB.NOT_FOUND);
len = db.get(testKey, enoughArray);
assert(len == testValue.length);
try {
db.close();
} catch (IOException e) {
System.err.println(e);
}
} catch (RocksDBException e) {
System.err.println(e);
}
if (db != null) {
db.close();
}
}
}
......@@ -16,7 +16,7 @@ import java.io.IOException;
* All methods of this class could potentially throw RocksDBException, which
* indicates sth wrong at the rocksdb library side and the call failed.
*/
public class RocksDB implements Closeable {
public class RocksDB {
public static final int NOT_FOUND = -1;
/**
* The factory constructor of RocksDB that opens a RocksDB instance given
......@@ -33,8 +33,8 @@ public class RocksDB implements Closeable {
return db;
}
@Override public void close() throws IOException {
if (nativeHandle != 0) {
public synchronized void close() {
if (nativeHandle_ != 0) {
close0();
}
}
......@@ -80,11 +80,15 @@ public class RocksDB implements Closeable {
return get(key, key.length);
}
@Override protected void finalize() {
close();
}
/**
* Private constructor.
*/
private RocksDB() {
nativeHandle = -1;
nativeHandle_ = 0;
}
// native methods
......@@ -99,5 +103,5 @@ public class RocksDB implements Closeable {
byte[] key, int keyLen) throws RocksDBException;
private native void close0();
private long nativeHandle;
private long nativeHandle_;
}
......@@ -29,7 +29,7 @@ class RocksDBJni {
// that stores the pointer to rocksdb::DB.
static jfieldID getHandleFieldID(JNIEnv* env) {
static jfieldID fid = env->GetFieldID(
getJClass(env), "nativeHandle", "J");
getJClass(env), "nativeHandle_", "J");
assert(fid != nullptr);
return fid;
}
......
......@@ -147,6 +147,7 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
return kNotFound;
} else if (!s.ok()) {
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
// Here since we are throwing a Java exception from c++ side.
// As a result, c++ does not know calling this function will in fact
// throwing an exception. As a result, the execution flow will
......@@ -164,10 +165,7 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
memcpy(value, cvalue.c_str(), length);
env->ReleaseByteArrayElements(jvalue, value, JNI_COMMIT);
if (cvalue_len > length) {
return static_cast<jint>(cvalue_len);
}
return length;
return static_cast<jint>(cvalue_len);
}
/*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册