summaryrefslogtreecommitdiff
path: root/db/db_impl.cc
diff options
context:
space:
mode:
Diffstat (limited to 'db/db_impl.cc')
-rw-r--r--db/db_impl.cc72
1 files changed, 38 insertions, 34 deletions
diff --git a/db/db_impl.cc b/db/db_impl.cc
index 4754ba3..1a4e459 100644
--- a/db/db_impl.cc
+++ b/db/db_impl.cc
@@ -4,11 +4,10 @@
#include "db/db_impl.h"
-#include <stdint.h>
-#include <stdio.h>
-
#include <algorithm>
#include <atomic>
+#include <cstdint>
+#include <cstdio>
#include <set>
#include <string>
#include <vector>
@@ -198,6 +197,9 @@ Status DBImpl::NewDB() {
new_db.EncodeTo(&record);
s = log.AddRecord(record);
if (s.ok()) {
+ s = file->Sync();
+ }
+ if (s.ok()) {
s = file->Close();
}
}
@@ -206,7 +208,7 @@ Status DBImpl::NewDB() {
// Make "CURRENT" file that points to the new manifest file.
s = SetCurrentFile(env_, dbname_, 1);
} else {
- env_->DeleteFile(manifest);
+ env_->RemoveFile(manifest);
}
return s;
}
@@ -220,7 +222,7 @@ void DBImpl::MaybeIgnoreError(Status* s) const {
}
}
-void DBImpl::DeleteObsoleteFiles() {
+void DBImpl::RemoveObsoleteFiles() {
mutex_.AssertHeld();
if (!bg_error_.ok()) {
@@ -282,7 +284,7 @@ void DBImpl::DeleteObsoleteFiles() {
// are therefore safe to delete while allowing other threads to proceed.
mutex_.Unlock();
for (const std::string& filename : files_to_delete) {
- env_->DeleteFile(dbname_ + "/" + filename);
+ env_->RemoveFile(dbname_ + "/" + filename);
}
mutex_.Lock();
}
@@ -302,6 +304,8 @@ Status DBImpl::Recover(VersionEdit* edit, bool* save_manifest) {
if (!env_->FileExists(CurrentFileName(dbname_))) {
if (options_.create_if_missing) {
+ Log(options_.info_log, "Creating DB %s since it was missing.",
+ dbname_.c_str());
s = NewDB();
if (!s.ok()) {
return s;
@@ -351,8 +355,8 @@ Status DBImpl::Recover(VersionEdit* edit, bool* save_manifest) {
}
if (!expected.empty()) {
char buf[50];
- snprintf(buf, sizeof(buf), "%d missing files; e.g.",
- static_cast<int>(expected.size()));
+ std::snprintf(buf, sizeof(buf), "%d missing files; e.g.",
+ static_cast<int>(expected.size()));
return Status::Corruption(buf, TableFileName(dbname_, *(expected.begin())));
}
@@ -569,7 +573,7 @@ void DBImpl::CompactMemTable() {
imm_->Unref();
imm_ = nullptr;
has_imm_.store(false, std::memory_order_release);
- DeleteObsoleteFiles();
+ RemoveObsoleteFiles();
} else {
RecordBackgroundError(s);
}
@@ -729,7 +733,7 @@ void DBImpl::BackgroundCompaction() {
// Move file to next level
assert(c->num_input_files(0) == 1);
FileMetaData* f = c->input(0, 0);
- c->edit()->DeleteFile(c->level(), f->number);
+ c->edit()->RemoveFile(c->level(), f->number);
c->edit()->AddFile(c->level() + 1, f->number, f->file_size, f->smallest,
f->largest);
status = versions_->LogAndApply(c->edit(), &mutex_);
@@ -749,7 +753,7 @@ void DBImpl::BackgroundCompaction() {
}
CleanupCompaction(compact);
c->ReleaseInputs();
- DeleteObsoleteFiles();
+ RemoveObsoleteFiles();
}
delete c;
@@ -1213,9 +1217,9 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {
uint64_t last_sequence = versions_->LastSequence();
Writer* last_writer = &w;
if (status.ok() && updates != nullptr) { // nullptr batch is for compactions
- WriteBatch* updates = BuildBatchGroup(&last_writer);
- WriteBatchInternal::SetSequence(updates, last_sequence + 1);
- last_sequence += WriteBatchInternal::Count(updates);
+ WriteBatch* write_batch = BuildBatchGroup(&last_writer);
+ WriteBatchInternal::SetSequence(write_batch, last_sequence + 1);
+ last_sequence += WriteBatchInternal::Count(write_batch);
// Add to log and apply to memtable. We can release the lock
// during this phase since &w is currently responsible for logging
@@ -1223,7 +1227,7 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {
// into mem_.
{
mutex_.Unlock();
- status = log_->AddRecord(WriteBatchInternal::Contents(updates));
+ status = log_->AddRecord(WriteBatchInternal::Contents(write_batch));
bool sync_error = false;
if (status.ok() && options.sync) {
status = logfile_->Sync();
@@ -1232,7 +1236,7 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {
}
}
if (status.ok()) {
- status = WriteBatchInternal::InsertInto(updates, mem_);
+ status = WriteBatchInternal::InsertInto(write_batch, mem_);
}
mutex_.Lock();
if (sync_error) {
@@ -1242,7 +1246,7 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {
RecordBackgroundError(status);
}
}
- if (updates == tmp_batch_) tmp_batch_->Clear();
+ if (write_batch == tmp_batch_) tmp_batch_->Clear();
versions_->SetLastSequence(last_sequence);
}
@@ -1397,26 +1401,26 @@ bool DBImpl::GetProperty(const Slice& property, std::string* value) {
return false;
} else {
char buf[100];
- snprintf(buf, sizeof(buf), "%d",
- versions_->NumLevelFiles(static_cast<int>(level)));
+ std::snprintf(buf, sizeof(buf), "%d",
+ versions_->NumLevelFiles(static_cast<int>(level)));
*value = buf;
return true;
}
} else if (in == "stats") {
char buf[200];
- snprintf(buf, sizeof(buf),
- " Compactions\n"
- "Level Files Size(MB) Time(sec) Read(MB) Write(MB)\n"
- "--------------------------------------------------\n");
+ std::snprintf(buf, sizeof(buf),
+ " Compactions\n"
+ "Level Files Size(MB) Time(sec) Read(MB) Write(MB)\n"
+ "--------------------------------------------------\n");
value->append(buf);
for (int level = 0; level < config::kNumLevels; level++) {
int files = versions_->NumLevelFiles(level);
if (stats_[level].micros > 0 || files > 0) {
- snprintf(buf, sizeof(buf), "%3d %8d %8.0f %9.0f %8.0f %9.0f\n", level,
- files, versions_->NumLevelBytes(level) / 1048576.0,
- stats_[level].micros / 1e6,
- stats_[level].bytes_read / 1048576.0,
- stats_[level].bytes_written / 1048576.0);
+ std::snprintf(buf, sizeof(buf), "%3d %8d %8.0f %9.0f %8.0f %9.0f\n",
+ level, files, versions_->NumLevelBytes(level) / 1048576.0,
+ stats_[level].micros / 1e6,
+ stats_[level].bytes_read / 1048576.0,
+ stats_[level].bytes_written / 1048576.0);
value->append(buf);
}
}
@@ -1433,8 +1437,8 @@ bool DBImpl::GetProperty(const Slice& property, std::string* value) {
total_usage += imm_->ApproximateMemoryUsage();
}
char buf[50];
- snprintf(buf, sizeof(buf), "%llu",
- static_cast<unsigned long long>(total_usage));
+ std::snprintf(buf, sizeof(buf), "%llu",
+ static_cast<unsigned long long>(total_usage));
value->append(buf);
return true;
}
@@ -1506,7 +1510,7 @@ Status DB::Open(const Options& options, const std::string& dbname, DB** dbptr) {
s = impl->versions_->LogAndApply(&edit, &impl->mutex_);
}
if (s.ok()) {
- impl->DeleteObsoleteFiles();
+ impl->RemoveObsoleteFiles();
impl->MaybeScheduleCompaction();
}
impl->mutex_.Unlock();
@@ -1539,15 +1543,15 @@ Status DestroyDB(const std::string& dbname, const Options& options) {
for (size_t i = 0; i < filenames.size(); i++) {
if (ParseFileName(filenames[i], &number, &type) &&
type != kDBLockFile) { // Lock file will be deleted at end
- Status del = env->DeleteFile(dbname + "/" + filenames[i]);
+ Status del = env->RemoveFile(dbname + "/" + filenames[i]);
if (result.ok() && !del.ok()) {
result = del;
}
}
}
env->UnlockFile(lock); // Ignore error since state is already gone
- env->DeleteFile(lockname);
- env->DeleteDir(dbname); // Ignore error in case dir contains other files
+ env->RemoveFile(lockname);
+ env->RemoveDir(dbname); // Ignore error in case dir contains other files
}
return result;
}