Skip to content

Commit

Permalink
correct output compaction slot
Browse files Browse the repository at this point in the history
* correct output compaction slot
* close file after read during the pack
  • Loading branch information
artpaul authored Oct 3, 2024
1 parent d228e9a commit ad52f05
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/bitcask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ std::error_code Database::PackFiles(

static_assert(std::is_trivially_destructible_v<decltype(updates)::value_type>);

const auto get_scatter_slot = [&](const size_t i) { return (slot * 8 + 1) + i; };

const auto cb = [&](const Record& record, const bool is_tombstone, const std::string_view key,
const std::string_view value) -> std::error_code {
auto ki = keys_.find(key);
Expand Down Expand Up @@ -729,7 +731,7 @@ std::error_code Database::PackFiles(

if (mode == CompactionMode::kScatter) {
i = XXH64(key.data(), key.size(), slot + 1) % 8;
index = (slot * 8 + 1) + i;
index = get_scatter_slot(i);
}

if (output[i].empty() || IsCapacityExceeded(output[i].back()->size, length)) {
Expand Down Expand Up @@ -793,6 +795,10 @@ std::error_code Database::PackFiles(
// TODO: finalize.
return ec;
}

read_lock.unlock();
// Close the file to avoid possible exhaustion of available file handles.
file->CloseFile();
}

// 2. Finalize output files.
Expand Down Expand Up @@ -824,12 +830,15 @@ std::error_code Database::PackFiles(
std::lock_guard file_lock(file_mutex_);
// 4. Append output files to LSM-tree.
for (size_t i = 0, end = output.size(); i != end; ++i) {
const auto index = (mode == CompactionMode::kGather) ? slot : get_scatter_slot(i);

// Adjust the counter of space used.
for (const auto& f : output[i]) {
space_used_.fetch_add(f->size, std::memory_order_relaxed);
}

files_[1 + i].insert(files_[1 + i].end(), output[i].begin(), output[i].end());
files_[index].insert(files_[index].end(), std::make_move_iterator(output[i].begin()),
std::make_move_iterator(output[i].end()));
}

return {};
Expand Down

0 comments on commit ad52f05

Please sign in to comment.