Skip to content

Commit 5ec38c3

Browse files
committed
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
1 parent 8a139a0 commit 5ec38c3

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

java/RocksDBSample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void main(String[] args) {
6262
byte[] enoughArray = new byte[50];
6363
int len;
6464
len = db.get(testKey, insufficientArray);
65-
assert(len > testKey.length);
65+
assert(len > insufficientArray.length);
6666
len = db.get("asdfjkl;".getBytes(), enoughArray);
6767
assert(len == RocksDB.NOT_FOUND);
6868
len = db.get(testKey, enoughArray);

java/org/rocksdb/RocksDB.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public static RocksDB open(String path) throws RocksDBException {
3434
}
3535

3636
@Override public void close() throws IOException {
37-
close0();
37+
if (nativeHandle != 0) {
38+
close0();
39+
}
3840
}
3941

4042
/**

java/rocksjni/rocksjni.cc

+24-16
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
121121
JNIEnv* env, jobject jdb,
122122
jbyteArray jkey, jint jkey_len,
123123
jbyteArray jvalue, jint jvalue_len) {
124+
static const int kNotFound = -1;
125+
static const int kStatusError = -2;
126+
124127
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle(env, jdb);
125128

126129
jboolean isCopy;
@@ -142,24 +145,29 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
142145

143146
if (s.IsNotFound()) {
144147
env->ReleaseByteArrayElements(jvalue, value, JNI_ABORT);
145-
return -1;
146-
} else if (s.ok()) {
147-
int cvalue_len = static_cast<int>(cvalue.size());
148-
int length = cvalue_len;
149-
// currently we prevent overflowing.
150-
if (length > jvalue_len) {
151-
length = jvalue_len;
152-
}
153-
memcpy(value, cvalue.c_str(), length);
154-
env->ReleaseByteArrayElements(jvalue, value, JNI_COMMIT);
155-
if (cvalue_len > length) {
156-
return static_cast<jint>(cvalue.size());
157-
}
158-
return length;
148+
return kNotFound;
149+
} else if (!s.ok()) {
150+
// Here since we are throwing a Java exception from c++ side.
151+
// As a result, c++ does not know calling this function will in fact
152+
// throwing an exception. As a result, the execution flow will
153+
// not stop here, and codes after this throw will still be
154+
// executed.
155+
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
156+
157+
// Return a dummy const value to avoid compilation error, although
158+
// java side might not have a chance to get the return value :)
159+
return kStatusError;
159160
}
160-
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
161161

162-
return -1;
162+
int cvalue_len = static_cast<int>(cvalue.size());
163+
int length = std::min(jvalue_len, cvalue_len);
164+
165+
memcpy(value, cvalue.c_str(), length);
166+
env->ReleaseByteArrayElements(jvalue, value, JNI_COMMIT);
167+
if (cvalue_len > length) {
168+
return static_cast<jint>(cvalue_len);
169+
}
170+
return length;
163171
}
164172

165173
/*

0 commit comments

Comments
 (0)