78 lines
1.9 KiB
C++
78 lines
1.9 KiB
C++
//
|
|
// Created by Kirill Zhukov on 20.04.2025.
|
|
//
|
|
|
|
#include "Compactor.h"
|
|
|
|
namespace usub::utils
|
|
{
|
|
Compactor::Compactor(VersionManager& vm) : version_manager(vm)
|
|
{
|
|
this->background_thread = std::thread([this] { this->run(); });
|
|
}
|
|
|
|
void Compactor::add_sstable_l0(const std::string& filename)
|
|
|
|
{
|
|
this->level0_files.push_back(filename);
|
|
if (this->level0_files.size() >= 4)
|
|
{
|
|
compact_level0();
|
|
}
|
|
}
|
|
|
|
void Compactor::compact_level0()
|
|
{
|
|
if (this->level0_files.size() < 2) return;
|
|
|
|
std::string merged_filename = "L1_" + std::to_string(std::time(nullptr)) + ".dat";
|
|
|
|
LFSkipList<Hash128, std::string> merged(this->version_manager);
|
|
for (const auto& file : this->level0_files)
|
|
{
|
|
read_sstable_with_mmap(merged, file);
|
|
}
|
|
|
|
write_sstable_with_index(merged, merged_filename);
|
|
|
|
this->level1_files.push_back(merged_filename);
|
|
for (const auto& file : this->level0_files)
|
|
{
|
|
::remove(file.c_str());
|
|
}
|
|
this->level0_files.clear();
|
|
}
|
|
|
|
void Compactor::compact_level1()
|
|
{
|
|
if (this->level1_files.size() < 4) return;
|
|
|
|
std::string merged_filename = "L2_" + std::to_string(std::time(nullptr)) + ".dat";
|
|
|
|
LFSkipList<Hash128, std::string> merged(this->version_manager);
|
|
for (const auto& file : this->level1_files)
|
|
{
|
|
read_sstable_with_mmap(merged, file);
|
|
}
|
|
|
|
write_sstable_with_index(merged, merged_filename);
|
|
|
|
this->level2_files.push_back(merged_filename);
|
|
for (const auto& file : this->level1_files)
|
|
{
|
|
::remove(file.c_str());
|
|
}
|
|
this->level1_files.clear();
|
|
}
|
|
|
|
void Compactor::run()
|
|
{
|
|
while (this->running.load())
|
|
{
|
|
compact_level0();
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
}
|
|
}
|
|
} // utils
|
|
// usub
|