-
Notifications
You must be signed in to change notification settings - Fork 24
/
sotest.c
82 lines (67 loc) · 2.47 KB
/
sotest.c
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
/*
* Copyright (c) 2021 Wu, Xingbo <[email protected]>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#include <stdio.h>
#include <unistd.h>
#include "remixdb.h"
int
main(int argc, char ** argv)
{
(void)argc;
(void)argv;
struct xdb * const xdb = remixdb_open("/tmp/xdbdemo", 256, 256); // blockcache=256MB, MemTable=256MB
struct xdb_ref * const ref = remixdb_ref(xdb);
bool r;
r = remixdb_set(ref, "remix", 5, "easy", 4);
printf("remixdb_set remix easy %c\n", r?'T':'F');
r = remixdb_set(ref, "time_travel", 11, "impossible", 10);
printf("remixdb_set time_travel impossible %c\n", r?'T':'F');
r = remixdb_del(ref, "time_travel", 11);
printf("remixdb_del time_travel %c\n", r?'T':'F');
r = remixdb_probe(ref, "time_travel", 11);
printf("remixdb_probe time_travel %c\n", r?'T':'F');
uint32_t klen_out = 0;
char kbuf_out[8] = {};
uint32_t vlen_out = 0;
char vbuf_out[8] = {};
r = remixdb_get(ref, "remix", 5, vbuf_out, &vlen_out);
printf("remixdb_get remix %c %u %.*s\n", r?'T':'F', vlen_out, vlen_out, vbuf_out);
// prepare a few keys for range ops
r = remixdb_set(ref, "00", 2, "0_value", 7);
r = remixdb_set(ref, "11", 2, "1_value", 7);
r = remixdb_set(ref, "22", 2, "2_value", 7);
struct xdb_iter * const iter = remixdb_iter_create(ref);
remixdb_iter_seek(iter, NULL, 0); // seek to the head
printf("remixdb_iter_seek \"\"\n");
while (remixdb_iter_valid(iter)) {
r = remixdb_iter_peek(iter, kbuf_out, &klen_out, vbuf_out, &vlen_out);
if (r) {
printf("remixdb_iter_peek klen=%u key=%.*s vlen=%u value=%.*s\n",
klen_out, klen_out, kbuf_out, vlen_out, vlen_out, vbuf_out);
} else {
printf("ERROR!\n");
}
remixdb_iter_skip(iter, 1);
}
// call iter_park if you will go idle but want to use the iter later
// don't need to call iter_park if you're actively using iter
remixdb_iter_park(iter);
usleep(10);
remixdb_iter_seek(iter, "0", 1);
printf("remixdb_iter_seek \"0\"\n");
// this time we don't want to copy the value
r = remixdb_iter_peek(iter, kbuf_out, &klen_out, NULL, NULL);
if (r){
printf("remixdb_iter_peek klen=%u key=%.*s\n", klen_out, klen_out, kbuf_out);
} else {
printf("ERROR: iter_peek failed\n");
}
remixdb_iter_destroy(iter);
// there must be no active iter when calling unref()
remixdb_unref(ref);
// unsafe operations: should have released all references
remixdb_close(xdb); // destroy also calls clean interally
return 0;
}