2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2014-08-03 22:11:44 +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 callback "bridge" between Java and C++ for
|
|
|
|
// rocksdb::Comparator.
|
|
|
|
|
|
|
|
#include "rocksjni/comparatorjnicallback.h"
|
2014-08-21 22:55:51 +02:00
|
|
|
#include "rocksjni/portal.h"
|
2014-08-03 22:11:44 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
BaseComparatorJniCallback::BaseComparatorJniCallback(
|
2014-08-21 22:55:51 +02:00
|
|
|
JNIEnv* env, jobject jComparator,
|
2014-10-06 19:35:53 +02:00
|
|
|
const ComparatorJniCallbackOptions* copt)
|
|
|
|
: mtx_compare(new port::Mutex(copt->use_adaptive_mutex)),
|
|
|
|
mtx_findShortestSeparator(new port::Mutex(copt->use_adaptive_mutex)) {
|
2014-08-03 22:11:44 +02:00
|
|
|
// Note: Comparator methods may be accessed by multiple threads,
|
|
|
|
// so we ref the jvm not the env
|
2015-09-01 03:36:17 +02:00
|
|
|
const jint rs __attribute__((unused)) = env->GetJavaVM(&m_jvm);
|
2014-08-03 22:11:44 +02:00
|
|
|
assert(rs == JNI_OK);
|
|
|
|
|
|
|
|
// Note: we want to access the Java Comparator instance
|
|
|
|
// across multiple method calls, so we create a global ref
|
|
|
|
m_jComparator = env->NewGlobalRef(jComparator);
|
|
|
|
|
|
|
|
// Note: The name of a Comparator will not change during it's lifetime,
|
|
|
|
// so we cache it in a global var
|
|
|
|
jmethodID jNameMethodId = AbstractComparatorJni::getNameMethodId(env);
|
|
|
|
jstring jsName = (jstring)env->CallObjectMethod(m_jComparator, jNameMethodId);
|
2014-08-21 22:55:51 +02:00
|
|
|
m_name = JniUtil::copyString(env, jsName); // also releases jsName
|
2014-08-03 22:11:44 +02:00
|
|
|
|
|
|
|
m_jCompareMethodId = AbstractComparatorJni::getCompareMethodId(env);
|
2014-08-21 22:55:51 +02:00
|
|
|
m_jFindShortestSeparatorMethodId =
|
|
|
|
AbstractComparatorJni::getFindShortestSeparatorMethodId(env);
|
|
|
|
m_jFindShortSuccessorMethodId =
|
|
|
|
AbstractComparatorJni::getFindShortSuccessorMethodId(env);
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach/Get a JNIEnv for the current native thread
|
|
|
|
*/
|
|
|
|
JNIEnv* BaseComparatorJniCallback::getJniEnv() const {
|
|
|
|
JNIEnv *env;
|
2015-09-01 03:36:17 +02:00
|
|
|
jint rs __attribute__((unused)) =
|
|
|
|
m_jvm->AttachCurrentThread(reinterpret_cast<void**>(&env), NULL);
|
2014-08-03 22:11:44 +02:00
|
|
|
assert(rs == JNI_OK);
|
|
|
|
return env;
|
2014-08-21 22:55:51 +02:00
|
|
|
}
|
2014-08-03 22:11:44 +02:00
|
|
|
|
|
|
|
const char* BaseComparatorJniCallback::Name() const {
|
|
|
|
return m_name.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BaseComparatorJniCallback::Compare(const Slice& a, const Slice& b) const {
|
|
|
|
JNIEnv* m_env = getJniEnv();
|
|
|
|
|
2014-10-06 19:35:53 +02:00
|
|
|
// TODO(adamretter): slice objects can potentially be cached using thread
|
|
|
|
// local variables to avoid locking. Could make this configurable depending on
|
|
|
|
// performance.
|
|
|
|
mtx_compare->Lock();
|
2014-08-15 14:34:10 +02:00
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
AbstractSliceJni::setHandle(m_env, m_jSliceA, &a);
|
|
|
|
AbstractSliceJni::setHandle(m_env, m_jSliceB, &b);
|
2014-08-21 22:55:51 +02:00
|
|
|
jint result =
|
|
|
|
m_env->CallIntMethod(m_jComparator, m_jCompareMethodId, m_jSliceA,
|
|
|
|
m_jSliceB);
|
2014-08-03 22:11:44 +02:00
|
|
|
|
2014-10-06 19:35:53 +02:00
|
|
|
mtx_compare->Unlock();
|
2014-08-15 14:34:10 +02:00
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
m_jvm->DetachCurrentThread();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
void BaseComparatorJniCallback::FindShortestSeparator(
|
|
|
|
std::string* start, const Slice& limit) const {
|
2014-08-03 22:11:44 +02:00
|
|
|
if (start == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEnv* m_env = getJniEnv();
|
|
|
|
|
|
|
|
const char* startUtf = start->c_str();
|
|
|
|
jstring jsStart = m_env->NewStringUTF(startUtf);
|
|
|
|
|
2014-10-06 19:35:53 +02:00
|
|
|
// TODO(adamretter): slice object can potentially be cached using thread local
|
|
|
|
// variable to avoid locking. Could make this configurable depending on
|
|
|
|
// performance.
|
|
|
|
mtx_findShortestSeparator->Lock();
|
2014-08-03 22:11:44 +02:00
|
|
|
|
2014-08-15 14:34:10 +02:00
|
|
|
AbstractSliceJni::setHandle(m_env, m_jSliceLimit, &limit);
|
2014-08-21 22:55:51 +02:00
|
|
|
jstring jsResultStart =
|
|
|
|
(jstring)m_env->CallObjectMethod(m_jComparator,
|
|
|
|
m_jFindShortestSeparatorMethodId, jsStart, m_jSliceLimit);
|
2014-08-03 22:11:44 +02:00
|
|
|
|
2014-10-06 19:35:53 +02:00
|
|
|
mtx_findShortestSeparator->Unlock();
|
2014-08-15 14:34:10 +02:00
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
m_env->DeleteLocalRef(jsStart);
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
if (jsResultStart != nullptr) {
|
|
|
|
// update start with result
|
|
|
|
*start =
|
|
|
|
JniUtil::copyString(m_env, jsResultStart); // also releases jsResultStart
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_jvm->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseComparatorJniCallback::FindShortSuccessor(std::string* key) const {
|
|
|
|
if (key == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEnv* m_env = getJniEnv();
|
|
|
|
|
|
|
|
const char* keyUtf = key->c_str();
|
|
|
|
jstring jsKey = m_env->NewStringUTF(keyUtf);
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
jstring jsResultKey =
|
|
|
|
(jstring)m_env->CallObjectMethod(m_jComparator,
|
|
|
|
m_jFindShortSuccessorMethodId, jsKey);
|
2014-08-03 22:11:44 +02:00
|
|
|
|
|
|
|
m_env->DeleteLocalRef(jsKey);
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
if (jsResultKey != nullptr) {
|
2014-10-06 19:35:53 +02:00
|
|
|
// updates key with result, also releases jsResultKey.
|
|
|
|
*key = JniUtil::copyString(m_env, jsResultKey);
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_jvm->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseComparatorJniCallback::~BaseComparatorJniCallback() {
|
|
|
|
JNIEnv* m_env = getJniEnv();
|
2014-08-21 22:55:51 +02:00
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
m_env->DeleteGlobalRef(m_jComparator);
|
|
|
|
|
|
|
|
// Note: do not need to explicitly detach, as this function is effectively
|
|
|
|
// called from the Java class's disposeInternal method, and so already
|
2014-08-21 22:55:51 +02:00
|
|
|
// has an attached thread, getJniEnv above is just a no-op Attach to get
|
|
|
|
// the env jvm->DetachCurrentThread();
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ComparatorJniCallback::ComparatorJniCallback(
|
2014-08-21 22:55:51 +02:00
|
|
|
JNIEnv* env, jobject jComparator,
|
|
|
|
const ComparatorJniCallbackOptions* copt) :
|
|
|
|
BaseComparatorJniCallback(env, jComparator, copt) {
|
2014-08-03 22:11:44 +02:00
|
|
|
m_jSliceA = env->NewGlobalRef(SliceJni::construct0(env));
|
|
|
|
m_jSliceB = env->NewGlobalRef(SliceJni::construct0(env));
|
|
|
|
m_jSliceLimit = env->NewGlobalRef(SliceJni::construct0(env));
|
|
|
|
}
|
|
|
|
|
2014-10-06 19:35:53 +02:00
|
|
|
ComparatorJniCallback::~ComparatorJniCallback() {
|
|
|
|
JNIEnv* m_env = getJniEnv();
|
|
|
|
m_env->DeleteGlobalRef(m_jSliceA);
|
|
|
|
m_env->DeleteGlobalRef(m_jSliceB);
|
|
|
|
m_env->DeleteGlobalRef(m_jSliceLimit);
|
|
|
|
}
|
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
DirectComparatorJniCallback::DirectComparatorJniCallback(
|
2014-08-21 22:55:51 +02:00
|
|
|
JNIEnv* env, jobject jComparator,
|
|
|
|
const ComparatorJniCallbackOptions* copt) :
|
|
|
|
BaseComparatorJniCallback(env, jComparator, copt) {
|
2014-08-03 22:11:44 +02:00
|
|
|
m_jSliceA = env->NewGlobalRef(DirectSliceJni::construct0(env));
|
|
|
|
m_jSliceB = env->NewGlobalRef(DirectSliceJni::construct0(env));
|
|
|
|
m_jSliceLimit = env->NewGlobalRef(DirectSliceJni::construct0(env));
|
|
|
|
}
|
2014-10-06 19:35:53 +02:00
|
|
|
|
|
|
|
DirectComparatorJniCallback::~DirectComparatorJniCallback() {
|
|
|
|
JNIEnv* m_env = getJniEnv();
|
|
|
|
m_env->DeleteGlobalRef(m_jSliceA);
|
|
|
|
m_env->DeleteGlobalRef(m_jSliceB);
|
|
|
|
m_env->DeleteGlobalRef(m_jSliceLimit);
|
|
|
|
}
|
2014-08-03 22:11:44 +02:00
|
|
|
} // namespace rocksdb
|