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).
6 // This file implements the "bridge" between Java and C++ and enables
7 // calling c++ rocksdb::Statistics methods from Java side.
13 #include "include/org_rocksdb_Statistics.h"
14 #include "rocksdb/statistics.h"
15 #include "rocksjni/portal.h"
16 #include "rocksjni/statisticsjni.h"
19 * Class: org_rocksdb_Statistics
20 * Method: newStatistics
23 jlong
Java_org_rocksdb_Statistics_newStatistics__(
24 JNIEnv
* env
, jclass jcls
) {
25 return Java_org_rocksdb_Statistics_newStatistics___3BJ(
26 env
, jcls
, nullptr, 0);
30 * Class: org_rocksdb_Statistics
31 * Method: newStatistics
34 jlong
Java_org_rocksdb_Statistics_newStatistics__J(
35 JNIEnv
* env
, jclass jcls
, jlong jother_statistics_handle
) {
36 return Java_org_rocksdb_Statistics_newStatistics___3BJ(
37 env
, jcls
, nullptr, jother_statistics_handle
);
41 * Class: org_rocksdb_Statistics
42 * Method: newStatistics
45 jlong
Java_org_rocksdb_Statistics_newStatistics___3B(
46 JNIEnv
* env
, jclass jcls
, jbyteArray jhistograms
) {
47 return Java_org_rocksdb_Statistics_newStatistics___3BJ(
48 env
, jcls
, jhistograms
, 0);
52 * Class: org_rocksdb_Statistics
53 * Method: newStatistics
56 jlong
Java_org_rocksdb_Statistics_newStatistics___3BJ(
57 JNIEnv
* env
, jclass
, jbyteArray jhistograms
, jlong jother_statistics_handle
) {
58 std::shared_ptr
<rocksdb::Statistics
>* pSptr_other_statistics
= nullptr;
59 if (jother_statistics_handle
> 0) {
60 pSptr_other_statistics
=
61 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(
62 jother_statistics_handle
);
65 std::set
<uint32_t> histograms
;
66 if (jhistograms
!= nullptr) {
67 const jsize len
= env
->GetArrayLength(jhistograms
);
69 jbyte
* jhistogram
= env
->GetByteArrayElements(jhistograms
, nullptr);
70 if (jhistogram
== nullptr) {
71 // exception thrown: OutOfMemoryError
75 for (jsize i
= 0; i
< len
; i
++) {
76 const rocksdb::Histograms histogram
=
77 rocksdb::HistogramTypeJni::toCppHistograms(jhistogram
[i
]);
78 histograms
.emplace(histogram
);
81 env
->ReleaseByteArrayElements(jhistograms
, jhistogram
, JNI_ABORT
);
85 std::shared_ptr
<rocksdb::Statistics
> sptr_other_statistics
= nullptr;
86 if (pSptr_other_statistics
!= nullptr) {
87 sptr_other_statistics
= *pSptr_other_statistics
;
90 auto* pSptr_statistics
= new std::shared_ptr
<rocksdb::StatisticsJni
>(
91 new rocksdb::StatisticsJni(sptr_other_statistics
, histograms
));
93 return reinterpret_cast<jlong
>(pSptr_statistics
);
97 * Class: org_rocksdb_Statistics
98 * Method: disposeInternal
101 void Java_org_rocksdb_Statistics_disposeInternal(
102 JNIEnv
*, jobject
, jlong jhandle
) {
104 auto* pSptr_statistics
=
105 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
106 delete pSptr_statistics
;
111 * Class: org_rocksdb_Statistics
115 jbyte
Java_org_rocksdb_Statistics_statsLevel(
116 JNIEnv
*, jobject
, jlong jhandle
) {
117 auto* pSptr_statistics
=
118 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
119 assert(pSptr_statistics
!= nullptr);
120 return rocksdb::StatsLevelJni::toJavaStatsLevel(
121 pSptr_statistics
->get()->get_stats_level());
125 * Class: org_rocksdb_Statistics
126 * Method: setStatsLevel
129 void Java_org_rocksdb_Statistics_setStatsLevel(
130 JNIEnv
*, jobject
, jlong jhandle
, jbyte jstats_level
) {
131 auto* pSptr_statistics
=
132 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
133 assert(pSptr_statistics
!= nullptr);
134 auto stats_level
= rocksdb::StatsLevelJni::toCppStatsLevel(jstats_level
);
135 pSptr_statistics
->get()->set_stats_level(stats_level
);
139 * Class: org_rocksdb_Statistics
140 * Method: getTickerCount
143 jlong
Java_org_rocksdb_Statistics_getTickerCount(
144 JNIEnv
*, jobject
, jlong jhandle
, jbyte jticker_type
) {
145 auto* pSptr_statistics
=
146 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
147 assert(pSptr_statistics
!= nullptr);
148 auto ticker
= rocksdb::TickerTypeJni::toCppTickers(jticker_type
);
149 uint64_t count
= pSptr_statistics
->get()->getTickerCount(ticker
);
150 return static_cast<jlong
>(count
);
154 * Class: org_rocksdb_Statistics
155 * Method: getAndResetTickerCount
158 jlong
Java_org_rocksdb_Statistics_getAndResetTickerCount(
159 JNIEnv
*, jobject
, jlong jhandle
, jbyte jticker_type
) {
160 auto* pSptr_statistics
=
161 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
162 assert(pSptr_statistics
!= nullptr);
163 auto ticker
= rocksdb::TickerTypeJni::toCppTickers(jticker_type
);
164 return pSptr_statistics
->get()->getAndResetTickerCount(ticker
);
168 * Class: org_rocksdb_Statistics
169 * Method: getHistogramData
170 * Signature: (JB)Lorg/rocksdb/HistogramData;
172 jobject
Java_org_rocksdb_Statistics_getHistogramData(
173 JNIEnv
* env
, jobject
, jlong jhandle
, jbyte jhistogram_type
) {
174 auto* pSptr_statistics
=
175 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
176 assert(pSptr_statistics
!= nullptr);
178 // TODO(AR) perhaps better to construct a Java Object Wrapper that
179 // uses ptr to C++ `new HistogramData`
180 rocksdb::HistogramData data
;
182 auto histogram
= rocksdb::HistogramTypeJni::toCppHistograms(jhistogram_type
);
183 pSptr_statistics
->get()->histogramData(
184 static_cast<rocksdb::Histograms
>(histogram
), &data
);
186 jclass jclazz
= rocksdb::HistogramDataJni::getJClass(env
);
187 if (jclazz
== nullptr) {
188 // exception occurred accessing class
192 jmethodID mid
= rocksdb::HistogramDataJni::getConstructorMethodId(env
);
193 if (mid
== nullptr) {
194 // exception occurred accessing method
198 return env
->NewObject(jclazz
, mid
, data
.median
, data
.percentile95
,
199 data
.percentile99
, data
.average
,
200 data
.standard_deviation
, data
.max
, data
.count
,
205 * Class: org_rocksdb_Statistics
206 * Method: getHistogramString
207 * Signature: (JB)Ljava/lang/String;
209 jstring
Java_org_rocksdb_Statistics_getHistogramString(
210 JNIEnv
* env
, jobject
, jlong jhandle
, jbyte jhistogram_type
) {
211 auto* pSptr_statistics
=
212 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
213 assert(pSptr_statistics
!= nullptr);
214 auto histogram
= rocksdb::HistogramTypeJni::toCppHistograms(jhistogram_type
);
215 auto str
= pSptr_statistics
->get()->getHistogramString(histogram
);
216 return env
->NewStringUTF(str
.c_str());
220 * Class: org_rocksdb_Statistics
224 void Java_org_rocksdb_Statistics_reset(
225 JNIEnv
* env
, jobject
, jlong jhandle
) {
226 auto* pSptr_statistics
=
227 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
228 assert(pSptr_statistics
!= nullptr);
229 rocksdb::Status s
= pSptr_statistics
->get()->Reset();
231 rocksdb::RocksDBExceptionJni::ThrowNew(env
, s
);
236 * Class: org_rocksdb_Statistics
238 * Signature: (J)Ljava/lang/String;
240 jstring
Java_org_rocksdb_Statistics_toString(
241 JNIEnv
* env
, jobject
, jlong jhandle
) {
242 auto* pSptr_statistics
=
243 reinterpret_cast<std::shared_ptr
<rocksdb::Statistics
>*>(jhandle
);
244 assert(pSptr_statistics
!= nullptr);
245 auto str
= pSptr_statistics
->get()->ToString();
246 return env
->NewStringUTF(str
.c_str());