-
Notifications
You must be signed in to change notification settings - Fork 24
/
sst.h
519 lines (364 loc) · 13.3 KB
/
sst.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* Copyright (c) 2016--2021 Wu, Xingbo <[email protected]>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#pragma once
#include "blkio.h"
#ifdef __cplusplus
extern "C" {
#endif
#define SST_VLEN_TS ((0x10000u)) // tomb stone
#define SST_VLEN_MASK ((0xffffu)) // real vlen == vlen & 0xffff
// kv {{{
extern size_t
sst_kv_vi128_estimate(const struct kv * const kv);
extern u8 *
sst_kv_vi128_encode(u8 * ptr, const struct kv * const kv);
extern size_t
sst_kv_size(const struct kv * const kv);
extern struct kv *
sst_kvref_dup2_kv(struct kvref * const kvref, struct kv * const out);
// }}} kv
// mm {{{
extern struct kv *
kvmap_mm_in_ts(struct kv * const kv, void * const priv);
extern struct kv *
kvmap_mm_out_ts(struct kv * const kv, struct kv * const out);
extern const struct kvmap_mm kvmap_mm_ts;
// }}} mm
// sst {{{
struct sst;
extern struct sst *
sst_open(const char * const dirname, const u64 seq, const u32 way);
extern const struct sst_meta *
sst_meta(struct sst * const sst);
extern void
sst_rcache(struct sst * const sst, struct rcache * const rc);
extern struct kv *
sst_get(struct sst * const map, const struct kref * const key, struct kv * const out);
extern bool
sst_probe(struct sst* const map, const struct kref * const key);
extern struct kv *
sst_first_key(struct sst * const map, struct kv * const out);
extern struct kv *
sst_last_key(struct sst * const map, struct kv * const out);
extern void
sst_destroy(struct sst * const map);
extern void
sst_dump(struct sst * const sst, const char * const fn);
extern void
sst_fprint(struct sst * const map, FILE * const out);
struct sst_iter;
extern struct sst_iter *
sst_iter_create(struct sst * const sst);
extern bool
sst_iter_ts(struct sst_iter * const iter);
extern void
sst_iter_seek(struct sst_iter * const iter, const struct kref * const key);
extern void
sst_iter_seek_null(struct sst_iter * const iter);
extern bool
sst_iter_valid(struct sst_iter * const iter);
extern struct kv *
sst_iter_peek(struct sst_iter * const iter, struct kv * const out);
extern bool
sst_iter_kref(struct sst_iter * const iter, struct kref * const kref);
extern bool
sst_iter_kvref(struct sst_iter * const iter, struct kvref * const kvref);
extern u64
sst_iter_retain(struct sst_iter * const iter);
extern void
sst_iter_release(struct sst_iter * const iter, const u64 opaque);
extern void
sst_iter_skip1(struct sst_iter * const iter);
extern void
sst_iter_skip(struct sst_iter * const iter, const u32 nr);
extern struct kv *
sst_iter_next(struct sst_iter * const iter, struct kv * const out);
extern void
sst_iter_park(struct sst_iter * const iter);
u64
sst_iter_retain(struct sst_iter * const iter);
void
sst_iter_release(struct sst_iter * const iter, const u64 opaque);
extern void
sst_iter_destroy(struct sst_iter * const iter);
// }}} sst
// build-sst {{{
// api contains sorted keys and supports iter_next().
// all keys in the map_api will be added to the sstable.
extern u64
sst_build(const char * const dirname, struct miter * const miter,
const u64 seq, const u32 way, const u32 maxblkid0, const bool del, const bool ckeys,
const struct kv * const k0, const struct kv * const kz);
// }}} build-sst
// msstx {{{
// msst (multi-sst)
struct msst;
struct msstx_iter;
// msstx
extern struct msst *
msstx_open(const char * const dirname, const u64 seq, const u32 nway);
extern void
msst_rcache(struct msst * const msst, struct rcache * const rc);
extern void
msstx_destroy(struct msst * const msst);
extern struct msstx_iter *
msstx_iter_create(struct msst * const msst);
extern struct kv *
msstx_get(struct msst * const msst, const struct kref * const key, struct kv * const out);
extern bool
msstx_probe(struct msst * const msst, const struct kref * const key);
extern bool
msstx_iter_valid(struct msstx_iter * const iter);
extern void
msstx_iter_seek(struct msstx_iter * const iter, const struct kref * const key);
extern void
msstx_iter_seek_null(struct msstx_iter * const iter);
extern struct kv *
msstx_iter_peek(struct msstx_iter * const iter, struct kv * const out);
extern bool
msstx_iter_kref(struct msstx_iter * const iter, struct kref * const kref);
extern bool
msstx_iter_kvref(struct msstx_iter * const iter, struct kvref * const kvref);
extern u64
msstx_iter_retain(struct msstx_iter * const iter);
extern void
msstx_iter_release(struct msstx_iter * const iter, const u64 opaque);
extern void
msstx_iter_skip1(struct msstx_iter * const iter);
extern void
msstx_iter_skip(struct msstx_iter * const iter, const u32 nr);
extern struct kv *
msstx_iter_next(struct msstx_iter * const iter, struct kv * const out);
extern void
msstx_iter_park(struct msstx_iter * const iter);
extern void
msstx_iter_destroy(struct msstx_iter * const iter);
// }}} msstx
// ssty {{{
struct ssty;
extern struct ssty *
ssty_open(const char * const dirname, const u64 seq, const u32 nway);
extern void
ssty_destroy(struct ssty * const ssty);
extern void
ssty_fprint(struct ssty * const ssty, FILE * const fout);
// }}} ssty
// mssty {{{
struct mssty_ref;
struct mssty_iter;
extern bool
mssty_open_y(const char * const dirname, struct msst * const msst);
extern struct msst *
mssty_open(const char * const dirname, const u64 seq, const u32 nway);
extern void
mssty_destroy(struct msst * const msst);
extern void
mssty_fprint(struct msst * const msst, FILE * const fout);
extern struct mssty_ref *
mssty_ref(struct msst * const msst);
extern struct msst *
mssty_unref(struct mssty_ref * const ref);
extern struct mssty_iter *
mssty_iter_create(struct mssty_ref * const ref);
extern bool
mssty_iter_valid(struct mssty_iter * const iter);
extern void
mssty_iter_seek(struct mssty_iter * const iter, const struct kref * const key);
extern void
mssty_iter_seek_null(struct mssty_iter * const iter);
extern void
mssty_iter_seek_near(struct mssty_iter * const iter, const struct kref * const key, const bool bsearch_keys);
extern struct kv *
mssty_iter_peek(struct mssty_iter * const iter, struct kv * const out);
extern bool
mssty_iter_kref(struct mssty_iter * const iter, struct kref * const kref);
extern bool
mssty_iter_kvref(struct mssty_iter * const iter, struct kvref * const kvref);
extern u64
mssty_iter_retain(struct mssty_iter * const iter);
extern void
mssty_iter_release(struct mssty_iter * const iter, const u64 opaque);
extern void
mssty_iter_skip1(struct mssty_iter * const iter);
extern void
mssty_iter_skip(struct mssty_iter * const iter, const u32 nr);
extern struct kv *
mssty_iter_next(struct mssty_iter * const iter, struct kv * const out);
extern void
mssty_iter_park(struct mssty_iter * const iter);
extern void
mssty_iter_destroy(struct mssty_iter * const iter);
// ts iter: ignore a key if its newest version is a tombstone
extern bool
mssty_iter_ts(struct mssty_iter * const iter);
extern void
mssty_iter_seek_ts(struct mssty_iter * const iter, const struct kref * const key);
extern void
mssty_iter_skip1_ts(struct mssty_iter * const iter);
extern void
mssty_iter_skip_ts(struct mssty_iter * const iter, const u32 nr);
extern struct kv *
mssty_iter_next_ts(struct mssty_iter * const iter, struct kv * const out);
// dup iter: return all versions, including old keys and tombstones
extern struct kv *
mssty_iter_peek_dup(struct mssty_iter * const iter, struct kv * const out);
extern void
mssty_iter_skip1_dup(struct mssty_iter * const iter);
extern void
mssty_iter_skip_dup(struct mssty_iter * const iter, const u32 nr);
extern struct kv *
mssty_iter_next_dup(struct mssty_iter * const iter, struct kv * const out);
extern bool
mssty_iter_kref_dup(struct mssty_iter * const iter, struct kref * const kref);
extern bool
mssty_iter_kvref_dup(struct mssty_iter * const iter, struct kvref * const kvref);
// mssty_get can return tombstone
extern struct kv *
mssty_get(struct mssty_ref * const ref, const struct kref * const key, struct kv * const out);
// mssty_probe can return tombstone
extern bool
mssty_probe(struct mssty_ref * const ref, const struct kref * const key);
// return NULL for tomestone
extern struct kv *
mssty_get_ts(struct mssty_ref * const ref, const struct kref * const key, struct kv * const out);
// return false for tomestone
extern bool
mssty_probe_ts(struct mssty_ref * const ref, const struct kref * const key);
extern bool
mssty_get_value_ts(struct mssty_ref * const ref, const struct kref * const key,
void * const vbuf_out, u32 * const vlen_out);
extern struct kv *
mssty_first(struct msst * const msst, struct kv * const out);
extern struct kv *
mssty_last(struct msst * const msst, struct kv * const out);
extern void
mssty_dump(struct msst * const msst, const char * const fn);
// }}} mssty
// build-ssty {{{
// build extended metadata based on a set of sstables.
// y0 and way0 are optional for speeding up the sorting
extern u32
ssty_build(const char * const dirname, struct msst * const msst,
const u64 seq, const u32 way, struct msst * const y0, const u32 way0, const bool tags);
// }}} build-ssty
// msstv {{{
struct msstv;
struct msstv_iter;
struct msstv_ref;
extern struct msstv *
msstv_create(const u64 nslots, const u64 version);
extern void
msstv_append(struct msstv * const v, struct msst * const msst, const struct kv * const anchor);
extern void
msstv_rcache(struct msstv * const v, struct rcache * const rc);
extern void
msstv_destroy(struct msstv * const v);
extern struct msstv *
msstv_open(const char * const dirname, const char * const filename);
extern struct msstv *
msstv_open_version(const char * const dirname, const u64 version);
extern struct msstv_ref *
msstv_ref(struct msstv * const v);
extern struct msstv *
msstv_unref(struct msstv_ref * const ref);
extern struct kv *
msstv_get(struct msstv_ref * const ref, const struct kref * const key, struct kv * const out);
extern bool
msstv_probe(struct msstv_ref * const ref, const struct kref * const key);
// return NULL for tomestone
extern struct kv *
msstv_get_ts(struct msstv_ref * const ref, const struct kref * const key, struct kv * const out);
// return false for tomestone
extern bool
msstv_probe_ts(struct msstv_ref * const ref, const struct kref * const key);
extern bool
msstv_get_value_ts(struct msstv_ref * const ref, const struct kref * const key,
void * const vbuf_out, u32 * const vlen_out);
extern struct msstv_iter *
msstv_iter_create(struct msstv_ref * const ref);
extern bool
msstv_iter_valid(struct msstv_iter * const vi);
extern void
msstv_iter_seek(struct msstv_iter * const vi, const struct kref * const key);
extern struct kv *
msstv_iter_peek(struct msstv_iter * const vi, struct kv * const out);
extern bool
msstv_iter_kref(struct msstv_iter * const vi, struct kref * const kref);
extern bool
msstv_iter_kvref(struct msstv_iter * const vi, struct kvref * const kvref);
extern u64
msstv_iter_retain(struct msstv_iter * const vi);
extern void
msstv_iter_release(struct msstv_iter * const vi, const u64 opaque);
extern void
msstv_iter_skip1(struct msstv_iter * const vi);
extern void
msstv_iter_skip(struct msstv_iter * const vi, const u32 nr);
extern struct kv *
msstv_iter_next(struct msstv_iter * const vi, struct kv * const out);
extern void
msstv_iter_park(struct msstv_iter * const vi);
extern bool
msstv_iter_ts(struct msstv_iter * const vi);
extern void
msstv_iter_seek_ts(struct msstv_iter * const vi, const struct kref * const key);
extern void
msstv_iter_skip1_ts(struct msstv_iter * const vi);
extern void
msstv_iter_skip_ts(struct msstv_iter * const vi, const u32 nr);
extern struct kv *
msstv_iter_next_ts(struct msstv_iter * const vi, struct kv * const out);
extern void
msstv_fprint(struct msstv * const v, FILE * const out);
extern void
msstv_iter_destroy(struct msstv_iter * const vi);
// UNSAFE!
// return the anchors of msstv terminated with NULL
// the returned pointer should be freed after use
// must use when holding a msstv
// anchor->vlen: 0: accepted; 1: rejected
extern struct kv **
msstv_anchors(struct msstv * const v);
// }}} msstv
// msstz {{{
struct msstz;
extern struct msstz *
msstz_open(const char * const dirname, const u64 cache_size_mb, const bool ckeys, const bool tags);
extern void
msstz_destroy(struct msstz * const z);
extern int
msstz_logfd(struct msstz * const z);
// return number of bytes written since opened
extern u64
msstz_stat_writes(struct msstz * const z);
extern u64
msstz_stat_reads(struct msstz * const z);
// default is 0
extern void
msstz_set_minsz(struct msstz * const z, const u64 minsz);
extern u64
msstz_version(struct msstz * const z);
extern struct msstv *
msstz_getv(struct msstz * const z);
extern void
msstz_putv(struct msstz * const z, struct msstv * const v);
typedef void (*msstz_range_cb)(void * priv, const bool accepted, const struct kv * k0, const struct kv * kz);
extern void
msstz_comp(struct msstz * const z, const struct kvmap_api * const api1, void * const map1,
const u32 nr_workers, const u32 co_per_worker, const u64 max_reject);
// }}} msstz
// api {{{
extern const struct kvmap_api kvmap_api_sst;
extern const struct kvmap_api kvmap_api_msstx;
extern const struct kvmap_api kvmap_api_mssty;
extern const struct kvmap_api kvmap_api_mssty_ts;
extern const struct kvmap_api kvmap_api_msstv;
extern const struct kvmap_api kvmap_api_msstv_ts;
// }}} api
#ifdef __cplusplus
}
#endif
// vim:fdm=marker