-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlog.cc
345 lines (330 loc) · 9.26 KB
/
vlog.cc
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
334
335
336
337
338
339
340
341
342
343
344
345
#include "vlog.h"
#include "type.h"
#include "utils.h"
#include <cerrno>
#include <fstream>
#include <iostream>
#include <vector>
const u8 vLogs::magic = 0xff;
/**
* @brief locate back if failed, read to next magic and do some check
* @param ifs
* @param ve
* @return -1 if failed, 0 if success
*/
int read_a_ventry(std::ifstream &ifs, vEntry &ve) {
// read bytes
TMagic magic;
TCheckSum checksum;
TKey key;
TLen vlen;
TValue value;
auto back_loc = ifs.tellg();
ifs.read(reinterpret_cast<char *>(&magic), sizeof(magic));
if (magic != vLogs::magic) {
ifs.seekg(back_loc);
return -1;
}
// ...
ifs.read(reinterpret_cast<char *>(&checksum), sizeof(checksum));
ifs.read(reinterpret_cast<char *>(&key), sizeof(key));
ifs.read(reinterpret_cast<char *>(&vlen), sizeof(vlen));
// HINT: an incorrect vlen can cause a crash here
// SO USE next magic to act as end symbol
// TBytes data(vlen);
std::vector<u8> data;
unsigned char byte;
ifs.read(reinterpret_cast<char *>(&byte), sizeof(byte));
while (byte != vLogs::magic && !ifs.eof() && !ifs.fail()) {
data.push_back(byte);
ifs.read(reinterpret_cast<char *>(&byte), sizeof(byte));
}
if (data.size() != vlen || (ifs.fail() && !ifs.eof())) {
ifs.seekg(back_loc);
return -1;
}
if (byte == vLogs::magic) {
// HINT: set the ifs back 1
ifs.seekg(-1, std::ios_base::cur);
}
TValue ret(data.begin(), data.end());
ve.magic = magic;
ve.checksum = checksum;
ve.key = key;
ve.vlen = vlen;
ve.vvalue = ret;
return 0;
}
vLogs::vLogs(const TPath &vpath) : vfilepath(vpath) {
// HINT: magic number is used for find the head(because the head and the
// tail won't be persistent), 0x7f HINT: checksum is the crc16 value
// calculated by {key, vlen, vvalue} HINT: saved key for gc check if can
// open file
head = 0;
tail = 0;
if (!std::filesystem::exists(vpath)) {
std::ofstream ofs(vpath, std::ios::binary);
ofs.close();
}
std::ifstream ifs(vpath, std::ios::binary | std::ios::ate);
if (ifs.is_open()) {
ifs.close();
}
if (ifs.tellg() != 0) {
reload_mem();
}
}
vLogs::~vLogs() = default;
/**
@brief add a value entry to vlog (not change the vlog if the entry is marked
deleted)
* @param v
* @param sync if sync to vlog file immediately
* @return TOff the write location in file
*/
TOff vLogs::addVlog(const vEntryProps &v) {
// HINT: add a new vEntry to the ves
// HINT: update the tail
std::fstream fs(vfilepath, std::ios::binary | std::ios::app);
if (v.vlen == 0) {
// deleted element
return fs.tellg();
}
TCheckSum checksum;
TBytes bytes = cal_bytes(v, checksum);
// std::printf("Checksum of v is: %x\n", checksum);
fs.write(reinterpret_cast<const char *>(bytes.data()), bytes.size());
// sync with file
fs.flush();
fs.close();
u64 ret = head;
head += bytes.size(); // head 在前面,gc从tail开始
return ret;
}
/**
@brief the tail will be set to the begin the first magic
*/
void vLogs::relocTail() {
std::ifstream ifs(vfilepath, std::ios::binary);
if (!ifs.is_open()) {
Log("relocTail: Failed to open file");
return;
}
u64 tmp = utils::seek_data_block(vfilepath);
ifs.seekg(tmp);
unsigned char byte;
vEntry ve;
TCheckSum cal_checksum;
auto back_loc = ifs.tellg();
auto last_loc = ifs.tellg();
ifs.read(reinterpret_cast<char *>(&byte), sizeof(byte));
int suc = -1;
// check the checksum
while (suc != 0) {
while (byte != vLogs::magic && !ifs.eof() && !ifs.fail()) {
ifs.read(reinterpret_cast<char *>(&byte), sizeof(byte));
}
if (ifs.eof() || ifs.fail()) {
Log("relocTail: incorrect offset");
ifs.seekg(back_loc);
return;
}
ifs.seekg(-1, std::ios_base::cur);
last_loc = ifs.tellg();
suc = read_a_ventry(ifs, ve);
cal_bytes(ve, cal_checksum);
if (suc && cal_checksum == ve.checksum) {
break;
}
}
tail = last_loc;
std::cout << "reloc to " << tail << std::endl;
}
/**
@brief read the first vEntry from vlog
* @param offset
* @param ve
*/
void vLogs::readVlog(TOff offset, vEntry &ve) {
relocTail();
// Now the tail is set to the begin of the first valid entry
std::ifstream ifs(vfilepath, std::ios::binary);
if (!ifs.is_open()) {
std::cerr << "In readVlog: Failed to open file" << std::endl;
ve = type::ve_not_found;
}
ifs.seekg(tail);
int suc = read_a_ventry(ifs, ve);
if (suc == -1) {
std::cerr << "readVlog: incorrect offset" << std::endl;
ifs.close();
return;
}
ifs.close();
}
/**
@brief read vEntrys util the read bytes reach the chunk_size
* @param offset
* @param ves should be empty
* @param chunk_size
*/
u64 vLogs::readVlogs(TOff offset, vEntrys &ves, u64 chunk_size,
std::vector<TOff> &locs) {
relocTail();
// Now the tail is set to the begin of the first valid entry
std::ifstream ifs(vfilepath, std::ios::binary);
if (!ifs.is_open()) {
Log("In readVlog: Failed to open file");
return 0;
}
vEntry ve;
ifs.seekg(tail);
locs.push_back(tail);
auto begin = ifs.tellg();
u64 size = 0;
while (size < chunk_size && size + begin < head) {
int suc = read_a_ventry(ifs, ve);
if (suc == -1) {
Log("readVlogs: incorrect offset");
ifs.close();
return 0;
}
ves.push_back(ve);
auto cur = ifs.tellg();
if (ifs.eof()) {
ifs.clear();
cur = ifs.seekg(0, std::ios::end).tellg();
}
// ATTENTION:when read to the end of file, cur's offset will be -1
Assert((cur != (uint64_t)(-1)), "Uncaught error in readVlogs");
size = cur - begin;
locs.push_back(cur);
}
// tail = size + begin;
ifs.close();
return size;
}
/**
@brief
* @param v vlog entry / entryProps
* @param checksum crc16
* @return TBytes the bytes generated by vlog entry to write
*/
TBytes vLogs::cal_bytes(const vEntryProps &v, TCheckSum &checksum) {
vEntry new_entry;
TKey key = v.key;
TLen vlen = v.vlen;
TValue vvalue = v.vvalue;
new_entry.key = key;
new_entry.vlen = vlen;
new_entry.vvalue = vvalue;
TBytes bytes;
bytes.push_back(magic);
constexpr static int keysize = sizeof(decltype(key));
constexpr static int lensize = sizeof(decltype(vlen));
std::vector<u8> data;
const u8 mask = 0xff;
for (int i = 0; i < keysize; ++i) {
// little-endian
u8 byte = (key >> (8 * i)) & mask;
data.push_back(byte);
}
for (int i = 0; i < lensize; ++i) {
// little-endian
u8 byte = (vlen >> (8 * i)) & mask;
data.push_back(byte);
}
data.insert(data.end(), vvalue.begin(), vvalue.end());
checksum = utils::crc16(data);
for (int i = 0; i < sizeof(checksum); ++i) {
// little-endian
u8 byte = (checksum >> (8 * i)) & mask;
bytes.push_back(byte);
}
bytes.insert(bytes.end(), data.begin(), data.end());
return bytes;
}
/**
@brief
* @param v vlog entry / entryProps
* @param checksum crc16
* @return TBytes the bytes generated by vlog entry to write
*/
inline TBytes vLogs::cal_bytes(const vEntry &v, TCheckSum &checksum) {
return cal_bytes({v.key, v.vlen, v.vvalue}, checksum);
}
/**
@brief clear all(mem, disk, param)
*/
void vLogs::clear() {
ves.clear();
head = 0;
tail = 0;
if (!std::filesystem::exists(vfilepath)) {
return;
}
std::ofstream ofs(vfilepath, std::ios::binary | std::ios::trunc);
ofs.close();
}
void vLogs::clear_mem() {
ves.clear();
head = 0;
tail = 0;
}
void vLogs::reload_mem() {
std::ifstream ifs(vfilepath,
std::ios::binary | std::ios::ate); // move to end
if (!ifs.is_open()) {
int errCode = errno; // get the errno
const char *errMsg =
std::strerror(errCode); // convert errno to error message
Log("Failed to open file, vpath=%s, error=%s", vfilepath.c_str(),
errMsg);
head = 0;
tail = 0;
return;
}
head = ifs.tellg();
if (head == 0) {
tail = 0;
return;
}
this->relocTail();
ifs.close();
}
/**
@brief
* @param ke key, off, len
* @return TValue "" if fail or deleted. Otherwise, return the value
*/
TValue vLogs::query(kEntry ke) {
if (ke.offset > head || ke.offset < tail || ke.len == 0) {
return "";
}
std::ifstream ifs(vfilepath, std::ios::binary);
if (!ifs.is_open()) [[unlikely]] {
return "";
}
ifs.seekg(ke.offset);
if (ifs.fail()) {
ifs.clear();
ifs.close();
return "";
}
vEntry ve;
int success = read_a_ventry(ifs, ve);
ifs.close();
if (success == -1)
return "";
return ve.vvalue;
}
void vLogs::gc(u64 new_tail) {
// HINT
if (new_tail <= tail) {
Log("Error vlog gc: new_tail <= tail");
return;
}
utils::de_alloc_file(vfilepath, tail, new_tail - tail);
std::cout << "gc done, new tail is " << new_tail << std::endl;
tail = new_tail;
}