-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqhash.c
481 lines (397 loc) · 11.1 KB
/
qhash.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
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
#include "./include/qhash.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
DB_TXN *txnid;
typedef struct hash_cursor gen_iter_t(unsigned hd, void *key);
typedef void gen_del_t(unsigned hd, void *key);
typedef int gen_vdel_t(unsigned hd, void *key, void *value);
typedef void print_t(void *value);
typedef void gen_put_t(void *key, void *value);
typedef int gen_get_t(unsigned hd, void *value, void *key);
typedef int gen_cond_t(unsigned qhds_n, unsigned is_value);
int u_gen_get(unsigned hd, void *value, void *key) {
return uhash_get(hd, value, * (unsigned *) key);
}
int s_gen_get(unsigned hd, void *value, void *key) {
return shash_get(hd, value, key);
}
int s_gen_pget(unsigned hd, void *value, void *key) {
return shash_pget(hd, value, key);
}
int u_gen_pget(unsigned hd, void *value, void *key) {
return uhash_pget(hd, value, * (unsigned *) key);
}
struct hdpair {
unsigned phd, hd[2];
} aux_hdp, prim;
typedef struct {
print_t *value_print;
gen_iter_t *iter;
gen_del_t *del;
gen_vdel_t *vdel;
gen_put_t *put;
gen_get_t *get;
gen_cond_t *cond;
} gen_t;
char value_buf[BUFSIZ], key_buf[BUFSIZ], *col;
unsigned qhds, ahds, qhds_n = 0, ahds_n = 0;
unsigned mode = 0, reverse = 0, tmprev_q, tmprev_a, bail = 0;
void u_print(void *value) {
printf("%u", * (unsigned *) value);
}
void s_print(void *value) {
printf("%s", (char *) value);
}
struct hash_cursor u_iter(unsigned hd, void *key) {
return hash_iter(hd, key, sizeof(unsigned));
}
struct hash_cursor s_iter(unsigned hd, void *key) {
return hash_iter(hd, key, key ? strlen(key) + 1 : 0);
}
void m0_u_del(unsigned hd, void *key) {
lhash_del(prim.phd, * (unsigned *) key);
}
void u_del(unsigned hd, void *key) {
uhash_del(hd, * (unsigned *) key);
}
void s_del(unsigned hd, void *key) {
shash_del(hd, key);
}
int m0_vdel(unsigned hd, void *key, void *value) {
return 1;
}
int m1_vdel(unsigned hd, void *key, void *value) {
if (!col)
return 1;
ahash_remove(hd, * (unsigned *) key, * (unsigned *) value);
return 0;
}
void m0_put() {
if (col)
lhash_put(prim.phd, * (unsigned *) key_buf, value_buf);
else {
unsigned id = lhash_new(prim.phd, value_buf);
printf("%u\n", id);
}
}
void m1_put(void *key, void *value) {
unsigned values[2] = { * (unsigned *) key, * (unsigned *) value };
hash_put(prim.phd, values, sizeof(values), &values[1], sizeof(unsigned));
}
int m0_cond(unsigned qhds_n, unsigned is_value) {
return (!reverse) == is_value;
}
int m1_cond(unsigned qhds_n, unsigned is_value) {
return qhds_n & 1;
}
gen_t m1_gen[2] = {{
.value_print = u_print, .iter = u_iter, .del = u_del, .vdel = m1_vdel,
.put = m1_put, .get = u_gen_get, .cond = m1_cond,
}, {
.value_print = u_print, .iter = u_iter, .del = u_del, .vdel = m1_vdel,
.put = m1_put, .get = u_gen_pget, .cond = m1_cond,
}}, m0_gen[2] = {{
.value_print = s_print, .iter = s_iter, .del = s_del, .vdel = m0_vdel,
.put = m0_put, .get = s_gen_pget, .cond = m0_cond,
}, {
.value_print = s_print, .iter = u_iter, .del = m0_u_del, .vdel = m0_vdel,
.put = m0_put, .get = u_gen_get, .cond = m0_cond,
}}, gen, *gen_r;
void
usage(char *prog)
{
fprintf(stderr, "Usage: %s [-mqa ARG] [[-rl] [-Rpdg ARG] ...] db_path", prog);
fprintf(stderr, " Options:\n");
fprintf(stderr, " -r reverse operation\n");
fprintf(stderr, " -l list all values\n");
fprintf(stderr, " -L list missing values\n");
fprintf(stderr, " -q other_db db to use string lookups and printing\n");
fprintf(stderr, " -a other_db db to use for reversed string lookups and printing\n");
fprintf(stderr, " -R KEY get random value of key (key is ignored in mode 0)\n");
fprintf(stderr, " -p KEY[:VAL] put a key/value pair\n");
fprintf(stderr, " -d KEY[:VAL] delete key/value pair(s)\n");
fprintf(stderr, " -g KEY get value(s) of a key\n");
fprintf(stderr, " -m 0-1 select mode of operation\n");
fprintf(stderr, " -x when printing associations, bail on first result\n");
fprintf(stderr, " Modes (default is 0):\n");
fprintf(stderr, " 0 index mode (1 string : 1 id)\n");
fprintf(stderr, " 1 associative mode (n id : n id)\n");
}
int u_get(unsigned hd, void *value, void *key) {
return uhash_get(aux_hdp.hd[0], value, * (unsigned *) key);
}
static inline void *rec_query(unsigned qhds, char *tbuf, char *buf, unsigned tmprev) {
struct hash_cursor c2 = lhash_iter(qhds);
unsigned aux;
char *aux2;
while (lhash_next(&aux, &aux_hdp, &c2)) {
tmprev = !tmprev;
/* fprintf(stderr, "req_query %u %u %u %u %s", qhds, tmprev, aux_hdp.hd[tmprev], * (unsigned *) buf, buf); */
if (m0_gen[!tmprev].get(aux_hdp.hd[tmprev], tbuf, buf)) {
/* fprintf(stderr, "?\n"); */
hash_fin(&c2);
return NULL;
}
/* fprintf(stderr, "!\n"); */
aux2 = buf;
buf = tbuf;
tbuf = aux2;
}
return buf;
}
inline static char *_gen_lookup(char *buf, unsigned *tmprev_r, char *str, unsigned qhds, unsigned qhds_n, unsigned is_value) {
unsigned cond = gen.cond(qhds_n, is_value);
char *ret = NULL;
if (cond)
strcpy(buf, str);
else {
unsigned ret = strtoul(str, NULL, 10);
memcpy(buf, &ret, sizeof(unsigned));
}
if (qhds_n)
ret = rec_query(qhds, buf, buf, !cond);
else
ret = buf;
*tmprev_r = (qhds_n & 1) != reverse;
return ret;
}
static char *gen_lookup(char *str) {
char *ret, *other_buf = value_buf;
tmprev_q = tmprev_a = reverse;
if (!str) {
gen = gen_r[tmprev_q];
return NULL;
}
gen = gen_r[reverse];
col = strchr(str, ':');
if (col) {
*col = '\0';
col++;
_gen_lookup(value_buf, &tmprev_a, col, ahds, ahds_n, 1);
other_buf = key_buf;
}
ret = _gen_lookup(other_buf, &tmprev_q, str, qhds, qhds_n, other_buf == value_buf);
gen = gen_r[tmprev_q];
return ret;
}
static inline void gen_del() {
char *iter_key = gen_lookup(optarg);
if (gen.vdel(prim.hd[!tmprev_q], value_buf, key_buf))
gen.del(prim.hd[!tmprev_q], value_buf);
}
static inline int assoc_exists(char *key_buf) {
if (!ahds_n)
return 1;
static char alt_buf[BUFSIZ];
memcpy(alt_buf, key_buf, sizeof(alt_buf));
return !!rec_query(ahds, alt_buf, alt_buf, 1);
}
static inline void assoc_print() {
static char alt_buf[BUFSIZ];
unsigned aux;
struct hash_cursor c2 = lhash_iter(ahds);
while (lhash_next(&aux, &aux_hdp, &c2)) {
putchar(' ');
if (u_get(aux_hdp.hd[0], alt_buf, key_buf)) {
s_print("-1");
continue;
}
s_print(alt_buf);
if (bail) {
hash_fin(&c2);
break;
}
}
}
static inline void gen_rand() {
unsigned count = 0, rand;
struct hash_cursor c;
char *iter_key = gen_lookup(mode ? optarg : NULL);
c = gen.iter(prim.hd[!tmprev_q], iter_key);
while (lhash_next((unsigned *) key_buf, value_buf, &c))
if (assoc_exists(key_buf))
count ++;
if (count == 0) {
printf("-1\n");
return;
}
rand = random() % count;
c = gen.iter(prim.hd[!tmprev_q], iter_key);
while (lhash_next((unsigned *) key_buf, value_buf, &c))
if (!assoc_exists(key_buf))
continue;
else if ((--count) <= rand) {
hash_fin(&c);
break;
}
if (mode)
gen.value_print(key_buf);
else {
u_print(key_buf);
putchar(' ');
gen.value_print(value_buf);
}
assoc_print();
printf("\n");
}
static inline void _gen_get() {
u_print(key_buf);
putchar(' ');
gen.value_print(value_buf);
assoc_print();
printf("\n");
}
static void gen_get(char *str) {
char *iter_key = gen_lookup(str);
struct hash_cursor c;
if (!iter_key) {
printf("-1\n");
return;
}
c = gen.iter(prim.hd[!tmprev_q], iter_key);
while (lhash_next((unsigned *) key_buf, value_buf, &c))
if (assoc_exists(key_buf))
_gen_get();
}
static void gen_list() {
struct hash_cursor c;
unsigned cond;
gen_lookup(NULL);
cond = gen.cond(qhds_n, 1);
c = gen.iter(prim.hd[!tmprev_q], NULL);
while (lhash_next((unsigned *) key_buf, value_buf, &c)) {
rec_query(qhds, key_buf, value_buf, !cond);
_gen_get();
}
}
static inline void gen_put() {
gen_lookup(optarg);
gen.put(key_buf, value_buf);
}
static inline void gen_list_missing() {
unsigned aux;
struct hash_cursor c;
gen_lookup(NULL);
if (!qhds_n) {
fprintf(stderr, "list missing needs a corresponding database\n");
return;
}
c = hash_iter(prim.hd[!tmprev_q], NULL, 0);
while (lhash_next((unsigned *) key_buf, value_buf, &c)) {
struct hash_cursor c2 = lhash_iter(qhds);
while (lhash_next(&aux, &aux_hdp, &c2))
if (m0_gen[tmprev_q].get(aux_hdp.hd[!tmprev_q], key_buf, value_buf)) {
u_print(key_buf);
putchar(' ');
gen.value_print(value_buf);
assoc_print();
putchar('\n');
}
}
}
void m0_assoc_rhd(void **data, uint32_t *len, void *key, void *value) {
*len = strlen(value) + 1;
*data = value;
if (* (unsigned *) key == (unsigned) -1)
*data = key;
}
void assoc_hd(void **data, uint32_t *len, void *key, void *value) {
*len = sizeof(unsigned);
*data = key;
}
void m1_assoc_rhd(void **data, uint32_t *len, void *key, void *value) {
*len = sizeof(unsigned);
*data = ((unsigned *) value);
}
unsigned gen_open(char *fname, unsigned mode) {
static int fmode = 0664;
unsigned existed = access(fname, F_OK) == 0;
hash_config.file = fname;
hash_config.mode = 0664;
hash_config.flags = 0;
aux_hdp.phd = lhash_init(0, "phd");
if (existed)
lhash_get(aux_hdp.phd, &mode, -2);
if (mode) {
aux_hdp.hd[0] = ahash_init("hd"); // needed for dupes
aux_hdp.hd[1] = ahash_init("rhd");
hash_assoc(aux_hdp.hd[0], aux_hdp.phd, assoc_hd);
hash_assoc(aux_hdp.hd[1], aux_hdp.phd, m1_assoc_rhd);
} else {
aux_hdp.hd[0] = hash_init("hd"); // not really needed but here for consistency
aux_hdp.hd[1] = ahash_init("rhd");
hash_assoc(aux_hdp.hd[0], aux_hdp.phd, assoc_hd);
hash_assoc(aux_hdp.hd[1], aux_hdp.phd, m0_assoc_rhd);
}
if (!existed)
uhash_put(aux_hdp.phd, -2, &mode, sizeof(mode));
return mode;
}
int
main(int argc, char *argv[])
{
static char *optstr = "xla:q:p:d:g:m:rR:L:?";
char *fname = argv[argc - 1], ch;
int fmode = 0444;
if (argc < 2) {
usage(*argv);
return EXIT_FAILURE;
}
ahds = lhash_init(sizeof(struct hdpair), NULL);
qhds = lhash_init(sizeof(struct hdpair), NULL);
gen_r = m0_gen;
while ((ch = getopt(argc, argv, optstr)) != -1)
switch (ch) {
case 'm':
switch (mode = strtoul(optarg, NULL, 10)) {
case 0: break;
case 1: gen_r = m1_gen; break;
default:
fprintf(stderr, "Invalid mode\n");
return EXIT_FAILURE;
}
break;
case 'a':
gen_open(optarg, 0);
lhash_new(ahds, &aux_hdp);
ahds_n++;
break;
case 'q':
gen_open(optarg, 0);
lhash_new(qhds, &aux_hdp);
qhds_n++;
break;
case 'x':
bail = 1;
break;
case 'p':
case 'd':
fmode = 0664;
case 'l':
case 'L':
case 'R':
case 'g':
case 'r': break;
default: usage(*argv); return EXIT_FAILURE;
case '?': usage(*argv); return EXIT_SUCCESS;
}
optind = 1;
if ((mode = gen_open(fname, mode)))
gen_r = m1_gen;
prim = aux_hdp;
srandom(time(NULL));
while ((ch = getopt(argc, argv, optstr)) != -1) switch (ch) {
case 'R': gen_rand(); break;
case 'L': gen_list_missing(); break;
case 'l': gen_list(); break;
case 'p': gen_put(); break;
case 'd': gen_del(); break;
case 'g': gen_get(optarg); break;
case 'r': reverse = !reverse; break;
}
hash_close(prim.phd, 0);
hash_close(prim.hd[0], 0);
hash_close(prim.hd[1], 0);
}