2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-08-03 22:11:44 +02:00
|
|
|
//
|
|
|
|
// 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>
|
2017-10-12 20:06:51 +02:00
|
|
|
#include <memory>
|
2014-08-21 22:55:51 +02:00
|
|
|
#include <string>
|
2017-10-12 20:06:51 +02:00
|
|
|
#include "rocksjni/jnicallback.h"
|
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
|
|
|
*/
|
2017-10-12 20:06:51 +02:00
|
|
|
class BaseComparatorJniCallback : public JniCallback, 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 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
|
2017-10-12 20:06:51 +02:00
|
|
|
std::unique_ptr<port::Mutex> mtx_compare;
|
2014-10-06 19:35:53 +02:00
|
|
|
// used for synchronisation in findShortestSeparator method
|
2017-10-12 20:06:51 +02:00
|
|
|
std::unique_ptr<port::Mutex> mtx_findShortestSeparator;
|
|
|
|
std::unique_ptr<const char[]> m_name;
|
2014-08-03 22:11:44 +02:00
|
|
|
jmethodID m_jCompareMethodId;
|
|
|
|
jmethodID m_jFindShortestSeparatorMethodId;
|
|
|
|
jmethodID m_jFindShortSuccessorMethodId;
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
protected:
|
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_
|