vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
write_ahead_log.h
1#ifndef STORAGE_WRITE_AHEAD_LOG_H
2#define STORAGE_WRITE_AHEAD_LOG_H
3
4#include <vkdb/lsm_tree.h>
5#include <vkdb/wal_lsm.h>
6
7namespace vkdb {
12const FilePath WAL_FILENAME{"wal.log"};
13
19template <ArithmeticNoCVRefQuals TValue>
21public:
26 WriteAheadLog() = delete;
27
34 explicit WriteAheadLog(FilePath lsm_tree_path) noexcept
35 : path_{lsm_tree_path / WAL_FILENAME} {}
36
41 WriteAheadLog(WriteAheadLog&&) noexcept = default;
42
47 WriteAheadLog& operator=(WriteAheadLog&&) noexcept = default;
48
53 WriteAheadLog(const WriteAheadLog&) = delete;
54
59 WriteAheadLog& operator=(const WriteAheadLog&) = delete;
60
65 ~WriteAheadLog() noexcept = default;
66
74 void append(const WALRecord<TValue>& record) {
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_) + "."
80 };
81 }
82 file << std::to_string(static_cast<int>(record.type));
83 file << " " << entryToString(record.entry) << "\n";
84 file.close();
85 }
86
94 void replay(LSMTree<TValue>& lsm_tree) {
95 if (!std::filesystem::exists(path_)) {
96 return;
97 }
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_) + "."
103 };
104 }
105
106 std::string line;
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))
111 };
112 switch (type) {
113 case WALRecordType::PUT:
114 lsm_tree.put(entry.first, entry.second.value(), false);
115 break;
116 case WALRecordType::REMOVE:
117 lsm_tree.remove(entry.first, false);
118 break;
119 }
120 }
121
122 file.close();
123 clear();
124 }
125
131 void clear() {
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_) + "."
137 };
138 }
139 file.close();
140 }
141
147 [[nodiscard]] FilePath path() const noexcept {
148 return path_;
149 }
150
151private:
156 FilePath path_;
157};
158} // namespace vkdb
159
160#endif // STORAGE_WRITE_AHEAD_LOG_H
LSM tree on TimeSeriesKey.
Definition wal_lsm.h:8
void put(const key_type &key, const TValue &value, bool log=true)
Put a key-value pair into the LSM tree.
Definition lsm_tree.h:108
void remove(const key_type &key, bool log=true)
Remove a key pair from the LSM tree.
Definition lsm_tree.h:128
Write-ahead log.
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.
Represents a WAL record.
Definition wal_lsm.h:28