Abstract duplicate code on key and value slice objects into generic methods

main
Adam Retter 10 years ago
parent 2d0dd8db3b
commit 3d246c89cc
  1. 45
      java/rocksjni/portal.h
  2. 136
      java/rocksjni/write_batch.cc
  3. 142
      java/rocksjni/write_batch_with_index.cc

@ -1009,7 +1009,7 @@ class WriteEntryJni {
class JniUtil {
public:
/**
/*
* Copies a jstring to a std::string
* and releases the original jstring
*/
@ -1019,6 +1019,49 @@ class JniUtil {
env->ReleaseStringUTFChars(js, utf);
return name;
}
/*
* Helper for operations on a key and value
* for example WriteBatch->Put
*
* TODO(AR) could be extended to cover returning rocksdb::Status
* from `op` and used for RocksDB->Put etc.
*/
static void kv_op(
std::function<void(rocksdb::Slice, rocksdb::Slice)> op,
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len) {
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
jentry_value_len);
op(key_slice, value_slice);
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
}
/*
* Helper for operations on a key
* for example WriteBatch->Delete
*
* TODO(AR) could be extended to cover returning rocksdb::Status
* from `op` and used for RocksDB->Delete etc.
*/
static void k_op(
std::function<void(rocksdb::Slice)> op,
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len) {
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
op(key_slice);
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
}
};
} // namespace rocksdb

