-
Notifications
You must be signed in to change notification settings - Fork 0
/
tctdb.c
6199 lines (5864 loc) · 190 KB
/
tctdb.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 table database API of Tokyo Cabinet
* Copyright (C) 2006-2012 FAL Labs
* 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 "tctdb.h"
#include "myconf.h"
#define TDBOPAQUESIZ 64 // size of using opaque field
#define TDBLEFTOPQSIZ 64 // size of left opaque field
#define TDBPAGEBUFSIZ 32768 // size of a buffer to read each page
#define TDBDEFBNUM 131071 // default bucket number
#define TDBDEFAPOW 4 // default alignment power
#define TDBDEFFPOW 10 // default free block pool power
#define TDBDEFLCNUM 4096 // default number of leaf cache
#define TDBDEFNCNUM 512 // default number of node cache
#define TDBDEFXMSIZ (64LL<<20) // default size of the extra mapped memory
#define TDBIDXSUFFIX "idx" // suffix of column index file
#define TDBIDXLMEMB 64 // number of members in each leaf of the index
#define TDBIDXNMEMB 256 // number of members in each node of the index
#define TDBIDXLSMAX 4096 // maximum size of each leaf of the index
#define TDBIDXICCBNUM 262139 // bucket number of the index cache
#define TDBIDXICCMAX (64LL<<20) // maximum size of the index cache
#define TDBIDXICCSYNC 0.01 // ratio of cache synchronization
#define TDBIDXQGUNIT 3 // unit number of the q-gram index
#define TDBFTSUNITMAX 32 // maximum number of full-text search units
#define TDBFTSOCRUNIT 8192 // maximum number of full-text search units
#define TDBFTSBMNUM 524287 // number of elements of full-text search bitmap
#define TDBNUMCNTCOL "_num" // column name of number counting
#define TDBCOLBUFSIZ 1024 // size of a buffer for a column value
#define TDBNUMCOLMAX 16 // maximum number of columns of the long double
#define TDBHINTUSIZ 256 // unit size of the hint string
#define TDBORDRATIO 0.2 // ratio of records to use the order index
enum { // enumeration for duplication behavior
TDBPDOVER, // overwrite an existing value
TDBPDKEEP, // keep the existing value
TDBPDCAT // concatenate values
};
typedef struct { // type of structure for a sort record
const char *kbuf; // pointer to the primary key
int ksiz; // size of the primary key
char *vbuf; // pointer to the value
int vsiz; // size of the value
} TDBSORTREC;
typedef struct { // type of structure for a full-text search unit
TCLIST *tokens; // q-gram tokens
bool sign; // positive sign
} TDBFTSUNIT;
typedef struct { // type of structure for a full-text string occurrence
const char *pkbuf; // primary key string
int32_t pksiz; // size of the primary key
int32_t off; // offset of the token
uint16_t seq; // sequence number
uint16_t hash; // hash value for counting sort
} TDBFTSSTROCR;
typedef struct { // type of structure for a full-text number occurrence
int64_t pkid; // primery key number
int32_t off; // offset of the token
uint16_t seq; // sequence number
uint16_t hash; // hash value for counting sort
} TDBFTSNUMOCR;
/* private macros */
#define TDBLOCKMETHOD(TC_tdb, TC_wr) \
((TC_tdb)->mmtx ? tctdblockmethod((TC_tdb), (TC_wr)) : true)
#define TDBUNLOCKMETHOD(TC_tdb) \
((TC_tdb)->mmtx ? tctdbunlockmethod(TC_tdb) : true)
#define TDBTHREADYIELD(TC_tdb) \
do { if((TC_tdb)->mmtx) sched_yield(); } while(false)
/* private function prototypes */
static void tctdbclear(TCTDB *tdb);
static bool tctdbopenimpl(TCTDB *tdb, const char *path, int omode);
static bool tctdbcloseimpl(TCTDB *tdb);
static bool tctdbputimpl(TCTDB *tdb, const void *pkbuf, int pksiz, TCMAP *cols, int dmode);
static bool tctdboutimpl(TCTDB *tdb, const char *pkbuf, int pksiz);
static TCMAP *tctdbgetimpl(TCTDB *tdb, const void *pkbuf, int pksiz);
static char *tctdbgetonecol(TCTDB *tdb, const void *pkbuf, int pksiz,
const void *nbuf, int nsiz, int *sp);
static double tctdbaddnumber(TCTDB *tdb, const void *pkbuf, int pksiz, double num);
static bool tctdboptimizeimpl(TCTDB *tdb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts);
static bool tctdbvanishimpl(TCTDB *tdb);
static bool tctdbcopyimpl(TCTDB *tdb, const char *path);
static bool tctdbtranbeginimpl(TCTDB *tdb);
static bool tctdbtrancommitimpl(TCTDB *tdb);
static bool tctdbtranabortimpl(TCTDB *tdb);
static bool tctdbsetindeximpl(TCTDB *tdb, const char *name, int type);
static int64_t tctdbgenuidimpl(TCTDB *tdb, int64_t inc);
static TCLIST *tctdbqrysearchimpl(TDBQRY *qry);
static TCMAP *tctdbqryidxfetch(TDBQRY *qry, TDBCOND *cond, TDBIDX *idx);
static long double tctdbatof(const char *str);
static bool tctdbqryidxcurjumpnum(BDBCUR *cur, const char *kbuf, int ksiz, bool first);
static bool tctdbqryonecondmatch(TDBQRY *qry, TDBCOND *cond, const char *pkbuf, int pksiz);
static bool tctdbqryallcondmatch(TDBQRY *qry, const char *pkbuf, int pksiz);
static bool tctdbqrycondmatch(TDBCOND *cond, const char *vbuf, int vsiz);
static bool tctdbqrycondcheckstrand(const char *tval, const char *oval);
static bool tctdbqrycondcheckstror(const char *tval, const char *oval);
static bool tctdbqrycondcheckstroreq(const char *vbuf, const char *expr);
static bool tctdbqrycondchecknumbt(const char *vbuf, const char *expr);
static bool tctdbqrycondchecknumoreq(const char *vbuf, const char *expr);
static bool tctdbqrycondcheckfts(const char *vbuf, int vsiz, TDBCOND *cond);
static int tdbcmppkeynumasc(const TCLISTDATUM *a, const TCLISTDATUM *b);
static int tdbcmppkeynumdesc(const TCLISTDATUM *a, const TCLISTDATUM *b);
static int tdbcmpsortrecstrasc(const TDBSORTREC *a, const TDBSORTREC *b);
static int tdbcmpsortrecstrdesc(const TDBSORTREC *a, const TDBSORTREC *b);
static int tdbcmpsortrecnumasc(const TDBSORTREC *a, const TDBSORTREC *b);
static int tdbcmpsortrecnumdesc(const TDBSORTREC *a, const TDBSORTREC *b);
static uint16_t tctdbidxhash(const char *pkbuf, int pksiz);
static bool tctdbidxput(TCTDB *tdb, const void *pkbuf, int pksiz, TCMAP *cols);
static bool tctdbidxputone(TCTDB *tdb, TDBIDX *idx, const char *pkbuf, int pksiz, uint16_t hash,
const char *vbuf, int vsiz);
static bool tctdbidxputtoken(TCTDB *tdb, TDBIDX *idx, const char *pkbuf, int pksiz,
const char *vbuf, int vsiz);
static bool tctdbidxputqgram(TCTDB *tdb, TDBIDX *idx, const char *pkbuf, int pksiz,
const char *vbuf, int vsiz);
static bool tctdbidxout(TCTDB *tdb, const void *pkbuf, int pksiz, TCMAP *cols);
static bool tctdbidxoutone(TCTDB *tdb, TDBIDX *idx, const char *pkbuf, int pksiz, uint16_t hash,
const char *vbuf, int vsiz);
static bool tctdbidxouttoken(TCTDB *tdb, TDBIDX *idx, const char *pkbuf, int pksiz,
const char *vbuf, int vsiz);
static bool tctdbidxoutqgram(TCTDB *tdb, TDBIDX *idx, const char *pkbuf, int pksiz,
const char *vbuf, int vsiz);
static bool tctdbidxsyncicc(TCTDB *tdb, TDBIDX *idx, bool all);
static int tctdbidxcmpkey(const char **a, const char **b);
static TCMAP *tctdbidxgetbytokens(TCTDB *tdb, TDBIDX *idx, const TCLIST *tokens, int op,
TCXSTR *hint);
static TCMAP *tctdbidxgetbyfts(TCTDB *tdb, TDBIDX *idx, TDBCOND *cond, TCXSTR *hint);
static void tctdbidxgetbyftsunion(TDBIDX *idx, const TCLIST *tokens, bool sign,
TCMAP *ores, TCMAP *nres, TCXSTR *hint);
static int tctdbidxftscmpstrocr(TDBFTSSTROCR *a, TDBFTSSTROCR *b);
static int tctdbidxftscmpnumocr(TDBFTSNUMOCR *a, TDBFTSNUMOCR *b);
static TDBFTSUNIT *tctdbftsparseexpr(const char *expr, int esiz, int op, int *np);
static bool tctdbdefragimpl(TCTDB *tdb, int64_t step);
static bool tctdbcacheclearimpl(TCTDB *tdb);
static bool tctdbforeachimpl(TCTDB *tdb, TCITER iter, void *op);
static int tctdbqryprocoutcb(const void *pkbuf, int pksiz, TCMAP *cols, void *op);
static bool tctdblockmethod(TCTDB *tdb, bool wr);
static bool tctdbunlockmethod(TCTDB *tdb);
/* debugging function prototypes */
void tctdbprintmeta(TCTDB *tdb);
/*************************************************************************************************
* API
*************************************************************************************************/
/* Get the message string corresponding to an error code. */
const char *tctdberrmsg(int ecode){
return tcerrmsg(ecode);
}
/* Create a table database object. */
TCTDB *tctdbnew(void){
TCTDB *tdb;
TCMALLOC(tdb, sizeof(*tdb));
tctdbclear(tdb);
tdb->hdb = tchdbnew();
tchdbtune(tdb->hdb, TDBDEFBNUM, TDBDEFAPOW, TDBDEFFPOW, 0);
tchdbsetxmsiz(tdb->hdb, TDBDEFXMSIZ);
return tdb;
}
/* Delete a table database object. */
void tctdbdel(TCTDB *tdb){
assert(tdb);
if(tdb->open) tctdbclose(tdb);
tchdbdel(tdb->hdb);
if(tdb->mmtx){
pthread_rwlock_destroy(tdb->mmtx);
TCFREE(tdb->mmtx);
}
TCFREE(tdb);
}
/* Get the last happened error code of a table database object. */
int tctdbecode(TCTDB *tdb){
assert(tdb);
return tchdbecode(tdb->hdb);
}
/* Set mutual exclusion control of a table database object for threading. */
bool tctdbsetmutex(TCTDB *tdb){
assert(tdb);
if(!TCUSEPTHREAD) return true;
if(tdb->mmtx || tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
TCMALLOC(tdb->mmtx, sizeof(pthread_rwlock_t));
bool err = false;
if(pthread_rwlock_init(tdb->mmtx, NULL) != 0) err = true;
if(err){
TCFREE(tdb->mmtx);
tdb->mmtx = NULL;
return false;
}
return tchdbsetmutex(tdb->hdb);
}
/* Set the tuning parameters of a table database object. */
bool tctdbtune(TCTDB *tdb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts){
assert(tdb);
if(tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
tdb->opts = opts;
uint8_t hopts = 0;
if(opts & TDBTLARGE) hopts |= HDBTLARGE;
if(opts & TDBTDEFLATE) hopts |= HDBTDEFLATE;
if(opts & TDBTBZIP) hopts |= HDBTBZIP;
if(opts & TDBTTCBS) hopts |= HDBTTCBS;
if(opts & TDBTEXCODEC) hopts |= HDBTEXCODEC;
bnum = (bnum > 0) ? bnum : TDBDEFBNUM;
apow = (apow >= 0) ? apow : TDBDEFAPOW;
fpow = (fpow >= 0) ? fpow : TDBDEFFPOW;
return tchdbtune(tdb->hdb, bnum, apow, fpow, hopts);
}
/* Set the caching parameters of a table database object. */
bool tctdbsetcache(TCTDB *tdb, int32_t rcnum, int32_t lcnum, int32_t ncnum){
assert(tdb);
if(tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
if(lcnum > 0) tdb->lcnum = lcnum;
if(ncnum > 0) tdb->ncnum = ncnum;
return tchdbsetcache(tdb->hdb, rcnum);
}
/* Set the size of the extra mapped memory of a table database object. */
bool tctdbsetxmsiz(TCTDB *tdb, int64_t xmsiz){
assert(tdb);
if(tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
return tchdbsetxmsiz(tdb->hdb, xmsiz);
}
/* Set the unit step number of auto defragmentation of a table database object. */
bool tctdbsetdfunit(TCTDB *tdb, int32_t dfunit){
assert(tdb);
if(tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
return tchdbsetdfunit(tdb->hdb, dfunit);
}
/* Open a database file and connect a table database object. */
bool tctdbopen(TCTDB *tdb, const char *path, int omode){
assert(tdb && path);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tctdbopenimpl(tdb, path, omode);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Close a table database object. */
bool tctdbclose(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tctdbcloseimpl(tdb);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Store a record into a table database object. */
bool tctdbput(TCTDB *tdb, const void *pkbuf, int pksiz, TCMAP *cols){
assert(tdb && pkbuf && pksiz >= 0 && cols);
int vsiz;
if(tcmapget(cols, "", 0, &vsiz)){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tctdbputimpl(tdb, pkbuf, pksiz, cols, TDBPDOVER);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Store a string record into a table database object with a zero separated column string. */
bool tctdbput2(TCTDB *tdb, const void *pkbuf, int pksiz, const void *cbuf, int csiz){
assert(tdb && pkbuf && pksiz >= 0 && cbuf && csiz >= 0);
TCMAP *cols = tcstrsplit4(cbuf, csiz);
bool rv = tctdbput(tdb, pkbuf, pksiz, cols);
tcmapdel(cols);
return rv;
}
/* Store a string record into a table database object with a tab separated column string. */
bool tctdbput3(TCTDB *tdb, const char *pkstr, const char *cstr){
assert(tdb && pkstr && cstr);
TCMAP *cols = tcstrsplit3(cstr, "\t");
bool rv = tctdbput(tdb, pkstr, strlen(pkstr), cols);
tcmapdel(cols);
return rv;
}
/* Store a new record into a table database object. */
bool tctdbputkeep(TCTDB *tdb, const void *pkbuf, int pksiz, TCMAP *cols){
assert(tdb && pkbuf && pksiz >= 0 && cols);
int vsiz;
if(tcmapget(cols, "", 0, &vsiz)){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tctdbputimpl(tdb, pkbuf, pksiz, cols, TDBPDKEEP);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Store a new string record into a table database object with a zero separated column string. */
bool tctdbputkeep2(TCTDB *tdb, const void *pkbuf, int pksiz, const void *cbuf, int csiz){
assert(tdb && pkbuf && pksiz >= 0 && cbuf && csiz >= 0);
TCMAP *cols = tcstrsplit4(cbuf, csiz);
bool rv = tctdbputkeep(tdb, pkbuf, pksiz, cols);
tcmapdel(cols);
return rv;
}
/* Store a new string record into a table database object with a tab separated column string. */
bool tctdbputkeep3(TCTDB *tdb, const char *pkstr, const char *cstr){
assert(tdb && pkstr && cstr);
TCMAP *cols = tcstrsplit3(cstr, "\t");
bool rv = tctdbputkeep(tdb, pkstr, strlen(pkstr), cols);
tcmapdel(cols);
return rv;
}
/* Concatenate columns of the existing record in a table database object. */
bool tctdbputcat(TCTDB *tdb, const void *pkbuf, int pksiz, TCMAP *cols){
assert(tdb && pkbuf && pksiz >= 0 && cols);
int vsiz;
if(tcmapget(cols, "", 0, &vsiz)){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tctdbputimpl(tdb, pkbuf, pksiz, cols, TDBPDCAT);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Concatenate columns in a table database object with a zero separated column string. */
bool tctdbputcat2(TCTDB *tdb, const void *pkbuf, int pksiz, const void *cbuf, int csiz){
assert(tdb && pkbuf && pksiz >= 0 && cbuf && csiz >= 0);
TCMAP *cols = tcstrsplit4(cbuf, csiz);
bool rv = tctdbputcat(tdb, pkbuf, pksiz, cols);
tcmapdel(cols);
return rv;
}
/* Concatenate columns in a table database object with with a tab separated column string. */
bool tctdbputcat3(TCTDB *tdb, const char *pkstr, const char *cstr){
assert(tdb && pkstr && cstr);
TCMAP *cols = tcstrsplit3(cstr, "\t");
bool rv = tctdbputcat(tdb, pkstr, strlen(pkstr), cols);
tcmapdel(cols);
return rv;
}
/* Remove a record of a table database object. */
bool tctdbout(TCTDB *tdb, const void *pkbuf, int pksiz){
assert(tdb && pkbuf && pksiz >= 0);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tctdboutimpl(tdb, pkbuf, pksiz);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Remove a string record of a table database object. */
bool tctdbout2(TCTDB *tdb, const char *pkstr){
assert(tdb && pkstr);
return tctdbout(tdb, pkstr, strlen(pkstr));
}
/* Retrieve a record in a table database object. */
TCMAP *tctdbget(TCTDB *tdb, const void *pkbuf, int pksiz){
assert(tdb && pkbuf && pksiz >= 0);
if(!TDBLOCKMETHOD(tdb, false)) return NULL;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return NULL;
}
TCMAP *rv = tctdbgetimpl(tdb, pkbuf, pksiz);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Retrieve a record in a table database object as a zero separated column string. */
char *tctdbget2(TCTDB *tdb, const void *pkbuf, int pksiz, int *sp){
assert(tdb && pkbuf && pksiz >= 0 && sp);
TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
if(!cols) return NULL;
char *cbuf = tcstrjoin4(cols, sp);
tcmapdel(cols);
return cbuf;
}
/* Retrieve a string record in a table database object as a tab separated column string. */
char *tctdbget3(TCTDB *tdb, const char *pkstr){
assert(tdb && pkstr);
TCMAP *cols = tctdbget(tdb, pkstr, strlen(pkstr));
if(!cols) return NULL;
char *cstr = tcstrjoin3(cols, '\t');
tcmapdel(cols);
return cstr;
}
/* Get the size of the value of a record in a table database object. */
int tctdbvsiz(TCTDB *tdb, const void *pkbuf, int pksiz){
assert(tdb && pkbuf && pksiz >= 0);
if(!TDBLOCKMETHOD(tdb, false)) return -1;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return -1;
}
int rv = tchdbvsiz(tdb->hdb, pkbuf, pksiz);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Get the size of the value of a string record in a table database object. */
int tctdbvsiz2(TCTDB *tdb, const char *pkstr){
assert(tdb && pkstr);
return tctdbvsiz(tdb, pkstr, strlen(pkstr));
}
/* Initialize the iterator of a table database object. */
bool tctdbiterinit(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tchdbiterinit(tdb->hdb);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Get the next primary key of the iterator of a table database object. */
void *tctdbiternext(TCTDB *tdb, int *sp){
assert(tdb && sp);
if(!TDBLOCKMETHOD(tdb, true)) return NULL;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return NULL;
}
char *rv = tchdbiternext(tdb->hdb, sp);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Get the next primary key string of the iterator of a table database object. */
char *tctdbiternext2(TCTDB *tdb){
assert(tdb);
int pksiz;
return tctdbiternext(tdb, &pksiz);
}
/* Get the columns of the next record of the iterator of a table database object. */
TCMAP *tctdbiternext3(TCTDB *tdb){
assert(tdb);
TCXSTR *kstr = tcxstrnew();
TCXSTR *vstr = tcxstrnew();
TCMAP *cols = NULL;
if(tchdbiternext3(tdb->hdb, kstr, vstr)){
cols = tcmapload(TCXSTRPTR(vstr), TCXSTRSIZE(vstr));
tcmapput(cols, "", 0, TCXSTRPTR(kstr), TCXSTRSIZE(kstr));
}
tcxstrdel(vstr);
tcxstrdel(kstr);
return cols;
}
/* Get forward matching primary keys in a table database object. */
TCLIST *tctdbfwmkeys(TCTDB *tdb, const void *pbuf, int psiz, int max){
assert(tdb && pbuf && psiz >= 0);
if(!TDBLOCKMETHOD(tdb, true)) return tclistnew();
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return tclistnew();
}
TCLIST *rv = tchdbfwmkeys(tdb->hdb, pbuf, psiz, max);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Get forward matching string primary keys in a table database object. */
TCLIST *tctdbfwmkeys2(TCTDB *tdb, const char *pstr, int max){
assert(tdb && pstr);
return tctdbfwmkeys(tdb, pstr, strlen(pstr), max);
}
/* Add an integer to a column of a record in a table database object. */
int tctdbaddint(TCTDB *tdb, const void *pkbuf, int pksiz, int num){
assert(tdb && pkbuf && pksiz >= 0);
if(!TDBLOCKMETHOD(tdb, true)) return INT_MIN;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return INT_MIN;
}
double rv = tctdbaddnumber(tdb, pkbuf, pksiz, num);
TDBUNLOCKMETHOD(tdb);
return isnan(rv) ? INT_MIN : (int)rv;
}
/* Add a real number to a column of a record in a table database object. */
double tctdbadddouble(TCTDB *tdb, const void *pkbuf, int pksiz, double num){
assert(tdb && pkbuf && pksiz >= 0);
if(!TDBLOCKMETHOD(tdb, true)) return INT_MIN;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return INT_MIN;
}
double rv = tctdbaddnumber(tdb, pkbuf, pksiz, num);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Synchronize updated contents of a table database object with the file and the device. */
bool tctdbsync(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode || tdb->tran){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool rv = tctdbmemsync(tdb, true);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Optimize the file of a table database object. */
bool tctdboptimize(TCTDB *tdb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode || tdb->tran){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
TDBTHREADYIELD(tdb);
bool rv = tctdboptimizeimpl(tdb, bnum, apow, fpow, opts);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Remove all records of a table database object. */
bool tctdbvanish(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode || tdb->tran){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
TDBTHREADYIELD(tdb);
bool rv = tctdbvanishimpl(tdb);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Copy the database file of a table database object. */
bool tctdbcopy(TCTDB *tdb, const char *path){
assert(tdb && path);
if(!TDBLOCKMETHOD(tdb, false)) return false;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
TDBTHREADYIELD(tdb);
bool rv = tctdbcopyimpl(tdb, path);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Begin the transaction of a table database object. */
bool tctdbtranbegin(TCTDB *tdb){
assert(tdb);
for(double wsec = 1.0 / sysconf(_SC_CLK_TCK); true; wsec *= 2){
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
if(!tdb->tran) break;
TDBUNLOCKMETHOD(tdb);
if(wsec > 1.0) wsec = 1.0;
tcsleep(wsec);
}
if(!tctdbtranbeginimpl(tdb)){
TDBUNLOCKMETHOD(tdb);
return false;
}
tdb->tran = true;
TDBUNLOCKMETHOD(tdb);
return true;
}
/* Commit the transaction of a table database object. */
bool tctdbtrancommit(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode || !tdb->tran){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
tdb->tran = false;
bool err = false;
if(!tctdbtrancommitimpl(tdb)) err = true;
TDBUNLOCKMETHOD(tdb);
return !err;
}
/* Abort the transaction of a table database object. */
bool tctdbtranabort(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode || !tdb->tran){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
tdb->tran = false;
bool err = false;
if(!tctdbtranabortimpl(tdb)) err = true;
TDBUNLOCKMETHOD(tdb);
return !err;
}
/* Get the file path of a table database object. */
const char *tctdbpath(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, false)) return NULL;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return NULL;
}
const char *rv = tchdbpath(tdb->hdb);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Get the number of records of a table database object. */
uint64_t tctdbrnum(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, false)) return 0;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return 0;
}
uint64_t rv = tchdbrnum(tdb->hdb);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Get the size of the database file of a table database object. */
uint64_t tctdbfsiz(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, false)) return 0;
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return 0;
}
uint64_t rv = tchdbfsiz(tdb->hdb);
TDBIDX *idxs = tdb->idxs;
int inum = tdb->inum;
for(int i = 0; i < inum; i++){
TDBIDX *idx = idxs + i;
switch(idx->type){
case TDBITLEXICAL:
case TDBITDECIMAL:
case TDBITTOKEN:
case TDBITQGRAM:
rv += tcbdbfsiz(idx->db);
break;
}
}
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Set a column index to a table database object. */
bool tctdbsetindex(TCTDB *tdb, const char *name, int type){
assert(tdb && name);
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode || tdb->tran){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool err = false;
double iccsync = tdb->iccsync;
tdb->iccsync = 1.0;
if(!tctdbsetindeximpl(tdb, name, type)) err = true;
if(!tctdbmemsync(tdb, false)) err = true;
tdb->iccsync = iccsync;
TDBUNLOCKMETHOD(tdb);
return !err;
}
/* Generate a unique ID number of a table database object. */
int64_t tctdbgenuid(TCTDB *tdb){
assert(tdb);
if(!TDBLOCKMETHOD(tdb, true)) return -1;
if(!tdb->open || !tdb->wmode){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return -1;
}
int64_t rv = tctdbgenuidimpl(tdb, 1);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Create a query object. */
TDBQRY *tctdbqrynew(TCTDB *tdb){
assert(tdb);
TDBQRY *qry;
TCMALLOC(qry, sizeof(*qry));
qry->tdb = tdb;
TCMALLOC(qry->conds, sizeof(qry->conds[0]) * 2);
qry->cnum = 0;
qry->oname = NULL;
qry->otype = TDBQOSTRASC;
qry->max = INT_MAX;
qry->skip = 0;
qry->hint = tcxstrnew3(TDBHINTUSIZ);
qry->count = 0;
return qry;
}
/* Delete a query object. */
void tctdbqrydel(TDBQRY *qry){
assert(qry);
tcxstrdel(qry->hint);
TCFREE(qry->oname);
TDBCOND *conds = qry->conds;
int cnum = qry->cnum;
for(int i = 0; i < cnum; i++){
TDBCOND *cond = conds + i;
if(cond->ftsunits){
TDBFTSUNIT *ftsunits = cond->ftsunits;
int ftsnum = cond->ftsnum;
for(int j = 0; j < ftsnum; j++){
TDBFTSUNIT *ftsunit = ftsunits + j;
tclistdel(ftsunit->tokens);
}
TCFREE(ftsunits);
}
if(cond->regex){
regfree(cond->regex);
TCFREE(cond->regex);
}
TCFREE(cond->expr);
TCFREE(cond->name);
}
TCFREE(conds);
TCFREE(qry);
}
/* Add a narrowing condition to a query object. */
void tctdbqryaddcond(TDBQRY *qry, const char *name, int op, const char *expr){
assert(qry && name && expr);
int cnum = qry->cnum;
TCREALLOC(qry->conds, qry->conds, sizeof(qry->conds[0]) * (cnum + 1));
TDBCOND *cond = qry->conds + cnum;
int nsiz = strlen(name);
int esiz = strlen(expr);
TCMEMDUP(cond->name, name, nsiz);
cond->nsiz = nsiz;
bool sign = true;
if(op & TDBQCNEGATE){
op &= ~TDBQCNEGATE;
sign = false;
}
bool noidx = false;
if(op & TDBQCNOIDX){
op &= ~TDBQCNOIDX;
noidx = true;
}
cond->op = op;
cond->sign = sign;
cond->noidx = noidx;
TCMEMDUP(cond->expr, expr, esiz);
cond->esiz = esiz;
cond->regex = NULL;
if(op == TDBQCSTRRX){
const char *rxstr = expr;
int rxopt = REG_EXTENDED | REG_NOSUB;
if(*rxstr == '*'){
rxopt |= REG_ICASE;
rxstr++;
}
regex_t rxbuf;
if(regcomp(&rxbuf, rxstr, rxopt) == 0){
TCMALLOC(cond->regex, sizeof(rxbuf));
memcpy(cond->regex, &rxbuf, sizeof(rxbuf));
}
}
cond->ftsunits = NULL;
cond->ftsnum = 0;
if(op >= TDBQCFTSPH && op <= TDBQCFTSEX){
cond->op = TDBQCFTSPH;
cond->ftsunits = tctdbftsparseexpr(expr, esiz, op, &(cond->ftsnum));
}
qry->cnum++;
}
/* Set the order of a query object. */
void tctdbqrysetorder(TDBQRY *qry, const char *name, int type){
assert(qry && name);
if(qry->oname) TCFREE(qry->oname);
qry->oname = tcstrdup(name);
qry->otype = type;
}
/* Set the limit number of records of the result of a query object. */
void tctdbqrysetlimit(TDBQRY *qry, int max, int skip){
assert(qry);
qry->max = (max >= 0) ? max : INT_MAX;
qry->skip = (skip > 0) ? skip : 0;
}
/* Execute the search of a query object. */
TCLIST *tctdbqrysearch(TDBQRY *qry){
assert(qry);
TCTDB *tdb = qry->tdb;
if(!TDBLOCKMETHOD(tdb, false)) return tclistnew();
if(!tdb->open){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return tclistnew();
}
TCLIST *rv = tctdbqrysearchimpl(qry);
TDBUNLOCKMETHOD(tdb);
return rv;
}
/* Remove each record corresponding to a query object. */
bool tctdbqrysearchout(TDBQRY *qry){
assert(qry);
return tctdbqryproc(qry, tctdbqryprocoutcb, NULL);
}
/* Process each record corresponding to a query object. */
bool tctdbqryproc(TDBQRY *qry, TDBQRYPROC proc, void *op){
assert(qry && proc);
TCTDB *tdb = qry->tdb;
if(!TDBLOCKMETHOD(tdb, true)) return false;
if(!tdb->open || !tdb->wmode){
tctdbsetecode(tdb, TCEINVALID, __FILE__, __LINE__, __func__);
TDBUNLOCKMETHOD(tdb);
return false;
}
bool err = false;
int64_t getnum = 0;
int64_t putnum = 0;
int64_t outnum = 0;
TCLIST *res = tctdbqrysearchimpl(qry);
int rnum = TCLISTNUM(res);
for(int i = 0; i < rnum; i++){
const char *pkbuf;
int pksiz;
TCLISTVAL(pkbuf, res, i, pksiz);
TCMAP *cols = tctdbgetimpl(tdb, pkbuf, pksiz);
if(!cols){
err = true;
continue;
}