2012-09-15 00:11:35 +00:00
|
|
|
// Copyright (c) 2012 Facebook.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
2012-11-29 00:42:36 +00:00
|
|
|
// found in the LICENSE file.
|
2012-09-15 00:11:35 +00:00
|
|
|
|
2013-08-06 19:54:37 +00:00
|
|
|
#include <algorithm>
|
2012-09-15 00:11:35 +00:00
|
|
|
#include <string>
|
|
|
|
#include <stdint.h>
|
2013-08-06 19:54:37 +00:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/filename.h"
|
2012-09-15 00:11:35 +00:00
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/mutexlock.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
Status DBImpl::DisableFileDeletions() {
|
|
|
|
MutexLock l(&mutex_);
|
2012-11-29 00:42:36 +00:00
|
|
|
disable_delete_obsolete_files_ = true;
|
2013-06-05 17:48:24 +00:00
|
|
|
Log(options_.info_log, "File Deletions Disabled");
|
2012-09-15 00:11:35 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBImpl::EnableFileDeletions() {
|
|
|
|
MutexLock l(&mutex_);
|
2012-11-29 00:42:36 +00:00
|
|
|
disable_delete_obsolete_files_ = false;
|
2013-06-05 17:48:24 +00:00
|
|
|
Log(options_.info_log, "File Deletions Enabled");
|
2012-09-15 00:11:35 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2012-11-29 00:42:36 +00:00
|
|
|
Status DBImpl::GetLiveFiles(std::vector<std::string>& ret,
|
2012-09-24 21:01:01 +00:00
|
|
|
uint64_t* manifest_file_size) {
|
|
|
|
|
|
|
|
*manifest_file_size = 0;
|
2012-09-15 00:11:35 +00:00
|
|
|
|
|
|
|
// flush all dirty data to disk.
|
|
|
|
Status status = Flush(FlushOptions());
|
|
|
|
if (!status.ok()) {
|
2012-11-29 00:42:36 +00:00
|
|
|
Log(options_.info_log, "Cannot Flush data %s\n",
|
2012-09-15 00:11:35 +00:00
|
|
|
status.ToString().c_str());
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
|
|
|
|
// Make a set of all of the live *.sst files
|
2012-11-29 00:42:36 +00:00
|
|
|
std::set<uint64_t> live;
|
2012-09-15 00:11:35 +00:00
|
|
|
versions_->AddLiveFilesCurrentVersion(&live);
|
|
|
|
|
|
|
|
ret.resize(live.size() + 2); //*.sst + CURRENT + MANIFEST
|
|
|
|
|
|
|
|
// create names of the live files. The names are not absolute
|
|
|
|
// paths, instead they are relative to dbname_;
|
|
|
|
std::set<uint64_t>::iterator it = live.begin();
|
|
|
|
for (unsigned int i = 0; i < live.size(); i++, it++) {
|
|
|
|
ret[i] = TableFileName("", *it);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[live.size()] = CurrentFileName("");
|
2012-11-29 00:42:36 +00:00
|
|
|
ret[live.size()+1] = DescriptorFileName("",
|
2012-09-15 00:11:35 +00:00
|
|
|
versions_->ManifestFileNumber());
|
|
|
|
|
2012-09-24 21:01:01 +00:00
|
|
|
// find length of manifest file while holding the mutex lock
|
|
|
|
*manifest_file_size = versions_->ManifestFileSize();
|
|
|
|
|
2012-09-15 00:11:35 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2013-08-06 19:54:37 +00:00
|
|
|
Status DBImpl::GetSortedWalFiles(VectorLogPtr& files) {
|
|
|
|
// First get sorted files in archive dir, then append sorted files from main
|
|
|
|
// dir to maintain sorted order
|
|
|
|
|
|
|
|
// list wal files in archive dir.
|
|
|
|
Status s;
|
|
|
|
std::string archivedir = ArchivalDirectory(dbname_);
|
|
|
|
if (env_->FileExists(archivedir)) {
|
|
|
|
s = AppendSortedWalsOfType(archivedir, files, kArchivedLogFile);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// list wal files in main db dir.
|
|
|
|
s = AppendSortedWalsOfType(dbname_, files, kAliveLogFile);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBImpl::DeleteWalFiles(const VectorLogPtr& files) {
|
|
|
|
Status s;
|
|
|
|
std::string archivedir = ArchivalDirectory(dbname_);
|
|
|
|
std::string files_not_deleted;
|
|
|
|
for (const auto& wal : files) {
|
|
|
|
/* Try deleting in archive dir. If fails, try deleting in main db dir.
|
|
|
|
* This is efficient because all except for very few wal files will be in
|
|
|
|
* archive. Checking for WalType is not much helpful because alive wal could
|
|
|
|
be archived now.
|
|
|
|
*/
|
|
|
|
if (!env_->DeleteFile(archivedir + "/" + wal->Filename()).ok() &&
|
|
|
|
!env_->DeleteFile(dbname_ + "/" + wal->Filename()).ok()) {
|
|
|
|
files_not_deleted.append(wal->Filename());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!files_not_deleted.empty()) {
|
|
|
|
return Status::IOError("Deleted all requested files except: " +
|
|
|
|
files_not_deleted);
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2012-09-15 00:11:35 +00:00
|
|
|
}
|