@ -27,6 +27,9 @@ import static org.junit.Assert.*;
* /
* /
public class BytewiseComparatorTest {
public class BytewiseComparatorTest {
private List < String > source_strings = Arrays . asList ( "b" , "d" , "f" , "h" , "j" , "l" ) ;
private List < String > interleaving_strings = Arrays . asList ( "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" ) ;
/ * *
/ * *
* Open the database using the C + + BytewiseComparatorImpl
* Open the database using the C + + BytewiseComparatorImpl
* and test the results against our Java BytewiseComparator
* and test the results against our Java BytewiseComparator
@ -42,7 +45,6 @@ public class BytewiseComparatorTest {
doRandomIterationTest (
doRandomIterationTest (
db ,
db ,
toJavaComparator ( new BytewiseComparator ( new ComparatorOptions ( ) ) ) ,
toJavaComparator ( new BytewiseComparator ( new ComparatorOptions ( ) ) ) ,
Arrays . asList ( "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ) ,
rnd ,
rnd ,
8 , 100 , 3
8 , 100 , 3
) ;
) ;
@ -67,7 +69,6 @@ public class BytewiseComparatorTest {
doRandomIterationTest (
doRandomIterationTest (
db ,
db ,
toJavaComparator ( new BytewiseComparator ( new ComparatorOptions ( ) ) ) ,
toJavaComparator ( new BytewiseComparator ( new ComparatorOptions ( ) ) ) ,
Arrays . asList ( "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ) ,
rnd ,
rnd ,
8 , 100 , 3
8 , 100 , 3
) ;
) ;
@ -94,7 +95,6 @@ public class BytewiseComparatorTest {
toJavaComparator ( new DirectBytewiseComparator (
toJavaComparator ( new DirectBytewiseComparator (
new ComparatorOptions ( ) )
new ComparatorOptions ( ) )
) ,
) ,
Arrays . asList ( "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ) ,
rnd ,
rnd ,
8 , 100 , 3
8 , 100 , 3
) ;
) ;
@ -121,7 +121,6 @@ public class BytewiseComparatorTest {
toJavaComparator ( new DirectBytewiseComparator (
toJavaComparator ( new DirectBytewiseComparator (
new ComparatorOptions ( ) )
new ComparatorOptions ( ) )
) ,
) ,
Arrays . asList ( "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ) ,
rnd ,
rnd ,
8 , 100 , 3
8 , 100 , 3
) ;
) ;
@ -148,7 +147,6 @@ public class BytewiseComparatorTest {
toJavaComparator (
toJavaComparator (
new ReverseBytewiseComparator ( new ComparatorOptions ( ) )
new ReverseBytewiseComparator ( new ComparatorOptions ( ) )
) ,
) ,
Arrays . asList ( "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ) ,
rnd ,
rnd ,
8 , 100 , 3
8 , 100 , 3
) ;
) ;
@ -176,7 +174,6 @@ public class BytewiseComparatorTest {
toJavaComparator (
toJavaComparator (
new ReverseBytewiseComparator ( new ComparatorOptions ( ) )
new ReverseBytewiseComparator ( new ComparatorOptions ( ) )
) ,
) ,
Arrays . asList ( "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ) ,
rnd ,
rnd ,
8 , 100 , 3
8 , 100 , 3
) ;
) ;
@ -188,7 +185,7 @@ public class BytewiseComparatorTest {
private void doRandomIterationTest (
private void doRandomIterationTest (
final RocksDB db , final java . util . Comparator < String > javaComparator ,
final RocksDB db , final java . util . Comparator < String > javaComparator ,
final List < String > source_strings , final Random rnd ,
final Random rnd ,
final int num_writes , final int num_iter_ops ,
final int num_writes , final int num_iter_ops ,
final int num_trigger_flush ) throws RocksDBException {
final int num_trigger_flush ) throws RocksDBException {
@ -228,7 +225,7 @@ public class BytewiseComparatorTest {
for ( int i = 0 ; i < num_iter_ops ; i + + ) {
for ( int i = 0 ; i < num_iter_ops ; i + + ) {
// Random walk and make sure iter and result_iter returns the
// Random walk and make sure iter and result_iter returns the
// same key and value
// same key and value
final int type = rnd . nextInt ( 6 ) ;
final int type = rnd . nextInt ( 7 ) ;
iter . status ( ) ;
iter . status ( ) ;
switch ( type ) {
switch ( type ) {
case 0 :
case 0 :
@ -242,14 +239,22 @@ public class BytewiseComparatorTest {
result_iter . seekToLast ( ) ;
result_iter . seekToLast ( ) ;
break ;
break ;
case 2 : {
case 2 : {
// Seek to random key
// Seek to random (existing or non-existing) key
final int key_idx = rnd . nextInt ( source _strings. size ( ) ) ;
final int key_idx = rnd . nextInt ( interleaving _strings. size ( ) ) ;
final String key = source _strings. get ( key_idx ) ;
final String key = interleaving _strings. get ( key_idx ) ;
iter . seek ( bytes ( key ) ) ;
iter . seek ( bytes ( key ) ) ;
result_iter . seek ( bytes ( key ) ) ;
result_iter . seek ( bytes ( key ) ) ;
break ;
break ;
}
}
case 3 :
case 3 : {
// SeekForPrev to random (existing or non-existing) key
final int key_idx = rnd . nextInt ( interleaving_strings . size ( ) ) ;
final String key = interleaving_strings . get ( key_idx ) ;
iter . seekForPrev ( bytes ( key ) ) ;
result_iter . seekForPrev ( bytes ( key ) ) ;
break ;
}
case 4 :
// Next
// Next
if ( is_valid ) {
if ( is_valid ) {
iter . next ( ) ;
iter . next ( ) ;
@ -258,7 +263,7 @@ public class BytewiseComparatorTest {
continue ;
continue ;
}
}
break ;
break ;
case 4 :
case 5 :
// Prev
// Prev
if ( is_valid ) {
if ( is_valid ) {
iter . prev ( ) ;
iter . prev ( ) ;
@ -268,7 +273,7 @@ public class BytewiseComparatorTest {
}
}
break ;
break ;
default : {
default : {
assert ( type = = 5 ) ;
assert ( type = = 6 ) ;
final int key_idx = rnd . nextInt ( source_strings . size ( ) ) ;
final int key_idx = rnd . nextInt ( source_strings . size ( ) ) ;
final String key = source_strings . get ( key_idx ) ;
final String key = source_strings . get ( key_idx ) ;
final byte [ ] result = db . get ( new ReadOptions ( ) , bytes ( key ) ) ;
final byte [ ] result = db . get ( new ReadOptions ( ) , bytes ( key ) ) ;
@ -413,6 +418,16 @@ public class BytewiseComparatorTest {
}
}
}
}
@Override
public void seekForPrev ( final byte [ ] target ) {
for ( offset = entries . size ( ) - 1 ; offset > = 0 ; offset - - ) {
if ( comparator . compare ( entries . get ( offset ) . getKey ( ) ,
( K ) new String ( target , StandardCharsets . UTF_8 ) ) < = 0 ) {
return ;
}
}
}
/ * *
/ * *
* Is ` a ` a prefix of ` b `
* Is ` a ` a prefix of ` b `
*
*