-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSTable.cpp
96 lines (78 loc) · 2.29 KB
/
SSTable.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "SSTable.hpp"
#include <fstream>
#include <filesystem>
#include <vector>
#include <iostream>
#include <string>
using std::ofstream;
using std::ifstream;
SSTable::SSTable(const SSTableId &id)
: sstbId(id){
}
SSTable::SSTable(const std::map<int, Value> &entries, const SSTableId &id)
: sstbId(id),space(entries.size()){
save(entries);
}
Value SSTable::search(int key) const {
std::map<int, Value> entries = load();
if (entries.count(key)) {
return entries[key];
}
return Value(false);
}
std::map<int, Value> SSTable::load() const {
std::map<int, Value> entries;
ifstream file(sstbId.name());
std::string line, item, op_string, key_str, visible_str,timestamp_str;
while (std::getline(file, line))
{
std::vector<int> items = std::vector<int>();
Value v;
std::stringstream linestream(line);
std::getline(linestream, key_str, ' '); // First argument is a key
std::getline(linestream, visible_str, ' '); // Second argument is if visible
int key = stoi(key_str);
int ifVisible = stoi(visible_str);
if (ifVisible == 0){
v.visible = false;
} else{
v.visible = true;
std::getline(linestream, timestamp_str, ' '); // Third argument is timestamp
unsigned long int t = stoi(timestamp_str);
v.timestamp = t;
while(std::getline(linestream, item, ' '))
{
items.push_back(stoi(item));
}
v.items = items;
}
entries[key] = v;
// std::cout << key << std::endl;
}
return entries;
}
int SSTable::getSpace() const {
return space;
}
void SSTable::remove() const {
std::filesystem::remove(std::filesystem::path(sstbId.name()));
}
uint64_t SSTable::number() const {
return sstbId.no;
}
void SSTable::save(const std::map<int, Value> &entries) {
std::ofstream file(sstbId.name());
for (const auto &i : entries) {
file << i.first << ' ';
if(i.second.visible){
file << 1 << ' '; //Visible == True
} else{
file << 0 << ' ';
}
file << i.second.timestamp << ' ';
for (const auto &j : i.second.items){
file << j<< ' ';
}
file << std::endl;
}
}