From a107691711435ae94bbb8bc1f8a1eed0a9f6de9b Mon Sep 17 00:00:00 2001 From: Igor Canadi Date: Mon, 13 Jan 2014 09:21:37 -0800 Subject: [PATCH] [column families] Implement refcounting ColumnFamilyData Summary: We don't want to delete ColumnFamilyData object if somebody has references to it. Test Plan: `make check` for now, but will need to implement bigger column family test case Reviewers: dhruba, haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D15111 --- db/version_set.cc | 5 +++-- db/version_set.h | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/db/version_set.cc b/db/version_set.cc index e1352ebe5..ad1169189 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -1163,7 +1163,7 @@ VersionSet::~VersionSet() { cfd.second->current->Unref(); // List must be empty assert(cfd.second->dummy_versions.next_ == &cfd.second->dummy_versions); - delete cfd.second; + cfd.second->Unref(); } for (auto file : obsolete_files_) { delete file; @@ -3059,7 +3059,8 @@ void VersionSet::DropColumnFamily(VersionEdit* edit) { cfd->second->current->Unref(); // List must be empty assert(cfd->second->dummy_versions.next_ == &cfd->second->dummy_versions); - delete cfd->second; + // might delete itself + cfd->second->Unref(); column_family_data_.erase(cfd); } diff --git a/db/version_set.h b/db/version_set.h index bd9b1095a..b89083613 100644 --- a/db/version_set.h +++ b/db/version_set.h @@ -223,11 +223,29 @@ struct ColumnFamilyData { Version dummy_versions; // Head of circular doubly-linked list of versions. Version* current; // == dummy_versions.prev_ ColumnFamilyOptions options; + int refs; + + void Ref() { + ++refs; + } + + void Unref() { + assert(refs > 0); + if (refs == 1) { + delete this; + } else { + --refs; + } + } ColumnFamilyData(const std::string& name, VersionSet* vset, const ColumnFamilyOptions& options) - : name(name), dummy_versions(vset), current(nullptr), options(options) {} + : name(name), + dummy_versions(vset), + current(nullptr), + options(options), + refs(1) {} ~ColumnFamilyData() {} };