diff --git a/include/rocksdb/utilities/stackable_db.h b/include/rocksdb/utilities/stackable_db.h index 06bf908bda9..b84c6341e22 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 Status Close() override { return db_->Close(); } @@ -375,6 +385,7 @@ class StackableDB : public DB { protected: DB* db_; + std::shared_ptr shared_db_ptr_; }; } // namespace rocksdb