jni: uniformly use GetByteArrayRegion() to copy bytes (#9380)

Summary:
Uniformly use GetByteArrayRegion() instead of GetByteArrayElements()
to copy bytes.
In addition, it can avoid an inefficient ReleaseByteArrayElements()
operation.
Some benefits of GetByteArrayRegion() can be referred to:
https://stackoverflow.com/a/2480493

Pull Request resolved: https://github.com/facebook/rocksdb/pull/9380

Reviewed By: ajkr

Differential Revision: D35135474

Pulled By: jay-zhuang

fbshipit-source-id: a32c1774d37f2d22b9bcd105d83e0bb984b71b54
main
Jermy Li 3 years ago committed by Facebook GitHub Bot
parent 1a130fa3c1
commit b83263bbe4
  1. 13
      java/rocksjni/rocksjni.cc
  2. 20
      java/rocksjni/write_batch_with_index.cc

@ -829,9 +829,11 @@ bool rocksdb_single_delete_helper(
const ROCKSDB_NAMESPACE::WriteOptions& write_options, const ROCKSDB_NAMESPACE::WriteOptions& write_options,
ROCKSDB_NAMESPACE::ColumnFamilyHandle* cf_handle, jbyteArray jkey, ROCKSDB_NAMESPACE::ColumnFamilyHandle* cf_handle, jbyteArray jkey,
jint jkey_len) { jint jkey_len) {
jbyte* key = env->GetByteArrayElements(jkey, nullptr); jbyte* key = new jbyte[jkey_len];
if (key == nullptr) { env->GetByteArrayRegion(jkey, 0, jkey_len, key);
// exception thrown: OutOfMemoryError if (env->ExceptionCheck()) {
// exception thrown: ArrayIndexOutOfBoundsException
delete[] key;
return false; return false;
} }
ROCKSDB_NAMESPACE::Slice key_slice(reinterpret_cast<char*>(key), jkey_len); ROCKSDB_NAMESPACE::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
@ -844,10 +846,7 @@ bool rocksdb_single_delete_helper(
s = db->SingleDelete(write_options, key_slice); s = db->SingleDelete(write_options, key_slice);
} }
// trigger java unref on key and value. delete[] key;
// by passing JNI_ABORT, it will simply release the reference without
// copying the result back to the java byte array.
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
if (s.ok()) { if (s.ok()) {
return true; return true;

@ -735,9 +735,11 @@ void Java_org_rocksdb_WBWIRocksIterator_seek0(JNIEnv* env, jobject /*jobj*/,
jlong handle, jbyteArray jtarget, jlong handle, jbyteArray jtarget,
jint jtarget_len) { jint jtarget_len) {
auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle); auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
jbyte* target = env->GetByteArrayElements(jtarget, nullptr); jbyte* target = new jbyte[jtarget_len];
if (target == nullptr) { env->GetByteArrayRegion(jtarget, 0, jtarget_len, target);
// exception thrown: OutOfMemoryError if (env->ExceptionCheck()) {
// exception thrown: ArrayIndexOutOfBoundsException
delete[] target;
return; return;
} }
@ -746,7 +748,7 @@ void Java_org_rocksdb_WBWIRocksIterator_seek0(JNIEnv* env, jobject /*jobj*/,
it->Seek(target_slice); it->Seek(target_slice);
env->ReleaseByteArrayElements(jtarget, target, JNI_ABORT); delete[] target;
} }
/* /*
@ -803,9 +805,11 @@ void Java_org_rocksdb_WBWIRocksIterator_seekForPrev0(JNIEnv* env,
jbyteArray jtarget, jbyteArray jtarget,
jint jtarget_len) { jint jtarget_len) {
auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle); auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
jbyte* target = env->GetByteArrayElements(jtarget, nullptr); jbyte* target = new jbyte[jtarget_len];
if (target == nullptr) { env->GetByteArrayRegion(jtarget, 0, jtarget_len, target);
// exception thrown: OutOfMemoryError if (env->ExceptionCheck()) {
// exception thrown: ArrayIndexOutOfBoundsException
delete[] target;
return; return;
} }
@ -814,7 +818,7 @@ void Java_org_rocksdb_WBWIRocksIterator_seekForPrev0(JNIEnv* env,
it->SeekForPrev(target_slice); it->SeekForPrev(target_slice);
env->ReleaseByteArrayElements(jtarget, target, JNI_ABORT); delete[] target;
} }
/* /*

Loading…
Cancel
Save