@ -27,9 +27,26 @@ class InternalKeyComparator;
class Mutex ;
class Mutex ;
class VersionSet ;
class VersionSet ;
MemTableListVersion : : MemTableListVersion ( MemTableListVersion * old )
void MemTableListVersion : : AddMemTable ( MemTable * m ) {
memlist_ . push_front ( m ) ;
* parent_memtable_list_memory_usage_ + = m - > ApproximateMemoryUsage ( ) ;
}
void MemTableListVersion : : UnrefMemTable ( autovector < MemTable * > * to_delete ,
MemTable * m ) {
if ( m - > Unref ( ) ) {
to_delete - > push_back ( m ) ;
assert ( * parent_memtable_list_memory_usage_ > = m - > ApproximateMemoryUsage ( ) ) ;
* parent_memtable_list_memory_usage_ - = m - > ApproximateMemoryUsage ( ) ;
} else {
}
}
MemTableListVersion : : MemTableListVersion (
size_t * parent_memtable_list_memory_usage , MemTableListVersion * old )
: max_write_buffer_number_to_maintain_ (
: max_write_buffer_number_to_maintain_ (
old - > max_write_buffer_number_to_maintain_ ) {
old - > max_write_buffer_number_to_maintain_ ) ,
parent_memtable_list_memory_usage_ ( parent_memtable_list_memory_usage ) {
if ( old ! = nullptr ) {
if ( old ! = nullptr ) {
memlist_ = old - > memlist_ ;
memlist_ = old - > memlist_ ;
for ( auto & m : memlist_ ) {
for ( auto & m : memlist_ ) {
@ -44,12 +61,14 @@ MemTableListVersion::MemTableListVersion(MemTableListVersion* old)
}
}
MemTableListVersion : : MemTableListVersion (
MemTableListVersion : : MemTableListVersion (
size_t * parent_memtable_list_memory_usage ,
int max_write_buffer_number_to_maintain )
int max_write_buffer_number_to_maintain )
: max_write_buffer_number_to_maintain_ (
: max_write_buffer_number_to_maintain_ ( max_write_buffer_number_to_maintain ) ,
max_write_buffer_number_to_maintain ) { }
parent_memtable_list_memory_usage_ ( parent_memtable_list_memory_usage ) { }
void MemTableListVersion : : Ref ( ) { + + refs_ ; }
void MemTableListVersion : : Ref ( ) { + + refs_ ; }
// called by superversion::clean()
void MemTableListVersion : : Unref ( autovector < MemTable * > * to_delete ) {
void MemTableListVersion : : Unref ( autovector < MemTable * > * to_delete ) {
assert ( refs_ > = 1 ) ;
assert ( refs_ > = 1 ) ;
- - refs_ ;
- - refs_ ;
@ -58,16 +77,10 @@ void MemTableListVersion::Unref(autovector<MemTable*>* to_delete) {
// that refs_ will not be zero
// that refs_ will not be zero
assert ( to_delete ! = nullptr ) ;
assert ( to_delete ! = nullptr ) ;
for ( const auto & m : memlist_ ) {
for ( const auto & m : memlist_ ) {
MemTable * x = m - > Unref ( ) ;
UnrefMemTable ( to_delete , m ) ;
if ( x ! = nullptr ) {
to_delete - > push_back ( x ) ;
}
}
}
for ( const auto & m : memlist_history_ ) {
for ( const auto & m : memlist_history_ ) {
MemTable * x = m - > Unref ( ) ;
UnrefMemTable ( to_delete , m ) ;
if ( x ! = nullptr ) {
to_delete - > push_back ( x ) ;
}
}
}
delete this ;
delete this ;
}
}
@ -180,7 +193,7 @@ SequenceNumber MemTableListVersion::GetEarliestSequenceNumber(
// caller is responsible for referencing m
// caller is responsible for referencing m
void MemTableListVersion : : Add ( MemTable * m , autovector < MemTable * > * to_delete ) {
void MemTableListVersion : : Add ( MemTable * m , autovector < MemTable * > * to_delete ) {
assert ( refs_ = = 1 ) ; // only when refs_ == 1 is MemTableListVersion mutable
assert ( refs_ = = 1 ) ; // only when refs_ == 1 is MemTableListVersion mutable
memlist_ . push_front ( m ) ;
AddMemTable ( m ) ;
TrimHistory ( to_delete ) ;
TrimHistory ( to_delete ) ;
}
}
@ -195,9 +208,7 @@ void MemTableListVersion::Remove(MemTable* m,
memlist_history_ . push_front ( m ) ;
memlist_history_ . push_front ( m ) ;
TrimHistory ( to_delete ) ;
TrimHistory ( to_delete ) ;
} else {
} else {
if ( m - > Unref ( ) ) {
UnrefMemTable ( to_delete , m ) ;
to_delete - > push_back ( m ) ;
}
}
}
}
}
@ -209,9 +220,7 @@ void MemTableListVersion::TrimHistory(autovector<MemTable*>* to_delete) {
MemTable * x = memlist_history_ . back ( ) ;
MemTable * x = memlist_history_ . back ( ) ;
memlist_history_ . pop_back ( ) ;
memlist_history_ . pop_back ( ) ;
if ( x - > Unref ( ) ) {
UnrefMemTable ( to_delete , x ) ;
to_delete - > push_back ( x ) ;
}
}
}
}
}
@ -361,7 +370,7 @@ void MemTableList::Add(MemTable* m, autovector<MemTable*>* to_delete) {
}
}
// Returns an estimate of the number of bytes of data in use.
// Returns an estimate of the number of bytes of data in use.
size_t MemTableList : : ApproximateMemoryUsage ( ) {
size_t MemTableList : : ApproximateUnflushedMemTables MemoryUsage ( ) {
size_t total_size = 0 ;
size_t total_size = 0 ;
for ( auto & memtable : current_ - > memlist_ ) {
for ( auto & memtable : current_ - > memlist_ ) {
total_size + = memtable - > ApproximateMemoryUsage ( ) ;
total_size + = memtable - > ApproximateMemoryUsage ( ) ;
@ -369,13 +378,15 @@ size_t MemTableList::ApproximateMemoryUsage() {
return total_size ;
return total_size ;
}
}
size_t MemTableList : : ApproximateMemoryUsage ( ) { return current_memory_usage_ ; }
void MemTableList : : InstallNewVersion ( ) {
void MemTableList : : InstallNewVersion ( ) {
if ( current_ - > refs_ = = 1 ) {
if ( current_ - > refs_ = = 1 ) {
// we're the only one using the version, just keep using it
// we're the only one using the version, just keep using it
} else {
} else {
// somebody else holds the current version, we need to create new one
// somebody else holds the current version, we need to create new one
MemTableListVersion * version = current_ ;
MemTableListVersion * version = current_ ;
current_ = new MemTableListVersion ( current_ ) ;
current_ = new MemTableListVersion ( & current_memory_usage_ , current_ ) ;
current_ - > Ref ( ) ;
current_ - > Ref ( ) ;
version - > Unref ( ) ;
version - > Unref ( ) ;
}
}