diff --git a/java/src/main/java/org/rocksdb/OptimisticTransactionDB.java b/java/src/main/java/org/rocksdb/OptimisticTransactionDB.java index fffa1f01db..4674eae010 100644 --- a/java/src/main/java/org/rocksdb/OptimisticTransactionDB.java +++ b/java/src/main/java/org/rocksdb/OptimisticTransactionDB.java @@ -5,6 +5,7 @@ package org.rocksdb; +import java.util.Arrays; import java.util.List; /** @@ -45,6 +46,8 @@ public class OptimisticTransactionDB extends RocksDB // the currently-created RocksDB. otdb.storeOptionsInstance(options); + otdb.storeDefaultColumnFamilyHandle(otdb.makeDefaultColumnFamilyHandle()); + return otdb; } @@ -67,7 +70,7 @@ public class OptimisticTransactionDB extends RocksDB final List columnFamilyDescriptors, final List columnFamilyHandles) throws RocksDBException { - + int defaultColumnFamilyIndex = -1; final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][]; final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()]; for (int i = 0; i < columnFamilyDescriptors.size(); i++) { @@ -75,6 +78,13 @@ public class OptimisticTransactionDB extends RocksDB .get(i); cfNames[i] = cfDescriptor.getName(); cfOptionHandles[i] = cfDescriptor.getOptions().nativeHandle_; + if (Arrays.equals(cfDescriptor.getName(), RocksDB.DEFAULT_COLUMN_FAMILY)) { + defaultColumnFamilyIndex = i; + } + } + if (defaultColumnFamilyIndex < 0) { + throw new IllegalArgumentException( + "You must provide the default column family in your columnFamilyDescriptors"); } final long[] handles = open(dbOptions.nativeHandle_, path, cfNames, @@ -86,12 +96,14 @@ public class OptimisticTransactionDB extends RocksDB // in RocksDB can prevent Java to GC during the life-time of // the currently-created RocksDB. otdb.storeOptionsInstance(dbOptions); - otdb.storeDefaultColumnFamilyHandle(otdb.makeDefaultColumnFamilyHandle()); for (int i = 1; i < handles.length; i++) { columnFamilyHandles.add(new ColumnFamilyHandle(otdb, handles[i])); } + otdb.ownedColumnFamilyHandles.addAll(columnFamilyHandles); + otdb.storeDefaultColumnFamilyHandle(columnFamilyHandles.get(defaultColumnFamilyIndex)); + return otdb; } @@ -133,6 +145,12 @@ public class OptimisticTransactionDB extends RocksDB @SuppressWarnings("PMD.EmptyCatchBlock") @Override public void close() { + for (final ColumnFamilyHandle columnFamilyHandle : // NOPMD - CloseResource + ownedColumnFamilyHandles) { + columnFamilyHandle.close(); + } + ownedColumnFamilyHandles.clear(); + if (owningHandle_.compareAndSet(true, false)) { try { closeDatabase(nativeHandle_); diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index f93536cf0f..8fe297844e 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -304,9 +304,7 @@ public class RocksDB extends RocksObject { */ public static RocksDB open(final DBOptions options, final String path, final List columnFamilyDescriptors, - final List columnFamilyHandles) - throws RocksDBException { - + final List columnFamilyHandles) throws RocksDBException { final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][]; final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()]; int defaultColumnFamilyIndex = -1; @@ -320,7 +318,7 @@ public class RocksDB extends RocksObject { } } if (defaultColumnFamilyIndex < 0) { - new IllegalArgumentException( + throw new IllegalArgumentException( "You must provide the default column family in your columnFamilyDescriptors"); } @@ -502,11 +500,19 @@ public class RocksDB extends RocksObject { final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][]; final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()]; + int defaultColumnFamilyIndex = -1; for (int i = 0; i < columnFamilyDescriptors.size(); i++) { final ColumnFamilyDescriptor cfDescriptor = columnFamilyDescriptors .get(i); cfNames[i] = cfDescriptor.getName(); cfOptionHandles[i] = cfDescriptor.getOptions().nativeHandle_; + if (Arrays.equals(cfDescriptor.getName(), RocksDB.DEFAULT_COLUMN_FAMILY)) { + defaultColumnFamilyIndex = i; + } + } + if (defaultColumnFamilyIndex < 0) { + throw new IllegalArgumentException( + "You must provide the default column family in your columnFamilyDescriptors"); } final long[] handles = @@ -521,7 +527,7 @@ public class RocksDB extends RocksObject { } db.ownedColumnFamilyHandles.addAll(columnFamilyHandles); - db.storeDefaultColumnFamilyHandle(db.makeDefaultColumnFamilyHandle()); + db.storeDefaultColumnFamilyHandle(columnFamilyHandles.get(defaultColumnFamilyIndex)); return db; } diff --git a/java/src/main/java/org/rocksdb/TransactionDB.java b/java/src/main/java/org/rocksdb/TransactionDB.java index 4acda68f25..0f75e5f970 100644 --- a/java/src/main/java/org/rocksdb/TransactionDB.java +++ b/java/src/main/java/org/rocksdb/TransactionDB.java @@ -6,6 +6,7 @@ package org.rocksdb; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -77,7 +78,7 @@ public class TransactionDB extends RocksDB final List columnFamilyDescriptors, final List columnFamilyHandles) throws RocksDBException { - + int defaultColumnFamilyIndex = -1; final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][]; final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()]; for (int i = 0; i < columnFamilyDescriptors.size(); i++) { @@ -85,6 +86,13 @@ public class TransactionDB extends RocksDB .get(i); cfNames[i] = cfDescriptor.getName(); cfOptionHandles[i] = cfDescriptor.getOptions().nativeHandle_; + if (Arrays.equals(cfDescriptor.getName(), RocksDB.DEFAULT_COLUMN_FAMILY)) { + defaultColumnFamilyIndex = i; + } + } + if (defaultColumnFamilyIndex < 0) { + throw new IllegalArgumentException( + "You must provide the default column family in your columnFamilyDescriptors"); } final long[] handles = open(dbOptions.nativeHandle_, @@ -95,12 +103,13 @@ public class TransactionDB extends RocksDB // in RocksDB can prevent Java to GC during the life-time of // the currently-created RocksDB. tdb.storeOptionsInstance(dbOptions); - tdb.storeDefaultColumnFamilyHandle(tdb.makeDefaultColumnFamilyHandle()); tdb.storeTransactionDbOptions(transactionDbOptions); for (int i = 1; i < handles.length; i++) { columnFamilyHandles.add(new ColumnFamilyHandle(tdb, handles[i])); } + tdb.ownedColumnFamilyHandles.addAll(columnFamilyHandles); + tdb.storeDefaultColumnFamilyHandle(columnFamilyHandles.get(defaultColumnFamilyIndex)); return tdb; } @@ -143,6 +152,12 @@ public class TransactionDB extends RocksDB @SuppressWarnings("PMD.EmptyCatchBlock") @Override public void close() { + for (final ColumnFamilyHandle columnFamilyHandle : // NOPMD - CloseResource + ownedColumnFamilyHandles) { + columnFamilyHandle.close(); + } + ownedColumnFamilyHandles.clear(); + if (owningHandle_.compareAndSet(true, false)) { try { closeDatabase(nativeHandle_); diff --git a/java/src/main/java/org/rocksdb/TtlDB.java b/java/src/main/java/org/rocksdb/TtlDB.java index 52d9dad94a..fa8e03598e 100644 --- a/java/src/main/java/org/rocksdb/TtlDB.java +++ b/java/src/main/java/org/rocksdb/TtlDB.java @@ -133,7 +133,7 @@ public class TtlDB extends RocksDB { } } if (defaultColumnFamilyIndex < 0) { - new IllegalArgumentException( + throw new IllegalArgumentException( "You must provide the default column family in your columnFamilyDescriptors"); } @@ -195,6 +195,12 @@ public class TtlDB extends RocksDB { @SuppressWarnings("PMD.EmptyCatchBlock") @Override public void close() { + for (final ColumnFamilyHandle columnFamilyHandle : // NOPMD - CloseResource + ownedColumnFamilyHandles) { + columnFamilyHandle.close(); + } + ownedColumnFamilyHandles.clear(); + if (owningHandle_.compareAndSet(true, false)) { try { closeDatabase(nativeHandle_); diff --git a/java/src/test/java/org/rocksdb/ColumnFamilyTest.java b/java/src/test/java/org/rocksdb/ColumnFamilyTest.java index fb8a450855..91fa161c8a 100644 --- a/java/src/test/java/org/rocksdb/ColumnFamilyTest.java +++ b/java/src/test/java/org/rocksdb/ColumnFamilyTest.java @@ -147,6 +147,228 @@ public class ColumnFamilyTest { } } + @Test + public void defaultColumnFamilySynonyms() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { + db.put("dfkey_syn_1".getBytes(), "dfvalue_syn_1".getBytes()); + } + + final List cfNames = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf2".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + + try (final DBOptions options = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final RocksDB db = RocksDB.open( + options, dbFolder.getRoot().getAbsolutePath(), cfNames, columnFamilyHandleList)) { + assertThat(columnFamilyHandleList.size()).isEqualTo(3); + assertThat(db.get(columnFamilyHandleList.get(1), "dfkey_syn_1".getBytes())) + .isEqualTo("dfvalue_syn_1".getBytes()); + db.put(columnFamilyHandleList.get(1), "dfkey_syn_2".getBytes(), "dfvalue_syn_2".getBytes()); + } + + final List cfNames2 = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor("new_cf2".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + final List columnFamilyHandleList2 = new ArrayList<>(); + + try (final RocksDB db = RocksDB.open(new DBOptions(), dbFolder.getRoot().getAbsolutePath(), + cfNames2, columnFamilyHandleList2)) { + assertThat(db.get("dfkey_syn_2".getBytes())).isEqualTo("dfvalue_syn_2".getBytes()); + } + } + + @Test + public void defaultColumnFamilySynonymsReadOnly() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true); + final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { + db.put("dfkey_syn_1".getBytes(), "dfvalue_syn_1".getBytes()); + } + + final List cfNames = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf2".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + + try (final DBOptions options = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final RocksDB db = RocksDB.open( + options, dbFolder.getRoot().getAbsolutePath(), cfNames, columnFamilyHandleList)) { + assertThat(columnFamilyHandleList.size()).isEqualTo(3); + assertThat(db.get(columnFamilyHandleList.get(1), "dfkey_syn_1".getBytes())) + .isEqualTo("dfvalue_syn_1".getBytes()); + db.put(columnFamilyHandleList.get(1), "dfkey_syn_2".getBytes(), "dfvalue_syn_2".getBytes()); + } + + final List cfNames2 = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor("new_cf2".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + final List columnFamilyHandleList2 = new ArrayList<>(); + + try (final RocksDB db = RocksDB.openReadOnly(new DBOptions(), + dbFolder.getRoot().getAbsolutePath(), cfNames2, columnFamilyHandleList2)) { + assertThat(db.get("dfkey_syn_2".getBytes())).isEqualTo("dfvalue_syn_2".getBytes()); + } + } + + @Test + public void defaultColumnFamilySynonymsOTDB() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true); + final OptimisticTransactionDB db = + OptimisticTransactionDB.open(options, dbFolder.getRoot().getAbsolutePath())) { + db.put("dfkey_syn_1".getBytes(), "dfvalue_syn_1".getBytes()); + } + + final List cfNames = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf2".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + + try (final DBOptions options = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final OptimisticTransactionDB db = OptimisticTransactionDB.open( + options, dbFolder.getRoot().getAbsolutePath(), cfNames, columnFamilyHandleList)) { + assertThat(columnFamilyHandleList.size()).isEqualTo(3); + assertThat(db.get(columnFamilyHandleList.get(1), "dfkey_syn_1".getBytes())) + .isEqualTo("dfvalue_syn_1".getBytes()); + db.put(columnFamilyHandleList.get(1), "dfkey_syn_2".getBytes(), "dfvalue_syn_2".getBytes()); + } + + final List cfNames2 = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor("new_cf2".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + final List columnFamilyHandleList2 = new ArrayList<>(); + + try (final OptimisticTransactionDB db = OptimisticTransactionDB.open(new DBOptions(), + dbFolder.getRoot().getAbsolutePath(), cfNames2, columnFamilyHandleList2)) { + assertThat(db.get("dfkey_syn_2".getBytes())).isEqualTo("dfvalue_syn_2".getBytes()); + } + } + + @Test + public void defaultColumnFamilySynonymsTDB() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true); + final TransactionDBOptions transactionDBOptions = new TransactionDBOptions(); + final TransactionDB db = TransactionDB.open( + options, transactionDBOptions, dbFolder.getRoot().getAbsolutePath())) { + db.put("dfkey_syn_1".getBytes(), "dfvalue_syn_1".getBytes()); + } + + final List cfNames = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf2".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + + try (final DBOptions options = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final TransactionDBOptions transactionDBOptions = new TransactionDBOptions(); + final TransactionDB db = TransactionDB.open(options, transactionDBOptions, + dbFolder.getRoot().getAbsolutePath(), cfNames, columnFamilyHandleList)) { + assertThat(columnFamilyHandleList.size()).isEqualTo(3); + assertThat(db.get(columnFamilyHandleList.get(1), "dfkey_syn_1".getBytes())) + .isEqualTo("dfvalue_syn_1".getBytes()); + db.put(columnFamilyHandleList.get(1), "dfkey_syn_2".getBytes(), "dfvalue_syn_2".getBytes()); + } + + final List cfNames2 = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor("new_cf2".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + final List columnFamilyHandleList2 = new ArrayList<>(); + + try (final TransactionDBOptions transactionDBOptions = new TransactionDBOptions(); + final TransactionDB db = TransactionDB.open(new DBOptions(), transactionDBOptions, + dbFolder.getRoot().getAbsolutePath(), cfNames2, columnFamilyHandleList2)) { + assertThat(db.get("dfkey_syn_2".getBytes())).isEqualTo("dfvalue_syn_2".getBytes()); + } + } + + @Test + public void defaultColumnFamilySynonymsTTLDB() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true); + final TtlDB db = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath())) { + db.put("dfkey_syn_1".getBytes(), "dfvalue_syn_1".getBytes()); + } + + final List cfNames = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), + new ColumnFamilyDescriptor("new_cf2".getBytes())); + final List columnFamilyHandleList = new ArrayList<>(); + + try (final DBOptions options = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final TtlDB db = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath(), cfNames, + columnFamilyHandleList, Arrays.asList(10, 10, 10), false)) { + assertThat(columnFamilyHandleList.size()).isEqualTo(3); + assertThat(db.get(columnFamilyHandleList.get(1), "dfkey_syn_1".getBytes())) + .isEqualTo("dfvalue_syn_1".getBytes()); + db.put(columnFamilyHandleList.get(1), "dfkey_syn_2".getBytes(), "dfvalue_syn_2".getBytes()); + } + + final List cfNames2 = + Arrays.asList(new ColumnFamilyDescriptor("new_cf1".getBytes()), + new ColumnFamilyDescriptor("new_cf2".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + final List columnFamilyHandleList2 = new ArrayList<>(); + + try (final TtlDB db = TtlDB.open(new DBOptions(), dbFolder.getRoot().getAbsolutePath(), + cfNames2, columnFamilyHandleList2, Arrays.asList(10, 10, 10), false)) { + assertThat(db.get("dfkey_syn_2".getBytes())).isEqualTo("dfvalue_syn_2".getBytes()); + } + } + + @Test(expected = IllegalArgumentException.class) + public void openColumnFamiliesNoDefault() throws RocksDBException { + try (final DBOptions dbOptions = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions myCfOpts = new ColumnFamilyOptions()) { + final List columnFamilyDescriptors = + Collections.singletonList(new ColumnFamilyDescriptor("myCf".getBytes(), myCfOpts)); + + final List columnFamilyHandles = new ArrayList<>(); + + RocksDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, + columnFamilyHandles); + } + } + + @Test(expected = IllegalArgumentException.class) + public void openColumnFamiliesNoDefaultReadOnly() throws RocksDBException { + try (final DBOptions dbOptions = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions myCfOpts = new ColumnFamilyOptions()) { + final List columnFamilyDescriptors = + Arrays.asList(new ColumnFamilyDescriptor("myCf".getBytes()), + new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); + + final List columnFamilyHandles = new ArrayList<>(); + + RocksDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, + columnFamilyHandles); + } + + try (final DBOptions dbOptions = new DBOptions()) { + final List columnFamilyDescriptors = + Collections.singletonList(new ColumnFamilyDescriptor("myCf".getBytes())); + + final List columnFamilyHandles = new ArrayList<>(); + + final RocksDB db = RocksDB.openReadOnly(dbOptions, dbFolder.getRoot().getAbsolutePath(), + columnFamilyDescriptors, columnFamilyHandles); + db.close(); + } + } + @Test public void getWithOutValueAndCf() throws RocksDBException { final List cfDescriptors = diff --git a/java/src/test/java/org/rocksdb/OptimisticTransactionDBTest.java b/java/src/test/java/org/rocksdb/OptimisticTransactionDBTest.java index 519b70b1d2..237fcaf0c8 100644 --- a/java/src/test/java/org/rocksdb/OptimisticTransactionDBTest.java +++ b/java/src/test/java/org/rocksdb/OptimisticTransactionDBTest.java @@ -5,15 +5,15 @@ package org.rocksdb; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; public class OptimisticTransactionDBTest { @@ -56,6 +56,21 @@ public class OptimisticTransactionDBTest { } } + @Test(expected = IllegalArgumentException.class) + public void open_columnFamilies_no_default() throws RocksDBException { + try (final DBOptions dbOptions = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions myCfOpts = new ColumnFamilyOptions()) { + final List columnFamilyDescriptors = + Collections.singletonList(new ColumnFamilyDescriptor("myCf".getBytes(), myCfOpts)); + + final List columnFamilyHandles = new ArrayList<>(); + + OptimisticTransactionDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(), + columnFamilyDescriptors, columnFamilyHandles); + } + } + @Test public void beginTransaction() throws RocksDBException { try (final Options options = new Options().setCreateIfMissing(true); @@ -128,4 +143,21 @@ public class OptimisticTransactionDBTest { assertThat(db.isOwningHandle()).isFalse(); } } + + @Test + public void otdbSimpleIterator() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0); + final OptimisticTransactionDB otdb = + OptimisticTransactionDB.open(options, dbFolder.getRoot().getAbsolutePath())) { + otdb.put("keyI".getBytes(), "valueI".getBytes()); + try (final RocksIterator iterator = otdb.newIterator()) { + iterator.seekToFirst(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("keyI".getBytes()); + assertThat(iterator.value()).isEqualTo("valueI".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isFalse(); + } + } + } } diff --git a/java/src/test/java/org/rocksdb/TransactionDBTest.java b/java/src/test/java/org/rocksdb/TransactionDBTest.java index 56acb21c72..e158c92fdf 100644 --- a/java/src/test/java/org/rocksdb/TransactionDBTest.java +++ b/java/src/test/java/org/rocksdb/TransactionDBTest.java @@ -31,10 +31,9 @@ public class TransactionDBTest { @Test public void open_columnFamilies() throws RocksDBException { - try(final DBOptions dbOptions = new DBOptions().setCreateIfMissing(true) - .setCreateMissingColumnFamilies(true); - final ColumnFamilyOptions myCfOpts = new ColumnFamilyOptions()) { - + try (final DBOptions dbOptions = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions myCfOpts = new ColumnFamilyOptions()) { final List columnFamilyDescriptors = Arrays.asList( new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), @@ -57,6 +56,24 @@ public class TransactionDBTest { } } + @Test(expected = IllegalArgumentException.class) + public void open_columnFamilies_no_default() throws RocksDBException { + try (final DBOptions dbOptions = + new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true); + final ColumnFamilyOptions myCfOpts = new ColumnFamilyOptions()) { + final List columnFamilyDescriptors = + Collections.singletonList(new ColumnFamilyDescriptor("myCf".getBytes(), myCfOpts)); + + final List columnFamilyHandles = new ArrayList<>(); + + try ( + final TransactionDBOptions txnDbOptions = new TransactionDBOptions(); + final TransactionDB ignored = TransactionDB.open(dbOptions, txnDbOptions, + dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, columnFamilyHandles)) { + } + } + } + @Test public void beginTransaction() throws RocksDBException { try (final Options options = new Options().setCreateIfMissing(true); @@ -174,4 +191,22 @@ public class TransactionDBTest { tdb.setDeadlockInfoBufferSize(123); } } + + @Test + public void tdbSimpleIterator() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0); + final TransactionDBOptions txnDbOptions = new TransactionDBOptions(); + final TransactionDB tdb = + TransactionDB.open(options, txnDbOptions, dbFolder.getRoot().getAbsolutePath())) { + tdb.put("keyI".getBytes(), "valueI".getBytes()); + try (final RocksIterator iterator = tdb.newIterator()) { + iterator.seekToFirst(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("keyI".getBytes()); + assertThat(iterator.value()).isEqualTo("valueI".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isFalse(); + } + } + } } diff --git a/java/src/test/java/org/rocksdb/TtlDBTest.java b/java/src/test/java/org/rocksdb/TtlDBTest.java index 86ba099985..abae3a44a7 100644 --- a/java/src/test/java/org/rocksdb/TtlDBTest.java +++ b/java/src/test/java/org/rocksdb/TtlDBTest.java @@ -51,6 +51,22 @@ public class TtlDBTest { } } + @Test + public void ttlDBSimpleIterator() throws RocksDBException { + try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0); + final TtlDB ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath())) { + ttlDB.put("keyI".getBytes(), "valueI".getBytes()); + try (final RocksIterator iterator = ttlDB.newIterator()) { + iterator.seekToFirst(); + assertThat(iterator.isValid()).isTrue(); + assertThat(iterator.key()).isEqualTo("keyI".getBytes()); + assertThat(iterator.value()).isEqualTo("valueI".getBytes()); + iterator.next(); + assertThat(iterator.isValid()).isFalse(); + } + } + } + @Test public void ttlDbOpenWithColumnFamilies() throws RocksDBException, InterruptedException {