@ -15,6 +15,7 @@
# include "rocksdb/env.h"
# include "rocksdb/env.h"
# include "rocksdb/iterator.h"
# include "rocksdb/iterator.h"
# include "rocksdb/table.h"
# include "rocksdb/table.h"
# include "rocksdb/table_properties.h"
# include "table/block.h"
# include "table/block.h"
# include "table/block_builder.h"
# include "table/block_builder.h"
# include "table/format.h"
# include "table/format.h"
@ -38,52 +39,64 @@ class SstFileReader {
bool has_to ,
bool has_to ,
const std : : string & to_key ) ;
const std : : string & to_key ) ;
Status ReadTableProperties ( TableProperties * table_properties ) ;
uint64_t GetReadNumber ( ) { return read_num_ ; }
uint64_t GetReadNumber ( ) { return read_num_ ; }
private :
private :
Status NewTableReader ( const std : : string & file_path ) ;
std : : string file_name_ ;
std : : string file_name_ ;
uint64_t read_num_ ;
uint64_t read_num_ ;
bool verify_checksum_ ;
bool verify_checksum_ ;
bool output_hex_ ;
bool output_hex_ ;
EnvOptions soptions_ ;
EnvOptions soptions_ ;
Status init_result_ ;
unique_ptr < TableReader > table_reader_ ;
unique_ptr < RandomAccessFile > file_ ;
// table_options_ and internal_comparator_ will also be used in
// ReadSequential internally (specifically, seek-related operations)
Options table_options_ ;
InternalKeyComparator internal_comparator_ ;
} ;
} ;
SstFileReader : : SstFileReader ( const std : : string & file_path ,
SstFileReader : : SstFileReader ( const std : : string & file_path ,
bool verify_checksum ,
bool verify_checksum ,
bool output_hex )
bool output_hex )
: file_name_ ( file_path ) , read_num_ ( 0 ) , verify_checksum_ ( verify_checksum ) ,
: file_name_ ( file_path ) , read_num_ ( 0 ) , verify_checksum_ ( verify_checksum ) ,
output_hex_ ( output_hex ) {
output_hex_ ( output_hex ) , internal_comparator_ ( BytewiseComparator ( ) ) {
std : : cout < < " Process " < < file_path < < " \n " ;
fprintf ( stdout , " Process %s \n " , file_path . c_str ( ) ) ;
init_result_ = NewTableReader ( file_name_ ) ;
}
}
Status SstFileReader : : ReadSequential ( bool print_kv ,
Status SstFileReader : : NewTableReader ( const std : : string & file_path ) {
uint64_t read_num ,
table_options_ . comparator = & internal_comparator_ ;
bool has_from ,
Status s = table_options_ . env - > NewRandomAccessFile ( file_path , & file_ ,
const std : : string & from_key ,
bool has_to ,
const std : : string & to_key )
{
unique_ptr < TableReader > table_reader ;
InternalKeyComparator internal_comparator_ ( BytewiseComparator ( ) ) ;
Options table_options ;
table_options . comparator = & internal_comparator_ ;
unique_ptr < RandomAccessFile > file ;
Status s = table_options . env - > NewRandomAccessFile ( file_name_ , & file ,
soptions_ ) ;
soptions_ ) ;
if ( ! s . ok ( ) ) {
if ( ! s . ok ( ) ) {
return s ;
return s ;
}
}
uint64_t file_size ;
uint64_t file_size ;
table_options . env - > GetFileSize ( file_name_ , & file_size ) ;
table_options_ . env - > GetFileSize ( file_path , & file_size ) ;
unique_ptr < TableFactory > table_factory ;
unique_ptr < TableFactory > table_factory ;
s = table_options . table_factory - > GetTableReader ( table_options , soptions_ ,
s = table_options_ . table_factory - > GetTableReader ( table_options_ , soptions_ ,
std : : move ( file ) , file_size ,
std : : move ( file_ ) , file_size ,
& table_reader ) ;
& table_reader_ ) ;
if ( ! s . ok ( ) ) {
return s ;
return s ;
}
Status SstFileReader : : ReadSequential ( bool print_kv ,
uint64_t read_num ,
bool has_from ,
const std : : string & from_key ,
bool has_to ,
const std : : string & to_key ) {
if ( ! table_reader_ ) {
return init_result_ ;
}
}
Iterator * iter = table_reader - > NewIterator ( ReadOptions ( verify_checksum_ ,
Iterator * iter = table_reader_ - > NewIterator ( ReadOptions ( verify_checksum_ ,
false ) ) ;
false ) ) ;
uint64_t i = 0 ;
uint64_t i = 0 ;
if ( has_from ) {
if ( has_from ) {
@ -113,11 +126,10 @@ Status SstFileReader::ReadSequential(bool print_kv,
}
}
if ( print_kv ) {
if ( print_kv ) {
std : : cout < < ikey . DebugString ( output_hex_ )
fprintf ( stdout , " %s => %s \n " ,
< < " => "
ikey . DebugString ( output_hex_ ) . c_str ( ) ,
< < value . ToString ( output_hex_ ) < < " \n " ;
value . ToString ( output_hex_ ) . c_str ( ) ) ;
}
}
}
}
read_num_ + = i ;
read_num_ + = i ;
@ -127,6 +139,15 @@ Status SstFileReader::ReadSequential(bool print_kv,
return ret ;
return ret ;
}
}
Status SstFileReader : : ReadTableProperties ( TableProperties * table_properties ) {
if ( ! table_reader_ ) {
return init_result_ ;
}
* table_properties = table_reader_ - > GetTableProperties ( ) ;
return init_result_ ;
}
} // namespace rocksdb
} // namespace rocksdb
static void print_help ( ) {
static void print_help ( ) {
@ -137,7 +158,8 @@ static void print_help() {
" [--input_key_hex] "
" [--input_key_hex] "
" [--from=<user_key>] "
" [--from=<user_key>] "
" [--to=<user_key>] "
" [--to=<user_key>] "
" [--read_num=NUM] \n " ) ;
" [--read_num=NUM] "
" [--show_properties] \n " ) ;
}
}
string HexToString ( const string & str ) {
string HexToString ( const string & str ) {
@ -158,7 +180,6 @@ string HexToString(const string& str) {
}
}
int main ( int argc , char * * argv ) {
int main ( int argc , char * * argv ) {
const char * dir_or_file = nullptr ;
const char * dir_or_file = nullptr ;
uint64_t read_num = - 1 ;
uint64_t read_num = - 1 ;
std : : string command ;
std : : string command ;
@ -170,10 +191,10 @@ int main(int argc, char** argv) {
bool input_key_hex = false ;
bool input_key_hex = false ;
bool has_from = false ;
bool has_from = false ;
bool has_to = false ;
bool has_to = false ;
bool show_properties = false ;
std : : string from_key ;
std : : string from_key ;
std : : string to_key ;
std : : string to_key ;
for ( int i = 1 ; i < argc ; i + + )
for ( int i = 1 ; i < argc ; i + + ) {
{
if ( strncmp ( argv [ i ] , " --file= " , 7 ) = = 0 ) {
if ( strncmp ( argv [ i ] , " --file= " , 7 ) = = 0 ) {
dir_or_file = argv [ i ] + 7 ;
dir_or_file = argv [ i ] + 7 ;
} else if ( strcmp ( argv [ i ] , " --output_hex " ) = = 0 ) {
} else if ( strcmp ( argv [ i ] , " --output_hex " ) = = 0 ) {
@ -194,7 +215,9 @@ int main(int argc, char** argv) {
} else if ( strncmp ( argv [ i ] , " --to= " , 5 ) = = 0 ) {
} else if ( strncmp ( argv [ i ] , " --to= " , 5 ) = = 0 ) {
to_key = argv [ i ] + 5 ;
to_key = argv [ i ] + 5 ;
has_to = true ;
has_to = true ;
} else {
} else if ( strcmp ( argv [ i ] , " --show_properties " ) = = 0 ) {
show_properties = true ;
} else {
print_help ( ) ;
print_help ( ) ;
exit ( 1 ) ;
exit ( 1 ) ;
}
}
@ -210,7 +233,7 @@ int main(int argc, char** argv) {
}
}
}
}
if ( dir_or_file = = nullptr ) {
if ( dir_or_file = = nullptr ) {
print_help ( ) ;
print_help ( ) ;
exit ( 1 ) ;
exit ( 1 ) ;
}
}
@ -225,18 +248,19 @@ int main(int argc, char** argv) {
dir = false ;
dir = false ;
}
}
std : : cout < < " from [ " < < rocksdb : : Slice ( from_key ) . ToString ( true )
fprintf ( stdout , " from [%s] to [%s] \n " ,
< < " ] to [ " < < rocksdb : : Slice ( to_key ) . ToString ( true ) < < " ] \n " ;
rocksdb : : Slice ( from_key ) . ToString ( true ) . c_str ( ) ,
rocksdb : : Slice ( to_key ) . ToString ( true ) . c_str ( ) ) ;
uint64_t total_read = 0 ;
uint64_t total_read = 0 ;
for ( size_t i = 0 ; i < filenames . size ( ) ; i + + ) {
for ( size_t i = 0 ; i < filenames . size ( ) ; i + + ) {
std : : string filename = filenames . at ( i ) ;
std : : string filename = filenames . at ( i ) ;
if ( filename . length ( ) < = 4 | |
if ( filename . length ( ) < = 4 | |
filename . rfind ( " .sst " ) ! = filename . length ( ) - 4 ) {
filename . rfind ( " .sst " ) ! = filename . length ( ) - 4 ) {
//ignore
// ignore
continue ;
continue ;
}
}
if ( dir ) {
if ( dir ) {
filename = std : : string ( dir_or_file ) + " / " + filename ;
filename = std : : string ( dir_or_file ) + " / " + filename ;
}
}
rocksdb : : SstFileReader reader ( filename , verify_checksum ,
rocksdb : : SstFileReader reader ( filename , verify_checksum ,
@ -257,5 +281,20 @@ int main(int argc, char** argv) {
break ;
break ;
}
}
}
}
if ( show_properties ) {
rocksdb : : TableProperties table_properties ;
st = reader . ReadTableProperties ( & table_properties ) ;
if ( ! st . ok ( ) ) {
fprintf ( stderr , " %s: %s \n " , filename . c_str ( ) , st . ToString ( ) . c_str ( ) ) ;
} else {
fprintf ( stdout ,
" Table Properties: \n "
" ------------------------------ \n "
" %s " , table_properties . ToString ( " \n " , " : " ) . c_str ( ) ) ;
fprintf ( stdout , " # deleted keys: %lu \n " ,
rocksdb : : GetDeletedKeys (
table_properties . user_collected_properties ) ) ;
}
}
}
}
}
}