1#ifndef STORAGE_WRITE_AHEAD_LOG_H
2#define STORAGE_WRITE_AHEAD_LOG_H
4#include <vkdb/lsm_tree.h>
5#include <vkdb/wal_lsm.h>
12const FilePath WAL_FILENAME{
"wal.log"};
19template <ArithmeticNoCVRefQuals TValue>
35 : path_{lsm_tree_path / WAL_FILENAME} {}
75 std::ofstream file{path_, std::ios::app};
76 if (!file.is_open()) {
77 throw std::runtime_error{
78 "WriteAheadLog::append(): Unable to open file "
79 + std::string(path_) +
"."
82 file << std::to_string(static_cast<int>(record.type));
83 file <<
" " << entryToString(record.entry) <<
"\n";
95 if (!std::filesystem::exists(path_)) {
98 std::ifstream file{path_};
99 if (!file.is_open()) {
100 throw std::runtime_error{
101 "WriteAheadLog::replay(): Unable to open file "
102 + std::string(path_) +
"."
107 while (std::getline(file, line)) {
108 WALRecordType type{std::stoi(line.substr(0))};
109 TimeSeriesEntry<TValue> entry{
110 entryFromString<TValue>(line.substr(line.find(
' ') + 2))
113 case WALRecordType::PUT:
114 lsm_tree.
put(entry.first, entry.second.value(),
false);
116 case WALRecordType::REMOVE:
117 lsm_tree.
remove(entry.first,
false);
132 std::ofstream file{path_};
133 if (!file.is_open()) {
134 throw std::runtime_error{
135 "WriteAheadLog::clear(): Unable to open file "
136 + std::string(path_) +
"."
147 [[nodiscard]] FilePath
path() const noexcept {
LSM tree on TimeSeriesKey.
void put(const key_type &key, const TValue &value, bool log=true)
Put a key-value pair into the LSM tree.
void remove(const key_type &key, bool log=true)
Remove a key pair from the LSM tree.
WriteAheadLog(FilePath lsm_tree_path) noexcept
Construct a new WriteAheadLog object given the path of the LSM tree.
void replay(LSMTree< TValue > &lsm_tree)
Replay the write-ahead log on the LSM tree.
FilePath path() const noexcept
Get the path of the write-ahead log.
WriteAheadLog(WriteAheadLog &&) noexcept=default
Move-construct a WriteAheadLog object.
void append(const WALRecord< TValue > &record)
Append a WAL record to the write-ahead log.
WriteAheadLog()=delete
Deleted default constructor.
void clear()
Clear the write-ahead log.