-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwal.cpp
More file actions
44 lines (38 loc) · 1.14 KB
/
Copy pathwal.cpp
File metadata and controls
44 lines (38 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "wal.hpp"
#include <sstream>
WAL::WAL(const std::string& filename) : filename_(filename) {
file_.open(filename_, std::ios::app);
}
WAL::~WAL() {
if (file_.is_open()) {
file_.close();
}
}
void WAL::append(const std::string& op, const std::string& key, const std::string& value) {
std::lock_guard<std::mutex> lock(mutex_);
if (file_.is_open()) {
file_ << op << " " << key << " " << value << "\n";
file_.flush();
}
}
void WAL::recover(std::unordered_map<std::string, std::string>& store) {
std::lock_guard<std::mutex> lock(mutex_);
std::ifstream infile(filename_);
std::string line, op, key, value;
while (std::getline(infile, line)) {
std::istringstream iss(line);
if (iss >> op >> key) {
if (op == "SET") {
iss >> value;
store[key] = value;
}
}
}
}
void WAL::truncate() {
std::lock_guard<std::mutex> lock(mutex_);
file_.close();
file_.open(filename_, std::ios::trunc | std::ios::out);
file_.close();
file_.open(filename_, std::ios::app);
}