2014-08-03 22:11:44 +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 and rocksdb::DirectComparator.
|
|
|
|
|
|
|
|
#ifndef JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
|
|
|
|
#define JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
|
|
|
|
|
|
|
|
#include <jni.h>
|
2014-08-21 22:55:51 +02:00
|
|
|
#include <string>
|
2014-08-03 22:11:44 +02:00
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/slice.h"
|
2014-08-15 14:34:10 +02:00
|
|
|
#include "port/port.h"
|
2014-08-03 22:11:44 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
2014-08-15 14:34:10 +02:00
|
|
|
|
|
|
|
struct ComparatorJniCallbackOptions {
|
|
|
|
// Use adaptive mutex, which spins in the user space before resorting
|
|
|
|
// to kernel. This could reduce context switch when the mutex is not
|
|
|
|
// heavily contended. However, if the mutex is hot, we could end up
|
|
|
|
// wasting spin time.
|
|
|
|
// Default: false
|
|
|
|
bool use_adaptive_mutex;
|
|
|
|
|
|
|
|
ComparatorJniCallbackOptions() : use_adaptive_mutex(false) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 Comparators to be implemented in Java.
|
|
|
|
*
|
|
|
|
* The design of this Comparator caches the Java Slice
|
|
|
|
* objects that are used in the compare and findShortestSeparator
|
|
|
|
* method callbacks. Instead of creating new objects for each callback
|
|
|
|
* of those functions, by reuse via setHandle we are a lot
|
|
|
|
* faster; Unfortunately this means that we have to
|
2014-10-06 19:35:53 +02:00
|
|
|
* introduce independent locking in regions of each of those methods
|
|
|
|
* via the mutexs mtx_compare and mtx_findShortestSeparator respectively
|
2014-08-15 14:34:10 +02:00
|
|
|
*/
|
2014-08-03 22:11:44 +02:00
|
|
|
class BaseComparatorJniCallback : public Comparator {
|
2014-08-21 22:55:51 +02:00
|
|
|
public:
|
|
|
|
BaseComparatorJniCallback(
|
|
|
|
JNIEnv* env, jobject jComparator,
|
|
|
|
const ComparatorJniCallbackOptions* copt);
|
2014-08-03 22:11:44 +02:00
|
|
|
virtual ~BaseComparatorJniCallback();
|
|
|
|
virtual const char* Name() const;
|
|
|
|
virtual int Compare(const Slice& a, const Slice& b) const;
|
2014-08-21 22:55:51 +02:00
|
|
|
virtual void FindShortestSeparator(
|
|
|
|
std::string* start, const Slice& limit) const;
|
2014-08-03 22:11:44 +02:00
|
|
|
virtual void FindShortSuccessor(std::string* key) const;
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
private:
|
2014-10-06 19:35:53 +02:00
|
|
|
// used for synchronisation in compare method
|
|
|
|
port::Mutex* mtx_compare;
|
|
|
|
// used for synchronisation in findShortestSeparator method
|
|
|
|
port::Mutex* mtx_findShortestSeparator;
|
2014-08-03 22:11:44 +02:00
|
|
|
JavaVM* m_jvm;
|
|
|
|
jobject m_jComparator;
|
|
|
|
std::string m_name;
|
|
|
|
jmethodID m_jCompareMethodId;
|
|
|
|
jmethodID m_jFindShortestSeparatorMethodId;
|
|
|
|
jmethodID m_jFindShortSuccessorMethodId;
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
protected:
|
2014-10-06 19:35:53 +02:00
|
|
|
JNIEnv* getJniEnv() const;
|
2014-08-03 22:11:44 +02:00
|
|
|
jobject m_jSliceA;
|
|
|
|
jobject m_jSliceB;
|
|
|
|
jobject m_jSliceLimit;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ComparatorJniCallback : public BaseComparatorJniCallback {
|
2014-08-21 22:55:51 +02:00
|
|
|
public:
|
|
|
|
ComparatorJniCallback(
|
|
|
|
JNIEnv* env, jobject jComparator,
|
|
|
|
const ComparatorJniCallbackOptions* copt);
|
2014-10-06 19:35:53 +02:00
|
|
|
~ComparatorJniCallback();
|
2014-08-03 22:11:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class DirectComparatorJniCallback : public BaseComparatorJniCallback {
|
2014-08-21 22:55:51 +02:00
|
|
|
public:
|
|
|
|
DirectComparatorJniCallback(
|
|
|
|
JNIEnv* env, jobject jComparator,
|
|
|
|
const ComparatorJniCallbackOptions* copt);
|
2014-10-06 19:35:53 +02:00
|
|
|
~DirectComparatorJniCallback();
|
2014-08-03 22:11:44 +02:00
|
|
|
};
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
#endif // JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
|