Skip to content

Commit

Permalink
Lazy load native library in Statistics constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhubner committed Oct 13, 2023
1 parent 5b11f5a commit a7dc7f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ test: java java_test

run_test:
$(JAVA_CMD) $(JAVA_ARGS) -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH):target/*" org.rocksdb.test.RocksJunitRunner $(ALL_JAVA_TESTS)
$(JAVA_CMD) $(JAVA_ARGS) -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH):target/*" org.rocksdb.test.RocksJunitRunner org.rocksdb.StatisticsTest

run_plugin_test:
$(JAVA_CMD) $(JAVA_ARGS) -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH):target/*" org.rocksdb.test.RocksJunitRunner $(ROCKSDB_PLUGIN_JAVA_TESTS)
Expand Down
12 changes: 10 additions & 2 deletions java/src/main/java/org/rocksdb/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
public class Statistics extends RocksObject {

public Statistics() {
super(newStatistics());
super(newStatisticsInstance());
}

public Statistics(final Statistics otherStatistics) {
super(newStatistics(otherStatistics.nativeHandle_));
}

public Statistics(final EnumSet<HistogramType> ignoreHistograms) {
super(newStatistics(toArrayValues(ignoreHistograms)));
super(newStatisticsInstance(toArrayValues(ignoreHistograms)));
}

public Statistics(final EnumSet<HistogramType> ignoreHistograms, final Statistics otherStatistics) {
Expand Down Expand Up @@ -134,8 +134,16 @@ public String toString() {
return toString(nativeHandle_);
}

private static long newStatisticsInstance() {
RocksDB.loadLibrary();
return newStatistics();
}
private static native long newStatistics();
private static native long newStatistics(final long otherStatisticsHandle);
private static long newStatisticsInstance(final byte[] ignoreHistograms) {
RocksDB.loadLibrary();
return newStatistics(ignoreHistograms);
}
private static native long newStatistics(final byte[] ignoreHistograms);
private static native long newStatistics(
final byte[] ignoreHistograms, final long otherStatisticsHandle);
Expand Down
14 changes: 10 additions & 4 deletions java/src/test/java/org/rocksdb/StatisticsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@
import org.junit.rules.TemporaryFolder;

import java.nio.charset.StandardCharsets;
import java.util.EnumSet;

import static org.assertj.core.api.Assertions.assertThat;

public class StatisticsTest {

@ClassRule
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
new RocksNativeLibraryResource();

@Rule
public TemporaryFolder dbFolder = new TemporaryFolder();


@Test
public void createStatistics() throws RocksDBException {
final Statistics statistics = new Statistics();
statistics.setStatsLevel(StatsLevel.EXCEPT_DETAILED_TIMERS);
final Statistics statisticsWithHistogramOptions = new Statistics(EnumSet.of(HistogramType.DB_WRITE, HistogramType.COMPACTION_TIME));
statisticsWithHistogramOptions.reset();
}

@Test
public void statsLevel() throws RocksDBException {
final Statistics statistics = new Statistics();
Expand Down

0 comments on commit a7dc7f9

Please sign in to comment.