diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index 9926bea80bb5..6c2152ae1fac 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -12,7 +12,6 @@ import java.nio.ByteBuffer; import java.util.*; import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.IntStream; import org.rocksdb.util.Environment; /** @@ -306,11 +305,19 @@ public static RocksDB open(final DBOptions options, final String path, 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) { + new IllegalArgumentException( + "You must provide the default column family in your columnFamilyDescriptors"); } final long[] handles = open(options.nativeHandle_, path, cfNames, @@ -325,20 +332,7 @@ public static RocksDB open(final DBOptions options, final String path, } db.ownedColumnFamilyHandles.addAll(columnFamilyHandles); - - // ColumnFamilyHandle.isDefaultColumnFamily() doesn't work here yet, as we are in process of - // opening database - OptionalInt defaultCfIndex = IntStream.of(0, columnFamilyDescriptors.size() - 1) - .filter(x - -> Arrays.equals(columnFamilyDescriptors.get(x).getName(), - RocksDB.DEFAULT_COLUMN_FAMILY)) - .findFirst(); - if (defaultCfIndex.isPresent()) { - db.storeDefaultColumnFamilyHandle(columnFamilyHandles.get(defaultCfIndex.getAsInt())); - } else { - throw new RocksDBException("No default column family"); - } - + db.storeDefaultColumnFamilyHandle(columnFamilyHandles.get(defaultColumnFamilyIndex)); return db; } diff --git a/java/src/main/java/org/rocksdb/TtlDB.java b/java/src/main/java/org/rocksdb/TtlDB.java index db08761fd5d8..c6e7b9c533e8 100644 --- a/java/src/main/java/org/rocksdb/TtlDB.java +++ b/java/src/main/java/org/rocksdb/TtlDB.java @@ -7,8 +7,6 @@ import java.util.Arrays; import java.util.List; -import java.util.OptionalInt; -import java.util.stream.IntStream; /** * Database with TTL support. @@ -122,6 +120,7 @@ public static TtlDB open(final DBOptions options, final String db_path, + " family handle."); } + 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++) { @@ -129,6 +128,13 @@ public static TtlDB open(final DBOptions options, final String db_path, 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) { + new IllegalArgumentException( + "You must provide the default column family in your columnFamilyDescriptors"); } final int[] ttlVals = new int[ttlValues.size()]; @@ -144,19 +150,7 @@ public static TtlDB open(final DBOptions options, final String db_path, } ttlDB.storeOptionsInstance(options); ttlDB.ownedColumnFamilyHandles.addAll(columnFamilyHandles); - - // ColumnFamilyHandle.isDefaultColumnFamily() doesn't work here yet, as we are in process of - // opening database - OptionalInt defaultCfIndex = IntStream.of(0, columnFamilyDescriptors.size() - 1) - .filter(x - -> Arrays.equals(columnFamilyDescriptors.get(x).getName(), - RocksDB.DEFAULT_COLUMN_FAMILY)) - .findFirst(); - if (defaultCfIndex.isPresent()) { - ttlDB.storeDefaultColumnFamilyHandle(columnFamilyHandles.get(defaultCfIndex.getAsInt())); - } else { - throw new RocksDBException("No defaultColumnFamily"); - } + ttlDB.storeDefaultColumnFamilyHandle(columnFamilyHandles.get(defaultColumnFamilyIndex)); return ttlDB; }