-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlavastone.hpp
354 lines (327 loc) · 9.41 KB
/
lavastone.hpp
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
346
347
348
349
350
351
352
353
354
#ifndef __LAVASTONE_HPP__
#define __LAVASTONE_HPP__
#include "lavapack.hpp"
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <initializer_list>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/type_index.hpp>
#ifndef USE_ROCKSDB
#define USE_ROCKSDB
#include "leveldb/db.h"
namespace kvdb = leveldb;
#else
#include "rocksdb/db.h"
namespace kvdb = rocksdb;
#endif
namespace lava {
kvdb::DB *db;
kvdb::Options options;
kvdb::Status s;
std::string path = "./kv";
void demand(bool cond, std::string str) {
if (!(cond)) {
std::cout << str << std::endl;
exit(1);
}
}
void put(const std::string key, const std::string value) {
assert(db->Put(kvdb::WriteOptions(), key, value).ok());
}
void get(const std::string key, std::string &value) {
s = db->Get(kvdb::ReadOptions(), key, &value);
demand(s.ok(), s.ToString() + key);
}
bool check_exists(const std::string key) {
std::string result;
s = db->Get(kvdb::ReadOptions(), key, &result);
if (s.IsNotFound()) {
return false;
} else {
demand(s.ok(), s.ToString());
return true;
}
}
void put_if_not_exists(const std::string key, const std::string value) {
if (!check_exists(key))
put(key, value);
}
template <typename T, typename Tcheck = void> struct Ref;
Ref<size_t> *numids;
template <typename T>
struct Ref<T, typename std::enable_if<std::conjunction<
std::disjunction<std::negation<is_begin<T>>,
std::is_same<T, std::string>>,
std::negation<is_begin_points_first<T>>>::value>::type> {
// key in kvstore
std::string id;
// cast to T type
operator T() const {
std::string result;
get(id, result);
T val;
::Unpack(result, &val);
return val;
}
Ref<T> operator++(int) {
T v = *this;
*this = v + 1;
return v;
}
Ref<T> &operator=(const T &v) {
put(id, ::Pack(&v));
return *this;
}
Ref<T> &operator=(const Ref<T> &v) {
// Guard self assignment
if (this == &v)
return *this;
*this = T(v);
return *this;
}
Ref() {
std::cerr << "initialize literal Ref\n";
// for creating new key / empty ref
// set the Ref's id and then increment
size_t prev_num_ids = *numids;
// I think that
// for some reason when we set size_t prev_num_ids = (*numids)++
// the compiler casts to a size_t and then increments
(*numids)++;
id = ::Pack(&prev_num_ids);
// initialize
*this = T();
}
Ref(const std::string_view id_) { id = id_; }
Ref(size_t id_) { id = ::Pack(&id_); }
};
bool is_initialized = false;
void init() {
options.create_if_missing = true;
s = kvdb::DB::Open(options, path, &db);
demand(s.ok(), s.ToString());
numids = new Ref<size_t>("");
std::string result;
s = db->Get(kvdb::ReadOptions(), "", &result);
if (s.IsNotFound()) {
std::cout << "initializing lava::numids = 0\n";
*numids = 0;
} else {
demand(s.ok(), s.ToString());
std::cout << "found existing lava::numids = " << *numids << "\n";
}
is_initialized = true;
}
// Ref to lavavector
template <template <typename, typename...> typename Collection, typename T,
typename... Args>
// do not treat Ref as container
struct Ref<Collection<T, Args...>,
typename std::enable_if<!std::disjunction<
std::is_same<Collection<T>, Ref<T>>,
std::is_same<Collection<T>, std::string>,
// must have .begin() method
is_not_begin<Collection<T>>,
is_begin_points_first<Collection<T>>>::value>::type> {
// key in kvstore
std::string id;
Ref<size_t> len;
// to avoid unnecessary disk access and wasting top-level kv ids, we must
// include the len member in the constructor intializer list which avoids
// default construction w/o arguments that would auto-select a key
Ref() : len{""} {
// for creating new key / empty ref
// set id and increment
demand(lava::is_initialized,
"must run lava::init() before declaring disk-backed refs");
size_t prev_num_ids = *numids;
(*numids)++;
id = Pack(&prev_num_ids);
len.id = id;
len = 0;
}
Ref(size_t id_) : len{""} {
demand(lava::is_initialized,
"must run lava::init() before declaring disk-backed refs");
id = Pack(&id_);
len.id = id;
// if not exists, initialize the len
size_t zero = 0;
put_if_not_exists(len.id, Pack(&zero));
}
Ref(std::string id_) : id{id_}, len{id_} {
// if not exists, initialize the len
demand(lava::is_initialized,
"must run lava::init() before declaring disk-backed refs");
size_t zero = 0;
put_if_not_exists(len.id, Pack(&zero));
}
Ref<T> operator[](const size_t &i) { return Ref<T>(id + Pack(&i)); }
Ref<T> at(const size_t &i) {
assert(i < len);
return (*this)[i];
}
operator Collection<T, Args...>() {
Collection<T, Args...> val;
for (size_t i = 0; i < len; i++) {
T elem = this->at(i);
val.insert(val.end(), elem);
}
return val;
}
Ref<Collection<T, Args...>> &operator=(const Collection<T, Args...> &v) {
len = 0;
for (auto elem : v)
push_back(elem);
return *this;
}
Ref<Collection<T, Args...>> &operator=(const Ref<Collection<T, Args...>> &v) {
// Guard self assignment
if (this == &v)
return *this;
*this = Collection<T, Args...>(v);
return *this;
}
size_t size() { return len; }
void push_back(const T &val) {
size_t i = len;
(*this)[i] = val;
len++;
}
void insert(const T &val) { this->push_back(val); }
};
bool startswith(const std::string &s, const std::string &start) {
auto mm = std::mismatch(start.begin(), start.end(), s.begin(), s.end());
if (mm.first == start.end()) {
return true;
}
return false;
}
// Ref to lavamap
template <template <typename, typename, typename...> typename Mapping,
typename T1, typename T2, typename... Args>
struct Ref<Mapping<T1, T2, Args...>,
typename std::enable_if<
is_begin_points_first<Mapping<T1, T2, Args...>>::value>::type> {
// key in kvstore
std::string id;
Ref<size_t> len;
Ref<T2> operator[](const T1 &k) {
std::string key = id + Pack(&k);
// increment for new keys
if (!check_exists(key)) {
Ref<T2> r(key);
r = T2();
len++;
}
return Ref<T2>(key);
}
int count(const T1 &k) { return check_exists(id + Pack(&k)); }
Ref<T2> at(const T1 &k) {
// check this key stores a value (either the T2 value packed or the len for
// a lavamap/lavavector)
assert(check_exists(id + Pack(&k)));
return (*this)[k];
}
operator Mapping<T1, T2, Args...>() const {
Mapping<T1, T2, Args...> val;
// Get a database iterator
std::shared_ptr<kvdb::Iterator> db_iter(
db->NewIterator(kvdb::ReadOptions()));
// seek to prefix
db_iter->Seek(id);
// skip this ref's len
db_iter->Next();
std::unordered_set<std::string> seen_ids;
while (db_iter->Valid()) {
std::string key_str = db_iter->key().ToString();
// halt iteration when outside ref id prefix
if (!startswith(key_str, id)) {
// we are past the prefix range
break;
}
bool seen = false;
for (auto s : seen_ids) {
// could seek as an optimization
if (startswith(key_str, s)) {
seen = true;
break;
}
}
if (seen) {
// we have already seen this id
db_iter->Next();
continue;
}
seen_ids.insert(key_str);
// trim id from key
std::string map_key_str = key_str.substr(id.length());
T1 key;
::Unpack(map_key_str, &key);
// set the key-value
val[key] = Ref<T2>(key_str);
db_iter->Next();
}
return val;
}
Ref<Mapping<T1, T2, Args...>> &operator=(const Mapping<T1, T2, Args...> &v) {
len = 0;
for (auto it : v)
(*this)[it.first] = it.second;
return *this;
}
Ref<Mapping<T1, T2, Args...>> &
operator=(const Ref<Mapping<T1, T2, Args...>> &v) {
// Guard self assignment
if (this == &v)
return *this;
*this = Mapping<T1, T2, Args...>(v);
return *this;
}
size_t size() { return len; }
// to avoid unnecessary disk access and wasting top-level kv ids, we must
// include the len member in the constructor intializer list which avoids
// default construction w/o arguments that would auto-select a key
Ref() : len{""} {
// for creating new key / empty ref
demand(lava::is_initialized,
"must run lava::init() before declaring disk-backed refs");
// set id and increment
size_t prev_num_ids = *numids;
(*numids)++;
id = Pack(&prev_num_ids);
len.id = id;
len = 0;
}
Ref(size_t id_) : len{""} {
demand(lava::is_initialized,
"must run lava::init() before declaring disk-backed refs");
id = Pack(&id_);
len.id = id;
// if not exists, initialize the len
size_t zero = 0;
put_if_not_exists(len.id, Pack(&zero));
}
Ref(std::string id_) : id{id_}, len{id_} {
demand(lava::is_initialized,
"must run lava::init() before declaring disk-backed refs");
// if not exists, initialize the len
size_t zero = 0;
put_if_not_exists(len.id, Pack(&zero));
}
};
} // namespace lava
#endif