From 1c6742e32f3f59a0bac0317bec56f758eba115fc Mon Sep 17 00:00:00 2001 From: Abhishek Kona Date: Fri, 7 Dec 2012 16:30:22 -0800 Subject: [PATCH] Refactor GetArchivalDirectoryName to filename.h Summary: filename.h has functions to do similar things. Moving code away from db_impl.cc Test Plan: make check Reviewers: dhruba Reviewed By: dhruba Differential Revision: https://reviews.facebook.net/D7251 --- db/db_impl.cc | 20 ++++++-------------- db/db_impl.h | 3 --- db/db_test.cc | 2 +- db/filename.cc | 3 +++ db/filename.h | 4 ++++ 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index cf73158fd..dc90ccec2 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -44,8 +44,6 @@ namespace leveldb { void dumpLeveldbBuildVersion(Logger * log); -const std::string DBImpl::ARCHIVAL_DIR = "archive"; - static Status NewLogger(const std::string& dbname, const std::string& db_log_dir, Env* env, @@ -334,13 +332,9 @@ void DBImpl::MaybeIgnoreError(Status* s) const { } } -std::string DBImpl::GetArchivalDirectoryName() { - return dbname_ + "/" + ARCHIVAL_DIR; -} - const Status DBImpl::CreateArchivalDirectory() { if (options_.WAL_ttl_seconds > 0) { - std::string archivalPath = GetArchivalDirectoryName(); + std::string archivalPath = ArchivalDirectory(dbname_); return env_->CreateDirIfMissing(archivalPath); } return Status::OK(); @@ -424,7 +418,6 @@ void DBImpl::PurgeObsoleteFiles(DeletionState& state) { } if (!keep) { - const std::string currentFile = state.allfiles[i]; if (type == kTableFile) { // record the files to be evicted from the cache state.files_to_evict.push_back(number); @@ -433,16 +426,15 @@ void DBImpl::PurgeObsoleteFiles(DeletionState& state) { int(type), static_cast(number)); if (type == kLogFile && options_.WAL_ttl_seconds > 0) { - Status st = env_->RenameFile(dbname_ + "/" + currentFile, - dbname_ + "/" + ARCHIVAL_DIR + "/" + currentFile); - + Status st = env_->RenameFile(LogFileName(dbname_, number), + ArchivedLogFileName(dbname_, number)); if (!st.ok()) { Log(options_.info_log, "RenameFile type=%d #%lld FAILED\n", int(type), static_cast(number)); } } else { - Status st = env_->DeleteFile(dbname_ + "/" + currentFile); + Status st = env_->DeleteFile(dbname_ + "/" + state.allfiles[i]); if(!st.ok()) { Log(options_.info_log, "Delete type=%d #%lld FAILED\n", int(type), @@ -485,7 +477,7 @@ void DBImpl::DeleteObsoleteFiles() { void DBImpl::PurgeObsoleteWALFiles() { if (options_.WAL_ttl_seconds != ULONG_MAX && options_.WAL_ttl_seconds > 0) { std::vector WALFiles; - std::string archivalDir = GetArchivalDirectoryName(); + std::string archivalDir = ArchivalDirectory(dbname_); env_->GetChildren(archivalDir, &WALFiles); int64_t currentTime; const Status status = env_->GetCurrentTime(¤tTime); @@ -900,7 +892,7 @@ Status DBImpl::GetUpdatesSince(SequenceNumber seq, return s; } // list wal files in archive dir. - s = ListAllWALFiles(GetArchivalDirectoryName(), &walFiles, kArchivedLogFile); + s = ListAllWALFiles(ArchivalDirectory(dbname_), &walFiles, kArchivedLogFile); if (!s.ok()) { return s; } diff --git a/db/db_impl.h b/db/db_impl.h index 7b45e1671..78bb0f699 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -57,8 +57,6 @@ class DBImpl : public DB { uint64_t* manifest_file_size); virtual Status GetUpdatesSince(SequenceNumber seq_number, TransactionLogIterator ** iter); - // Return's the path of the archival directory. - std::string GetArchivalDirectoryName(); // Extra methods (for testing) that are not in the public DB interface @@ -316,7 +314,6 @@ protected: CompactionStats* stats_; static const int KEEP_LOG_FILE_NUM = 1000; - static const std::string ARCHIVAL_DIR; std::string db_absolute_path_; // count of the number of contiguous delaying writes diff --git a/db/db_test.cc b/db/db_test.cc index acafb1c95..d67e84827 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -2225,7 +2225,7 @@ TEST(DBTest, WALArchival) { // Re-open db. Causes deletion/archival to take place. // Assert that the files moved under "/archive". - std::string archiveDir = dbfull()->GetArchivalDirectoryName(); + std::string archiveDir = ArchivalDirectory(dbname_); for (int i = 0; i < 10; ++i) { diff --git a/db/filename.cc b/db/filename.cc index ce4a49eeb..92710952d 100644 --- a/db/filename.cc +++ b/db/filename.cc @@ -57,6 +57,9 @@ std::string LogFileName(const std::string& name, uint64_t number) { return MakeFileName(name, number, "log"); } +std::string ArchivalDirectory(const std::string& dbname) { + return dbname + "/" + ARCHIVAL_DIR; +} std::string ArchivedLogFileName(const std::string& name, uint64_t number) { assert(number > 0); return MakeFileName(name + "/archive", number, "log"); diff --git a/db/filename.h b/db/filename.h index e71d0336a..aff2522ef 100644 --- a/db/filename.h +++ b/db/filename.h @@ -32,6 +32,10 @@ enum FileType { // "dbname". extern std::string LogFileName(const std::string& dbname, uint64_t number); +static const std::string ARCHIVAL_DIR = "archive"; + +extern std::string ArchivalDirectory(const std::string& dbname); + // Return the name of the archived log file with the specified number // in the db named by "dbname". The result will be prefixed with "dbname". extern std::string ArchivedLogFileName(const std::string& dbname,