From 3e684aa68557f2589327cb7f5e3ce4aec140cfc5 Mon Sep 17 00:00:00 2001 From: fyrz Date: Tue, 2 Dec 2014 14:29:00 +0100 Subject: [PATCH] Integrated changes from D29571 --- java/org/rocksdb/CompressionType.java | 54 +++++++++++++++---- java/org/rocksdb/RocksDB.java | 16 +++--- java/org/rocksdb/benchmark/DbBenchmark.java | 35 ++++-------- .../rocksdb/test/CompressionOptionsTest.java | 22 ++++++++ java/org/rocksdb/test/MixedOptionsTest.java | 1 - .../rocksdb/test/PlainTableConfigTest.java | 4 -- 6 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 java/org/rocksdb/test/CompressionOptionsTest.java diff --git a/java/org/rocksdb/CompressionType.java b/java/org/rocksdb/CompressionType.java index 9c158ccf4..c718d26a9 100644 --- a/java/org/rocksdb/CompressionType.java +++ b/java/org/rocksdb/CompressionType.java @@ -14,25 +14,59 @@ package org.rocksdb; * compression method (if any) is used to compress a block.

*/ public enum CompressionType { - NO_COMPRESSION((byte) 0), - SNAPPY_COMPRESSION((byte) 1), - ZLIB_COMPRESSION((byte) 2), - BZLIB2_COMPRESSION((byte) 3), - LZ4_COMPRESSION((byte) 4), - LZ4HC_COMPRESSION((byte) 5); - private final byte value_; + NO_COMPRESSION((byte) 0, null), + SNAPPY_COMPRESSION((byte) 1, "snappy"), + ZLIB_COMPRESSION((byte) 2, "z"), + BZLIB2_COMPRESSION((byte) 3, "bzip2"), + LZ4_COMPRESSION((byte) 4, "lz4"), + LZ4HC_COMPRESSION((byte) 5, "lz4hc"); - private CompressionType(byte value) { - value_ = value; + /** + *

Get the CompressionType enumeration value by + * passing the library name to this method.

+ * + *

If library cannot be found the enumeration + * value {@code NO_COMPRESSION} will be returned.

+ * + * @return CompressionType instance. + */ + public static CompressionType getCompressionType(String libraryName) { + if (libraryName != null) { + for (CompressionType compressionType : CompressionType.values()) { + if (compressionType.getLibraryName() != null && + compressionType.getLibraryName().equals(libraryName)) { + return compressionType; + } + } + } + return CompressionType.NO_COMPRESSION; } /** - * Returns the byte value of the enumerations value + *

Returns the byte value of the enumerations value.

* * @return byte representation */ public byte getValue() { return value_; } + + /** + *

Returns the library name of the compression type + * identified by the enumeration value.

+ * + * @return library name + */ + public String getLibraryName() { + return libraryName_; + } + + private CompressionType(byte value, final String libraryName) { + value_ = value; + libraryName_ = libraryName; + } + + private final byte value_; + private final String libraryName_; } diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index bb88710ed..3d420adea 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -18,8 +18,6 @@ import org.rocksdb.util.Environment; public class RocksDB extends RocksObject { public static final String DEFAULT_COLUMN_FAMILY = "default"; public static final int NOT_FOUND = -1; - private static final String[] compressionLibs_ = { - "snappy", "z", "bzip2", "lz4", "lz4hc"}; static { RocksDB.loadLibrary(); @@ -35,9 +33,11 @@ public class RocksDB extends RocksObject { public static synchronized void loadLibrary() { String tmpDir = System.getenv("ROCKSDB_SHAREDLIB_DIR"); // loading possibly necessary libraries. - for (String lib : compressionLibs_) { + for (CompressionType compressionType : CompressionType.values()) { try { - System.loadLibrary(lib); + if (compressionType.getLibraryName() != null) { + System.loadLibrary(compressionType.getLibraryName()); + } } catch (UnsatisfiedLinkError e) { // since it may be optional, we ignore its loading failure here. } @@ -60,10 +60,14 @@ public class RocksDB extends RocksObject { * of a library. */ public static synchronized void loadLibrary(List paths) { - for (String lib : compressionLibs_) { + for (CompressionType compressionType : CompressionType.values()) { + if (compressionType.equals(CompressionType.NO_COMPRESSION)) { + continue; + } for (String path : paths) { try { - System.load(path + "/" + Environment.getSharedLibraryName(lib)); + System.load(path + "/" + Environment.getSharedLibraryName( + compressionType.getLibraryName())); break; } catch (UnsatisfiedLinkError e) { // since they are optional, we ignore loading fails. diff --git a/java/org/rocksdb/benchmark/DbBenchmark.java b/java/org/rocksdb/benchmark/DbBenchmark.java index 26b295f7b..64fc5f0a7 100644 --- a/java/org/rocksdb/benchmark/DbBenchmark.java +++ b/java/org/rocksdb/benchmark/DbBenchmark.java @@ -163,15 +163,6 @@ public class DbBenchmark { EXISTING } - enum CompressionType { - NONE, - SNAPPY, - ZLIB, - BZIP2, - LZ4, - LZ4HC - } - static { RocksDB.loadLibrary(); } @@ -457,24 +448,16 @@ public class DbBenchmark { // options.setPrefixSize((Integer)flags_.get(Flag.prefix_size)); // options.setKeysPerPrefix((Long)flags_.get(Flag.keys_per_prefix)); compressionType_ = (String) flags.get(Flag.compression_type); - compression_ = CompressionType.NONE; + compression_ = CompressionType.NO_COMPRESSION; try { - switch (compressionType_) { - case "snappy": - System.loadLibrary("snappy"); - break; - case "zlib": - System.loadLibrary("z"); - break; - case "bzip2": - System.loadLibrary("bzip2"); - break; - case "lz4": - System.loadLibrary("lz4"); - break; - case "lz4hc": - System.loadLibrary("lz4hc"); - break; + if (compressionType_!=null) { + final CompressionType compressionType = + CompressionType.getCompressionType(compressionType_); + if (compressionType != null && + compressionType != CompressionType.NO_COMPRESSION) { + System.loadLibrary(compressionType.getLibraryName()); + } + } } catch (UnsatisfiedLinkError e) { System.err.format("Unable to load %s library:%s%n" + diff --git a/java/org/rocksdb/test/CompressionOptionsTest.java b/java/org/rocksdb/test/CompressionOptionsTest.java new file mode 100644 index 000000000..f8aff9268 --- /dev/null +++ b/java/org/rocksdb/test/CompressionOptionsTest.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.test; + +import org.junit.Test; +import org.rocksdb.CompressionType; + + +public class CompressionOptionsTest +{ + @Test + public void getCompressionType() { + for (CompressionType compressionType : CompressionType.values()) { + String libraryName = compressionType.getLibraryName(); + compressionType.equals(CompressionType.getCompressionType( + libraryName)); + } + } +} diff --git a/java/org/rocksdb/test/MixedOptionsTest.java b/java/org/rocksdb/test/MixedOptionsTest.java index 0f15e668c..528bea2e3 100644 --- a/java/org/rocksdb/test/MixedOptionsTest.java +++ b/java/org/rocksdb/test/MixedOptionsTest.java @@ -53,6 +53,5 @@ public class MixedOptionsTest { options.optimizeUniversalStyleCompaction(400); options.optimizeForPointLookup(1024); options.prepareForBulkLoad(); - System.out.println("Mixed options test passed"); } } diff --git a/java/org/rocksdb/test/PlainTableConfigTest.java b/java/org/rocksdb/test/PlainTableConfigTest.java index a533141ea..72347e7d4 100644 --- a/java/org/rocksdb/test/PlainTableConfigTest.java +++ b/java/org/rocksdb/test/PlainTableConfigTest.java @@ -63,10 +63,6 @@ public class PlainTableConfigTest { public void encodingType() { PlainTableConfig plainTableConfig = new PlainTableConfig(); plainTableConfig.setEncodingType(EncodingType.kPrefix); - assertThat(EncodingType.valueOf("kPrefix")).isEqualTo( - EncodingType.kPrefix); - assertThat(EncodingType.values().length). - isEqualTo(2); assertThat(plainTableConfig.encodingType()).isEqualTo( EncodingType.kPrefix); }