1 #include "bmbase.c"
2 #include <kclangc.h>
3
4 #define DEFAULT_DB "kyc_bench.kct"
5
6 typedef struct BM_KYC {
7 KCDB *db;
8 } BM_KYC;
9
env_setup()10 static void env_setup() {
11 fprintf(stderr, " engine: KyotoCabinet %s\n", KCVERSION);
12 }
13
db_size_bytes(BMCTX * ctx)14 uint64_t db_size_bytes(BMCTX *ctx) {
15 const char *path = bm.param_db ? bm.param_db : DEFAULT_DB;
16 IWP_FILE_STAT fst;
17 iwrc rc = iwp_fstat(path, &fst);
18 if (rc) {
19 iwlog_ecode_error3(rc);
20 return 0;
21 }
22 return fst.size;
23 }
24
db_open(BMCTX * ctx)25 static void *db_open(BMCTX *ctx) {
26 if (ctx->db) {
27 return 0; // db is not closed properly
28 }
29 bool wal_enabled = false;
30 for (int i = 0; i < bm.argc; ++i) {
31 if (!strcmp(bm.argv[i], "-w")) {
32 wal_enabled = true;
33 }
34 }
35 const char *path = bm.param_db ? bm.param_db : DEFAULT_DB;
36 BM_KYC *bmdb = malloc(sizeof(*bmdb));
37 bmdb->db = kcdbnew();
38 uint32_t mode = KCOWRITER | KCOCREATE;
39 if (ctx->freshdb) {
40 mode |= KCOTRUNCATE;
41 }
42 if (wal_enabled) {
43 mode |= KCOAUTOTRAN;
44 }
45 if (!kcdbopen(bmdb->db, path, mode)) {
46 kcdbdel(bmdb->db);
47 free(bmdb);
48 return 0;
49 }
50 return bmdb;
51 }
52
db_close(BMCTX * ctx)53 static bool db_close(BMCTX *ctx) {
54 if (!ctx->db) {
55 return false;
56 }
57 BM_KYC *bmdb = ctx->db;
58 int32_t rc = kcdbclose(bmdb->db);
59 if (!rc) {
60 fprintf(stderr, "db_close: %s\n", kcdbemsg(bmdb->db));
61 return false;
62 }
63 kcdbdel(bmdb->db);
64 free(bmdb);
65 return true;
66 }
67
db_put(BMCTX * ctx,const IWKV_val * key,const IWKV_val * val,bool sync)68 static bool db_put(BMCTX *ctx, const IWKV_val *key, const IWKV_val *val, bool sync) {
69 BM_KYC *bmdb = ctx->db;
70 int32_t rc = kcdbset(bmdb->db, key->data, key->size, val->data, val->size);
71 if (!rc) {
72 fprintf(stderr, "db_put: %s\n", kcdbemsg(bmdb->db));
73 return false;
74 }
75 if (sync) {
76 rc = kcdbsync(bmdb->db, true, 0, 0);
77 if (!rc) {
78 fprintf(stderr, "db_put:kcdbsync: %s\n", kcdbemsg(bmdb->db));
79 return false;
80 }
81 }
82 return true;
83 }
84
db_get(BMCTX * ctx,const IWKV_val * key,IWKV_val * val,bool * found)85 static bool db_get(BMCTX *ctx, const IWKV_val *key, IWKV_val *val, bool *found) {
86 BM_KYC *bmdb = ctx->db;
87 size_t sp = 0;
88 char *vbuf = kcdbget(bmdb->db, key->data, key->size, &sp);
89 val->data = vbuf;
90 val->size = sp;
91 *found = (val->data != 0);
92 return true;
93 }
94
db_del(BMCTX * ctx,const IWKV_val * key,bool sync)95 static bool db_del(BMCTX *ctx, const IWKV_val *key, bool sync) {
96 BM_KYC *bmdb = ctx->db;
97 kcdbremove(bmdb->db, key->data, key->size);
98 if (sync) {
99 int32_t rc = kcdbsync(bmdb->db, true, 0, 0);
100 if (!rc) {
101 fprintf(stderr, "db_del:kcdbsync: %s\n", kcdbemsg(bmdb->db));
102 return false;
103 }
104 }
105 return true;
106 }
107
db_read_seq(BMCTX * ctx,bool reverse)108 static bool db_read_seq(BMCTX *ctx, bool reverse) {
109 int32_t rc = true;
110 BM_KYC *bmdb = ctx->db;
111 KCCUR *cur = kcdbcursor(bmdb->db);
112 if (!cur) {
113 return false;
114 }
115 if (reverse) {
116 rc = kccurjumpback(cur);
117 } else {
118 rc = kccurjump(cur);
119 }
120 if (!rc) {
121 if (kcdbecode(bmdb->db) == KCENOREC) {
122 kccurdel(cur);
123 return true;
124 }
125 }
126 if (!rc) {
127 fprintf(stderr, "db_read_seq: %s\n", kcdbemsg(bmdb->db));
128 kccurdel(cur);
129 return false;
130 }
131 for (int i = 0; i < bm.param_num && rc; ++i) {
132 if (reverse) {
133 rc = kccurstepback(cur);
134 } else {
135 rc = kccurstep(cur);
136 }
137 }
138 if (kcdbecode(bmdb->db) != KCENOREC) {
139 fprintf(stderr, "db_read_seq: %s\n", kcdbemsg(bmdb->db));
140 }
141 kccurdel(cur);
142 return true;
143 }
144
db_cursor_to_key(BMCTX * ctx,const IWKV_val * key,IWKV_val * val,bool * found)145 static bool db_cursor_to_key(BMCTX *ctx, const IWKV_val *key, IWKV_val *val, bool *found) {
146 int32_t rc = true;
147 BM_KYC *bmdb = ctx->db;
148 KCCUR *cur = kcdbcursor(bmdb->db);
149 *found = false;
150 rc = kccurjumpkey(cur, key->data, key->size);
151 if (rc) {
152 *found = true;
153 }
154 if (!rc && (kcdbecode(bmdb->db) != KCENOREC)) {
155 fprintf(stderr, "db_cursor_to_key: %s\n", kcdbemsg(bmdb->db));
156 kccurdel(cur);
157 return false;
158 }
159 val->data = kccurgetvalue(cur, &val->size, 0);
160 kccurdel(cur);
161 return true;
162 }
163
main(int argc,char ** argv)164 int main(int argc, char **argv) {
165 if (argc < 1) {
166 return -1;
167 }
168 g_program = argv[0];
169 bm.env_setup = env_setup;
170 bm.db_size_bytes = db_size_bytes;
171 bm.val_free = kcfree;
172 bm.db_open = db_open;
173 bm.db_close = db_close;
174 bm.db_put = db_put;
175 bm.db_get = db_get;
176 bm.db_del = db_del;
177 bm.db_read_seq = db_read_seq;
178 bm.db_cursor_to_key = db_cursor_to_key;
179 if (!bm_bench_run(argc, argv)) {
180 return 1;
181 }
182 return 0;
183 }
184