@ -22,11 +22,17 @@ namespace {
class TwoLevelIterator : public Iterator {
public :
explicit TwoLevelIterator ( TwoLevelIteratorState * state ,
Iterator * first_level_iter ) ;
Iterator * first_level_iter ,
bool need_free_iter_and_state ) ;
virtual ~ TwoLevelIterator ( ) {
first_level_iter_ . DeleteIter ( false ) ;
first_level_iter_ . DeleteIter ( ! need_free_iter_and_state_ ) ;
second_level_iter_ . DeleteIter ( false ) ;
if ( need_free_iter_and_state_ ) {
delete state_ ;
} else {
state_ - > ~ TwoLevelIteratorState ( ) ;
}
}
virtual void Seek ( const Slice & target ) override ;
@ -65,9 +71,10 @@ class TwoLevelIterator: public Iterator {
void SetSecondLevelIterator ( Iterator * iter ) ;
void InitDataBlock ( ) ;
std : : unique_ptr < TwoLevelIteratorState > state_ ;
TwoLevelIteratorState * state_ ;
IteratorWrapper first_level_iter_ ;
IteratorWrapper second_level_iter_ ; // May be nullptr
bool need_free_iter_and_state_ ;
Status status_ ;
// If second_level_iter is non-nullptr, then "data_block_handle_" holds the
// "index_value" passed to block_function_ to create the second_level_iter.
@ -75,8 +82,11 @@ class TwoLevelIterator: public Iterator {
} ;
TwoLevelIterator : : TwoLevelIterator ( TwoLevelIteratorState * state ,
Iterator * first_level_iter )
: state_ ( state ) , first_level_iter_ ( first_level_iter ) { }
Iterator * first_level_iter ,
bool need_free_iter_and_state )
: state_ ( state ) ,
first_level_iter_ ( first_level_iter ) ,
need_free_iter_and_state_ ( need_free_iter_and_state ) { }
void TwoLevelIterator : : Seek ( const Slice & target ) {
if ( state_ - > check_prefix_may_match & &
@ -186,12 +196,15 @@ void TwoLevelIterator::InitDataBlock() {
} // namespace
Iterator * NewTwoLevelIterator ( TwoLevelIteratorState * state ,
Iterator * first_level_iter , Arena * arena ) {
Iterator * first_level_iter , Arena * arena ,
bool need_free_iter_and_state ) {
if ( arena = = nullptr ) {
return new TwoLevelIterator ( state , first_level_iter ) ;
return new TwoLevelIterator ( state , first_level_iter ,
need_free_iter_and_state ) ;
} else {
auto mem = arena - > AllocateAligned ( sizeof ( TwoLevelIterator ) ) ;
return new ( mem ) TwoLevelIterator ( state , first_level_iter ) ;
return new ( mem )
TwoLevelIterator ( state , first_level_iter , need_free_iter_and_state ) ;
}
}