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. 2
      java/org/rocksdb/RocksDB.java
  3. 32
      java/rocksjni/rocksjni.cc

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

@ -34,8 +34,10 @@ public class RocksDB implements Closeable {
}
@Override public void close() throws IOException {
if (nativeHandle != 0) {
close0();
}
}
/**
* Set the database entry for "key" to "value".

@ -121,6 +121,9 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
JNIEnv* env, jobject jdb,
jbyteArray jkey, jint jkey_len,
jbyteArray jvalue, jint jvalue_len) {
static const int kNotFound = -1;
static const int kStatusError = -2;
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
jboolean isCopy;
@ -142,25 +145,30 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
if (s.IsNotFound()) {
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
return -1;
} else if (s.ok()) {
int cvalue_len = static_cast<int>(cvalue.size());
int length = cvalue_len;
// currently we prevent overflowing.
if (length > jvalue_len) {
length = jvalue_len;
return kNotFound;
} else if (!s.ok()) {
// 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
// not stop here, and codes after this throw will still be
// executed.
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
// Return a dummy const value to avoid compilation error, although
// java side might not have a chance to get the return value :)
return kStatusError;
}
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.size());
return static_cast<jint>(cvalue_len);
}
return length;
}
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
return -1;
}
/*
* Class: org_rocksdb_RocksDB

Loading…
Cancel
Save