?? tcbtest.c
字號:
/************************************************************************************************* * The test cases of the B+ tree database API * Copyright (C) 2006-2009 Mikio Hirabayashi * This file is part of Tokyo Cabinet. * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation; either * version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * You should have received a copy of the GNU Lesser General Public License along with Tokyo * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA. *************************************************************************************************/#include <tcutil.h>#include <tcbdb.h>#include "myconf.h"#define RECBUFSIZ 48 // buffer for records/* global variables */const char *g_progname; // program nameunsigned int g_randseed; // random seedint g_dbgfd; // debugging output/* function prototypes */int main(int argc, char **argv);static void usage(void);static void iprintf(const char *format, ...);static void iputchar(int c);static void eprint(TCBDB *bdb, const char *func);static void mprint(TCBDB *bdb);static void sysprint(void);static int myrand(int range);static void *pdprocfunc(const void *vbuf, int vsiz, int *sp, void *op);static bool iterfunc(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op);static int runwrite(int argc, char **argv);static int runread(int argc, char **argv);static int runremove(int argc, char **argv);static int runrcat(int argc, char **argv);static int runqueue(int argc, char **argv);static int runmisc(int argc, char **argv);static int runwicked(int argc, char **argv);static int procwrite(const char *path, int rnum, int lmemb, int nmemb, int bnum, int apow, int fpow, bool mt, TCCMP cmp, int opts, int lcnum, int ncnum, int xmsiz, int lsmax, int capnum, int omode, bool rnd);static int procread(const char *path, bool mt, TCCMP cmp, int lcnum, int ncnum, int xmsiz, int omode, bool wb, bool rnd);static int procremove(const char *path, bool mt, TCCMP cmp, int lcnum, int ncnum, int xmsiz, int omode, bool rnd);static int procrcat(const char *path, int rnum, int lmemb, int nmemb, int bnum, int apow, int fpow, bool mt, TCCMP cmp, int opts, int lcnum, int ncnum, int xmsiz, int lsmax, int capnum, int omode, int pnum, bool dai, bool dad, bool rl, bool ru);static int procqueue(const char *path, int rnum, int lmemb, int nmemb, int bnum, int apow, int fpow, bool mt, TCCMP cmp, int opts, int lcnum, int ncnum, int xmsiz, int lsmax, int capnum, int omode);static int procmisc(const char *path, int rnum, bool mt, int opts, int omode);static int procwicked(const char *path, int rnum, bool mt, int opts, int omode);/* main routine */int main(int argc, char **argv){ g_progname = argv[0]; const char *ebuf = getenv("TCRNDSEED"); g_randseed = ebuf ? tcatoix(ebuf) : tctime() * 1000; srand(g_randseed); ebuf = getenv("TCDBGFD"); g_dbgfd = ebuf ? tcatoix(ebuf) : UINT16_MAX; if(argc < 2) usage(); int rv = 0; if(!strcmp(argv[1], "write")){ rv = runwrite(argc, argv); } else if(!strcmp(argv[1], "read")){ rv = runread(argc, argv); } else if(!strcmp(argv[1], "remove")){ rv = runremove(argc, argv); } else if(!strcmp(argv[1], "rcat")){ rv = runrcat(argc, argv); } else if(!strcmp(argv[1], "queue")){ rv = runqueue(argc, argv); } else if(!strcmp(argv[1], "misc")){ rv = runmisc(argc, argv); } else if(!strcmp(argv[1], "wicked")){ rv = runwicked(argc, argv); } else { usage(); } return rv;}/* print the usage and exit */static void usage(void){ fprintf(stderr, "%s: test cases of the B+ tree database API of Tokyo Cabinet\n", g_progname); fprintf(stderr, "\n"); fprintf(stderr, "usage:\n"); fprintf(stderr, " %s write [-mt] [-cd|-ci|-cj] [-tl] [-td|-tb|-tt|-tx] [-lc num] [-nc num]" " [-xm num] [-ls num] [-ca num] [-nl|-nb] [-rnd] path rnum" " [lmemb [nmemb [bnum [apow [fpow]]]]]\n", g_progname); fprintf(stderr, " %s read [-mt] [-cd|-ci|-cj] [-lc num] [-nc num] [-xm num] [-nl|-nb]" " [-wb] [-rnd] path\n", g_progname); fprintf(stderr, " %s remove [-mt] [-cd|-ci|-cj] [-lc num] [-nc num] [-xm num] [-nl|-nb] [-rnd]" " path\n", g_progname); fprintf(stderr, " %s rcat [-mt] [-cd|-ci|-cj] [-tl] [-td|-tb|-tt|-tx] [-lc num] [-nc num]" " [-xm num] [-ls num] [-ca num] [-nl|-nb] [-pn num] [-dai|-dad|-rl|-ru] path rnum" " [lmemb [nmemb [bnum [apow [fpow]]]]]\n", g_progname); fprintf(stderr, " %s queue [-mt] [-cd|-ci|-cj] [-tl] [-td|-tb|-tt|-tx] [-lc num] [-nc num]" " [-xm num] [-ls num] [-ca num] [-nl|-nb] path rnum" " [lmemb [nmemb [bnum [apow [fpow]]]]]\n", g_progname); fprintf(stderr, " %s misc [-mt] [-tl] [-td|-tb|-tt|-tx] [-nl|-nb] path rnum\n", g_progname); fprintf(stderr, " %s wicked [-mt] [-tl] [-td|-tb|-tt|-tx] [-nl|-nb] path rnum\n", g_progname); fprintf(stderr, "\n"); exit(1);}/* print formatted information string and flush the buffer */static void iprintf(const char *format, ...){ va_list ap; va_start(ap, format); vprintf(format, ap); fflush(stdout); va_end(ap);}/* print a character and flush the buffer */static void iputchar(int c){ putchar(c); fflush(stdout);}/* print error message of hash database */static void eprint(TCBDB *bdb, const char *func){ const char *path = tcbdbpath(bdb); int ecode = tcbdbecode(bdb); fprintf(stderr, "%s: %s: %s: error: %d: %s\n", g_progname, path ? path : "-", func, ecode, tcbdberrmsg(ecode));}/* print members of B+ tree database */static void mprint(TCBDB *bdb){ if(bdb->hdb->cnt_writerec < 0) return; iprintf("max leaf member: %d\n", tcbdblmemb(bdb)); iprintf("max node member: %d\n", tcbdbnmemb(bdb)); iprintf("leaf number: %d\n", tcbdblnum(bdb)); iprintf("node number: %d\n", tcbdbnnum(bdb)); iprintf("bucket number: %lld\n", (long long)tcbdbbnum(bdb)); iprintf("used bucket number: %lld\n", (long long)tcbdbbnumused(bdb)); iprintf("cnt_saveleaf: %lld\n", (long long)bdb->cnt_saveleaf); iprintf("cnt_loadleaf: %lld\n", (long long)bdb->cnt_loadleaf); iprintf("cnt_killleaf: %lld\n", (long long)bdb->cnt_killleaf); iprintf("cnt_adjleafc: %lld\n", (long long)bdb->cnt_adjleafc); iprintf("cnt_savenode: %lld\n", (long long)bdb->cnt_savenode); iprintf("cnt_loadnode: %lld\n", (long long)bdb->cnt_loadnode); iprintf("cnt_adjnodec: %lld\n", (long long)bdb->cnt_adjnodec); iprintf("cnt_writerec: %lld\n", (long long)bdb->hdb->cnt_writerec); iprintf("cnt_reuserec: %lld\n", (long long)bdb->hdb->cnt_reuserec); iprintf("cnt_moverec: %lld\n", (long long)bdb->hdb->cnt_moverec); iprintf("cnt_readrec: %lld\n", (long long)bdb->hdb->cnt_readrec); iprintf("cnt_searchfbp: %lld\n", (long long)bdb->hdb->cnt_searchfbp); iprintf("cnt_insertfbp: %lld\n", (long long)bdb->hdb->cnt_insertfbp); iprintf("cnt_splicefbp: %lld\n", (long long)bdb->hdb->cnt_splicefbp); iprintf("cnt_dividefbp: %lld\n", (long long)bdb->hdb->cnt_dividefbp); iprintf("cnt_mergefbp: %lld\n", (long long)bdb->hdb->cnt_mergefbp); iprintf("cnt_reducefbp: %lld\n", (long long)bdb->hdb->cnt_reducefbp); iprintf("cnt_appenddrp: %lld\n", (long long)bdb->hdb->cnt_appenddrp); iprintf("cnt_deferdrp: %lld\n", (long long)bdb->hdb->cnt_deferdrp); iprintf("cnt_flushdrp: %lld\n", (long long)bdb->hdb->cnt_flushdrp); iprintf("cnt_adjrecc: %lld\n", (long long)bdb->hdb->cnt_adjrecc);}/* print system information */static void sysprint(void){ TCMAP *info = tcsysinfo(); if(info){ tcmapiterinit(info); const char *kbuf; while((kbuf = tcmapiternext2(info)) != NULL){ iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf)); } tcmapdel(info); }}/* get a random number */static int myrand(int range){ if(range < 2) return 0; int high = (unsigned int)rand() >> 4; int low = range * (rand() / (RAND_MAX + 1.0)); low &= (unsigned int)INT_MAX >> 4; return (high + low) % range;}/* duplication callback function */static void *pdprocfunc(const void *vbuf, int vsiz, int *sp, void *op){ if(op){ char *buf = NULL; int len = 0; switch((int)(intptr_t)op){ case 1: len = vsiz + 1; buf = tcmalloc(len + 1); memset(buf, '*', len); break; case 2: buf = (void *)-1; break; } *sp = len; return buf; } if(myrand(4) == 0) return (void *)-1; if(myrand(2) == 0) return NULL; int len = myrand(RECBUFSIZ); char buf[RECBUFSIZ]; memset(buf, '*', len); *sp = len; return tcmemdup(buf, len);}/* iterator function */static bool iterfunc(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op){ unsigned int sum = 0; while(--ksiz >= 0){ sum += ((char *)kbuf)[ksiz]; } while(--vsiz >= 0){ sum += ((char *)vbuf)[vsiz]; } return myrand(100 + (sum & 0xff)) > 0;}/* parse arguments of write command */static int runwrite(int argc, char **argv){ char *path = NULL; char *rstr = NULL; char *lmstr = NULL; char *nmstr = NULL; char *bstr = NULL; char *astr = NULL; char *fstr = NULL; bool mt = false; TCCMP cmp = NULL; int opts = 0; int lcnum = 0; int ncnum = 0; int xmsiz = 0; int lsmax = 0; int capnum = 0; int omode = 0; bool rnd = false; for(int i = 2; i < argc; i++){ if(!path && argv[i][0] == '-'){ if(!strcmp(argv[i], "-mt")){ mt = true; } else if(!strcmp(argv[i], "-cd")){ cmp = tccmpdecimal; } else if(!strcmp(argv[i], "-ci")){ cmp = tccmpint32; } else if(!strcmp(argv[i], "-cj")){ cmp = tccmpint64; } else if(!strcmp(argv[i], "-tl")){ opts |= BDBTLARGE; } else if(!strcmp(argv[i], "-td")){ opts |= BDBTDEFLATE; } else if(!strcmp(argv[i], "-tb")){ opts |= BDBTBZIP; } else if(!strcmp(argv[i], "-tt")){ opts |= BDBTTCBS; } else if(!strcmp(argv[i], "-tx")){ opts |= BDBTEXCODEC; } else if(!strcmp(argv[i], "-lc")){ if(++i >= argc) usage(); lcnum = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-nc")){ if(++i >= argc) usage(); ncnum = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-xm")){ if(++i >= argc) usage(); xmsiz = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-ls")){ if(++i >= argc) usage(); lsmax = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-ca")){ if(++i >= argc) usage(); capnum = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-nl")){ omode |= BDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= BDBOLCKNB; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else { usage(); } } else if(!path){ path = argv[i]; } else if(!rstr){ rstr = argv[i]; } else if(!lmstr){ lmstr = argv[i]; } else if(!nmstr){ nmstr = argv[i]; } else if(!bstr){ bstr = argv[i]; } else if(!astr){ astr = argv[i]; } else if(!fstr){ fstr = argv[i]; } else { usage(); } } if(!path || !rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int lmemb = lmstr ? tcatoix(lmstr) : -1; int nmemb = nmstr ? tcatoix(nmstr) : -1; int bnum = bstr ? tcatoix(bstr) : -1; int apow = astr ? tcatoix(astr) : -1; int fpow = fstr ? tcatoix(fstr) : -1; int rv = procwrite(path, rnum, lmemb, nmemb, bnum, apow, fpow, mt, cmp, opts, lcnum, ncnum, xmsiz, lsmax, capnum, omode, rnd); return rv;}/* parse arguments of read command */static int runread(int argc, char **argv){ char *path = NULL; bool mt = false; TCCMP cmp = NULL; int lcnum = 0; int ncnum = 0; int xmsiz = 0; int omode = 0; bool wb = false; bool rnd = false; for(int i = 2; i < argc; i++){ if(!path && argv[i][0] == '-'){ if(!strcmp(argv[i], "-mt")){ mt = true; } else if(!strcmp(argv[i], "-cd")){ cmp = tccmpdecimal; } else if(!strcmp(argv[i], "-ci")){ cmp = tccmpint32; } else if(!strcmp(argv[i], "-cj")){ cmp = tccmpint64; } else if(!strcmp(argv[i], "-lc")){ if(++i >= argc) usage(); lcnum = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-nc")){ if(++i >= argc) usage(); ncnum = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-xm")){ if(++i >= argc) usage(); xmsiz = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-nl")){ omode |= BDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= BDBOLCKNB; } else if(!strcmp(argv[i], "-wb")){ wb = true; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else { usage(); } } else if(!path){ path = argv[i]; } else { usage(); } } if(!path) usage(); int rv = procread(path, mt, cmp, lcnum, ncnum, xmsiz, omode, wb, rnd); return rv;}/* parse arguments of remove command */static int runremove(int argc, char **argv){ char *path = NULL; bool mt = false; TCCMP cmp = NULL; int lcnum = 0; int ncnum = 0; int xmsiz = 0; int omode = 0; bool rnd = false; for(int i = 2; i < argc; i++){ if(!path && argv[i][0] == '-'){ if(!strcmp(argv[i], "-mt")){ mt = true; } else if(!strcmp(argv[i], "-cd")){ cmp = tccmpdecimal; } else if(!strcmp(argv[i], "-ci")){ cmp = tccmpint32; } else if(!strcmp(argv[i], "-cj")){ cmp = tccmpint64; } else if(!strcmp(argv[i], "-lc")){ if(++i >= argc) usage(); lcnum = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-nc")){ if(++i >= argc) usage(); ncnum = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-xm")){ if(++i >= argc) usage(); xmsiz = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-nl")){ omode |= BDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= BDBOLCKNB; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else { usage(); } } else if(!path){ path = argv[i]; } else { usage(); } } if(!path) usage(); int rv = procremove(path, mt, cmp, lcnum, ncnum, xmsiz, omode, rnd); return rv;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -