2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2014-04-02 22:14:55 +02:00
|
|
|
// 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>
|
|
|
|
|
2016-06-21 03:01:03 +02:00
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
2014-04-02 22:14:55 +02:00
|
|
|
#include "include/org_rocksdb_WriteBatch.h"
|
2014-10-23 17:19:38 +02:00
|
|
|
#include "include/org_rocksdb_WriteBatch_Handler.h"
|
2014-04-02 22:14:55 +02:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
2016-06-21 03:01:03 +02:00
|
|
|
#include "rocksdb/immutable_options.h"
|
2014-04-02 22:14:55 +02:00
|
|
|
#include "rocksdb/memtablerep.h"
|
2016-06-21 03:01:03 +02:00
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "rocksdb/write_batch.h"
|
|
|
|
#include "rocksdb/write_buffer_manager.h"
|
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
#include "rocksjni/writebatchhandlerjnicallback.h"
|
2015-10-13 00:06:38 +02:00
|
|
|
#include "table/scoped_arena_iterator.h"
|
2014-04-02 22:14:55 +02:00
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: newWriteBatch
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: (I)J
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2016-01-20 18:05:41 +01:00
|
|
|
jlong Java_org_rocksdb_WriteBatch_newWriteBatch(
|
|
|
|
JNIEnv* env, jclass jcls, jint jreserved_bytes) {
|
2014-04-02 22:14:55 +02:00
|
|
|
rocksdb::WriteBatch* wb = new rocksdb::WriteBatch(
|
2014-10-09 23:16:41 +02:00
|
|
|
static_cast<size_t>(jreserved_bytes));
|
2016-01-20 18:05:41 +01:00
|
|
|
return reinterpret_cast<jlong>(wb);
|
2014-04-02 22:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
2015-01-03 19:36:59 +01:00
|
|
|
* Method: count0
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J)I
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
jint Java_org_rocksdb_WriteBatch_count0(JNIEnv* env, jobject jobj,
|
|
|
|
jlong jwb_handle) {
|
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
return static_cast<jint>(wb->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
2015-01-03 19:36:59 +01:00
|
|
|
* Method: clear0
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
void Java_org_rocksdb_WriteBatch_clear0(JNIEnv* env, jobject jobj,
|
|
|
|
jlong jwb_handle) {
|
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
wb->Clear();
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:51:14 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: setSavePoint0
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_setSavePoint0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle) {
|
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
wb->SetSavePoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: rollbackToSavePoint0
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_rollbackToSavePoint0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle) {
|
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
|
|
|
assert(wb != nullptr);
|
|
|
|
|
|
|
|
auto s = wb->RollbackToSavePoint();
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
2014-10-13 10:34:52 +02:00
|
|
|
* Method: put
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J[BI[BI)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
void Java_org_rocksdb_WriteBatch_put__J_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle,
|
2014-04-02 22:14:55 +02:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 23:12:36 +01:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2016-02-01 21:00:40 +01:00
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2015-01-03 18:52:17 +01:00
|
|
|
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);
|
2014-10-13 10:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: put
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J[BI[BIJ)V
|
2014-10-13 10:34:52 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
void Java_org_rocksdb_WriteBatch_put__J_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle,
|
2014-10-13 10:34:52 +02:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 23:12:36 +01:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2016-02-01 21:00:40 +01:00
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
assert(wb != nullptr);
|
2015-01-03 18:52:17 +01:00
|
|
|
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,
|
2014-11-06 23:12:36 +01:00
|
|
|
jentry_value_len);
|
2014-04-02 22:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
2014-10-13 10:34:52 +02:00
|
|
|
* Method: merge
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J[BI[BI)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
void Java_org_rocksdb_WriteBatch_merge__J_3BI_3BI(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle,
|
2014-10-13 10:34:52 +02:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 23:12:36 +01:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len) {
|
2016-02-01 21:00:40 +01:00
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2015-01-03 18:52:17 +01:00
|
|
|
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);
|
2014-10-13 10:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: merge
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J[BI[BIJ)V
|
2014-10-13 10:34:52 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
void Java_org_rocksdb_WriteBatch_merge__J_3BI_3BIJ(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle,
|
2014-10-13 10:34:52 +02:00
|
|
|
jbyteArray jkey, jint jkey_len,
|
2014-11-06 23:12:36 +01:00
|
|
|
jbyteArray jentry_value, jint jentry_value_len, jlong jcf_handle) {
|
2016-02-01 21:00:40 +01:00
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
assert(wb != nullptr);
|
2015-01-03 18:52:17 +01:00
|
|
|
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);
|
2014-04-02 22:14:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 10:34:52 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: remove
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J[BI)V
|
2014-10-13 10:34:52 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
void Java_org_rocksdb_WriteBatch_remove__J_3BI(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle,
|
2014-10-13 10:34:52 +02:00
|
|
|
jbyteArray jkey, jint jkey_len) {
|
2016-02-01 21:00:40 +01:00
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2015-01-03 18:52:17 +01:00
|
|
|
assert(wb != nullptr);
|
|
|
|
auto remove = [&wb] (rocksdb::Slice key) {
|
|
|
|
wb->Delete(key);
|
|
|
|
};
|
|
|
|
rocksdb::JniUtil::k_op(remove, env, jobj, jkey, jkey_len);
|
2014-10-13 10:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: remove
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J[BIJ)V
|
2014-10-13 10:34:52 +02:00
|
|
|
*/
|
2016-02-01 21:00:40 +01:00
|
|
|
void Java_org_rocksdb_WriteBatch_remove__J_3BIJ(
|
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle,
|
2014-10-13 10:34:52 +02:00
|
|
|
jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
|
2016-02-01 21:00:40 +01:00
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2015-01-03 18:52:17 +01:00
|
|
|
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);
|
2014-10-13 10:34:52 +02:00
|
|
|
}
|
|
|
|
|
2014-04-02 22:14:55 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: putLogData
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (J[BI)V
|
2014-04-02 22:14:55 +02:00
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_putLogData(
|
2016-02-01 21:00:40 +01:00
|
|
|
JNIEnv* env, jobject jobj, jlong jwb_handle, jbyteArray jblob,
|
|
|
|
jint jblob_len) {
|
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
assert(wb != nullptr);
|
2015-01-03 18:52:17 +01:00
|
|
|
auto putLogData = [&wb] (rocksdb::Slice blob) {
|
|
|
|
wb->PutLogData(blob);
|
|
|
|
};
|
|
|
|
rocksdb::JniUtil::k_op(putLogData, env, jobj, jblob, jblob_len);
|
2014-04-02 22:14:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:19:38 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch
|
|
|
|
* Method: iterate
|
2016-02-01 21:00:40 +01:00
|
|
|
* Signature: (JJ)V
|
2014-10-23 17:19:38 +02:00
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_WriteBatch_iterate(
|
2016-02-01 21:00:40 +01:00
|
|
|
JNIEnv* env , jobject jobj, jlong jwb_handle, jlong handlerHandle) {
|
|
|
|
auto* wb = reinterpret_cast<rocksdb::WriteBatch*>(jwb_handle);
|
2014-10-23 17:19:38 +02:00
|
|
|
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 22:14:55 +02: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 03:16:29 +02:00
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
2014-04-02 22:14:55 +02: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 03:16:29 +02:00
|
|
|
void Java_org_rocksdb_WriteBatch_disposeInternal(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
|
|
|
delete reinterpret_cast<rocksdb::WriteBatch*>(handle);
|
2014-04-02 22:14:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:19:38 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_WriteBatch_Handler
|
|
|
|
* Method: createNewHandler0
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: ()J
|
2014-10-23 17:19:38 +02:00
|
|
|
*/
|
2016-01-20 18:05:41 +01:00
|
|
|
jlong Java_org_rocksdb_WriteBatch_00024Handler_createNewHandler0(
|
2014-10-23 17:19:38 +02:00
|
|
|
JNIEnv* env, jobject jobj) {
|
|
|
|
const rocksdb::WriteBatchHandlerJniCallback* h =
|
|
|
|
new rocksdb::WriteBatchHandlerJniCallback(env, jobj);
|
2016-01-20 18:05:41 +01:00
|
|
|
return reinterpret_cast<jlong>(h);
|
2014-10-23 17:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 19:56:45 +01:00
|
|
|
delete reinterpret_cast<rocksdb::WriteBatchHandlerJniCallback*>(handle);
|
2014-10-23 17:19:38 +02:00
|
|
|
}
|