-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSTable.cpp
333 lines (279 loc) · 8.94 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "SSTable.h"
void SSTable::toSSTable(const SkipList &memTable, const string &fileName,
uint64_t timeStamp) {
ofstream out(fileName, ios::out | ios::binary | ios::trunc);
if (out.fail())
throw runtime_error("toSSTable(SkipList): open file " + fileName +
" failed!");
auto length = memTable.getLength();
auto min = memTable.getMinKey();
auto max = memTable.getMaxKey();
// Header
write64(out, timeStamp);
write64(out, length);
write64(out, min);
write64(out, max);
// Bloom Filter
BloomFilter bloomFilter(memTable);
bloomFilter.write(out);
// Key, Offset
auto i = memTable.constBegin();
uint32_t offset = 0;
while (i.hasNext()) {
i.next();
write64(out, i.key());
write32(out, offset);
offset += i.value().size();
}
write64(out, 0);
write32(out, offset);
// Value
i = memTable.constBegin();
while (i.hasNext()) {
i.next();
out.write(i.value().c_str(), i.value().size());
}
out.close();
}
SSTableHeader SSTable::readHeader(const string &fileName) {
ifstream in(fileName, ios_base::in | ios_base::binary);
if (in.fail())
throw runtime_error("readHeader: Open file " + fileName + " failed!");
uint64_t timeStamp, length, minKey, maxKey;
read64(in, timeStamp);
read64(in, length);
read64(in, minKey);
read64(in, maxKey);
in.close();
return SSTableHeader(timeStamp, length, minKey, maxKey);
}
void SSTable::readDic(const string &fileName, SSTableDic &dic) {
ifstream in(fileName, ios_base::in | ios_base::binary);
if (in.fail())
throw runtime_error("readDic: Open file " + fileName + " failed!");
uint64_t length;
in.seekg(8, ifstream::beg); // skip timeStamp
read64(in, length);
in.seekg(10272, ifstream::beg); // skip header and bloomFilter
// read keys and offsets
vector<uint64_t> keys;
vector<int> offsets;
uint64_t key;
int offset;
for (uint64_t i = 0; i < length; i++) {
read64(in, key);
read32(in, offset);
keys.push_back(key);
offsets.push_back(offset);
}
read64(in, key);
read32(in, offset);
offsets.push_back(offset);
// read values
for (uint64_t i = 0; i < length; i++) {
auto valueSize = offsets[i + 1] - offsets[i];
char *buf = new char[valueSize + 1];
in.read(buf, valueSize);
buf[valueSize] = '\0';
string value(buf);
delete[] buf;
dic.emplace_back(keys[i], value);
}
in.close();
}
void SSTable::toSSTable(const SSTableDic &dic, const string &fileName,
uint64_t timeStamp) {
ofstream out(fileName, ios::out | ios::binary | ios::trunc);
if (out.fail())
throw runtime_error("toSSTable(dic): Open file " + fileName + " failed!");
auto length = dic.size();
auto min = dic[0].first;
auto max = dic.back().first;
// Header
write64(out, timeStamp);
write64(out, length);
write64(out, min);
write64(out, max);
// BloomFilter
BloomFilter bloomFilter(dic);
bloomFilter.write(out);
// Key, Offset
uint32_t offset = 0;
for (const auto &pair : dic) {
write64(out, pair.first);
write32(out, offset);
offset += pair.second.size();
}
write64(out, 0);
write32(out, offset);
// Value
for (const auto &pair : dic) {
out.write(pair.second.c_str(), (long)pair.second.size());
}
out.close();
}
void SSTable::write64(ofstream &out, uint64_t n) { out.write((char *)&n, 8); }
void SSTable::write32(ofstream &out, uint32_t n) { out.write((char *)&n, 4); }
void SSTable::read64(ifstream &in, uint64_t &n) { in.read((char *)&n, 8); }
void SSTable::read32(ifstream &in, int &n) { in.read((char *)&n, 4); }
string SSTable::get(const string &fileName, uint64_t key) {
ifstream in(fileName, ios_base::in | ios_base::binary);
if (in.fail())
throw runtime_error("readDic: Open file " + fileName + " failed!");
in.seekg(8, ifstream::beg); // skip timeStamp
uint64_t length;
read64(in, length);
in.seekg(10272, ifstream::beg); // skip header and bloomFilter
int offset = -1;
int valueSize = -1;
for (uint64_t i = 0; i < length; i++) {
uint64_t _key;
int _offset;
read64(in, _key);
read32(in, _offset);
if (_key == key) {
offset = _offset;
read64(in, _key);
read32(in, _offset);
valueSize = _offset - offset;
break;
}
}
if (offset < 0) return "";
in.seekg(10272 + (length + 1) * 12 + offset, ifstream::beg);
char *buf = new char[valueSize + 1];
in.read(buf, valueSize);
buf[valueSize] = '\0';
string value(buf);
delete[] buf;
in.close();
return value;
}
SSTableHeader::SSTableHeader(uint64_t _timeStamp, uint64_t _size,
uint64_t _minKey, uint64_t _maxKey)
: timeStamp(_timeStamp), length(_size), minKey(_minKey), maxKey(_maxKey) {}
SSTableHeader::SSTableHeader() = default;
SSTableHeader::SSTableHeader(const SSTableHeader &other) = default;
BloomFilter::BloomFilter(const SkipList &memTable) {
auto i = memTable.constBegin();
while (i.hasNext()) {
i.next();
unsigned int hash[4] = {0};
MurmurHash3_x64_128(&i.key(), sizeof(i.key()), 1, &hash);
bits[hash[0] % (10240 * 8)] = true;
bits[hash[1] % (10240 * 8)] = true;
bits[hash[2] % (10240 * 8)] = true;
bits[hash[3] % (10240 * 8)] = true;
}
}
void BloomFilter::write(ofstream &out) {
out.seekp(32, std::ofstream::beg);
for (int i = 0; i < 10240 * 8; i += 8) {
char temp = false;
for (int j = 0; j <= 7; j++)
temp |=
bits[i + j] << j; // NOLINT(cppcoreguidelines-narrowing-conversions)
out.write(&temp, sizeof(temp));
}
}
BloomFilter::BloomFilter(const SSTableDic &dic) {
for (const auto &pair : dic) {
unsigned int hash[4] = {0};
MurmurHash3_x64_128(&pair.first, sizeof(pair.first), 1, &hash);
bits[hash[0] % (10240 * 8)] = true;
bits[hash[1] % (10240 * 8)] = true;
bits[hash[2] % (10240 * 8)] = true;
bits[hash[3] % (10240 * 8)] = true;
}
}
BloomFilter::BloomFilter(ifstream &in) {
in.seekg(32, ifstream::beg);
char buffer[10240];
in.read(buffer, 10240);
int p = 0;
for (char c : buffer) {
for (int i = 0; i < 8; i++) bits[p++] = (c >> i) & 1;
}
}
bool BloomFilter::exists(uint64_t key) const {
unsigned int hash[4] = {0};
MurmurHash3_x64_128(&key, sizeof(key), 1, &hash);
return bits[hash[0] % (10240 * 8)] && bits[hash[1] % (10240 * 8)] &&
bits[hash[2] % (10240 * 8)] && bits[hash[3] % (10240 * 8)];
}
BloomFilter::BloomFilter() = default;
SSTableCache::SSTableCache(const SkipList &memTable, uint64_t timeStamp,
string _fileName)
: bloomFilter(memTable), fileName(std::move(_fileName)) {
header.length = memTable.getLength();
header.minKey = memTable.getMinKey();
header.maxKey = memTable.getMaxKey();
header.timeStamp = timeStamp;
auto i = memTable.constBegin();
uint32_t offset = 0;
while (i.hasNext()) {
i.next();
index.emplace_back(i.key(), offset);
offset += i.value().size();
}
index.emplace_back(0, offset);
}
SSTableCache::SSTableCache(const SSTableDic &dic, uint64_t timeStamp,
string _fileName)
: bloomFilter(dic), fileName(std::move(_fileName)) {
header.length = dic.size();
header.minKey = dic.front().first;
header.maxKey = dic.back().first;
header.timeStamp = timeStamp;
uint32_t offset = 0;
for (const auto &pair : dic) {
index.emplace_back(pair.first, offset);
offset += pair.second.size();
}
index.emplace_back(0, offset);
}
string SSTableCache::get(uint64_t key) const {
if (!(header.minKey <= key && key <= header.maxKey)) return "";
if (!bloomFilter.exists(key)) return "";
auto cmp = [](SSTableIndex left, uint64_t key) { return left.first < key; };
auto low = lower_bound(index.begin(), index.end() - 1, key, cmp);
if (low->first != key) return "";
ifstream in(fileName, ios_base::in | ios_base::binary);
if (in.fail())
throw runtime_error("readDic: Open file " + fileName + " failed!");
auto offset = low->second;
low++;
int valueSize;
valueSize = low->second - offset;
in.seekg(10272 + (header.length + 1) * 12 + offset, ifstream::beg);
char *buf = new char[valueSize + 1];
in.read(buf, valueSize);
buf[valueSize] = '\0';
string value(buf);
delete[] buf;
in.close();
return value;
}
SSTableHeader SSTableCache::getHeader() const { return header; }
SSTableCache::SSTableCache(string _fileName) : fileName(std::move(_fileName)) {
ifstream in(fileName, ios_base::in | ios_base::binary);
if (in.fail())
throw runtime_error("readHeader: Open file " + fileName + " failed!");
SSTable::read64(in, header.timeStamp);
SSTable::read64(in, header.length);
SSTable::read64(in, header.minKey);
SSTable::read64(in, header.maxKey);
bloomFilter = BloomFilter(in);
in.seekg(32 + 10240, ifstream::beg);
uint64_t key;
int offset;
for (uint64_t i = 0; i < header.length; i++) {
SSTable::read64(in, key);
SSTable::read32(in, offset);
index.emplace_back(key, offset);
}
SSTable::read64(in, key);
SSTable::read32(in, offset);
index.emplace_back(key, offset);
in.close();
}