12
12
import java .nio .ByteBuffer ;
13
13
import java .util .*;
14
14
import java .util .concurrent .atomic .AtomicReference ;
15
- import java .util .stream .IntStream ;
16
15
import org .rocksdb .util .Environment ;
17
16
18
17
/**
@@ -306,11 +305,19 @@ public static RocksDB open(final DBOptions options, final String path,
306
305
307
306
final byte [][] cfNames = new byte [columnFamilyDescriptors .size ()][];
308
307
final long [] cfOptionHandles = new long [columnFamilyDescriptors .size ()];
308
+ int defaultColumnFamilyIndex = -1 ;
309
309
for (int i = 0 ; i < columnFamilyDescriptors .size (); i ++) {
310
310
final ColumnFamilyDescriptor cfDescriptor = columnFamilyDescriptors
311
311
.get (i );
312
312
cfNames [i ] = cfDescriptor .getName ();
313
313
cfOptionHandles [i ] = cfDescriptor .getOptions ().nativeHandle_ ;
314
+ if (Arrays .equals (cfDescriptor .getName (),
315
+ RocksDB .DEFAULT_COLUMN_FAMILY )) {
316
+ defaultColumnFamilyIndex = i ;
317
+ }
318
+ }
319
+ if (defaultColumnFamilyIndex < 0 ) {
320
+ new IllegalArgumentException ("You must provide the default column family in your columnFamilyDescriptors" );
314
321
}
315
322
316
323
final long [] handles = open (options .nativeHandle_ , path , cfNames ,
@@ -325,20 +332,7 @@ public static RocksDB open(final DBOptions options, final String path,
325
332
}
326
333
327
334
db .ownedColumnFamilyHandles .addAll (columnFamilyHandles );
328
-
329
- // ColumnFamilyHandle.isDefaultColumnFamily() doesn't work here yet, as we are in process of
330
- // opening database
331
- OptionalInt defaultCfIndex = IntStream .of (0 , columnFamilyDescriptors .size () - 1 )
332
- .filter (x
333
- -> Arrays .equals (columnFamilyDescriptors .get (x ).getName (),
334
- RocksDB .DEFAULT_COLUMN_FAMILY ))
335
- .findFirst ();
336
- if (defaultCfIndex .isPresent ()) {
337
- db .storeDefaultColumnFamilyHandle (columnFamilyHandles .get (defaultCfIndex .getAsInt ()));
338
- } else {
339
- throw new RocksDBException ("No default column family" );
340
- }
341
-
335
+ db .storeDefaultColumnFamilyHandle (columnFamilyHandles .get (defaultColumnFamilyIndex ));
342
336
return db ;
343
337
}
344
338
0 commit comments