@ -63,32 +63,6 @@ void Java_org_rocksdb_WriteBatch_clear0(JNIEnv* env, jobject jobj) {
wb->Clear();
}
/*
* Helper for WriteBatch put operations
*/
void write_batch_put_helper(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len,
rocksdb::ColumnFamilyHandle* cf_handle) {
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
jentry_value_len);
if (cf_handle != nullptr) {
wb->Put(cf_handle, key_slice, value_slice);
} else {
// backwards compatibility
wb->Put(key_slice, value_slice);
}
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
}
/*
* Class: org_rocksdb_WriteBatch
* Method: put
@ -98,8 +72,13 @@ void Java_org_rocksdb_WriteBatch_put___3BI_3BI(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len) {
write_batch_put_helper(env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len, nullptr);
auto* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
auto put = [&wb] (rocksdb::Slice key, rocksdb::Slice value) {
wb->Put(key, value);
};
rocksdb::JniUtil::kv_op(put, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
}
/*
@ -111,35 +90,15 @@ void Java_org_rocksdb_WriteBatch_put___3BI_3BIJ(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
write_batch_put_helper(env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len, cf_handle);
}
/*
* Helper for write batch merge operations
*/
void write_batch_merge_helper(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len,
rocksdb::ColumnFamilyHandle* cf_handle) {
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
auto* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
assert(cf_handle != nullptr);
auto put = [&wb, &cf_handle] (rocksdb::Slice key, rocksdb::Slice value) {
wb->Put(cf_handle, key, value);
};
rocksdb::JniUtil::kv_op(put, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
if (cf_handle != nullptr) {
wb->Merge(cf_handle, key_slice, value_slice);
} else {
// backwards compatibility
wb->Merge(key_slice, value_slice);
}
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
}
/*
@ -151,8 +110,13 @@ void Java_org_rocksdb_WriteBatch_merge___3BI_3BI(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len) {
write_batch_merge_helper(env, jobj, jkey, jkey_len,
jentry_value, jentry_value_len, nullptr);
auto* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
auto merge = [&wb] (rocksdb::Slice key, rocksdb::Slice value) {
wb->Merge(key, value);
};
rocksdb::JniUtil::kv_op(merge, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
}
/*
@ -164,29 +128,15 @@ void Java_org_rocksdb_WriteBatch_merge___3BI_3BIJ(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
write_batch_merge_helper(env, jobj, jkey, jkey_len,
jentry_value, jentry_value_len, cf_handle);
}
/*
* Helper for write batch remove operations
*/
void write_batch_remove_helper(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
rocksdb::ColumnFamilyHandle* cf_handle) {
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
auto* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
if (cf_handle != nullptr) {
wb->Delete(cf_handle, key_slice);
} else {
wb->Delete(key_slice);
}
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
assert(cf_handle != nullptr);
auto merge = [&wb, &cf_handle] (rocksdb::Slice key, rocksdb::Slice value) {
wb->Merge(cf_handle, key, value);
};
rocksdb::JniUtil::kv_op(merge, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
}
/*
@ -197,7 +147,12 @@ void write_batch_remove_helper(
void Java_org_rocksdb_WriteBatch_remove___3BI(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len) {
write_batch_remove_helper(env, jobj, jkey, jkey_len, nullptr);
auto* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
auto remove = [&wb] (rocksdb::Slice key) {
wb->Delete(key);
};
rocksdb::JniUtil::k_op(remove, env, jobj, jkey, jkey_len);
}
/*
@ -208,8 +163,14 @@ void Java_org_rocksdb_WriteBatch_remove___3BI(
void Java_org_rocksdb_WriteBatch_remove___3BIJ(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
write_batch_remove_helper(env, jobj, jkey, jkey_len, cf_handle);
auto* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
assert(cf_handle != nullptr);
auto remove = [&wb, &cf_handle] (rocksdb::Slice key) {
wb->Delete(cf_handle, key);
};
rocksdb::JniUtil::k_op(remove, env, jobj, jkey, jkey_len);
}
/*
@ -219,13 +180,12 @@ void Java_org_rocksdb_WriteBatch_remove___3BIJ(
*/
void Java_org_rocksdb_WriteBatch_putLogData(
JNIEnv* env, jobject jobj, jbyteArray jblob, jint jblob_len) {
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
auto* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
assert(wb != nullptr);
jbyte* blob = env->GetByteArrayElements(jblob, nullptr);
rocksdb::Slice blob_slice(reinterpret_cast<char*>(blob), jblob_len);
wb->PutLogData(blob_slice);
env->ReleaseByteArrayElements(jblob, blob, JNI_ABORT);
auto putLogData = [&wb] (rocksdb::Slice blob) {
wb->PutLogData(blob);
};
rocksdb::JniUtil::k_op(putLogData, env, jobj, jblob, jblob_len);
}
/*

@ -65,34 +65,6 @@ jint Java_org_rocksdb_WriteBatchWithIndex_count0(
return static_cast<jint>(wbwi->GetWriteBatch()->Count());
}
//TODO(AR) make generic with WriteBatch equivalent
/*
* Helper for WriteBatchWithIndex put operations
*/
void write_batch_with_index_put_helper(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len,
rocksdb::ColumnFamilyHandle* cf_handle) {
rocksdb::WriteBatchWithIndex* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
jentry_value_len);
if (cf_handle != nullptr) {
wbwi->Put(cf_handle, key_slice, value_slice);
} else {
// backwards compatibility
wbwi->Put(key_slice, value_slice);
}
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
}
/*
* Class: org_rocksdb_WriteBatchWithIndex
* Method: put
@ -101,8 +73,14 @@ void write_batch_with_index_put_helper(
void Java_org_rocksdb_WriteBatchWithIndex_put___3BI_3BI(
JNIEnv* env, jobject jobj, jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len) {
write_batch_with_index_put_helper(env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len, nullptr);
auto* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
auto put = [&wbwi] (rocksdb::Slice key, rocksdb::Slice value) {
wbwi->Put(key, value);
};
rocksdb::JniUtil::kv_op(put, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
}
/*
@ -113,37 +91,16 @@ void Java_org_rocksdb_WriteBatchWithIndex_put___3BI_3BI(
void Java_org_rocksdb_WriteBatchWithIndex_put___3BI_3BIJ(
JNIEnv* env, jobject jobj, jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
write_batch_with_index_put_helper(env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len, cf_handle);
}
//TODO(AR) make generic with WriteBatch equivalent
/*
* Helper for WriteBatchWithIndex merge operations
*/
void write_batch_with_index_merge_helper(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len,
rocksdb::ColumnFamilyHandle* cf_handle) {
rocksdb::WriteBatchWithIndex* wbwi =
auto* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
assert(cf_handle != nullptr);
auto put = [&wbwi, &cf_handle] (rocksdb::Slice key, rocksdb::Slice value) {
wbwi->Put(cf_handle, key, value);
};
rocksdb::JniUtil::kv_op(put, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
if (cf_handle != nullptr) {
wbwi->Merge(cf_handle, key_slice, value_slice);
} else {
// backwards compatibility
wbwi->Merge(key_slice, value_slice);
}
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
}
/*
@ -154,8 +111,14 @@ void write_batch_with_index_merge_helper(
void Java_org_rocksdb_WriteBatchWithIndex_merge___3BI_3BI(
JNIEnv* env, jobject jobj, jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len) {
write_batch_with_index_merge_helper(env, jobj, jkey, jkey_len,
jentry_value, jentry_value_len, nullptr);
auto* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
auto merge = [&wbwi] (rocksdb::Slice key, rocksdb::Slice value) {
wbwi->Merge(key, value);
};
rocksdb::JniUtil::kv_op(merge, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
}
/*
@ -166,31 +129,16 @@ void Java_org_rocksdb_WriteBatchWithIndex_merge___3BI_3BI(
void Java_org_rocksdb_WriteBatchWithIndex_merge___3BI_3BIJ(
JNIEnv* env, jobject jobj, jbyteArray jkey, jint jkey_len,
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
write_batch_with_index_merge_helper(env, jobj, jkey, jkey_len,
jentry_value, jentry_value_len, cf_handle);
}
//TODO(AR) make generic with WriteBatch equivalent
/*
* Helper for write batch remove operations
*/
void write_batch_with_index_remove_helper(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len,
rocksdb::ColumnFamilyHandle* cf_handle) {
rocksdb::WriteBatchWithIndex* wbwi =
auto* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
if (cf_handle != nullptr) {
wbwi->Delete(cf_handle, key_slice);
} else {
wbwi->Delete(key_slice);
}
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
assert(cf_handle != nullptr);
auto merge = [&wbwi, &cf_handle] (rocksdb::Slice key, rocksdb::Slice value) {
wbwi->Merge(cf_handle, key, value);
};
rocksdb::JniUtil::kv_op(merge, env, jobj, jkey, jkey_len, jentry_value,
jentry_value_len);
}
/*
@ -200,7 +148,13 @@ void write_batch_with_index_remove_helper(
*/
void Java_org_rocksdb_WriteBatchWithIndex_remove___3BI(
JNIEnv* env, jobject jobj, jbyteArray jkey, jint jkey_len) {
write_batch_with_index_remove_helper(env, jobj, jkey, jkey_len, nullptr);
auto* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
auto remove = [&wbwi] (rocksdb::Slice key) {
wbwi->Delete(key);
};
rocksdb::JniUtil::k_op(remove, env, jobj, jkey, jkey_len);
}
/*
@ -211,8 +165,15 @@ void Java_org_rocksdb_WriteBatchWithIndex_remove___3BI(
void Java_org_rocksdb_WriteBatchWithIndex_remove___3BIJ(
JNIEnv* env, jobject jobj,
jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
auto* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
write_batch_with_index_remove_helper(env, jobj, jkey, jkey_len, cf_handle);
assert(cf_handle != nullptr);
auto remove = [&wbwi, &cf_handle] (rocksdb::Slice key) {
wbwi->Delete(cf_handle, key);
};
rocksdb::JniUtil::k_op(remove, env, jobj, jkey, jkey_len);
}
/*
@ -222,14 +183,13 @@ void Java_org_rocksdb_WriteBatchWithIndex_remove___3BIJ(
*/
void Java_org_rocksdb_WriteBatchWithIndex_putLogData(
JNIEnv* env, jobject jobj, jbyteArray jblob, jint jblob_len) {
rocksdb::WriteBatchWithIndex* wbwi =
auto* wbwi =
rocksdb::WriteBatchWithIndexJni::getHandle(env, jobj);
assert(wbwi != nullptr);
jbyte* blob = env->GetByteArrayElements(jblob, nullptr);
rocksdb::Slice blob_slice(reinterpret_cast<char*>(blob), jblob_len);
wbwi->PutLogData(blob_slice);
env->ReleaseByteArrayElements(jblob, blob, JNI_ABORT);
auto putLogData = [&wbwi] (rocksdb::Slice blob) {
wbwi->PutLogData(blob);
};
rocksdb::JniUtil::k_op(putLogData, env, jobj, jblob, jblob_len);
}
/*

Loading…
Cancel
Save