From 10fc6c7d253a4755ce67901453bd12f7e8925619 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang Date: Wed, 23 Jul 2014 15:44:25 -0700 Subject: [PATCH] [Java] Add compaction style to options Summary: Add compression type to options make rocksdbjava make sample Reviewers: haobo, yhchiang, sdong, dhruba, rsumbaly, zzbennett, swapnilghike Reviewed By: yhchiang, sdong CC: leveldb Differential Revision: https://reviews.facebook.net/D20463 --- java/RocksDBSample.java | 4 +++- java/org/rocksdb/CompactionStyle.java | 22 +++++++++++++++++ java/org/rocksdb/Options.java | 24 +++++++++++++++++++ java/org/rocksdb/RocksDB.java | 1 - java/org/rocksdb/StatisticsCollector.java | 17 +++++++++---- .../rocksdb/test/StatisticsCollectorTest.java | 2 +- java/rocksjni/options.cc | 21 ++++++++++++++++ 7 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 java/org/rocksdb/CompactionStyle.java diff --git a/java/RocksDBSample.java b/java/RocksDBSample.java index f98c5e4c5..dfecde342 100644 --- a/java/RocksDBSample.java +++ b/java/RocksDBSample.java @@ -45,7 +45,8 @@ public class RocksDBSample { .setMaxBackgroundCompactions(10) .setFilter(filter) .setCacheNumShardBits(6) - .setCompressionType(CompressionType.SNAPPY_COMPRESSION); + .setCompressionType(CompressionType.SNAPPY_COMPRESSION) + .setCompactionStyle(CompactionStyle.UNIVERSAL); Statistics stats = options.statisticsPtr(); assert(options.createIfMissing() == true); @@ -56,6 +57,7 @@ public class RocksDBSample { assert(options.maxBackgroundCompactions() == 10); assert(options.cacheNumShardBits() == 6); assert(options.compressionType() == CompressionType.SNAPPY_COMPRESSION); + assert(options.compactionStyle() == CompactionStyle.UNIVERSAL); assert(options.memTableFactoryName().equals("SkipListFactory")); options.setMemTableConfig( diff --git a/java/org/rocksdb/CompactionStyle.java b/java/org/rocksdb/CompactionStyle.java new file mode 100644 index 000000000..5c41dfdd2 --- /dev/null +++ b/java/org/rocksdb/CompactionStyle.java @@ -0,0 +1,22 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +package org.rocksdb; + +public enum CompactionStyle { + LEVEL((byte) 0), + UNIVERSAL((byte) 1), + FIFO((byte) 2); + + private final byte value_; + + private CompactionStyle(byte value) { + value_ = value; + } + + public byte getValue() { + return value_; + } +} diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index e0e7e3e59..95f994606 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -1372,6 +1372,30 @@ public class Options extends RocksObject { return this; } private native void setCompressionType(long handle, byte compressionType); + + /** + * Compaction style for DB. + * + * @return Compaction style. + */ + public CompactionStyle compactionStyle() { + return CompactionStyle.values()[compactionStyle(nativeHandle_)]; + } + private native byte compactionStyle(long handle); + + /** + * Set compaction style for DB. + * + * Default: LEVEL. + * + * @param compactionStyle Compaction style. + * @return the reference to the current option. + */ + public Options setCompactionStyle(CompactionStyle compactionStyle) { + setCompactionStyle(nativeHandle_, compactionStyle.getValue()); + return this; + } + private native void setCompactionStyle(long handle, byte compactionStyle); /** * If true, place whole keys in the filter (not just prefixes). diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 9fca2df43..c7b06cc6d 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -119,7 +119,6 @@ public class RocksDB extends RocksObject { options.numShardBits_, path); db.storeOptionsInstance(options); - return db; } diff --git a/java/org/rocksdb/StatisticsCollector.java b/java/org/rocksdb/StatisticsCollector.java index 0aa03736d..29815c46d 100644 --- a/java/org/rocksdb/StatisticsCollector.java +++ b/java/org/rocksdb/StatisticsCollector.java @@ -46,13 +46,19 @@ public class StatisticsCollector { _executorService.submit(collectStatistics()); } - public void shutDown() throws InterruptedException { + /** + * Shuts down statistics collector. + * + * @param shutdownTimeout Time in milli-seconds to wait for shutdown before + * killing the collection process. + */ + public void shutDown(int shutdownTimeout) throws InterruptedException { _isRunning = false; - _executorService.shutdown(); + _executorService.shutdownNow(); // Wait for collectStatistics runnable to finish so that disposal of // statistics does not cause any exceptions to be thrown. - _executorService.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS); + _executorService.awaitTermination(shutdownTimeout, TimeUnit.MILLISECONDS); } private Runnable collectStatistics() { @@ -62,6 +68,9 @@ public class StatisticsCollector { public void run() { while (_isRunning) { try { + if(Thread.currentThread().isInterrupted()) { + break; + } for(StatsCollectorInput statsCollectorInput : _statsCollectorInputList) { Statistics statistics = statsCollectorInput.getStatistics(); @@ -86,7 +95,7 @@ public class StatisticsCollector { } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new RuntimeException("Thread got interrupted!", e); + break; } catch (Exception e) { throw new RuntimeException("Error while calculating statistics", e); diff --git a/java/org/rocksdb/test/StatisticsCollectorTest.java b/java/org/rocksdb/test/StatisticsCollectorTest.java index 20ccf8f87..e497d14df 100644 --- a/java/org/rocksdb/test/StatisticsCollectorTest.java +++ b/java/org/rocksdb/test/StatisticsCollectorTest.java @@ -33,7 +33,7 @@ public class StatisticsCollectorTest { assert(callback.tickerCallbackCount > 0); assert(callback.histCallbackCount > 0); - statsCollector.shutDown(); + statsCollector.shutDown(1000); db.close(); opt.dispose(); diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 763b28f5d..abbf598a7 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -956,6 +956,27 @@ jbyte Java_org_rocksdb_Options_compressionType( return reinterpret_cast(jhandle)->compression; } +/* + * Class: org_rocksdb_Options + * Method: setCompactionStyle + * Signature: (JB)V + */ +void Java_org_rocksdb_Options_setCompactionStyle( + JNIEnv* env, jobject jobj, jlong jhandle, jbyte compaction_style) { + reinterpret_cast(jhandle)->compaction_style = + static_cast(compaction_style); +} + +/* + * Class: org_rocksdb_Options + * Method: compactionStyle + * Signature: (J)B + */ +jbyte Java_org_rocksdb_Options_compactionStyle( + JNIEnv* env, jobject jobj, jlong jhandle) { + return reinterpret_cast(jhandle)->compaction_style; +} + /* * Class: org_rocksdb_Options * Method: wholeKeyFiltering