forked from ehiggs/tokyocabinet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tchdb.c
5145 lines (4834 loc) · 159 KB
/
tchdb.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 hash 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 "myconf.h"
#define HDBFILEMODE 00644 // permission of created files
#define HDBIOBUFSIZ 8192 // size of an I/O buffer
#define HDBMAGICDATA "ToKyO CaBiNeT" // magic data for identification
#define HDBHEADSIZ 256 // size of the reagion of the header
#define HDBTYPEOFF 32 // offset of the region for the database type
#define HDBFLAGSOFF 33 // offset of the region for the additional flags
#define HDBAPOWOFF 34 // offset of the region for the alignment power
#define HDBFPOWOFF 35 // offset of the region for the free block pool power
#define HDBOPTSOFF 36 // offset of the region for the options
#define HDBBNUMOFF 40 // offset of the region for the bucket number
#define HDBRNUMOFF 48 // offset of the region for the record number
#define HDBFSIZOFF 56 // offset of the region for the file size
#define HDBFRECOFF 64 // offset of the region for the first record offset
#define HDBOPAQUEOFF 128 // offset of the region for the opaque field
#define HDBDEFBNUM 131071 // default bucket number
#define HDBDEFAPOW 4 // default alignment power
#define HDBMAXAPOW 16 // maximum alignment power
#define HDBDEFFPOW 10 // default free block pool power
#define HDBMAXFPOW 20 // maximum free block pool power
#define HDBDEFXMSIZ (64LL<<20) // default size of the extra mapped memory
#define HDBXFSIZINC 32768 // increment of extra file size
#define HDBMINRUNIT 48 // minimum record reading unit
#define HDBMAXHSIZ 32 // maximum record header size
#define HDBFBPALWRAT 2 // allowance ratio of the free block pool
#define HDBFBPBSIZ 64 // base region size of the free block pool
#define HDBFBPESIZ 4 // size of each region of the free block pool
#define HDBFBPMGFREQ 4096 // frequency to merge the free block pool
#define HDBDRPUNIT 65536 // unit size of the delayed record pool
#define HDBDRPLAT 2048 // latitude size of the delayed record pool
#define HDBDFRSRAT 2 // step ratio of auto defragmentation
#define HDBFBMAXSIZ (INT32_MAX/4) // maximum size of a free block pool
#define HDBCACHEOUT 128 // number of records in a process of cacheout
#define HDBWALSUFFIX "wal" // suffix of write ahead logging file
typedef struct { // type of structure for a record
uint64_t off; // offset of the record
uint32_t rsiz; // size of the whole record
uint8_t magic; // magic number
uint8_t hash; // second hash value
uint64_t left; // offset of the left child record
uint64_t right; // offset of the right child record
uint32_t ksiz; // size of the key
uint32_t vsiz; // size of the value
uint16_t psiz; // size of the padding
const char *kbuf; // pointer to the key
const char *vbuf; // pointer to the value
uint64_t boff; // offset of the body
char *bbuf; // buffer of the body
} TCHREC;
typedef struct { // type of structure for a free block
uint64_t off; // offset of the block
uint32_t rsiz; // size of the block
} HDBFB;
enum { // enumeration for magic data
HDBMAGICREC = 0xc8, // for data block
HDBMAGICFB = 0xb0 // for free block
};
enum { // enumeration for duplication behavior
HDBPDOVER, // overwrite an existing value
HDBPDKEEP, // keep the existing value
HDBPDCAT, // concatenate values
HDBPDADDINT, // add an integer
HDBPDADDDBL, // add a real number
HDBPDPROC // process by a callback function
};
typedef struct { // type of structure for a duplication callback
TCPDPROC proc; // function pointer
void *op; // opaque pointer
} HDBPDPROCOP;
/* private macros */
#define HDBLOCKMETHOD(TC_hdb, TC_wr) \
((TC_hdb)->mmtx ? tchdblockmethod((TC_hdb), (TC_wr)) : true)
#define HDBUNLOCKMETHOD(TC_hdb) \
((TC_hdb)->mmtx ? tchdbunlockmethod(TC_hdb) : true)
#define HDBLOCKRECORD(TC_hdb, TC_bidx, TC_wr) \
((TC_hdb)->mmtx ? tchdblockrecord((TC_hdb), (uint8_t)(TC_bidx), (TC_wr)) : true)
#define HDBUNLOCKRECORD(TC_hdb, TC_bidx) \
((TC_hdb)->mmtx ? tchdbunlockrecord((TC_hdb), (uint8_t)(TC_bidx)) : true)
#define HDBLOCKALLRECORDS(TC_hdb, TC_wr) \
((TC_hdb)->mmtx ? tchdblockallrecords((TC_hdb), (TC_wr)) : true)
#define HDBUNLOCKALLRECORDS(TC_hdb) \
((TC_hdb)->mmtx ? tchdbunlockallrecords(TC_hdb) : true)
#define HDBLOCKDB(TC_hdb) \
((TC_hdb)->mmtx ? tchdblockdb(TC_hdb) : true)
#define HDBUNLOCKDB(TC_hdb) \
((TC_hdb)->mmtx ? tchdbunlockdb(TC_hdb) : true)
#define HDBLOCKWAL(TC_hdb) \
((TC_hdb)->mmtx ? tchdblockwal(TC_hdb) : true)
#define HDBUNLOCKWAL(TC_hdb) \
((TC_hdb)->mmtx ? tchdbunlockwal(TC_hdb) : true)
#define HDBTHREADYIELD(TC_hdb) \
do { if((TC_hdb)->mmtx) sched_yield(); } while(false)
/* private function prototypes */
static uint64_t tcgetprime(uint64_t num);
static bool tchdbseekwrite(TCHDB *hdb, off_t off, const void *buf, size_t size);
static bool tchdbseekread(TCHDB *hdb, off_t off, void *buf, size_t size);
static bool tchdbseekreadtry(TCHDB *hdb, off_t off, void *buf, size_t size);
static void tchdbdumpmeta(TCHDB *hdb, char *hbuf);
static void tchdbloadmeta(TCHDB *hdb, const char *hbuf);
static void tchdbclear(TCHDB *hdb);
static int32_t tchdbpadsize(TCHDB *hdb, uint64_t off);
static void tchdbsetflag(TCHDB *hdb, int flag, bool sign);
static uint64_t tchdbbidx(TCHDB *hdb, const char *kbuf, int ksiz, uint8_t *hp);
static off_t tchdbgetbucket(TCHDB *hdb, uint64_t bidx);
static void tchdbsetbucket(TCHDB *hdb, uint64_t bidx, uint64_t off);
static bool tchdbsavefbp(TCHDB *hdb);
static bool tchdbloadfbp(TCHDB *hdb);
static void tcfbpsortbyoff(HDBFB *fbpool, int fbpnum);
static void tcfbpsortbyrsiz(HDBFB *fbpool, int fbpnum);
static void tchdbfbpmerge(TCHDB *hdb);
static void tchdbfbpinsert(TCHDB *hdb, uint64_t off, uint32_t rsiz);
static bool tchdbfbpsearch(TCHDB *hdb, TCHREC *rec);
static bool tchdbfbpsplice(TCHDB *hdb, TCHREC *rec, uint32_t nsiz);
static void tchdbfbptrim(TCHDB *hdb, uint64_t base, uint64_t next, uint64_t off, uint32_t rsiz);
static bool tchdbwritefb(TCHDB *hdb, uint64_t off, uint32_t rsiz);
static bool tchdbwriterec(TCHDB *hdb, TCHREC *rec, uint64_t bidx, off_t entoff);
static bool tchdbreadrec(TCHDB *hdb, TCHREC *rec, char *rbuf);
static bool tchdbreadrecbody(TCHDB *hdb, TCHREC *rec);
static bool tchdbremoverec(TCHDB *hdb, TCHREC *rec, char *rbuf, uint64_t bidx, off_t entoff);
static bool tchdbshiftrec(TCHDB *hdb, TCHREC *rec, char *rbuf, off_t destoff);
static int tcreckeycmp(const char *abuf, int asiz, const char *bbuf, int bsiz);
static bool tchdbflushdrp(TCHDB *hdb);
static void tchdbcacheadjust(TCHDB *hdb);
static bool tchdbwalinit(TCHDB *hdb);
static bool tchdbwalwrite(TCHDB *hdb, uint64_t off, int64_t size);
static int tchdbwalrestore(TCHDB *hdb, const char *path);
static bool tchdbwalremove(TCHDB *hdb, const char *path);
static bool tchdbopenimpl(TCHDB *hdb, const char *path, int omode);
static bool tchdbcloseimpl(TCHDB *hdb);
static bool tchdbputimpl(TCHDB *hdb, const char *kbuf, int ksiz, uint64_t bidx, uint8_t hash,
const char *vbuf, int vsiz, int dmode);
static void tchdbdrpappend(TCHDB *hdb, const char *kbuf, int ksiz, const char *vbuf, int vsiz,
uint8_t hash);
static bool tchdbputasyncimpl(TCHDB *hdb, const char *kbuf, int ksiz, uint64_t bidx,
uint8_t hash, const char *vbuf, int vsiz);
static bool tchdboutimpl(TCHDB *hdb, const char *kbuf, int ksiz, uint64_t bidx, uint8_t hash);
static char *tchdbgetimpl(TCHDB *hdb, const char *kbuf, int ksiz, uint64_t bidx, uint8_t hash,
int *sp);
static int tchdbgetintobuf(TCHDB *hdb, const char *kbuf, int ksiz, uint64_t bidx, uint8_t hash,
char *vbuf, int max);
static char *tchdbgetnextimpl(TCHDB *hdb, const char *kbuf, int ksiz, int *sp,
const char **vbp, int *vsp);
static int tchdbvsizimpl(TCHDB *hdb, const char *kbuf, int ksiz, uint64_t bidx, uint8_t hash);
static bool tchdbiterinitimpl(TCHDB *hdb);
static char *tchdbiternextimpl(TCHDB *hdb, int *sp);
static bool tchdbiternextintoxstr(TCHDB *hdb, TCXSTR *kxstr, TCXSTR *vxstr);
static bool tchdboptimizeimpl(TCHDB *hdb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts);
static bool tchdbvanishimpl(TCHDB *hdb);
static bool tchdbcopyimpl(TCHDB *hdb, const char *path);
static bool tchdbdefragimpl(TCHDB *hdb, int64_t step);
static bool tchdbiterjumpimpl(TCHDB *hdb, const char *kbuf, int ksiz);
static bool tchdbforeachimpl(TCHDB *hdb, TCITER iter, void *op);
static bool tchdblockmethod(TCHDB *hdb, bool wr);
static bool tchdbunlockmethod(TCHDB *hdb);
static bool tchdblockrecord(TCHDB *hdb, uint8_t bidx, bool wr);
static bool tchdbunlockrecord(TCHDB *hdb, uint8_t bidx);
static bool tchdblockallrecords(TCHDB *hdb, bool wr);
static bool tchdbunlockallrecords(TCHDB *hdb);
static bool tchdblockdb(TCHDB *hdb);
static bool tchdbunlockdb(TCHDB *hdb);
static bool tchdblockwal(TCHDB *hdb);
static bool tchdbunlockwal(TCHDB *hdb);
/* debugging function prototypes */
void tchdbprintmeta(TCHDB *hdb);
void tchdbprintrec(TCHDB *hdb, TCHREC *rec);
/*************************************************************************************************
* API
*************************************************************************************************/
/* Get the message string corresponding to an error code. */
const char *tchdberrmsg(int ecode){
return tcerrmsg(ecode);
}
/* Create a hash database object. */
TCHDB *tchdbnew(void){
TCHDB *hdb;
TCMALLOC(hdb, sizeof(*hdb));
tchdbclear(hdb);
return hdb;
}
/* Delete a hash database object. */
void tchdbdel(TCHDB *hdb){
assert(hdb);
if(hdb->fd >= 0) tchdbclose(hdb);
if(hdb->mmtx){
pthread_key_delete(*(pthread_key_t *)hdb->eckey);
pthread_mutex_destroy(hdb->wmtx);
pthread_mutex_destroy(hdb->dmtx);
for(int i = UINT8_MAX; i >= 0; i--){
pthread_rwlock_destroy((pthread_rwlock_t *)hdb->rmtxs + i);
}
pthread_rwlock_destroy(hdb->mmtx);
TCFREE(hdb->eckey);
TCFREE(hdb->wmtx);
TCFREE(hdb->dmtx);
TCFREE(hdb->rmtxs);
TCFREE(hdb->mmtx);
}
TCFREE(hdb);
}
/* Get the last happened error code of a hash database object. */
int tchdbecode(TCHDB *hdb){
assert(hdb);
return hdb->mmtx ?
(int)(intptr_t)pthread_getspecific(*(pthread_key_t *)hdb->eckey) : hdb->ecode;
}
/* Set mutual exclusion control of a hash database object for threading. */
bool tchdbsetmutex(TCHDB *hdb){
assert(hdb);
if(!TCUSEPTHREAD) return true;
if(hdb->mmtx || hdb->fd >= 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
pthread_mutexattr_t rma;
pthread_mutexattr_init(&rma);
TCMALLOC(hdb->mmtx, sizeof(pthread_rwlock_t));
TCMALLOC(hdb->rmtxs, (UINT8_MAX + 1) * sizeof(pthread_rwlock_t));
TCMALLOC(hdb->dmtx, sizeof(pthread_mutex_t));
TCMALLOC(hdb->wmtx, sizeof(pthread_mutex_t));
TCMALLOC(hdb->eckey, sizeof(pthread_key_t));
bool err = false;
if(pthread_mutexattr_settype(&rma, PTHREAD_MUTEX_RECURSIVE) != 0) err = true;
if(pthread_rwlock_init(hdb->mmtx, NULL) != 0) err = true;
for(int i = 0; i <= UINT8_MAX; i++){
if(pthread_rwlock_init((pthread_rwlock_t *)hdb->rmtxs + i, NULL) != 0) err = true;
}
if(pthread_mutex_init(hdb->dmtx, &rma) != 0) err = true;
if(pthread_mutex_init(hdb->wmtx, NULL) != 0) err = true;
if(pthread_key_create(hdb->eckey, NULL) != 0) err = true;
if(err){
tchdbsetecode(hdb, TCETHREAD, __FILE__, __LINE__, __func__);
pthread_mutexattr_destroy(&rma);
TCFREE(hdb->eckey);
TCFREE(hdb->wmtx);
TCFREE(hdb->dmtx);
TCFREE(hdb->rmtxs);
TCFREE(hdb->mmtx);
hdb->eckey = NULL;
hdb->wmtx = NULL;
hdb->dmtx = NULL;
hdb->rmtxs = NULL;
hdb->mmtx = NULL;
return false;
}
pthread_mutexattr_destroy(&rma);
return true;
}
/* Set the tuning parameters of a hash database object. */
bool tchdbtune(TCHDB *hdb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts){
assert(hdb);
if(hdb->fd >= 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
hdb->bnum = (bnum > 0) ? tcgetprime(bnum) : HDBDEFBNUM;
hdb->apow = (apow >= 0) ? tclmin(apow, HDBMAXAPOW) : HDBDEFAPOW;
hdb->fpow = (fpow >= 0) ? tclmin(fpow, HDBMAXFPOW) : HDBDEFFPOW;
hdb->opts = opts;
if(!_tc_deflate) hdb->opts &= ~HDBTDEFLATE;
if(!_tc_bzcompress) hdb->opts &= ~HDBTBZIP;
return true;
}
/* Set the caching parameters of a hash database object. */
bool tchdbsetcache(TCHDB *hdb, int32_t rcnum){
assert(hdb);
if(hdb->fd >= 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
hdb->rcnum = (rcnum > 0) ? tclmin(tclmax(rcnum, HDBCACHEOUT * 2), INT_MAX / 4) : 0;
return true;
}
/* Set the size of the extra mapped memory of a hash database object. */
bool tchdbsetxmsiz(TCHDB *hdb, int64_t xmsiz){
assert(hdb);
if(hdb->fd >= 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
hdb->xmsiz = (xmsiz > 0) ? tcpagealign(xmsiz) : 0;
return true;
}
/* Set the unit step number of auto defragmentation of a hash database object. */
bool tchdbsetdfunit(TCHDB *hdb, int32_t dfunit){
assert(hdb);
if(hdb->fd >= 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
return false;
}
hdb->dfunit = (dfunit > 0) ? dfunit : 0;
return true;
}
/* Open a database file and connect a hash database object. */
bool tchdbopen(TCHDB *hdb, const char *path, int omode){
assert(hdb && path);
if(!HDBLOCKMETHOD(hdb, true)) return false;
if(hdb->fd >= 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
char *rpath = tcrealpath(path);
if(!rpath){
int ecode = TCEOPEN;
switch(errno){
case EACCES: ecode = TCENOPERM; break;
case ENOENT: ecode = TCENOFILE; break;
case ENOTDIR: ecode = TCENOFILE; break;
}
tchdbsetecode(hdb, ecode, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(!tcpathlock(rpath)){
tchdbsetecode(hdb, TCETHREAD, __FILE__, __LINE__, __func__);
TCFREE(rpath);
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbopenimpl(hdb, path, omode);
if(rv){
hdb->rpath = rpath;
} else {
tcpathunlock(rpath);
TCFREE(rpath);
}
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Close a database object. */
bool tchdbclose(TCHDB *hdb){
assert(hdb);
if(!HDBLOCKMETHOD(hdb, true)) return false;
if(hdb->fd < 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbcloseimpl(hdb);
tcpathunlock(hdb->rpath);
TCFREE(hdb->rpath);
hdb->rpath = NULL;
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Store a record into a hash database object. */
bool tchdbput(TCHDB *hdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(hdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return false;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0 || !(hdb->omode & HDBOWRITER)){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return false;
}
if(!HDBLOCKRECORD(hdb, bidx, true)){
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->zmode){
char *zbuf;
if(hdb->opts & HDBTDEFLATE){
zbuf = _tc_deflate(vbuf, vsiz, &vsiz, _TCZMRAW);
} else if(hdb->opts & HDBTBZIP){
zbuf = _tc_bzcompress(vbuf, vsiz, &vsiz);
} else if(hdb->opts & HDBTTCBS){
zbuf = tcbsencode(vbuf, vsiz, &vsiz);
} else {
zbuf = hdb->enc(vbuf, vsiz, &vsiz, hdb->encop);
}
if(!zbuf){
tchdbsetecode(hdb, TCEMISC, __FILE__, __LINE__, __func__);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, zbuf, vsiz, HDBPDOVER);
TCFREE(zbuf);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, vbuf, vsiz, HDBPDOVER);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv;
}
/* Store a string record into a hash database object. */
bool tchdbput2(TCHDB *hdb, const char *kstr, const char *vstr){
assert(hdb && kstr && vstr);
return tchdbput(hdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Store a new record into a hash database object. */
bool tchdbputkeep(TCHDB *hdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(hdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return false;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0 || !(hdb->omode & HDBOWRITER)){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return false;
}
if(!HDBLOCKRECORD(hdb, bidx, true)){
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->zmode){
char *zbuf;
if(hdb->opts & HDBTDEFLATE){
zbuf = _tc_deflate(vbuf, vsiz, &vsiz, _TCZMRAW);
} else if(hdb->opts & HDBTBZIP){
zbuf = _tc_bzcompress(vbuf, vsiz, &vsiz);
} else if(hdb->opts & HDBTTCBS){
zbuf = tcbsencode(vbuf, vsiz, &vsiz);
} else {
zbuf = hdb->enc(vbuf, vsiz, &vsiz, hdb->encop);
}
if(!zbuf){
tchdbsetecode(hdb, TCEMISC, __FILE__, __LINE__, __func__);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, zbuf, vsiz, HDBPDKEEP);
TCFREE(zbuf);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, vbuf, vsiz, HDBPDKEEP);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv;
}
/* Store a new string record into a hash database object. */
bool tchdbputkeep2(TCHDB *hdb, const char *kstr, const char *vstr){
return tchdbputkeep(hdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Concatenate a value at the end of the existing record in a hash database object. */
bool tchdbputcat(TCHDB *hdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(hdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return false;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0 || !(hdb->omode & HDBOWRITER)){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return false;
}
if(!HDBLOCKRECORD(hdb, bidx, true)){
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->zmode){
char *zbuf;
int osiz;
char *obuf = tchdbgetimpl(hdb, kbuf, ksiz, bidx, hash, &osiz);
if(obuf){
TCREALLOC(obuf, obuf, osiz + vsiz + 1);
memcpy(obuf + osiz, vbuf, vsiz);
if(hdb->opts & HDBTDEFLATE){
zbuf = _tc_deflate(obuf, osiz + vsiz, &vsiz, _TCZMRAW);
} else if(hdb->opts & HDBTBZIP){
zbuf = _tc_bzcompress(obuf, osiz + vsiz, &vsiz);
} else if(hdb->opts & HDBTTCBS){
zbuf = tcbsencode(obuf, osiz + vsiz, &vsiz);
} else {
zbuf = hdb->enc(obuf, osiz + vsiz, &vsiz, hdb->encop);
}
TCFREE(obuf);
} else {
if(hdb->opts & HDBTDEFLATE){
zbuf = _tc_deflate(vbuf, vsiz, &vsiz, _TCZMRAW);
} else if(hdb->opts & HDBTBZIP){
zbuf = _tc_bzcompress(vbuf, vsiz, &vsiz);
} else if(hdb->opts & HDBTTCBS){
zbuf = tcbsencode(vbuf, vsiz, &vsiz);
} else {
zbuf = hdb->enc(vbuf, vsiz, &vsiz, hdb->encop);
}
}
if(!zbuf){
tchdbsetecode(hdb, TCEMISC, __FILE__, __LINE__, __func__);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, zbuf, vsiz, HDBPDOVER);
TCFREE(zbuf);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, vbuf, vsiz, HDBPDCAT);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv;
}
/* Concatenate a string value at the end of the existing record in a hash database object. */
bool tchdbputcat2(TCHDB *hdb, const char *kstr, const char *vstr){
assert(hdb && kstr && vstr);
return tchdbputcat(hdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Store a record into a hash database object in asynchronous fashion. */
bool tchdbputasync(TCHDB *hdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
assert(hdb && kbuf && ksiz >= 0 && vbuf && vsiz >= 0);
if(!HDBLOCKMETHOD(hdb, true)) return false;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
hdb->async = true;
if(hdb->fd < 0 || !(hdb->omode & HDBOWRITER)){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->zmode){
char *zbuf;
if(hdb->opts & HDBTDEFLATE){
zbuf = _tc_deflate(vbuf, vsiz, &vsiz, _TCZMRAW);
} else if(hdb->opts & HDBTBZIP){
zbuf = _tc_bzcompress(vbuf, vsiz, &vsiz);
} else if(hdb->opts & HDBTTCBS){
zbuf = tcbsencode(vbuf, vsiz, &vsiz);
} else {
zbuf = hdb->enc(vbuf, vsiz, &vsiz, hdb->encop);
}
if(!zbuf){
tchdbsetecode(hdb, TCEMISC, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbputasyncimpl(hdb, kbuf, ksiz, bidx, hash, zbuf, vsiz);
TCFREE(zbuf);
HDBUNLOCKMETHOD(hdb);
return rv;
}
bool rv = tchdbputasyncimpl(hdb, kbuf, ksiz, bidx, hash, vbuf, vsiz);
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Store a string record into a hash database object in asynchronous fashion. */
bool tchdbputasync2(TCHDB *hdb, const char *kstr, const char *vstr){
assert(hdb && kstr && vstr);
return tchdbputasync(hdb, kstr, strlen(kstr), vstr, strlen(vstr));
}
/* Remove a record of a hash database object. */
bool tchdbout(TCHDB *hdb, const void *kbuf, int ksiz){
assert(hdb && kbuf && ksiz >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return false;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0 || !(hdb->omode & HDBOWRITER)){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return false;
}
if(!HDBLOCKRECORD(hdb, bidx, true)){
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdboutimpl(hdb, kbuf, ksiz, bidx, hash);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv;
}
/* Remove a string record of a hash database object. */
bool tchdbout2(TCHDB *hdb, const char *kstr){
assert(hdb && kstr);
return tchdbout(hdb, kstr, strlen(kstr));
}
/* Retrieve a record in a hash database object. */
void *tchdbget(TCHDB *hdb, const void *kbuf, int ksiz, int *sp){
assert(hdb && kbuf && ksiz >= 0 && sp);
if(!HDBLOCKMETHOD(hdb, false)) return NULL;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return NULL;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return NULL;
}
if(!HDBLOCKRECORD(hdb, bidx, false)){
HDBUNLOCKMETHOD(hdb);
return false;
}
char *rv = tchdbgetimpl(hdb, kbuf, ksiz, bidx, hash, sp);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Retrieve a string record in a hash database object. */
char *tchdbget2(TCHDB *hdb, const char *kstr){
assert(hdb && kstr);
int vsiz;
return tchdbget(hdb, kstr, strlen(kstr), &vsiz);
}
/* Retrieve a record in a hash database object and write the value into a buffer. */
int tchdbget3(TCHDB *hdb, const void *kbuf, int ksiz, void *vbuf, int max){
assert(hdb && kbuf && ksiz >= 0 && vbuf && max >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return -1;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return -1;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return -1;
}
if(!HDBLOCKRECORD(hdb, bidx, false)){
HDBUNLOCKMETHOD(hdb);
return -1;
}
int rv = tchdbgetintobuf(hdb, kbuf, ksiz, bidx, hash, vbuf, max);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Get the size of the value of a record in a hash database object. */
int tchdbvsiz(TCHDB *hdb, const void *kbuf, int ksiz){
assert(hdb && kbuf && ksiz >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return -1;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return -1;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return -1;
}
if(!HDBLOCKRECORD(hdb, bidx, false)){
HDBUNLOCKMETHOD(hdb);
return -1;
}
int rv = tchdbvsizimpl(hdb, kbuf, ksiz, bidx, hash);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Get the size of the value of a string record in a hash database object. */
int tchdbvsiz2(TCHDB *hdb, const char *kstr){
assert(hdb && kstr);
return tchdbvsiz(hdb, kstr, strlen(kstr));
}
/* Initialize the iterator of a hash database object. */
bool tchdbiterinit(TCHDB *hdb){
assert(hdb);
if(!HDBLOCKMETHOD(hdb, true)) return false;
if(hdb->fd < 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbiterinitimpl(hdb);
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Get the next key of the iterator of a hash database object. */
void *tchdbiternext(TCHDB *hdb, int *sp){
assert(hdb && sp);
if(!HDBLOCKMETHOD(hdb, true)) return NULL;
if(hdb->fd < 0 || hdb->iter < 1){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return NULL;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return NULL;
}
char *rv = tchdbiternextimpl(hdb, sp);
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Get the next key string of the iterator of a hash database object. */
char *tchdbiternext2(TCHDB *hdb){
assert(hdb);
int vsiz;
return tchdbiternext(hdb, &vsiz);
}
/* Get the next extensible objects of the iterator of a hash database object. */
bool tchdbiternext3(TCHDB *hdb, TCXSTR *kxstr, TCXSTR *vxstr){
assert(hdb && kxstr && vxstr);
if(!HDBLOCKMETHOD(hdb, true)) return false;
if(hdb->fd < 0 || hdb->iter < 1){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return false;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return false;
}
bool rv = tchdbiternextintoxstr(hdb, kxstr, vxstr);
HDBUNLOCKMETHOD(hdb);
return rv;
}
/* Get forward matching keys in a hash database object. */
TCLIST *tchdbfwmkeys(TCHDB *hdb, const void *pbuf, int psiz, int max){
assert(hdb && pbuf && psiz >= 0);
TCLIST* keys = tclistnew();
if(!HDBLOCKMETHOD(hdb, true)) return keys;
if(hdb->fd < 0){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return keys;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return keys;
}
if(max < 0) max = INT_MAX;
uint64_t iter = hdb->iter;
tchdbiterinitimpl(hdb);
char *kbuf;
int ksiz;
while(TCLISTNUM(keys) < max && (kbuf = tchdbiternextimpl(hdb, &ksiz)) != NULL){
if(ksiz >= psiz && !memcmp(kbuf, pbuf, psiz)){
tclistpushmalloc(keys, kbuf, ksiz);
} else {
TCFREE(kbuf);
}
}
hdb->iter = iter;
HDBUNLOCKMETHOD(hdb);
return keys;
}
/* Get forward matching string keys in a hash database object. */
TCLIST *tchdbfwmkeys2(TCHDB *hdb, const char *pstr, int max){
assert(hdb && pstr);
return tchdbfwmkeys(hdb, pstr, strlen(pstr), max);
}
/* Add an integer to a record in a hash database object. */
int tchdbaddint(TCHDB *hdb, const void *kbuf, int ksiz, int num){
assert(hdb && kbuf && ksiz >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return INT_MIN;
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0 || !(hdb->omode & HDBOWRITER)){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return INT_MIN;
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return INT_MIN;
}
if(!HDBLOCKRECORD(hdb, bidx, true)){
HDBUNLOCKMETHOD(hdb);
return INT_MIN;
}
if(hdb->zmode){
char *zbuf;
int osiz;
char *obuf = tchdbgetimpl(hdb, kbuf, ksiz, bidx, hash, &osiz);
if(obuf){
if(osiz != sizeof(num)){
tchdbsetecode(hdb, TCEKEEP, __FILE__, __LINE__, __func__);
TCFREE(obuf);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return INT_MIN;
}
num += *(int *)obuf;
TCFREE(obuf);
}
int zsiz;
if(hdb->opts & HDBTDEFLATE){
zbuf = _tc_deflate((char *)&num, sizeof(num), &zsiz, _TCZMRAW);
} else if(hdb->opts & HDBTBZIP){
zbuf = _tc_bzcompress((char *)&num, sizeof(num), &zsiz);
} else if(hdb->opts & HDBTTCBS){
zbuf = tcbsencode((char *)&num, sizeof(num), &zsiz);
} else {
zbuf = hdb->enc((char *)&num, sizeof(num), &zsiz, hdb->encop);
}
if(!zbuf){
tchdbsetecode(hdb, TCEMISC, __FILE__, __LINE__, __func__);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return INT_MIN;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, zbuf, zsiz, HDBPDOVER);
TCFREE(zbuf);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv ? num : INT_MIN;
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, (char *)&num, sizeof(num), HDBPDADDINT);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
if(hdb->dfunit > 0 && hdb->dfcnt > hdb->dfunit &&
!tchdbdefrag(hdb, hdb->dfunit * HDBDFRSRAT + 1)) rv = false;
return rv ? num : INT_MIN;
}
/* Add a real number to a record in a hash database object. */
double tchdbadddouble(TCHDB *hdb, const void *kbuf, int ksiz, double num){
assert(hdb && kbuf && ksiz >= 0);
if(!HDBLOCKMETHOD(hdb, false)) return nan("");
uint8_t hash;
uint64_t bidx = tchdbbidx(hdb, kbuf, ksiz, &hash);
if(hdb->fd < 0 || !(hdb->omode & HDBOWRITER)){
tchdbsetecode(hdb, TCEINVALID, __FILE__, __LINE__, __func__);
HDBUNLOCKMETHOD(hdb);
return nan("");
}
if(hdb->async && !tchdbflushdrp(hdb)){
HDBUNLOCKMETHOD(hdb);
return nan("");
}
if(!HDBLOCKRECORD(hdb, bidx, true)){
HDBUNLOCKMETHOD(hdb);
return nan("");
}
if(hdb->zmode){
char *zbuf;
int osiz;
char *obuf = tchdbgetimpl(hdb, kbuf, ksiz, bidx, hash, &osiz);
if(obuf){
if(osiz != sizeof(num)){
tchdbsetecode(hdb, TCEKEEP, __FILE__, __LINE__, __func__);
TCFREE(obuf);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return nan("");
}
num += *(double *)obuf;
TCFREE(obuf);
}
int zsiz;
if(hdb->opts & HDBTDEFLATE){
zbuf = _tc_deflate((char *)&num, sizeof(num), &zsiz, _TCZMRAW);
} else if(hdb->opts & HDBTBZIP){
zbuf = _tc_bzcompress((char *)&num, sizeof(num), &zsiz);
} else if(hdb->opts & HDBTTCBS){
zbuf = tcbsencode((char *)&num, sizeof(num), &zsiz);
} else {
zbuf = hdb->enc((char *)&num, sizeof(num), &zsiz, hdb->encop);
}
if(!zbuf){
tchdbsetecode(hdb, TCEMISC, __FILE__, __LINE__, __func__);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);
return nan("");
}
bool rv = tchdbputimpl(hdb, kbuf, ksiz, bidx, hash, zbuf, zsiz, HDBPDOVER);
TCFREE(zbuf);
HDBUNLOCKRECORD(hdb, bidx);
HDBUNLOCKMETHOD(hdb);