-
Notifications
You must be signed in to change notification settings - Fork 2
/
depot.c
2219 lines (2063 loc) · 66.3 KB
/
depot.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
/*************************************************************************************************
* Implementation of Depot
* Copyright (C) 2000-2007 Mikio Hirabayashi
* This file is part of QDBM, Quick Database Manager.
* QDBM 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. QDBM 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 QDBM; if
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*************************************************************************************************/
#define QDBM_INTERNAL 1
#include "depot.h"
#include "myconf.h"
#define DP_FILEMODE 00644 /* permission of a creating file */
#define DP_MAGICNUMB "[DEPOT]\n\f" /* magic number on environments of big endian */
#define DP_MAGICNUML "[depot]\n\f" /* magic number on environments of little endian */
#define DP_HEADSIZ 48 /* size of the reagion of the header */
#define DP_LIBVEROFF 12 /* offset of the region for the library version */
#define DP_FLAGSOFF 16 /* offset of the region for flags */
#define DP_FSIZOFF 24 /* offset of the region for the file size */
#define DP_BNUMOFF 32 /* offset of the region for the bucket number */
#define DP_RNUMOFF 40 /* offset of the region for the record number */
#define DP_DEFBNUM 8191 /* default bucket number */
#define DP_FBPOOLSIZ 16 /* size of free block pool */
#define DP_ENTBUFSIZ 128 /* size of the entity buffer */
#define DP_STKBUFSIZ 256 /* size of the stack key buffer */
#define DP_WRTBUFSIZ 8192 /* size of the writing buffer */
#define DP_FSBLKSIZ 4096 /* size of a block of the file system */
#define DP_TMPFSUF MYEXTSTR "dptmp" /* suffix of a temporary file */
#define DP_OPTBLOAD 0.25 /* ratio of bucket loading at optimization */
#define DP_OPTRUNIT 256 /* number of records in a process of optimization */
#define DP_NUMBUFSIZ 32 /* size of a buffer for a number */
#define DP_IOBUFSIZ 8192 /* size of an I/O buffer */
/* get the first hash value */
#define DP_FIRSTHASH(DP_res, DP_kbuf, DP_ksiz) \
do { \
const unsigned char *_DP_p; \
int _DP_ksiz; \
_DP_p = (const unsigned char *)(DP_kbuf); \
_DP_ksiz = DP_ksiz; \
if((_DP_ksiz) == sizeof(int)){ \
memcpy(&(DP_res), (DP_kbuf), sizeof(int)); \
} else { \
(DP_res) = 751; \
} \
while(_DP_ksiz--){ \
(DP_res) = (DP_res) * 31 + *(_DP_p)++; \
} \
(DP_res) = ((DP_res) * 87767623) & INT_MAX; \
} while(FALSE)
/* get the second hash value */
#define DP_SECONDHASH(DP_res, DP_kbuf, DP_ksiz) \
do { \
const unsigned char *_DP_p; \
int _DP_ksiz; \
_DP_p = (const unsigned char *)(DP_kbuf) + DP_ksiz - 1; \
_DP_ksiz = DP_ksiz; \
for((DP_res) = 19780211; _DP_ksiz--;){ \
(DP_res) = (DP_res) * 37 + *(_DP_p)--; \
} \
(DP_res) = ((DP_res) * 43321879) & INT_MAX; \
} while(FALSE)
/* get the third hash value */
#define DP_THIRDHASH(DP_res, DP_kbuf, DP_ksiz) \
do { \
int _DP_i; \
(DP_res) = 774831917; \
for(_DP_i = (DP_ksiz) - 1; _DP_i >= 0; _DP_i--){ \
(DP_res) = (DP_res) * 29 + ((const unsigned char *)(DP_kbuf))[_DP_i]; \
} \
(DP_res) = ((DP_res) * 5157883) & INT_MAX; \
} while(FALSE)
enum { /* enumeration for a record header */
DP_RHIFLAGS, /* offset of flags */
DP_RHIHASH, /* offset of value of the second hash function */
DP_RHIKSIZ, /* offset of the size of the key */
DP_RHIVSIZ, /* offset of the size of the value */
DP_RHIPSIZ, /* offset of the size of the padding bytes */
DP_RHILEFT, /* offset of the offset of the left child */
DP_RHIRIGHT, /* offset of the offset of the right child */
DP_RHNUM /* number of elements of a header */
};
enum { /* enumeration for the flag of a record */
DP_RECFDEL = 1 << 0, /* deleted */
DP_RECFREUSE = 1 << 1 /* reusable */
};
/* private function prototypes */
static int dpbigendian(void);
static char *dpstrdup(const char *str);
static int dplock(int fd, int ex, int nb);
static int dpwrite(int fd, const void *buf, int size);
static int dpseekwrite(int fd, int off, const void *buf, int size);
static int dpseekwritenum(int fd, int off, int num);
static int dpread(int fd, void *buf, int size);
static int dpseekread(int fd, int off, void *buf, int size);
static int dpfcopy(int destfd, int destoff, int srcfd, int srcoff);
static int dpgetprime(int num);
static int dppadsize(DEPOT *depot, int ksiz, int vsiz);
static int dprecsize(int *head);
static int dprechead(DEPOT *depot, int off, int *head, char *ebuf, int *eep);
static char *dpreckey(DEPOT *depot, int off, int *head);
static char *dprecval(DEPOT *depot, int off, int *head, int start, int max);
static int dprecvalwb(DEPOT *depot, int off, int *head, int start, int max, char *vbuf);
static int dpkeycmp(const char *abuf, int asiz, const char *bbuf, int bsiz);
static int dprecsearch(DEPOT *depot, const char *kbuf, int ksiz, int hash, int *bip, int *offp,
int *entp, int *head, char *ebuf, int *eep, int delhit);
static int dprecrewrite(DEPOT *depot, int off, int rsiz, const char *kbuf, int ksiz,
const char *vbuf, int vsiz, int hash, int left, int right);
static int dprecappend(DEPOT *depot, const char *kbuf, int ksiz, const char *vbuf, int vsiz,
int hash, int left, int right);
static int dprecover(DEPOT *depot, int off, int *head, const char *vbuf, int vsiz, int cat);
static int dprecdelete(DEPOT *depot, int off, int *head, int reusable);
static void dpfbpoolcoal(DEPOT *depot);
static int dpfbpoolcmp(const void *a, const void *b);
/*************************************************************************************************
* public objects
*************************************************************************************************/
/* String containing the version information. */
const char *dpversion = _QDBM_VERSION;
/* Get a message string corresponding to an error code. */
const char *dperrmsg(int ecode){
switch(ecode){
case DP_ENOERR: return "no error";
case DP_EFATAL: return "with fatal error";
case DP_EMODE: return "invalid mode";
case DP_EBROKEN: return "broken database file";
case DP_EKEEP: return "existing record";
case DP_ENOITEM: return "no item found";
case DP_EALLOC: return "memory allocation error";
case DP_EMAP: return "memory mapping error";
case DP_EOPEN: return "open error";
case DP_ECLOSE: return "close error";
case DP_ETRUNC: return "trunc error";
case DP_ESYNC: return "sync error";
case DP_ESTAT: return "stat error";
case DP_ESEEK: return "seek error";
case DP_EREAD: return "read error";
case DP_EWRITE: return "write error";
case DP_ELOCK: return "lock error";
case DP_EUNLINK: return "unlink error";
case DP_EMKDIR: return "mkdir error";
case DP_ERMDIR: return "rmdir error";
case DP_EMISC: return "miscellaneous error";
}
return "(invalid ecode)";
}
/* Get a database handle. */
DEPOT *dpopen(const char *name, int omode, int bnum){
char hbuf[DP_HEADSIZ], *map, c, *tname;
int i, mode, fd, inode, fsiz, rnum, msiz, *fbpool;
struct stat sbuf;
time_t mtime;
DEPOT *depot;
assert(name);
mode = O_RDONLY;
if(omode & DP_OWRITER){
mode = O_RDWR;
if(omode & DP_OCREAT) mode |= O_CREAT;
}
if((fd = open(name, mode, DP_FILEMODE)) == -1){
dpecodeset(DP_EOPEN, __FILE__, __LINE__);
return NULL;
}
if(!(omode & DP_ONOLCK)){
if(!dplock(fd, omode & DP_OWRITER, omode & DP_OLCKNB)){
close(fd);
return NULL;
}
}
if((omode & DP_OWRITER) && (omode & DP_OTRUNC)){
if(ftruncate(fd, 0) == -1){
close(fd);
dpecodeset(DP_ETRUNC, __FILE__, __LINE__);
return NULL;
}
}
if(fstat(fd, &sbuf) == -1 || !S_ISREG(sbuf.st_mode) ||
(sbuf.st_ino == 0 && lstat(name, &sbuf) == -1)){
close(fd);
dpecodeset(DP_ESTAT, __FILE__, __LINE__);
return NULL;
}
inode = sbuf.st_ino;
mtime = sbuf.st_mtime;
fsiz = sbuf.st_size;
if((omode & DP_OWRITER) && fsiz == 0){
memset(hbuf, 0, DP_HEADSIZ);
if(dpbigendian()){
memcpy(hbuf, DP_MAGICNUMB, strlen(DP_MAGICNUMB));
} else {
memcpy(hbuf, DP_MAGICNUML, strlen(DP_MAGICNUML));
}
sprintf(hbuf + DP_LIBVEROFF, "%d", _QDBM_LIBVER / 100);
bnum = bnum < 1 ? DP_DEFBNUM : bnum;
bnum = dpgetprime(bnum);
memcpy(hbuf + DP_BNUMOFF, &bnum, sizeof(int));
rnum = 0;
memcpy(hbuf + DP_RNUMOFF, &rnum, sizeof(int));
fsiz = DP_HEADSIZ + bnum * sizeof(int);
memcpy(hbuf + DP_FSIZOFF, &fsiz, sizeof(int));
if(!dpseekwrite(fd, 0, hbuf, DP_HEADSIZ)){
close(fd);
return NULL;
}
if(omode & DP_OSPARSE){
c = 0;
if(!dpseekwrite(fd, fsiz - 1, &c, 1)){
close(fd);
return NULL;
}
} else {
if(!(map = malloc(bnum * sizeof(int)))){
close(fd);
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
return NULL;
}
memset(map, 0, bnum * sizeof(int));
if(!dpseekwrite(fd, DP_HEADSIZ, map, bnum * sizeof(int))){
free(map);
close(fd);
return NULL;
}
free(map);
}
}
if(!dpseekread(fd, 0, hbuf, DP_HEADSIZ)){
close(fd);
dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
return NULL;
}
if(!(omode & DP_ONOLCK) &&
((dpbigendian() ? memcmp(hbuf, DP_MAGICNUMB, strlen(DP_MAGICNUMB)) != 0 :
memcmp(hbuf, DP_MAGICNUML, strlen(DP_MAGICNUML)) != 0) ||
*((int *)(hbuf + DP_FSIZOFF)) != fsiz)){
close(fd);
dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
return NULL;
}
bnum = *((int *)(hbuf + DP_BNUMOFF));
rnum = *((int *)(hbuf + DP_RNUMOFF));
if(bnum < 1 || rnum < 0 || fsiz < DP_HEADSIZ + bnum * sizeof(int)){
close(fd);
dpecodeset(DP_EBROKEN, __FILE__, __LINE__);
return NULL;
}
msiz = DP_HEADSIZ + bnum * sizeof(int);
map = mmap(0, msiz, PROT_READ | ((mode & DP_OWRITER) ? PROT_WRITE : 0), MAP_SHARED, fd, 0);
if(map == MAP_FAILED){
close(fd);
dpecodeset(DP_EMAP, __FILE__, __LINE__);
return NULL;
}
tname = NULL;
fbpool = NULL;
if(!(depot = malloc(sizeof(DEPOT))) || !(tname = dpstrdup(name)) ||
!(fbpool = malloc(DP_FBPOOLSIZ * 2 * sizeof(int)))){
free(fbpool);
free(tname);
free(depot);
munmap(map, msiz);
close(fd);
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
return NULL;
}
depot->name = tname;
depot->wmode = (mode & DP_OWRITER);
depot->inode = inode;
depot->mtime = mtime;
depot->fd = fd;
depot->fsiz = fsiz;
depot->map = map;
depot->msiz = msiz;
depot->buckets = (int *)(map + DP_HEADSIZ);
depot->bnum = bnum;
depot->rnum = rnum;
depot->fatal = FALSE;
depot->ioff = 0;
depot->fbpool = fbpool;
for(i = 0; i < DP_FBPOOLSIZ * 2; i += 2){
depot->fbpool[i] = -1;
depot->fbpool[i+1] = -1;
}
depot->fbpsiz = DP_FBPOOLSIZ * 2;
depot->fbpinc = 0;
depot->align = 0;
return depot;
}
/* Close a database handle. */
int dpclose(DEPOT *depot){
int fatal, err;
assert(depot);
fatal = depot->fatal;
err = FALSE;
if(depot->wmode){
*((int *)(depot->map + DP_FSIZOFF)) = depot->fsiz;
*((int *)(depot->map + DP_RNUMOFF)) = depot->rnum;
}
if(depot->map != MAP_FAILED){
if(munmap(depot->map, depot->msiz) == -1){
err = TRUE;
dpecodeset(DP_EMAP, __FILE__, __LINE__);
}
}
if(close(depot->fd) == -1){
err = TRUE;
dpecodeset(DP_ECLOSE, __FILE__, __LINE__);
}
free(depot->fbpool);
free(depot->name);
free(depot);
if(fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
return err ? FALSE : TRUE;
}
/* Store a record. */
int dpput(DEPOT *depot, const char *kbuf, int ksiz, const char *vbuf, int vsiz, int dmode){
int head[DP_RHNUM], next[DP_RHNUM];
int i, hash, bi, off, entoff, ee, newoff, rsiz, nsiz, fdel, mroff, mrsiz, mi, min;
char ebuf[DP_ENTBUFSIZ], *tval, *swap;
assert(depot && kbuf && vbuf);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
if(!depot->wmode){
dpecodeset(DP_EMODE, __FILE__, __LINE__);
return FALSE;
}
if(ksiz < 0) ksiz = strlen(kbuf);
if(vsiz < 0) vsiz = strlen(vbuf);
newoff = -1;
DP_SECONDHASH(hash, kbuf, ksiz);
switch(dprecsearch(depot, kbuf, ksiz, hash, &bi, &off, &entoff, head, ebuf, &ee, TRUE)){
case -1:
depot->fatal = TRUE;
return FALSE;
case 0:
fdel = head[DP_RHIFLAGS] & DP_RECFDEL;
if(dmode == DP_DKEEP && !fdel){
dpecodeset(DP_EKEEP, __FILE__, __LINE__);
return FALSE;
}
if(fdel){
head[DP_RHIPSIZ] += head[DP_RHIVSIZ];
head[DP_RHIVSIZ] = 0;
}
rsiz = dprecsize(head);
nsiz = DP_RHNUM * sizeof(int) + ksiz + vsiz;
if(dmode == DP_DCAT) nsiz += head[DP_RHIVSIZ];
if(off + rsiz >= depot->fsiz){
if(rsiz < nsiz){
head[DP_RHIPSIZ] += nsiz - rsiz;
rsiz = nsiz;
depot->fsiz = off + rsiz;
}
} else {
while(nsiz > rsiz && off + rsiz < depot->fsiz){
if(!dprechead(depot, off + rsiz, next, NULL, NULL)) return FALSE;
if(!(next[DP_RHIFLAGS] & DP_RECFREUSE)) break;
head[DP_RHIPSIZ] += dprecsize(next);
rsiz += dprecsize(next);
}
for(i = 0; i < depot->fbpsiz; i += 2){
if(depot->fbpool[i] >= off && depot->fbpool[i] < off + rsiz){
depot->fbpool[i] = -1;
depot->fbpool[i+1] = -1;
}
}
}
if(nsiz <= rsiz){
if(!dprecover(depot, off, head, vbuf, vsiz, dmode == DP_DCAT)){
depot->fatal = TRUE;
return FALSE;
}
} else {
tval = NULL;
if(dmode == DP_DCAT){
if(ee && DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] + head[DP_RHIVSIZ] <= DP_ENTBUFSIZ){
if(!(tval = malloc(head[DP_RHIVSIZ] + vsiz + 1))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
memcpy(tval, ebuf + (DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ]), head[DP_RHIVSIZ]);
} else {
if(!(tval = dprecval(depot, off, head, 0, -1))){
depot->fatal = TRUE;
return FALSE;
}
if(!(swap = realloc(tval, head[DP_RHIVSIZ] + vsiz + 1))){
free(tval);
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
tval = swap;
}
memcpy(tval + head[DP_RHIVSIZ], vbuf, vsiz);
vsiz += head[DP_RHIVSIZ];
vbuf = tval;
}
mi = -1;
min = -1;
for(i = 0; i < depot->fbpsiz; i += 2){
if(depot->fbpool[i+1] < nsiz) continue;
if(mi == -1 || depot->fbpool[i+1] < min){
mi = i;
min = depot->fbpool[i+1];
}
}
if(mi >= 0){
mroff = depot->fbpool[mi];
mrsiz = depot->fbpool[mi+1];
depot->fbpool[mi] = -1;
depot->fbpool[mi+1] = -1;
} else {
mroff = -1;
mrsiz = -1;
}
if(!dprecdelete(depot, off, head, TRUE)){
free(tval);
depot->fatal = TRUE;
return FALSE;
}
if(mroff > 0 && nsiz <= mrsiz){
if(!dprecrewrite(depot, mroff, mrsiz, kbuf, ksiz, vbuf, vsiz,
hash, head[DP_RHILEFT], head[DP_RHIRIGHT])){
free(tval);
depot->fatal = TRUE;
return FALSE;
}
newoff = mroff;
} else {
if((newoff = dprecappend(depot, kbuf, ksiz, vbuf, vsiz,
hash, head[DP_RHILEFT], head[DP_RHIRIGHT])) == -1){
free(tval);
depot->fatal = TRUE;
return FALSE;
}
}
free(tval);
}
if(fdel) depot->rnum++;
break;
default:
if((newoff = dprecappend(depot, kbuf, ksiz, vbuf, vsiz, hash, 0, 0)) == -1){
depot->fatal = TRUE;
return FALSE;
}
depot->rnum++;
break;
}
if(newoff > 0){
if(entoff > 0){
if(!dpseekwritenum(depot->fd, entoff, newoff)){
depot->fatal = TRUE;
return FALSE;
}
} else {
depot->buckets[bi] = newoff;
}
}
return TRUE;
}
/* Delete a record. */
int dpout(DEPOT *depot, const char *kbuf, int ksiz){
int head[DP_RHNUM], hash, bi, off, entoff, ee;
char ebuf[DP_ENTBUFSIZ];
assert(depot && kbuf);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
if(!depot->wmode){
dpecodeset(DP_EMODE, __FILE__, __LINE__);
return FALSE;
}
if(ksiz < 0) ksiz = strlen(kbuf);
DP_SECONDHASH(hash, kbuf, ksiz);
switch(dprecsearch(depot, kbuf, ksiz, hash, &bi, &off, &entoff, head, ebuf, &ee, FALSE)){
case -1:
depot->fatal = TRUE;
return FALSE;
case 0:
break;
default:
dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
return FALSE;
}
if(!dprecdelete(depot, off, head, FALSE)){
depot->fatal = TRUE;
return FALSE;
}
depot->rnum--;
return TRUE;
}
/* Retrieve a record. */
char *dpget(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, int *sp){
int head[DP_RHNUM], hash, bi, off, entoff, ee, vsiz;
char ebuf[DP_ENTBUFSIZ], *vbuf;
assert(depot && kbuf && start >= 0);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return NULL;
}
if(ksiz < 0) ksiz = strlen(kbuf);
DP_SECONDHASH(hash, kbuf, ksiz);
switch(dprecsearch(depot, kbuf, ksiz, hash, &bi, &off, &entoff, head, ebuf, &ee, FALSE)){
case -1:
depot->fatal = TRUE;
return NULL;
case 0:
break;
default:
dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
return NULL;
}
if(start > head[DP_RHIVSIZ]){
dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
return NULL;
}
if(ee && DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] + head[DP_RHIVSIZ] <= DP_ENTBUFSIZ){
head[DP_RHIVSIZ] -= start;
if(max < 0){
vsiz = head[DP_RHIVSIZ];
} else {
vsiz = max < head[DP_RHIVSIZ] ? max : head[DP_RHIVSIZ];
}
if(!(vbuf = malloc(vsiz + 1))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
depot->fatal = TRUE;
return NULL;
}
memcpy(vbuf, ebuf + (DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] + start), vsiz);
vbuf[vsiz] = '\0';
} else {
if(!(vbuf = dprecval(depot, off, head, start, max))){
depot->fatal = TRUE;
return NULL;
}
}
if(sp){
if(max < 0){
*sp = head[DP_RHIVSIZ];
} else {
*sp = max < head[DP_RHIVSIZ] ? max : head[DP_RHIVSIZ];
}
}
return vbuf;
}
/* Retrieve a record and write the value into a buffer. */
int dpgetwb(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, char *vbuf){
int head[DP_RHNUM], hash, bi, off, entoff, ee, vsiz;
char ebuf[DP_ENTBUFSIZ];
assert(depot && kbuf && start >= 0 && max >= 0 && vbuf);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return -1;
}
if(ksiz < 0) ksiz = strlen(kbuf);
DP_SECONDHASH(hash, kbuf, ksiz);
switch(dprecsearch(depot, kbuf, ksiz, hash, &bi, &off, &entoff, head, ebuf, &ee, FALSE)){
case -1:
depot->fatal = TRUE;
return -1;
case 0:
break;
default:
dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
return -1;
}
if(start > head[DP_RHIVSIZ]){
dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
return -1;
}
if(ee && DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] + head[DP_RHIVSIZ] <= DP_ENTBUFSIZ){
head[DP_RHIVSIZ] -= start;
vsiz = max < head[DP_RHIVSIZ] ? max : head[DP_RHIVSIZ];
memcpy(vbuf, ebuf + (DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] + start), vsiz);
} else {
if((vsiz = dprecvalwb(depot, off, head, start, max, vbuf)) == -1){
depot->fatal = TRUE;
return -1;
}
}
return vsiz;
}
/* Get the size of the value of a record. */
int dpvsiz(DEPOT *depot, const char *kbuf, int ksiz){
int head[DP_RHNUM], hash, bi, off, entoff, ee;
char ebuf[DP_ENTBUFSIZ];
assert(depot && kbuf);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return -1;
}
if(ksiz < 0) ksiz = strlen(kbuf);
DP_SECONDHASH(hash, kbuf, ksiz);
switch(dprecsearch(depot, kbuf, ksiz, hash, &bi, &off, &entoff, head, ebuf, &ee, FALSE)){
case -1:
depot->fatal = TRUE;
return -1;
case 0:
break;
default:
dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
return -1;
}
return head[DP_RHIVSIZ];
}
/* Initialize the iterator of a database handle. */
int dpiterinit(DEPOT *depot){
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
depot->ioff = 0;
return TRUE;
}
/* Get the next key of the iterator. */
char *dpiternext(DEPOT *depot, int *sp){
int off, head[DP_RHNUM], ee;
char ebuf[DP_ENTBUFSIZ], *kbuf;
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return NULL;
}
off = DP_HEADSIZ + depot->bnum * sizeof(int);
off = off > depot->ioff ? off : depot->ioff;
while(off < depot->fsiz){
if(!dprechead(depot, off, head, ebuf, &ee)){
depot->fatal = TRUE;
return NULL;
}
if(head[DP_RHIFLAGS] & DP_RECFDEL){
off += dprecsize(head);
} else {
if(ee && DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] <= DP_ENTBUFSIZ){
if(!(kbuf = malloc(head[DP_RHIKSIZ] + 1))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
depot->fatal = TRUE;
return NULL;
}
memcpy(kbuf, ebuf + (DP_RHNUM * sizeof(int)), head[DP_RHIKSIZ]);
kbuf[head[DP_RHIKSIZ]] = '\0';
} else {
if(!(kbuf = dpreckey(depot, off, head))){
depot->fatal = TRUE;
return NULL;
}
}
depot->ioff = off + dprecsize(head);
if(sp) *sp = head[DP_RHIKSIZ];
return kbuf;
}
}
dpecodeset(DP_ENOITEM, __FILE__, __LINE__);
return NULL;
}
/* Set alignment of a database handle. */
int dpsetalign(DEPOT *depot, int align){
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
if(!depot->wmode){
dpecodeset(DP_EMODE, __FILE__, __LINE__);
return FALSE;
}
depot->align = align;
return TRUE;
}
/* Set the size of the free block pool of a database handle. */
int dpsetfbpsiz(DEPOT *depot, int size){
int *fbpool;
int i;
assert(depot && size >= 0);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
if(!depot->wmode){
dpecodeset(DP_EMODE, __FILE__, __LINE__);
return FALSE;
}
size *= 2;
if(!(fbpool = realloc(depot->fbpool, size * sizeof(int) + 1))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
return FALSE;
}
for(i = 0; i < size; i += 2){
fbpool[i] = -1;
fbpool[i+1] = -1;
}
depot->fbpool = fbpool;
depot->fbpsiz = size;
return TRUE;
}
/* Synchronize contents of updating a database with the file and the device. */
int dpsync(DEPOT *depot){
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
if(!depot->wmode){
dpecodeset(DP_EMODE, __FILE__, __LINE__);
return FALSE;
}
*((int *)(depot->map + DP_FSIZOFF)) = depot->fsiz;
*((int *)(depot->map + DP_RNUMOFF)) = depot->rnum;
if(msync(depot->map, depot->msiz, MS_SYNC) == -1){
dpecodeset(DP_EMAP, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
if(fsync(depot->fd) == -1){
dpecodeset(DP_ESYNC, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
return TRUE;
}
/* Optimize a database. */
int dpoptimize(DEPOT *depot, int bnum){
DEPOT *tdepot;
char *name;
int i, err, off, head[DP_RHNUM], ee, ksizs[DP_OPTRUNIT], vsizs[DP_OPTRUNIT], unum;
char ebuf[DP_ENTBUFSIZ], *kbufs[DP_OPTRUNIT], *vbufs[DP_OPTRUNIT];
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return FALSE;
}
if(!depot->wmode){
dpecodeset(DP_EMODE, __FILE__, __LINE__);
return FALSE;
}
if(!(name = malloc(strlen(depot->name) + strlen(DP_TMPFSUF) + 1))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
depot->fatal = FALSE;
return FALSE;
}
sprintf(name, "%s%s", depot->name, DP_TMPFSUF);
if(bnum < 0){
bnum = (int)(depot->rnum * (1.0 / DP_OPTBLOAD)) + 1;
if(bnum < DP_DEFBNUM / 2) bnum = DP_DEFBNUM / 2;
}
if(!(tdepot = dpopen(name, DP_OWRITER | DP_OCREAT | DP_OTRUNC, bnum))){
free(name);
depot->fatal = TRUE;
return FALSE;
}
free(name);
if(!dpsetflags(tdepot, dpgetflags(depot))){
dpclose(tdepot);
depot->fatal = TRUE;
return FALSE;
}
tdepot->align = depot->align;
err = FALSE;
off = DP_HEADSIZ + depot->bnum * sizeof(int);
unum = 0;
while(off < depot->fsiz){
if(!dprechead(depot, off, head, ebuf, &ee)){
err = TRUE;
break;
}
if(!(head[DP_RHIFLAGS] & DP_RECFDEL)){
if(ee && DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] <= DP_ENTBUFSIZ){
if(!(kbufs[unum] = malloc(head[DP_RHIKSIZ] + 1))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
err = TRUE;
break;
}
memcpy(kbufs[unum], ebuf + (DP_RHNUM * sizeof(int)), head[DP_RHIKSIZ]);
if(DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ] + head[DP_RHIVSIZ] <= DP_ENTBUFSIZ){
if(!(vbufs[unum] = malloc(head[DP_RHIVSIZ] + 1))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
err = TRUE;
break;
}
memcpy(vbufs[unum], ebuf + (DP_RHNUM * sizeof(int) + head[DP_RHIKSIZ]),
head[DP_RHIVSIZ]);
} else {
vbufs[unum] = dprecval(depot, off, head, 0, -1);
}
} else {
kbufs[unum] = dpreckey(depot, off, head);
vbufs[unum] = dprecval(depot, off, head, 0, -1);
}
ksizs[unum] = head[DP_RHIKSIZ];
vsizs[unum] = head[DP_RHIVSIZ];
unum++;
if(unum >= DP_OPTRUNIT){
for(i = 0; i < unum; i++){
if(kbufs[i] && vbufs[i]){
if(!dpput(tdepot, kbufs[i], ksizs[i], vbufs[i], vsizs[i], DP_DKEEP)) err = TRUE;
} else {
err = TRUE;
}
free(kbufs[i]);
free(vbufs[i]);
if(err) break;
}
unum = 0;
}
}
off += dprecsize(head);
if(err) break;
}
for(i = 0; i < unum; i++){
if(kbufs[i] && vbufs[i]){
if(!dpput(tdepot, kbufs[i], ksizs[i], vbufs[i], vsizs[i], DP_DKEEP)) err = TRUE;
} else {
err = TRUE;
}
free(kbufs[i]);
free(vbufs[i]);
if(err) break;
}
if(!dpsync(tdepot)) err = TRUE;
if(err){
unlink(tdepot->name);
dpclose(tdepot);
depot->fatal = TRUE;
return FALSE;
}
if(munmap(depot->map, depot->msiz) == -1){
dpclose(tdepot);
dpecodeset(DP_EMAP, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
depot->map = MAP_FAILED;
if(ftruncate(depot->fd, 0) == -1){
dpclose(tdepot);
unlink(tdepot->name);
dpecodeset(DP_ETRUNC, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
if(dpfcopy(depot->fd, 0, tdepot->fd, 0) == -1){
dpclose(tdepot);
unlink(tdepot->name);
depot->fatal = TRUE;
return FALSE;
}
depot->fsiz = tdepot->fsiz;
depot->bnum = tdepot->bnum;
depot->ioff = 0;
for(i = 0; i < depot->fbpsiz; i += 2){
depot->fbpool[i] = -1;
depot->fbpool[i+1] = -1;
}
depot->msiz = tdepot->msiz;
depot->map = mmap(0, depot->msiz, PROT_READ | PROT_WRITE, MAP_SHARED, depot->fd, 0);
if(depot->map == MAP_FAILED){
dpecodeset(DP_EMAP, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
depot->buckets = (int *)(depot->map + DP_HEADSIZ);
if(!(name = dpname(tdepot))){
dpclose(tdepot);
unlink(tdepot->name);
depot->fatal = TRUE;
return FALSE;
}
if(!dpclose(tdepot)){
free(name);
unlink(tdepot->name);
depot->fatal = TRUE;
return FALSE;
}
if(unlink(name) == -1){
free(name);
dpecodeset(DP_EUNLINK, __FILE__, __LINE__);
depot->fatal = TRUE;
return FALSE;
}
free(name);
return TRUE;
}
/* Get the name of a database. */
char *dpname(DEPOT *depot){
char *name;
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return NULL;
}
if(!(name = dpstrdup(depot->name))){
dpecodeset(DP_EALLOC, __FILE__, __LINE__);
depot->fatal = TRUE;
return NULL;
}
return name;
}
/* Get the size of a database file. */
int dpfsiz(DEPOT *depot){
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return -1;
}
return depot->fsiz;
}
/* Get the number of the elements of the bucket array. */
int dpbnum(DEPOT *depot){
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return -1;
}
return depot->bnum;
}
/* Get the number of the used elements of the bucket array. */
int dpbusenum(DEPOT *depot){
int i, hits;
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);
return -1;
}
hits = 0;
for(i = 0; i < depot->bnum; i++){
if(depot->buckets[i]) hits++;
}
return hits;
}
/* Get the number of the records stored in a database. */
int dprnum(DEPOT *depot){
assert(depot);
if(depot->fatal){
dpecodeset(DP_EFATAL, __FILE__, __LINE__);