From: Yi Wu Date: Fri, 26 Jan 2018 23:12:16 +0000 (-0800) Subject: StackableDB optionally take shared ownership of the underlying DB X-Git-Tag: rocksdb-5.10.2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d91f420dd9b3fbdbb792502a61b74911a46fdad0;p=rocksdb.git StackableDB optionally take shared ownership of the underlying DB Summary: Allow StackableDB optionally takes a shared_ptr on construction and thus hold shared ownership of the underlying DB. Closes https://github.com/facebook/rocksdb/pull/3423 Differential Revision: D6824163 Pulled By: yiwu-arbug fbshipit-source-id: dbdc30c42e007533a987ef413785e192340f03eb --- diff --git a/include/rocksdb/utilities/stackable_db.h b/include/rocksdb/utilities/stackable_db.h index 1035e6f5..6d571cd1 100644 --- a/include/rocksdb/utilities/stackable_db.h +++ b/include/rocksdb/utilities/stackable_db.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include "rocksdb/db.h" @@ -18,11 +19,20 @@ namespace rocksdb { // This class contains APIs to stack rocksdb wrappers.Eg. Stack TTL over base d class StackableDB : public DB { public: - // StackableDB is the owner of db now! + // StackableDB take sole ownership of the underlying db. explicit StackableDB(DB* db) : db_(db) {} + // StackableDB take shared ownership of the underlying db. + explicit StackableDB(std::shared_ptr db) + : db_(db.get()), shared_db_ptr_(db) {} + ~StackableDB() { - delete db_; + if (shared_db_ptr_ == nullptr) { + delete db_; + } else { + assert(shared_db_ptr_.get() == db_); + } + db_ = nullptr; } virtual DB* GetBaseDB() { @@ -373,6 +383,7 @@ class StackableDB : public DB { protected: DB* db_; + std::shared_ptr shared_db_ptr_; }; } // namespace rocksdb