-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSTable.hpp
More file actions
250 lines (193 loc) · 6.03 KB
/
SSTable.hpp
File metadata and controls
250 lines (193 loc) · 6.03 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
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
#pragma once
#ifndef SSTABLE_HPP
#define SSTABLE_HPP
#include <map>
#include <string>
#include <fstream>
#include <iostream>
#include <set>
#include <io.h>
using namespace std;
#define DEFAULT_MAX_SIZE 1024 * 1024 * 2 // 2 MB
typedef map<uint64_t, string> MAP_DATA;
typedef map<uint64_t, uint64_t> MAP_INDEX;
class SSTable
{
private:
string dir;
uint64_t size = 0;
uint64_t max_size = DEFAULT_MAX_SIZE;
uint64_t min_key = UINT64_MAX;
uint64_t max_key = 0;
uint64_t index_offset = 0;
MAP_INDEX imap;
public:
SSTable(){}
SSTable(string path){SET_DIR_PATH(path);}
~SSTable(){}
public:
void SET_MAX_SIZE(uint64_t msize){
this->max_size = msize;
}
void SET_DIR_PATH(const string path){
this->dir = path;
// if file existed, load its infomation
if(_access(dir.c_str(), 0) != -1){
ifstream infile(dir, ios::binary);
infile.seekg(-8, ios::end);
infile.read((char*)&index_offset, 8);
// load index map into memory
infile.seekg(index_offset, ios::beg);
uint64_t tmpkey, tmpofst;
while(!infile.eof()){
infile.read((char*)&tmpkey, 8);
infile.read((char*)&tmpofst, 8);
if(tmpkey == index_offset)
break;
min_key = (min_key < tmpkey) ? min_key : tmpkey;
max_key = (max_key > tmpkey) ? max_key : tmpkey;
imap.insert(pair<uint64_t, uint64_t>(tmpkey, tmpofst));
}
infile.close();
}
}
uint64_t GET_MIN_KEY(){ return min_key; }
uint64_t GET_MAX_KEY(){ return max_key; }
uint64_t GET_SIZE(){ return size; }
uint64_t GET_MAX_SIZE(){ return max_size; }
void WRITE_TO_DIR(MAP_DATA &table){
min_key = UINT64_MAX;
max_key = 0;
index_offset = 0;
imap.clear();
size = 0;
ofstream outfile(dir, ios::binary);
if(!outfile)
return;
// MAP_INDEX index;
uint64_t offset = 0; // keys' offsets
uint64_t key;
string tmpdata = "";
MAP_DATA::iterator iter = table.begin();
while(iter != table.end()){
key = iter->first;
min_key = (min_key < key) ? min_key : key;
max_key = (max_key > key) ? max_key : key;
imap.insert(pair<uint64_t, uint64_t>(key, offset));
outfile.write((char*)&key, 8);
tmpdata = iter->second;
offset += (8 + tmpdata.length());
outfile.write(tmpdata.c_str(), sizeof(char) * tmpdata.size());
MAP_DATA::iterator tmp = iter;
iter++;
table.erase(tmp);
// make sure the size of a sstable near 2 MB
size += (sizeof(char) * tmpdata.size());
if(size >= max_size)
break;
}
this->index_offset = offset;
MAP_INDEX::iterator it = imap.begin();
uint64_t tmpkey, tmpoft;
while(it != imap.end()){
tmpkey = it->first;
tmpoft = it->second;
outfile.write((char*)&tmpkey, 8);
outfile.write((char*)&tmpoft, 8);
it++;
}
outfile.write((char*)&index_offset, 8);
outfile.close();
}
string GET(uint64_t key, bool &flag){
flag = false;
if(key < min_key || key > max_key)
return "";
uint64_t tmpkey, offset;
MAP_INDEX::iterator iter = imap.begin();
while (iter != imap.end())
{
if(iter->first == key)
break;
iter++;
}
if(iter == imap.end())
return "";
offset = iter->second;
offset += sizeof(uint64_t);
iter++;
uint64_t nextoft = (iter == imap.end()) ? index_offset : iter->second;
if(nextoft == offset){
flag = true;
return "";
}
ifstream kfile(dir, ios::binary);
char ch;
string res = "";
uint64_t count = 0;
while(count < nextoft){
kfile.read(&ch, 1);
if(count >= offset)
res.push_back(ch);
count++;
}
kfile.close();
flag = true;
return res;
}
// compaction helpers
void INDEX_TO_MAP(MAP_INDEX &index){
ifstream infile(dir, ios::binary);
infile.seekg(index_offset, ios::beg);
uint64_t tmpkey, tmpoft;
while(!infile.eof()){
infile.read((char*)&tmpkey, 8);
infile.read((char*)&tmpoft, 8);
index.insert(pair<uint64_t, uint64_t>(tmpkey, tmpoft));
}
infile.close();
}
void KV_TO_MAP(MAP_DATA &table){
MAP_INDEX::iterator iter = imap.begin();
uint64_t offset, nextoft, key;
ifstream infile(dir, ios::binary);
char ch;
string res = "";
uint64_t count = 0;
while(iter != imap.end()){
infile.read((char*)&key, 8);
offset = 8 + iter->second;
iter++;
if(iter == imap.end())
break;
nextoft = iter->second;
while(count < nextoft - offset){
infile.read(&ch, 1);
res.push_back(ch);
count++;
}
table.insert(pair<uint64_t, string>(key, res));
res = "";
count = 0;
}
// last kv-pair
while(count < index_offset - offset){
infile.read(&ch, 1);
res.push_back(ch);
count++;
}
table.insert(pair<uint64_t, string>(key, res));
infile.close();
RESET();
}
void RESET(){
min_key = UINT64_MAX;
max_key = 0;
index_offset = 0;
size = 0;
imap.clear();
if(_access(dir.c_str(), 0) == 0)
remove(dir.c_str());
}
};
#endif // SSTable_hpp