2014-10-23 17:19:38 +02: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 callback "bridge" between Java and C++ for
|
|
|
|
// rocksdb::Comparator.
|
|
|
|
|
|
|
|
#include "rocksjni/writebatchhandlerjnicallback.h"
|
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
WriteBatchHandlerJniCallback::WriteBatchHandlerJniCallback(
|
2014-11-05 19:56:45 +01:00
|
|
|
JNIEnv* env, jobject jWriteBatchHandler)
|
|
|
|
: m_env(env) {
|
2014-10-23 17:19:38 +02:00
|
|
|
|
|
|
|
// Note: we want to access the Java WriteBatchHandler instance
|
|
|
|
// across multiple method calls, so we create a global ref
|
|
|
|
m_jWriteBatchHandler = env->NewGlobalRef(jWriteBatchHandler);
|
|
|
|
|
|
|
|
m_jPutMethodId = WriteBatchHandlerJni::getPutMethodId(env);
|
|
|
|
m_jMergeMethodId = WriteBatchHandlerJni::getMergeMethodId(env);
|
|
|
|
m_jDeleteMethodId = WriteBatchHandlerJni::getDeleteMethodId(env);
|
|
|
|
m_jLogDataMethodId = WriteBatchHandlerJni::getLogDataMethodId(env);
|
|
|
|
m_jContinueMethodId = WriteBatchHandlerJni::getContinueMethodId(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBatchHandlerJniCallback::Put(const Slice& key, const Slice& value) {
|
2014-11-05 19:56:45 +01:00
|
|
|
const jbyteArray j_key = sliceToJArray(key);
|
|
|
|
const jbyteArray j_value = sliceToJArray(value);
|
|
|
|
|
|
|
|
m_env->CallVoidMethod(
|
2014-10-23 17:19:38 +02:00
|
|
|
m_jWriteBatchHandler,
|
|
|
|
m_jPutMethodId,
|
2014-11-05 19:56:45 +01:00
|
|
|
j_key,
|
|
|
|
j_value);
|
|
|
|
|
|
|
|
m_env->DeleteLocalRef(j_value);
|
|
|
|
m_env->DeleteLocalRef(j_key);
|
2014-10-23 17:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBatchHandlerJniCallback::Merge(const Slice& key, const Slice& value) {
|
2014-11-05 19:56:45 +01:00
|
|
|
const jbyteArray j_key = sliceToJArray(key);
|
|
|
|
const jbyteArray j_value = sliceToJArray(value);
|
|
|
|
|
|
|
|
m_env->CallVoidMethod(
|
2014-10-23 17:19:38 +02:00
|
|
|
m_jWriteBatchHandler,
|
|
|
|
m_jMergeMethodId,
|
2014-11-05 19:56:45 +01:00
|
|
|
j_key,
|
|
|
|
j_value);
|
|
|
|
|
|
|
|
m_env->DeleteLocalRef(j_value);
|
|
|
|
m_env->DeleteLocalRef(j_key);
|
2014-10-23 17:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBatchHandlerJniCallback::Delete(const Slice& key) {
|
2014-11-05 19:56:45 +01:00
|
|
|
const jbyteArray j_key = sliceToJArray(key);
|
|
|
|
|
|
|
|
m_env->CallVoidMethod(
|
2014-10-23 17:19:38 +02:00
|
|
|
m_jWriteBatchHandler,
|
|
|
|
m_jDeleteMethodId,
|
2014-11-05 19:56:45 +01:00
|
|
|
j_key);
|
|
|
|
|
|
|
|
m_env->DeleteLocalRef(j_key);
|
2014-10-23 17:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBatchHandlerJniCallback::LogData(const Slice& blob) {
|
2014-11-05 19:56:45 +01:00
|
|
|
const jbyteArray j_blob = sliceToJArray(blob);
|
|
|
|
|
|
|
|
m_env->CallVoidMethod(
|
2014-10-23 17:19:38 +02:00
|
|
|
m_jWriteBatchHandler,
|
|
|
|
m_jLogDataMethodId,
|
2014-11-05 19:56:45 +01:00
|
|
|
j_blob);
|
|
|
|
|
|
|
|
m_env->DeleteLocalRef(j_blob);
|
2014-10-23 17:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteBatchHandlerJniCallback::Continue() {
|
2014-11-05 19:56:45 +01:00
|
|
|
jboolean jContinue = m_env->CallBooleanMethod(
|
2014-10-23 17:19:38 +02:00
|
|
|
m_jWriteBatchHandler,
|
|
|
|
m_jContinueMethodId);
|
|
|
|
|
|
|
|
return static_cast<bool>(jContinue == JNI_TRUE);
|
|
|
|
}
|
|
|
|
|
2014-11-05 19:56:45 +01:00
|
|
|
/*
|
|
|
|
* Creates a Java Byte Array from the data in a Slice
|
|
|
|
*
|
|
|
|
* When calling this function
|
|
|
|
* you must remember to call env->DeleteLocalRef
|
|
|
|
* on the result after you have finished with it
|
|
|
|
*/
|
2014-10-23 17:19:38 +02:00
|
|
|
jbyteArray WriteBatchHandlerJniCallback::sliceToJArray(const Slice& s) {
|
2014-11-13 20:39:30 +01:00
|
|
|
jbyteArray ja = m_env->NewByteArray(static_cast<jsize>(s.size()));
|
2014-11-05 19:56:45 +01:00
|
|
|
m_env->SetByteArrayRegion(
|
2014-11-13 20:39:30 +01:00
|
|
|
ja, 0, static_cast<jsize>(s.size()),
|
2014-10-23 17:19:38 +02:00
|
|
|
reinterpret_cast<const jbyte*>(s.data()));
|
|
|
|
return ja;
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteBatchHandlerJniCallback::~WriteBatchHandlerJniCallback() {
|
|
|
|
m_env->DeleteGlobalRef(m_jWriteBatchHandler);
|
|
|
|
}
|
|
|
|
} // namespace rocksdb
|