From 11c26bd4a40d13a63250344c37df116a981ee9f9 Mon Sep 17 00:00:00 2001 From: Igor Canadi Date: Mon, 25 Nov 2013 12:39:23 -0800 Subject: [PATCH] [RocksDB] Interface changes required for BackupableDB Summary: This is part of https://reviews.facebook.net/D14295 -- smaller diff that is easier to review Test Plan: make asan_check Reviewers: dhruba, haobo, emayanke Reviewed By: emayanke CC: leveldb, kailiu, reconnect.grayhat Differential Revision: https://reviews.facebook.net/D14301 --- db/db_impl.cc | 4 ++ db/db_impl.h | 1 + db/db_test.cc | 4 ++ include/rocksdb/db.h | 3 ++ include/utilities/stackable_db.h | 82 ++++++++++++++------------------ utilities/ttl/db_ttl.cc | 4 ++ utilities/ttl/db_ttl.h | 4 +- 7 files changed, 54 insertions(+), 48 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index f8d5f446b..1f28af324 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -3131,6 +3131,10 @@ Status DBImpl::MakeRoomForWrite(bool force) { return s; } +Env* DBImpl::GetEnv() const { + return env_; +} + bool DBImpl::GetProperty(const Slice& property, std::string* value) { value->clear(); diff --git a/db/db_impl.h b/db/db_impl.h index dc4c20a51..fdd0a2520 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -67,6 +67,7 @@ class DBImpl : public DB { virtual int NumberLevels(); virtual int MaxMemCompactionLevel(); virtual int Level0StopWriteTrigger(); + virtual Env* GetEnv() const; virtual Status Flush(const FlushOptions& options); virtual Status DisableFileDeletions(); virtual Status EnableFileDeletions(); diff --git a/db/db_test.cc b/db/db_test.cc index aca07bcff..5e257483e 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -4462,6 +4462,10 @@ class ModelDB: public DB { return -1; } + virtual Env* GetEnv() const { + return nullptr; + } + virtual Status Flush(const rocksdb::FlushOptions& options) { Status ret; return ret; diff --git a/include/rocksdb/db.h b/include/rocksdb/db.h index 260202091..0ec7ffba7 100644 --- a/include/rocksdb/db.h +++ b/include/rocksdb/db.h @@ -228,6 +228,9 @@ class DB { // Number of files in level-0 that would stop writes. virtual int Level0StopWriteTrigger() = 0; + // Get Env object from the DB + virtual Env* GetEnv() const = 0; + // Flush all mem-table data. virtual Status Flush(const FlushOptions& options) = 0; diff --git a/include/utilities/stackable_db.h b/include/utilities/stackable_db.h index f15a22e12..b1e308b20 100644 --- a/include/utilities/stackable_db.h +++ b/include/utilities/stackable_db.h @@ -10,152 +10,140 @@ namespace rocksdb { // This class contains APIs to stack rocksdb wrappers.Eg. Stack TTL over base d class StackableDB : public DB { public: - explicit StackableDB(StackableDB* sdb) : sdb_(sdb) {} + // StackableDB is the owner of db now! + explicit StackableDB(DB* db) : db_(db) {} - // Returns the DB object that is the lowermost component in the stack of DBs - virtual DB* GetRawDB() { - return sdb_->GetRawDB(); + ~StackableDB() { + delete db_; } - // convert a DB to StackableDB - // TODO: This function does not work yet. Passing nullptr to StackableDB in - // NewStackableDB's constructor will cause segfault on object's usage - static StackableDB* DBToStackableDB(DB* db) { - class NewStackableDB : public StackableDB { - public: - NewStackableDB(DB* db) - : StackableDB(nullptr), - db_(db) {} - - DB* GetRawDB() { - return db_; - } - - private: - DB* db_; - }; - return new NewStackableDB(db); + virtual DB* GetBaseDB() { + return db_; } virtual Status Put(const WriteOptions& options, const Slice& key, const Slice& val) override { - return sdb_->Put(options, key, val); + return db_->Put(options, key, val); } virtual Status Get(const ReadOptions& options, const Slice& key, std::string* value) override { - return sdb_->Get(options, key, value); + return db_->Get(options, key, value); } virtual std::vector MultiGet(const ReadOptions& options, const std::vector& keys, std::vector* values) override { - return sdb_->MultiGet(options, keys, values); + return db_->MultiGet(options, keys, values); } virtual bool KeyMayExist(const ReadOptions& options, const Slice& key, std::string* value, bool* value_found = nullptr) override { - return sdb_->KeyMayExist(options, key, value, value_found); + return db_->KeyMayExist(options, key, value, value_found); } virtual Status Delete(const WriteOptions& wopts, const Slice& key) override { - return sdb_->Delete(wopts, key); + return db_->Delete(wopts, key); } virtual Status Merge(const WriteOptions& options, const Slice& key, const Slice& value) override { - return sdb_->Merge(options, key, value); + return db_->Merge(options, key, value); } virtual Status Write(const WriteOptions& opts, WriteBatch* updates) override { - return sdb_->Write(opts, updates); + return db_->Write(opts, updates); } virtual Iterator* NewIterator(const ReadOptions& opts) override { - return sdb_->NewIterator(opts); + return db_->NewIterator(opts); } virtual const Snapshot* GetSnapshot() override { - return sdb_->GetSnapshot(); + return db_->GetSnapshot(); } virtual void ReleaseSnapshot(const Snapshot* snapshot) override { - return sdb_->ReleaseSnapshot(snapshot); + return db_->ReleaseSnapshot(snapshot); } virtual bool GetProperty(const Slice& property, std::string* value) override { - return sdb_->GetProperty(property, value); + return db_->GetProperty(property, value); } virtual void GetApproximateSizes(const Range* r, int n, uint64_t* sizes) override { - return sdb_->GetApproximateSizes(r, n, sizes); + return db_->GetApproximateSizes(r, n, sizes); } virtual void CompactRange(const Slice* begin, const Slice* end, bool reduce_level = false, int target_level = -1) override { - return sdb_->CompactRange(begin, end, reduce_level, target_level); + return db_->CompactRange(begin, end, reduce_level, target_level); } virtual int NumberLevels() override { - return sdb_->NumberLevels(); + return db_->NumberLevels(); } virtual int MaxMemCompactionLevel() override { - return sdb_->MaxMemCompactionLevel(); + return db_->MaxMemCompactionLevel(); } virtual int Level0StopWriteTrigger() override { - return sdb_->Level0StopWriteTrigger(); + return db_->Level0StopWriteTrigger(); + } + + virtual Env* GetEnv() const override { + return db_->GetEnv(); } virtual Status Flush(const FlushOptions& fopts) override { - return sdb_->Flush(fopts); + return db_->Flush(fopts); } virtual Status DisableFileDeletions() override { - return sdb_->DisableFileDeletions(); + return db_->DisableFileDeletions(); } virtual Status EnableFileDeletions() override { - return sdb_->EnableFileDeletions(); + return db_->EnableFileDeletions(); } virtual Status GetLiveFiles(std::vector& vec, uint64_t* mfs, bool flush_memtable = true) override { - return sdb_->GetLiveFiles(vec, mfs, flush_memtable); + return db_->GetLiveFiles(vec, mfs, flush_memtable); } virtual SequenceNumber GetLatestSequenceNumber() const override { - return sdb_->GetLatestSequenceNumber(); + return db_->GetLatestSequenceNumber(); } virtual Status GetSortedWalFiles(VectorLogPtr& files) override { - return sdb_->GetSortedWalFiles(files); + return db_->GetSortedWalFiles(files); } virtual Status DeleteFile(std::string name) override { - return sdb_->DeleteFile(name); + return db_->DeleteFile(name); } virtual Status GetUpdatesSince(SequenceNumber seq_number, unique_ptr* iter) override { - return sdb_->GetUpdatesSince(seq_number, iter); + return db_->GetUpdatesSince(seq_number, iter); } protected: - StackableDB* sdb_; + DB* db_; }; } // namespace rocksdb diff --git a/utilities/ttl/db_ttl.cc b/utilities/ttl/db_ttl.cc index a019102d9..127a2e566 100644 --- a/utilities/ttl/db_ttl.cc +++ b/utilities/ttl/db_ttl.cc @@ -254,6 +254,10 @@ int DBWithTTL::Level0StopWriteTrigger() { return db_->Level0StopWriteTrigger(); } +Env* DBWithTTL::GetEnv() const { + return db_->GetEnv(); +} + Status DBWithTTL::Flush(const FlushOptions& fopts) { return db_->Flush(fopts); } diff --git a/utilities/ttl/db_ttl.h b/utilities/ttl/db_ttl.h index ffee0ccf2..d5c51dd3f 100644 --- a/utilities/ttl/db_ttl.h +++ b/utilities/ttl/db_ttl.h @@ -67,6 +67,8 @@ class DBWithTTL : public StackableDB { virtual int Level0StopWriteTrigger(); + virtual Env* GetEnv() const; + virtual Status Flush(const FlushOptions& fopts); virtual Status DisableFileDeletions(); @@ -88,7 +90,7 @@ class DBWithTTL : public StackableDB { // Simulate a db crash, no elegant closing of database. void TEST_Destroy_DBWithTtl(); - virtual DB* GetRawDB() { + virtual DB* GetBaseDB() { return db_; }