Skip to content

Commit

Permalink
merge data into .tmp file first
Browse files Browse the repository at this point in the history
  • Loading branch information
artpaul authored Aug 10, 2024
1 parent fabd68d commit 4c976a6
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/bitcask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,12 @@ std::error_code Database::Initialize() {
if (!entry.is_regular_file()) {
continue;
}
if (entry.path().extension() != ".dat") {
continue;
}

auto index = ParseLayoutIndex(entry.path().filename().string());
if (!index || index.value() > 10000) {
if (!index) {
continue;
}

Expand Down Expand Up @@ -658,6 +661,22 @@ std::error_code Database::PackFiles(
return {};
};

const auto rename_temporary = [](const auto& file) -> std::error_code {
std::error_code ec;

const auto target = std::filesystem::path(file->path).replace_extension("dat");
// Move file.
std::filesystem::rename(file->path, target, ec);
// Check error code.
if (ec) {
return ec;
}
// Update location of the file.
file->path = std::move(target);

return {};
};

// 1. Process input files.
for (const auto& file : files) {
// Acquiring read lock to prevent closing the file handle during the read.
Expand All @@ -677,17 +696,19 @@ std::error_code Database::PackFiles(

// 2. Finalize output files.
for (size_t i = 0, end = output.size(); i != end; ++i) {
const auto& f = output[i];

std::for_each(f.begin(), f.end(), [this](auto& f) {
for (const auto& f : output[i]) {
// Append index at the end of file.
if (options_.write_index) {
WriteIndex(f); // TODO: handle errors.
}
// Ensure that all data has been written to the storage device
// before deleting the source files.
f->CloseFile(true);
});
// Rename temporary file.
if (auto ec = rename_temporary(f)) {
return ec;
}
}
}

// 3. Assign new location for the entries read from the input files.
Expand Down

0 comments on commit 4c976a6

Please sign in to comment.