From 0345c2156f23d5b91d62bf45e320f93e3ac556ea Mon Sep 17 00:00:00 2001 From: fyrz Date: Tue, 28 Oct 2014 22:38:08 +0100 Subject: [PATCH 1/5] [RocksJava] Extend Options with ColumnFamilyOptions implementation ColumnFamilyOptions implementation with tests [RocksJava] Extended ColumnFamilyTest Summary: Options Refactoring split part 3 Test Plan: make rocksdbjava make jtest Reviewers: yhchiang, ankgup87 Subscribers: dhruba Differential Revision: https://reviews.facebook.net/D28023 --- java/Makefile | 4 +- java/org/rocksdb/ColumnFamilyOptions.java | 647 ++++++++++++++++++ java/org/rocksdb/Options.java | 14 + .../rocksdb/test/ColumnFamilyOptionsTest.java | 229 +++++++ java/org/rocksdb/test/MixedOptionsTest.java | 51 ++ java/org/rocksdb/test/OptionsTest.java | 206 +----- java/rocksjni/options.cc | 5 +- 7 files changed, 947 insertions(+), 209 deletions(-) create mode 100644 java/org/rocksdb/ColumnFamilyOptions.java create mode 100644 java/org/rocksdb/test/ColumnFamilyOptionsTest.java create mode 100644 java/org/rocksdb/test/MixedOptionsTest.java diff --git a/java/Makefile b/java/Makefile index 9fd714ee9..ef49f3fc9 100644 --- a/java/Makefile +++ b/java/Makefile @@ -77,14 +77,16 @@ test: java java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.BlockBasedTableConfigTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.DBOptionsTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ColumnFamilyTest + java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ColumnFamilyOptionsTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.FilterTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.FlushTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.KeyMayExistTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.MemTableTest + java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.MergeTest + java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.MixedOptionsTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.OptionsTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.PlainTableConfigTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ReadOnlyTest - java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.MergeTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ReadOptionsTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.RocksIteratorTest java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.SnapshotTest diff --git a/java/org/rocksdb/ColumnFamilyOptions.java b/java/org/rocksdb/ColumnFamilyOptions.java new file mode 100644 index 000000000..9ce1e9a98 --- /dev/null +++ b/java/org/rocksdb/ColumnFamilyOptions.java @@ -0,0 +1,647 @@ +// 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; + +/** + * ColumnFamilyOptions to control the behavior of a database. It will be used + * during the creation of a {@link org.rocksdb.RocksDB} (i.e., RocksDB.open()). + * + * If {@link #dispose()} function is not called, then it will be GC'd automatically + * and native resources will be released as part of the process. + */ +public class ColumnFamilyOptions extends RocksObject + implements ColumnFamilyOptionsInterface { + static { + RocksDB.loadLibrary(); + } + + /** + * Construct ColumnFamilyOptions. + * + * This constructor will create (by allocating a block of memory) + * an {@code rocksdb::DBOptions} in the c++ side. + */ + public ColumnFamilyOptions() { + super(); + newColumnFamilyOptions(); + } + + @Override + public ColumnFamilyOptions optimizeForPointLookup( + long blockCacheSizeMb) { + optimizeForPointLookup(nativeHandle_, + blockCacheSizeMb); + return this; + } + + @Override + public ColumnFamilyOptions optimizeLevelStyleCompaction() { + optimizeLevelStyleCompaction(nativeHandle_, + DEFAULT_COMPACTION_MEMTABLE_MEMORY_BUDGET); + return this; + } + + @Override + public ColumnFamilyOptions optimizeLevelStyleCompaction( + long memtableMemoryBudget) { + optimizeLevelStyleCompaction(nativeHandle_, + memtableMemoryBudget); + return this; + } + + @Override + public ColumnFamilyOptions optimizeUniversalStyleCompaction() { + optimizeUniversalStyleCompaction(nativeHandle_, + DEFAULT_COMPACTION_MEMTABLE_MEMORY_BUDGET); + return this; + } + + @Override + public ColumnFamilyOptions optimizeUniversalStyleCompaction( + long memtableMemoryBudget) { + optimizeUniversalStyleCompaction(nativeHandle_, + memtableMemoryBudget); + return this; + } + + @Override + public ColumnFamilyOptions setComparator(BuiltinComparator builtinComparator) { + assert(isInitialized()); + setComparatorHandle(nativeHandle_, builtinComparator.ordinal()); + return this; + } + + @Override + public ColumnFamilyOptions setComparator(AbstractComparator comparator) { + assert (isInitialized()); + setComparatorHandle(nativeHandle_, comparator.nativeHandle_); + comparator_ = comparator; + return this; + } + + @Override + public ColumnFamilyOptions setMergeOperatorName(String name) { + setMergeOperatorName(nativeHandle_, name); + return this; + } + + @Override + public ColumnFamilyOptions setMergeOperator(MergeOperator mergeOperator) { + setMergeOperator(nativeHandle_, mergeOperator.newMergeOperatorHandle()); + return this; + } + + @Override + public ColumnFamilyOptions setWriteBufferSize(long writeBufferSize) + throws RocksDBException { + assert(isInitialized()); + setWriteBufferSize(nativeHandle_, writeBufferSize); + return this; + } + + @Override + public long writeBufferSize() { + assert(isInitialized()); + return writeBufferSize(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMaxWriteBufferNumber( + int maxWriteBufferNumber) { + assert(isInitialized()); + setMaxWriteBufferNumber(nativeHandle_, maxWriteBufferNumber); + return this; + } + + @Override + public int maxWriteBufferNumber() { + assert(isInitialized()); + return maxWriteBufferNumber(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMinWriteBufferNumberToMerge( + int minWriteBufferNumberToMerge) { + setMinWriteBufferNumberToMerge(nativeHandle_, minWriteBufferNumberToMerge); + return this; + } + + @Override + public int minWriteBufferNumberToMerge() { + return minWriteBufferNumberToMerge(nativeHandle_); + } + + @Override + public ColumnFamilyOptions useFixedLengthPrefixExtractor(int n) { + assert(isInitialized()); + useFixedLengthPrefixExtractor(nativeHandle_, n); + return this; + } + + @Override + public ColumnFamilyOptions setCompressionType(CompressionType compressionType) { + setCompressionType(nativeHandle_, compressionType.getValue()); + return this; + } + + @Override + public CompressionType compressionType() { + return CompressionType.values()[compressionType(nativeHandle_)]; + } + + @Override + public ColumnFamilyOptions setNumLevels(int numLevels) { + setNumLevels(nativeHandle_, numLevels); + return this; + } + + @Override + public int numLevels() { + return numLevels(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setLevelZeroFileNumCompactionTrigger( + int numFiles) { + setLevelZeroFileNumCompactionTrigger( + nativeHandle_, numFiles); + return this; + } + + @Override + public int levelZeroFileNumCompactionTrigger() { + return levelZeroFileNumCompactionTrigger(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setLevelZeroSlowdownWritesTrigger( + int numFiles) { + setLevelZeroSlowdownWritesTrigger(nativeHandle_, numFiles); + return this; + } + + @Override + public int levelZeroSlowdownWritesTrigger() { + return levelZeroSlowdownWritesTrigger(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setLevelZeroStopWritesTrigger(int numFiles) { + setLevelZeroStopWritesTrigger(nativeHandle_, numFiles); + return this; + } + + @Override + public int levelZeroStopWritesTrigger() { + return levelZeroStopWritesTrigger(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMaxMemCompactionLevel( + int maxMemCompactionLevel) { + setMaxMemCompactionLevel(nativeHandle_, maxMemCompactionLevel); + return this; + } + + @Override + public int maxMemCompactionLevel() { + return maxMemCompactionLevel(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setTargetFileSizeBase(long targetFileSizeBase) { + setTargetFileSizeBase(nativeHandle_, targetFileSizeBase); + return this; + } + + @Override + public long targetFileSizeBase() { + return targetFileSizeBase(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setTargetFileSizeMultiplier(int multiplier) { + setTargetFileSizeMultiplier(nativeHandle_, multiplier); + return this; + } + + @Override + public int targetFileSizeMultiplier() { + return targetFileSizeMultiplier(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMaxBytesForLevelBase( + long maxBytesForLevelBase) { + setMaxBytesForLevelBase(nativeHandle_, maxBytesForLevelBase); + return this; + } + + @Override + public long maxBytesForLevelBase() { + return maxBytesForLevelBase(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMaxBytesForLevelMultiplier(int multiplier) { + setMaxBytesForLevelMultiplier(nativeHandle_, multiplier); + return this; + } + + @Override + public int maxBytesForLevelMultiplier() { + return maxBytesForLevelMultiplier(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setExpandedCompactionFactor(int expandedCompactionFactor) { + setExpandedCompactionFactor(nativeHandle_, expandedCompactionFactor); + return this; + } + + @Override + public int expandedCompactionFactor() { + return expandedCompactionFactor(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setSourceCompactionFactor(int sourceCompactionFactor) { + setSourceCompactionFactor(nativeHandle_, sourceCompactionFactor); + return this; + } + + @Override + public int sourceCompactionFactor() { + return sourceCompactionFactor(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMaxGrandparentOverlapFactor( + int maxGrandparentOverlapFactor) { + setMaxGrandparentOverlapFactor(nativeHandle_, maxGrandparentOverlapFactor); + return this; + } + + @Override + public int maxGrandparentOverlapFactor() { + return maxGrandparentOverlapFactor(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setSoftRateLimit(double softRateLimit) { + setSoftRateLimit(nativeHandle_, softRateLimit); + return this; + } + + @Override + public double softRateLimit() { + return softRateLimit(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setHardRateLimit(double hardRateLimit) { + setHardRateLimit(nativeHandle_, hardRateLimit); + return this; + } + + @Override + public double hardRateLimit() { + return hardRateLimit(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setRateLimitDelayMaxMilliseconds( + int rateLimitDelayMaxMilliseconds) { + setRateLimitDelayMaxMilliseconds( + nativeHandle_, rateLimitDelayMaxMilliseconds); + return this; + } + + @Override + public int rateLimitDelayMaxMilliseconds() { + return rateLimitDelayMaxMilliseconds(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setArenaBlockSize(long arenaBlockSize) + throws RocksDBException { + setArenaBlockSize(nativeHandle_, arenaBlockSize); + return this; + } + + @Override + public long arenaBlockSize() { + return arenaBlockSize(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setDisableAutoCompactions(boolean disableAutoCompactions) { + setDisableAutoCompactions(nativeHandle_, disableAutoCompactions); + return this; + } + + @Override + public boolean disableAutoCompactions() { + return disableAutoCompactions(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setPurgeRedundantKvsWhileFlush( + boolean purgeRedundantKvsWhileFlush) { + setPurgeRedundantKvsWhileFlush( + nativeHandle_, purgeRedundantKvsWhileFlush); + return this; + } + + @Override + public boolean purgeRedundantKvsWhileFlush() { + return purgeRedundantKvsWhileFlush(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setCompactionStyle(CompactionStyle compactionStyle) { + setCompactionStyle(nativeHandle_, compactionStyle.getValue()); + return this; + } + + @Override + public CompactionStyle compactionStyle() { + return CompactionStyle.values()[compactionStyle(nativeHandle_)]; + } + + @Override + public ColumnFamilyOptions setVerifyChecksumsInCompaction( + boolean verifyChecksumsInCompaction) { + setVerifyChecksumsInCompaction( + nativeHandle_, verifyChecksumsInCompaction); + return this; + } + + @Override + public boolean verifyChecksumsInCompaction() { + return verifyChecksumsInCompaction(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setFilterDeletes(boolean filterDeletes) { + setFilterDeletes(nativeHandle_, filterDeletes); + return this; + } + + @Override + public boolean filterDeletes() { + return filterDeletes(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMaxSequentialSkipInIterations(long maxSequentialSkipInIterations) { + setMaxSequentialSkipInIterations(nativeHandle_, maxSequentialSkipInIterations); + return this; + } + + @Override + public long maxSequentialSkipInIterations() { + return maxSequentialSkipInIterations(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMemTableConfig(MemTableConfig config) + throws RocksDBException { + memTableConfig_ = config; + setMemTableFactory(nativeHandle_, config.newMemTableFactoryHandle()); + return this; + } + + @Override + public String memTableFactoryName() { + assert(isInitialized()); + return memTableFactoryName(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setTableFormatConfig(TableFormatConfig config) { + tableFormatConfig_ = config; + setTableFactory(nativeHandle_, config.newTableFactoryHandle()); + return this; + } + + @Override + public String tableFactoryName() { + assert(isInitialized()); + return tableFactoryName(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setInplaceUpdateSupport(boolean inplaceUpdateSupport) { + setInplaceUpdateSupport(nativeHandle_, inplaceUpdateSupport); + return this; + } + + @Override + public boolean inplaceUpdateSupport() { + return inplaceUpdateSupport(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setInplaceUpdateNumLocks(long inplaceUpdateNumLocks) + throws RocksDBException { + setInplaceUpdateNumLocks(nativeHandle_, inplaceUpdateNumLocks); + return this; + } + + @Override + public long inplaceUpdateNumLocks() { + return inplaceUpdateNumLocks(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMemtablePrefixBloomBits(int memtablePrefixBloomBits) { + setMemtablePrefixBloomBits(nativeHandle_, memtablePrefixBloomBits); + return this; + } + + @Override + public int memtablePrefixBloomBits() { + return memtablePrefixBloomBits(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMemtablePrefixBloomProbes(int memtablePrefixBloomProbes) { + setMemtablePrefixBloomProbes(nativeHandle_, memtablePrefixBloomProbes); + return this; + } + + @Override + public int memtablePrefixBloomProbes() { + return memtablePrefixBloomProbes(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setBloomLocality(int bloomLocality) { + setBloomLocality(nativeHandle_, bloomLocality); + return this; + } + + @Override + public int bloomLocality() { + return bloomLocality(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMaxSuccessiveMerges(long maxSuccessiveMerges) + throws RocksDBException { + setMaxSuccessiveMerges(nativeHandle_, maxSuccessiveMerges); + return this; + } + + @Override + public long maxSuccessiveMerges() { + return maxSuccessiveMerges(nativeHandle_); + } + + @Override + public ColumnFamilyOptions setMinPartialMergeOperands(int minPartialMergeOperands) { + setMinPartialMergeOperands(nativeHandle_, minPartialMergeOperands); + return this; + } + + @Override + public int minPartialMergeOperands() { + return minPartialMergeOperands(nativeHandle_); + } + + /** + * Release the memory allocated for the current instance + * in the c++ side. + */ + @Override protected void disposeInternal() { + assert(isInitialized()); + disposeInternal(nativeHandle_); + } + + private native void newColumnFamilyOptions(); + private native void disposeInternal(long handle); + + private native void optimizeForPointLookup(long handle, + long blockCacheSizeMb); + private native void optimizeLevelStyleCompaction(long handle, + long memtableMemoryBudget); + private native void optimizeUniversalStyleCompaction(long handle, + long memtableMemoryBudget); + private native void setComparatorHandle(long handle, int builtinComparator); + private native void setComparatorHandle(long optHandle, long comparatorHandle); + private native void setMergeOperatorName( + long handle, String name); + private native void setMergeOperator( + long handle, long mergeOperatorHandle); + private native void setWriteBufferSize(long handle, long writeBufferSize) + throws RocksDBException; + private native long writeBufferSize(long handle); + private native void setMaxWriteBufferNumber( + long handle, int maxWriteBufferNumber); + private native int maxWriteBufferNumber(long handle); + private native void setMinWriteBufferNumberToMerge( + long handle, int minWriteBufferNumberToMerge); + private native int minWriteBufferNumberToMerge(long handle); + private native void setCompressionType(long handle, byte compressionType); + private native byte compressionType(long handle); + private native void useFixedLengthPrefixExtractor( + long handle, int prefixLength); + private native void setNumLevels( + long handle, int numLevels); + private native int numLevels(long handle); + private native void setLevelZeroFileNumCompactionTrigger( + long handle, int numFiles); + private native int levelZeroFileNumCompactionTrigger(long handle); + private native void setLevelZeroSlowdownWritesTrigger( + long handle, int numFiles); + private native int levelZeroSlowdownWritesTrigger(long handle); + private native void setLevelZeroStopWritesTrigger( + long handle, int numFiles); + private native int levelZeroStopWritesTrigger(long handle); + private native void setMaxMemCompactionLevel( + long handle, int maxMemCompactionLevel); + private native int maxMemCompactionLevel(long handle); + private native void setTargetFileSizeBase( + long handle, long targetFileSizeBase); + private native long targetFileSizeBase(long handle); + private native void setTargetFileSizeMultiplier( + long handle, int multiplier); + private native int targetFileSizeMultiplier(long handle); + private native void setMaxBytesForLevelBase( + long handle, long maxBytesForLevelBase); + private native long maxBytesForLevelBase(long handle); + private native void setMaxBytesForLevelMultiplier( + long handle, int multiplier); + private native int maxBytesForLevelMultiplier(long handle); + private native void setExpandedCompactionFactor( + long handle, int expandedCompactionFactor); + private native int expandedCompactionFactor(long handle); + private native void setSourceCompactionFactor( + long handle, int sourceCompactionFactor); + private native int sourceCompactionFactor(long handle); + private native void setMaxGrandparentOverlapFactor( + long handle, int maxGrandparentOverlapFactor); + private native int maxGrandparentOverlapFactor(long handle); + private native void setSoftRateLimit( + long handle, double softRateLimit); + private native double softRateLimit(long handle); + private native void setHardRateLimit( + long handle, double hardRateLimit); + private native double hardRateLimit(long handle); + private native void setRateLimitDelayMaxMilliseconds( + long handle, int rateLimitDelayMaxMilliseconds); + private native int rateLimitDelayMaxMilliseconds(long handle); + private native void setArenaBlockSize( + long handle, long arenaBlockSize) throws RocksDBException; + private native long arenaBlockSize(long handle); + private native void setDisableAutoCompactions( + long handle, boolean disableAutoCompactions); + private native boolean disableAutoCompactions(long handle); + private native void setCompactionStyle(long handle, byte compactionStyle); + private native byte compactionStyle(long handle); + private native void setPurgeRedundantKvsWhileFlush( + long handle, boolean purgeRedundantKvsWhileFlush); + private native boolean purgeRedundantKvsWhileFlush(long handle); + private native void setVerifyChecksumsInCompaction( + long handle, boolean verifyChecksumsInCompaction); + private native boolean verifyChecksumsInCompaction(long handle); + private native void setFilterDeletes( + long handle, boolean filterDeletes); + private native boolean filterDeletes(long handle); + private native void setMaxSequentialSkipInIterations( + long handle, long maxSequentialSkipInIterations); + private native long maxSequentialSkipInIterations(long handle); + private native void setMemTableFactory(long handle, long factoryHandle); + private native String memTableFactoryName(long handle); + private native void setTableFactory(long handle, long factoryHandle); + private native String tableFactoryName(long handle); + private native void setInplaceUpdateSupport( + long handle, boolean inplaceUpdateSupport); + private native boolean inplaceUpdateSupport(long handle); + private native void setInplaceUpdateNumLocks( + long handle, long inplaceUpdateNumLocks) throws RocksDBException; + private native long inplaceUpdateNumLocks(long handle); + private native void setMemtablePrefixBloomBits( + long handle, int memtablePrefixBloomBits); + private native int memtablePrefixBloomBits(long handle); + private native void setMemtablePrefixBloomProbes( + long handle, int memtablePrefixBloomProbes); + private native int memtablePrefixBloomProbes(long handle); + private native void setBloomLocality( + long handle, int bloomLocality); + private native int bloomLocality(long handle); + private native void setMaxSuccessiveMerges( + long handle, long maxSuccessiveMerges) throws RocksDBException; + private native long maxSuccessiveMerges(long handle); + private native void setMinPartialMergeOperands( + long handle, int minPartialMergeOperands); + private native int minPartialMergeOperands(long handle); + + MemTableConfig memTableConfig_; + TableFormatConfig tableFormatConfig_; + AbstractComparator comparator_; +} diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index 7ad1e1bf2..2d6fa08cd 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -29,6 +29,20 @@ public class Options extends RocksObject env_ = RocksEnv.getDefault(); } + /** + * Construct options for opening a RocksDB. Reusing database options + * and column family options. + * + * @param dbOptions {@link org.rocksdb.DBOptions} instance + * @param columnFamilyOptions {@link org.rocksdb.ColumnFamilyOptions} + * instance + */ + public Options(DBOptions dbOptions, ColumnFamilyOptions columnFamilyOptions) { + super(); + newOptions(dbOptions.nativeHandle_, columnFamilyOptions.nativeHandle_); + env_ = RocksEnv.getDefault(); + } + @Override public Options setCreateIfMissing(boolean flag) { assert(isInitialized()); diff --git a/java/org/rocksdb/test/ColumnFamilyOptionsTest.java b/java/org/rocksdb/test/ColumnFamilyOptionsTest.java new file mode 100644 index 000000000..95289a301 --- /dev/null +++ b/java/org/rocksdb/test/ColumnFamilyOptionsTest.java @@ -0,0 +1,229 @@ +// 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.rocksdb.*; + +import java.util.Random; + +public class ColumnFamilyOptionsTest { + static { + RocksDB.loadLibrary(); + } + + public static void testCFOptions(ColumnFamilyOptionsInterface opt) { + Random rand = PlatformRandomHelper. + getPlatformSpecificRandomFactory(); + { // WriteBufferSize test + try { + long longValue = rand.nextLong(); + opt.setWriteBufferSize(longValue); + assert(opt.writeBufferSize() == longValue); + } catch (RocksDBException e) { + assert(false); + } + } + + { // MaxWriteBufferNumber test + int intValue = rand.nextInt(); + opt.setMaxWriteBufferNumber(intValue); + assert(opt.maxWriteBufferNumber() == intValue); + } + + { // MinWriteBufferNumberToMerge test + int intValue = rand.nextInt(); + opt.setMinWriteBufferNumberToMerge(intValue); + assert(opt.minWriteBufferNumberToMerge() == intValue); + } + + { // NumLevels test + int intValue = rand.nextInt(); + opt.setNumLevels(intValue); + assert(opt.numLevels() == intValue); + } + + { // LevelFileNumCompactionTrigger test + int intValue = rand.nextInt(); + opt.setLevelZeroFileNumCompactionTrigger(intValue); + assert(opt.levelZeroFileNumCompactionTrigger() == intValue); + } + + { // LevelSlowdownWritesTrigger test + int intValue = rand.nextInt(); + opt.setLevelZeroSlowdownWritesTrigger(intValue); + assert(opt.levelZeroSlowdownWritesTrigger() == intValue); + } + + { // LevelStopWritesTrigger test + int intValue = rand.nextInt(); + opt.setLevelZeroStopWritesTrigger(intValue); + assert(opt.levelZeroStopWritesTrigger() == intValue); + } + + { // MaxMemCompactionLevel test + int intValue = rand.nextInt(); + opt.setMaxMemCompactionLevel(intValue); + assert(opt.maxMemCompactionLevel() == intValue); + } + + { // TargetFileSizeBase test + long longValue = rand.nextLong(); + opt.setTargetFileSizeBase(longValue); + assert(opt.targetFileSizeBase() == longValue); + } + + { // TargetFileSizeMultiplier test + int intValue = rand.nextInt(); + opt.setTargetFileSizeMultiplier(intValue); + assert(opt.targetFileSizeMultiplier() == intValue); + } + + { // MaxBytesForLevelBase test + long longValue = rand.nextLong(); + opt.setMaxBytesForLevelBase(longValue); + assert(opt.maxBytesForLevelBase() == longValue); + } + + { // MaxBytesForLevelMultiplier test + int intValue = rand.nextInt(); + opt.setMaxBytesForLevelMultiplier(intValue); + assert(opt.maxBytesForLevelMultiplier() == intValue); + } + + { // ExpandedCompactionFactor test + int intValue = rand.nextInt(); + opt.setExpandedCompactionFactor(intValue); + assert(opt.expandedCompactionFactor() == intValue); + } + + { // SourceCompactionFactor test + int intValue = rand.nextInt(); + opt.setSourceCompactionFactor(intValue); + assert(opt.sourceCompactionFactor() == intValue); + } + + { // MaxGrandparentOverlapFactor test + int intValue = rand.nextInt(); + opt.setMaxGrandparentOverlapFactor(intValue); + assert(opt.maxGrandparentOverlapFactor() == intValue); + } + + { // SoftRateLimit test + double doubleValue = rand.nextDouble(); + opt.setSoftRateLimit(doubleValue); + assert(opt.softRateLimit() == doubleValue); + } + + { // HardRateLimit test + double doubleValue = rand.nextDouble(); + opt.setHardRateLimit(doubleValue); + assert(opt.hardRateLimit() == doubleValue); + } + + { // RateLimitDelayMaxMilliseconds test + int intValue = rand.nextInt(); + opt.setRateLimitDelayMaxMilliseconds(intValue); + assert(opt.rateLimitDelayMaxMilliseconds() == intValue); + } + + { // ArenaBlockSize test + try { + long longValue = rand.nextLong(); + opt.setArenaBlockSize(longValue); + assert(opt.arenaBlockSize() == longValue); + } catch (RocksDBException e) { + assert(false); + } + } + + { // DisableAutoCompactions test + boolean boolValue = rand.nextBoolean(); + opt.setDisableAutoCompactions(boolValue); + assert(opt.disableAutoCompactions() == boolValue); + } + + { // PurgeRedundantKvsWhileFlush test + boolean boolValue = rand.nextBoolean(); + opt.setPurgeRedundantKvsWhileFlush(boolValue); + assert(opt.purgeRedundantKvsWhileFlush() == boolValue); + } + + { // VerifyChecksumsInCompaction test + boolean boolValue = rand.nextBoolean(); + opt.setVerifyChecksumsInCompaction(boolValue); + assert(opt.verifyChecksumsInCompaction() == boolValue); + } + + { // FilterDeletes test + boolean boolValue = rand.nextBoolean(); + opt.setFilterDeletes(boolValue); + assert(opt.filterDeletes() == boolValue); + } + + { // MaxSequentialSkipInIterations test + long longValue = rand.nextLong(); + opt.setMaxSequentialSkipInIterations(longValue); + assert(opt.maxSequentialSkipInIterations() == longValue); + } + + { // InplaceUpdateSupport test + boolean boolValue = rand.nextBoolean(); + opt.setInplaceUpdateSupport(boolValue); + assert(opt.inplaceUpdateSupport() == boolValue); + } + + { // InplaceUpdateNumLocks test + try { + long longValue = rand.nextLong(); + opt.setInplaceUpdateNumLocks(longValue); + assert(opt.inplaceUpdateNumLocks() == longValue); + } catch (RocksDBException e) { + assert(false); + } + } + + { // MemtablePrefixBloomBits test + int intValue = rand.nextInt(); + opt.setMemtablePrefixBloomBits(intValue); + assert(opt.memtablePrefixBloomBits() == intValue); + } + + { // MemtablePrefixBloomProbes test + int intValue = rand.nextInt(); + opt.setMemtablePrefixBloomProbes(intValue); + assert(opt.memtablePrefixBloomProbes() == intValue); + } + + { // BloomLocality test + int intValue = rand.nextInt(); + opt.setBloomLocality(intValue); + assert(opt.bloomLocality() == intValue); + } + + { // MaxSuccessiveMerges test + try { + long longValue = rand.nextLong(); + opt.setMaxSuccessiveMerges(longValue); + assert(opt.maxSuccessiveMerges() == longValue); + } catch (RocksDBException e){ + assert(false); + } + } + + { // MinPartialMergeOperands test + int intValue = rand.nextInt(); + opt.setMinPartialMergeOperands(intValue); + assert(opt.minPartialMergeOperands() == intValue); + } + } + + public static void main(String[] args) { + ColumnFamilyOptions opt = new ColumnFamilyOptions(); + testCFOptions(opt); + opt.dispose(); + System.out.println("Passed DBOptionsTest"); + } +} diff --git a/java/org/rocksdb/test/MixedOptionsTest.java b/java/org/rocksdb/test/MixedOptionsTest.java new file mode 100644 index 000000000..edaa2c318 --- /dev/null +++ b/java/org/rocksdb/test/MixedOptionsTest.java @@ -0,0 +1,51 @@ +// 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.rocksdb.*; + +public class MixedOptionsTest { + static { + RocksDB.loadLibrary(); + } + public static void main(String[] args) { + // Set a table factory and check the names + ColumnFamilyOptions cfOptions = new ColumnFamilyOptions(); + cfOptions.setTableFormatConfig(new BlockBasedTableConfig(). + setFilter(new BloomFilter())); + assert(cfOptions.tableFactoryName().equals( + "BlockBasedTable")); + cfOptions.setTableFormatConfig(new PlainTableConfig()); + assert(cfOptions.tableFactoryName().equals("PlainTable")); + // Initialize a dbOptions object from cf options and + // db options + DBOptions dbOptions = new DBOptions(); + Options options = new Options(dbOptions, cfOptions); + assert(options.tableFactoryName().equals("PlainTable")); + // Free instances + options.dispose(); + options = null; + cfOptions.dispose(); + cfOptions = null; + dbOptions.dispose(); + dbOptions = null; + System.gc(); + System.runFinalization(); + // Test Optimize for statements + cfOptions = new ColumnFamilyOptions(); + cfOptions.optimizeUniversalStyleCompaction(); + cfOptions.optimizeLevelStyleCompaction(); + cfOptions.optimizeForPointLookup(1024); + options = new Options(); + options.optimizeLevelStyleCompaction(); + options.optimizeLevelStyleCompaction(400); + options.optimizeUniversalStyleCompaction(); + options.optimizeUniversalStyleCompaction(400); + options.optimizeForPointLookup(1024); + options.prepareForBulkLoad(); + System.out.println("Mixed options test passed"); + } +} diff --git a/java/org/rocksdb/test/OptionsTest.java b/java/org/rocksdb/test/OptionsTest.java index ea8da6c66..defdcc304 100644 --- a/java/org/rocksdb/test/OptionsTest.java +++ b/java/org/rocksdb/test/OptionsTest.java @@ -6,10 +6,7 @@ package org.rocksdb.test; import java.util.Random; - -import org.rocksdb.DBOptions; import org.rocksdb.RocksDB; -import org.rocksdb.RocksDBException; import org.rocksdb.Options; public class OptionsTest { @@ -23,208 +20,7 @@ public class OptionsTest { getPlatformSpecificRandomFactory(); DBOptionsTest.testDBOptions(opt); - - { // WriteBufferSize test - try { - long longValue = rand.nextLong(); - opt.setWriteBufferSize(longValue); - assert(opt.writeBufferSize() == longValue); - } catch (RocksDBException e) { - assert(false); - } - } - - { // MaxWriteBufferNumber test - int intValue = rand.nextInt(); - opt.setMaxWriteBufferNumber(intValue); - assert(opt.maxWriteBufferNumber() == intValue); - } - - { // MinWriteBufferNumberToMerge test - int intValue = rand.nextInt(); - opt.setMinWriteBufferNumberToMerge(intValue); - assert(opt.minWriteBufferNumberToMerge() == intValue); - } - - { // NumLevels test - int intValue = rand.nextInt(); - opt.setNumLevels(intValue); - assert(opt.numLevels() == intValue); - } - - { // LevelFileNumCompactionTrigger test - int intValue = rand.nextInt(); - opt.setLevelZeroFileNumCompactionTrigger(intValue); - assert(opt.levelZeroFileNumCompactionTrigger() == intValue); - } - - { // LevelSlowdownWritesTrigger test - int intValue = rand.nextInt(); - opt.setLevelZeroSlowdownWritesTrigger(intValue); - assert(opt.levelZeroSlowdownWritesTrigger() == intValue); - } - - { // LevelStopWritesTrigger test - int intValue = rand.nextInt(); - opt.setLevelZeroStopWritesTrigger(intValue); - assert(opt.levelZeroStopWritesTrigger() == intValue); - } - - { // MaxMemCompactionLevel test - int intValue = rand.nextInt(); - opt.setMaxMemCompactionLevel(intValue); - assert(opt.maxMemCompactionLevel() == intValue); - } - - { // TargetFileSizeBase test - long longValue = rand.nextLong(); - opt.setTargetFileSizeBase(longValue); - assert(opt.targetFileSizeBase() == longValue); - } - - { // TargetFileSizeMultiplier test - int intValue = rand.nextInt(); - opt.setTargetFileSizeMultiplier(intValue); - assert(opt.targetFileSizeMultiplier() == intValue); - } - - { // MaxBytesForLevelBase test - long longValue = rand.nextLong(); - opt.setMaxBytesForLevelBase(longValue); - assert(opt.maxBytesForLevelBase() == longValue); - } - - { // MaxBytesForLevelMultiplier test - int intValue = rand.nextInt(); - opt.setMaxBytesForLevelMultiplier(intValue); - assert(opt.maxBytesForLevelMultiplier() == intValue); - } - - { // ExpandedCompactionFactor test - int intValue = rand.nextInt(); - opt.setExpandedCompactionFactor(intValue); - assert(opt.expandedCompactionFactor() == intValue); - } - - { // SourceCompactionFactor test - int intValue = rand.nextInt(); - opt.setSourceCompactionFactor(intValue); - assert(opt.sourceCompactionFactor() == intValue); - } - - { // MaxGrandparentOverlapFactor test - int intValue = rand.nextInt(); - opt.setMaxGrandparentOverlapFactor(intValue); - assert(opt.maxGrandparentOverlapFactor() == intValue); - } - - { // SoftRateLimit test - double doubleValue = rand.nextDouble(); - opt.setSoftRateLimit(doubleValue); - assert(opt.softRateLimit() == doubleValue); - } - - { // HardRateLimit test - double doubleValue = rand.nextDouble(); - opt.setHardRateLimit(doubleValue); - assert(opt.hardRateLimit() == doubleValue); - } - - { // RateLimitDelayMaxMilliseconds test - int intValue = rand.nextInt(); - opt.setRateLimitDelayMaxMilliseconds(intValue); - assert(opt.rateLimitDelayMaxMilliseconds() == intValue); - } - - { // ArenaBlockSize test - try { - long longValue = rand.nextLong(); - opt.setArenaBlockSize(longValue); - assert(opt.arenaBlockSize() == longValue); - } catch (RocksDBException e) { - assert(false); - } - } - - { // DisableAutoCompactions test - boolean boolValue = rand.nextBoolean(); - opt.setDisableAutoCompactions(boolValue); - assert(opt.disableAutoCompactions() == boolValue); - } - - { // PurgeRedundantKvsWhileFlush test - boolean boolValue = rand.nextBoolean(); - opt.setPurgeRedundantKvsWhileFlush(boolValue); - assert(opt.purgeRedundantKvsWhileFlush() == boolValue); - } - - { // VerifyChecksumsInCompaction test - boolean boolValue = rand.nextBoolean(); - opt.setVerifyChecksumsInCompaction(boolValue); - assert(opt.verifyChecksumsInCompaction() == boolValue); - } - - { // FilterDeletes test - boolean boolValue = rand.nextBoolean(); - opt.setFilterDeletes(boolValue); - assert(opt.filterDeletes() == boolValue); - } - - { // MaxSequentialSkipInIterations test - long longValue = rand.nextLong(); - opt.setMaxSequentialSkipInIterations(longValue); - assert(opt.maxSequentialSkipInIterations() == longValue); - } - - { // InplaceUpdateSupport test - boolean boolValue = rand.nextBoolean(); - opt.setInplaceUpdateSupport(boolValue); - assert(opt.inplaceUpdateSupport() == boolValue); - } - - { // InplaceUpdateNumLocks test - try { - long longValue = rand.nextLong(); - opt.setInplaceUpdateNumLocks(longValue); - assert(opt.inplaceUpdateNumLocks() == longValue); - } catch (RocksDBException e) { - assert(false); - } - } - - { // MemtablePrefixBloomBits test - int intValue = rand.nextInt(); - opt.setMemtablePrefixBloomBits(intValue); - assert(opt.memtablePrefixBloomBits() == intValue); - } - - { // MemtablePrefixBloomProbes test - int intValue = rand.nextInt(); - opt.setMemtablePrefixBloomProbes(intValue); - assert(opt.memtablePrefixBloomProbes() == intValue); - } - - { // BloomLocality test - int intValue = rand.nextInt(); - opt.setBloomLocality(intValue); - assert(opt.bloomLocality() == intValue); - } - - { // MaxSuccessiveMerges test - try { - long longValue = rand.nextLong(); - opt.setMaxSuccessiveMerges(longValue); - assert(opt.maxSuccessiveMerges() == longValue); - } catch (RocksDBException e){ - assert(false); - } - } - - { // MinPartialMergeOperands test - int intValue = rand.nextInt(); - opt.setMinPartialMergeOperands(intValue); - assert(opt.minPartialMergeOperands() == intValue); - } + ColumnFamilyOptionsTest.testCFOptions(opt); opt.dispose(); System.out.println("Passed OptionsTest"); diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index de614594f..7ce685d43 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -1740,9 +1740,8 @@ void Java_org_rocksdb_Options_prepareForBulkLoad( */ void Java_org_rocksdb_ColumnFamilyOptions_newColumnFamilyOptions( JNIEnv* env, jobject jobj) { - // TODO(fyrz) needs to be enabled back when ColumnFamilyOptions are available - // rocksdb::ColumnFamilyOptions* op = new rocksdb::ColumnFamilyOptions(); - // rocksdb::ColumnFamilyOptionsJni::setHandle(env, jobj, op); + rocksdb::ColumnFamilyOptions* op = new rocksdb::ColumnFamilyOptions(); + rocksdb::ColumnFamilyOptionsJni::setHandle(env, jobj, op); } /* From 75010d2084563c74fdb341efb37e61abfa158fdb Mon Sep 17 00:00:00 2001 From: fyrz Date: Fri, 31 Oct 2014 23:39:14 +0100 Subject: [PATCH 2/5] [RocksJava] ColumnFamily custom Options API extension ********************* *************************** ******** ************* ******** ******** *********** ******** ******** ********* ******** ************************************* ************************************* ************************************* ****** *** *** ****** ****** *** *** *** ****** ****** *** ****** *************************** ********************* --- java/Makefile | 1 + java/org/rocksdb/ColumnFamilyDescriptor.java | 58 +++++++++++++++ java/org/rocksdb/RocksDB.java | 52 ++++++++------ java/org/rocksdb/test/ColumnFamilyTest.java | 13 ++-- java/org/rocksdb/test/KeyMayExistTest.java | 7 +- java/org/rocksdb/test/MergeTest.java | 31 +++++--- java/org/rocksdb/test/ReadOnlyTest.java | 18 +++-- java/rocksjni/options.cc | 3 +- java/rocksjni/portal.h | 28 ++++++++ java/rocksjni/rocksjni.cc | 74 ++++++++++++++------ 10 files changed, 216 insertions(+), 69 deletions(-) create mode 100644 java/org/rocksdb/ColumnFamilyDescriptor.java diff --git a/java/Makefile b/java/Makefile index ef49f3fc9..a4bee144a 100644 --- a/java/Makefile +++ b/java/Makefile @@ -5,6 +5,7 @@ NATIVE_JAVA_CLASSES = org.rocksdb.AbstractComparator\ org.rocksdb.BlockBasedTableConfig\ org.rocksdb.BloomFilter\ org.rocksdb.ColumnFamilyHandle\ + org.rocksdb.ColumnFamilyOptions\ org.rocksdb.Comparator\ org.rocksdb.ComparatorOptions\ org.rocksdb.DBOptions\ diff --git a/java/org/rocksdb/ColumnFamilyDescriptor.java b/java/org/rocksdb/ColumnFamilyDescriptor.java new file mode 100644 index 000000000..b01c0e858 --- /dev/null +++ b/java/org/rocksdb/ColumnFamilyDescriptor.java @@ -0,0 +1,58 @@ +// 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; + +/** + *

