-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValueTable.cpp
283 lines (208 loc) · 8.05 KB
/
ValueTable.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Created by 唐仁初 on 2022/10/11.
//
#include "ValueTable.h"
namespace mvcc {
ValueTable::ValueTable(int max_level, ValueTable::CleanThreshold threshold) : skipList_(max_level),
buffer_(max_level),
deleted_nums(0),
threshold_(threshold),
status_(0) {
if (threshold == high) {
percent = 0.5;
} else if (threshold == medium) {
percent = 0.3;
} else if (threshold == low) {
percent = 0.15;
} else {
percent = 1;
}
}
ValueTable::Iterator ValueTable::begin() {
return Iterator(skipList_.begin());
}
ValueTable::Iterator ValueTable::end() {
return Iterator(skipList_.end());
}
bool ValueTable::transaction(const std::vector<std::pair<std::string, std::string>> &kvs) {
auto transaction = Coordinator.startTransaction();
// 如果正在压缩则写缓冲区
if (status_ == 1) {
for (auto &kv: kvs) {
Value *value_node = &buffer_[kv.first];
transaction.appendOperation(value_node, kv.second);
}
return transaction.tryCommit();
}
for (auto &kv: kvs) {
Value *value_node = &skipList_[kv.first];
transaction.appendOperation(value_node, kv.second);
}
tryCompact();
return transaction.tryCommit();
}
bool ValueTable::bulkWrite(const std::vector<std::pair<std::string, std::string>> &kvs) {
auto bulk = Coordinator.startBulkWriteOperation();
// 如果正在压缩则写缓冲区
if (status_ == 1) {
for (auto &kv: kvs) {
Value *value_node = &buffer_[kv.first];
bulk.appendOperation(value_node, kv.second);
}
return bulk.run();
}
for (auto &kv: kvs) {
Value *value_node = &skipList_[kv.first];
bulk.appendOperation(value_node, kv.second);
}
tryCompact();
return bulk.run();
}
bool ValueTable::emplace(const std::string &key, const std::string &value) {
return update(key, value);
}
bool ValueTable::update(const std::string &key, const std::string &value) {
if (key.empty())
return false;
// 如果正在开启清理,则写入缓冲区中
if (status_.load() == 1) {
Value *value_node = &buffer_[key];
auto write = Coordinator.startWriteOperation(value_node, value);
bool write_res = write.write();
if (write_res) {
mem_use_.fetch_add(key.size() + value.size());
}
return write_res;
}
Value *value_node = &skipList_[key];
auto write = Coordinator.startWriteOperation(value_node, value);
bool write_res = write.write();
if (write_res) {
mem_use_.fetch_add(key.size() + value.size());
}
tryCompact(); // 写操作时会进行清理检查
return write_res;
}
std::string ValueTable::read(const std::string &key) {
auto it = skipList_.find(key);
if (it == skipList_.end()) {
// 缓冲区无内容,直接返回
if (status_.load() == 0)
return {};
// 如果缓冲区有内容,还需要查找缓冲区
it = buffer_.find(key);
if (it == buffer_.end()) {
return {};
}
}
Value *value_node = &(*it);
auto read = Coordinator.startReadOperation(value_node);
return read.read();
}
void ValueTable::tryCompact() {
if(threshold_ == never)
return ;
auto hl = static_cast<double >(skipList_.size()) * percent;
if(hl < static_cast<double >(deleted_nums))
compact();
}
void ValueTable::clearBuffer() {
std::thread th([this] {
status_.store(2); // 当前事务都重新写入到表中
// 获取最新事务号,等待所有写入事务执行完毕,确保buffer不会更新
auto newest = Coordinator.getNewestVersion();
while (newest > Coordinator.getLowestVersion()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// 等待当前事务提交完毕后,融合缓冲区
skipList_.merge(buffer_);
// 所有数据已经放入主表,不需要再进行读取
status_.store(0);
// 确保所有的读事务都已经结束,然后释放缓冲区
newest = Coordinator.getNewestVersion();
while (newest > Coordinator.getLowestVersion()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
buffer_.clear();
});
th.detach();
}
void ValueTable::compact() {
if (status_.load() != 0)
return;
// 确保缓冲区已经被清空了
if (buffer_.size() != 0) {
return;
}
// 判断是否无活跃事务
if (Coordinator.aliveOperationNum() != 0) {
return;
}
// 锁住表
int ex = 0;
int dt = 1;
// 只能有一个线程锁住表
if (!status_.compare_exchange_weak(ex, dt))
return;
// 判断是否无活跃事务,若无则开始压缩表
if (Coordinator.aliveOperationNum() != 0) {
// 合并缓冲区内容
clearBuffer();
return;
}
// 先压缩表,然后开始合并缓冲区内容
std::thread th([this] {
try{
// 获取表中已删除的个数(不太准确)
size_t deleted = deleted_nums.load();
// 安全清理所有删除掉的节点
skipList_.compact();
status_.store(2); // 当前事务都重新写入到表中
// 获取最新事务号,等待所有写入事务执行完毕,确保buffer不会更新
auto newest = Coordinator.getNewestVersion();
while (newest > Coordinator.getLowestVersion()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// 等待当前事务提交完毕后,融合缓冲区
skipList_.merge(buffer_);
// 更新已删除计数
deleted_nums.fetch_sub(deleted);
// 所有数据已经放入主表,不需要再进行读取
status_.store(0);
// 确保所有的读事务都已经结束,然后释放缓冲区
newest = Coordinator.getNewestVersion();
while (newest > Coordinator.getLowestVersion()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
buffer_.clear();
} catch (...) {
}
});
th.join();
}
bool ValueTable::erase(const std::string &key) {
deleted_nums.fetch_add(1); // 增加删除计数
return skipList_.erase(key);
}
bool ValueTable::exist(const std::string &key) {
return skipList_.find(key) != skipList_.end();
}
ValueTable::Iterator ValueTable::find(const std::string &key) {
auto pos = skipList_.find(key);
if (pos == skipList_.end()){
if (status_.load() == 0)
return end();
pos = buffer_.find(key);
if (pos == buffer_.end()) {
return end();
}
}
return Iterator(pos);
}
size_t ValueTable::size() const {
return skipList_.size();
}
size_t ValueTable::memoryUse() const {
return mem_use_.load();
}
}