1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
6 // This file implements the callback "bridge" between Java and C++ for
7 // rocksdb::Comparator and rocksdb::DirectComparator.
9 #ifndef JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
10 #define JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
14 #include "rocksdb/comparator.h"
15 #include "rocksdb/slice.h"
16 #include "port/port.h"
20 struct ComparatorJniCallbackOptions
{
21 // Use adaptive mutex, which spins in the user space before resorting
22 // to kernel. This could reduce context switch when the mutex is not
23 // heavily contended. However, if the mutex is hot, we could end up
26 bool use_adaptive_mutex
;
28 ComparatorJniCallbackOptions() : use_adaptive_mutex(false) {
33 * This class acts as a bridge between C++
34 * and Java. The methods in this class will be
35 * called back from the RocksDB storage engine (C++)
36 * we then callback to the appropriate Java method
37 * this enables Comparators to be implemented in Java.
39 * The design of this Comparator caches the Java Slice
40 * objects that are used in the compare and findShortestSeparator
41 * method callbacks. Instead of creating new objects for each callback
42 * of those functions, by reuse via setHandle we are a lot
43 * faster; Unfortunately this means that we have to
44 * introduce independent locking in regions of each of those methods
45 * via the mutexs mtx_compare and mtx_findShortestSeparator respectively
47 class BaseComparatorJniCallback
: public Comparator
{
49 BaseComparatorJniCallback(
50 JNIEnv
* env
, jobject jComparator
,
51 const ComparatorJniCallbackOptions
* copt
);
52 virtual ~BaseComparatorJniCallback();
53 virtual const char* Name() const;
54 virtual int Compare(const Slice
& a
, const Slice
& b
) const;
55 virtual void FindShortestSeparator(
56 std::string
* start
, const Slice
& limit
) const;
57 virtual void FindShortSuccessor(std::string
* key
) const;
60 // used for synchronisation in compare method
61 port::Mutex
* mtx_compare
;
62 // used for synchronisation in findShortestSeparator method
63 port::Mutex
* mtx_findShortestSeparator
;
64 jobject m_jComparator
;
66 jmethodID m_jCompareMethodId
;
67 jmethodID m_jFindShortestSeparatorMethodId
;
68 jmethodID m_jFindShortSuccessorMethodId
;
74 jobject m_jSliceLimit
;
77 class ComparatorJniCallback
: public BaseComparatorJniCallback
{
79 ComparatorJniCallback(
80 JNIEnv
* env
, jobject jComparator
,
81 const ComparatorJniCallbackOptions
* copt
);
82 ~ComparatorJniCallback();
85 class DirectComparatorJniCallback
: public BaseComparatorJniCallback
{
87 DirectComparatorJniCallback(
88 JNIEnv
* env
, jobject jComparator
,
89 const ComparatorJniCallbackOptions
* copt
);
90 ~DirectComparatorJniCallback();
92 } // namespace rocksdb
94 #endif // JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_