-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcbdb.c
4174 lines (3842 loc) · 125 KB
/
tcbdb.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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* The B+ tree database API of Tokyo Cabinet
* Copyright (C) 2006-2009 Mikio Hirabayashi
* This file is part of Tokyo Cabinet.
* Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "tcutil.h"
#include "tchdb.h"
#include "tcbdb.h"
#include "myconf.h"
#define BDBOPAQUESIZ 64 // size of using opaque field
#define BDBLEFTOPQSIZ 64 // size of left opaque field
#define BDBPAGEBUFSIZ 32768 // size of a buffer to read each page
#define BDBNODEIDBASE ((1LL<<48)+1) // base number of node ID
#define BDBLEVELMAX 64 // max level of B+ tree
#define BDBCACHEOUT 8 // number of pages in a process of cacheout
#define BDBDEFLMEMB 128 // default number of members in each leaf
#define BDBMINLMEMB 4 // minimum number of members in each leaf
#define BDBDEFNMEMB 256 // default number of members in each node
#define BDBMINNMEMB 4 // minimum number of members in each node
#define BDBDEFBNUM 32749 // default bucket number
#define BDBDEFAPOW 8 // default alignment power
#define BDBDEFFPOW 10 // default free block pool power
#define BDBDEFLCNUM 1024 // default number of leaf cache
#define BDBDEFNCNUM 512 // default number of node cache
#define BDBDEFLSMAX 16384 // default maximum size of each leaf
#define BDBMINLSMAX 512 // minimum maximum size of each leaf
typedef struct { // type of structure for a record
int ksiz; // size of the key region
int vsiz; // size of the value region
TCLIST *rest; // list of value objects
} BDBREC;
typedef struct { // type of structure for a leaf page
uint64_t id; // ID number of the leaf
TCPTRLIST *recs; // list of records
int size; // predicted size of serialized buffer
uint64_t prev; // ID number of the previous leaf
uint64_t next; // ID number of the next leaf
bool dirty; // whether to be written back
bool dead; // whether to be removed
} BDBLEAF;
typedef struct { // type of structure for a page index
uint64_t pid; // ID number of the referring page
int ksiz; // size of the key region
} BDBIDX;
typedef struct { // type of structure for a node page
uint64_t id; // ID number of the node
uint64_t heir; // ID of the child before the first index
TCPTRLIST *idxs; // list of indices
bool dirty; // whether to be written back
bool dead; // whether to be removed
} BDBNODE;
enum { // enumeration for duplication behavior
BDBPDOVER, // overwrite an existing value
BDBPDKEEP, // keep the existing value
BDBPDCAT, // concatenate values
BDBPDDUP, // allow duplication of keys
BDBPDDUPB, // allow backward duplication
BDBPDADDINT, // add an integer
BDBPDADDDBL, // add a real number
BDBPDPROC // process by a callback function
};
typedef struct { // type of structure for a duplication callback
TCPDPROC proc; // function pointer
void *op; // opaque pointer
} BDBPDPROCOP;
/* private macros */
#define BDBLOCKMETHOD(TC_bdb, TC_wr) \
((TC_bdb)->mmtx ? tcbdblockmethod((TC_bdb), (TC_wr)) : true)
#define BDBUNLOCKMETHOD(TC_bdb) \
((TC_bdb)->mmtx ? tcbdbunlockmethod(TC_bdb) : true)
#define BDBLOCKCACHE(TC_bdb) \
((TC_bdb)->mmtx ? tcbdblockcache(TC_bdb) : true)
#define BDBUNLOCKCACHE(TC_bdb) \
((TC_bdb)->mmtx ? tcbdbunlockcache(TC_bdb) : true)
#define BDBTHREADYIELD(TC_bdb) \
do { if((TC_bdb)->mmtx) sched_yield(); } while(false)
/* private function prototypes */
static void tcbdbclear(TCBDB *bdb);
static void tcbdbdumpmeta(TCBDB *bdb);
static void tcbdbloadmeta(TCBDB *bdb);
static BDBLEAF *tcbdbleafnew(TCBDB *bdb, uint64_t prev, uint64_t next);
static bool tcbdbleafcacheout(TCBDB *bdb, BDBLEAF *leaf);
static bool tcbdbleafsave(TCBDB *bdb, BDBLEAF *leaf);
static BDBLEAF *tcbdbleafload(TCBDB *bdb, uint64_t id);
static bool tcbdbleafcheck(TCBDB *bdb, uint64_t id);
static BDBLEAF *tcbdbgethistleaf(TCBDB *bdb, const char *kbuf, int ksiz, uint64_t id);
static bool tcbdbleafaddrec(TCBDB *bdb, BDBLEAF *leaf, int dmode,
const char *kbuf, int ksiz, const char *vbuf, int vsiz);
static BDBLEAF *tcbdbleafdivide(TCBDB *bdb, BDBLEAF *leaf);
static bool tcbdbleafkill(TCBDB *bdb, BDBLEAF *leaf);
static BDBNODE *tcbdbnodenew(TCBDB *bdb, uint64_t heir);
static bool tcbdbnodecacheout(TCBDB *bdb, BDBNODE *node);
static bool tcbdbnodesave(TCBDB *bdb, BDBNODE *node);
static BDBNODE *tcbdbnodeload(TCBDB *bdb, uint64_t id);
static void tcbdbnodeaddidx(TCBDB *bdb, BDBNODE *node, bool order, uint64_t pid,
const char *kbuf, int ksiz);
static bool tcbdbnodesubidx(TCBDB *bdb, BDBNODE *node, uint64_t pid);
static uint64_t tcbdbsearchleaf(TCBDB *bdb, const char *kbuf, int ksiz);
static BDBREC *tcbdbsearchrec(TCBDB *bdb, BDBLEAF *leaf, const char *kbuf, int ksiz, int *ip);
static void tcbdbremoverec(TCBDB *bdb, BDBLEAF *leaf, BDBREC *rec, int ri);
static bool tcbdbcacheadjust(TCBDB *bdb);
static void tcbdbcachepurge(TCBDB *bdb);
static bool tcbdbopenimpl(TCBDB *bdb, const char *path, int omode);
static bool tcbdbcloseimpl(TCBDB *bdb);
static bool tcbdbputimpl(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz,
int dmode);
static bool tcbdboutimpl(TCBDB *bdb, const char *kbuf, int ksiz);
static bool tcbdboutlist(TCBDB *bdb, const char *kbuf, int ksiz);
static const char *tcbdbgetimpl(TCBDB *bdb, const char *kbuf, int ksiz, int *sp);
static int tcbdbgetnum(TCBDB *bdb, const char *kbuf, int ksiz);
static TCLIST *tcbdbgetlist(TCBDB *bdb, const char *kbuf, int ksiz);
static bool tcbdbrangeimpl(TCBDB *bdb, const char *bkbuf, int bksiz, bool binc,
const char *ekbuf, int eksiz, bool einc, int max, TCLIST *keys);
static bool tcbdbrangefwm(TCBDB *bdb, const char *pbuf, int psiz, int max, TCLIST *keys);
static bool tcbdboptimizeimpl(TCBDB *bdb, int32_t lmemb, int32_t nmemb,
int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts);
static bool tcbdbvanishimpl(TCBDB *bdb);
static bool tcbdblockmethod(TCBDB *bdb, bool wr);
static bool tcbdbunlockmethod(TCBDB *bdb);
static bool tcbdblockcache(TCBDB *bdb);
static bool tcbdbunlockcache(TCBDB *bdb);
static bool tcbdbcurfirstimpl(BDBCUR *cur);
static bool tcbdbcurlastimpl(BDBCUR *cur);
static bool tcbdbcurjumpimpl(BDBCUR *cur, const char *kbuf, int ksiz, bool forward);
static bool tcbdbcuradjust(BDBCUR *cur, bool forward);
static bool tcbdbcurprevimpl(BDBCUR *cur);
static bool tcbdbcurnextimpl(BDBCUR *cur);
static bool tcbdbcurputimpl(BDBCUR *cur, const char *vbuf, int vsiz, int mode);
static bool tcbdbcuroutimpl(BDBCUR *cur);
static bool tcbdbcurrecimpl(BDBCUR *cur, const char **kbp, int *ksp, const char **vbp, int *vsp);
static bool tcbdbforeachimpl(TCBDB *bdb, TCITER iter, void *op);
/* debugging function prototypes */
void tcbdbprintmeta(TCBDB *bdb);
void tcbdbprintleaf(TCBDB *bdb, BDBLEAF *leaf);
void tcbdbprintnode(TCBDB *bdb, BDBNODE *node);
/*************************************************************************************************
* API
*************************************************************************************************/
/* Get the message string corresponding to an error code. */
const char *tcbdberrmsg(int ecode){
return tcerrmsg(ecode);
}
/* Create a B+ tree database object. */
TCBDB *tcbdbnew(void){
TCBDB *bdb;
TCMALLOC(bdb, sizeof(*bdb));
tcbdbclear(bdb);
bdb->hdb = tchdbnew();
TCMALLOC(bdb->hist, sizeof(*bdb->hist) * BDBLEVELMAX);
tchdbtune(bdb->hdb, BDBDEFBNUM, BDBDEFAPOW, BDBDEFFPOW, 0);
tchdbsetxmsiz(bdb->hdb, 0);
return bdb;
}
/* Delete a B+ tree database object. */
void tcbdbdel(TCBDB *bdb){
assert(bdb);
if(bdb->open) tcbdbclose(bdb);
TCFREE(bdb->hist);
tchdbdel(bdb->hdb);
if(bdb->mmtx){
pthread_mutex_destroy(bdb->cmtx);
pthread_rwlock_destroy(bdb->mmtx);
TCFREE(bdb->cmtx);
TCFREE(bdb->mmtx);
}
TCFREE(bdb);
}
/* Get the last happened error code of a B+ tree database object. */
int tcbdbecode(TCBDB *bdb){
assert(bdb);
return tchdbecode(bdb->hdb);
}
/* Set mutual exclusion control of a B+ tree database object for threading. */
bool tcbdbsetmutex(TCBDB *bdb){
assert(bdb);
if(!TCUSEPTHREAD) return true;
if(bdb->mmtx || bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
TCMALLOC(bdb->mmtx, sizeof(pthread_rwlock_t));
TCMALLOC(bdb->cmtx, sizeof(pthread_mutex_t));
bool err = false;
if(pthread_rwlock_init(bdb->mmtx, NULL) != 0) err = true;
if(pthread_mutex_init(bdb->cmtx, NULL) != 0) err = true;
if(err){
TCFREE(bdb->cmtx);
TCFREE(bdb->mmtx);
bdb->cmtx = NULL;
bdb->mmtx = NULL;
return false;
}
return tchdbsetmutex(bdb->hdb);
}
/* Set the custom comparison function of a B+ tree database object. */
bool tcbdbsetcmpfunc(TCBDB *bdb, TCCMP cmp, void *cmpop){
assert(bdb && cmp);
if(bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
bdb->cmp = cmp;
bdb->cmpop = cmpop;
return true;
}
/* Set the tuning parameters of a B+ tree database object. */
bool tcbdbtune(TCBDB *bdb, int32_t lmemb, int32_t nmemb,
int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts){
assert(bdb);
if(bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
bdb->lmemb = (lmemb > 0) ? tclmax(lmemb, BDBMINLMEMB) : BDBDEFLMEMB;
bdb->nmemb = (nmemb > 0) ? tclmax(nmemb, BDBMINNMEMB) : BDBDEFNMEMB;
bdb->opts = opts;
uint8_t hopts = 0;
if(opts & BDBTLARGE) hopts |= HDBTLARGE;
if(opts & BDBTDEFLATE) hopts |= HDBTDEFLATE;
if(opts & BDBTBZIP) hopts |= HDBTBZIP;
if(opts & BDBTTCBS) hopts |= HDBTTCBS;
if(opts & BDBTEXCODEC) hopts |= HDBTEXCODEC;
bnum = (bnum > 0) ? bnum : BDBDEFBNUM;
apow = (apow >= 0) ? apow : BDBDEFAPOW;
fpow = (fpow >= 0) ? fpow : BDBDEFFPOW;
return tchdbtune(bdb->hdb, bnum, apow, fpow, hopts);
}
/* Set the caching parameters of a B+ tree database object. */
bool tcbdbsetcache(TCBDB *bdb, int32_t lcnum, int32_t ncnum){
assert(bdb);
if(bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
if(lcnum > 0) bdb->lcnum = tclmax(lcnum, BDBLEVELMAX);
if(ncnum > 0) bdb->ncnum = tclmax(ncnum, BDBLEVELMAX);
return true;
}
/* Set the size of the extra mapped memory of a B+ tree database object. */
bool tcbdbsetxmsiz(TCBDB *bdb, int64_t xmsiz){
assert(bdb);
if(bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
return tchdbsetxmsiz(bdb->hdb, xmsiz);
}
/* Set the unit step number of auto defragmentation of a B+ tree database object. */
bool tcbdbsetdfunit(TCBDB *bdb, int32_t dfunit){
assert(bdb);
if(bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
return tchdbsetdfunit(bdb->hdb, dfunit);
}
/* Open a database file and connect a B+ tree database object. */
bool tcbdbopen(TCBDB *bdb, const char *path, int omode){
assert(bdb && path);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbopenimpl(bdb, path, omode);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Close a B+ tree database object. */
bool tcbdbclose(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbcloseimpl(bdb);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Store a record into a B+ tree database object. */
bool tcbdbput(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(bdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbputimpl(bdb, kbuf, ksiz, vbuf, vsiz, BDBPDOVER);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Store a string record into a B+ tree database object. */
bool tcbdbput2(TCBDB *bdb, const char *kstr, const char *vstr){
assert(bdb && kstr && vstr);
return tcbdbput(bdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Store a new record into a B+ tree database object. */
bool tcbdbputkeep(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(bdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbputimpl(bdb, kbuf, ksiz, vbuf, vsiz, BDBPDKEEP);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Store a new string record into a B+ tree database object. */
bool tcbdbputkeep2(TCBDB *bdb, const char *kstr, const char *vstr){
assert(bdb && kstr && vstr);
return tcbdbputkeep(bdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Concatenate a value at the end of the existing record in a B+ tree database object. */
bool tcbdbputcat(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(bdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbputimpl(bdb, kbuf, ksiz, vbuf, vsiz, BDBPDCAT);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Concatenate a string value at the end of the existing record in a B+ tree database object. */
bool tcbdbputcat2(TCBDB *bdb, const char *kstr, const char *vstr){
assert(bdb && kstr && vstr);
return tcbdbputcat(bdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Store a record into a B+ tree database object with allowing duplication of keys. */
bool tcbdbputdup(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(bdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbputimpl(bdb, kbuf, ksiz, vbuf, vsiz, BDBPDDUP);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Store a string record into a B+ tree database object with allowing duplication of keys. */
bool tcbdbputdup2(TCBDB *bdb, const char *kstr, const char *vstr){
assert(bdb && kstr && vstr);
return tcbdbputdup(bdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Store records into a B+ tree database object with allowing duplication of keys. */
bool tcbdbputdup3(TCBDB *bdb, const void *kbuf, int ksiz, const TCLIST *vals){
assert(bdb && kbuf && ksiz >= 0 && vals);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool err = false;
int ln = TCLISTNUM(vals);
for(int i = 0; i < ln; i++){
const char *vbuf;
int vsiz;
TCLISTVAL(vbuf, vals, i, vsiz);
if(!tcbdbputimpl(bdb, kbuf, ksiz, vbuf, vsiz, BDBPDDUP)) err = true;
}
BDBUNLOCKMETHOD(bdb);
return !err;
}
/* Remove a record of a B+ tree database object. */
bool tcbdbout(TCBDB *bdb, const void *kbuf, int ksiz){
assert(bdb && kbuf && ksiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdboutimpl(bdb, kbuf, ksiz);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Remove a string record of a B+ tree database object. */
bool tcbdbout2(TCBDB *bdb, const char *kstr){
assert(bdb && kstr);
return tcbdbout(bdb, kstr, strlen(kstr));
}
/* Remove records of a B+ tree database object. */
bool tcbdbout3(TCBDB *bdb, const void *kbuf, int ksiz){
assert(bdb && kbuf && ksiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdboutlist(bdb, kbuf, ksiz);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Retrieve a record in a B+ tree database object. */
void *tcbdbget(TCBDB *bdb, const void *kbuf, int ksiz, int *sp){
assert(bdb && kbuf && ksiz >= 0 && sp);
if(!BDBLOCKMETHOD(bdb, false)) return NULL;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return NULL;
}
const char *vbuf = tcbdbgetimpl(bdb, kbuf, ksiz, sp);
char *rv;
if(vbuf){
TCMEMDUP(rv, vbuf, *sp);
} else {
rv = NULL;
}
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
if(!bdb->tran && !tcbdbcacheadjust(bdb)){
TCFREE(rv);
rv = NULL;
}
BDBUNLOCKMETHOD(bdb);
}
return rv;
}
/* Retrieve a string record in a B+ tree database object. */
char *tcbdbget2(TCBDB *bdb, const char *kstr){
assert(bdb && kstr);
int vsiz;
return tcbdbget(bdb, kstr, strlen(kstr), &vsiz);
}
/* Retrieve a record in a B+ tree database object and write the value into a buffer. */
const void *tcbdbget3(TCBDB *bdb, const void *kbuf, int ksiz, int *sp){
assert(bdb && kbuf && ksiz >= 0 && sp);
if(!BDBLOCKMETHOD(bdb, false)) return NULL;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return NULL;
}
const char *rv = tcbdbgetimpl(bdb, kbuf, ksiz, sp);
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = NULL;
BDBUNLOCKMETHOD(bdb);
}
return rv;
}
/* Retrieve records in a B+ tree database object. */
TCLIST *tcbdbget4(TCBDB *bdb, const void *kbuf, int ksiz){
assert(bdb && kbuf && ksiz >= 0);
if(!BDBLOCKMETHOD(bdb, false)) return NULL;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return NULL;
}
TCLIST *rv = tcbdbgetlist(bdb, kbuf, ksiz);
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
if(!bdb->tran && !tcbdbcacheadjust(bdb)){
if(rv) tclistdel(rv);
rv = NULL;
}
BDBUNLOCKMETHOD(bdb);
}
return rv;
}
/* Get the number of records corresponding a key in a B+ tree database object. */
int tcbdbvnum(TCBDB *bdb, const void *kbuf, int ksiz){
assert(bdb && kbuf && ksiz >= 0);
if(!BDBLOCKMETHOD(bdb, false)) return 0;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return 0;
}
int rv = tcbdbgetnum(bdb, kbuf, ksiz);
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = 0;
BDBUNLOCKMETHOD(bdb);
}
return rv;
}
/* Get the number of records corresponding a string key in a B+ tree database object. */
int tcbdbvnum2(TCBDB *bdb, const char *kstr){
assert(bdb && kstr);
return tcbdbvnum(bdb, kstr, strlen(kstr));
}
/* Get the size of the value of a record in a B+ tree database object. */
int tcbdbvsiz(TCBDB *bdb, const void *kbuf, int ksiz){
assert(bdb && kbuf && ksiz >= 0);
int vsiz;
if(!tcbdbget3(bdb, kbuf, ksiz, &vsiz)) return -1;
return vsiz;
}
/* Get the size of the value of a string record in a B+ tree database object. */
int tcbdbvsiz2(TCBDB *bdb, const char *kstr){
assert(bdb && kstr);
return tcbdbvsiz(bdb, kstr, strlen(kstr));
}
/* Get keys of ranged records in a B+ tree database object. */
TCLIST *tcbdbrange(TCBDB *bdb, const void *bkbuf, int bksiz, bool binc,
const void *ekbuf, int eksiz, bool einc, int max){
assert(bdb);
TCLIST *keys = tclistnew();
if(!BDBLOCKMETHOD(bdb, false)) return keys;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return keys;
}
tcbdbrangeimpl(bdb, bkbuf, bksiz, binc, ekbuf, eksiz, einc, max, keys);
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
tcbdbcacheadjust(bdb);
BDBUNLOCKMETHOD(bdb);
}
return keys;
}
/* Get string keys of ranged records in a B+ tree database object. */
TCLIST *tcbdbrange2(TCBDB *bdb, const char *bkstr, bool binc,
const char *ekstr, bool einc, int max){
assert(bdb);
return tcbdbrange(bdb, bkstr, bkstr ? strlen(bkstr) : 0, binc,
ekstr, ekstr ? strlen(ekstr) : 0, einc, max);
}
/* Get forward matching keys in a B+ tree database object. */
TCLIST *tcbdbfwmkeys(TCBDB *bdb, const void *pbuf, int psiz, int max){
assert(bdb && pbuf && psiz >= 0);
TCLIST *keys = tclistnew();
if(!BDBLOCKMETHOD(bdb, false)) return keys;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return keys;
}
tcbdbrangefwm(bdb, pbuf, psiz, max, keys);
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
tcbdbcacheadjust(bdb);
BDBUNLOCKMETHOD(bdb);
}
return keys;
}
/* Get forward matching string keys in a B+ tree database object. */
TCLIST *tcbdbfwmkeys2(TCBDB *bdb, const char *pstr, int max){
assert(bdb && pstr);
return tcbdbfwmkeys(bdb, pstr, strlen(pstr), max);
}
/* Add an integer to a record in a B+ tree database object. */
int tcbdbaddint(TCBDB *bdb, const void *kbuf, int ksiz, int num){
assert(bdb && kbuf && ksiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return INT_MIN;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return INT_MIN;
}
bool rv = tcbdbputimpl(bdb, kbuf, ksiz, (char *)&num, sizeof(num), BDBPDADDINT);
BDBUNLOCKMETHOD(bdb);
return rv ? num : INT_MIN;
}
/* Add a real number to a record in a B+ tree database object. */
double tcbdbadddouble(TCBDB *bdb, const void *kbuf, int ksiz, double num){
assert(bdb && kbuf && ksiz >= 0);
if(!BDBLOCKMETHOD(bdb, true)) return nan("");
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return nan("");
}
bool rv = tcbdbputimpl(bdb, kbuf, ksiz, (char *)&num, sizeof(num), BDBPDADDDBL);
BDBUNLOCKMETHOD(bdb);
return rv ? num : nan("");
}
/* Synchronize updated contents of a B+ tree database object with the file and the device. */
bool tcbdbsync(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode || bdb->tran){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbmemsync(bdb, true);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Optimize the file of a B+ tree database object. */
bool tcbdboptimize(TCBDB *bdb, int32_t lmemb, int32_t nmemb,
int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode || bdb->tran){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
BDBTHREADYIELD(bdb);
bool rv = tcbdboptimizeimpl(bdb, lmemb, nmemb, bnum, apow, fpow, opts);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Remove all records of a B+ tree database object. */
bool tcbdbvanish(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode || bdb->tran){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
BDBTHREADYIELD(bdb);
bool rv = tcbdbvanishimpl(bdb);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Copy the database file of a B+ tree database object. */
bool tcbdbcopy(TCBDB *bdb, const char *path){
assert(bdb && path);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
BDBTHREADYIELD(bdb);
TCLIST *lids = tclistnew();
TCLIST *nids = tclistnew();
const char *vbuf;
int vsiz;
TCMAP *leafc = bdb->leafc;
tcmapiterinit(leafc);
while((vbuf = tcmapiternext(leafc, &vsiz)) != NULL){
TCLISTPUSH(lids, vbuf, vsiz);
}
TCMAP *nodec = bdb->nodec;
tcmapiterinit(nodec);
while((vbuf = tcmapiternext(nodec, &vsiz)) != NULL){
TCLISTPUSH(nids, vbuf, vsiz);
}
BDBUNLOCKMETHOD(bdb);
bool err = false;
int ln = TCLISTNUM(lids);
for(int i = 0; i < ln; i++){
vbuf = TCLISTVALPTR(lids, i);
if(BDBLOCKMETHOD(bdb, true)){
BDBTHREADYIELD(bdb);
if(bdb->open){
int rsiz;
BDBLEAF *leaf = (BDBLEAF *)tcmapget(bdb->leafc, vbuf, sizeof(leaf->id), &rsiz);
if(leaf && leaf->dirty && !tcbdbleafsave(bdb, leaf)) err = true;
} else {
err = true;
}
BDBUNLOCKMETHOD(bdb);
} else {
err = true;
}
}
ln = TCLISTNUM(nids);
for(int i = 0; i < ln; i++){
vbuf = TCLISTVALPTR(nids, i);
if(BDBLOCKMETHOD(bdb, true)){
if(bdb->open){
int rsiz;
BDBNODE *node = (BDBNODE *)tcmapget(bdb->nodec, vbuf, sizeof(node->id), &rsiz);
if(node && node->dirty && !tcbdbnodesave(bdb, node)) err = true;
} else {
err = true;
}
BDBUNLOCKMETHOD(bdb);
} else {
err = true;
}
}
tclistdel(nids);
tclistdel(lids);
if(!tcbdbtranbegin(bdb)) err = true;
if(BDBLOCKMETHOD(bdb, false)){
BDBTHREADYIELD(bdb);
if(!tchdbcopy(bdb->hdb, path)) err = true;
BDBUNLOCKMETHOD(bdb);
} else {
err = true;
}
if(!tcbdbtrancommit(bdb)) err = true;
return !err;
}
/* Begin the transaction of a B+ tree database object. */
bool tcbdbtranbegin(TCBDB *bdb){
assert(bdb);
for(double wsec = 1.0 / sysconf(_SC_CLK_TCK); true; wsec *= 2){
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
if(!bdb->tran) break;
BDBUNLOCKMETHOD(bdb);
if(wsec > 1.0) wsec = 1.0;
tcsleep(wsec);
}
if(!tcbdbmemsync(bdb, false)){
BDBUNLOCKMETHOD(bdb);
return false;
}
if(!tchdbtranbegin(bdb->hdb)){
BDBUNLOCKMETHOD(bdb);
return false;
}
bdb->tran = true;
TCMEMDUP(bdb->rbopaque, bdb->opaque, BDBOPAQUESIZ);
BDBUNLOCKMETHOD(bdb);
return true;
}
/* Commit the transaction of a B+ tree database object. */
bool tcbdbtrancommit(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode || !bdb->tran){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
TCFREE(bdb->rbopaque);
bdb->tran = false;
bdb->rbopaque = NULL;
bool err = false;
if(!tcbdbmemsync(bdb, false)) err = true;
if(!tcbdbcacheadjust(bdb)) err = true;
if(err){
tchdbtranabort(bdb->hdb);
} else if(!tchdbtrancommit(bdb->hdb)){
err = true;
}
BDBUNLOCKMETHOD(bdb);
return !err;
}
/* Abort the transaction of a B+ tree database object. */
bool tcbdbtranabort(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, true)) return false;
if(!bdb->open || !bdb->wmode || !bdb->tran){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
tcbdbcachepurge(bdb);
memcpy(bdb->opaque, bdb->rbopaque, BDBOPAQUESIZ);
tcbdbloadmeta(bdb);
TCFREE(bdb->rbopaque);
bdb->tran = false;
bdb->rbopaque = NULL;
bdb->hleaf = 0;
bdb->lleaf = 0;
bdb->clock++;
bool err = false;
if(!tcbdbcacheadjust(bdb)) err = true;
if(!tchdbtranvoid(bdb->hdb)) err = true;
BDBUNLOCKMETHOD(bdb);
return !err;
}
/* Get the file path of a B+ tree database object. */
const char *tcbdbpath(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, false)) return NULL;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return NULL;
}
const char *rv = tchdbpath(bdb->hdb);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Get the number of records of a B+ tree database object. */
uint64_t tcbdbrnum(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, false)) return 0;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return 0;
}
uint64_t rv = bdb->rnum;
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Get the size of the database file of a B+ tree database object. */
uint64_t tcbdbfsiz(TCBDB *bdb){
assert(bdb);
if(!BDBLOCKMETHOD(bdb, false)) return 0;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return 0;
}
uint64_t rv = tchdbfsiz(bdb->hdb);
BDBUNLOCKMETHOD(bdb);
return rv;
}
/* Create a cursor object. */
BDBCUR *tcbdbcurnew(TCBDB *bdb){
assert(bdb);
BDBCUR *cur;
TCMALLOC(cur, sizeof(*cur));
cur->bdb = bdb;
cur->clock = 0;
cur->id = 0;
cur->kidx = 0;
cur->vidx = 0;
return cur;
}
/* Delete a cursor object. */
void tcbdbcurdel(BDBCUR *cur){
assert(cur);
TCFREE(cur);
}
/* Move a cursor object to the first record. */
bool tcbdbcurfirst(BDBCUR *cur){
assert(cur);
TCBDB *bdb = cur->bdb;
if(!BDBLOCKMETHOD(bdb, false)) return false;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbcurfirstimpl(cur);
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = false;
BDBUNLOCKMETHOD(bdb);
}
return rv;
}
/* Move a cursor object to the last record. */
bool tcbdbcurlast(BDBCUR *cur){
assert(cur);
TCBDB *bdb = cur->bdb;
if(!BDBLOCKMETHOD(bdb, false)) return false;
if(!bdb->open){
tcbdbsetecode(bdb, TCEINVALID, __FILE__, __LINE__, __func__);
BDBUNLOCKMETHOD(bdb);
return false;
}
bool rv = tcbdbcurlastimpl(cur);
bool adj = TCMAPRNUM(bdb->leafc) > bdb->lcnum || TCMAPRNUM(bdb->nodec) > bdb->ncnum;
BDBUNLOCKMETHOD(bdb);
if(adj && BDBLOCKMETHOD(bdb, true)){
if(!bdb->tran && !tcbdbcacheadjust(bdb)) rv = false;