parent
00211f9c5b
commit
8fb4751d50
@ -0,0 +1,98 @@ |
||||
// 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( |
||||
JNIEnv* env, jobject jWriteBatchHandler) { |
||||
|
||||
// Note: WriteBatchHandler methods may be accessed by multiple threads,
|
||||
// so we ref the jvm not the env
|
||||
const jint rs = env->GetJavaVM(&m_jvm); |
||||
assert(rs == JNI_OK); |
||||
|
||||
// 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); |
||||
} |
||||
|
||||
/**
|
||||
* Attach/Get a JNIEnv for the current native thread |
||||
*/ |
||||
JNIEnv* WriteBatchHandlerJniCallback::getJniEnv() const { |
||||
JNIEnv *env; |
||||
jint rs = m_jvm->AttachCurrentThread(reinterpret_cast<void **>(&env), NULL); |
||||
assert(rs == JNI_OK); |
||||
return env; |
||||
} |
||||
|
||||
void WriteBatchHandlerJniCallback::Put(const Slice& key, const Slice& value) { |
||||
getJniEnv()->CallVoidMethod( |
||||
m_jWriteBatchHandler, |
||||
m_jPutMethodId, |
||||
sliceToJArray(key), |
||||
sliceToJArray(value)); |
||||
} |
||||
|
||||
void WriteBatchHandlerJniCallback::Merge(const Slice& key, const Slice& value) { |
||||
getJniEnv()->CallVoidMethod( |
||||
m_jWriteBatchHandler, |
||||
m_jMergeMethodId, |
||||
sliceToJArray(key), |
||||
sliceToJArray(value)); |
||||
} |
||||
|
||||
void WriteBatchHandlerJniCallback::Delete(const Slice& key) { |
||||
getJniEnv()->CallVoidMethod( |
||||
m_jWriteBatchHandler, |
||||
m_jDeleteMethodId, |
||||
sliceToJArray(key)); |
||||
} |
||||
|
||||
void WriteBatchHandlerJniCallback::LogData(const Slice& blob) { |
||||
getJniEnv()->CallVoidMethod( |
||||
m_jWriteBatchHandler, |
||||
m_jLogDataMethodId, |
||||
sliceToJArray(blob)); |
||||
} |
||||
|
||||
bool WriteBatchHandlerJniCallback::Continue() { |
||||
jboolean jContinue = getJniEnv()->CallBooleanMethod( |
||||
m_jWriteBatchHandler, |
||||
m_jContinueMethodId); |
||||
|
||||
return static_cast<bool>(jContinue == JNI_TRUE); |
||||
} |
||||
|
||||
jbyteArray WriteBatchHandlerJniCallback::sliceToJArray(const Slice& s) { |
||||
jbyteArray ja = getJniEnv()->NewByteArray(s.size()); |
||||
getJniEnv()->SetByteArrayRegion( |
||||
ja, 0, s.size(), |
||||
reinterpret_cast<const jbyte*>(s.data())); |
||||
return ja; |
||||
} |
||||
|
||||
WriteBatchHandlerJniCallback::~WriteBatchHandlerJniCallback() { |
||||
JNIEnv* m_env = getJniEnv(); |
||||
|
||||
m_env->DeleteGlobalRef(m_jWriteBatchHandler); |
||||
|
||||
// Note: do not need to explicitly detach, as this function is effectively
|
||||
// called from the Java class's disposeInternal method, and so already
|
||||
// has an attached thread, getJniEnv above is just a no-op Attach to get
|
||||
// the env jvm->DetachCurrentThread();
|
||||
} |
||||
} // namespace rocksdb
|
@ -0,0 +1,47 @@ |
||||
// 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::WriteBatch::Handler.
|
||||
|
||||
#ifndef JAVA_ROCKSJNI_WRITEBATCHHANDLERJNICALLBACK_H_ |
||||
#define JAVA_ROCKSJNI_WRITEBATCHHANDLERJNICALLBACK_H_ |
||||
|
||||
#include <jni.h> |
||||
#include "rocksdb/write_batch.h" |
||||
|
||||
namespace rocksdb { |
||||
/**
|
||||
* This class acts as a bridge between C++ |
||||
* and Java. The methods in this class will be |
||||
* called back from the RocksDB storage engine (C++) |
||||
* we then callback to the appropriate Java method |
||||
* this enables Write Batch Handlers to be implemented in Java. |
||||
*/ |
||||
class WriteBatchHandlerJniCallback : public WriteBatch::Handler { |
||||
public: |
||||
WriteBatchHandlerJniCallback( |
||||
JNIEnv* env, jobject jWriteBackHandler); |
||||
~WriteBatchHandlerJniCallback(); |
||||
void Put(const Slice& key, const Slice& value); |
||||
void Merge(const Slice& key, const Slice& value); |
||||
void Delete(const Slice& key); |
||||
void LogData(const Slice& blob); |
||||
bool Continue(); |
||||
|
||||
private: |
||||
JavaVM* m_jvm; |
||||
jobject m_jWriteBatchHandler; |
||||
JNIEnv* getJniEnv() const; |
||||
jbyteArray sliceToJArray(const Slice& s); |
||||
jmethodID m_jPutMethodId; |
||||
jmethodID m_jMergeMethodId; |
||||
jmethodID m_jDeleteMethodId; |
||||
jmethodID m_jLogDataMethodId; |
||||
jmethodID m_jContinueMethodId; |
||||
}; |
||||
} // namespace rocksdb
|
||||
|
||||
#endif // JAVA_ROCKSJNI_WRITEBATCHHANDLERJNICALLBACK_H_
|
Loading…
Reference in new issue