@ -12,7 +12,9 @@
# include "db/table_properties_collector.h"
# include "db/table_properties_collector.h"
# include "rocksdb/table_properties.h"
# include "rocksdb/table_properties.h"
# include "rocksdb/table.h"
# include "rocksdb/table.h"
# include "rocksdb/plain_table_factory.h"
# include "table/block_based_table_factory.h"
# include "table/block_based_table_factory.h"
# include "table/meta_blocks.h"
# include "util/coding.h"
# include "util/coding.h"
# include "util/testharness.h"
# include "util/testharness.h"
# include "util/testutil.h"
# include "util/testutil.h"
@ -20,8 +22,6 @@
namespace rocksdb {
namespace rocksdb {
class TablePropertiesTest {
class TablePropertiesTest {
private :
unique_ptr < TableReader > table_reader_ ;
} ;
} ;
// TODO(kailiu) the following classes should be moved to some more general
// TODO(kailiu) the following classes should be moved to some more general
@ -93,22 +93,6 @@ void MakeBuilder(
options . compression ) ) ;
options . compression ) ) ;
}
}
void OpenTable (
const Options & options ,
const std : : string & contents ,
std : : unique_ptr < TableReader > * table_reader ) {
std : : unique_ptr < RandomAccessFile > file ( new FakeRandomeAccessFile ( contents ) ) ;
auto s = options . table_factory - > GetTableReader (
options ,
EnvOptions ( ) ,
std : : move ( file ) ,
contents . size ( ) ,
table_reader
) ;
ASSERT_OK ( s ) ;
}
// Collects keys that starts with "A" in a table.
// Collects keys that starts with "A" in a table.
class RegularKeysStartWithA : public TablePropertiesCollector {
class RegularKeysStartWithA : public TablePropertiesCollector {
public :
public :
@ -141,9 +125,12 @@ class RegularKeysStartWithA: public TablePropertiesCollector {
uint32_t count_ = 0 ;
uint32_t count_ = 0 ;
} ;
} ;
TEST ( TablePropertiesTest , CustomizedTablePropertiesCollector ) {
extern uint64_t kBlockBasedTableMagicNumber ;
Options options ;
extern uint64_t kPlainTableMagicNumber ;
void TestCustomizedTablePropertiesCollector (
uint64_t magic_number ,
bool encode_as_internal ,
const Options & options ) {
// make sure the entries will be inserted with order.
// make sure the entries will be inserted with order.
std : : map < std : : string , std : : string > kvs = {
std : : map < std : : string , std : : string > kvs = {
{ " About " , " val5 " } , // starts with 'A'
{ " About " , " val5 " } , // starts with 'A'
@ -155,18 +142,7 @@ TEST(TablePropertiesTest, CustomizedTablePropertiesCollector) {
{ " Find " , " val6 " } ,
{ " Find " , " val6 " } ,
} ;
} ;
// Test properties collectors with internal keys or regular keys
for ( bool encode_as_internal : { true , false } ) {
// -- Step 1: build table
// -- Step 1: build table
auto collector = new RegularKeysStartWithA ( ) ;
if ( encode_as_internal ) {
options . table_properties_collectors = {
std : : make_shared < UserKeyTablePropertiesCollector > ( collector )
} ;
} else {
options . table_properties_collectors . resize ( 1 ) ;
options . table_properties_collectors [ 0 ] . reset ( collector ) ;
}
std : : unique_ptr < TableBuilder > builder ;
std : : unique_ptr < TableBuilder > builder ;
std : : unique_ptr < FakeWritableFile > writable ;
std : : unique_ptr < FakeWritableFile > writable ;
MakeBuilder ( options , & writable , & builder ) ;
MakeBuilder ( options , & writable , & builder ) ;
@ -181,22 +157,65 @@ TEST(TablePropertiesTest, CustomizedTablePropertiesCollector) {
}
}
ASSERT_OK ( builder - > Finish ( ) ) ;
ASSERT_OK ( builder - > Finish ( ) ) ;
// -- Step 2: Open table
// -- Step 2: Read properties
std : : unique_ptr < TableReader > table_reader ;
FakeRandomeAccessFile readable ( writable - > contents ( ) ) ;
OpenTable ( options , writable - > contents ( ) , & table_reader ) ;
TableProperties props ;
const auto & properties =
Status s = ReadTableProperties (
table_reader - > GetTableProperties ( ) . user_collected_properties ;
& readable ,
writable - > contents ( ) . size ( ) ,
magic_number ,
Env : : Default ( ) ,
nullptr ,
& props
) ;
ASSERT_OK ( s ) ;
auto user_collected = props . user_collected_properties ;
ASSERT_EQ ( " Rocksdb " , properties . at ( " TablePropertiesTest " ) ) ;
ASSERT_EQ ( " Rocksdb " , user_collected . at ( " TablePropertiesTest " ) ) ;
uint32_t starts_with_A = 0 ;
uint32_t starts_with_A = 0 ;
Slice key ( properties . at ( " Count " ) ) ;
Slice key ( user_collected . at ( " Count " ) ) ;
ASSERT_TRUE ( GetVarint32 ( & key , & starts_with_A ) ) ;
ASSERT_TRUE ( GetVarint32 ( & key , & starts_with_A ) ) ;
ASSERT_EQ ( 3u , starts_with_A ) ;
ASSERT_EQ ( 3u , starts_with_A ) ;
}
}
TEST ( TablePropertiesTest , CustomizedTablePropertiesCollector ) {
// Test properties collectors with internal keys or regular keys
// for block based table
for ( bool encode_as_internal : { true , false } ) {
Options options ;
auto collector = new RegularKeysStartWithA ( ) ;
if ( encode_as_internal ) {
options . table_properties_collectors = {
std : : make_shared < UserKeyTablePropertiesCollector > ( collector )
} ;
} else {
options . table_properties_collectors . resize ( 1 ) ;
options . table_properties_collectors [ 0 ] . reset ( collector ) ;
}
TestCustomizedTablePropertiesCollector (
kBlockBasedTableMagicNumber ,
encode_as_internal ,
options
) ;
}
}
TEST ( TablePropertiesTest , InternalKeyPropertiesCollector ) {
// test plain table
Options options ;
options . table_properties_collectors . push_back (
std : : make_shared < RegularKeysStartWithA > ( )
) ;
options . table_factory = std : : make_shared < PlainTableFactory > ( 8 , 8 , 0 ) ;
TestCustomizedTablePropertiesCollector (
kPlainTableMagicNumber , true , options
) ;
}
void TestInternalKeyPropertiesCollector (
uint64_t magic_number ,
bool sanitized ,
std : : shared_ptr < TableFactory > table_factory ) {
InternalKey keys [ ] = {
InternalKey keys [ ] = {
InternalKey ( " A " , 0 , ValueType : : kTypeValue ) ,
InternalKey ( " A " , 0 , ValueType : : kTypeValue ) ,
InternalKey ( " B " , 0 , ValueType : : kTypeValue ) ,
InternalKey ( " B " , 0 , ValueType : : kTypeValue ) ,
@ -207,10 +226,10 @@ TEST(TablePropertiesTest, InternalKeyPropertiesCollector) {
InternalKey ( " Z " , 0 , ValueType : : kTypeDeletion ) ,
InternalKey ( " Z " , 0 , ValueType : : kTypeDeletion ) ,
} ;
} ;
for ( bool sanitized : { false , true } ) {
std : : unique_ptr < TableBuilder > builder ;
std : : unique_ptr < TableBuilder > builder ;
std : : unique_ptr < FakeWritableFile > writable ;
std : : unique_ptr < FakeWritableFile > writable ;
Options options ;
Options options ;
options . table_factory = table_factory ;
if ( sanitized ) {
if ( sanitized ) {
options . table_properties_collectors = {
options . table_properties_collectors = {
std : : make_shared < RegularKeysStartWithA > ( )
std : : make_shared < RegularKeysStartWithA > ( )
@ -241,21 +260,46 @@ TEST(TablePropertiesTest, InternalKeyPropertiesCollector) {
ASSERT_OK ( builder - > Finish ( ) ) ;
ASSERT_OK ( builder - > Finish ( ) ) ;
std : : unique_ptr < TableReader > table_reader ;
FakeRandomeAccessFile readable ( writable - > contents ( ) ) ;
OpenTable ( options , writable - > contents ( ) , & table_reader ) ;
TableProperties props ;
const auto & properties =
Status s = ReadTableProperties (
table_reader - > GetTableProperties ( ) . user_collected_properties ;
& readable ,
writable - > contents ( ) . size ( ) ,
magic_number ,
Env : : Default ( ) ,
nullptr ,
& props
) ;
ASSERT_OK ( s ) ;
uint64_t deleted = GetDeletedKeys ( properties ) ;
auto user_collected = props . user_collected_properties ;
uint64_t deleted = GetDeletedKeys ( user_collected ) ;
ASSERT_EQ ( 4u , deleted ) ;
ASSERT_EQ ( 4u , deleted ) ;
if ( sanitized ) {
if ( sanitized ) {
uint32_t starts_with_A = 0 ;
uint32_t starts_with_A = 0 ;
Slice key ( properties . at ( " Count " ) ) ;
Slice key ( user_collected . at ( " Count " ) ) ;
ASSERT_TRUE ( GetVarint32 ( & key , & starts_with_A ) ) ;
ASSERT_TRUE ( GetVarint32 ( & key , & starts_with_A ) ) ;
ASSERT_EQ ( 1u , starts_with_A ) ;
ASSERT_EQ ( 1u , starts_with_A ) ;
}
}
}
}
TEST ( TablePropertiesTest , InternalKeyPropertiesCollector ) {
TestInternalKeyPropertiesCollector (
kBlockBasedTableMagicNumber ,
true /* sanitize */ ,
std : : make_shared < BlockBasedTableFactory > ( )
) ;
TestInternalKeyPropertiesCollector (
kBlockBasedTableMagicNumber ,
true /* not sanitize */ ,
std : : make_shared < BlockBasedTableFactory > ( )
) ;
TestInternalKeyPropertiesCollector (
kPlainTableMagicNumber ,
false /* not sanitize */ ,
std : : make_shared < PlainTableFactory > ( 8 , 8 , 0 )
) ;
}
}
} // namespace rocksdb
} // namespace rocksdb