@@ -121,6 +121,9 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
121
121
JNIEnv* env, jobject jdb,
122
122
jbyteArray jkey, jint jkey_len,
123
123
jbyteArray jvalue, jint jvalue_len) {
124
+ static const int kNotFound = -1 ;
125
+ static const int kStatusError = -2 ;
126
+
124
127
rocksdb::DB* db = rocksdb::RocksDBJni::getHandle (env, jdb);
125
128
126
129
jboolean isCopy;
@@ -142,24 +145,29 @@ jint Java_org_rocksdb_RocksDB_get___3BI_3BI(
142
145
143
146
if (s.IsNotFound ()) {
144
147
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 ;
159
160
}
160
- rocksdb::RocksDBExceptionJni::ThrowNew (env, s);
161
161
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;
163
171
}
164
172
165
173
/*
0 commit comments