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
This commit is contained in:
Yi Wu 2018-01-26 15:12:16 -08:00 committed by Facebook Github Bot
parent 4927b4e662
commit 439855a774

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
#include "rocksdb/db.h" #include "rocksdb/db.h"
@ -18,11 +19,20 @@ namespace rocksdb {
// This class contains APIs to stack rocksdb wrappers.Eg. Stack TTL over base d // This class contains APIs to stack rocksdb wrappers.Eg. Stack TTL over base d
class StackableDB : public DB { class StackableDB : public DB {
public: public:
// StackableDB is the owner of db now! // StackableDB take sole ownership of the underlying db.
explicit StackableDB(DB* db) : db_(db) {} explicit StackableDB(DB* db) : db_(db) {}
// StackableDB take shared ownership of the underlying db.
explicit StackableDB(std::shared_ptr<DB> db)
: db_(db.get()), shared_db_ptr_(db) {}
~StackableDB() { ~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(); } virtual Status Close() override { return db_->Close(); }
@ -375,6 +385,7 @@ class StackableDB : public DB {
protected: protected:
DB* db_; DB* db_;
std::shared_ptr<DB> shared_db_ptr_;
}; };
} // namespace rocksdb } // namespace rocksdb