Minor fix in rocksdb jni library.

Summary: Fix a bug in RocksDBSample.java and minor code improvement in rocksjni.cc.

Test Plan:
make jni
make jtest

Reviewers: haobo, sdong

Reviewed By: haobo

CC: leveldb

Differential Revision: https://reviews.facebook.net/D17325
main
Yueh-Hsuan Chiang 11 years ago
parent 8a139a054c
commit 5ec38c3d3e
  1. 2
      java/RocksDBSample.java
  2. 4
      java/org/rocksdb/RocksDB.java
  3. 40
      java/rocksjni/rocksjni.cc

@ -62,7 +62,7 @@ public class RocksDBSample {
byte[] enoughArray = new byte[50]; byte[] enoughArray = new byte[50];
int len; int len;
len = db.get(testKey, insufficientArray); len = db.get(testKey, insufficientArray);
assert(len > testKey.length); assert(len > insufficientArray.length);
len = db.get("asdfjkl;".getBytes(), enoughArray); len = db.get("asdfjkl;".getBytes(), enoughArray);
assert(len == RocksDB.NOT_FOUND); assert(len == RocksDB.NOT_FOUND);
len = db.get(testKey, enoughArray); len = db.get(testKey, enoughArray);

@ -34,7 +34,9 @@ public class RocksDB implements Closeable {
} }
@Override public void close() throws IOException { @Override public void close() throws IOException {
close0(); if (nativeHandle != 0) {
close0();
}
} }
/** /**

@ -121,6 +121,9 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
JNIEnv* env, jobject jdb, JNIEnv* env, jobject jdb,
jbyteArray jkey, jint jkey_len, jbyteArray jkey, jint jkey_len,
jbyteArray jvalue, jint jvalue_len) { jbyteArray jvalue, jint jvalue_len) {
static const int kNotFound = -1;
static const int kStatusError = -2;
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb); rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
jboolean isCopy; jboolean isCopy;
@ -142,24 +145,29 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
if (s.IsNotFound()) { if (s.IsNotFound()) {
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT); env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
return -1; return kNotFound;
} else if (s.ok()) { } else if (!s.ok()) {
int cvalue_len = static_cast<int>(cvalue.size()); // Here since we are throwing a Java exception from c++ side.
int length = cvalue_len; // As a result, c++ does not know calling this function will in fact
// currently we prevent overflowing. // throwing an exception. As a result, the execution flow will
if (length > jvalue_len) { // not stop here, and codes after this throw will still be
length = jvalue_len; // executed.
} rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
memcpy(value, cvalue.c_str(), length);
env->ReleaseByteArrayElements(jvalue, value, JNI_COMMIT); // Return a dummy const value to avoid compilation error, although
if (cvalue_len > length) { // java side might not have a chance to get the return value :)
return static_cast<jint>(cvalue.size()); return kStatusError;
}
return length;
} }
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
return -1; int cvalue_len = static_cast<int>(cvalue.size());
int length = std::min(jvalue_len, cvalue_len);
memcpy(value, cvalue.c_str(), length);
env->ReleaseByteArrayElements(jvalue, value, JNI_COMMIT);
if (cvalue_len > length) {
return static_cast<jint>(cvalue_len);
}
return length;
} }
/* /*

Loading…
Cancel
Save