]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | // |
6 | // This file implements the "bridge" between Java and C++ and enables | |
f67539c2 | 7 | // calling c++ ROCKSDB_NAMESPACE::Statistics methods from Java side. |
7c673cae | 8 | |
1e59de90 TL |
9 | #include "rocksdb/statistics.h" |
10 | ||
7c673cae | 11 | #include <jni.h> |
1e59de90 | 12 | |
11fdf7f2 TL |
13 | #include <memory> |
14 | #include <set> | |
7c673cae FG |
15 | |
16 | #include "include/org_rocksdb_Statistics.h" | |
1e59de90 | 17 | #include "rocksjni/cplusplus_to_java_convert.h" |
11fdf7f2 TL |
18 | #include "rocksjni/portal.h" |
19 | #include "rocksjni/statisticsjni.h" | |
20 | ||
21 | /* | |
22 | * Class: org_rocksdb_Statistics | |
23 | * Method: newStatistics | |
24 | * Signature: ()J | |
25 | */ | |
1e59de90 TL |
26 | jlong Java_org_rocksdb_Statistics_newStatistics__(JNIEnv* env, jclass jcls) { |
27 | return Java_org_rocksdb_Statistics_newStatistics___3BJ(env, jcls, nullptr, 0); | |
11fdf7f2 TL |
28 | } |
29 | ||
30 | /* | |
31 | * Class: org_rocksdb_Statistics | |
32 | * Method: newStatistics | |
33 | * Signature: (J)J | |
34 | */ | |
35 | jlong Java_org_rocksdb_Statistics_newStatistics__J( | |
36 | JNIEnv* env, jclass jcls, jlong jother_statistics_handle) { | |
37 | return Java_org_rocksdb_Statistics_newStatistics___3BJ( | |
38 | env, jcls, nullptr, jother_statistics_handle); | |
39 | } | |
40 | ||
41 | /* | |
42 | * Class: org_rocksdb_Statistics | |
43 | * Method: newStatistics | |
44 | * Signature: ([B)J | |
45 | */ | |
1e59de90 TL |
46 | jlong Java_org_rocksdb_Statistics_newStatistics___3B(JNIEnv* env, jclass jcls, |
47 | jbyteArray jhistograms) { | |
48 | return Java_org_rocksdb_Statistics_newStatistics___3BJ(env, jcls, jhistograms, | |
49 | 0); | |
11fdf7f2 TL |
50 | } |
51 | ||
52 | /* | |
53 | * Class: org_rocksdb_Statistics | |
54 | * Method: newStatistics | |
55 | * Signature: ([BJ)J | |
56 | */ | |
57 | jlong Java_org_rocksdb_Statistics_newStatistics___3BJ( | |
1e59de90 TL |
58 | JNIEnv* env, jclass, jbyteArray jhistograms, |
59 | jlong jother_statistics_handle) { | |
f67539c2 TL |
60 | std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>* pSptr_other_statistics = |
61 | nullptr; | |
11fdf7f2 TL |
62 | if (jother_statistics_handle > 0) { |
63 | pSptr_other_statistics = | |
f67539c2 | 64 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
11fdf7f2 TL |
65 | jother_statistics_handle); |
66 | } | |
67 | ||
68 | std::set<uint32_t> histograms; | |
69 | if (jhistograms != nullptr) { | |
70 | const jsize len = env->GetArrayLength(jhistograms); | |
71 | if (len > 0) { | |
72 | jbyte* jhistogram = env->GetByteArrayElements(jhistograms, nullptr); | |
73 | if (jhistogram == nullptr) { | |
74 | // exception thrown: OutOfMemoryError | |
75 | return 0; | |
76 | } | |
77 | ||
78 | for (jsize i = 0; i < len; i++) { | |
f67539c2 TL |
79 | const ROCKSDB_NAMESPACE::Histograms histogram = |
80 | ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram[i]); | |
11fdf7f2 TL |
81 | histograms.emplace(histogram); |
82 | } | |
83 | ||
84 | env->ReleaseByteArrayElements(jhistograms, jhistogram, JNI_ABORT); | |
85 | } | |
86 | } | |
87 | ||
f67539c2 TL |
88 | std::shared_ptr<ROCKSDB_NAMESPACE::Statistics> sptr_other_statistics = |
89 | nullptr; | |
11fdf7f2 TL |
90 | if (pSptr_other_statistics != nullptr) { |
91 | sptr_other_statistics = *pSptr_other_statistics; | |
92 | } | |
93 | ||
f67539c2 TL |
94 | auto* pSptr_statistics = |
95 | new std::shared_ptr<ROCKSDB_NAMESPACE::StatisticsJni>( | |
96 | new ROCKSDB_NAMESPACE::StatisticsJni(sptr_other_statistics, | |
97 | histograms)); | |
11fdf7f2 | 98 | |
1e59de90 | 99 | return GET_CPLUSPLUS_POINTER(pSptr_statistics); |
11fdf7f2 TL |
100 | } |
101 | ||
102 | /* | |
103 | * Class: org_rocksdb_Statistics | |
104 | * Method: disposeInternal | |
105 | * Signature: (J)V | |
106 | */ | |
1e59de90 TL |
107 | void Java_org_rocksdb_Statistics_disposeInternal(JNIEnv*, jobject, |
108 | jlong jhandle) { | |
11fdf7f2 TL |
109 | if (jhandle > 0) { |
110 | auto* pSptr_statistics = | |
f67539c2 TL |
111 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
112 | jhandle); | |
11fdf7f2 TL |
113 | delete pSptr_statistics; |
114 | } | |
115 | } | |
116 | ||
117 | /* | |
118 | * Class: org_rocksdb_Statistics | |
119 | * Method: statsLevel | |
120 | * Signature: (J)B | |
121 | */ | |
1e59de90 | 122 | jbyte Java_org_rocksdb_Statistics_statsLevel(JNIEnv*, jobject, jlong jhandle) { |
11fdf7f2 | 123 | auto* pSptr_statistics = |
f67539c2 TL |
124 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
125 | jhandle); | |
11fdf7f2 | 126 | assert(pSptr_statistics != nullptr); |
f67539c2 | 127 | return ROCKSDB_NAMESPACE::StatsLevelJni::toJavaStatsLevel( |
494da23a | 128 | pSptr_statistics->get()->get_stats_level()); |
11fdf7f2 TL |
129 | } |
130 | ||
131 | /* | |
132 | * Class: org_rocksdb_Statistics | |
133 | * Method: setStatsLevel | |
134 | * Signature: (JB)V | |
135 | */ | |
1e59de90 TL |
136 | void Java_org_rocksdb_Statistics_setStatsLevel(JNIEnv*, jobject, jlong jhandle, |
137 | jbyte jstats_level) { | |
11fdf7f2 | 138 | auto* pSptr_statistics = |
f67539c2 TL |
139 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
140 | jhandle); | |
11fdf7f2 | 141 | assert(pSptr_statistics != nullptr); |
f67539c2 TL |
142 | auto stats_level = |
143 | ROCKSDB_NAMESPACE::StatsLevelJni::toCppStatsLevel(jstats_level); | |
494da23a | 144 | pSptr_statistics->get()->set_stats_level(stats_level); |
11fdf7f2 TL |
145 | } |
146 | ||
147 | /* | |
148 | * Class: org_rocksdb_Statistics | |
149 | * Method: getTickerCount | |
150 | * Signature: (JB)J | |
151 | */ | |
1e59de90 TL |
152 | jlong Java_org_rocksdb_Statistics_getTickerCount(JNIEnv*, jobject, |
153 | jlong jhandle, | |
154 | jbyte jticker_type) { | |
11fdf7f2 | 155 | auto* pSptr_statistics = |
f67539c2 TL |
156 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
157 | jhandle); | |
11fdf7f2 | 158 | assert(pSptr_statistics != nullptr); |
f67539c2 | 159 | auto ticker = ROCKSDB_NAMESPACE::TickerTypeJni::toCppTickers(jticker_type); |
494da23a TL |
160 | uint64_t count = pSptr_statistics->get()->getTickerCount(ticker); |
161 | return static_cast<jlong>(count); | |
11fdf7f2 | 162 | } |
7c673cae FG |
163 | |
164 | /* | |
165 | * Class: org_rocksdb_Statistics | |
11fdf7f2 TL |
166 | * Method: getAndResetTickerCount |
167 | * Signature: (JB)J | |
7c673cae | 168 | */ |
1e59de90 TL |
169 | jlong Java_org_rocksdb_Statistics_getAndResetTickerCount(JNIEnv*, jobject, |
170 | jlong jhandle, | |
171 | jbyte jticker_type) { | |
11fdf7f2 | 172 | auto* pSptr_statistics = |
f67539c2 TL |
173 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
174 | jhandle); | |
11fdf7f2 | 175 | assert(pSptr_statistics != nullptr); |
f67539c2 | 176 | auto ticker = ROCKSDB_NAMESPACE::TickerTypeJni::toCppTickers(jticker_type); |
11fdf7f2 | 177 | return pSptr_statistics->get()->getAndResetTickerCount(ticker); |
7c673cae FG |
178 | } |
179 | ||
180 | /* | |
181 | * Class: org_rocksdb_Statistics | |
11fdf7f2 TL |
182 | * Method: getHistogramData |
183 | * Signature: (JB)Lorg/rocksdb/HistogramData; | |
7c673cae | 184 | */ |
1e59de90 TL |
185 | jobject Java_org_rocksdb_Statistics_getHistogramData(JNIEnv* env, jobject, |
186 | jlong jhandle, | |
187 | jbyte jhistogram_type) { | |
11fdf7f2 | 188 | auto* pSptr_statistics = |
f67539c2 TL |
189 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
190 | jhandle); | |
11fdf7f2 | 191 | assert(pSptr_statistics != nullptr); |
7c673cae | 192 | |
494da23a TL |
193 | // TODO(AR) perhaps better to construct a Java Object Wrapper that |
194 | // uses ptr to C++ `new HistogramData` | |
f67539c2 | 195 | ROCKSDB_NAMESPACE::HistogramData data; |
494da23a | 196 | |
f67539c2 TL |
197 | auto histogram = |
198 | ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram_type); | |
11fdf7f2 | 199 | pSptr_statistics->get()->histogramData( |
f67539c2 | 200 | static_cast<ROCKSDB_NAMESPACE::Histograms>(histogram), &data); |
7c673cae | 201 | |
f67539c2 | 202 | jclass jclazz = ROCKSDB_NAMESPACE::HistogramDataJni::getJClass(env); |
11fdf7f2 | 203 | if (jclazz == nullptr) { |
7c673cae FG |
204 | // exception occurred accessing class |
205 | return nullptr; | |
206 | } | |
207 | ||
f67539c2 TL |
208 | jmethodID mid = |
209 | ROCKSDB_NAMESPACE::HistogramDataJni::getConstructorMethodId(env); | |
11fdf7f2 | 210 | if (mid == nullptr) { |
7c673cae FG |
211 | // exception occurred accessing method |
212 | return nullptr; | |
213 | } | |
214 | ||
11fdf7f2 TL |
215 | return env->NewObject(jclazz, mid, data.median, data.percentile95, |
216 | data.percentile99, data.average, | |
1e59de90 TL |
217 | data.standard_deviation, data.max, data.count, data.sum, |
218 | data.min); | |
11fdf7f2 TL |
219 | } |
220 | ||
221 | /* | |
222 | * Class: org_rocksdb_Statistics | |
223 | * Method: getHistogramString | |
224 | * Signature: (JB)Ljava/lang/String; | |
225 | */ | |
1e59de90 TL |
226 | jstring Java_org_rocksdb_Statistics_getHistogramString(JNIEnv* env, jobject, |
227 | jlong jhandle, | |
228 | jbyte jhistogram_type) { | |
11fdf7f2 | 229 | auto* pSptr_statistics = |
f67539c2 TL |
230 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
231 | jhandle); | |
11fdf7f2 | 232 | assert(pSptr_statistics != nullptr); |
f67539c2 TL |
233 | auto histogram = |
234 | ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram_type); | |
11fdf7f2 TL |
235 | auto str = pSptr_statistics->get()->getHistogramString(histogram); |
236 | return env->NewStringUTF(str.c_str()); | |
237 | } | |
238 | ||
239 | /* | |
240 | * Class: org_rocksdb_Statistics | |
241 | * Method: reset | |
242 | * Signature: (J)V | |
243 | */ | |
1e59de90 | 244 | void Java_org_rocksdb_Statistics_reset(JNIEnv* env, jobject, jlong jhandle) { |
11fdf7f2 | 245 | auto* pSptr_statistics = |
f67539c2 TL |
246 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
247 | jhandle); | |
11fdf7f2 | 248 | assert(pSptr_statistics != nullptr); |
f67539c2 | 249 | ROCKSDB_NAMESPACE::Status s = pSptr_statistics->get()->Reset(); |
11fdf7f2 | 250 | if (!s.ok()) { |
f67539c2 | 251 | ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s); |
11fdf7f2 TL |
252 | } |
253 | } | |
254 | ||
255 | /* | |
256 | * Class: org_rocksdb_Statistics | |
257 | * Method: toString | |
258 | * Signature: (J)Ljava/lang/String; | |
259 | */ | |
1e59de90 TL |
260 | jstring Java_org_rocksdb_Statistics_toString(JNIEnv* env, jobject, |
261 | jlong jhandle) { | |
11fdf7f2 | 262 | auto* pSptr_statistics = |
f67539c2 TL |
263 | reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>( |
264 | jhandle); | |
11fdf7f2 TL |
265 | assert(pSptr_statistics != nullptr); |
266 | auto str = pSptr_statistics->get()->ToString(); | |
267 | return env->NewStringUTF(str.c_str()); | |
7c673cae | 268 | } |