Skip to content

Commit 320ae72

Browse files
author
Ankit Gupta
committed
Add histogramType for statistics
1 parent 144066a commit 320ae72

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ ldb: tools/ldb.o $(LIBOBJECTS)
420420
# ---------------------------------------------------------------------------
421421
# Jni stuff
422422
# ---------------------------------------------------------------------------
423-
JNI_NATIVE_SOURCES = ./java/rocksjni/rocksjni.cc ./java/rocksjni/options.cc ./java/rocksjni/write_batch.cc
423+
JNI_NATIVE_SOURCES = ./java/rocksjni/rocksjni.cc ./java/rocksjni/options.cc ./java/rocksjni/write_batch.cc ./java/rocksjni/statistics.cc
424424

425425
JAVA_INCLUDE = -I/usr/lib/jvm/java-openjdk/include/ -I/usr/lib/jvm/java-openjdk/include/linux
426426
ROCKSDBJNILIB = ./java/librocksdbjni.so

java/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sample: java
1616
javac -cp $(ROCKSDB_JAR) RocksDBSample.java
1717
@rm -rf /tmp/rocksdbjni
1818
@rm -rf /tmp/rocksdbjni_not_found
19-
java -ea -Djava.library.path=.:../ -cp ".:./*" RocksDBSample /tmp/rocksdbjni
19+
java -ea -Djava.library.path=.:../ -cp ".:./*" -Xcheck:jni RocksDBSample /tmp/rocksdbjni
2020
@rm -rf /tmp/rocksdbjni
2121
@rm -rf /tmp/rocksdbjni_not_found
2222

java/RocksDBSample.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void main(String[] args) {
4040
.setDisableSeekCompaction(true)
4141
.setBlockSize(64 * SizeUnit.KB)
4242
.setMaxBackgroundCompactions(10);
43-
Statistics stats = new Statistics(options.statisticsPtr());
43+
Statistics stats = options.statisticsPtr();
4444

4545
assert(options.createIfMissing() == true);
4646
assert(options.writeBufferSize() == 8 * SizeUnit.KB);
@@ -124,16 +124,24 @@ public static void main(String[] args) {
124124
writeOpts.dispose();
125125

126126
try {
127-
for(TickerType statsType : TickerType.values()) {
127+
for (TickerType statsType : TickerType.values()) {
128128
stats.getTickerCount(statsType);
129129
}
130130
System.out.println("getTickerCount() passed.");
131-
}
132-
catch(Exception e) {
131+
} catch (Exception e) {
133132
System.out.println("Failed in call to getTickerCount()");
134133
assert(false); //Should never reach here.
135134
}
136-
135+
136+
try {
137+
for (HistogramType histogramType : HistogramType.values()) {
138+
HistogramData data = stats.geHistogramData(histogramType);
139+
}
140+
System.out.println("geHistogramData() passed.");
141+
} catch (Exception e) {
142+
System.out.println("Failed in call to geHistogramData()");
143+
assert(false); //Should never reach here.
144+
}
137145
} catch (RocksDBException e) {
138146
System.err.println(e);
139147
}

java/org/rocksdb/Options.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,16 @@ public Options createStatistics() {
220220
*
221221
* @see createStatistics()
222222
*/
223-
public long statisticsPtr() {
223+
public Statistics statisticsPtr() {
224224
assert(isInitialized());
225225

226226
long statsPtr = statisticsPtr(nativeHandle_);
227-
assert(statsPtr != 0);
227+
if(statsPtr == 0) {
228+
createStatistics();
229+
statsPtr = statisticsPtr(nativeHandle_);
230+
}
228231

229-
return statsPtr;
232+
return new Statistics(statsPtr);
230233
}
231234

232235
/**

java/org/rocksdb/Statistics.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public class Statistics {
1313

14-
private long statsHandle_;
14+
private final long statsHandle_;
1515

1616
public Statistics(long statsHandle) {
1717
statsHandle_ = statsHandle;
@@ -22,9 +22,16 @@ public long getTickerCount(TickerType tickerType) {
2222
return getTickerCount0(tickerType.getValue(), statsHandle_);
2323
}
2424

25+
public HistogramData geHistogramData(HistogramType histogramType) {
26+
assert(isInitialized());
27+
HistogramData hist = geHistogramData0(histogramType.getValue(), statsHandle_);
28+
return hist;
29+
}
30+
2531
private boolean isInitialized() {
2632
return (statsHandle_ != 0);
2733
}
2834

29-
private native long getTickerCount0(int ticker, long handle);
35+
private native long getTickerCount0(int tickerType, long handle);
36+
private native HistogramData geHistogramData0(int histogramType, long handle);
3037
}

java/rocksjni/portal.h

+10
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,15 @@ class WriteBatchJni {
170170
reinterpret_cast<jlong>(wb));
171171
}
172172
};
173+
174+
class HistogramDataJni {
175+
public:
176+
static jmethodID getConstructorMethodId(JNIEnv* env, jclass jclazz) {
177+
static jmethodID mid = env->GetMethodID(
178+
jclazz, "<init>", "(DDDDD)V");
179+
assert(mid != nullptr);
180+
return mid;
181+
}
182+
};
173183
} // namespace rocksdb
174184
#endif // JAVA_ROCKSJNI_PORTAL_H_

java/rocksjni/statistics.cc

+20-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,34 @@
1313
#include "include/org_rocksdb_Statistics.h"
1414
#include "rocksjni/portal.h"
1515
#include "rocksdb/statistics.h"
16+
#include <iostream>
1617

1718
/*
1819
* Class: org_rocksdb_Statistics
1920
* Method: getTickerCount0
2021
* Signature: (IJ)J
2122
*/
2223
jlong Java_org_rocksdb_Statistics_getTickerCount0(
23-
JNIEnv* env, jobject jobj, int ticker, jlong handle) {
24+
JNIEnv* env, jobject jobj, int tickerType, jlong handle) {
2425
auto st = reinterpret_cast<rocksdb::Statistics*>(handle);
2526
assert(st != nullptr);
2627

27-
return st->getTickerCount(static_cast<rocksdb::Tickers>(ticker));
28+
return st->getTickerCount(static_cast<rocksdb::Tickers>(tickerType));
29+
}
30+
31+
jobject Java_org_rocksdb_Statistics_geHistogramData0(
32+
JNIEnv* env, jobject jobj, int histogramType, jlong handle) {
33+
auto st = reinterpret_cast<rocksdb::Statistics*>(handle);
34+
assert(st != nullptr);
35+
36+
rocksdb::HistogramData data;
37+
st->histogramData(static_cast<rocksdb::Histograms>(histogramType),
38+
&data);
39+
40+
// Don't reuse class pointer
41+
jclass jclazz = env->FindClass("org/rocksdb/HistogramData");
42+
jmethodID mid = rocksdb::HistogramDataJni::getConstructorMethodId(
43+
env, jclazz);
44+
return env->NewObject(jclazz, mid, data.median, data.percentile95,
45+
data.percentile99, data.average, data.standard_deviation);
2846
}

0 commit comments

Comments
 (0)