@ -32,10 +32,15 @@ namespace rocksdb {
class LevelIterator : public InternalIterator {
class LevelIterator : public InternalIterator {
public :
public :
LevelIterator ( const ColumnFamilyData * const cfd ,
LevelIterator ( const ColumnFamilyData * const cfd ,
const ReadOptions & read_options ,
const ReadOptions & read_options ,
const std : : vector < FileMetaData * > & files )
const std : : vector < FileMetaData * > & files )
: cfd_ ( cfd ) , read_options_ ( read_options ) , files_ ( files ) , valid_ ( false ) ,
: cfd_ ( cfd ) ,
file_index_ ( std : : numeric_limits < uint32_t > : : max ( ) ) { }
read_options_ ( read_options ) ,
files_ ( files ) ,
valid_ ( false ) ,
file_index_ ( std : : numeric_limits < uint32_t > : : max ( ) ) ,
file_iter_ ( nullptr ) ,
pinned_iters_mgr_ ( nullptr ) { }
void SetFileIndex ( uint32_t file_index ) {
void SetFileIndex ( uint32_t file_index ) {
assert ( file_index < files_ . size ( ) ) ;
assert ( file_index < files_ . size ( ) ) ;
@ -47,10 +52,20 @@ class LevelIterator : public InternalIterator {
}
}
void Reset ( ) {
void Reset ( ) {
assert ( file_index_ < files_ . size ( ) ) ;
assert ( file_index_ < files_ . size ( ) ) ;
file_iter_ . reset ( cfd_ - > table_cache ( ) - > NewIterator (
// Reset current pointer
if ( pinned_iters_mgr_ & & pinned_iters_mgr_ - > PinningEnabled ( ) ) {
pinned_iters_mgr_ - > PinIterator ( file_iter_ ) ;
} else {
delete file_iter_ ;
}
file_iter_ = cfd_ - > table_cache ( ) - > NewIterator (
read_options_ , * ( cfd_ - > soptions ( ) ) , cfd_ - > internal_comparator ( ) ,
read_options_ , * ( cfd_ - > soptions ( ) ) , cfd_ - > internal_comparator ( ) ,
files_ [ file_index_ ] - > fd , nullptr /* table_reader_ptr */ , nullptr ,
files_ [ file_index_ ] - > fd , nullptr /* table_reader_ptr */ , nullptr ,
false ) ) ;
false ) ;
file_iter_ - > SetPinnedItersMgr ( pinned_iters_mgr_ ) ;
}
}
void SeekToLast ( ) override {
void SeekToLast ( ) override {
status_ = Status : : NotSupported ( " LevelIterator::SeekToLast() " ) ;
status_ = Status : : NotSupported ( " LevelIterator::SeekToLast() " ) ;
@ -105,6 +120,20 @@ class LevelIterator : public InternalIterator {
}
}
return Status : : OK ( ) ;
return Status : : OK ( ) ;
}
}
bool IsKeyPinned ( ) const override {
return pinned_iters_mgr_ & & pinned_iters_mgr_ - > PinningEnabled ( ) & &
file_iter_ - > IsKeyPinned ( ) ;
}
bool IsValuePinned ( ) const override {
return pinned_iters_mgr_ & & pinned_iters_mgr_ - > PinningEnabled ( ) & &
file_iter_ - > IsValuePinned ( ) ;
}
void SetPinnedItersMgr ( PinnedIteratorsManager * pinned_iters_mgr ) override {
pinned_iters_mgr_ = pinned_iters_mgr ;
if ( file_iter_ ) {
file_iter_ - > SetPinnedItersMgr ( pinned_iters_mgr_ ) ;
}
}
private :
private :
const ColumnFamilyData * const cfd_ ;
const ColumnFamilyData * const cfd_ ;
@ -114,7 +143,8 @@ class LevelIterator : public InternalIterator {
bool valid_ ;
bool valid_ ;
uint32_t file_index_ ;
uint32_t file_index_ ;
Status status_ ;
Status status_ ;
std : : unique_ptr < InternalIterator > file_iter_ ;
InternalIterator * file_iter_ ;
PinnedIteratorsManager * pinned_iters_mgr_ ;
} ;
} ;
ForwardIterator : : ForwardIterator ( DBImpl * db , const ReadOptions & read_options ,
ForwardIterator : : ForwardIterator ( DBImpl * db , const ReadOptions & read_options ,
@ -135,7 +165,8 @@ ForwardIterator::ForwardIterator(DBImpl* db, const ReadOptions& read_options,
has_iter_trimmed_for_upper_bound_ ( false ) ,
has_iter_trimmed_for_upper_bound_ ( false ) ,
current_over_upper_bound_ ( false ) ,
current_over_upper_bound_ ( false ) ,
is_prev_set_ ( false ) ,
is_prev_set_ ( false ) ,
is_prev_inclusive_ ( false ) {
is_prev_inclusive_ ( false ) ,
pinned_iters_mgr_ ( nullptr ) {
if ( sv_ ) {
if ( sv_ ) {
RebuildIterators ( false ) ;
RebuildIterators ( false ) ;
}
}
@ -145,6 +176,13 @@ ForwardIterator::~ForwardIterator() {
Cleanup ( true ) ;
Cleanup ( true ) ;
}
}
namespace {
// Used in PinnedIteratorsManager to release pinned SuperVersion
static void ReleaseSuperVersionFunc ( void * sv ) {
delete reinterpret_cast < SuperVersion * > ( sv ) ;
}
} // namespace
void ForwardIterator : : SVCleanup ( ) {
void ForwardIterator : : SVCleanup ( ) {
if ( sv_ ! = nullptr & & sv_ - > Unref ( ) ) {
if ( sv_ ! = nullptr & & sv_ - > Unref ( ) ) {
// Job id == 0 means that this is not our background process, but rather
// Job id == 0 means that this is not our background process, but rather
@ -157,7 +195,11 @@ void ForwardIterator::SVCleanup() {
db_ - > ScheduleBgLogWriterClose ( & job_context ) ;
db_ - > ScheduleBgLogWriterClose ( & job_context ) ;
}
}
db_ - > mutex_ . Unlock ( ) ;
db_ - > mutex_ . Unlock ( ) ;
delete sv_ ;
if ( pinned_iters_mgr_ & & pinned_iters_mgr_ - > PinningEnabled ( ) ) {
pinned_iters_mgr_ - > PinPtr ( sv_ , & ReleaseSuperVersionFunc ) ;
} else {
delete sv_ ;
}
if ( job_context . HaveSomethingToDelete ( ) ) {
if ( job_context . HaveSomethingToDelete ( ) ) {
db_ - > PurgeObsoleteFiles (
db_ - > PurgeObsoleteFiles (
job_context , read_options_ . background_purge_on_iterator_cleanup ) ;
job_context , read_options_ . background_purge_on_iterator_cleanup ) ;
@ -168,18 +210,21 @@ void ForwardIterator::SVCleanup() {
void ForwardIterator : : Cleanup ( bool release_sv ) {
void ForwardIterator : : Cleanup ( bool release_sv ) {
if ( mutable_iter_ ! = nullptr ) {
if ( mutable_iter_ ! = nullptr ) {
mutable_iter_ - > ~ InternalIterator ( ) ;
DeleteIterator ( mutable_iter_ , true /* is_arena */ ) ;
}
}
for ( auto * m : imm_iters_ ) {
for ( auto * m : imm_iters_ ) {
m - > ~ InternalIterator ( ) ;
DeleteIterator ( m , true /* is_arena */ ) ;
}
}
imm_iters_ . clear ( ) ;
imm_iters_ . clear ( ) ;
for ( auto * f : l0_iters_ ) {
for ( auto * f : l0_iters_ ) {
delete f ;
DeleteIterator ( f ) ;
}
}
l0_iters_ . clear ( ) ;
l0_iters_ . clear ( ) ;
for ( auto * l : level_iters_ ) {
for ( auto * l : level_iters_ ) {
delete l ;
DeleteIterator ( l ) ;
}
}
level_iters_ . clear ( ) ;
level_iters_ . clear ( ) ;
@ -280,7 +325,7 @@ void ForwardIterator::SeekInternal(const Slice& internal_key,
l0 [ i ] - > largest . user_key ( ) ) > 0 ) {
l0 [ i ] - > largest . user_key ( ) ) > 0 ) {
if ( read_options_ . iterate_upper_bound ! = nullptr ) {
if ( read_options_ . iterate_upper_bound ! = nullptr ) {
has_iter_trimmed_for_upper_bound_ = true ;
has_iter_trimmed_for_upper_bound_ = true ;
delete l0_iters_ [ i ] ;
DeleteIterator ( l0_iters_ [ i ] ) ;
l0_iters_ [ i ] = nullptr ;
l0_iters_ [ i ] = nullptr ;
}
}
continue ;
continue ;
@ -295,7 +340,7 @@ void ForwardIterator::SeekInternal(const Slice& internal_key,
immutable_min_heap_ . push ( l0_iters_ [ i ] ) ;
immutable_min_heap_ . push ( l0_iters_ [ i ] ) ;
} else {
} else {
has_iter_trimmed_for_upper_bound_ = true ;
has_iter_trimmed_for_upper_bound_ = true ;
delete l0_iters_ [ i ] ;
DeleteIterator ( l0_iters_ [ i ] ) ;
l0_iters_ [ i ] = nullptr ;
l0_iters_ [ i ] = nullptr ;
}
}
}
}
@ -372,7 +417,7 @@ void ForwardIterator::SeekInternal(const Slice& internal_key,
} else {
} else {
// Nothing in this level is interesting. Remove.
// Nothing in this level is interesting. Remove.
has_iter_trimmed_for_upper_bound_ = true ;
has_iter_trimmed_for_upper_bound_ = true ;
delete level_iters_ [ level - 1 ] ;
DeleteIterator ( level_iters_ [ level - 1 ] ) ;
level_iters_ [ level - 1 ] = nullptr ;
level_iters_ [ level - 1 ] = nullptr ;
}
}
}
}
@ -485,6 +530,50 @@ Status ForwardIterator::GetProperty(std::string prop_name, std::string* prop) {
return Status : : InvalidArgument ( ) ;
return Status : : InvalidArgument ( ) ;
}
}
void ForwardIterator : : SetPinnedItersMgr (
PinnedIteratorsManager * pinned_iters_mgr ) {
pinned_iters_mgr_ = pinned_iters_mgr ;
UpdateChildrenPinnedItersMgr ( ) ;
}
void ForwardIterator : : UpdateChildrenPinnedItersMgr ( ) {
// Set PinnedIteratorsManager for mutable memtable iterator.
if ( mutable_iter_ ) {
mutable_iter_ - > SetPinnedItersMgr ( pinned_iters_mgr_ ) ;
}
// Set PinnedIteratorsManager for immutable memtable iterators.
for ( InternalIterator * child_iter : imm_iters_ ) {
if ( child_iter ) {
child_iter - > SetPinnedItersMgr ( pinned_iters_mgr_ ) ;
}
}
// Set PinnedIteratorsManager for L0 files iterators.
for ( InternalIterator * child_iter : l0_iters_ ) {
if ( child_iter ) {
child_iter - > SetPinnedItersMgr ( pinned_iters_mgr_ ) ;
}
}
// Set PinnedIteratorsManager for L1+ levels iterators.
for ( LevelIterator * child_iter : level_iters_ ) {
if ( child_iter ) {
child_iter - > SetPinnedItersMgr ( pinned_iters_mgr_ ) ;
}
}
}
bool ForwardIterator : : IsKeyPinned ( ) const {
return pinned_iters_mgr_ & & pinned_iters_mgr_ - > PinningEnabled ( ) & &
current_ - > IsKeyPinned ( ) ;
}
bool ForwardIterator : : IsValuePinned ( ) const {
return pinned_iters_mgr_ & & pinned_iters_mgr_ - > PinningEnabled ( ) & &
current_ - > IsValuePinned ( ) ;
}
void ForwardIterator : : RebuildIterators ( bool refresh_sv ) {
void ForwardIterator : : RebuildIterators ( bool refresh_sv ) {
// Clean up
// Clean up
Cleanup ( refresh_sv ) ;
Cleanup ( refresh_sv ) ;
@ -513,6 +602,8 @@ void ForwardIterator::RebuildIterators(bool refresh_sv) {
BuildLevelIterators ( vstorage ) ;
BuildLevelIterators ( vstorage ) ;
current_ = nullptr ;
current_ = nullptr ;
is_prev_set_ = false ;
is_prev_set_ = false ;
UpdateChildrenPinnedItersMgr ( ) ;
}
}
void ForwardIterator : : RenewIterators ( ) {
void ForwardIterator : : RenewIterators ( ) {
@ -521,10 +612,10 @@ void ForwardIterator::RenewIterators() {
svnew = cfd_ - > GetReferencedSuperVersion ( & ( db_ - > mutex_ ) ) ;
svnew = cfd_ - > GetReferencedSuperVersion ( & ( db_ - > mutex_ ) ) ;
if ( mutable_iter_ ! = nullptr ) {
if ( mutable_iter_ ! = nullptr ) {
mutable_iter_ - > ~ InternalIterator ( ) ;
DeleteIterator ( mutable_iter_ , true /* is_arena */ ) ;
}
}
for ( auto * m : imm_iters_ ) {
for ( auto * m : imm_iters_ ) {
m - > ~ InternalIterator ( ) ;
DeleteIterator ( m , true /* is_arena */ ) ;
}
}
imm_iters_ . clear ( ) ;
imm_iters_ . clear ( ) ;
@ -565,13 +656,13 @@ void ForwardIterator::RenewIterators() {
}
}
for ( auto * f : l0_iters_ ) {
for ( auto * f : l0_iters_ ) {
delete f ;
DeleteIterator ( f ) ;
}
}
l0_iters_ . clear ( ) ;
l0_iters_ . clear ( ) ;
l0_iters_ = l0_iters_new ;
l0_iters_ = l0_iters_new ;
for ( auto * l : level_iters_ ) {
for ( auto * l : level_iters_ ) {
delete l ;
DeleteIterator ( l ) ;
}
}
level_iters_ . clear ( ) ;
level_iters_ . clear ( ) ;
BuildLevelIterators ( vstorage_new ) ;
BuildLevelIterators ( vstorage_new ) ;
@ -579,6 +670,8 @@ void ForwardIterator::RenewIterators() {
is_prev_set_ = false ;
is_prev_set_ = false ;
SVCleanup ( ) ;
SVCleanup ( ) ;
sv_ = svnew ;
sv_ = svnew ;
UpdateChildrenPinnedItersMgr ( ) ;
}
}
void ForwardIterator : : BuildLevelIterators ( const VersionStorageInfo * vstorage ) {
void ForwardIterator : : BuildLevelIterators ( const VersionStorageInfo * vstorage ) {
@ -608,10 +701,11 @@ void ForwardIterator::ResetIncompleteIterators() {
if ( ! l0_iters_ [ i ] | | ! l0_iters_ [ i ] - > status ( ) . IsIncomplete ( ) ) {
if ( ! l0_iters_ [ i ] | | ! l0_iters_ [ i ] - > status ( ) . IsIncomplete ( ) ) {
continue ;
continue ;
}
}
delete l0_iters_ [ i ] ;
DeleteIterator ( l0_iters_ [ i ] ) ;
l0_iters_ [ i ] = cfd_ - > table_cache ( ) - > NewIterator (
l0_iters_ [ i ] = cfd_ - > table_cache ( ) - > NewIterator (
read_options_ , * cfd_ - > soptions ( ) , cfd_ - > internal_comparator ( ) ,
read_options_ , * cfd_ - > soptions ( ) , cfd_ - > internal_comparator ( ) ,
l0_files [ i ] - > fd ) ;
l0_files [ i ] - > fd ) ;
l0_iters_ [ i ] - > SetPinnedItersMgr ( pinned_iters_mgr_ ) ;
}
}
for ( auto * level_iter : level_iters_ ) {
for ( auto * level_iter : level_iters_ ) {
@ -700,7 +794,7 @@ void ForwardIterator::DeleteCurrentIter() {
}
}
if ( l0_iters_ [ i ] = = current_ ) {
if ( l0_iters_ [ i ] = = current_ ) {
has_iter_trimmed_for_upper_bound_ = true ;
has_iter_trimmed_for_upper_bound_ = true ;
delete l0_iters_ [ i ] ;
DeleteIterator ( l0_iters_ [ i ] ) ;
l0_iters_ [ i ] = nullptr ;
l0_iters_ [ i ] = nullptr ;
return ;
return ;
}
}
@ -712,7 +806,7 @@ void ForwardIterator::DeleteCurrentIter() {
}
}
if ( level_iters_ [ level - 1 ] = = current_ ) {
if ( level_iters_ [ level - 1 ] = = current_ ) {
has_iter_trimmed_for_upper_bound_ = true ;
has_iter_trimmed_for_upper_bound_ = true ;
delete level_iters_ [ level - 1 ] ;
DeleteIterator ( level_iters_ [ level - 1 ] ) ;
level_iters_ [ level - 1 ] = nullptr ;
level_iters_ [ level - 1 ] = nullptr ;
}
}
}
}
@ -776,6 +870,22 @@ uint32_t ForwardIterator::FindFileInRange(
return right ;
return right ;
}
}
void ForwardIterator : : DeleteIterator ( InternalIterator * iter , bool is_arena ) {
if ( iter = = nullptr ) {
return ;
}
if ( pinned_iters_mgr_ & & pinned_iters_mgr_ - > PinningEnabled ( ) ) {
pinned_iters_mgr_ - > PinIterator ( iter , is_arena ) ;
} else {
if ( is_arena ) {
iter - > ~ InternalIterator ( ) ;
} else {
delete iter ;
}
}
}
} // namespace rocksdb
} // namespace rocksdb
# endif // ROCKSDB_LITE
# endif // ROCKSDB_LITE