From e55b3c040cc17a17e42afc5c5204394ff96babf5 Mon Sep 17 00:00:00 2001 From: Igor Canadi Date: Sun, 26 Jan 2014 17:40:43 -0800 Subject: [PATCH] Fixing ref-counting memtables --- db/memtablelist.cc | 10 +++++++--- db/memtablelist.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/db/memtablelist.cc b/db/memtablelist.cc index 884e326aaf..ca662569ff 100644 --- a/db/memtablelist.cc +++ b/db/memtablelist.cc @@ -76,17 +76,16 @@ void MemTableListVersion::AddIterators(const ReadOptions& options, } } +// caller is responsible for referencing m void MemTableListVersion::Add(MemTable* m) { assert(refs_ == 1); // only when refs_ == 1 is MemTableListVersion mutable - m->Ref(); memlist_.push_front(m); ++size_; } +// caller is responsible for unreferencing m void MemTableListVersion::Remove(MemTable* m) { assert(refs_ == 1); // only when refs_ == 1 is MemTableListVersion mutable - MemTable* x __attribute__((unused)) = m->Unref(); - assert(x == nullptr); // it still needs to be alive! memlist_.remove(m); --size_; } @@ -232,6 +231,11 @@ Status MemTableList::InstallMemtableFlushResults( void MemTableList::Add(MemTable* m) { assert(current_->size_ >= num_flush_not_started_); InstallNewVersion(); + // this method is used to move mutable memtable into an immutable list. + // since mutable memtable is already refcounted by the DBImpl, + // and when moving to the imutable list we don't unref it, + // we don't have to ref the memtable here. we just take over the + // reference from the DBImpl. current_->Add(m); m->MarkImmutable(); num_flush_not_started_++; diff --git a/db/memtablelist.h b/db/memtablelist.h index 16bb49743a..2a2b614084 100644 --- a/db/memtablelist.h +++ b/db/memtablelist.h @@ -40,12 +40,12 @@ class MemTableListVersion { void AddIterators(const ReadOptions& options, std::vector* iterator_list); + private: // REQUIRE: m is mutable memtable void Add(MemTable* m); // REQUIRE: m is mutable memtable void Remove(MemTable* m); - private: friend class MemTableList; std::list memlist_; int size_ = 0;