📄 tchtest.c
字号:
/************************************************************************************************* * The test cases of the hash database API * Copyright (C) 2006-2008 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 <tchdb.h>#include "myconf.h"#define RECBUFSIZ 32 // buffer for records/* global variables */const char *g_progname; // program nameint 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(TCHDB *hdb, const char *func);static void mprint(TCHDB *hdb);static int myrand(int range);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 runmisc(int argc, char **argv);static int runwicked(int argc, char **argv);static int procwrite(const char *path, int rnum, int bnum, int apow, int fpow, bool mt, int opts, int rcnum, int xmsiz, int omode, bool as, bool rnd);static int procread(const char *path, bool mt, int rcnum, int xmsiz, int omode, bool wb, bool rnd);static int procremove(const char *path, bool mt, int rcnum, int xmsiz, int omode, bool rnd);static int procrcat(const char *path, int rnum, int bnum, int apow, int fpow, bool mt, int opts, int rcnum, int xmsiz, int omode, int pnum, bool dai, bool dad, bool rl);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]; g_dbgfd = -1; const char *ebuf = getenv("TCDBGFD"); if(ebuf) g_dbgfd = tcatoi(ebuf); srand((unsigned int)(tctime() * 1000) % UINT_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], "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 hash database API of Tokyo Cabinet\n", g_progname); fprintf(stderr, "\n"); fprintf(stderr, "usage:\n"); fprintf(stderr, " %s write [-mt] [-tl] [-td|-tb|-tt|-tx] [-rc num] [-xm num] [-nl|-nb]" " [-as] [-rnd] path rnum [bnum [apow [fpow]]]\n", g_progname); fprintf(stderr, " %s read [-mt] [-rc num] [-xm num] [-nl|-nb] [-wb] [-rnd] path\n", g_progname); fprintf(stderr, " %s remove [-mt] [-rc num] [-xm num] [-nl|-nb] [-rnd] path\n", g_progname); fprintf(stderr, " %s rcat [-mt] [-rc num] [-xm num] [-tl] [-td|-tb|-tt|-tx] [-nl|-nb]" " [-pn num] [-dai|-dad|-rl] path rnum [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(TCHDB *hdb, const char *func){ const char *path = tchdbpath(hdb); int ecode = tchdbecode(hdb); fprintf(stderr, "%s: %s: %s: error: %d: %s\n", g_progname, path ? path : "-", func, ecode, tchdberrmsg(ecode));}/* print members of hash database */static void mprint(TCHDB *hdb){ if(hdb->cnt_writerec < 0) return; iprintf("bucket number: %lld\n", (long long)tchdbbnum(hdb)); iprintf("used bucket number: %lld\n", (long long)tchdbbnumused(hdb)); iprintf("cnt_writerec: %lld\n", (long long)hdb->cnt_writerec); iprintf("cnt_reuserec: %lld\n", (long long)hdb->cnt_reuserec); iprintf("cnt_moverec: %lld\n", (long long)hdb->cnt_moverec); iprintf("cnt_readrec: %lld\n", (long long)hdb->cnt_readrec); iprintf("cnt_searchfbp: %lld\n", (long long)hdb->cnt_searchfbp); iprintf("cnt_insertfbp: %lld\n", (long long)hdb->cnt_insertfbp); iprintf("cnt_splicefbp: %lld\n", (long long)hdb->cnt_splicefbp); iprintf("cnt_dividefbp: %lld\n", (long long)hdb->cnt_dividefbp); iprintf("cnt_mergefbp: %lld\n", (long long)hdb->cnt_mergefbp); iprintf("cnt_reducefbp: %lld\n", (long long)hdb->cnt_reducefbp); iprintf("cnt_appenddrp: %lld\n", (long long)hdb->cnt_appenddrp); iprintf("cnt_deferdrp: %lld\n", (long long)hdb->cnt_deferdrp); iprintf("cnt_flushdrp: %lld\n", (long long)hdb->cnt_flushdrp); iprintf("cnt_adjrecc: %lld\n", (long long)hdb->cnt_adjrecc);}/* get a random number */static int myrand(int range){ return (int)((double)range * rand() / (RAND_MAX + 1.0));}/* parse arguments of write command */static int runwrite(int argc, char **argv){ char *path = NULL; char *rstr = NULL; char *bstr = NULL; char *astr = NULL; char *fstr = NULL; bool mt = false; int opts = 0; int rcnum = 0; int xmsiz = -1; int omode = 0; bool as = 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], "-tl")){ opts |= HDBTLARGE; } else if(!strcmp(argv[i], "-td")){ opts |= HDBTDEFLATE; } else if(!strcmp(argv[i], "-tb")){ opts |= HDBTBZIP; } else if(!strcmp(argv[i], "-tt")){ opts |= HDBTTCBS; } else if(!strcmp(argv[i], "-tx")){ opts |= HDBTEXCODEC; } else if(!strcmp(argv[i], "-rc")){ if(++i >= argc) usage(); rcnum = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-xm")){ if(++i >= argc) usage(); xmsiz = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-nl")){ omode |= HDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= HDBOLCKNB; } else if(!strcmp(argv[i], "-as")){ as = true; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else { usage(); } } else if(!path){ path = argv[i]; } else if(!rstr){ rstr = 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 = tcatoi(rstr); if(rnum < 1) usage(); int bnum = bstr ? tcatoi(bstr) : -1; int apow = astr ? tcatoi(astr) : -1; int fpow = fstr ? tcatoi(fstr) : -1; int rv = procwrite(path, rnum, bnum, apow, fpow, mt, opts, rcnum, xmsiz, omode, as, rnd); return rv;}/* parse arguments of read command */static int runread(int argc, char **argv){ char *path = NULL; bool mt = false; int rcnum = 0; int xmsiz = -1; 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], "-rc")){ if(++i >= argc) usage(); rcnum = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-xm")){ if(++i >= argc) usage(); xmsiz = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-nl")){ omode |= HDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= HDBOLCKNB; } 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, rcnum, xmsiz, omode, wb, rnd); return rv;}/* parse arguments of remove command */static int runremove(int argc, char **argv){ char *path = NULL; bool mt = false; int rcnum = 0; int xmsiz = -1; 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], "-rc")){ if(++i >= argc) usage(); rcnum = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-xm")){ if(++i >= argc) usage(); xmsiz = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-nl")){ omode |= HDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= HDBOLCKNB; } 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, rcnum, xmsiz, omode, rnd); return rv;}/* parse arguments of rcat command */static int runrcat(int argc, char **argv){ char *path = NULL; char *rstr = NULL; char *bstr = NULL; char *astr = NULL; char *fstr = NULL; bool mt = false; int opts = 0; int rcnum = 0; int xmsiz = -1; int omode = 0; int pnum = 0; bool dai = false; bool dad = false; bool rl = 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], "-tl")){ opts |= HDBTLARGE; } else if(!strcmp(argv[i], "-td")){ opts |= HDBTDEFLATE; } else if(!strcmp(argv[i], "-tb")){ opts |= HDBTBZIP; } else if(!strcmp(argv[i], "-tt")){ opts |= HDBTTCBS; } else if(!strcmp(argv[i], "-tx")){ opts |= HDBTEXCODEC; } else if(!strcmp(argv[i], "-rc")){ if(++i >= argc) usage(); rcnum = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-xm")){ if(++i >= argc) usage(); xmsiz = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-nl")){ omode |= HDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= HDBOLCKNB; } else if(!strcmp(argv[i], "-pn")){ if(++i >= argc) usage(); pnum = tcatoi(argv[i]); } else if(!strcmp(argv[i], "-dai")){ dai = true; } else if(!strcmp(argv[i], "-dad")){ dad = true; } else if(!strcmp(argv[i], "-rl")){ rl = true; } else { usage(); } } else if(!path){ path = argv[i]; } else if(!rstr){ rstr = 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 = tcatoi(rstr); if(rnum < 1) usage(); int bnum = bstr ? tcatoi(bstr) : -1; int apow = astr ? tcatoi(astr) : -1; int fpow = fstr ? tcatoi(fstr) : -1; int rv = procrcat(path, rnum, bnum, apow, fpow, mt, opts, rcnum, xmsiz, omode, pnum, dai, dad, rl); return rv;}/* parse arguments of misc command */static int runmisc(int argc, char **argv){ char *path = NULL; char *rstr = NULL; bool mt = false; int opts = 0; int omode = 0; for(int i = 2; i < argc; i++){ if(!path && argv[i][0] == '-'){ if(!strcmp(argv[i], "-mt")){ mt = true; } else if(!strcmp(argv[i], "-tl")){ opts |= HDBTLARGE; } else if(!strcmp(argv[i], "-td")){ opts |= HDBTDEFLATE; } else if(!strcmp(argv[i], "-tb")){ opts |= HDBTBZIP; } else if(!strcmp(argv[i], "-tt")){ opts |= HDBTTCBS; } else if(!strcmp(argv[i], "-tx")){ opts |= HDBTEXCODEC; } else if(!strcmp(argv[i], "-nl")){ omode |= HDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= HDBOLCKNB; } else { usage(); } } else if(!path){ path = argv[i]; } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!path || !rstr) usage(); int rnum = tcatoi(rstr); if(rnum < 1) usage(); int rv = procmisc(path, rnum, mt, opts, omode); return rv;}/* parse arguments of wicked command */static int runwicked(int argc, char **argv){ char *path = NULL; char *rstr = NULL; bool mt = false; int opts = 0; int omode = 0; for(int i = 2; i < argc; i++){ if(!path && argv[i][0] == '-'){ if(!strcmp(argv[i], "-mt")){ mt = true; } else if(!strcmp(argv[i], "-tl")){ opts |= HDBTLARGE; } else if(!strcmp(argv[i], "-td")){ opts |= HDBTDEFLATE; } else if(!strcmp(argv[i], "-tb")){ opts |= HDBTBZIP; } else if(!strcmp(argv[i], "-tt")){ opts |= HDBTTCBS; } else if(!strcmp(argv[i], "-tx")){ opts |= HDBTEXCODEC; } else if(!strcmp(argv[i], "-nl")){ omode |= HDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= HDBOLCKNB; } else { usage(); } } else if(!path){ path = argv[i]; } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!path || !rstr) usage(); int rnum = tcatoi(rstr); if(rnum < 1) usage(); int rv = procwicked(path, rnum, mt, opts, omode); return rv;}/* perform write command */static int procwrite(const char *path, int rnum, int bnum, int apow, int fpow, bool mt, int opts, int rcnum, int xmsiz, int omode, bool as, bool rnd){ iprintf("<Writing Test>\n path=%s rnum=%d bnum=%d apow=%d fpow=%d mt=%d" " opts=%d rcnum=%d xmsiz=%d omode=%d as=%d rnd=%d\n\n", path, rnum, bnum, apow, fpow, mt, opts, rcnum, xmsiz, omode, as, rnd); bool err = false; double stime = tctime(); TCHDB *hdb = tchdbnew(); if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd); if(mt && !tchdbsetmutex(hdb)){ eprint(hdb, "tchdbsetmutex"); err = true; } if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(hdb, "tchdbsetcodecfunc"); err = true; } if(!tchdbtune(hdb, bnum, apow, fpow, opts)){ eprint(hdb, "tchdbtune"); err = true; } if(!tchdbsetcache(hdb, rcnum)){ eprint(hdb, "tchdbsetcache"); err = true; } if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){ eprint(hdb, "tchdbsetxmsiz"); err = true; } if(!rnd) omode |= HDBOTRUNC; if(!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | omode)){ eprint(hdb, "tchdbopen"); err = true; } for(int i = 1; i <= rnum; i++){ char buf[RECBUFSIZ]; int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i); if(as){ if(!tchdbputasync(hdb, buf, len, buf, len)){ eprint(hdb, "tchdbput"); err = true; break; } } else { if(!tchdbput(hdb, buf, len, buf, len)){ eprint(hdb, "tchdbput"); err = true; break; } } if(rnum > 250 && i % (rnum / 250) == 0){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -