Skip to content

Commit

Permalink
cleanup temporaries on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
artpaul committed Aug 10, 2024
1 parent 39f3f24 commit 383c1d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/bitcask/bitcask.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct Options {

uint32_t max_file_size = std::numeric_limits<uint32_t>::max();

/// If the database has been loaded successfully, clean up any temporary files at startup.
bool clean_temporary_on_startup = true;

/// Flush in-core data to storage device after write.
bool data_sync = false;

Expand Down
12 changes: 12 additions & 0 deletions src/bitcask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ std::error_code Database::Rotate() {
}

std::error_code Database::Initialize() {
std::vector<std::filesystem::path> cleanup;
unordered_string_map<uint64_t> tombstones;

const auto cb = [&](const Record& record, bool is_tombstone, std::string_view key) -> std::error_code {
Expand Down Expand Up @@ -560,6 +561,9 @@ std::error_code Database::Initialize() {
continue;
}
if (entry.path().extension() != ".dat") {
if (options_.clean_temporary_on_startup && entry.path().extension() == ".tmp") {
cleanup.push_back(entry.path());
}
continue;
}

Expand Down Expand Up @@ -589,6 +593,14 @@ std::error_code Database::Initialize() {
files_[index.value()].push_back(std::move(file));
}
}

// Cleanup temporaries.
for (const auto& path : cleanup) {
std::error_code ec;

std::filesystem::remove(path, ec);
}

return {};
}

Expand Down

0 comments on commit 383c1d1

Please sign in to comment.