@ -7,54 +7,59 @@
namespace leveldb {
namespace leveldb {
Iterator : : Iterator ( ) {
Iterator : : Iterator ( ) {
cleanup_ . function = nullptr ;
cleanup_ . next = nullptr ;
cleanup_head_ . function = nullptr ;
cleanup_head_ . next = nullptr ;
}
}
Iterator : : ~ Iterator ( ) {
Iterator : : ~ Iterator ( ) {
if ( cleanup_ . function ! = nullptr ) {
( * cleanup_ . function ) ( cleanup_ . arg1 , cleanup_ . arg2 ) ;
for ( Cleanup * c = cleanup_ . next ; c ! = nullptr ; ) {
( * c - > function ) ( c - > arg1 , c - > arg2 ) ;
Cleanup * next = c - > next ;
delete c ;
c = next ;
if ( ! cleanup_head_ . IsEmpty ( ) ) {
cleanup_head_ . Run ( ) ;
for ( CleanupNode * node = cleanup_head_ . next ; node ! = nullptr ; ) {
node - > Run ( ) ;
CleanupNode * next_node = node - > next ;
delete node ;
node = next_node ;
}
}
}
}
}
}
void Iterator : : RegisterCleanup ( CleanupFunction func , void * arg1 , void * arg2 ) {
void Iterator : : RegisterCleanup ( CleanupFunction func , void * arg1 , void * arg2 ) {
assert ( func ! = nullptr ) ;
assert ( func ! = nullptr ) ;
Cleanup * c ;
if ( cleanup_ . function = = nullptr ) {
c = & cleanup_ ;
CleanupNode * node ;
if ( cleanup_head_ . IsEmpty ( ) ) {
node = & cleanup_head _ ;
} else {
} else {
c = new Cleanup ;
c - > next = cleanup_ . next ;
cleanup_ . next = c ;
node = new CleanupNode ( ) ;
node - > next = cleanup_head _ . next ;
cleanup_head_ . next = node ;
}
}
c - > function = func ;
c - > arg1 = arg1 ;
c - > arg2 = arg2 ;
node - > function = func ;
node - > arg1 = arg1 ;
node - > arg2 = arg2 ;
}
}
namespace {
namespace {
class EmptyIterator : public Iterator {
class EmptyIterator : public Iterator {
public :
public :
EmptyIterator ( const Status & s ) : status_ ( s ) { }
EmptyIterator ( const Status & s ) : status_ ( s ) { }
virtual bool Valid ( ) const { return false ; }
virtual void Seek ( const Slice & target ) { }
virtual void SeekToFirst ( ) { }
virtual void SeekToLast ( ) { }
virtual void Next ( ) { assert ( false ) ; }
virtual void Prev ( ) { assert ( false ) ; }
Slice key ( ) const { assert ( false ) ; return Slice ( ) ; }
Slice value ( ) const { assert ( false ) ; return Slice ( ) ; }
virtual Status status ( ) const { return status_ ; }
~ EmptyIterator ( ) override = default ;
bool Valid ( ) const override { return false ; }
void Seek ( const Slice & target ) override { }
void SeekToFirst ( ) override { }
void SeekToLast ( ) override { }
void Next ( ) override { assert ( false ) ; }
void Prev ( ) override { assert ( false ) ; }
Slice key ( ) const override { assert ( false ) ; return Slice ( ) ; }
Slice value ( ) const override { assert ( false ) ; return Slice ( ) ; }
Status status ( ) const override { return status_ ; }
private :
private :
Status status_ ;
Status status_ ;
} ;
} ;
} // namespace
} // anonymous namespace
Iterator * NewEmptyIterator ( ) {
Iterator * NewEmptyIterator ( ) {
return new EmptyIterator ( Status : : OK ( ) ) ;
return new EmptyIterator ( Status : : OK ( ) ) ;