Describes a column family with a + * name and respective Options.

+ */ +public class ColumnFamilyDescriptor { + + /** + *

Creates a new Column Family using a name and default + * options,

+ * + * @param columnFamilyName name of column family. + */ + public ColumnFamilyDescriptor(final String columnFamilyName){ + this(columnFamilyName, new ColumnFamilyOptions()); + } + + /** + *

Creates a new Column Family using a name and custom + * options.

+ * + * @param columnFamilyName name of column family. + * @param columnFamilyOptions options to be used with + * column family. + */ + public ColumnFamilyDescriptor(final String columnFamilyName, + final ColumnFamilyOptions columnFamilyOptions) { + columnFamilyName_ = columnFamilyName; + columnFamilyOptions_ = columnFamilyOptions; + } + + /** + * Retrieve name of column family. + * + * @return column family name. + */ + public String columnFamilyName() { + return columnFamilyName_; + } + + /** + * Retrieve assigned options instance. + * + * @return Options instance assigned to this instance. + */ + public ColumnFamilyOptions columnFamilyOptions() { + return columnFamilyOptions_; + } + + private final String columnFamilyName_; + private final ColumnFamilyOptions columnFamilyOptions_; +} diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 8efdaea1f..4b580f81f 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -123,7 +123,7 @@ public class RocksDB extends RocksObject { *

* * @param path the path to the rocksdb. - * @param columnFamilyNames list of column family names + * @param columnFamilyDescriptors list of column family descriptors * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances * on open. * @return a {@link RocksDB} instance on success, null if the specified @@ -132,12 +132,13 @@ public class RocksDB extends RocksObject { * @throws org.rocksdb.RocksDBException * @see Options#setCreateIfMissing(boolean) */ - public static RocksDB open(String path, List columnFamilyNames, + public static RocksDB open(String path, + List columnFamilyDescriptors, List columnFamilyHandles) throws RocksDBException { // This allows to use the rocksjni default Options instead of // the c++ one. Options options = new Options(); - return open(options, path, columnFamilyNames, columnFamilyHandles); + return open(options, path, columnFamilyDescriptors, columnFamilyHandles); } /** @@ -198,7 +199,7 @@ public class RocksDB extends RocksObject { * * @param options {@link org.rocksdb.Options} instance. * @param path the path to the rocksdb. - * @param columnFamilyNames list of column family names + * @param columnFamilyDescriptors list of column family descriptors * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances * on open. * @return a {@link RocksDB} instance on success, null if the specified @@ -207,13 +208,14 @@ public class RocksDB extends RocksObject { * @throws org.rocksdb.RocksDBException * @see Options#setCreateIfMissing(boolean) */ - public static RocksDB open(Options options, String path, List columnFamilyNames, + public static RocksDB open(Options options, String path, + List columnFamilyDescriptors, List columnFamilyHandles) throws RocksDBException { RocksDB db = new RocksDB(); List cfReferences = db.open(options.nativeHandle_, path, - columnFamilyNames, columnFamilyNames.size()); - for (int i=0; i columnFamilyNames, + public static RocksDB openReadOnly(String path, + List columnFamilyDescriptors, List columnFamilyHandles) throws RocksDBException { // This allows to use the rocksjni default Options instead of // the c++ one. Options options = new Options(); - return openReadOnly(options, path, columnFamilyNames, columnFamilyHandles); + return openReadOnly(options, path, columnFamilyDescriptors, + columnFamilyHandles); } /** @@ -299,7 +303,7 @@ public class RocksDB extends RocksObject { * * @param options {@link Options} instance. * @param path the path to the RocksDB. - * @param columnFamilyNames list of column family names + * @param columnFamilyDescriptors list of column family descriptors * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances * on open. * @return a {@link RocksDB} instance on success, null if the specified @@ -307,15 +311,16 @@ public class RocksDB extends RocksObject { * @throws RocksDBException */ public static RocksDB openReadOnly(Options options, String path, - List columnFamilyNames, List columnFamilyHandles) + List columnFamilyDescriptors, + List columnFamilyHandles) throws RocksDBException { // when non-default Options is used, keeping an Options reference // in RocksDB can prevent Java to GC during the life-time of // the currently-created RocksDB. RocksDB db = new RocksDB(); List cfReferences = db.openROnly(options.nativeHandle_, path, - columnFamilyNames, columnFamilyNames.size()); - for (int i=0; i open(long optionsHandle, String path, - List columnFamilyNames, int columnFamilyNamesLength) + List columnFamilyDescriptors, + int columnFamilyDescriptorsLength) throws RocksDBException; protected native static List listColumnFamilies( long optionsHandle, String path) throws RocksDBException; protected native void openROnly( long optionsHandle, String path) throws RocksDBException; protected native List openROnly( - long optionsHandle, String path, List columnFamilyNames, - int columnFamilyNamesLength) throws RocksDBException; + long optionsHandle, String path, + List columnFamilyDescriptors, + int columnFamilyDescriptorsLength) throws RocksDBException; protected native void put( long handle, byte[] key, int keyLen, byte[] value, int valueLen) throws RocksDBException; @@ -1231,8 +1239,8 @@ public class RocksDB extends RocksObject { protected native void releaseSnapshot( long nativeHandle, long snapshotHandle); private native void disposeInternal(long handle); - private native long createColumnFamily(long handle, long opt_handle, - String name) throws RocksDBException; + private native long createColumnFamily(long handle, + ColumnFamilyDescriptor columnFamilyDescriptor) throws RocksDBException; private native void dropColumnFamily(long handle, long cfHandle) throws RocksDBException; private native void flush(long handle, long flushOptHandle) throws RocksDBException; diff --git a/java/org/rocksdb/test/ColumnFamilyTest.java b/java/org/rocksdb/test/ColumnFamilyTest.java index 57fd2e347..38f6df7f8 100644 --- a/java/org/rocksdb/test/ColumnFamilyTest.java +++ b/java/org/rocksdb/test/ColumnFamilyTest.java @@ -43,7 +43,8 @@ public class ColumnFamilyTest { // Test createColumnFamily try { - db.createColumnFamily("new_cf"); + db.createColumnFamily(new ColumnFamilyDescriptor("new_cf", + new ColumnFamilyOptions())); } catch (RocksDBException e) { assert(false); } @@ -67,11 +68,12 @@ public class ColumnFamilyTest { } // Test open database with column family names - List cfNames = new ArrayList(); + List cfNames = + new ArrayList(); List columnFamilyHandleList = new ArrayList(); - cfNames.add("default"); - cfNames.add("new_cf"); + cfNames.add(new ColumnFamilyDescriptor("default")); + cfNames.add(new ColumnFamilyDescriptor("new_cf")); try { db = RocksDB.open(options, db_path, cfNames, columnFamilyHandleList); @@ -100,7 +102,8 @@ public class ColumnFamilyTest { // Test create write to and drop ColumnFamily ColumnFamilyHandle tmpColumnFamilyHandle = null; try { - tmpColumnFamilyHandle = db.createColumnFamily("tmpCF"); + tmpColumnFamilyHandle = db.createColumnFamily( + new ColumnFamilyDescriptor("tmpCF", new ColumnFamilyOptions())); db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); db.dropColumnFamily(tmpColumnFamilyHandle); tmpColumnFamilyHandle.dispose(); diff --git a/java/org/rocksdb/test/KeyMayExistTest.java b/java/org/rocksdb/test/KeyMayExistTest.java index c83a70e52..e3b4ed763 100644 --- a/java/org/rocksdb/test/KeyMayExistTest.java +++ b/java/org/rocksdb/test/KeyMayExistTest.java @@ -22,11 +22,12 @@ public class KeyMayExistTest { .setCreateMissingColumnFamilies(true); try { // open database using cf names - List cfNames = new ArrayList(); + List cfNames = + new ArrayList(); List columnFamilyHandleList = new ArrayList(); - cfNames.add("default"); - cfNames.add("new_cf"); + cfNames.add(new ColumnFamilyDescriptor("default")); + cfNames.add(new ColumnFamilyDescriptor("new_cf")); db = RocksDB.open(options, DB_PATH, cfNames, columnFamilyHandleList); assert(columnFamilyHandleList.size()==2); diff --git a/java/org/rocksdb/test/MergeTest.java b/java/org/rocksdb/test/MergeTest.java index 9435718f8..e3c70c885 100644 --- a/java/org/rocksdb/test/MergeTest.java +++ b/java/org/rocksdb/test/MergeTest.java @@ -46,13 +46,18 @@ public class MergeTest { opt.setCreateMissingColumnFamilies(true); opt.setMergeOperatorName("stringappend"); - List cfNames = new ArrayList(); + List cfDescr = + new ArrayList(); List columnFamilyHandleList = new ArrayList(); - cfNames.add("default"); - cfNames.add("new_cf"); + cfDescr.add(new ColumnFamilyDescriptor("default", + new ColumnFamilyOptions().setMergeOperatorName( + "stringappend"))); + cfDescr.add(new ColumnFamilyDescriptor("default", + new ColumnFamilyOptions().setMergeOperatorName( + "stringappend"))); RocksDB db = RocksDB.open(opt, db_cf_path_string, - cfNames, columnFamilyHandleList); + cfDescr, columnFamilyHandleList); // writing aa under key db.put(columnFamilyHandleList.get(1), @@ -103,13 +108,18 @@ public class MergeTest { StringAppendOperator stringAppendOperator = new StringAppendOperator(); opt.setMergeOperator(stringAppendOperator); - List cfNames = new ArrayList(); + List cfDescr = + new ArrayList(); List columnFamilyHandleList = new ArrayList(); - cfNames.add("default"); - cfNames.add("new_cf"); + cfDescr.add(new ColumnFamilyDescriptor("default", + new ColumnFamilyOptions().setMergeOperator( + stringAppendOperator))); + cfDescr.add(new ColumnFamilyDescriptor("new_cf", + new ColumnFamilyOptions().setMergeOperator( + stringAppendOperator))); RocksDB db = RocksDB.open(opt, db_path_operator, - cfNames, columnFamilyHandleList); + cfDescr, columnFamilyHandleList); // writing aa under key db.put(columnFamilyHandleList.get(1), @@ -121,7 +131,10 @@ public class MergeTest { String strValue = new String(value); // Test also with createColumnFamily - ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily("new_cf2"); + ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily( + new ColumnFamilyDescriptor("new_cf2", + new ColumnFamilyOptions().setMergeOperator( + new StringAppendOperator()))); // writing xx under cfkey2 db.put(columnFamilyHandle, "cfkey2".getBytes(), "xx".getBytes()); // merge yy under cfkey2 diff --git a/java/org/rocksdb/test/ReadOnlyTest.java b/java/org/rocksdb/test/ReadOnlyTest.java index 87e8f1e9e..21b5eb9ae 100644 --- a/java/org/rocksdb/test/ReadOnlyTest.java +++ b/java/org/rocksdb/test/ReadOnlyTest.java @@ -34,12 +34,15 @@ public class ReadOnlyTest { db2.close(); - List cfNames = new ArrayList(); - cfNames.add("default"); + List cfNames = + new ArrayList(); + cfNames.add(new ColumnFamilyDescriptor("default")); db = RocksDB.open(DB_PATH, cfNames, columnFamilyHandleList); - columnFamilyHandleList.add(db.createColumnFamily("new_cf")); - columnFamilyHandleList.add(db.createColumnFamily("new_cf2")); + columnFamilyHandleList.add(db.createColumnFamily( + new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions()))); + columnFamilyHandleList.add(db.createColumnFamily( + new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions()))); db.put(columnFamilyHandleList.get(2), "key2".getBytes(), "value2".getBytes()); @@ -47,9 +50,10 @@ public class ReadOnlyTest { assert(db2.get("key2".getBytes())==null); assert(db2.get(columnFamilyHandleList.get(0), "key2".getBytes())==null); - List cfNewName = new ArrayList(); - cfNewName.add("default"); - cfNewName.add("new_cf2"); + List cfNewName = + new ArrayList(); + cfNewName.add(new ColumnFamilyDescriptor("default")); + cfNewName.add(new ColumnFamilyDescriptor("new_cf2")); db3 = RocksDB.openReadOnly(DB_PATH, cfNewName, db3ColumnFamilyHandleList); assert(new String(db3.get(db3ColumnFamilyHandleList.get(1), "key2".getBytes())).equals("value2")); diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 7ce685d43..109930cdc 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -12,9 +12,8 @@ #include #include "include/org_rocksdb_Options.h" -//TODO(fyrz) to be commented in with options refactoring pull requests #include "include/org_rocksdb_DBOptions.h" -//#include "include/org_rocksdb_ColumnFamilyOptions.h" +#include "include/org_rocksdb_ColumnFamilyOptions.h" #include "include/org_rocksdb_WriteOptions.h" #include "include/org_rocksdb_ReadOptions.h" #include "include/org_rocksdb_ComparatorOptions.h" diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index 3a5641d46..9fdab09a4 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -159,6 +159,34 @@ class DBOptionsJni { } }; +class ColumnFamilyDescriptorJni { + public: + // Get the java class id of org.rocksdb.ColumnFamilyDescriptor + static jclass getColumnFamilyDescriptorClass(JNIEnv* env) { + jclass jclazz = env->FindClass("org/rocksdb/ColumnFamilyDescriptor"); + assert(jclazz != nullptr); + return jclazz; + } + + // Get the java method id of columnFamilyName + static jmethodID getColumnFamilyNameMethod(JNIEnv* env) { + static jmethodID mid = env->GetMethodID( + getColumnFamilyDescriptorClass(env), + "columnFamilyName", "()Ljava/lang/String;"); + assert(mid != nullptr); + return mid; + } + + // Get the java method id of columnFamilyOptions + static jmethodID getColumnFamilyOptionsMethod(JNIEnv* env) { + static jmethodID mid = env->GetMethodID( + getColumnFamilyDescriptorClass(env), + "columnFamilyOptions", "()Lorg/rocksdb/ColumnFamilyOptions;"); + assert(mid != nullptr); + return mid; + } +}; + class ColumnFamilyOptionsJni { public: // Get the java class id of org.rocksdb.ColumnFamilyOptions. diff --git a/java/rocksjni/rocksjni.cc b/java/rocksjni/rocksjni.cc index 44d45a2c2..4fa1a544c 100644 --- a/java/rocksjni/rocksjni.cc +++ b/java/rocksjni/rocksjni.cc @@ -69,7 +69,7 @@ void Java_org_rocksdb_RocksDB_openROnly__JLjava_lang_String_2( jobject Java_org_rocksdb_RocksDB_openROnly__JLjava_lang_String_2Ljava_util_List_2I( JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path, - jobject jcfname_list, jint jcfname_count) { + jobject jcfdesc_list, jint jcfdesc_count) { auto opt = reinterpret_cast(jopt_handle); rocksdb::DB* db = nullptr; const char* db_path = env->GetStringUTFChars(jdb_path, 0); @@ -79,23 +79,34 @@ jobject std::vector column_families; std::vector handles; - // get iterator for cfnames + // get iterator for ColumnFamilyDescriptors jobject iteratorObj = env->CallObjectMethod( - jcfname_list, rocksdb::ListJni::getIteratorMethod(env)); + jcfdesc_list, rocksdb::ListJni::getIteratorMethod(env)); - // iterate over cfnames and convert cfnames to - // ColumnFamilyDescriptor instances + // iterate over ColumnFamilyDescriptors while (env->CallBooleanMethod( iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) { - jstring jstr = (jstring) env->CallObjectMethod(iteratorObj, + // get ColumnFamilyDescriptor + jobject jcf_descriptor = env->CallObjectMethod(iteratorObj, rocksdb::ListJni::getNextMethod(env)); + // get ColumnFamilyName + jstring jstr = (jstring) env->CallObjectMethod(jcf_descriptor, + rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyNameMethod( + env)); + // get CF Options + jobject jcf_opt_obj = env->CallObjectMethod(jcf_descriptor, + rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyOptionsMethod( + env)); + rocksdb::ColumnFamilyOptions* cfOptions = + rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj); + const char* cfname = env->GetStringUTFChars(jstr, 0); // free allocated cfnames after call to open cfnames_to_free.push_back(cfname); jcfnames_for_free.push_back(jstr); column_families.push_back(rocksdb::ColumnFamilyDescriptor(cfname, - *static_cast(opt))); + *cfOptions)); } rocksdb::Status s = rocksdb::DB::OpenForReadOnly(*opt, @@ -141,7 +152,7 @@ jobject */ jobject Java_org_rocksdb_RocksDB_open__JLjava_lang_String_2Ljava_util_List_2I( JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path, - jobject jcfname_list, jint jcfname_count) { + jobject jcfdesc_list, jint jcfdesc_count) { auto opt = reinterpret_cast(jopt_handle); rocksdb::DB* db = nullptr; const char* db_path = env->GetStringUTFChars(jdb_path, 0); @@ -151,23 +162,34 @@ jobject Java_org_rocksdb_RocksDB_open__JLjava_lang_String_2Ljava_util_List_2I( std::vector column_families; std::vector handles; - // get iterator for cfnames + // get iterator for ColumnFamilyDescriptors jobject iteratorObj = env->CallObjectMethod( - jcfname_list, rocksdb::ListJni::getIteratorMethod(env)); + jcfdesc_list, rocksdb::ListJni::getIteratorMethod(env)); - // iterate over cfnames and convert cfnames to - // ColumnFamilyDescriptor instances + // iterate over ColumnFamilyDescriptors while (env->CallBooleanMethod( iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) { - jstring jstr = (jstring) env->CallObjectMethod(iteratorObj, + // get ColumnFamilyDescriptor + jobject jcf_descriptor = env->CallObjectMethod(iteratorObj, rocksdb::ListJni::getNextMethod(env)); + // get ColumnFamilyName + jstring jstr = (jstring) env->CallObjectMethod(jcf_descriptor, + rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyNameMethod( + env)); + // get CF Options + jobject jcf_opt_obj = env->CallObjectMethod(jcf_descriptor, + rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyOptionsMethod( + env)); + rocksdb::ColumnFamilyOptions* cfOptions = + rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj); + const char* cfname = env->GetStringUTFChars(jstr, 0); // free allocated cfnames after call to open cfnames_to_free.push_back(cfname); jcfnames_for_free.push_back(jstr); column_families.push_back(rocksdb::ColumnFamilyDescriptor(cfname, - *static_cast(opt))); + *cfOptions)); } rocksdb::Status s = rocksdb::DB::Open(*opt, db_path, column_families, @@ -1151,18 +1173,28 @@ jlongArray Java_org_rocksdb_RocksDB_iterators( /* * Class: org_rocksdb_RocksDB * Method: createColumnFamily - * Signature: (JJLjava/lang/String;)J; + * Signature: (JLorg/rocksdb/ColumnFamilyDescriptor;)J; */ jlong Java_org_rocksdb_RocksDB_createColumnFamily( - JNIEnv* env, jobject jdb, jlong jdb_handle, jlong jopt_handle, - jstring jcfname) { + JNIEnv* env, jobject jdb, jlong jdb_handle, + jobject jcf_descriptor) { rocksdb::ColumnFamilyHandle* handle; - const char* cfname = env->GetStringUTFChars(jcfname, 0); auto db_handle = reinterpret_cast(jdb_handle); - auto opt = reinterpret_cast(jopt_handle); + + jstring jstr = (jstring) env->CallObjectMethod(jcf_descriptor, + rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyNameMethod( + env)); + // get CF Options + jobject jcf_opt_obj = env->CallObjectMethod(jcf_descriptor, + rocksdb::ColumnFamilyDescriptorJni::getColumnFamilyOptionsMethod( + env)); + rocksdb::ColumnFamilyOptions* cfOptions = + rocksdb::ColumnFamilyOptionsJni::getHandle(env, jcf_opt_obj); + + const char* cfname = env->GetStringUTFChars(jstr, 0); rocksdb::Status s = db_handle->CreateColumnFamily( - *static_cast(opt), cfname, &handle); - env->ReleaseStringUTFChars(jcfname, cfname); + *cfOptions, cfname, &handle); + env->ReleaseStringUTFChars(jstr, cfname); if (s.ok()) { return reinterpret_cast(handle); From fa9cfc65f343f8530fb99712ba1e522bff067519 Mon Sep 17 00:00:00 2001 From: fyrz Date: Wed, 5 Nov 2014 12:50:26 +0100 Subject: [PATCH 3/5] [RocksJava] Integrated Review comments from yhchiang in D28023 --- java/org/rocksdb/RocksDB.java | 22 ++++++++++----------- java/org/rocksdb/test/ColumnFamilyTest.java | 6 +++++- java/org/rocksdb/test/KeyMayExistTest.java | 2 +- java/org/rocksdb/test/MergeTest.java | 7 ++----- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 4b580f81f..c62c2f160 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -130,14 +130,14 @@ public class RocksDB extends RocksObject { * {@link RocksDB} can not be opened. * * @throws org.rocksdb.RocksDBException - * @see Options#setCreateIfMissing(boolean) + * @see DBOptions#setCreateIfMissing(boolean) */ public static RocksDB open(String path, List columnFamilyDescriptors, List columnFamilyHandles) throws RocksDBException { // This allows to use the rocksjni default Options instead of // the c++ one. - Options options = new Options(); + DBOptions options = new DBOptions(); return open(options, path, columnFamilyDescriptors, columnFamilyHandles); } @@ -197,7 +197,7 @@ public class RocksDB extends RocksObject { *

* ColumnFamily handles are disposed when the RocksDB instance is disposed.

* - * @param options {@link org.rocksdb.Options} instance. + * @param options {@link org.rocksdb.DBOptions} instance. * @param path the path to the rocksdb. * @param columnFamilyDescriptors list of column family descriptors * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances @@ -206,16 +206,16 @@ public class RocksDB extends RocksObject { * {@link RocksDB} can not be opened. * * @throws org.rocksdb.RocksDBException - * @see Options#setCreateIfMissing(boolean) + * @see DBOptions#setCreateIfMissing(boolean) */ - public static RocksDB open(Options options, String path, + public static RocksDB open(DBOptions options, String path, List columnFamilyDescriptors, List columnFamilyHandles) throws RocksDBException { RocksDB db = new RocksDB(); List cfReferences = db.open(options.nativeHandle_, path, columnFamilyDescriptors, columnFamilyDescriptors.size()); - for (int i=0; i columnFamilyHandles) throws RocksDBException { // This allows to use the rocksjni default Options instead of // the c++ one. - Options options = new Options(); + DBOptions options = new DBOptions(); return openReadOnly(options, path, columnFamilyDescriptors, columnFamilyHandles); } @@ -301,7 +301,7 @@ public class RocksDB extends RocksObject { * options instance have been closed. If user doesn't call options dispose * explicitly,then this options instance will be GC'd automatically.

* - * @param options {@link Options} instance. + * @param options {@link DBOptions} instance. * @param path the path to the RocksDB. * @param columnFamilyDescriptors list of column family descriptors * @param columnFamilyHandles will be filled with ColumnFamilyHandle instances @@ -310,7 +310,7 @@ public class RocksDB extends RocksObject { * {@link RocksDB} can not be opened. * @throws RocksDBException */ - public static RocksDB openReadOnly(Options options, String path, + public static RocksDB openReadOnly(DBOptions options, String path, List columnFamilyDescriptors, List columnFamilyHandles) throws RocksDBException { @@ -342,7 +342,7 @@ public class RocksDB extends RocksObject { return RocksDB.listColumnFamilies(options.nativeHandle_, path); } - private void storeOptionsInstance(Options options) { + private void storeOptionsInstance(DBOptionsInterface options) { options_ = options; } @@ -1247,5 +1247,5 @@ public class RocksDB extends RocksObject { private native void flush(long handle, long flushOptHandle, long cfHandle) throws RocksDBException; - protected Options options_; + protected DBOptionsInterface options_; } diff --git a/java/org/rocksdb/test/ColumnFamilyTest.java b/java/org/rocksdb/test/ColumnFamilyTest.java index 38f6df7f8..0f3ee0c25 100644 --- a/java/org/rocksdb/test/ColumnFamilyTest.java +++ b/java/org/rocksdb/test/ColumnFamilyTest.java @@ -22,6 +22,10 @@ public class ColumnFamilyTest { RocksDB db = null; Options options = new Options(); options.setCreateIfMissing(true); + + DBOptions dbOptions = new DBOptions(); + dbOptions.setCreateIfMissing(true); + try { db = RocksDB.open(options, db_path); } catch (RocksDBException e) { @@ -76,7 +80,7 @@ public class ColumnFamilyTest { cfNames.add(new ColumnFamilyDescriptor("new_cf")); try { - db = RocksDB.open(options, db_path, cfNames, columnFamilyHandleList); + db = RocksDB.open(dbOptions, db_path, cfNames, columnFamilyHandleList); assert(columnFamilyHandleList.size() == 2); db.put("dfkey1".getBytes(), "dfvalue".getBytes()); db.put(columnFamilyHandleList.get(0), "dfkey2".getBytes(), diff --git a/java/org/rocksdb/test/KeyMayExistTest.java b/java/org/rocksdb/test/KeyMayExistTest.java index e3b4ed763..03be46fbe 100644 --- a/java/org/rocksdb/test/KeyMayExistTest.java +++ b/java/org/rocksdb/test/KeyMayExistTest.java @@ -17,7 +17,7 @@ public class KeyMayExistTest { public static void main(String[] args){ RocksDB db; - Options options = new Options(); + DBOptions options = new DBOptions(); options.setCreateIfMissing(true) .setCreateMissingColumnFamilies(true); try { diff --git a/java/org/rocksdb/test/MergeTest.java b/java/org/rocksdb/test/MergeTest.java index e3c70c885..d802559e1 100644 --- a/java/org/rocksdb/test/MergeTest.java +++ b/java/org/rocksdb/test/MergeTest.java @@ -7,7 +7,6 @@ package org.rocksdb.test; import java.util.List; import java.util.ArrayList; -import java.util.Collections; import org.rocksdb.*; public class MergeTest { @@ -41,10 +40,9 @@ public class MergeTest { public static void testCFStringOption() throws InterruptedException, RocksDBException { - Options opt = new Options(); + DBOptions opt = new DBOptions(); opt.setCreateIfMissing(true); opt.setCreateMissingColumnFamilies(true); - opt.setMergeOperatorName("stringappend"); List cfDescr = new ArrayList(); @@ -102,11 +100,10 @@ public class MergeTest { public static void testCFOperatorOption() throws InterruptedException, RocksDBException { - Options opt = new Options(); + DBOptions opt = new DBOptions(); opt.setCreateIfMissing(true); opt.setCreateMissingColumnFamilies(true); StringAppendOperator stringAppendOperator = new StringAppendOperator(); - opt.setMergeOperator(stringAppendOperator); List cfDescr = new ArrayList(); From 9d2ba2136168e09a8fc5ec3a54714bcbc96e3256 Mon Sep 17 00:00:00 2001 From: fyrz Date: Sat, 8 Nov 2014 18:58:35 +0100 Subject: [PATCH 4/5] [RocksJava] Incorporated review comments --- java/Makefile | 6 + java/RocksDBColumnFamilySample.java | 146 ++++++++++++++++++++ java/org/rocksdb/test/ColumnFamilyTest.java | 6 +- 3 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 java/RocksDBColumnFamilySample.java diff --git a/java/Makefile b/java/Makefile index a4bee144a..21066b991 100644 --- a/java/Makefile +++ b/java/Makefile @@ -70,6 +70,12 @@ sample: java @rm -rf /tmp/rocksdbjni @rm -rf /tmp/rocksdbjni_not_found +column_family_sample: java + javac -cp $(ROCKSDB_JAR) RocksDBColumnFamilySample.java + @rm -rf /tmp/rocksdbjni + java -ea -Djava.library.path=.:../ -cp ".:./*" -Xcheck:jni RocksDBColumnFamilySample /tmp/rocksdbjni + @rm -rf /tmp/rocksdbjni + test: java @rm -rf /tmp/rocksdbjni_* javac org/rocksdb/test/*.java diff --git a/java/RocksDBColumnFamilySample.java b/java/RocksDBColumnFamilySample.java new file mode 100644 index 000000000..23ff07c85 --- /dev/null +++ b/java/RocksDBColumnFamilySample.java @@ -0,0 +1,146 @@ +// 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. + +import org.rocksdb.*; + +import java.util.ArrayList; +import java.util.List; + +public class RocksDBColumnFamilySample { + static { + RocksDB.loadLibrary(); + } + + public static void main(String[] args) throws RocksDBException { + if (args.length < 1) { + System.out.println( + "usage: RocksDBColumnFamilySample db_path"); + return; + } + String db_path = args[0]; + + System.out.println("RocksDBColumnFamilySample"); + RocksDB db = null; + DBOptions dbOptions = null; + List iterators = new ArrayList<>(); + RocksIterator iterator = null; + ColumnFamilyHandle cfHandle = null; + WriteBatch wb = null; + try { + // Setup DBOptions + dbOptions = new DBOptions(). + setCreateIfMissing(true). + setCreateMissingColumnFamilies(true); + // Setup ColumnFamily descriptors + List cfNames = + new ArrayList<>(); + // Default column family + cfNames.add(new ColumnFamilyDescriptor("default")); + // New column families + cfNames.add(new ColumnFamilyDescriptor("cf_green", + new ColumnFamilyOptions().setComparator( + BuiltinComparator.BYTEWISE_COMPARATOR))); + cfNames.add(new ColumnFamilyDescriptor("cf_blue", + new ColumnFamilyOptions().setComparator( + BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR))); + cfNames.add(new ColumnFamilyDescriptor("cf_red", + new ColumnFamilyOptions(). + setMergeOperator(new StringAppendOperator()))); + + List cfHandles = + new ArrayList<>(); + db = RocksDB.open(dbOptions, + db_path, cfNames, cfHandles); + // List column families in database + System.out.println("List existent column families:"); + List cfListing = RocksDB.listColumnFamilies( + new Options(), db_path); + for (byte[] cf : cfListing) { + System.out.format(" - %s\n", new String(cf)); + } + // Bootstrapping values + System.out.println("Writing values to database."); + for (int i=0; i < cfNames.size(); i++) { + for (int j=0; j < 10; j++) { + db.put(cfHandles.get(i), + String.valueOf(j).getBytes(), + String.valueOf(j).getBytes()); + } + } + // Retrieve values using get + System.out.println("Retrieve values with get."); + for (int i=0; i < cfNames.size(); i++) { + for (int j=0; j < 10; j++) { + System.out.format(" %s", new String( + db.get(cfHandles.get(i), + String.valueOf(j).getBytes()))); + } + System.out.println(""); + } + // Add a new column family to existing database + System.out.println("Add new column family"); + cfHandle = db.createColumnFamily(new ColumnFamilyDescriptor( + "cf_temp", new ColumnFamilyOptions(). + setMergeOperator(new StringAppendOperator()))); + System.out.println("Write key/value into new column family."); + db.put(cfHandle, "key".getBytes(), "value".getBytes()); + System.out.format("Lookup 'key' retrieved value: %s\n", new String( + db.get(cfHandle, "key".getBytes()))); + // Delete key + System.out.println("Delete key/value in new column family."); + db.remove(cfHandle, "key".getBytes()); + // WriteBatch with column family + wb = new WriteBatch(); + wb.put(cfHandle, "key".getBytes(), "value".getBytes()); + wb.put(cfHandle, "key2".getBytes(), "value2".getBytes()); + wb.remove(cfHandle, "key2".getBytes()); + wb.merge(cfHandle, "key".getBytes(), "morevalues".getBytes()); + db.write(new WriteOptions(), wb); + // Retrieve a single iterator with a cf handle + System.out.println("Retrieve values using a iterator on" + + " a column family."); + iterator = db.newIterator(cfHandle); + iterator.seekToFirst(); + while(iterator.isValid()) { + System.out.format(" %s", new String( + iterator.value())); + iterator.next(); + } + System.out.println(""); + // Delete column family + System.out.println("Delete column family."); + db.dropColumnFamily(cfHandle); + // Retrieve values from cf using iterator + System.out.println("Retrieve values with iterators"); + iterators = db.newIterators(cfHandles); + assert(iterators.size() == 4); + for (RocksIterator iter : iterators) { + iter.seekToFirst(); + while(iter.isValid()) { + System.out.format(" %s", new String( + iter.value())); + iter.next(); + } + System.out.println(""); + } + } finally { + if (db != null) { + db.close(); + } + if (dbOptions != null) { + dbOptions.dispose(); + } + if (iterator != null) { + iterator.dispose(); + } + for (RocksIterator iter : iterators) { + iter.dispose(); + } + if (wb != null) { + wb.dispose(); + } + } + } +} diff --git a/java/org/rocksdb/test/ColumnFamilyTest.java b/java/org/rocksdb/test/ColumnFamilyTest.java index 0f3ee0c25..350c4446c 100644 --- a/java/org/rocksdb/test/ColumnFamilyTest.java +++ b/java/org/rocksdb/test/ColumnFamilyTest.java @@ -73,9 +73,9 @@ public class ColumnFamilyTest { // Test open database with column family names List cfNames = - new ArrayList(); + new ArrayList<>(); List columnFamilyHandleList = - new ArrayList(); + new ArrayList<>(); cfNames.add(new ColumnFamilyDescriptor("default")); cfNames.add(new ColumnFamilyDescriptor("new_cf")); @@ -222,8 +222,6 @@ public class ColumnFamilyTest { .equals("value")); } catch (RocksDBException e) { assert(false); - } catch (IllegalArgumentException e) { - assert(false); } // Test multiget without correct number of column From 9a255b95f061c820d9e3e7f50f0542a5db6db834 Mon Sep 17 00:00:00 2001 From: fyrz Date: Wed, 12 Nov 2014 19:49:13 +0100 Subject: [PATCH 5/5] [RocksJava] Sample and Default value - RocksDB ColumnFamilySample adjusted to C++ sample. - DefaultColumnFamily is available now as constant in RocksDB. --- java/RocksDBColumnFamilySample.java | 157 +++++++++------------------- java/org/rocksdb/RocksDB.java | 1 + 2 files changed, 52 insertions(+), 106 deletions(-) diff --git a/java/RocksDBColumnFamilySample.java b/java/RocksDBColumnFamilySample.java index 23ff07c85..200e53a1d 100644 --- a/java/RocksDBColumnFamilySample.java +++ b/java/RocksDBColumnFamilySample.java @@ -23,120 +23,65 @@ public class RocksDBColumnFamilySample { System.out.println("RocksDBColumnFamilySample"); RocksDB db = null; - DBOptions dbOptions = null; - List iterators = new ArrayList<>(); - RocksIterator iterator = null; - ColumnFamilyHandle cfHandle = null; + Options options = null; + ColumnFamilyHandle columnFamilyHandle = null; WriteBatch wb = null; try { - // Setup DBOptions - dbOptions = new DBOptions(). - setCreateIfMissing(true). - setCreateMissingColumnFamilies(true); - // Setup ColumnFamily descriptors - List cfNames = - new ArrayList<>(); - // Default column family - cfNames.add(new ColumnFamilyDescriptor("default")); - // New column families - cfNames.add(new ColumnFamilyDescriptor("cf_green", - new ColumnFamilyOptions().setComparator( - BuiltinComparator.BYTEWISE_COMPARATOR))); - cfNames.add(new ColumnFamilyDescriptor("cf_blue", - new ColumnFamilyOptions().setComparator( - BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR))); - cfNames.add(new ColumnFamilyDescriptor("cf_red", - new ColumnFamilyOptions(). - setMergeOperator(new StringAppendOperator()))); + options = new Options().setCreateIfMissing(true); + db = RocksDB.open(options, db_path); + assert(db != null); + + // create column family + columnFamilyHandle = db.createColumnFamily( + new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions())); + assert(columnFamilyHandle != null); - List cfHandles = - new ArrayList<>(); - db = RocksDB.open(dbOptions, - db_path, cfNames, cfHandles); - // List column families in database - System.out.println("List existent column families:"); - List cfListing = RocksDB.listColumnFamilies( - new Options(), db_path); - for (byte[] cf : cfListing) { - System.out.format(" - %s\n", new String(cf)); - } - // Bootstrapping values - System.out.println("Writing values to database."); - for (int i=0; i < cfNames.size(); i++) { - for (int j=0; j < 10; j++) { - db.put(cfHandles.get(i), - String.valueOf(j).getBytes(), - String.valueOf(j).getBytes()); - } - } - // Retrieve values using get - System.out.println("Retrieve values with get."); - for (int i=0; i < cfNames.size(); i++) { - for (int j=0; j < 10; j++) { - System.out.format(" %s", new String( - db.get(cfHandles.get(i), - String.valueOf(j).getBytes()))); - } - System.out.println(""); - } - // Add a new column family to existing database - System.out.println("Add new column family"); - cfHandle = db.createColumnFamily(new ColumnFamilyDescriptor( - "cf_temp", new ColumnFamilyOptions(). - setMergeOperator(new StringAppendOperator()))); - System.out.println("Write key/value into new column family."); - db.put(cfHandle, "key".getBytes(), "value".getBytes()); - System.out.format("Lookup 'key' retrieved value: %s\n", new String( - db.get(cfHandle, "key".getBytes()))); - // Delete key - System.out.println("Delete key/value in new column family."); - db.remove(cfHandle, "key".getBytes()); - // WriteBatch with column family - wb = new WriteBatch(); - wb.put(cfHandle, "key".getBytes(), "value".getBytes()); - wb.put(cfHandle, "key2".getBytes(), "value2".getBytes()); - wb.remove(cfHandle, "key2".getBytes()); - wb.merge(cfHandle, "key".getBytes(), "morevalues".getBytes()); - db.write(new WriteOptions(), wb); - // Retrieve a single iterator with a cf handle - System.out.println("Retrieve values using a iterator on" + - " a column family."); - iterator = db.newIterator(cfHandle); - iterator.seekToFirst(); - while(iterator.isValid()) { - System.out.format(" %s", new String( - iterator.value())); - iterator.next(); - } - System.out.println(""); - // Delete column family - System.out.println("Delete column family."); - db.dropColumnFamily(cfHandle); - // Retrieve values from cf using iterator - System.out.println("Retrieve values with iterators"); - iterators = db.newIterators(cfHandles); - assert(iterators.size() == 4); - for (RocksIterator iter : iterators) { - iter.seekToFirst(); - while(iter.isValid()) { - System.out.format(" %s", new String( - iter.value())); - iter.next(); - } - System.out.println(""); - } } finally { + if (columnFamilyHandle != null) { + columnFamilyHandle.dispose(); + } if (db != null) { db.close(); + db = null; } - if (dbOptions != null) { - dbOptions.dispose(); + } + + // open DB with two column families + List columnFamilyDescriptors = new ArrayList<>(); + // have to open default column family + columnFamilyDescriptors.add(new ColumnFamilyDescriptor( + RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions())); + // open the new one, too + columnFamilyDescriptors.add(new ColumnFamilyDescriptor( + "new_cf", new ColumnFamilyOptions())); + List columnFamilyHandles = new ArrayList<>(); + try { + db = RocksDB.open(new DBOptions(), db_path, + columnFamilyDescriptors, columnFamilyHandles); + assert(db != null); + + // put and get from non-default column family + db.put(columnFamilyHandles.get(0), new WriteOptions(), + "key".getBytes(), "value".getBytes()); + String value = new String(db.get(columnFamilyHandles.get(0), + "key".getBytes())); + + // atomic write + wb = new WriteBatch(); + wb.put(columnFamilyHandles.get(0), "key2".getBytes(), "value2".getBytes()); + wb.put(columnFamilyHandles.get(1), "key3".getBytes(), "value3".getBytes()); + wb.remove(columnFamilyHandles.get(0), "key".getBytes()); + db.write(new WriteOptions(), wb); + + // drop column family + db.dropColumnFamily(columnFamilyHandles.get(1)); + + } finally { + for (ColumnFamilyHandle handle : columnFamilyHandles){ + handle.dispose(); } - if (iterator != null) { - iterator.dispose(); - } - for (RocksIterator iter : iterators) { - iter.dispose(); + if (db != null) { + db.close(); } if (wb != null) { wb.dispose(); diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index c62c2f160..690d84ec8 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -16,6 +16,7 @@ import org.rocksdb.util.Environment; * indicates sth wrong at the RocksDB library side and the call failed. */ 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"};