]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/rocksjni/comparatorjnicallback.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / rocksjni / comparatorjnicallback.h
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.
5 //
6 // This file implements the callback "bridge" between Java and C++ for
7 // rocksdb::Comparator and rocksdb::DirectComparator.
8
9 #ifndef JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
10 #define JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
11
12 #include <jni.h>
13 #include <string>
14 #include "rocksdb/comparator.h"
15 #include "rocksdb/slice.h"
16 #include "port/port.h"
17
18 namespace rocksdb {
19
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
24 // wasting spin time.
25 // Default: false
26 bool use_adaptive_mutex;
27
28 ComparatorJniCallbackOptions() : use_adaptive_mutex(false) {
29 }
30 };
31
32 /**
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.
38 *
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
46 */
47 class BaseComparatorJniCallback : public Comparator {
48 public:
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;
58
59 private:
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;
65 std::string m_name;
66 jmethodID m_jCompareMethodId;
67 jmethodID m_jFindShortestSeparatorMethodId;
68 jmethodID m_jFindShortSuccessorMethodId;
69
70 protected:
71 JavaVM* m_jvm;
72 jobject m_jSliceA;
73 jobject m_jSliceB;
74 jobject m_jSliceLimit;
75 };
76
77 class ComparatorJniCallback : public BaseComparatorJniCallback {
78 public:
79 ComparatorJniCallback(
80 JNIEnv* env, jobject jComparator,
81 const ComparatorJniCallbackOptions* copt);
82 ~ComparatorJniCallback();
83 };
84
85 class DirectComparatorJniCallback : public BaseComparatorJniCallback {
86 public:
87 DirectComparatorJniCallback(
88 JNIEnv* env, jobject jComparator,
89 const ComparatorJniCallbackOptions* copt);
90 ~DirectComparatorJniCallback();
91 };
92 } // namespace rocksdb
93
94 #endif // JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_