-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcdb.c
737 lines (689 loc) · 23.8 KB
/
tcdb.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#include <assert.h>
#include <string.h>
#include <tcutil.h>
#include "tcdb.h"
/** Redis object types */
enum {
RK_TCDB_NONE = -1,
RK_TCDB_STRING = 0,
RK_TCDB_LIST = 1,
RK_TCDB_SET = 2,
RK_TCDB_ZSET = 3,
RK_TCDB_HASH = 4
};
/* Private macro */
#define RKTCDBOCHECK(RK_type, RK_exp) \
(((RK_type) >= 0 && (RK_type) != (RK_exp)) ? false : true)
/** Private function prototypes */
static int rk_tcdb_obj_search(rk_tcdb_t *db, const char *kbuf, int ksiz, int *type);
static int64_t rk_tcdb_add_int(rk_tcdb_t *db, const char *kbuf, int ksiz, int64_t num);
static char *rk_tcdb_hash_get(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, int *sp);
static int rk_tcdb_hash_put(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, const char *vbuf, int vsiz);
static int rk_tcdb_hash_out(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz);
static int rk_tcdb_hash_put_keep(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, const char *vbuf, int vsiz);
static int rk_tcdb_hash_exists(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz);
static int rk_tcdb_hash_rnum(TCTDB *db, const char *kbuf, int ksiz);
static TCLIST *rk_tcdb_hash_keys(TCTDB *db, const char *kbuf, int ksiz);
/**
* NOTES
* --
* So far the `str` table will *always* store values as raw data (i.e. pure
* byte array). In other words even if INCR and DECR commands give integers
* values, we won't store these values as serialized int-s (this is what
* Tokyo Cabinet does with `tchdbaddint` function - see HDBPDADDINT flag).
* We could think of optimizing this by maintaining one Hash DB per kind of
* encoding (see REDIS_ENCODING_RAW, REDIS_ENCODING_INT, etc).
*/
void rk_tcdb_skel_init(rk_skel_t *skel) {
assert(skel);
memset(skel, 0, sizeof(*skel));
skel->opq = rk_tcdb_new();
skel->free = (void (*)(void *))rk_tcdb_free;
skel->open = (bool (*)(void *, const char *))rk_tcdb_open;
skel->close = (bool (*)(void *))rk_tcdb_close;
skel->del = (int (*)(void *, const char *, int))rk_tcdb_del;
skel->exists = (int (*)(void *, const char *, int))rk_tcdb_exists;
skel->type = (char *(*)(void *, const char *, int))rk_tcdb_type;
skel->get = (char *(*)(void *, const char *, int, int *))rk_tcdb_get;
skel->set = (bool (*)(void *, const char *, int, const char *, int))rk_tcdb_set;
skel->setnx = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_setnx;
skel->incr = (int64_t (*)(void *, const char *, int))rk_tcdb_incr;
skel->decr = (int64_t (*)(void *, const char *, int))rk_tcdb_decr;
skel->incrby = (int64_t (*)(void *, const char *, int, int64_t))rk_tcdb_incrby;
skel->decrby = (int64_t (*)(void *, const char *, int, int64_t))rk_tcdb_decrby;
skel->getset = (char *(*)(void *, const char *, int, const char *, int, int *))rk_tcdb_getset;
skel->hget = (char *(*)(void *, const char *, int, const char *, int, int *))rk_tcdb_hget;
skel->hset = (int (*)(void *, const char *, int, const char *, int, const char *, int))rk_tcdb_hset;
skel->hdel = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_hdel;
skel->hsetnx = (int (*)(void *, const char *, int, const char *, int, const char *, int))rk_tcdb_hsetnx;
skel->hexists = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_hexists;
skel->hlen = (int (*)(void *, const char *, int))rk_tcdb_hlen;
skel->sadd = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_sadd;
skel->srem = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_srem;
skel->scard = (int (*)(void *, const char *, int))rk_tcdb_scard;
skel->sismember = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_sismember;
skel->smembers = (rk_listval_t *(*)(void *, const char *, int))rk_tcdb_smembers;
skel->llen = (int (*)(void *, const char *, int))rk_tcdb_llen;
skel->lpush = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_lpush;
skel->rpush = (int (*)(void *, const char *, int, const char *, int))rk_tcdb_rpush;
skel->lpop = (char *(*)(void *, const char *, int, int *))rk_tcdb_lpop;
skel->rpop = (char *(*)(void *, const char *, int, int *))rk_tcdb_rpop;
skel->lrange = (rk_listval_t *(*)(void *, const char *, int, int, int))rk_tcdb_lrange;
}
rk_tcdb_t *rk_tcdb_new(void) {
rk_tcdb_t *db = malloc(sizeof(*db));
db->open = false;
db->str = NULL;
db->hsh = NULL;
db->set = NULL;
db->lst = NULL;
return db;
}
void rk_tcdb_free(rk_tcdb_t *db) {
assert(db);
if (db->open) rk_tcdb_close(db);
free(db);
}
bool rk_tcdb_open(rk_tcdb_t *db, const char *path) {
assert(db && path);
bool err = false;
int omode;
TCHDB *str = NULL;
TCTDB *hsh = NULL;
TCTDB *set = NULL;
TCBDB *lst = NULL;
omode = HDBOWRITER | HDBOCREAT | HDBOLCKNB;
str = tchdbnew();
if (!tchdbopen(str, path, omode)) err = true;
if (!err) {
char *hpath = tcsprintf("%s.hash", path);
omode = TDBOWRITER | TDBOCREAT | TDBOLCKNB;
hsh = tctdbnew();
if (!tctdbopen(hsh, hpath, omode)) err = true;
free(hpath);
}
if (!err) {
char *spath = tcsprintf("%s.set", path);
omode = TDBOWRITER | TDBOCREAT | TDBOLCKNB;
set = tctdbnew();
if (!tctdbopen(set, spath, omode)) err = true;
free(spath);
}
if (!err) {
char *lpath = tcsprintf("%s.list", path);
omode = BDBOWRITER | BDBOCREAT | BDBOLCKNB;
lst = tcbdbnew();
if (!tcbdbopen(lst, lpath, omode)) err = true;
free(lpath);
}
if (!err) {
db->str = str;
db->hsh = hsh;
db->set = set;
db->lst = lst;
db->open = true;
}
else {
if (str) tchdbdel(str);
if (hsh) tctdbdel(hsh);
if (set) tctdbdel(set);
if (lst) tcbdbdel(lst);
}
return !err;
}
bool rk_tcdb_close(rk_tcdb_t *db) {
assert(db);
if (!db->open) return false;
bool err = false;
if (!tchdbclose(db->str)) err = true;
tchdbdel(db->str);
if (!tctdbclose(db->hsh)) err = true;
tctdbdel(db->hsh);
if (!tctdbclose(db->set)) err = true;
tctdbdel(db->set);
if (!tcbdbclose(db->lst)) err = true;
tcbdbdel(db->lst);
db->str = NULL;
db->hsh = NULL;
db->set = NULL;
db->lst = NULL;
db->open = false;
return !err;
}
int rk_tcdb_del(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz);
if (!db->open) return -1;
if (!tchdbout(db->str, kbuf, ksiz)) {
if (tchdbecode(db->str) != TCENOREC) return -1;
}
else return 1;
if (!tctdbout(db->hsh, kbuf, ksiz)) {
if (tctdbecode(db->hsh) != TCENOREC) return -1;
}
else return 1;
if (!tctdbout(db->set, kbuf, ksiz)) {
if (tctdbecode(db->set) != TCENOREC) return -1;
}
else return 1;
if (!tcbdbout3(db->lst, kbuf, ksiz)) {
if (tcbdbecode(db->lst) != TCENOREC) return -1;
}
else return 1;
return 0;
}
int rk_tcdb_exists(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return -1;
int type;
return rk_tcdb_obj_search(db, kbuf, ksiz, &type);
}
char *rk_tcdb_type(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
switch (type) {
case RK_TCDB_STRING: return tcstrdup("string");
case RK_TCDB_LIST: return tcstrdup("list");
case RK_TCDB_SET: return tcstrdup("set");
case RK_TCDB_ZSET: return tcstrdup("zset");
case RK_TCDB_HASH: return tcstrdup("hash");
}
return tcstrdup("none");
}
char *rk_tcdb_get(rk_tcdb_t *db, const char *kbuf, int ksiz, int *sp) {
assert(db && kbuf && ksiz >= 0 && sp);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) return NULL;
return tchdbget(db->str, kbuf, ksiz, sp);
}
bool rk_tcdb_set(rk_tcdb_t *db, const char *kbuf, int ksiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return false;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) {
/* According to Redis spec "if key already holds a value, it is
overwritten, regardless of its type" */
if (!rk_tcdb_del(db, kbuf, ksiz)) return false;
}
return tchdbput(db->str, kbuf, ksiz, vbuf, vsiz);
}
int rk_tcdb_setnx(rk_tcdb_t *db, const char *kbuf, int ksiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) {
/* SETNX behaves the same than SET regarding overwriting */
if (!rk_tcdb_del(db, kbuf, ksiz)) return -1;
}
int rv = 1;
if (!tchdbputkeep(db->str, kbuf, ksiz, vbuf, vsiz)) {
rv = (tchdbecode(db->str) == TCEKEEP) ? 0 : -1;
}
return rv;
}
int64_t rk_tcdb_incr(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return INT_MIN;
/* TODO: see above NOTES */
/* return tchdbaddint(db->str, kbuf, ksiz, 1); */
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return INT_MIN;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) return INT_MIN;
return rk_tcdb_add_int(db, kbuf, ksiz, 1);
}
int64_t rk_tcdb_decr(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return INT_MIN;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return INT_MIN;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) return INT_MIN;
return rk_tcdb_add_int(db, kbuf, ksiz, -1);
}
int64_t rk_tcdb_incrby(rk_tcdb_t *db, const char *kbuf, int ksiz, int64_t inc) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return INT_MIN;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return INT_MIN;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) return INT_MIN;
return rk_tcdb_add_int(db, kbuf, ksiz, inc);
}
int64_t rk_tcdb_decrby(rk_tcdb_t *db, const char *kbuf, int ksiz, int64_t dec) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return INT_MIN;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return INT_MIN;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) return INT_MIN;
return rk_tcdb_add_int(db, kbuf, ksiz, -dec);
}
char *rk_tcdb_getset(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *vbuf, int vsiz, int *sp) {
assert(db && kbuf && ksiz >= 0 && vbuf && vsiz >= 0 && sp);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
if (!RKTCDBOCHECK(type, RK_TCDB_STRING)) return NULL;
int siz;
char *buf = tchdbget(db->str, kbuf, ksiz, &siz);
if (!tchdbput(db->str, kbuf, ksiz, vbuf, vsiz)) {
if (buf) free(buf);
return NULL;
}
if (buf) *sp = siz;
return buf;
}
char *rk_tcdb_hget(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, int *sp) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0 && sp);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
if (!RKTCDBOCHECK(type, RK_TCDB_HASH)) return NULL;
return rk_tcdb_hash_get(db->hsh, kbuf, ksiz, fbuf, fsiz, sp);
}
int rk_tcdb_hset(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0 && vbuf && vsiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_HASH)) return -1;
return rk_tcdb_hash_put(db->hsh, kbuf, ksiz, fbuf, fsiz, vbuf, vsiz);
}
int rk_tcdb_hdel(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_HASH)) return -1;
return rk_tcdb_hash_out(db->hsh, kbuf, ksiz, fbuf, fsiz);
}
int rk_tcdb_hsetnx(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0 && vbuf && vsiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_HASH)) return -1;
return rk_tcdb_hash_put_keep(db->hsh, kbuf, ksiz, fbuf, fsiz, vbuf, vsiz);
}
int rk_tcdb_hexists(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_HASH)) return -1;
return rk_tcdb_hash_exists(db->hsh, kbuf, ksiz, fbuf, fsiz);
}
int rk_tcdb_hlen(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_HASH)) return -1;
return rk_tcdb_hash_rnum(db->hsh, kbuf, ksiz);
}
int rk_tcdb_sadd(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *mbuf, int msiz) {
assert(db && kbuf && ksiz >= 0 && mbuf && msiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_SET)) return -1;
return rk_tcdb_hash_put(db->set, kbuf, ksiz, mbuf, msiz, "", 0);
}
int rk_tcdb_srem(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *mbuf, int msiz) {
assert(db && kbuf && ksiz >= 0 && mbuf && msiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_SET)) return -1;
return rk_tcdb_hash_out(db->set, kbuf, ksiz, mbuf, msiz);
}
int rk_tcdb_scard(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_SET)) return -1;
return rk_tcdb_hash_rnum(db->set, kbuf, ksiz);
}
int rk_tcdb_sismember(rk_tcdb_t *db, const char *kbuf, int ksiz,
const char *mbuf, int msiz) {
assert(db && kbuf && ksiz >= 0 && mbuf && msiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_SET)) return -1;
return rk_tcdb_hash_exists(db->set, kbuf, ksiz, mbuf, msiz);
}
rk_listval_t *rk_tcdb_smembers(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
if (!RKTCDBOCHECK(type, RK_TCDB_SET)) return NULL;
rk_val_t *ary = NULL;
TCLIST *keys = rk_tcdb_hash_keys(db->set, kbuf, ksiz);
int num = 0;
if (keys != NULL) {
int siz = tclistnum(keys);
if (siz > 0) {
ary = malloc(siz*sizeof(rk_val_t));
int i;
for (i = 0; i < siz; i++) {
int vsiz;
const char *vbuf = tclistval(keys, i, &vsiz);
ary[i].buf = tcmemdup(vbuf, vsiz);
ary[i].siz = vsiz;
}
num = siz;
}
tclistdel(keys);
}
rk_listval_t *lval = NULL;
if (ary != NULL) {
lval = malloc(sizeof(*lval));
lval->ary = ary;
lval->num = num;
}
return lval;
}
int rk_tcdb_llen(rk_tcdb_t *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_LIST)) return -1;
return tcbdbvnum(db->lst, kbuf, ksiz);
}
int rk_tcdb_lpush(rk_tcdb_t *db, const char *kbuf, int ksiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_LIST)) return -1;
if (!tcbdbputdupback(db->lst, kbuf, ksiz, vbuf, vsiz)) return -1;
return tcbdbvnum(db->lst, kbuf, ksiz);
}
int rk_tcdb_rpush(rk_tcdb_t *db, const char *kbuf, int ksiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return -1;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return -1;
if (!RKTCDBOCHECK(type, RK_TCDB_LIST)) return -1;
if (!tcbdbputdup(db->lst, kbuf, ksiz, vbuf, vsiz)) return -1;
return tcbdbvnum(db->lst, kbuf, ksiz);
}
char *rk_tcdb_lpop(rk_tcdb_t *db, const char *kbuf, int ksiz, int *sp) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
if (!RKTCDBOCHECK(type, RK_TCDB_LIST)) return NULL;
int vsiz;
char *vbuf = NULL;
BDBCUR *cur = tcbdbcurnew(db->lst);
if (tcbdbcurjump(cur, kbuf, ksiz)) {
int siz;
const void *buf = tcbdbcurkey3(cur, &siz);
if (siz == ksiz && !memcmp(buf, kbuf, ksiz)) {
vbuf = tcbdbcurval(cur, &vsiz);
if (!tcbdbcurout(cur)) {
if (vbuf) free(vbuf);
vbuf = NULL;
}
}
}
tcbdbcurdel(cur);
if (vbuf != NULL) *sp = vsiz;
return vbuf;
}
char *rk_tcdb_rpop(rk_tcdb_t *db, const char *kbuf, int ksiz, int *sp) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
if (!RKTCDBOCHECK(type, RK_TCDB_LIST)) return NULL;
int vsiz;
char *vbuf = NULL;
BDBCUR *cur = tcbdbcurnew(db->lst);
if (tcbdbcurjumpback(cur, kbuf, ksiz)) {
int siz;
const void *buf = tcbdbcurkey3(cur, &siz);
if (siz == ksiz && !memcmp(buf, kbuf, ksiz)) {
vbuf = tcbdbcurval(cur, &vsiz);
if (!tcbdbcurout(cur)) {
if (vbuf) free(vbuf);
vbuf = NULL;
}
}
}
tcbdbcurdel(cur);
if (vbuf != NULL) *sp = vsiz;
return vbuf;
}
rk_listval_t *rk_tcdb_lrange(rk_tcdb_t *db, const char *kbuf, int ksiz,
int start, int stop) {
assert(db && kbuf && ksiz >= 0);
if (!db->open) return NULL;
int type;
if (rk_tcdb_obj_search(db, kbuf, ksiz, &type) < 0) return NULL;
if (!RKTCDBOCHECK(type, RK_TCDB_LIST)) return NULL;
rk_val_t *ary = NULL;
int num = 0;
TCLIST *vals = tcbdbget4(db->lst, kbuf, ksiz);
int llen = (vals != NULL) ? tclistnum(vals) : 0;
if (llen > 0 && start < llen && stop >= -llen) {
int low = start < 0 ? tclmax(start, -llen) + llen : start;
int high = stop < 0 ? stop + llen : tclmin(stop, llen-1);
if (high >= low) {
int siz = high - low + 1;
ary = malloc(siz*sizeof(rk_val_t));
int i;
for (i = 0; i < siz; i++) {
int vsiz;
const char *vbuf = tclistval(vals, i + low, &vsiz);
ary[i].buf = tcmemdup(vbuf, vsiz);
ary[i].siz = vsiz;
}
num = siz;
}
}
if (vals) tclistdel(vals);
rk_listval_t *lval = NULL;
if (ary != NULL) {
lval = malloc(sizeof(*lval));
lval->ary = ary;
lval->num = num;
}
return lval;
}
static int rk_tcdb_obj_search(rk_tcdb_t *db, const char *kbuf, int ksiz, int *type) {
assert(db && kbuf && ksiz >= 0 && type);
if (!tchdbiterinit2(db->str, kbuf, ksiz)) {
if (tchdbecode(db->str) != TCENOREC) return -1;
}
else {
*type = RK_TCDB_STRING;
return 1;
}
if (!tctdbiterinit2(db->hsh, kbuf, ksiz)) {
if (tctdbecode(db->hsh) != TCENOREC) return -1;
}
else {
*type = RK_TCDB_HASH;
return 1;
}
if (!tctdbiterinit2(db->set, kbuf, ksiz)) {
if (tctdbecode(db->set) != TCENOREC) return -1;
}
else {
*type = RK_TCDB_SET;
return 1;
}
BDBCUR *cur = tcbdbcurnew(db->lst);
if (tcbdbcurjump(cur, kbuf, ksiz)) {
int siz;
const void *buf = tcbdbcurkey3(cur, &siz);
if (siz == ksiz && !memcmp(buf, kbuf, ksiz)) {
tcbdbcurdel(cur);
*type = RK_TCDB_LIST;
return 1;
}
}
tcbdbcurdel(cur);
*type = RK_TCDB_NONE;
return 0;
}
static int64_t rk_tcdb_add_int(rk_tcdb_t *db, const char *kbuf, int ksiz, int64_t num) {
assert(db && kbuf && ksiz >= 0);
bool err = false;
int64_t rv;
int vsiz;
char buf[32];
int siz;
char *vbuf = tchdbget(db->str, kbuf, ksiz, &vsiz);
if (vbuf == NULL) {
if (tchdbecode(db->str) == TCENOREC) rv = num;
else err = true;
}
else {
if (tcstrisnum(vbuf)) rv = tcatoi(vbuf) + num;
else err = true;
free(vbuf);
}
if (!err) {
siz = sprintf(buf, "%lld", (long long) rv);
if (!tchdbput(db->str, kbuf, ksiz, buf, siz)) err = true;
}
return (err ? INT_MIN : rv);
}
static char *rk_tcdb_hash_get(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, int *sp) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0 && sp);
return tctdbget4(db, kbuf, ksiz, fbuf, fsiz, sp);
}
static int rk_tcdb_hash_put(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0 && vbuf && vsiz >= 0);
TCMAP *cols = NULL;
if (!tctdbiterinit2(db, kbuf, ksiz)) {
if (tctdbecode(db) != TCENOREC) return -1;
else cols = tcmapnew();
}
else {
cols = tctdbiternext3(db);
tcmapout2(cols, "");
}
if (!cols) return -1;
int rsiz;
const void *rbuf = tcmapget(cols, fbuf, fsiz, &rsiz);
int rv = (rbuf == NULL ? 1 : 0);
tcmapput(cols, fbuf, fsiz, vbuf, vsiz);
bool err = !tctdbput(db, kbuf, ksiz, cols);
if (cols) tcmapdel(cols);
return err ? -1 : rv;
}
static int rk_tcdb_hash_out(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0);
TCMAP *cols = NULL;
if (!tctdbiterinit2(db, kbuf, ksiz)) {
if (tctdbecode(db) != TCENOREC) return -1;
else return 0;
}
else {
cols = tctdbiternext3(db);
tcmapout2(cols, "");
}
if (!cols) return -1;
int rv = 0;
bool err = false;
if (tcmapout(cols, fbuf, fsiz)) {
rv = 1;
err = !tctdbput(db, kbuf, ksiz, cols);
}
if (cols) tcmapdel(cols);
return err ? -1 : rv;
}
static int rk_tcdb_hash_put_keep(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz, const char *vbuf, int vsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0 && vbuf && vsiz >= 0);
TCMAP *cols = NULL;
if (!tctdbiterinit2(db, kbuf, ksiz)) {
if (tctdbecode(db) != TCENOREC) return -1;
else cols = tcmapnew();
}
else {
cols = tctdbiternext3(db);
tcmapout2(cols, "");
}
if (!cols) return -1;
int rv = 0;
bool err = false;
if (tcmapputkeep(cols, fbuf, fsiz, vbuf, vsiz)) {
rv = 1;
err = !tctdbput(db, kbuf, ksiz, cols);
}
if (cols) tcmapdel(cols);
return err ? -1 : rv;
}
static int rk_tcdb_hash_exists(TCTDB *db, const char *kbuf, int ksiz,
const char *fbuf, int fsiz) {
assert(db && kbuf && ksiz >= 0 && fbuf && fsiz >= 0);
TCMAP *cols = NULL;
if (!tctdbiterinit2(db, kbuf, ksiz)) {
if (tctdbecode(db) != TCENOREC) return -1;
else return 0;
}
else {
cols = tctdbiternext3(db);
}
if (!cols) return -1;
int rsiz;
const void *rbuf = tcmapget(cols, fbuf, fsiz, &rsiz);
int rv = (rbuf == NULL ? 0 : 1);
if (cols) tcmapdel(cols);
return rv;
}
static int rk_tcdb_hash_rnum(TCTDB *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
TCMAP *cols = NULL;
if (!tctdbiterinit2(db, kbuf, ksiz)) {
if (tctdbecode(db) != TCENOREC) return -1;
else return 0;
}
else {
cols = tctdbiternext3(db);
}
if (!cols) return -1;
int rv = tcmaprnum(cols) - 1;
if (cols) tcmapdel(cols);
return rv;
}
static TCLIST *rk_tcdb_hash_keys(TCTDB *db, const char *kbuf, int ksiz) {
assert(db && kbuf && ksiz >= 0);
TCMAP *cols = NULL;
if (!tctdbiterinit2(db, kbuf, ksiz)) return NULL;
else {
cols = tctdbiternext3(db);
tcmapout2(cols, "");
}
if (!cols) return NULL;
TCLIST *keys = tcmapkeys(cols);
tcmapdel(cols);
return keys;
}