]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/rocksjni/comparatorjnicallback.h
update ceph source to reef 18.1.2
[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 both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 // This file implements the callback "bridge" between Java and C++ for
7 // ROCKSDB_NAMESPACE::Comparator
8
9 #ifndef JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
10 #define JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_
11
12 #include <jni.h>
13
14 #include <memory>
15 #include <string>
16
17 #include "port/port.h"
18 #include "rocksdb/comparator.h"
19 #include "rocksdb/slice.h"
20 #include "rocksjni/jnicallback.h"
21 #include "util/thread_local.h"
22
23 namespace ROCKSDB_NAMESPACE {
24
25 enum ReusedSynchronisationType {
26 /**
27 * Standard mutex.
28 */
29 MUTEX,
30
31 /**
32 * Use adaptive mutex, which spins in the user space before resorting
33 * to kernel. This could reduce context switch when the mutex is not
34 * heavily contended. However, if the mutex is hot, we could end up
35 * wasting spin time.
36 */
37 ADAPTIVE_MUTEX,
38
39 /**
40 * There is a reused buffer per-thread.
41 */
42 THREAD_LOCAL
43 };
44
45 struct ComparatorJniCallbackOptions {
46 // Set the synchronisation type used to guard the reused buffers.
47 // Only used if max_reused_buffer_size > 0.
48 // Default: ADAPTIVE_MUTEX
49 ReusedSynchronisationType reused_synchronisation_type =
50 ReusedSynchronisationType::ADAPTIVE_MUTEX;
51
52 // Indicates if a direct byte buffer (i.e. outside of the normal
53 // garbage-collected heap) is used for the callbacks to Java,
54 // as opposed to a non-direct byte buffer which is a wrapper around
55 // an on-heap byte[].
56 // Default: true
57 bool direct_buffer = true;
58
59 // Maximum size of a buffer (in bytes) that will be reused.
60 // Comparators will use 5 of these buffers,
61 // so the retained memory size will be 5 * max_reused_buffer_size.
62 // When a buffer is needed for transferring data to a callback,
63 // if it requires less than max_reused_buffer_size, then an
64 // existing buffer will be reused, else a new buffer will be
65 // allocated just for that callback. -1 to disable.
66 // Default: 64 bytes
67 int32_t max_reused_buffer_size = 64;
68 };
69
70 /**
71 * This class acts as a bridge between C++
72 * and Java. The methods in this class will be
73 * called back from the RocksDB storage engine (C++)
74 * we then callback to the appropriate Java method
75 * this enables Comparators to be implemented in Java.
76 *
77 * The design of this Comparator caches the Java Slice
78 * objects that are used in the compare and findShortestSeparator
79 * method callbacks. Instead of creating new objects for each callback
80 * of those functions, by reuse via setHandle we are a lot
81 * faster; Unfortunately this means that we have to
82 * introduce independent locking in regions of each of those methods
83 * via the mutexs mtx_compare and mtx_findShortestSeparator respectively
84 */
85 class ComparatorJniCallback : public JniCallback, public Comparator {
86 public:
87 ComparatorJniCallback(JNIEnv* env, jobject jcomparator,
88 const ComparatorJniCallbackOptions* options);
89 ~ComparatorJniCallback();
90 virtual const char* Name() const;
91 virtual int Compare(const Slice& a, const Slice& b) const;
92 virtual void FindShortestSeparator(std::string* start,
93 const Slice& limit) const;
94 virtual void FindShortSuccessor(std::string* key) const;
95 const ComparatorJniCallbackOptions* m_options;
96
97 private:
98 struct ThreadLocalBuf {
99 ThreadLocalBuf(JavaVM* _jvm, bool _direct_buffer, jobject _jbuf)
100 : jvm(_jvm), direct_buffer(_direct_buffer), jbuf(_jbuf) {}
101 JavaVM* jvm;
102 bool direct_buffer;
103 jobject jbuf;
104 };
105 inline void MaybeLockForReuse(const std::unique_ptr<port::Mutex>& mutex,
106 const bool cond) const;
107 inline void MaybeUnlockForReuse(const std::unique_ptr<port::Mutex>& mutex,
108 const bool cond) const;
109 jobject GetBuffer(JNIEnv* env, const Slice& src, bool reuse_buffer,
110 ThreadLocalPtr* tl_buf, jobject jreuse_buffer) const;
111 jobject ReuseBuffer(JNIEnv* env, const Slice& src,
112 jobject jreuse_buffer) const;
113 jobject NewBuffer(JNIEnv* env, const Slice& src) const;
114 void DeleteBuffer(JNIEnv* env, jobject jbuffer) const;
115 // used for synchronisation in compare method
116 std::unique_ptr<port::Mutex> mtx_compare;
117 // used for synchronisation in findShortestSeparator method
118 std::unique_ptr<port::Mutex> mtx_shortest;
119 // used for synchronisation in findShortSuccessor method
120 std::unique_ptr<port::Mutex> mtx_short;
121 std::unique_ptr<const char[]> m_name;
122 jclass m_abstract_comparator_jni_bridge_clazz; // TODO(AR) could we make this
123 // static somehow?
124 jclass m_jbytebuffer_clazz; // TODO(AR) we could cache this globally for the
125 // entire VM if we switch more APIs to use
126 // ByteBuffer // TODO(AR) could we make this
127 // static somehow?
128 jmethodID m_jcompare_mid; // TODO(AR) could we make this static somehow?
129 jmethodID m_jshortest_mid; // TODO(AR) could we make this static somehow?
130 jmethodID m_jshort_mid; // TODO(AR) could we make this static somehow?
131 jobject m_jcompare_buf_a;
132 jobject m_jcompare_buf_b;
133 jobject m_jshortest_buf_start;
134 jobject m_jshortest_buf_limit;
135 jobject m_jshort_buf_key;
136 ThreadLocalPtr* m_tl_buf_a;
137 ThreadLocalPtr* m_tl_buf_b;
138 };
139 } // namespace ROCKSDB_NAMESPACE
140
141 #endif // JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_