summaryrefslogtreecommitdiffstats
path: root/src/rocksdb/java/rocksjni/statistics.cc
blob: f59e79e6ce5a96a6fcc19d3ff7632eace672f376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
//
// This file implements the "bridge" between Java and C++ and enables
// calling c++ ROCKSDB_NAMESPACE::Statistics methods from Java side.

#include <jni.h>
#include <memory>
#include <set>

#include "include/org_rocksdb_Statistics.h"
#include "rocksdb/statistics.h"
#include "rocksjni/portal.h"
#include "rocksjni/statisticsjni.h"

/*
 * Class:     org_rocksdb_Statistics
 * Method:    newStatistics
 * Signature: ()J
 */
jlong Java_org_rocksdb_Statistics_newStatistics__(
    JNIEnv* env, jclass jcls) {
  return Java_org_rocksdb_Statistics_newStatistics___3BJ(
      env, jcls, nullptr, 0);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    newStatistics
 * Signature: (J)J
 */
jlong Java_org_rocksdb_Statistics_newStatistics__J(
    JNIEnv* env, jclass jcls, jlong jother_statistics_handle) {
  return Java_org_rocksdb_Statistics_newStatistics___3BJ(
      env, jcls, nullptr, jother_statistics_handle);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    newStatistics
 * Signature: ([B)J
 */
jlong Java_org_rocksdb_Statistics_newStatistics___3B(
    JNIEnv* env, jclass jcls, jbyteArray jhistograms) {
  return Java_org_rocksdb_Statistics_newStatistics___3BJ(
      env, jcls, jhistograms, 0);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    newStatistics
 * Signature: ([BJ)J
 */
jlong Java_org_rocksdb_Statistics_newStatistics___3BJ(
    JNIEnv* env, jclass, jbyteArray jhistograms, jlong jother_statistics_handle) {
  std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>* pSptr_other_statistics =
      nullptr;
  if (jother_statistics_handle > 0) {
    pSptr_other_statistics =
        reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
            jother_statistics_handle);
  }

  std::set<uint32_t> histograms;
  if (jhistograms != nullptr) {
    const jsize len = env->GetArrayLength(jhistograms);
    if (len > 0) {
      jbyte* jhistogram = env->GetByteArrayElements(jhistograms, nullptr);
      if (jhistogram == nullptr) {
        // exception thrown: OutOfMemoryError
        return 0;
      }

      for (jsize i = 0; i < len; i++) {
        const ROCKSDB_NAMESPACE::Histograms histogram =
            ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram[i]);
        histograms.emplace(histogram);
      }

      env->ReleaseByteArrayElements(jhistograms, jhistogram, JNI_ABORT);
    }
  }

  std::shared_ptr<ROCKSDB_NAMESPACE::Statistics> sptr_other_statistics =
      nullptr;
  if (pSptr_other_statistics != nullptr) {
    sptr_other_statistics = *pSptr_other_statistics;
  }

  auto* pSptr_statistics =
      new std::shared_ptr<ROCKSDB_NAMESPACE::StatisticsJni>(
          new ROCKSDB_NAMESPACE::StatisticsJni(sptr_other_statistics,
                                               histograms));

  return reinterpret_cast<jlong>(pSptr_statistics);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    disposeInternal
 * Signature: (J)V
 */
void Java_org_rocksdb_Statistics_disposeInternal(
    JNIEnv*, jobject, jlong jhandle) {
  if (jhandle > 0) {
    auto* pSptr_statistics =
        reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
            jhandle);
    delete pSptr_statistics;
  }
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    statsLevel
 * Signature: (J)B
 */
jbyte Java_org_rocksdb_Statistics_statsLevel(
    JNIEnv*, jobject, jlong jhandle) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);
  return ROCKSDB_NAMESPACE::StatsLevelJni::toJavaStatsLevel(
      pSptr_statistics->get()->get_stats_level());
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    setStatsLevel
 * Signature: (JB)V
 */
void Java_org_rocksdb_Statistics_setStatsLevel(
    JNIEnv*, jobject, jlong jhandle, jbyte jstats_level) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);
  auto stats_level =
      ROCKSDB_NAMESPACE::StatsLevelJni::toCppStatsLevel(jstats_level);
  pSptr_statistics->get()->set_stats_level(stats_level);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    getTickerCount
 * Signature: (JB)J
 */
jlong Java_org_rocksdb_Statistics_getTickerCount(
    JNIEnv*, jobject, jlong jhandle, jbyte jticker_type) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);
  auto ticker = ROCKSDB_NAMESPACE::TickerTypeJni::toCppTickers(jticker_type);
  uint64_t count = pSptr_statistics->get()->getTickerCount(ticker);
  return static_cast<jlong>(count);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    getAndResetTickerCount
 * Signature: (JB)J
 */
jlong Java_org_rocksdb_Statistics_getAndResetTickerCount(
    JNIEnv*, jobject, jlong jhandle, jbyte jticker_type) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);
  auto ticker = ROCKSDB_NAMESPACE::TickerTypeJni::toCppTickers(jticker_type);
  return pSptr_statistics->get()->getAndResetTickerCount(ticker);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    getHistogramData
 * Signature: (JB)Lorg/rocksdb/HistogramData;
 */
jobject Java_org_rocksdb_Statistics_getHistogramData(
    JNIEnv* env, jobject, jlong jhandle, jbyte jhistogram_type) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);

  // TODO(AR) perhaps better to construct a Java Object Wrapper that
  //    uses ptr to C++ `new HistogramData`
  ROCKSDB_NAMESPACE::HistogramData data;

  auto histogram =
      ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram_type);
  pSptr_statistics->get()->histogramData(
      static_cast<ROCKSDB_NAMESPACE::Histograms>(histogram), &data);

  jclass jclazz = ROCKSDB_NAMESPACE::HistogramDataJni::getJClass(env);
  if (jclazz == nullptr) {
    // exception occurred accessing class
    return nullptr;
  }

  jmethodID mid =
      ROCKSDB_NAMESPACE::HistogramDataJni::getConstructorMethodId(env);
  if (mid == nullptr) {
    // exception occurred accessing method
    return nullptr;
  }

  return env->NewObject(jclazz, mid, data.median, data.percentile95,
                        data.percentile99, data.average,
                        data.standard_deviation, data.max, data.count,
                        data.sum, data.min);
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    getHistogramString
 * Signature: (JB)Ljava/lang/String;
 */
jstring Java_org_rocksdb_Statistics_getHistogramString(
    JNIEnv* env, jobject, jlong jhandle, jbyte jhistogram_type) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);
  auto histogram =
      ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram_type);
  auto str = pSptr_statistics->get()->getHistogramString(histogram);
  return env->NewStringUTF(str.c_str());
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    reset
 * Signature: (J)V
 */
void Java_org_rocksdb_Statistics_reset(
    JNIEnv* env, jobject, jlong jhandle) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);
  ROCKSDB_NAMESPACE::Status s = pSptr_statistics->get()->Reset();
  if (!s.ok()) {
    ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
  }
}

/*
 * Class:     org_rocksdb_Statistics
 * Method:    toString
 * Signature: (J)Ljava/lang/String;
 */
jstring Java_org_rocksdb_Statistics_toString(
    JNIEnv* env, jobject, jlong jhandle) {
  auto* pSptr_statistics =
      reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
          jhandle);
  assert(pSptr_statistics != nullptr);
  auto str = pSptr_statistics->get()->ToString();
  return env->NewStringUTF(str.c_str());
}