2014-04-02 20:14:55 +00:00
|
|
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// This file implements the "bridge" between Java and C++ and enables
|
|
|
|
// calling c++ rocksdb::WriteBatch methods from Java side.
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_WriteBatch.h"
|
2014-10-23 15:19:38 +00:00
|
|
|
#include "include/org_rocksdb_WriteBatch_Handler.h"
|
2014-11-02 00:08:41 +00:00
|
|
|
#include "include/org_rocksdb_test_WriteBatchInternal.h"
|
|
|
|
#include "include/org_rocksdb_test_WriteBatchTest.h"
|
2014-04-02 20:14:55 +00:00
|
|
|
#include "rocksjni/portal.h"
|
2014-10-23 15:19:38 +00:00
|
|
|
#include "rocksjni/writebatchhandlerjnicallback.h"
|
2014-04-02 20:14:55 +00:00
|
|
|
#include "rocksdb/db.h"
|
2014-09-10 19:00:32 +00:00
|
|
|
#include "rocksdb/immutable_options.h"
|
2014-04-02 20:14:55 +00:00
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "rocksdb/write_batch.h"
|
2014-10-23 15:19:38 +00:00
|
|
|
#include "rocksdb/status.h"
|
2014-04-02 20:14:55 +00:00
|
|
|
#include "db/write_batch_internal.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/memtablerep.h"
|
|
|
|
#include "util/logging.h"
|
2014-09-05 00:40:41 +00:00
|
|
|
#include "util/scoped_arena_iterator.h"
|
2014-04-02 20:14:55 +00:00
|
|
|
#include "util/testharness.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: newWriteBatch
|
|
|
|
* Signature: (I)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_newWriteBatch(
|
|
|
|
JNIEnv* env, jobject jobj, jint jreserved_bytes) {
|
|
|
|
rocksdb::WriteBatch* wb = new rocksdb::WriteBatch(
|
2014-10-09 21:16:41 +00:00
|
|
|
static_cast<size_t>(jreserved_bytes));
|
2014-04-02 20:14:55 +00:00
|
|
|
|
|
|
|
rocksdb::WriteBatchJni::setHandle(env, jobj, wb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: count
|
|
|
|
* Signature: ()I
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_WriteBatch_count(JNIEnv* env, jobject jobj) {
|
|
|
|
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
return static_cast<jint>(wb->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: clear
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_clear(JNIEnv* env, jobject jobj) {
|
|
|
|
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
wb->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-10-13 08:34:52 +00:00
|
|
|
* Helper for WriteBatch put operations
|
2014-04-02 20:14:55 +00:00
|
|
|
*/
|
2014-10-13 08:34:52 +00:00
|
|
|
void write_batch_put_helper(
|
2014-04-02 20:14:55 +00:00
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle) {
|
2014-04-02 20:14:55 +00:00
|
|
|
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
|
2014-04-02 20:14:55 +00:00
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
2014-11-06 22:12:36 +00:00
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
|
|
|
|
jentry_value_len);
|
2014-10-13 08:34:52 +00:00
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
wb->Put(cf_handle, key_slice, value_slice);
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
wb->Put(key_slice, value_slice);
|
|
|
|
}
|
2014-04-02 20:14:55 +00:00
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
2014-11-06 22:12:36 +00:00
|
|
|
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
|
2014-04-02 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
2014-10-13 08:34:52 +00:00
|
|
|
* Method: put
|
2014-04-02 20:14:55 +00:00
|
|
|
* Signature: ([BI[BI)V
|
|
|
|
*/
|
2014-10-13 08:34:52 +00:00
|
|
|
void Java_org_rocksdb_WriteBatch_put___3BI_3BI(
|
2014-04-02 20:14:55 +00:00
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
|
|
|
write_batch_put_helper(env, jobj, jkey, jkey_len, jentry_value,
|
|
|
|
jentry_value_len, nullptr);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: put
|
|
|
|
* Signature: ([BI[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_put___3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
2014-11-06 22:12:36 +00:00
|
|
|
write_batch_put_helper(env, jobj, jkey, jkey_len, jentry_value,
|
|
|
|
jentry_value_len, cf_handle);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for write batch merge operations
|
|
|
|
*/
|
|
|
|
void write_batch_merge_helper(
|
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len,
|
2014-10-13 08:34:52 +00:00
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle) {
|
2014-04-02 20:14:55 +00:00
|
|
|
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
jbyte* key = env->GetByteArrayElements(jkey, nullptr);
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyte* value = env->GetByteArrayElements(jentry_value, nullptr);
|
2014-04-02 20:14:55 +00:00
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char*>(key), jkey_len);
|
2014-11-06 22:12:36 +00:00
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char*>(value),
|
|
|
|
jentry_value_len);
|
2014-10-13 08:34:52 +00:00
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
wb->Merge(cf_handle, key_slice, value_slice);
|
|
|
|
} else {
|
|
|
|
// backwards compatibility
|
|
|
|
wb->Merge(key_slice, value_slice);
|
|
|
|
}
|
2014-04-02 20:14:55 +00:00
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
2014-11-06 22:12:36 +00:00
|
|
|
env->ReleaseByteArrayElements(jentry_value, value, JNI_ABORT);
|
2014-04-02 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
2014-10-13 08:34:52 +00:00
|
|
|
* Method: merge
|
|
|
|
* Signature: ([BI[BI)V
|
2014-04-02 20:14:55 +00:00
|
|
|
*/
|
2014-10-13 08:34:52 +00:00
|
|
|
void Java_org_rocksdb_WriteBatch_merge___3BI_3BI(
|
2014-04-02 20:14:55 +00:00
|
|
|
JNIEnv* env, jobject jobj,
|
2014-10-13 08:34:52 +00:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2014-10-13 08:34:52 +00:00
|
|
|
write_batch_merge_helper(env, jobj, jkey, jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jentry_value, jentry_value_len, nullptr);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: merge
|
|
|
|
* Signature: ([BI[BIJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_merge___3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2014-10-13 08:34:52 +00:00
|
|
|
auto cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
|
|
|
|
write_batch_merge_helper(env, jobj, jkey, jkey_len,
|
2014-11-06 22:12:36 +00:00
|
|
|
jentry_value, jentry_value_len, cf_handle);
|
2014-10-13 08:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for write batch remove operations
|
|
|
|
*/
|
|
|
|
void write_batch_remove_helper(
|
|
|
|
JNIEnv* env, jobject jobj,
|
|
|
|
jbyteArray jkey, jint jkey_len,
|
|
|
|
rocksdb::ColumnFamilyHandle* cf_handle) {
|
2014-04-02 20:14:55 +00:00
|
|
|
rocksdb::WriteBatch* 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);
|
2014-10-13 08:34:52 +00:00
|
|
|
if (cf_handle != nullptr) {
|
|
|
|
wb->Delete(cf_handle, key_slice);
|
|
|
|
} else {
|
|
|
|
wb->Delete(key_slice);
|
|
|
|
}
|
2014-04-02 20:14:55 +00:00
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:34:52 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: remove
|
|
|
|
* Signature: ([BI)V
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: remove
|
|
|
|
* Signature: ([BIJ)V
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: putLogData
|
|
|
|
* Signature: ([BI)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_putLogData(
|
|
|
|
JNIEnv* env, jobject jobj, jbyteArray jblob, jint jblob_len) {
|
|
|
|
rocksdb::WriteBatch* 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);
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:19:38 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: iterate
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_iterate(
|
|
|
|
JNIEnv* env , jobject jobj, jlong handlerHandle) {
|
|
|
|
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
rocksdb::Status s = wb->Iterate(
|
|
|
|
reinterpret_cast<rocksdb::WriteBatchHandlerJniCallback*>(handlerHandle));
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
[Java] Generalize dis-own native handle and refine dispose framework.
Summary:
1. Move disOwnNativeHandle() function from RocksDB to RocksObject
to allow other RocksObject to use disOwnNativeHandle() when its
ownership of native handle has been transferred.
2. RocksObject now has an abstract implementation of dispose(),
which does the following two things. First, it checks whether
both isOwningNativeHandle() and isInitialized() return true.
If so, it will call the protected abstract function dispose0(),
which all the subclasses of RocksObject should implement. Second,
it sets nativeHandle_ = 0. This redesign ensure all subclasses
of RocksObject have the same dispose behavior.
3. All subclasses of RocksObject now should implement dispose0()
instead of dispose(), and dispose0() will be called only when
isInitialized() returns true.
Test Plan:
make rocksdbjava
make jtest
Reviewers: dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett, haobo
Reviewed By: haobo
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D18801
2014-05-29 01:16:29 +00:00
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
2014-04-02 20:14:55 +00:00
|
|
|
*/
|
[Java] Generalize dis-own native handle and refine dispose framework.
Summary:
1. Move disOwnNativeHandle() function from RocksDB to RocksObject
to allow other RocksObject to use disOwnNativeHandle() when its
ownership of native handle has been transferred.
2. RocksObject now has an abstract implementation of dispose(),
which does the following two things. First, it checks whether
both isOwningNativeHandle() and isInitialized() return true.
If so, it will call the protected abstract function dispose0(),
which all the subclasses of RocksObject should implement. Second,
it sets nativeHandle_ = 0. This redesign ensure all subclasses
of RocksObject have the same dispose behavior.
3. All subclasses of RocksObject now should implement dispose0()
instead of dispose(), and dispose0() will be called only when
isInitialized() returns true.
Test Plan:
make rocksdbjava
make jtest
Reviewers: dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett, haobo
Reviewed By: haobo
Subscribers: leveldb
Differential Revision: https://reviews.facebook.net/D18801
2014-05-29 01:16:29 +00:00
|
|
|
void Java_org_rocksdb_WriteBatch_disposeInternal(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
|
|
|
delete reinterpret_cast<rocksdb::WriteBatch*>(handle);
|
2014-04-02 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-02 00:08:41 +00:00
|
|
|
* Class: org_rocksdb_test_WriteBatchInternal
|
2014-04-02 20:14:55 +00:00
|
|
|
* Method: setSequence
|
|
|
|
* Signature: (Lorg/rocksdb/WriteBatch;J)V
|
|
|
|
*/
|
2014-11-02 00:08:41 +00:00
|
|
|
void Java_org_rocksdb_test_WriteBatchInternal_setSequence(
|
2014-04-02 20:14:55 +00:00
|
|
|
JNIEnv* env, jclass jclazz, jobject jobj, jlong jsn) {
|
|
|
|
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
rocksdb::WriteBatchInternal::SetSequence(
|
|
|
|
wb, static_cast<rocksdb::SequenceNumber>(jsn));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-02 00:08:41 +00:00
|
|
|
* Class: org_rocksdb_test_WriteBatchInternal
|
2014-04-02 20:14:55 +00:00
|
|
|
* Method: sequence
|
|
|
|
* Signature: (Lorg/rocksdb/WriteBatch;)J
|
|
|
|
*/
|
2014-11-02 00:08:41 +00:00
|
|
|
jlong Java_org_rocksdb_test_WriteBatchInternal_sequence(
|
2014-04-02 20:14:55 +00:00
|
|
|
JNIEnv* env, jclass jclazz, jobject jobj) {
|
|
|
|
rocksdb::WriteBatch* wb = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
return static_cast<jlong>(rocksdb::WriteBatchInternal::Sequence(wb));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-02 00:08:41 +00:00
|
|
|
* Class: org_rocksdb_test_WriteBatchInternal
|
2014-04-02 20:14:55 +00:00
|
|
|
* Method: append
|
|
|
|
* Signature: (Lorg/rocksdb/WriteBatch;Lorg/rocksdb/WriteBatch;)V
|
|
|
|
*/
|
2014-11-02 00:08:41 +00:00
|
|
|
void Java_org_rocksdb_test_WriteBatchInternal_append(
|
2014-04-02 20:14:55 +00:00
|
|
|
JNIEnv* env, jclass jclazz, jobject jwb1, jobject jwb2) {
|
|
|
|
rocksdb::WriteBatch* wb1 = rocksdb::WriteBatchJni::getHandle(env, jwb1);
|
|
|
|
assert(wb1 != nullptr);
|
|
|
|
rocksdb::WriteBatch* wb2 = rocksdb::WriteBatchJni::getHandle(env, jwb2);
|
|
|
|
assert(wb2 != nullptr);
|
|
|
|
|
|
|
|
rocksdb::WriteBatchInternal::Append(wb1, wb2);
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:19:38 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch_Handler
|
|
|
|
* Method: createNewHandler0
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_00024Handler_createNewHandler0(
|
|
|
|
JNIEnv* env, jobject jobj) {
|
|
|
|
const rocksdb::WriteBatchHandlerJniCallback* h =
|
|
|
|
new rocksdb::WriteBatchHandlerJniCallback(env, jobj);
|
|
|
|
rocksdb::WriteBatchHandlerJni::setHandle(env, jobj, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch_Handler
|
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_00024Handler_disposeInternal(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
2014-11-05 18:56:45 +00:00
|
|
|
delete reinterpret_cast<rocksdb::WriteBatchHandlerJniCallback*>(handle);
|
2014-10-23 15:19:38 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 20:14:55 +00:00
|
|
|
/*
|
2014-11-02 00:08:41 +00:00
|
|
|
* Class: org_rocksdb_test_WriteBatchTest
|
2014-04-02 20:14:55 +00:00
|
|
|
* Method: getContents
|
|
|
|
* Signature: (Lorg/rocksdb/WriteBatch;)[B
|
|
|
|
*/
|
2014-11-02 00:08:41 +00:00
|
|
|
jbyteArray Java_org_rocksdb_test_WriteBatchTest_getContents(
|
2014-04-02 20:14:55 +00:00
|
|
|
JNIEnv* env, jclass jclazz, jobject jobj) {
|
|
|
|
rocksdb::WriteBatch* b = rocksdb::WriteBatchJni::getHandle(env, jobj);
|
|
|
|
assert(b != nullptr);
|
|
|
|
|
|
|
|
// todo: Currently the following code is directly copied from
|
|
|
|
// db/write_bench_test.cc. It could be implemented in java once
|
|
|
|
// all the necessary components can be accessed via jni api.
|
|
|
|
|
|
|
|
rocksdb::InternalKeyComparator cmp(rocksdb::BytewiseComparator());
|
|
|
|
auto factory = std::make_shared<rocksdb::SkipListFactory>();
|
|
|
|
rocksdb::Options options;
|
|
|
|
options.memtable_factory = factory;
|
2014-09-10 19:00:32 +00:00
|
|
|
rocksdb::MemTable* mem = new rocksdb::MemTable(
|
|
|
|
cmp, rocksdb::ImmutableCFOptions(options),
|
2014-10-27 20:32:21 +00:00
|
|
|
rocksdb::MutableCFOptions(options,
|
|
|
|
rocksdb::ImmutableCFOptions(options)));
|
2014-04-02 20:14:55 +00:00
|
|
|
mem->Ref();
|
|
|
|
std::string state;
|
2014-11-18 18:20:10 +00:00
|
|
|
rocksdb::ColumnFamilyMemTablesDefault cf_mems_default(mem);
|
2014-04-08 16:52:26 +00:00
|
|
|
rocksdb::Status s =
|
|
|
|
rocksdb::WriteBatchInternal::InsertInto(b, &cf_mems_default);
|
2014-04-02 20:14:55 +00:00
|
|
|
int count = 0;
|
2014-09-10 19:00:32 +00:00
|
|
|
rocksdb::Arena arena;
|
|
|
|
rocksdb::ScopedArenaIterator iter(mem->NewIterator(
|
|
|
|
rocksdb::ReadOptions(), &arena));
|
2014-04-02 20:14:55 +00:00
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
rocksdb::ParsedInternalKey ikey;
|
|
|
|
memset(reinterpret_cast<void*>(&ikey), 0, sizeof(ikey));
|
|
|
|
ASSERT_TRUE(rocksdb::ParseInternalKey(iter->key(), &ikey));
|
|
|
|
switch (ikey.type) {
|
|
|
|
case rocksdb::kTypeValue:
|
|
|
|
state.append("Put(");
|
|
|
|
state.append(ikey.user_key.ToString());
|
|
|
|
state.append(", ");
|
|
|
|
state.append(iter->value().ToString());
|
|
|
|
state.append(")");
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
case rocksdb::kTypeMerge:
|
|
|
|
state.append("Merge(");
|
|
|
|
state.append(ikey.user_key.ToString());
|
|
|
|
state.append(", ");
|
|
|
|
state.append(iter->value().ToString());
|
|
|
|
state.append(")");
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
case rocksdb::kTypeDeletion:
|
|
|
|
state.append("Delete(");
|
|
|
|
state.append(ikey.user_key.ToString());
|
|
|
|
state.append(")");
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state.append("@");
|
|
|
|
state.append(rocksdb::NumberToString(ikey.sequence));
|
|
|
|
}
|
|
|
|
if (!s.ok()) {
|
|
|
|
state.append(s.ToString());
|
|
|
|
} else if (count != rocksdb::WriteBatchInternal::Count(b)) {
|
|
|
|
state.append("CountMismatch()");
|
|
|
|
}
|
|
|
|
delete mem->Unref();
|
|
|
|
|
2014-11-11 21:47:22 +00:00
|
|
|
jbyteArray jstate = env->NewByteArray(static_cast<jsize>(state.size()));
|
|
|
|
env->SetByteArrayRegion(jstate, 0, static_cast<jsize>(state.size()),
|
|
|
|
reinterpret_cast<const jbyte*>(state.c_str()));
|
2014-04-02 20:14:55 +00:00
|
|
|
|
|
|
|
return jstate;
|
|
|
|
}
|