@ -92,6 +92,7 @@ class FilePicker {
const InternalKeyComparator * internal_comparator )
const InternalKeyComparator * internal_comparator )
: num_levels_ ( num_levels ) ,
: num_levels_ ( num_levels ) ,
curr_level_ ( static_cast < unsigned int > ( - 1 ) ) ,
curr_level_ ( static_cast < unsigned int > ( - 1 ) ) ,
returned_file_level_ ( static_cast < unsigned int > ( - 1 ) ) ,
hit_file_level_ ( static_cast < unsigned int > ( - 1 ) ) ,
hit_file_level_ ( static_cast < unsigned int > ( - 1 ) ) ,
search_left_bound_ ( 0 ) ,
search_left_bound_ ( 0 ) ,
search_right_bound_ ( FileIndexer : : kLevelMaxIndex ) ,
search_right_bound_ ( FileIndexer : : kLevelMaxIndex ) ,
@ -118,6 +119,8 @@ class FilePicker {
}
}
}
}
int GetCurrentLevel ( ) { return returned_file_level_ ; }
FdWithKeyRange * GetNextFile ( ) {
FdWithKeyRange * GetNextFile ( ) {
while ( ! search_ended_ ) { // Loops over different levels.
while ( ! search_ended_ ) { // Loops over different levels.
while ( curr_index_in_curr_level_ < curr_file_level_ - > num_files ) {
while ( curr_index_in_curr_level_ < curr_file_level_ - > num_files ) {
@ -190,6 +193,7 @@ class FilePicker {
}
}
prev_file_ = f ;
prev_file_ = f ;
# endif
# endif
returned_file_level_ = curr_level_ ;
if ( curr_level_ > 0 & & cmp_largest < 0 ) {
if ( curr_level_ > 0 & & cmp_largest < 0 ) {
// No more files to search in this level.
// No more files to search in this level.
search_ended_ = ! PrepareNextLevel ( ) ;
search_ended_ = ! PrepareNextLevel ( ) ;
@ -216,6 +220,7 @@ class FilePicker {
private :
private :
unsigned int num_levels_ ;
unsigned int num_levels_ ;
unsigned int curr_level_ ;
unsigned int curr_level_ ;
unsigned int returned_file_level_ ;
unsigned int hit_file_level_ ;
unsigned int hit_file_level_ ;
int32_t search_left_bound_ ;
int32_t search_left_bound_ ;
int32_t search_right_bound_ ;
int32_t search_right_bound_ ;
@ -322,7 +327,7 @@ Version::~Version() {
f - > refs - - ;
f - > refs - - ;
if ( f - > refs < = 0 ) {
if ( f - > refs < = 0 ) {
if ( f - > table_reader_handle ) {
if ( f - > table_reader_handle ) {
cfd_ - > table_cache ( ) - > ReleaseHandle ( f - > table_reader_handle ) ;
cfd_ - > table_cache ( ) - > EraseHandle ( f - > fd , f - > table_reader_handle ) ;
f - > table_reader_handle = nullptr ;
f - > table_reader_handle = nullptr ;
}
}
vset_ - > obsolete_files_ . push_back ( f ) ;
vset_ - > obsolete_files_ . push_back ( f ) ;
@ -486,7 +491,7 @@ class LevelFileIteratorState : public TwoLevelIteratorState {
const EnvOptions & env_options ,
const EnvOptions & env_options ,
const InternalKeyComparator & icomparator ,
const InternalKeyComparator & icomparator ,
HistogramImpl * file_read_hist , bool for_compaction ,
HistogramImpl * file_read_hist , bool for_compaction ,
bool prefix_enabled , bool skip_filters )
bool prefix_enabled , bool skip_filters , int level )
: TwoLevelIteratorState ( prefix_enabled ) ,
: TwoLevelIteratorState ( prefix_enabled ) ,
table_cache_ ( table_cache ) ,
table_cache_ ( table_cache ) ,
read_options_ ( read_options ) ,
read_options_ ( read_options ) ,
@ -494,7 +499,8 @@ class LevelFileIteratorState : public TwoLevelIteratorState {
icomparator_ ( icomparator ) ,
icomparator_ ( icomparator ) ,
file_read_hist_ ( file_read_hist ) ,
file_read_hist_ ( file_read_hist ) ,
for_compaction_ ( for_compaction ) ,
for_compaction_ ( for_compaction ) ,
skip_filters_ ( skip_filters ) { }
skip_filters_ ( skip_filters ) ,
level_ ( level ) { }
InternalIterator * NewSecondaryIterator ( const Slice & meta_handle ) override {
InternalIterator * NewSecondaryIterator ( const Slice & meta_handle ) override {
if ( meta_handle . size ( ) ! = sizeof ( FileDescriptor ) ) {
if ( meta_handle . size ( ) ! = sizeof ( FileDescriptor ) ) {
@ -506,7 +512,7 @@ class LevelFileIteratorState : public TwoLevelIteratorState {
return table_cache_ - > NewIterator (
return table_cache_ - > NewIterator (
read_options_ , env_options_ , icomparator_ , * fd ,
read_options_ , env_options_ , icomparator_ , * fd ,
nullptr /* don't need reference to table*/ , file_read_hist_ ,
nullptr /* don't need reference to table*/ , file_read_hist_ ,
for_compaction_ , nullptr /* arena */ , skip_filters_ ) ;
for_compaction_ , nullptr /* arena */ , skip_filters_ , level_ ) ;
}
}
}
}
@ -522,6 +528,7 @@ class LevelFileIteratorState : public TwoLevelIteratorState {
HistogramImpl * file_read_hist_ ;
HistogramImpl * file_read_hist_ ;
bool for_compaction_ ;
bool for_compaction_ ;
bool skip_filters_ ;
bool skip_filters_ ;
int level_ ;
} ;
} ;
// A wrapper of version builder which references the current version in
// A wrapper of version builder which references the current version in
@ -789,7 +796,8 @@ void Version::AddIterators(const ReadOptions& read_options,
const auto & file = storage_info_ . LevelFilesBrief ( 0 ) . files [ i ] ;
const auto & file = storage_info_ . LevelFilesBrief ( 0 ) . files [ i ] ;
merge_iter_builder - > AddIterator ( cfd_ - > table_cache ( ) - > NewIterator (
merge_iter_builder - > AddIterator ( cfd_ - > table_cache ( ) - > NewIterator (
read_options , soptions , cfd_ - > internal_comparator ( ) , file . fd , nullptr ,
read_options , soptions , cfd_ - > internal_comparator ( ) , file . fd , nullptr ,
cfd_ - > internal_stats ( ) - > GetFileReadHist ( 0 ) , false , arena ) ) ;
cfd_ - > internal_stats ( ) - > GetFileReadHist ( 0 ) , false , arena ,
false /* skip_filters */ , 0 /* level */ ) ) ;
}
}
// For levels > 0, we can use a concatenating iterator that sequentially
// For levels > 0, we can use a concatenating iterator that sequentially
@ -804,7 +812,7 @@ void Version::AddIterators(const ReadOptions& read_options,
cfd_ - > internal_stats ( ) - > GetFileReadHist ( level ) ,
cfd_ - > internal_stats ( ) - > GetFileReadHist ( level ) ,
false /* for_compaction */ ,
false /* for_compaction */ ,
cfd_ - > ioptions ( ) - > prefix_extractor ! = nullptr ,
cfd_ - > ioptions ( ) - > prefix_extractor ! = nullptr ,
IsFilterSkipped ( level ) ) ;
IsFilterSkipped ( level ) , level ) ;
mem = arena - > AllocateAligned ( sizeof ( LevelFileNumIterator ) ) ;
mem = arena - > AllocateAligned ( sizeof ( LevelFileNumIterator ) ) ;
auto * first_level_iter = new ( mem ) LevelFileNumIterator (
auto * first_level_iter = new ( mem ) LevelFileNumIterator (
cfd_ - > internal_comparator ( ) , & storage_info_ . LevelFilesBrief ( level ) ) ;
cfd_ - > internal_comparator ( ) , & storage_info_ . LevelFilesBrief ( level ) ) ;
@ -909,7 +917,8 @@ void Version::Get(const ReadOptions& read_options, const LookupKey& k,
read_options , * internal_comparator ( ) , f - > fd , ikey , & get_context ,
read_options , * internal_comparator ( ) , f - > fd , ikey , & get_context ,
cfd_ - > internal_stats ( ) - > GetFileReadHist ( fp . GetHitFileLevel ( ) ) ,
cfd_ - > internal_stats ( ) - > GetFileReadHist ( fp . GetHitFileLevel ( ) ) ,
IsFilterSkipped ( static_cast < int > ( fp . GetHitFileLevel ( ) ) ,
IsFilterSkipped ( static_cast < int > ( fp . GetHitFileLevel ( ) ) ,
fp . IsHitFileLastInLevel ( ) ) ) ;
fp . IsHitFileLastInLevel ( ) ) ,
fp . GetCurrentLevel ( ) ) ;
// TODO: examine the behavior for corrupted key
// TODO: examine the behavior for corrupted key
if ( ! status - > ok ( ) ) {
if ( ! status - > ok ( ) ) {
return ;
return ;
@ -2062,9 +2071,16 @@ VersionSet::VersionSet(const std::string& dbname, const DBOptions* db_options,
env_options_ ( storage_options ) ,
env_options_ ( storage_options ) ,
env_options_compactions_ ( env_options_ ) { }
env_options_compactions_ ( env_options_ ) { }
void CloseTables ( void * ptr , size_t ) {
TableReader * table_reader = reinterpret_cast < TableReader * > ( ptr ) ;
table_reader - > Close ( ) ;
}
VersionSet : : ~ VersionSet ( ) {
VersionSet : : ~ VersionSet ( ) {
// we need to delete column_family_set_ because its destructor depends on
// we need to delete column_family_set_ because its destructor depends on
// VersionSet
// VersionSet
column_family_set_ - > get_table_cache ( ) - > ApplyToAllCacheEntries ( & CloseTables ,
false ) ;
column_family_set_ . reset ( ) ;
column_family_set_ . reset ( ) ;
for ( auto file : obsolete_files_ ) {
for ( auto file : obsolete_files_ ) {
delete file ;
delete file ;
@ -3275,7 +3291,8 @@ InternalIterator* VersionSet::MakeInputIterator(const Compaction* c) {
read_options , env_options_compactions_ ,
read_options , env_options_compactions_ ,
cfd - > internal_comparator ( ) , flevel - > files [ i ] . fd , nullptr ,
cfd - > internal_comparator ( ) , flevel - > files [ i ] . fd , nullptr ,
nullptr , /* no per level latency histogram*/
nullptr , /* no per level latency histogram*/
true /* for compaction */ ) ;
true /* for_compaction */ , nullptr /* arena */ ,
false /* skip_filters */ , ( int ) which /* level */ ) ;
}
}
} else {
} else {
// Create concatenating iterator for the files from this level
// Create concatenating iterator for the files from this level
@ -3285,7 +3302,7 @@ InternalIterator* VersionSet::MakeInputIterator(const Compaction* c) {
cfd - > internal_comparator ( ) ,
cfd - > internal_comparator ( ) ,
nullptr /* no per level latency histogram */ ,
nullptr /* no per level latency histogram */ ,
true /* for_compaction */ , false /* prefix enabled */ ,
true /* for_compaction */ , false /* prefix enabled */ ,
false /* skip_filters */ ) ,
false /* skip_filters */ , ( int ) which /* level */ ) ,
new LevelFileNumIterator ( cfd - > internal_comparator ( ) ,
new LevelFileNumIterator ( cfd - > internal_comparator ( ) ,
c - > input_levels ( which ) ) ) ;
c - > input_levels ( which ) ) ) ;
}
}