1 /*************************************************************************************************
2 * The test cases of the B+ tree database API
3 * Copyright (C) 2006-2008 Mikio Hirabayashi
4 * This file is part of Tokyo Cabinet.
5 * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
6 * the GNU Lesser General Public License as published by the Free Software Foundation; either
7 * version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
8 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10 * License for more details.
11 * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12 * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13 * Boston, MA 02111-1307 USA.
14 *************************************************************************************************/
21 #define RECBUFSIZ 32 // buffer for records
23 typedef struct { // type of structure for write thread
30 typedef struct { // type of structure for read thread
38 typedef struct { // type of structure for remove thread
45 typedef struct { // type of structure for wicked thread
53 typedef struct { // type of structure for typical thread
62 /* global variables */
63 const char *g_progname; // program name
64 int g_dbgfd; // debugging output
67 /* function prototypes */
68 int main(int argc, char **argv);
69 static void usage(void);
70 static void iprintf(const char *format, ...);
71 static void eprint(TCBDB *bdb, const char *func);
72 static void mprint(TCBDB *bdb);
73 static int myrand(int range);
74 static int myrandnd(int range);
75 static int runwrite(int argc, char **argv);
76 static int runread(int argc, char **argv);
77 static int runremove(int argc, char **argv);
78 static int runwicked(int argc, char **argv);
79 static int runtypical(int argc, char **argv);
80 static int procwrite(const char *path, int tnum, int rnum, int lmemb, int nmemb,
81 int bnum, int apow, int fpow, int opts, int omode, bool rnd);
82 static int procread(const char *path, int tnum, int omode, bool wb, bool rnd);
83 static int procremove(const char *path, int tnum, int omode, bool rnd);
84 static int procwicked(const char *path, int tnum, int rnum, int opts, int omode, bool nc);
85 static int proctypical(const char *path, int tnum, int rnum, int lmemb, int nmemb,
86 int bnum, int apow, int fpow, int opts, int omode, bool nc, int rratio);
87 static void *threadwrite(void *targ);
88 static void *threadread(void *targ);
89 static void *threadremove(void *targ);
90 static void *threadwicked(void *targ);
91 static void *threadtypical(void *targ);
95 int main(int argc, char **argv){
98 const char *ebuf = getenv("TCDBGFD");
99 if(ebuf) g_dbgfd = atoi(ebuf);
100 srand((unsigned int)(tctime() * 1000) % UINT_MAX);
101 if(argc < 2) usage();
103 if(!strcmp(argv[1], "write")){
104 rv = runwrite(argc, argv);
105 } else if(!strcmp(argv[1], "read")){
106 rv = runread(argc, argv);
107 } else if(!strcmp(argv[1], "remove")){
108 rv = runremove(argc, argv);
109 } else if(!strcmp(argv[1], "wicked")){
110 rv = runwicked(argc, argv);
111 } else if(!strcmp(argv[1], "typical")){
112 rv = runtypical(argc, argv);
120 /* print the usage and exit */
121 static void usage(void){
122 fprintf(stderr, "%s: test cases of the B+ tree database API of Tokyo Cabinet\n", g_progname);
123 fprintf(stderr, "\n");
124 fprintf(stderr, "usage:\n");
125 fprintf(stderr, " %s write [-tl] [-td|-tb] [-nl|-nb] [-rnd] path tnum rnum"
126 " [lmemb [nmemb [bnum [apow [fpow]]]]]\n", g_progname);
127 fprintf(stderr, " %s read [-nl|-nb] [-wb] [-rnd] path tnum\n", g_progname);
128 fprintf(stderr, " %s remove [-nl|-nb] [-rnd] path tnum\n", g_progname);
129 fprintf(stderr, " %s wicked [-tl] [-td|-tb] [-nl|-nb] [-nc] path tnum rnum\n",
131 fprintf(stderr, " %s typical [-tl] [-td|-tb] [-nl|-nb] [-nc] [-rr] path tnum rnum"
132 " [lmemb [nmemb [bnum [apow [fpow]]]]]\n", g_progname);
133 fprintf(stderr, "\n");
138 /* print formatted information string and flush the buffer */
139 static void iprintf(const char *format, ...){
141 va_start(ap, format);
148 /* print error message of hash database */
149 static void eprint(TCBDB *bdb, const char *func){
150 const char *path = tcbdbpath(bdb);
151 int ecode = tcbdbecode(bdb);
152 fprintf(stderr, "%s: %s: %s: error: %d: %s\n",
153 g_progname, path ? path : "-", func, ecode, tcbdberrmsg(ecode));
157 /* print members of hash database */
158 static void mprint(TCBDB *bdb){
159 if(bdb->hdb->cnt_writerec < 0) return;
160 iprintf("max leaf member: %d\n", tcbdblmemb(bdb));
161 iprintf("max node member: %d\n", tcbdbnmemb(bdb));
162 iprintf("leaf number: %d\n", tcbdblnum(bdb));
163 iprintf("node number: %d\n", tcbdbnnum(bdb));
164 iprintf("bucket number: %lld\n", (long long)tcbdbbnum(bdb));
165 iprintf("used bucket number: %lld\n", (long long)tcbdbbnumused(bdb));
166 iprintf("cnt_saveleaf: %lld\n", (long long)bdb->cnt_saveleaf);
167 iprintf("cnt_loadleaf: %lld\n", (long long)bdb->cnt_loadleaf);
168 iprintf("cnt_killleaf: %lld\n", (long long)bdb->cnt_killleaf);
169 iprintf("cnt_adjleafc: %lld\n", (long long)bdb->cnt_adjleafc);
170 iprintf("cnt_savenode: %lld\n", (long long)bdb->cnt_savenode);
171 iprintf("cnt_loadnode: %lld\n", (long long)bdb->cnt_loadnode);
172 iprintf("cnt_adjnodec: %lld\n", (long long)bdb->cnt_adjnodec);
173 iprintf("cnt_writerec: %lld\n", (long long)bdb->hdb->cnt_writerec);
174 iprintf("cnt_reuserec: %lld\n", (long long)bdb->hdb->cnt_reuserec);
175 iprintf("cnt_moverec: %lld\n", (long long)bdb->hdb->cnt_moverec);
176 iprintf("cnt_readrec: %lld\n", (long long)bdb->hdb->cnt_readrec);
177 iprintf("cnt_searchfbp: %lld\n", (long long)bdb->hdb->cnt_searchfbp);
178 iprintf("cnt_insertfbp: %lld\n", (long long)bdb->hdb->cnt_insertfbp);
179 iprintf("cnt_splicefbp: %lld\n", (long long)bdb->hdb->cnt_splicefbp);
180 iprintf("cnt_dividefbp: %lld\n", (long long)bdb->hdb->cnt_dividefbp);
181 iprintf("cnt_mergefbp: %lld\n", (long long)bdb->hdb->cnt_mergefbp);
182 iprintf("cnt_reducefbp: %lld\n", (long long)bdb->hdb->cnt_reducefbp);
183 iprintf("cnt_appenddrp: %lld\n", (long long)bdb->hdb->cnt_appenddrp);
184 iprintf("cnt_deferdrp: %lld\n", (long long)bdb->hdb->cnt_deferdrp);
185 iprintf("cnt_flushdrp: %lld\n", (long long)bdb->hdb->cnt_flushdrp);
186 iprintf("cnt_adjrecc: %lld\n", (long long)bdb->hdb->cnt_adjrecc);
190 /* get a random number */
191 static int myrand(int range){
192 return (int)((double)range * rand() / (RAND_MAX + 1.0));
196 /* get a random number based on normal distribution */
197 static int myrandnd(int range){
198 int num = (int)tcdrandnd(range >> 1, range / 10);
199 return (num < 0 || num >= range) ? 0 : num;
203 /* parse arguments of write command */
204 static int runwrite(int argc, char **argv){
216 for(int i = 2; i < argc; i++){
217 if(!path && argv[i][0] == '-'){
218 if(!strcmp(argv[i], "-tl")){
220 } else if(!strcmp(argv[i], "-td")){
222 } else if(!strcmp(argv[i], "-tb")){
224 } else if(!strcmp(argv[i], "-nl")){
226 } else if(!strcmp(argv[i], "-nb")){
228 } else if(!strcmp(argv[i], "-rnd")){
253 if(!path || !tstr || !rstr) usage();
254 int tnum = atoi(tstr);
255 int rnum = atoi(rstr);
256 if(tnum < 1 || rnum < 1) usage();
257 int lmemb = lmstr ? atoi(lmstr) : -1;
258 int nmemb = nmstr ? atoi(nmstr) : -1;
259 int bnum = bstr ? atoi(bstr) : -1;
260 int apow = astr ? atoi(astr) : -1;
261 int fpow = fstr ? atoi(fstr) : -1;
262 int rv = procwrite(path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, rnd);
267 /* parse arguments of read command */
268 static int runread(int argc, char **argv){
274 for(int i = 2; i < argc; i++){
275 if(!path && argv[i][0] == '-'){
276 if(!strcmp(argv[i], "-nl")){
278 } else if(!strcmp(argv[i], "-nb")){
280 } else if(!strcmp(argv[i], "-wb")){
282 } else if(!strcmp(argv[i], "-rnd")){
295 if(!path || !tstr) usage();
296 int tnum = atoi(tstr);
297 if(tnum < 1) usage();
298 int rv = procread(path, tnum, omode, wb, rnd);
303 /* parse arguments of remove command */
304 static int runremove(int argc, char **argv){
309 for(int i = 2; i < argc; i++){
310 if(!path && argv[i][0] == '-'){
311 if(!strcmp(argv[i], "-nl")){
313 } else if(!strcmp(argv[i], "-nb")){
315 } else if(!strcmp(argv[i], "-rnd")){
328 if(!path || !tstr) usage();
329 int tnum = atoi(tstr);
330 if(tnum < 1) usage();
331 int rv = procremove(path, tnum, omode, rnd);
336 /* parse arguments of wicked command */
337 static int runwicked(int argc, char **argv){
344 for(int i = 2; i < argc; i++){
345 if(!path && argv[i][0] == '-'){
346 if(!strcmp(argv[i], "-tl")){
348 } else if(!strcmp(argv[i], "-td")){
350 } else if(!strcmp(argv[i], "-tb")){
352 } else if(!strcmp(argv[i], "-nl")){
354 } else if(!strcmp(argv[i], "-nb")){
356 } else if(!strcmp(argv[i], "-nc")){
371 if(!path || !tstr || !rstr) usage();
372 int tnum = atoi(tstr);
373 int rnum = atoi(rstr);
374 if(tnum < 1 || rnum < 1) usage();
375 int rv = procwicked(path, tnum, rnum, opts, omode, nc);
380 /* parse arguments of typical command */
381 static int runtypical(int argc, char **argv){
394 for(int i = 2; i < argc; i++){
395 if(!path && argv[i][0] == '-'){
396 if(!strcmp(argv[i], "-tl")){
398 } else if(!strcmp(argv[i], "-td")){
400 } else if(!strcmp(argv[i], "-tb")){
402 } else if(!strcmp(argv[i], "-nl")){
404 } else if(!strcmp(argv[i], "-nb")){
406 } else if(!strcmp(argv[i], "-nc")){
408 } else if(!strcmp(argv[i], "-rr")){
409 if(++i >= argc) usage();
410 rratio = atoi(argv[i]);
434 if(!path || !tstr || !rstr) usage();
435 int tnum = atoi(tstr);
436 int rnum = atoi(rstr);
437 if(tnum < 1 || rnum < 1) usage();
438 int lmemb = lmstr ? atoi(lmstr) : -1;
439 int nmemb = nmstr ? atoi(nmstr) : -1;
440 int bnum = bstr ? atoi(bstr) : -1;
441 int apow = astr ? atoi(astr) : -1;
442 int fpow = fstr ? atoi(fstr) : -1;
443 int rv = proctypical(path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, nc, rratio);
448 /* perform write command */
449 static int procwrite(const char *path, int tnum, int rnum, int lmemb, int nmemb,
450 int bnum, int apow, int fpow, int opts, int omode, bool rnd){
451 iprintf("<Writing Test>\n path=%s tnum=%d rnum=%d lmemb=%d nmemb=%d"
452 " bnum=%d apow=%d fpow=%d opts=%d omode=%d rnd=%d\n\n",
453 path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, rnd);
455 double stime = tctime();
456 TCBDB *bdb = tcbdbnew();
457 if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);
458 if(!tcbdbsetmutex(bdb)){
459 eprint(bdb, "tcbdbsetmutex");
462 if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){
463 eprint(bdb, "tcbdbtune");
466 if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){
467 eprint(bdb, "tcbdbopen");
470 TARGWRITE targs[tnum];
471 pthread_t threads[tnum];
474 targs[0].rnum = rnum;
477 if(threadwrite(targs) != NULL) err = true;
479 for(int i = 0; i < tnum; i++){
481 targs[i].rnum = rnum;
484 if(pthread_create(threads + i, NULL, threadwrite, targs + i) != 0){
485 eprint(bdb, "pthread_create");
490 for(int i = 0; i < tnum; i++){
491 if(targs[i].id == -1) continue;
493 if(pthread_join(threads[i], &rv) != 0){
494 eprint(bdb, "pthread_join");
501 iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));
502 iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));
504 if(!tcbdbclose(bdb)){
505 eprint(bdb, "tcbdbclose");
509 iprintf("time: %.3f\n", tctime() - stime);
510 iprintf("%s\n\n", err ? "error" : "ok");
515 /* perform read command */
516 static int procread(const char *path, int tnum, int omode, bool wb, bool rnd){
517 iprintf("<Reading Test>\n path=%s tnum=%d omode=%d wb=%d rnd=%d\n\n",
518 path, tnum, omode, wb, rnd);
520 double stime = tctime();
521 TCBDB *bdb = tcbdbnew();
522 if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);
523 if(!tcbdbsetmutex(bdb)){
524 eprint(bdb, "tcbdbsetmutex");
527 if(!tcbdbopen(bdb, path, BDBOREADER | omode)){
528 eprint(bdb, "tcbdbopen");
531 int rnum = tcbdbrnum(bdb) / tnum;
532 TARGREAD targs[tnum];
533 pthread_t threads[tnum];
536 targs[0].rnum = rnum;
540 if(threadread(targs) != NULL) err = true;
542 for(int i = 0; i < tnum; i++){
544 targs[i].rnum = rnum;
548 if(pthread_create(threads + i, NULL, threadread, targs + i) != 0){
549 eprint(bdb, "pthread_create");
554 for(int i = 0; i < tnum; i++){
555 if(targs[i].id == -1) continue;
557 if(pthread_join(threads[i], &rv) != 0){
558 eprint(bdb, "pthread_join");
565 iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));
566 iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));
568 if(!tcbdbclose(bdb)){
569 eprint(bdb, "tcbdbclose");
573 iprintf("time: %.3f\n", tctime() - stime);
574 iprintf("%s\n\n", err ? "error" : "ok");
579 /* perform remove command */
580 static int procremove(const char *path, int tnum, int omode, bool rnd){
581 iprintf("<Removing Test>\n path=%s tnum=%d omode=%d rnd=%d\n\n", path, tnum, omode, rnd);
583 double stime = tctime();
584 TCBDB *bdb = tcbdbnew();
585 if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);
586 if(!tcbdbsetmutex(bdb)){
587 eprint(bdb, "tcbdbsetmutex");
590 if(!tcbdbopen(bdb, path, BDBOWRITER | omode)){
591 eprint(bdb, "tcbdbopen");
594 int rnum = tcbdbrnum(bdb) / tnum;
595 TARGREMOVE targs[tnum];
596 pthread_t threads[tnum];
599 targs[0].rnum = rnum;
602 if(threadremove(targs) != NULL) err = true;
604 for(int i = 0; i < tnum; i++){
606 targs[i].rnum = rnum;
609 if(pthread_create(threads + i, NULL, threadremove, targs + i) != 0){
610 eprint(bdb, "pthread_create");
615 for(int i = 0; i < tnum; i++){
616 if(targs[i].id == -1) continue;
618 if(pthread_join(threads[i], &rv) != 0){
619 eprint(bdb, "pthread_join");
626 iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));
627 iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));
629 if(!tcbdbclose(bdb)){
630 eprint(bdb, "tcbdbclose");
634 iprintf("time: %.3f\n", tctime() - stime);
635 iprintf("%s\n\n", err ? "error" : "ok");
640 /* perform wicked command */
641 static int procwicked(const char *path, int tnum, int rnum, int opts, int omode, bool nc){
642 iprintf("<Writing Test>\n path=%s tnum=%d rnum=%d opts=%d omode=%d nc=%d\n\n",
643 path, tnum, rnum, opts, omode, nc);
645 double stime = tctime();
646 TCBDB *bdb = tcbdbnew();
647 if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);
648 if(!tcbdbsetmutex(bdb)){
649 eprint(bdb, "tcbdbsetmutex");
652 if(!tcbdbtune(bdb, 10, 10, rnum / 50, 10, -1, opts)){
653 eprint(bdb, "tcbdbtune");
656 if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){
657 eprint(bdb, "tcbdbopen");
660 TARGWICKED targs[tnum];
661 pthread_t threads[tnum];
662 TCMAP *map = tcmapnew();
665 targs[0].rnum = rnum;
669 if(threadwicked(targs) != NULL) err = true;
671 for(int i = 0; i < tnum; i++){
673 targs[i].rnum = rnum;
678 if(pthread_create(threads + i, NULL, threadwicked, targs + i) != 0){
679 eprint(bdb, "pthread_create");
684 for(int i = 0; i < tnum; i++){
685 if(targs[i].id == -1) continue;
687 if(pthread_join(threads[i], &rv) != 0){
688 eprint(bdb, "pthread_join");
697 eprint(bdb, "tcbdbsync");
700 if(tcbdbrnum(bdb) != tcmaprnum(map)){
701 eprint(bdb, "(validation)");
704 int end = rnum * tnum;
705 for(int i = 1; i <= end && !err; i++){
706 char kbuf[RECBUFSIZ];
707 int ksiz = sprintf(kbuf, "%d", i - 1);
709 const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz);
711 char *rbuf = tcbdbget(bdb, kbuf, ksiz, &rsiz);
715 eprint(bdb, "tcbdbget");
717 } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
718 eprint(bdb, "(validation)");
723 if(rbuf || tcbdbecode(bdb) != TCENOREC){
724 eprint(bdb, "(validation)");
729 if(i % 50 == 0) iprintf(" (%08d)\n", i);
731 if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum);
734 iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));
735 iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));
737 if(!tcbdbclose(bdb)){
738 eprint(bdb, "tcbdbclose");
742 iprintf("time: %.3f\n", tctime() - stime);
743 iprintf("%s\n\n", err ? "error" : "ok");
748 /* perform typical command */
749 static int proctypical(const char *path, int tnum, int rnum, int lmemb, int nmemb,
750 int bnum, int apow, int fpow, int opts, int omode, bool nc, int rratio){
751 iprintf("<Typical Access Test>\n path=%s tnum=%d rnum=%d lmemb=%d nmemb=%d"
752 " bnum=%d apow=%d fpow=%d opts=%d omode=%d nc=%d rratio=%d\n\n",
753 path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, nc, rratio);
755 double stime = tctime();
756 TCBDB *bdb = tcbdbnew();
757 if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);
758 if(!tcbdbsetmutex(bdb)){
759 eprint(bdb, "tcbdbsetmutex");
762 if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){
763 eprint(bdb, "tcbdbtune");
766 if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){
767 eprint(bdb, "tcbdbopen");
770 TARGTYPICAL targs[tnum];
771 pthread_t threads[tnum];
774 targs[0].rnum = rnum;
776 targs[0].rratio = rratio;
778 if(threadtypical(targs) != NULL) err = true;
780 for(int i = 0; i < tnum; i++){
782 targs[i].rnum = rnum;
784 targs[i].rratio = rratio;
786 if(pthread_create(threads + i, NULL, threadtypical, targs + i) != 0){
787 eprint(bdb, "pthread_create");
792 for(int i = 0; i < tnum; i++){
793 if(targs[i].id == -1) continue;
795 if(pthread_join(threads[i], &rv) != 0){
796 eprint(bdb, "pthread_join");
803 iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));
804 iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));
806 if(!tcbdbclose(bdb)){
807 eprint(bdb, "tcbdbclose");
811 iprintf("time: %.3f\n", tctime() - stime);
812 iprintf("%s\n\n", err ? "error" : "ok");
817 /* thread the write function */
818 static void *threadwrite(void *targ){
819 TCBDB *bdb = ((TARGWRITE *)targ)->bdb;
820 int rnum = ((TARGWRITE *)targ)->rnum;
821 bool rnd = ((TARGWRITE *)targ)->rnd;
822 int id = ((TARGWRITE *)targ)->id;
824 int base = id * rnum;
825 for(int i = 1; i <= rnum; i++){
827 int len = sprintf(buf, "%08d", base + (rnd ? myrand(i) : i));
828 if(!tcbdbput(bdb, buf, len, buf, len)){
829 eprint(bdb, "tcbdbput");
833 if(id <= 0 && rnum > 250 && i % (rnum / 250) == 0){
836 if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
839 return err ? "error" : NULL;
843 /* thread the read function */
844 static void *threadread(void *targ){
845 TCBDB *bdb = ((TARGREAD *)targ)->bdb;
846 int rnum = ((TARGREAD *)targ)->rnum;
847 bool wb = ((TARGREAD *)targ)->wb;
848 bool rnd = ((TARGREAD *)targ)->rnd;
849 int id = ((TARGREAD *)targ)->id;
851 int base = id * rnum;
852 for(int i = 1; i <= rnum && !err; i++){
853 char kbuf[RECBUFSIZ];
854 int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrandnd(i) : i));
858 const char *vbuf = tcbdbget3(bdb, kbuf, ksiz, &vsiz);
859 if(!vbuf && (!rnd || tcbdbecode(bdb) != TCENOREC)){
860 eprint(bdb, "tcbdbget3");
864 char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);
865 if(!vbuf && (!rnd || tcbdbecode(bdb) != TCENOREC)){
866 eprint(bdb, "tcbdbget");
871 if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){
874 if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
877 return err ? "error" : NULL;
881 /* thread the remove function */
882 static void *threadremove(void *targ){
883 TCBDB *bdb = ((TARGREMOVE *)targ)->bdb;
884 int rnum = ((TARGREMOVE *)targ)->rnum;
885 bool rnd = ((TARGREMOVE *)targ)->rnd;
886 int id = ((TARGREMOVE *)targ)->id;
888 int base = id * rnum;
889 for(int i = 1; i <= rnum; i++){
890 char kbuf[RECBUFSIZ];
891 int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrand(i + 1) : i));
892 if(!tcbdbout(bdb, kbuf, ksiz) && (!rnd || tcbdbecode(bdb) != TCENOREC)){
893 eprint(bdb, "tcbdbout");
897 if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){
900 if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
903 return err ? "error" : NULL;
907 /* thread the wicked function */
908 static void *threadwicked(void *targ){
909 TCBDB *bdb = ((TARGWICKED *)targ)->bdb;
910 int rnum = ((TARGWICKED *)targ)->rnum;
911 bool nc = ((TARGWICKED *)targ)->nc;
912 int id = ((TARGWICKED *)targ)->id;
913 TCMAP *map = ((TARGWICKED *)targ)->map;
914 BDBCUR *cur = tcbdbcurnew(bdb);
916 for(int i = 1; i <= rnum && !err; i++){
917 char kbuf[RECBUFSIZ];
918 int ksiz = sprintf(kbuf, "%d", myrand(rnum * (id + 1)));
919 char vbuf[RECBUFSIZ];
920 int vsiz = myrand(RECBUFSIZ);
921 memset(vbuf, '*', vsiz);
924 if(!nc) tcglobalmutexlock();
927 if(id == 0) putchar('0');
928 if(!tcbdbput(bdb, kbuf, ksiz, vbuf, vsiz)){
929 eprint(bdb, "tcbdbput");
932 if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);
935 if(id == 0) putchar('1');
936 if(!tcbdbput2(bdb, kbuf, vbuf)){
937 eprint(bdb, "tcbdbput2");
940 if(!nc) tcmapput2(map, kbuf, vbuf);
943 if(id == 0) putchar('2');
944 if(!tcbdbputkeep(bdb, kbuf, ksiz, vbuf, vsiz) && tcbdbecode(bdb) != TCEKEEP){
945 eprint(bdb, "tcbdbputkeep");
948 if(!nc) tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);
951 if(id == 0) putchar('3');
952 if(!tcbdbputkeep2(bdb, kbuf, vbuf) && tcbdbecode(bdb) != TCEKEEP){
953 eprint(bdb, "tcbdbputkeep2");
956 if(!nc) tcmapputkeep2(map, kbuf, vbuf);
959 if(id == 0) putchar('4');
960 if(!tcbdbputcat(bdb, kbuf, ksiz, vbuf, vsiz)){
961 eprint(bdb, "tcbdbputcat");
964 if(!nc) tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);
967 if(id == 0) putchar('5');
968 if(!tcbdbputcat2(bdb, kbuf, vbuf)){
969 eprint(bdb, "tcbdbputcat2");
972 if(!nc) tcmapputcat2(map, kbuf, vbuf);
975 if(id == 0) putchar('6');
977 if(!tcbdbputdup(bdb, kbuf, ksiz, vbuf, vsiz)){
978 eprint(bdb, "tcbdbputdup");
984 if(id == 0) putchar('7');
986 if(!tcbdbputdup2(bdb, kbuf, vbuf)){
987 eprint(bdb, "tcbdbputdup2");
993 if(id == 0) putchar('8');
995 if(!tcbdbout(bdb, kbuf, ksiz) && tcbdbecode(bdb) != TCENOREC){
996 eprint(bdb, "tcbdbout");
999 if(!nc) tcmapout(map, kbuf, ksiz);
1003 if(id == 0) putchar('9');
1004 if(myrand(10) == 0){
1005 if(!tcbdbout2(bdb, kbuf) && tcbdbecode(bdb) != TCENOREC){
1006 eprint(bdb, "tcbdbout2");
1009 if(!nc) tcmapout2(map, kbuf);
1013 if(id == 0) putchar('A');
1014 if(!(rbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz))){
1015 if(tcbdbecode(bdb) != TCENOREC){
1016 eprint(bdb, "tcbdbget");
1019 rbuf = tcsprintf("[%d]", myrand(i + 1));
1020 vsiz = strlen(rbuf);
1022 vsiz += myrand(vsiz);
1023 if(myrand(3) == 0) vsiz += PATH_MAX;
1024 rbuf = tcrealloc(rbuf, vsiz + 1);
1025 for(int j = 0; j < vsiz; j++){
1026 rbuf[j] = myrand(0x100);
1028 if(!tcbdbput(bdb, kbuf, ksiz, rbuf, vsiz)){
1029 eprint(bdb, "tcbdbput");
1032 if(!nc) tcmapput(map, kbuf, ksiz, rbuf, vsiz);
1036 if(id == 0) putchar('B');
1037 if(!(rbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz)) && tcbdbecode(bdb) != TCENOREC){
1038 eprint(bdb, "tcbdbget");
1044 if(id == 0) putchar('C');
1045 if(!(rbuf = tcbdbget2(bdb, kbuf)) && tcbdbecode(bdb) != TCENOREC){
1046 eprint(bdb, "tcbdbget");
1052 if(id == 0) putchar('D');
1053 if(!tcbdbget3(bdb, kbuf, ksiz, &vsiz) && tcbdbecode(bdb) != TCENOREC){
1054 eprint(bdb, "tcbdbget");
1059 if(id == 0) putchar('E');
1060 if(myrand(rnum / 50) == 0){
1063 if(!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC){
1064 eprint(bdb, "tcbdbcurfirst");
1069 if(!tcbdbcurlast(cur) && tcbdbecode(bdb) != TCENOREC){
1070 eprint(bdb, "tcbdbcurfirst");
1075 if(!tcbdbcurjump(cur, kbuf, ksiz) && tcbdbecode(bdb) != TCENOREC){
1076 eprint(bdb, "tcbdbcurjump");
1082 TCXSTR *ikey = tcxstrnew();
1083 TCXSTR *ival = tcxstrnew();
1084 for(int j = myrand(rnum) / 1000 + 1; j >= 0; j--){
1086 if(!tcbdbcurrec(cur, ikey, ival)){
1087 int ecode = tcbdbecode(bdb);
1088 if(ecode != TCEINVALID && ecode != TCENOREC){
1089 eprint(bdb, "tcbdbcurrec");
1095 if(!tcbdbcurkey3(cur, &iksiz)){
1096 int ecode = tcbdbecode(bdb);
1097 if(ecode != TCEINVALID && ecode != TCENOREC){
1098 eprint(bdb, "tcbdbcurkey3");
1104 if(!tcbdbcurprev(cur)){
1105 int ecode = tcbdbecode(bdb);
1106 if(ecode != TCEINVALID && ecode != TCENOREC){
1107 eprint(bdb, "tcbdbcurprev");
1112 if(!tcbdbcurnext(cur)){
1113 int ecode = tcbdbecode(bdb);
1114 if(ecode != TCEINVALID && ecode != TCENOREC){
1115 eprint(bdb, "tcbdbcurnext");
1125 if(id == 0) putchar('@');
1126 if(myrand(10000) == 0) srand((unsigned int)(tctime() * 1000) % UINT_MAX);
1129 if(!nc) tcglobalmutexunlock();
1131 if(i % 50 == 0) iprintf(" (%08d)\n", i);
1132 if(id == 0 && i == rnum / 4){
1133 if(!tcbdboptimize(bdb, -1, -1, -1, -1, -1, -1)){
1134 eprint(bdb, "tcbdboptimize");
1137 if(!tcbdbcurfirst(cur)){
1138 eprint(bdb, "tcbdbcurfirst");
1145 return err ? "error" : NULL;
1149 /* thread the typical function */
1150 static void *threadtypical(void *targ){
1151 TCBDB *bdb = ((TARGTYPICAL *)targ)->bdb;
1152 int rnum = ((TARGTYPICAL *)targ)->rnum;
1153 bool nc = ((TARGTYPICAL *)targ)->nc;
1154 int rratio = ((TARGTYPICAL *)targ)->rratio;
1155 int id = ((TARGTYPICAL *)targ)->id;
1157 TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL;
1158 int base = id * rnum;
1159 int mrange = tclmax(50 + rratio, 100);
1160 BDBCUR *cur = tcbdbcurnew(bdb);
1161 for(int i = 1; !err && i <= rnum; i++){
1162 char buf[RECBUFSIZ];
1163 int len = sprintf(buf, "%08d", base + myrandnd(i));
1164 int rnd = myrand(mrange);
1166 if(!tcbdbput(bdb, buf, len, buf, len)){
1167 eprint(bdb, "tcbdbput");
1170 if(map) tcmapput(map, buf, len, buf, len);
1171 } else if(rnd < 15){
1172 if(!tcbdbputkeep(bdb, buf, len, buf, len) && tcbdbecode(bdb) != TCEKEEP){
1173 eprint(bdb, "tcbdbputkeep");
1176 if(map) tcmapputkeep(map, buf, len, buf, len);
1177 } else if(rnd < 20){
1178 if(!tcbdbputcat(bdb, buf, len, buf, len)){
1179 eprint(bdb, "tcbdbputcat");
1182 if(map) tcmapputcat(map, buf, len, buf, len);
1183 } else if(rnd < 25){
1184 if(!tcbdbout(bdb, buf, len) && tcbdbecode(bdb) && tcbdbecode(bdb) != TCENOREC){
1185 eprint(bdb, "tcbdbout");
1188 if(map) tcmapout(map, buf, len);
1189 } else if(rnd < 27){
1192 if(!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC){
1193 eprint(bdb, "tcbdbcurfirst");
1196 for(int j = 0; !err && j < 10; j++){
1198 char *kbuf = tcbdbcurkey(cur, &ksiz);
1201 char *vbuf = tcbdbcurval(cur, &vsiz);
1204 } else if(tcbdbecode(bdb) != TCENOREC){
1205 eprint(bdb, "tcbdbcurval");
1209 } else if(tcbdbecode(bdb) != TCENOREC){
1210 eprint(bdb, "tcbdbcurkey");
1217 if(!tcbdbcurlast(cur) && tcbdbecode(bdb) != TCENOREC){
1218 eprint(bdb, "tcbdbcurlast");
1221 for(int j = 0; !err && j < 10; j++){
1223 char *kbuf = tcbdbcurkey(cur, &ksiz);
1226 char *vbuf = tcbdbcurval(cur, &vsiz);
1229 } else if(tcbdbecode(bdb) != TCENOREC){
1230 eprint(bdb, "tcbdbcurval");
1234 } else if(tcbdbecode(bdb) != TCENOREC){
1235 eprint(bdb, "tcbdbcurkey");
1242 if(!tcbdbcurjump(cur, buf, len) && tcbdbecode(bdb) != TCENOREC){
1243 eprint(bdb, "tcbdbcurjump");
1246 for(int j = 0; !err && j < 10; j++){
1248 char *kbuf = tcbdbcurkey(cur, &ksiz);
1251 char *vbuf = tcbdbcurval(cur, &vsiz);
1254 } else if(tcbdbecode(bdb) != TCENOREC){
1255 eprint(bdb, "tcbdbcurval");
1259 } else if(tcbdbecode(bdb) != TCENOREC){
1260 eprint(bdb, "tcbdbcurkey");
1269 char *vbuf = tcbdbget(bdb, buf, len, &vsiz);
1273 const char *mbuf = tcmapget(map, buf, len, &msiz);
1274 if(msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
1275 eprint(bdb, "(validation)");
1281 if(tcbdbecode(bdb) != TCENOREC){
1282 eprint(bdb, "tcbdbget");
1285 if(map && tcmapget(map, buf, len, &vsiz)){
1286 eprint(bdb, "(validation)");
1291 if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){
1294 if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
1302 while(!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL){
1304 char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);
1307 const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz);
1308 if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
1309 eprint(bdb, "(validation)");
1314 eprint(bdb, "(validation)");
1320 return err ? "error" : NULL;