forked from stephentu/silo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
txn_btree.h
483 lines (424 loc) · 12.4 KB
/
txn_btree.h
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#ifndef _NDB_TXN_BTREE_H_
#define _NDB_TXN_BTREE_H_
#include "base_txn_btree.h"
// XXX: hacky
extern void txn_btree_test();
struct txn_btree_ {
class key_reader {
public:
inline ALWAYS_INLINE const std::string &
operator()(const std::string &s)
{
return s;
}
#if NDB_MASSTREE
inline ALWAYS_INLINE lcdf::Str operator()(lcdf::Str s) {
return s;
}
#endif
};
class key_writer {
public:
constexpr key_writer(const std::string *k)
: k(k) {}
template <typename StringAllocator>
inline const std::string *
fully_materialize(bool stable_input, StringAllocator &sa)
{
if (stable_input || !k)
return k;
std::string * const ret = sa();
ret->assign(k->data(), k->size());
return ret;
}
private:
const std::string *k;
};
// does not bother to interpret the bytes from a record
class single_value_reader {
public:
typedef std::string value_type;
constexpr single_value_reader(std::string *px, size_t max_bytes_read)
: px(px), max_bytes_read(max_bytes_read) {}
template <typename StringAllocator>
inline bool
operator()(const uint8_t *data, size_t sz, StringAllocator &sa)
{
const size_t readsz = std::min(sz, max_bytes_read);
px->assign((const char *) data, readsz);
return true;
}
inline std::string &
results()
{
return *px;
}
inline const std::string &
results() const
{
return *px;
}
template <typename StringAllocator>
inline void
dup(const std::string &vdup, StringAllocator &sa)
{
*px = vdup;
}
private:
std::string *px;
size_t max_bytes_read;
};
class value_reader {
public:
typedef std::string value_type;
constexpr value_reader(size_t max_bytes_read)
: px(nullptr), max_bytes_read(max_bytes_read) {}
template <typename StringAllocator>
inline bool
operator()(const uint8_t *data, size_t sz, StringAllocator &sa)
{
px = sa();
const size_t readsz = std::min(sz, max_bytes_read);
px->assign((const char *) data, readsz);
return true;
}
inline std::string &
results()
{
return *px;
}
inline const std::string &
results() const
{
return *px;
}
template <typename StringAllocator>
inline void
dup(const std::string &vdup, StringAllocator &sa)
{
px = sa();
*px = vdup;
}
private:
std::string *px;
size_t max_bytes_read;
};
class value_writer {
public:
constexpr value_writer(const std::string *v) : v(v) {}
inline size_t
compute_needed(const uint8_t *buf, size_t sz)
{
return v ? v->size() : 0;
}
template <typename StringAllocator>
inline const std::string *
fully_materialize(bool stable_input, StringAllocator &sa)
{
if (stable_input || !v)
return v;
std::string * const ret = sa();
ret->assign(v->data(), v->size());
return ret;
}
// input [buf, buf+sz) is old value
inline void
operator()(uint8_t *buf, size_t sz)
{
if (!v)
return;
NDB_MEMCPY(buf, v->data(), v->size());
}
private:
const std::string *v;
};
static size_t
tuple_writer(dbtuple::TupleWriterMode mode, const void *v, uint8_t *p, size_t sz)
{
const std::string * const vx = reinterpret_cast<const std::string *>(v);
switch (mode) {
case dbtuple::TUPLE_WRITER_NEEDS_OLD_VALUE:
return 0;
case dbtuple::TUPLE_WRITER_COMPUTE_NEEDED:
case dbtuple::TUPLE_WRITER_COMPUTE_DELTA_NEEDED:
return vx->size();
case dbtuple::TUPLE_WRITER_DO_WRITE:
case dbtuple::TUPLE_WRITER_DO_DELTA_WRITE:
NDB_MEMCPY(p, vx->data(), vx->size());
return 0;
}
ALWAYS_ASSERT(false);
return 0;
}
typedef std::string Key;
typedef key_reader KeyReader;
typedef key_writer KeyWriter;
typedef std::string Value;
typedef single_value_reader SingleValueReader;
typedef value_reader ValueReader;
typedef value_writer ValueWriter;
};
/**
* This class implements a serializable, multi-version b-tree
*
* It presents mostly same interface as the underlying concurrent b-tree,
* but the interface is serializable. The main interface differences are,
* insert() and put() do not return a boolean value to indicate whether or not
* they caused the tree to mutate
*
* A txn_btree does not allow keys to map to NULL records, even though the
* underlying concurrent btree does- this simplifies some of the book-keeping
* Additionally, keys cannot map to zero length records.
*
* Note that the txn_btree *manages* the memory of both keys and values internally.
* See the specific notes on search()/insert() about memory ownership
*/
template <template <typename> class Transaction>
class txn_btree : public base_txn_btree<Transaction, txn_btree_> {
typedef base_txn_btree<Transaction, txn_btree_> super_type;
public:
//template <typename Traits>
//struct transaction {
// typedef Transaction<txn_btree_, Traits> type;
//};
//template <typename Traits>
// using transaction = Transaction<txn_btree_, Traits>;
typedef typename super_type::string_type string_type;
typedef typename super_type::keystring_type keystring_type;
typedef typename super_type::size_type size_type;
typedef txn_btree_::Key key_type;
typedef txn_btree_::Value value_type;
typedef txn_btree_::KeyReader key_reader_type;
typedef txn_btree_::KeyWriter key_writer_type;
typedef txn_btree_::SingleValueReader single_value_reader_type;
typedef txn_btree_::ValueReader value_reader_type;
typedef txn_btree_::ValueWriter value_writer_type;
struct search_range_callback {
public:
virtual ~search_range_callback() {}
virtual bool invoke(const keystring_type &k, const string_type &v) = 0;
};
private:
template <typename T>
class type_callback_wrapper : public search_range_callback {
public:
constexpr type_callback_wrapper(T *callback)
: callback(callback) {}
virtual bool
invoke(const keystring_type &k, const string_type &v)
{
return callback->operator()(k, v);
}
private:
T *const callback;
};
static inline ALWAYS_INLINE string_type
to_string_type(const varkey &k)
{
return string_type((const char *) k.data(), k.size());
}
template <typename Traits>
static inline const std::string *
stablize(Transaction<Traits> &t, const std::string &s)
{
if (Traits::stable_input_memory)
return &s;
std::string * const px = t.string_allocator()();
*px = s;
return px;
}
template <typename Traits>
static inline const std::string *
stablize(Transaction<Traits> &t, const uint8_t *p, size_t sz)
{
if (!sz)
return nullptr;
std::string * const px = t.string_allocator()();
px->assign((const char *) p, sz);
return px;
}
template <typename Traits>
static inline const std::string *
stablize(Transaction<Traits> &t, const varkey &k)
{
return stablize(t, k.data(), k.size());
}
public:
txn_btree(size_type value_size_hint = 128,
bool mostly_append = false,
const std::string &name = "<unknown>")
: super_type(value_size_hint, mostly_append, name)
{}
template <typename Traits>
inline bool
search(Transaction<Traits> &t,
const varkey &k,
value_type &v,
size_t max_bytes_read = string_type::npos)
{
return search(t, to_string_type(k), v, max_bytes_read);
}
// either returns false or v is set to not-empty with value
// precondition: max_bytes_read > 0
template <typename Traits>
inline bool
search(Transaction<Traits> &t,
const key_type &k,
value_type &v,
size_type max_bytes_read = string_type::npos)
{
single_value_reader_type r(&v, max_bytes_read);
return this->do_search(t, k, r);
}
template <typename Traits>
inline void
search_range_call(Transaction<Traits> &t,
const key_type &lower,
const key_type *upper,
search_range_callback &callback,
size_type max_bytes_read = string_type::npos)
{
key_reader_type kr;
value_reader_type vr(max_bytes_read);
this->do_search_range_call(t, lower, upper, callback, kr, vr);
}
template <typename Traits>
inline void
rsearch_range_call(Transaction<Traits> &t,
const key_type &upper,
const key_type *lower,
search_range_callback &callback,
size_type max_bytes_read = string_type::npos)
{
key_reader_type kr;
value_reader_type vr(max_bytes_read);
this->do_rsearch_range_call(t, upper, lower, callback, kr, vr);
}
template <typename Traits>
inline void
search_range_call(Transaction<Traits> &t,
const varkey &lower,
const varkey *upper,
search_range_callback &callback,
size_type max_bytes_read = string_type::npos)
{
key_type u;
if (upper)
u = to_string_type(*upper);
search_range_call(t, to_string_type(lower),
upper ? &u : nullptr, callback, max_bytes_read);
}
template <typename Traits>
inline void
rsearch_range_call(Transaction<Traits> &t,
const varkey &upper,
const varkey *lower,
search_range_callback &callback,
size_type max_bytes_read = string_type::npos)
{
key_type l;
if (lower)
l = to_string_type(*lower);
rsearch_range_call(t, to_string_type(upper),
lower ? &l : nullptr, callback, max_bytes_read);
}
template <typename Traits, typename T>
inline void
search_range(Transaction<Traits> &t,
const key_type &lower,
const key_type *upper,
T &callback,
size_type max_bytes_read = string_type::npos)
{
type_callback_wrapper<T> w(&callback);
search_range_call(t, lower, upper, w, max_bytes_read);
}
template <typename Traits, typename T>
inline void
search_range(Transaction<Traits> &t,
const varkey &lower,
const varkey *upper,
T &callback,
size_type max_bytes_read = string_type::npos)
{
key_type u;
if (upper)
u = to_string_type(*upper);
search_range(t, to_string_type(lower),
upper ? &u : nullptr, callback, max_bytes_read);
}
template <typename Traits>
inline void
put(Transaction<Traits> &t, const key_type &k, const value_type &v)
{
INVARIANT(!v.empty());
this->do_tree_put(
t, stablize(t, k), stablize(t, v),
txn_btree_::tuple_writer, false);
}
template <typename Traits>
inline void
put(Transaction<Traits> &t, const varkey &k, const value_type &v)
{
INVARIANT(!v.empty());
this->do_tree_put(
t, stablize(t, k), stablize(t, v),
txn_btree_::tuple_writer, false);
}
template <typename Traits>
inline void
insert(Transaction<Traits> &t, const key_type &k, const value_type &v)
{
INVARIANT(!v.empty());
this->do_tree_put(
t, stablize(t, k), stablize(t, v),
txn_btree_::tuple_writer, true);
}
// insert() methods below are for legacy use
template <typename Traits>
inline void
insert(Transaction<Traits> &t, const key_type &k, const uint8_t *v, size_type sz)
{
INVARIANT(v);
INVARIANT(sz);
this->do_tree_put(
t, stablize(t, k), stablize(t, v, sz),
txn_btree_::tuple_writer, true);
}
template <typename Traits>
inline void
insert(Transaction<Traits> &t, const varkey &k, const uint8_t *v, size_type sz)
{
INVARIANT(v);
INVARIANT(sz);
this->do_tree_put(
t, stablize(t, k), stablize(t, v, sz),
txn_btree_::tuple_writer, true);
}
template <typename Traits, typename T>
inline void
insert_object(Transaction<Traits> &t, const varkey &k, const T &obj)
{
insert(t, k, (const uint8_t *) &obj, sizeof(obj));
}
template <typename Traits, typename T>
inline void
insert_object(Transaction<Traits> &t, const key_type &k, const T &obj)
{
insert(t, k, (const uint8_t *) &obj, sizeof(obj));
}
template <typename Traits>
inline void
remove(Transaction<Traits> &t, const key_type &k)
{
this->do_tree_put(t, stablize(t, k), nullptr, txn_btree_::tuple_writer, false);
}
template <typename Traits>
inline void
remove(Transaction<Traits> &t, const varkey &k)
{
this->do_tree_put(t, stablize(t, k), nullptr, txn_btree_::tuple_writer, false);
}
static void Test();
};
#endif /* _NDB_TXN_BTREE_H_ */