@ -32,6 +32,7 @@ int main() {
# include "util/string_util.h"
# include "util/string_util.h"
# include "util/testharness.h"
# include "util/testharness.h"
# include "utilities/merge_operators.h"
# include "utilities/merge_operators.h"
# include "util/coding.h"
using GFLAGS : : ParseCommandLineFlags ;
using GFLAGS : : ParseCommandLineFlags ;
@ -65,12 +66,16 @@ struct TestKey {
} ;
} ;
// return a slice backed by test_key
// return a slice backed by test_key
inline Slice TestKeyToSlice ( const TestKey & test_key ) {
inline Slice TestKeyToSlice ( std : : string & s , const TestKey & test_key ) {
return Slice ( ( const char * ) & test_key , sizeof ( test_key ) ) ;
s . clear ( ) ;
PutFixed64 ( & s , test_key . prefix ) ;
PutFixed64 ( & s , test_key . sorted ) ;
return Slice ( s . c_str ( ) , s . size ( ) ) ;
}
}
inline const TestKey * SliceToTestKey ( const Slice & slice ) {
inline const TestKey SliceToTestKey ( const Slice & slice ) {
return ( const TestKey * ) slice . data ( ) ;
return TestKey ( DecodeFixed64 ( slice . data ( ) ) ,
DecodeFixed64 ( slice . data ( ) + 8 ) ) ;
}
}
class TestKeyComparator : public Comparator {
class TestKeyComparator : public Comparator {
@ -79,8 +84,10 @@ class TestKeyComparator : public Comparator {
// Compare needs to be aware of the possibility of a and/or b is
// Compare needs to be aware of the possibility of a and/or b is
// prefix only
// prefix only
virtual int Compare ( const Slice & a , const Slice & b ) const override {
virtual int Compare ( const Slice & a , const Slice & b ) const override {
const TestKey * key_a = SliceToTestKey ( a ) ;
const TestKey kkey_a = SliceToTestKey ( a ) ;
const TestKey * key_b = SliceToTestKey ( b ) ;
const TestKey kkey_b = SliceToTestKey ( b ) ;
const TestKey * key_a = & kkey_a ;
const TestKey * key_b = & kkey_b ;
if ( key_a - > prefix ! = key_b - > prefix ) {
if ( key_a - > prefix ! = key_b - > prefix ) {
if ( key_a - > prefix < key_b - > prefix ) return - 1 ;
if ( key_a - > prefix < key_b - > prefix ) return - 1 ;
if ( key_a - > prefix > key_b - > prefix ) return 1 ;
if ( key_a - > prefix > key_b - > prefix ) return 1 ;
@ -111,7 +118,8 @@ class TestKeyComparator : public Comparator {
}
}
bool operator ( ) ( const TestKey & a , const TestKey & b ) const {
bool operator ( ) ( const TestKey & a , const TestKey & b ) const {
return Compare ( TestKeyToSlice ( a ) , TestKeyToSlice ( b ) ) < 0 ;
std : : string sa , sb ;
return Compare ( TestKeyToSlice ( sa , a ) , TestKeyToSlice ( sb , b ) ) < 0 ;
}
}
virtual const char * Name ( ) const override {
virtual const char * Name ( ) const override {
@ -128,30 +136,35 @@ namespace {
void PutKey ( DB * db , WriteOptions write_options , uint64_t prefix ,
void PutKey ( DB * db , WriteOptions write_options , uint64_t prefix ,
uint64_t suffix , const Slice & value ) {
uint64_t suffix , const Slice & value ) {
TestKey test_key ( prefix , suffix ) ;
TestKey test_key ( prefix , suffix ) ;
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
ASSERT_OK ( db - > Put ( write_options , key , value ) ) ;
ASSERT_OK ( db - > Put ( write_options , key , value ) ) ;
}
}
void PutKey ( DB * db , WriteOptions write_options , const TestKey & test_key ,
void PutKey ( DB * db , WriteOptions write_options , const TestKey & test_key ,
const Slice & value ) {
const Slice & value ) {
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
ASSERT_OK ( db - > Put ( write_options , key , value ) ) ;
ASSERT_OK ( db - > Put ( write_options , key , value ) ) ;
}
}
void MergeKey ( DB * db , WriteOptions write_options , const TestKey & test_key ,
void MergeKey ( DB * db , WriteOptions write_options , const TestKey & test_key ,
const Slice & value ) {
const Slice & value ) {
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
ASSERT_OK ( db - > Merge ( write_options , key , value ) ) ;
ASSERT_OK ( db - > Merge ( write_options , key , value ) ) ;
}
}
void DeleteKey ( DB * db , WriteOptions write_options , const TestKey & test_key ) {
void DeleteKey ( DB * db , WriteOptions write_options , const TestKey & test_key ) {
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
ASSERT_OK ( db - > Delete ( write_options , key ) ) ;
ASSERT_OK ( db - > Delete ( write_options , key ) ) ;
}
}
void SeekIterator ( Iterator * iter , uint64_t prefix , uint64_t suffix ) {
void SeekIterator ( Iterator * iter , uint64_t prefix , uint64_t suffix ) {
TestKey test_key ( prefix , suffix ) ;
TestKey test_key ( prefix , suffix ) ;
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
iter - > Seek ( key ) ;
iter - > Seek ( key ) ;
}
}
@ -160,7 +173,8 @@ const std::string kNotFoundResult = "NOT_FOUND";
std : : string Get ( DB * db , const ReadOptions & read_options , uint64_t prefix ,
std : : string Get ( DB * db , const ReadOptions & read_options , uint64_t prefix ,
uint64_t suffix ) {
uint64_t suffix ) {
TestKey test_key ( prefix , suffix ) ;
TestKey test_key ( prefix , suffix ) ;
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s2 ;
Slice key = TestKeyToSlice ( s2 , test_key ) ;
std : : string result ;
std : : string result ;
Status s = db - > Get ( read_options , key , & result ) ;
Status s = db - > Get ( read_options , key , & result ) ;
@ -527,7 +541,9 @@ TEST_F(PrefixTest, PrefixValid) {
PutKey ( db . get ( ) , write_options , 12345 , 9 , v19 ) ;
PutKey ( db . get ( ) , write_options , 12345 , 9 , v19 ) ;
PutKey ( db . get ( ) , write_options , 12346 , 8 , v16 ) ;
PutKey ( db . get ( ) , write_options , 12346 , 8 , v16 ) ;
db - > Flush ( FlushOptions ( ) ) ;
db - > Flush ( FlushOptions ( ) ) ;
db - > Delete ( write_options , TestKeyToSlice ( TestKey ( 12346 , 8 ) ) ) ;
TestKey test_key ( 12346 , 8 ) ;
std : : string s ;
db - > Delete ( write_options , TestKeyToSlice ( s , test_key ) ) ;
db - > Flush ( FlushOptions ( ) ) ;
db - > Flush ( FlushOptions ( ) ) ;
read_options . prefix_same_as_start = true ;
read_options . prefix_same_as_start = true ;
std : : unique_ptr < Iterator > iter ( db - > NewIterator ( read_options ) ) ;
std : : unique_ptr < Iterator > iter ( db - > NewIterator ( read_options ) ) ;
@ -583,7 +599,8 @@ TEST_F(PrefixTest, DynamicPrefixIterator) {
for ( uint64_t sorted = 0 ; sorted < FLAGS_items_per_prefix ; sorted + + ) {
for ( uint64_t sorted = 0 ; sorted < FLAGS_items_per_prefix ; sorted + + ) {
TestKey test_key ( prefix , sorted ) ;
TestKey test_key ( prefix , sorted ) ;
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
std : : string value ( FLAGS_value_size , 0 ) ;
std : : string value ( FLAGS_value_size , 0 ) ;
perf_context . Reset ( ) ;
perf_context . Reset ( ) ;
@ -605,7 +622,8 @@ TEST_F(PrefixTest, DynamicPrefixIterator) {
for ( auto prefix : prefixes ) {
for ( auto prefix : prefixes ) {
TestKey test_key ( prefix , FLAGS_items_per_prefix / 2 ) ;
TestKey test_key ( prefix , FLAGS_items_per_prefix / 2 ) ;
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
std : : string value = " v " + ToString ( 0 ) ;
std : : string value = " v " + ToString ( 0 ) ;
perf_context . Reset ( ) ;
perf_context . Reset ( ) ;
@ -639,7 +657,8 @@ TEST_F(PrefixTest, DynamicPrefixIterator) {
prefix < FLAGS_total_prefixes + 10000 ;
prefix < FLAGS_total_prefixes + 10000 ;
prefix + + ) {
prefix + + ) {
TestKey test_key ( prefix , 0 ) ;
TestKey test_key ( prefix , 0 ) ;
Slice key = TestKeyToSlice ( test_key ) ;
std : : string s ;
Slice key = TestKeyToSlice ( s , test_key ) ;
perf_context . Reset ( ) ;
perf_context . Reset ( ) ;
StopWatchNano timer ( Env : : Default ( ) , true ) ;
StopWatchNano timer ( Env : : Default ( ) , true ) ;
@ -725,8 +744,8 @@ TEST_F(PrefixTest, PrefixSeekModePrev) {
ASSERT_TRUE ( iter - > Valid ( ) ) ;
ASSERT_TRUE ( iter - > Valid ( ) ) ;
if ( FLAGS_enable_print ) {
if ( FLAGS_enable_print ) {
std : : cout < < " round " < < prefix
std : : cout < < " round " < < prefix
< < " iter: " < < SliceToTestKey ( iter - > key ( ) ) - > prefix
< < " iter: " < < SliceToTestKey ( iter - > key ( ) ) . prefix
< < SliceToTestKey ( iter - > key ( ) ) - > sorted
< < SliceToTestKey ( iter - > key ( ) ) . sorted
< < " | map: " < < it - > first . prefix < < it - > first . sorted < < " | "
< < " | map: " < < it - > first . prefix < < it - > first . sorted < < " | "
< < iter - > value ( ) . ToString ( ) < < " " < < it - > second < < std : : endl ;
< < iter - > value ( ) . ToString ( ) < < " " < < it - > second < < std : : endl ;
}
}
@ -747,16 +766,16 @@ TEST_F(PrefixTest, PrefixSeekModePrev) {
}
}
}
}
if ( ! iter - > Valid ( ) | |
if ( ! iter - > Valid ( ) | |
SliceToTestKey ( iter - > key ( ) ) - > prefix ! = stored_prefix ) {
SliceToTestKey ( iter - > key ( ) ) . prefix ! = stored_prefix ) {
break ;
break ;
}
}
stored_prefix = SliceToTestKey ( iter - > key ( ) ) - > prefix ;
stored_prefix = SliceToTestKey ( iter - > key ( ) ) . prefix ;
ASSERT_TRUE ( iter - > Valid ( ) ) ;
ASSERT_TRUE ( iter - > Valid ( ) ) ;
ASSERT_NE ( it , whole_map . end ( ) ) ;
ASSERT_NE ( it , whole_map . end ( ) ) ;
ASSERT_EQ ( iter - > value ( ) , it - > second ) ;
ASSERT_EQ ( iter - > value ( ) , it - > second ) ;
if ( FLAGS_enable_print ) {
if ( FLAGS_enable_print ) {
std : : cout < < " iter: " < < SliceToTestKey ( iter - > key ( ) ) - > prefix
std : : cout < < " iter: " < < SliceToTestKey ( iter - > key ( ) ) . prefix
< < SliceToTestKey ( iter - > key ( ) ) - > sorted
< < SliceToTestKey ( iter - > key ( ) ) . sorted
< < " | map: " < < it - > first . prefix < < it - > first . sorted
< < " | map: " < < it - > first . prefix < < it - > first . sorted
< < " | " < < iter - > value ( ) . ToString ( ) < < " " < < it - > second
< < " | " < < iter - > value ( ) . ToString ( ) < < " " < < it - > second
< < std : : endl ;
< < std : : endl ;
@ -810,7 +829,8 @@ TEST_F(PrefixTest, PrefixSeekModePrev3) {
options . write_buffer_size = 1024 * 1024 ;
options . write_buffer_size = 1024 * 1024 ;
std : : string v14 ( " v14 " ) ;
std : : string v14 ( " v14 " ) ;
TestKey upper_bound_key = TestKey ( 1 , 5 ) ;
TestKey upper_bound_key = TestKey ( 1 , 5 ) ;
Slice upper_bound = TestKeyToSlice ( upper_bound_key ) ;
std : : string s ;
Slice upper_bound = TestKeyToSlice ( s , upper_bound_key ) ;
{
{
DestroyDB ( kDbName , Options ( ) ) ;
DestroyDB ( kDbName , Options ( ) ) ;