@ -5,16 +5,17 @@
package org.rocksdb ;
import java.util.* ;
import static java.nio.charset.StandardCharsets.UTF_8 ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertTrue ;
import java.util.* ;
import org.junit.ClassRule ;
import org.junit.Rule ;
import org.junit.Test ;
import org.junit.rules.TemporaryFolder ;
import static java.nio.charset.StandardCharsets.UTF_8 ;
import static org.assertj.core.api.Assertions.assertThat ;
public class ColumnFamilyTest {
@ClassRule
@ -141,33 +142,19 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) , cfNames ,
columnFamilyHandleList ) ) {
try {
assertThat ( columnFamilyHandleList . size ( ) ) . isEqualTo ( 2 ) ;
db . put ( "dfkey1" . getBytes ( ) , "dfvalue" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 0 ) , "dfkey2" . getBytes ( ) ,
"dfvalue" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey1" . getBytes ( ) ,
"newcfvalue" . getBytes ( ) ) ;
String retVal = new String ( db . get ( columnFamilyHandleList . get ( 1 ) ,
"newcfkey1" . getBytes ( ) ) ) ;
assertThat ( retVal ) . isEqualTo ( "newcfvalue" ) ;
assertThat ( ( db . get ( columnFamilyHandleList . get ( 1 ) ,
"dfkey1" . getBytes ( ) ) ) ) . isNull ( ) ;
db . delete ( columnFamilyHandleList . get ( 1 ) , "newcfkey1" . getBytes ( ) ) ;
assertThat ( ( db . get ( columnFamilyHandleList . get ( 1 ) ,
"newcfkey1" . getBytes ( ) ) ) ) . isNull ( ) ;
db . delete ( columnFamilyHandleList . get ( 0 ) , new WriteOptions ( ) ,
"dfkey2" . getBytes ( ) ) ;
assertThat ( db . get ( columnFamilyHandleList . get ( 0 ) , new ReadOptions ( ) ,
"dfkey2" . getBytes ( ) ) ) . isNull ( ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
assertThat ( columnFamilyHandleList . size ( ) ) . isEqualTo ( 2 ) ;
db . put ( "dfkey1" . getBytes ( ) , "dfvalue" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 0 ) , "dfkey2" . getBytes ( ) , "dfvalue" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey1" . getBytes ( ) , "newcfvalue" . getBytes ( ) ) ;
String retVal = new String ( db . get ( columnFamilyHandleList . get ( 1 ) , "newcfkey1" . getBytes ( ) ) ) ;
assertThat ( retVal ) . isEqualTo ( "newcfvalue" ) ;
assertThat ( ( db . get ( columnFamilyHandleList . get ( 1 ) , "dfkey1" . getBytes ( ) ) ) ) . isNull ( ) ;
db . delete ( columnFamilyHandleList . get ( 1 ) , "newcfkey1" . getBytes ( ) ) ;
assertThat ( ( db . get ( columnFamilyHandleList . get ( 1 ) , "newcfkey1" . getBytes ( ) ) ) ) . isNull ( ) ;
db . delete ( columnFamilyHandleList . get ( 0 ) , new WriteOptions ( ) , "dfkey2" . getBytes ( ) ) ;
assertThat ( db . get ( columnFamilyHandleList . get ( 0 ) , new ReadOptions ( ) , "dfkey2" . getBytes ( ) ) )
. isNull ( ) ;
}
}
@ -184,30 +171,22 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) , cfDescriptors ,
columnFamilyHandleList ) ) {
try {
db . put ( columnFamilyHandleList . get ( 0 ) , new WriteOptions ( ) ,
"key1" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . put ( "key2" . getBytes ( ) , "12345678" . getBytes ( ) ) ;
final byte [ ] outValue = new byte [ 5 ] ;
// not found value
int getResult = db . get ( "keyNotFound" . getBytes ( ) , outValue ) ;
assertThat ( getResult ) . isEqualTo ( RocksDB . NOT_FOUND ) ;
// found value which fits in outValue
getResult = db . get ( columnFamilyHandleList . get ( 0 ) , "key1" . getBytes ( ) ,
outValue ) ;
assertThat ( getResult ) . isNotEqualTo ( RocksDB . NOT_FOUND ) ;
assertThat ( outValue ) . isEqualTo ( "value" . getBytes ( ) ) ;
// found value which fits partially
getResult = db . get ( columnFamilyHandleList . get ( 0 ) , new ReadOptions ( ) ,
"key2" . getBytes ( ) , outValue ) ;
assertThat ( getResult ) . isNotEqualTo ( RocksDB . NOT_FOUND ) ;
assertThat ( outValue ) . isEqualTo ( "12345" . getBytes ( ) ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
db . put (
columnFamilyHandleList . get ( 0 ) , new WriteOptions ( ) , "key1" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . put ( "key2" . getBytes ( ) , "12345678" . getBytes ( ) ) ;
final byte [ ] outValue = new byte [ 5 ] ;
// not found value
int getResult = db . get ( "keyNotFound" . getBytes ( ) , outValue ) ;
assertThat ( getResult ) . isEqualTo ( RocksDB . NOT_FOUND ) ;
// found value which fits in outValue
getResult = db . get ( columnFamilyHandleList . get ( 0 ) , "key1" . getBytes ( ) , outValue ) ;
assertThat ( getResult ) . isNotEqualTo ( RocksDB . NOT_FOUND ) ;
assertThat ( outValue ) . isEqualTo ( "value" . getBytes ( ) ) ;
// found value which fits partially
getResult =
db . get ( columnFamilyHandleList . get ( 0 ) , new ReadOptions ( ) , "key2" . getBytes ( ) , outValue ) ;
assertThat ( getResult ) . isNotEqualTo ( RocksDB . NOT_FOUND ) ;
assertThat ( outValue ) . isEqualTo ( "12345" . getBytes ( ) ) ;
}
}
@ -223,22 +202,12 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) , cfDescriptors ,
columnFamilyHandleList ) ) {
ColumnFamilyHandle tmpColumnFamilyHandle = null ;
try {
tmpColumnFamilyHandle = db . createColumnFamily (
new ColumnFamilyDescriptor ( "tmpCF" . getBytes ( ) ,
new ColumnFamilyOptions ( ) ) ) ;
db . put ( tmpColumnFamilyHandle , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . dropColumnFamily ( tmpColumnFamilyHandle ) ;
assertThat ( tmpColumnFamilyHandle . isOwningHandle ( ) ) . isTrue ( ) ;
} finally {
if ( tmpColumnFamilyHandle ! = null ) {
tmpColumnFamilyHandle . close ( ) ;
}
for ( ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
ColumnFamilyHandle tmpColumnFamilyHandle ;
tmpColumnFamilyHandle = db . createColumnFamily (
new ColumnFamilyDescriptor ( "tmpCF" . getBytes ( ) , new ColumnFamilyOptions ( ) ) ) ;
db . put ( tmpColumnFamilyHandle , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . dropColumnFamily ( tmpColumnFamilyHandle ) ;
assertThat ( tmpColumnFamilyHandle . isOwningHandle ( ) ) . isTrue ( ) ;
}
}
@ -256,29 +225,15 @@ public class ColumnFamilyTest {
columnFamilyHandleList ) ) {
ColumnFamilyHandle tmpColumnFamilyHandle = null ;
ColumnFamilyHandle tmpColumnFamilyHandle2 = null ;
try {
tmpColumnFamilyHandle = db . createColumnFamily (
new ColumnFamilyDescriptor ( "tmpCF" . getBytes ( ) ,
new ColumnFamilyOptions ( ) ) ) ;
tmpColumnFamilyHandle2 = db . createColumnFamily (
new ColumnFamilyDescriptor ( "tmpCF2" . getBytes ( ) ,
new ColumnFamilyOptions ( ) ) ) ;
db . put ( tmpColumnFamilyHandle , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . put ( tmpColumnFamilyHandle2 , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . dropColumnFamilies ( Arrays . asList ( tmpColumnFamilyHandle , tmpColumnFamilyHandle2 ) ) ;
assertThat ( tmpColumnFamilyHandle . isOwningHandle ( ) ) . isTrue ( ) ;
assertThat ( tmpColumnFamilyHandle2 . isOwningHandle ( ) ) . isTrue ( ) ;
} finally {
if ( tmpColumnFamilyHandle ! = null ) {
tmpColumnFamilyHandle . close ( ) ;
}
if ( tmpColumnFamilyHandle2 ! = null ) {
tmpColumnFamilyHandle2 . close ( ) ;
}
for ( ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
tmpColumnFamilyHandle = db . createColumnFamily (
new ColumnFamilyDescriptor ( "tmpCF" . getBytes ( ) , new ColumnFamilyOptions ( ) ) ) ;
tmpColumnFamilyHandle2 = db . createColumnFamily (
new ColumnFamilyDescriptor ( "tmpCF2" . getBytes ( ) , new ColumnFamilyOptions ( ) ) ) ;
db . put ( tmpColumnFamilyHandle , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . put ( tmpColumnFamilyHandle2 , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . dropColumnFamilies ( Arrays . asList ( tmpColumnFamilyHandle , tmpColumnFamilyHandle2 ) ) ;
assertThat ( tmpColumnFamilyHandle . isOwningHandle ( ) ) . isTrue ( ) ;
assertThat ( tmpColumnFamilyHandle2 . isOwningHandle ( ) ) . isTrue ( ) ;
}
}
@ -300,36 +255,24 @@ public class ColumnFamilyTest {
cfDescriptors , columnFamilyHandleList ) ;
final WriteBatch writeBatch = new WriteBatch ( ) ;
final WriteOptions writeOpt = new WriteOptions ( ) ) {
try {
writeBatch . put ( "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
writeBatch . put ( db . getDefaultColumnFamily ( ) ,
"mergeKey" . getBytes ( ) , "merge" . getBytes ( ) ) ;
writeBatch . merge ( db . getDefaultColumnFamily ( ) , "mergeKey" . getBytes ( ) ,
"merge" . getBytes ( ) ) ;
writeBatch . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) ,
"value" . getBytes ( ) ) ;
writeBatch . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey2" . getBytes ( ) ,
"value2" . getBytes ( ) ) ;
writeBatch . delete ( "xyz" . getBytes ( ) ) ;
writeBatch . delete ( columnFamilyHandleList . get ( 1 ) , "xyz" . getBytes ( ) ) ;
db . write ( writeOpt , writeBatch ) ;
assertThat ( db . get ( columnFamilyHandleList . get ( 1 ) ,
"xyz" . getBytes ( ) ) = = null ) ;
assertThat ( new String ( db . get ( columnFamilyHandleList . get ( 1 ) ,
"newcfkey" . getBytes ( ) ) ) ) . isEqualTo ( "value" ) ;
assertThat ( new String ( db . get ( columnFamilyHandleList . get ( 1 ) ,
"newcfkey2" . getBytes ( ) ) ) ) . isEqualTo ( "value2" ) ;
assertThat ( new String ( db . get ( "key" . getBytes ( ) ) ) ) . isEqualTo ( "value" ) ;
// check if key is merged
assertThat ( new String ( db . get ( db . getDefaultColumnFamily ( ) ,
"mergeKey" . getBytes ( ) ) ) ) . isEqualTo ( "merge,merge" ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
writeBatch . put ( "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
writeBatch . put ( db . getDefaultColumnFamily ( ) , "mergeKey" . getBytes ( ) , "merge" . getBytes ( ) ) ;
writeBatch . merge ( db . getDefaultColumnFamily ( ) , "mergeKey" . getBytes ( ) , "merge" . getBytes ( ) ) ;
writeBatch . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) , "value" . getBytes ( ) ) ;
writeBatch . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey2" . getBytes ( ) , "value2" . getBytes ( ) ) ;
writeBatch . delete ( "xyz" . getBytes ( ) ) ;
writeBatch . delete ( columnFamilyHandleList . get ( 1 ) , "xyz" . getBytes ( ) ) ;
db . write ( writeOpt , writeBatch ) ;
assertThat ( db . get ( columnFamilyHandleList . get ( 1 ) , "xyz" . getBytes ( ) ) = = null ) ;
assertThat ( new String ( db . get ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) ) ) )
. isEqualTo ( "value" ) ;
assertThat ( new String ( db . get ( columnFamilyHandleList . get ( 1 ) , "newcfkey2" . getBytes ( ) ) ) )
. isEqualTo ( "value2" ) ;
assertThat ( new String ( db . get ( "key" . getBytes ( ) ) ) ) . isEqualTo ( "value" ) ;
// check if key is merged
assertThat ( new String ( db . get ( db . getDefaultColumnFamily ( ) , "mergeKey" . getBytes ( ) ) ) )
. isEqualTo ( "merge,merge" ) ;
}
}
}
@ -346,32 +289,21 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ,
cfDescriptors , columnFamilyHandleList ) ) {
try {
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) ,
"value" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey2" . getBytes ( ) ,
"value2" . getBytes ( ) ) ;
try ( final RocksIterator rocksIterator =
db . newIterator ( columnFamilyHandleList . get ( 1 ) ) ) {
rocksIterator . seekToFirst ( ) ;
Map < String , String > refMap = new HashMap < > ( ) ;
refMap . put ( "newcfkey" , "value" ) ;
refMap . put ( "newcfkey2" , "value2" ) ;
int i = 0 ;
while ( rocksIterator . isValid ( ) ) {
i + + ;
assertThat ( refMap . get ( new String ( rocksIterator . key ( ) ) ) ) .
isEqualTo ( new String ( rocksIterator . value ( ) ) ) ;
rocksIterator . next ( ) ;
}
assertThat ( i ) . isEqualTo ( 2 ) ;
}
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey2" . getBytes ( ) , "value2" . getBytes ( ) ) ;
try ( final RocksIterator rocksIterator = db . newIterator ( columnFamilyHandleList . get ( 1 ) ) ) {
rocksIterator . seekToFirst ( ) ;
Map < String , String > refMap = new HashMap < > ( ) ;
refMap . put ( "newcfkey" , "value" ) ;
refMap . put ( "newcfkey2" , "value2" ) ;
int i = 0 ;
while ( rocksIterator . isValid ( ) ) {
i + + ;
assertThat ( refMap . get ( new String ( rocksIterator . key ( ) ) ) )
. isEqualTo ( new String ( rocksIterator . value ( ) ) ) ;
rocksIterator . next ( ) ;
}
assertThat ( i ) . isEqualTo ( 2 ) ;
}
}
}
@ -388,35 +320,20 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ,
cfDescriptors , columnFamilyHandleList ) ) {
try {
db . put ( columnFamilyHandleList . get ( 0 ) , "key" . getBytes ( ) ,
"value" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) ,
"value" . getBytes ( ) ) ;
final List < byte [ ] > keys = Arrays . asList ( new byte [ ] [ ] {
"key" . getBytes ( ) , "newcfkey" . getBytes ( )
} ) ;
List < byte [ ] > retValues = db . multiGetAsList ( columnFamilyHandleList , keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) )
. isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) )
. isEqualTo ( "value" ) ;
retValues = db . multiGetAsList ( new ReadOptions ( ) , columnFamilyHandleList ,
keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) )
. isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) )
. isEqualTo ( "value" ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
db . put ( columnFamilyHandleList . get ( 0 ) , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) , "value" . getBytes ( ) ) ;
final List < byte [ ] > keys =
Arrays . asList ( new byte [ ] [ ] { "key" . getBytes ( ) , "newcfkey" . getBytes ( ) } ) ;
List < byte [ ] > retValues = db . multiGetAsList ( columnFamilyHandleList , keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) ) . isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) ) . isEqualTo ( "value" ) ;
retValues = db . multiGetAsList ( new ReadOptions ( ) , columnFamilyHandleList , keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) ) . isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) ) . isEqualTo ( "value" ) ;
}
}
@ -432,35 +349,19 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ,
cfDescriptors , columnFamilyHandleList ) ) {
try {
db . put ( columnFamilyHandleList . get ( 0 ) , "key" . getBytes ( ) ,
"value" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) ,
"value" . getBytes ( ) ) ;
final List < byte [ ] > keys = Arrays . asList ( new byte [ ] [ ] {
"key" . getBytes ( ) , "newcfkey" . getBytes ( )
} ) ;
List < byte [ ] > retValues = db . multiGetAsList ( columnFamilyHandleList ,
keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) )
. isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) )
. isEqualTo ( "value" ) ;
retValues = db . multiGetAsList ( new ReadOptions ( ) , columnFamilyHandleList ,
keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) )
. isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) )
. isEqualTo ( "value" ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
db . put ( columnFamilyHandleList . get ( 0 ) , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "newcfkey" . getBytes ( ) , "value" . getBytes ( ) ) ;
final List < byte [ ] > keys =
Arrays . asList ( new byte [ ] [ ] { "key" . getBytes ( ) , "newcfkey" . getBytes ( ) } ) ;
List < byte [ ] > retValues = db . multiGetAsList ( columnFamilyHandleList , keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) ) . isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) ) . isEqualTo ( "value" ) ;
retValues = db . multiGetAsList ( new ReadOptions ( ) , columnFamilyHandleList , keys ) ;
assertThat ( retValues . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( new String ( retValues . get ( 0 ) ) ) . isEqualTo ( "value" ) ;
assertThat ( new String ( retValues . get ( 1 ) ) ) . isEqualTo ( "value" ) ;
}
}
@ -476,30 +377,18 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ,
cfDescriptors , columnFamilyHandleList ) ) {
try {
assertThat ( db . getProperty ( "rocksdb.estimate-num-keys" ) ) .
isNotNull ( ) ;
assertThat ( db . getLongProperty ( columnFamilyHandleList . get ( 0 ) ,
"rocksdb.estimate-num-keys" ) ) . isGreaterThanOrEqualTo ( 0 ) ;
assertThat ( db . getProperty ( "rocksdb.stats" ) ) . isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 0 ) ,
"rocksdb.sstables" ) ) . isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 1 ) ,
"rocksdb.estimate-num-keys" ) ) . isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 1 ) ,
"rocksdb.stats" ) ) . isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 1 ) ,
"rocksdb.sstables" ) ) . isNotNull ( ) ;
assertThat ( db . getAggregatedLongProperty ( "rocksdb.estimate-num-keys" ) ) .
isNotNull ( ) ;
assertThat ( db . getAggregatedLongProperty ( "rocksdb.estimate-num-keys" ) ) .
isGreaterThanOrEqualTo ( 0 ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
assertThat ( db . getProperty ( "rocksdb.estimate-num-keys" ) ) . isNotNull ( ) ;
assertThat ( db . getLongProperty ( columnFamilyHandleList . get ( 0 ) , "rocksdb.estimate-num-keys" ) )
. isGreaterThanOrEqualTo ( 0 ) ;
assertThat ( db . getProperty ( "rocksdb.stats" ) ) . isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 0 ) , "rocksdb.sstables" ) ) . isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 1 ) , "rocksdb.estimate-num-keys" ) )
. isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 1 ) , "rocksdb.stats" ) ) . isNotNull ( ) ;
assertThat ( db . getProperty ( columnFamilyHandleList . get ( 1 ) , "rocksdb.sstables" ) ) . isNotNull ( ) ;
assertThat ( db . getAggregatedLongProperty ( "rocksdb.estimate-num-keys" ) ) . isNotNull ( ) ;
assertThat ( db . getAggregatedLongProperty ( "rocksdb.estimate-num-keys" ) )
. isGreaterThanOrEqualTo ( 0 ) ;
}
}
@ -547,10 +436,6 @@ public class ColumnFamilyTest {
rocksIterator . close ( ) ;
}
}
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
}
}
@ -566,15 +451,8 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ,
cfDescriptors , columnFamilyHandleList ) ) {
try {
db . dropColumnFamily ( columnFamilyHandleList . get ( 1 ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "key" . getBytes ( ) ,
"value" . getBytes ( ) ) ;
} finally {
for ( ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
db . dropColumnFamily ( columnFamilyHandleList . get ( 1 ) ) ;
db . put ( columnFamilyHandleList . get ( 1 ) , "key" . getBytes ( ) , "value" . getBytes ( ) ) ;
}
}
@ -589,15 +467,8 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ,
cfDescriptors , columnFamilyHandleList ) ) {
try {
db . dropColumnFamily ( columnFamilyHandleList . get ( 1 ) ) ;
db . delete ( columnFamilyHandleList . get ( 1 ) , "key" . getBytes ( ) ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
db . dropColumnFamily ( columnFamilyHandleList . get ( 1 ) ) ;
db . delete ( columnFamilyHandleList . get ( 1 ) , "key" . getBytes ( ) ) ;
}
}
@ -612,15 +483,8 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) , cfDescriptors ,
columnFamilyHandleList ) ) {
try {
db . dropColumnFamily ( columnFamilyHandleList . get ( 1 ) ) ;
db . get ( columnFamilyHandleList . get ( 1 ) , "key" . getBytes ( ) ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
db . dropColumnFamily ( columnFamilyHandleList . get ( 1 ) ) ;
db . get ( columnFamilyHandleList . get ( 1 ) , "key" . getBytes ( ) ) ;
}
}
@ -635,19 +499,11 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) , cfDescriptors ,
columnFamilyHandleList ) ) {
try {
final List < byte [ ] > keys = new ArrayList < > ( ) ;
keys . add ( "key" . getBytes ( ) ) ;
keys . add ( "newcfkey" . getBytes ( ) ) ;
final List < ColumnFamilyHandle > cfCustomList = new ArrayList < > ( ) ;
db . multiGetAsList ( cfCustomList , keys ) ;
} finally {
for ( final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}
final List < byte [ ] > keys = new ArrayList < > ( ) ;
keys . add ( "key" . getBytes ( ) ) ;
keys . add ( "newcfkey" . getBytes ( ) ) ;
final List < ColumnFamilyHandle > cfCustomList = new ArrayList < > ( ) ;
db . multiGetAsList ( cfCustomList , keys ) ;
}
}
@ -661,25 +517,12 @@ public class ColumnFamilyTest {
final byte [ ] b0 = new byte [ ] { ( byte ) 0x00 } ;
final byte [ ] b1 = new byte [ ] { ( byte ) 0x01 } ;
final byte [ ] b2 = new byte [ ] { ( byte ) 0x02 } ;
ColumnFamilyHandle cf1 = null , cf2 = null , cf3 = null ;
try {
cf1 = db . createColumnFamily ( new ColumnFamilyDescriptor ( b0 ) ) ;
cf2 = db . createColumnFamily ( new ColumnFamilyDescriptor ( b1 ) ) ;
final List < byte [ ] > families = RocksDB . listColumnFamilies ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
assertThat ( families ) . contains ( "default" . getBytes ( ) , b0 , b1 ) ;
cf3 = db . createColumnFamily ( new ColumnFamilyDescriptor ( b2 ) ) ;
} finally {
if ( cf1 ! = null ) {
cf1 . close ( ) ;
}
if ( cf2 ! = null ) {
cf2 . close ( ) ;
}
if ( cf3 ! = null ) {
cf3 . close ( ) ;
}
}
db . createColumnFamily ( new ColumnFamilyDescriptor ( b0 ) ) ;
db . createColumnFamily ( new ColumnFamilyDescriptor ( b1 ) ) ;
final List < byte [ ] > families =
RocksDB . listColumnFamilies ( options , dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
assertThat ( families ) . contains ( "default" . getBytes ( ) , b0 , b1 ) ;
db . createColumnFamily ( new ColumnFamilyDescriptor ( b2 ) ) ;
}
}
@ -690,22 +533,13 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
) {
try {
final byte [ ] b0 = new byte [ ] { 0 , 0 } ;
final byte [ ] b1 = new byte [ ] { 0 , 1 } ;
cf1 = db . createColumnFamily ( new ColumnFamilyDescriptor ( b0 ) ) ;
cf2 = db . createColumnFamily ( new ColumnFamilyDescriptor ( b1 ) ) ;
final List < byte [ ] > families = RocksDB . listColumnFamilies ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
assertThat ( families ) . contains ( "default" . getBytes ( ) , b0 , b1 ) ;
} finally {
if ( cf1 ! = null ) {
cf1 . close ( ) ;
}
if ( cf2 ! = null ) {
cf2 . close ( ) ;
}
}
final byte [ ] b0 = new byte [ ] { 0 , 0 } ;
final byte [ ] b1 = new byte [ ] { 0 , 1 } ;
cf1 = db . createColumnFamily ( new ColumnFamilyDescriptor ( b0 ) ) ;
cf2 = db . createColumnFamily ( new ColumnFamilyDescriptor ( b1 ) ) ;
final List < byte [ ] > families =
RocksDB . listColumnFamilies ( options , dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
assertThat ( families ) . contains ( "default" . getBytes ( ) , b0 , b1 ) ;
}
}
@ -716,17 +550,57 @@ public class ColumnFamilyTest {
final RocksDB db = RocksDB . open ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
) {
final String simplifiedChinese = "\u7b80\u4f53\u5b57" ;
columnFamilyHandle =
db . createColumnFamily ( new ColumnFamilyDescriptor ( simplifiedChinese . getBytes ( ) ) ) ;
final List < byte [ ] > families =
RocksDB . listColumnFamilies ( options , dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
assertThat ( families ) . contains ( "default" . getBytes ( ) , simplifiedChinese . getBytes ( ) ) ;
}
}
@Test
public void testDestroyColumnFamilyHandle ( ) throws RocksDBException {
try ( final Options options = new Options ( ) . setCreateIfMissing ( true ) ;
final RocksDB db = RocksDB . open ( options , dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ; ) {
final byte [ ] name1 = "cf1" . getBytes ( ) ;
final byte [ ] name2 = "cf2" . getBytes ( ) ;
final ColumnFamilyDescriptor desc1 = new ColumnFamilyDescriptor ( name1 ) ;
final ColumnFamilyDescriptor desc2 = new ColumnFamilyDescriptor ( name2 ) ;
final ColumnFamilyHandle cf1 = db . createColumnFamily ( desc1 ) ;
final ColumnFamilyHandle cf2 = db . createColumnFamily ( desc2 ) ;
assertTrue ( cf1 . isOwningHandle ( ) ) ;
assertTrue ( cf2 . isOwningHandle ( ) ) ;
assertFalse ( cf1 . isDefaultColumnFamily ( ) ) ;
db . destroyColumnFamilyHandle ( cf1 ) ;
// At this point cf1 should not be used!
assertFalse ( cf1 . isOwningHandle ( ) ) ;
assertTrue ( cf2 . isOwningHandle ( ) ) ;
}
}
@Test
@Deprecated
/ * *
* @deprecated Now explicitly closing instances of ColumnFamilyHandle is not required .
* RocksDB instance will take care of closing its associated ColumnFamilyHandle objects .
* /
public void testColumnFamilyCloseBeforeDb ( ) throws RocksDBException {
final List < ColumnFamilyDescriptor > cfNames =
Arrays . asList ( new ColumnFamilyDescriptor ( RocksDB . DEFAULT_COLUMN_FAMILY ) ,
new ColumnFamilyDescriptor ( "new_cf" . getBytes ( ) ) ) ;
final List < ColumnFamilyHandle > columnFamilyHandleList = new ArrayList < > ( ) ;
try ( final DBOptions options =
new DBOptions ( ) . setCreateIfMissing ( true ) . setCreateMissingColumnFamilies ( true ) ;
final RocksDB db = RocksDB . open (
options , dbFolder . getRoot ( ) . getAbsolutePath ( ) , cfNames , columnFamilyHandleList ) ) {
try {
final String simplifiedChinese = "\u7b80\u4f53\u5b57" ;
columnFamilyHandle = db . createColumnFamily (
new ColumnFamilyDescriptor ( simplifiedChinese . getBytes ( ) ) ) ;
final List < byte [ ] > families = RocksDB . listColumnFamilies ( options ,
dbFolder . getRoot ( ) . getAbsolutePath ( ) ) ;
assertThat ( families ) . contains ( "default" . getBytes ( ) ,
simplifiedChinese . getBytes ( ) ) ;
db . put ( "testKey" . getBytes ( ) , "tstValue" . getBytes ( ) ) ;
// Do something...
} finally {
if ( columnFamilyHandle ! = null ) {
for ( final ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList ) {
columnFamilyHandle . close ( ) ;
}
}