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 package org.rocksdb;
7 
8 import java.util.EnumSet;
9 
10 /**
11  * Statistics to analyze the performance of a db. Pointer for statistics object
12  * is managed by Options class.
13  */
14 public class Statistics extends RocksObject {
15 
Statistics()16   public Statistics() {
17     super(newStatistics());
18   }
19 
Statistics(final Statistics otherStatistics)20   public Statistics(final Statistics otherStatistics) {
21     super(newStatistics(otherStatistics.nativeHandle_));
22   }
23 
Statistics(final EnumSet<HistogramType> ignoreHistograms)24   public Statistics(final EnumSet<HistogramType> ignoreHistograms) {
25     super(newStatistics(toArrayValues(ignoreHistograms)));
26   }
27 
Statistics(final EnumSet<HistogramType> ignoreHistograms, final Statistics otherStatistics)28   public Statistics(final EnumSet<HistogramType> ignoreHistograms, final Statistics otherStatistics) {
29     super(newStatistics(toArrayValues(ignoreHistograms), otherStatistics.nativeHandle_));
30   }
31 
32   /**
33    * Intentionally package-private.
34    *
35    * Used from {@link DBOptions#statistics()}
36    *
37    * @param existingStatisticsHandle The C++ pointer to an existing statistics object
38    */
Statistics(final long existingStatisticsHandle)39   Statistics(final long existingStatisticsHandle) {
40     super(existingStatisticsHandle);
41   }
42 
toArrayValues(final EnumSet<HistogramType> histogramTypes)43   private static byte[] toArrayValues(final EnumSet<HistogramType> histogramTypes) {
44     final byte[] values = new byte[histogramTypes.size()];
45     int i = 0;
46     for(final HistogramType histogramType : histogramTypes) {
47       values[i++] = histogramType.getValue();
48     }
49     return values;
50   }
51 
52   /**
53    * Gets the current stats level.
54    *
55    * @return The stats level.
56    */
statsLevel()57   public StatsLevel statsLevel() {
58     return StatsLevel.getStatsLevel(statsLevel(nativeHandle_));
59   }
60 
61   /**
62    * Sets the stats level.
63    *
64    * @param statsLevel The stats level to set.
65    */
setStatsLevel(final StatsLevel statsLevel)66   public void setStatsLevel(final StatsLevel statsLevel) {
67     setStatsLevel(nativeHandle_, statsLevel.getValue());
68   }
69 
70   /**
71    * Get the count for a ticker.
72    *
73    * @param tickerType The ticker to get the count for
74    *
75    * @return The count for the ticker
76    */
getTickerCount(final TickerType tickerType)77   public long getTickerCount(final TickerType tickerType) {
78     assert(isOwningHandle());
79     return getTickerCount(nativeHandle_, tickerType.getValue());
80   }
81 
82   /**
83    * Get the count for a ticker and reset the tickers count.
84    *
85    * @param tickerType The ticker to get the count for
86    *
87    * @return The count for the ticker
88    */
getAndResetTickerCount(final TickerType tickerType)89   public long getAndResetTickerCount(final TickerType tickerType) {
90     assert(isOwningHandle());
91     return getAndResetTickerCount(nativeHandle_, tickerType.getValue());
92   }
93 
94   /**
95    * Gets the histogram data for a particular histogram.
96    *
97    * @param histogramType The histogram to retrieve the data for
98    *
99    * @return The histogram data
100    */
getHistogramData(final HistogramType histogramType)101   public HistogramData getHistogramData(final HistogramType histogramType) {
102     assert(isOwningHandle());
103     return getHistogramData(nativeHandle_, histogramType.getValue());
104   }
105 
106   /**
107    * Gets a string representation of a particular histogram.
108    *
109    * @param histogramType The histogram to retrieve the data for
110    *
111    * @return A string representation of the histogram data
112    */
getHistogramString(final HistogramType histogramType)113   public String getHistogramString(final HistogramType histogramType) {
114     assert(isOwningHandle());
115     return getHistogramString(nativeHandle_, histogramType.getValue());
116   }
117 
118   /**
119    * Resets all ticker and histogram stats.
120    *
121    * @throws RocksDBException if an error occurs when resetting the statistics.
122    */
reset()123   public void reset() throws RocksDBException {
124     assert(isOwningHandle());
125     reset(nativeHandle_);
126   }
127 
128   /**
129    * String representation of the statistic object.
130    */
131   @Override
toString()132   public String toString() {
133     assert(isOwningHandle());
134     return toString(nativeHandle_);
135   }
136 
newStatistics()137   private native static long newStatistics();
newStatistics(final long otherStatisticsHandle)138   private native static long newStatistics(final long otherStatisticsHandle);
newStatistics(final byte[] ignoreHistograms)139   private native static long newStatistics(final byte[] ignoreHistograms);
newStatistics(final byte[] ignoreHistograms, final long otherStatisticsHandle)140   private native static long newStatistics(final byte[] ignoreHistograms, final long otherStatisticsHandle);
141 
disposeInternal(final long handle)142   @Override protected final native void disposeInternal(final long handle);
143 
statsLevel(final long handle)144   private native byte statsLevel(final long handle);
setStatsLevel(final long handle, final byte statsLevel)145   private native void setStatsLevel(final long handle, final byte statsLevel);
getTickerCount(final long handle, final byte tickerType)146   private native long getTickerCount(final long handle, final byte tickerType);
getAndResetTickerCount(final long handle, final byte tickerType)147   private native long getAndResetTickerCount(final long handle, final byte tickerType);
getHistogramData(final long handle, final byte histogramType)148   private native HistogramData getHistogramData(final long handle, final byte histogramType);
getHistogramString(final long handle, final byte histogramType)149   private native String getHistogramString(final long handle, final byte histogramType);
reset(final long nativeHandle)150   private native void reset(final long nativeHandle) throws RocksDBException;
toString(final long nativeHandle)151   private native String toString(final long nativeHandle);
152 }
153