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
This commit is contained in:
Abhishek Kona 2012-12-07 16:30:22 -08:00
parent 38671c4d54
commit 1c6742e32f
5 changed files with 14 additions and 18 deletions

View File

@ -44,8 +44,6 @@ namespace leveldb {
void dumpLeveldbBuildVersion(Logger * log); void dumpLeveldbBuildVersion(Logger * log);
const std::string DBImpl::ARCHIVAL_DIR = "archive";
static Status NewLogger(const std::string& dbname, static Status NewLogger(const std::string& dbname,
const std::string& db_log_dir, const std::string& db_log_dir,
Env* env, Env* env,
@ -334,13 +332,9 @@ void DBImpl::MaybeIgnoreError(Status* s) const {
} }
} }
std::string DBImpl::GetArchivalDirectoryName() {
return dbname_ + "/" + ARCHIVAL_DIR;
}
const Status DBImpl::CreateArchivalDirectory() { const Status DBImpl::CreateArchivalDirectory() {
if (options_.WAL_ttl_seconds > 0) { if (options_.WAL_ttl_seconds > 0) {
std::string archivalPath = GetArchivalDirectoryName(); std::string archivalPath = ArchivalDirectory(dbname_);
return env_->CreateDirIfMissing(archivalPath); return env_->CreateDirIfMissing(archivalPath);
} }
return Status::OK(); return Status::OK();
@ -424,7 +418,6 @@ void DBImpl::PurgeObsoleteFiles(DeletionState& state) {
} }
if (!keep) { if (!keep) {
const std::string currentFile = state.allfiles[i];
if (type == kTableFile) { if (type == kTableFile) {
// record the files to be evicted from the cache // record the files to be evicted from the cache
state.files_to_evict.push_back(number); state.files_to_evict.push_back(number);
@ -433,16 +426,15 @@ void DBImpl::PurgeObsoleteFiles(DeletionState& state) {
int(type), int(type),
static_cast<unsigned long long>(number)); static_cast<unsigned long long>(number));
if (type == kLogFile && options_.WAL_ttl_seconds > 0) { if (type == kLogFile && options_.WAL_ttl_seconds > 0) {
Status st = env_->RenameFile(dbname_ + "/" + currentFile, Status st = env_->RenameFile(LogFileName(dbname_, number),
dbname_ + "/" + ARCHIVAL_DIR + "/" + currentFile); ArchivedLogFileName(dbname_, number));
if (!st.ok()) { if (!st.ok()) {
Log(options_.info_log, "RenameFile type=%d #%lld FAILED\n", Log(options_.info_log, "RenameFile type=%d #%lld FAILED\n",
int(type), int(type),
static_cast<unsigned long long>(number)); static_cast<unsigned long long>(number));
} }
} else { } else {
Status st = env_->DeleteFile(dbname_ + "/" + currentFile); Status st = env_->DeleteFile(dbname_ + "/" + state.allfiles[i]);
if(!st.ok()) { if(!st.ok()) {
Log(options_.info_log, "Delete type=%d #%lld FAILED\n", Log(options_.info_log, "Delete type=%d #%lld FAILED\n",
int(type), int(type),
@ -485,7 +477,7 @@ void DBImpl::DeleteObsoleteFiles() {
void DBImpl::PurgeObsoleteWALFiles() { void DBImpl::PurgeObsoleteWALFiles() {
if (options_.WAL_ttl_seconds != ULONG_MAX && options_.WAL_ttl_seconds > 0) { if (options_.WAL_ttl_seconds != ULONG_MAX && options_.WAL_ttl_seconds > 0) {
std::vector<std::string> WALFiles; std::vector<std::string> WALFiles;
std::string archivalDir = GetArchivalDirectoryName(); std::string archivalDir = ArchivalDirectory(dbname_);
env_->GetChildren(archivalDir, &WALFiles); env_->GetChildren(archivalDir, &WALFiles);
int64_t currentTime; int64_t currentTime;
const Status status = env_->GetCurrentTime(&currentTime); const Status status = env_->GetCurrentTime(&currentTime);
@ -900,7 +892,7 @@ Status DBImpl::GetUpdatesSince(SequenceNumber seq,
return s; return s;
} }
// list wal files in archive dir. // list wal files in archive dir.
s = ListAllWALFiles(GetArchivalDirectoryName(), &walFiles, kArchivedLogFile); s = ListAllWALFiles(ArchivalDirectory(dbname_), &walFiles, kArchivedLogFile);
if (!s.ok()) { if (!s.ok()) {
return s; return s;
} }

View File

@ -57,8 +57,6 @@ class DBImpl : public DB {
uint64_t* manifest_file_size); uint64_t* manifest_file_size);
virtual Status GetUpdatesSince(SequenceNumber seq_number, virtual Status GetUpdatesSince(SequenceNumber seq_number,
TransactionLogIterator ** iter); 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 // Extra methods (for testing) that are not in the public DB interface
@ -316,7 +314,6 @@ protected:
CompactionStats* stats_; CompactionStats* stats_;
static const int KEEP_LOG_FILE_NUM = 1000; static const int KEEP_LOG_FILE_NUM = 1000;
static const std::string ARCHIVAL_DIR;
std::string db_absolute_path_; std::string db_absolute_path_;
// count of the number of contiguous delaying writes // count of the number of contiguous delaying writes

View File

@ -2225,7 +2225,7 @@ TEST(DBTest, WALArchival) {
// Re-open db. Causes deletion/archival to take place. // Re-open db. Causes deletion/archival to take place.
// Assert that the files moved under "/archive". // Assert that the files moved under "/archive".
std::string archiveDir = dbfull()->GetArchivalDirectoryName(); std::string archiveDir = ArchivalDirectory(dbname_);
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {

View File

@ -57,6 +57,9 @@ std::string LogFileName(const std::string& name, uint64_t number) {
return MakeFileName(name, number, "log"); 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) { std::string ArchivedLogFileName(const std::string& name, uint64_t number) {
assert(number > 0); assert(number > 0);
return MakeFileName(name + "/archive", number, "log"); return MakeFileName(name + "/archive", number, "log");

View File

@ -32,6 +32,10 @@ enum FileType {
// "dbname". // "dbname".
extern std::string LogFileName(const std::string& dbname, uint64_t number); 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 // 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". // in the db named by "dbname". The result will be prefixed with "dbname".
extern std::string ArchivedLogFileName(const std::string& dbname, extern std::string ArchivedLogFileName(const std::string& dbname,