-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLsmTree.cpp
More file actions
78 lines (73 loc) · 2.25 KB
/
LsmTree.cpp
File metadata and controls
78 lines (73 loc) · 2.25 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
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
#include "LsmTree.h"
std::string LsmTree::insert(std::string &value) {
std::string key = getNewKey();
logFile << "insert " << key << " " << value << std::endl;
bloomFilter -> add(key);
dataMemoryUsed += (key.length() + value.length());
data[key] = value;
if(dataMemoryUsed > bufferSizeForUpdate) {
logFile << "update sortedStringTable" << std::endl;
sortetStringTable -> update(data, updates, deleted);
dataMemoryUsed = 0;
}
return key;
}
std::string LsmTree::get(std::string &key) {
std::string result = "";
logFile << "get " << key << std::endl;
if(!bloomFilter -> count(key) || deleted.count(key))
return "";
if(data.count(key))
return data[key];
if(updates.count(key))
return updates[key];
return sortetStringTable -> get(key);
}
void LsmTree::update(std::string &key, std::string &value) {
logFile << "update " << key << " " << value << "\n";
if(!bloomFilter -> count(key))
return;
if(data.count(key))
data[key] = value;
else {
if(sortetStringTable -> get(key).empty())
return;
updates[key] = value;
dataMemoryUsed += (key.length() + value.length());
if(dataMemoryUsed > bufferSizeForUpdate) {
logFile << "update sortedStringTable" << std::endl;
sortetStringTable -> update(data, updates, deleted);
dataMemoryUsed = 0;
}
}
}
std::string LsmTree::getNewKey() {
std::string result = "";
for(int i = 0; i < keyLength; i++)
{
int next = rand() % 36;
if(next < 26)
result += ('a' + next);
else
result += ('0' + next - 26);
}
if(bloomFilter -> count(result))
return getNewKey();
return result;
}
void LsmTree::erase(std::string &key) {
logFile << "erase " << key << std::endl;
if(!bloomFilter -> count(key))
return;
if(data.count(key))
data.erase(key);
else {
deleted.insert(key);
dataMemoryUsed += (key.length());
if(dataMemoryUsed > bufferSizeForUpdate) {
logFile << "update sortedStringTable" << std::endl;
sortetStringTable -> update(data, updates, deleted);
dataMemoryUsed = 0;
}
}
}