-
Notifications
You must be signed in to change notification settings - Fork 0
/
peekSSTable.cpp
91 lines (76 loc) · 1.99 KB
/
peekSSTable.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
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "utils.h"
int v;
using namespace std;
using SSTableDic = vector<pair<uint64_t, string>>;
void read64(ifstream &in, uint64_t &n) { in.read((char *)&n, 8); }
void read32(ifstream &in, int &n) { in.read((char *)&n, 4); }
void peek(string fileName) {
ifstream in(fileName, ios_base::in | ios_base::binary);
if (in.fail())
throw runtime_error("readDic: Open file " + fileName + " failed!");
in.seekg(0, in.end);
auto size = in.tellg();
in.seekg(0, in.beg);
uint64_t timeStamp, length, minKey, maxKey;
read64(in, timeStamp);
read64(in, length);
read64(in, minKey);
read64(in, maxKey);
in.seekg(32 + 10240, in.beg);
// read keys and offsets
vector<uint64_t> keys;
vector<int> offsets;
uint64_t key;
int offset;
for (int 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);
keys.push_back(key);
offsets.push_back(offset);
SSTableDic dic;
// read values
for (int i = 0; i < length; i++) {
string value;
auto valueSize = offsets[i + 1] - offsets[i];
value.reserve(offsets[i + 1] - offsets[i]);
while (valueSize--) {
char c;
in.read(&c, 1);
value.push_back(c);
}
dic.emplace_back(keys[i], value);
}
cout << ">>>>> File <<<<<" << endl;
cout << fileName << endl;
cout << "TimeStamp: " << timeStamp << endl
<< "Length: " << length << endl
<< "minKey: " << minKey << endl
<< "maxKey: " << maxKey << endl
<< endl;
if (v) {
for (auto pair : dic) {
cout << pair.first << ": " << pair.second.size() << endl;
}
}
in.close();
}
int main() {
int level;
cout << "Input level: ";
cin >> level;
cout << "Verbose?: ";
cin >> v;
vector<string> files;
string path = "./data/level-" + to_string(level);
utils::scanDir(path, files);
for (auto file : files) peek(path + "/" + file);
}