⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tctmttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************************************************************* * The test cases of the table 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 <tctdb.h>#include "myconf.h"#define RECBUFSIZ      48                // buffer for recordstypedef struct {                         // type of structure for write thread  TCTDB *tdb;  int rnum;  bool rnd;  int id;} TARGWRITE;typedef struct {                         // type of structure for read thread  TCTDB *tdb;  int rnum;  bool rnd;  int id;} TARGREAD;typedef struct {                         // type of structure for remove thread  TCTDB *tdb;  int rnum;  bool rnd;  int id;} TARGREMOVE;typedef struct {                         // type of structure for wicked thread  TCTDB *tdb;  int rnum;  int id;} TARGWICKED;typedef struct {                         // type of structure for typical thread  TCTDB *tdb;  int rnum;  int rratio;  int id;} TARGTYPICAL;/* 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(TCTDB *tdb, const char *func);static void sysprint(void);static int myrand(int range);static int myrandnd(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 runwicked(int argc, char **argv);static int runtypical(int argc, char **argv);static int procwrite(const char *path, int tnum, int rnum, int bnum, int apow, int fpow,                     int opts, int rcnum, int lcnum, int ncnum, int xmsiz,                     int iflags, int omode, bool rnd);static int procread(const char *path, int tnum, int rcnum, int lcnum, int ncnum, int xmsiz,                    int omode, bool rnd);static int procremove(const char *path, int tnum, int rcnum, int lcnum, int ncnum, int xmsiz,                      int omode, bool rnd);static int procwicked(const char *path, int tnum, int rnum, int opts, int omode);static int proctypical(const char *path, int tnum, int rnum, int bnum, int apow, int fpow,                       int opts, int rcnum, int lcnum, int ncnum, int xmsiz, int omode,                       int rratio);static void *threadwrite(void *targ);static void *threadread(void *targ);static void *threadremove(void *targ);static void *threadwicked(void *targ);static void *threadtypical(void *targ);/* 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], "wicked")){    rv = runwicked(argc, argv);  } else if(!strcmp(argv[1], "typical")){    rv = runtypical(argc, argv);  } else {    usage();  }  return rv;}/* print the usage and exit */static void usage(void){  fprintf(stderr, "%s: test cases of the table database API of Tokyo Cabinet\n", g_progname);  fprintf(stderr, "\n");  fprintf(stderr, "usage:\n");  fprintf(stderr, "  %s write [-tl] [-td|-tb|-tt|-tx] [-rc num] [-lc num] [-nc num] [-xm num]"          " [-ip] [-is] [-in] [-it] [-if] [-nl|-nb] [-rnd] path tnum rnum [bnum [apow [fpow]]]\n",          g_progname);  fprintf(stderr, "  %s read [-rc num] [-lc num] [-nc num] [-xm num] [-nl|-nb] [-rnd]"          " path tnum\n", g_progname);  fprintf(stderr, "  %s remove [-rc num] [-lc num] [-nc num] [-xm num] [-nl|-nb] [-rnd]"          " path tnum\n", g_progname);  fprintf(stderr, "  %s wicked [-tl] [-td|-tb|-tt|-tx] [-nl|-nb] path tnum rnum\n", g_progname);  fprintf(stderr, "  %s typical [-tl] [-td|-tb|-tt|-tx] [-rc num] [-lc num] [-nc num] [-xm num]"          " [-nl|-nb] [-rr num] path tnum rnum [bnum [apow [fpow]]]\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 table database */static void eprint(TCTDB *tdb, const char *func){  const char *path = tctdbpath(tdb);  int ecode = tctdbecode(tdb);  fprintf(stderr, "%s: %s: %s: error: %d: %s\n",          g_progname, path ? path : "-", func, ecode, tctdberrmsg(ecode));}/* 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;}/* get a random number based on normal distribution */static int myrandnd(int range){  int num = (int)tcdrandnd(range >> 1, range / 10);  return (num < 0 || num >= range) ? 0 : num;}/* parse arguments of write command */static int runwrite(int argc, char **argv){  char *path = NULL;  char *tstr = NULL;  char *rstr = NULL;  char *bstr = NULL;  char *astr = NULL;  char *fstr = NULL;  int opts = 0;  int rcnum = 0;  int lcnum = 0;  int ncnum = 0;  int xmsiz = -1;  int iflags = 0;  int omode = 0;  bool rnd = false;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tl")){        opts |= TDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= TDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= TDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= TDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= TDBTEXCODEC;      } else if(!strcmp(argv[i], "-rc")){        if(++i >= argc) usage();        rcnum = tcatoix(argv[i]);      } 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], "-ip")){        iflags |= 1 << 0;      } else if(!strcmp(argv[i], "-is")){        iflags |= 1 << 1;      } else if(!strcmp(argv[i], "-in")){        iflags |= 1 << 2;      } else if(!strcmp(argv[i], "-it")){        iflags |= 1 << 3;      } else if(!strcmp(argv[i], "-if")){        iflags |= 1 << 4;      } else if(!strcmp(argv[i], "-nl")){        omode |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else if(!strcmp(argv[i], "-rnd")){        rnd = true;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = 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 || !tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int bnum = bstr ? tcatoix(bstr) : -1;  int apow = astr ? tcatoix(astr) : -1;  int fpow = fstr ? tcatoix(fstr) : -1;  int rv = procwrite(path, tnum, rnum, bnum, apow, fpow, opts, rcnum, lcnum, ncnum, xmsiz,                     iflags, omode, rnd);  return rv;}/* parse arguments of read command */static int runread(int argc, char **argv){  char *path = NULL;  char *tstr = NULL;  int rcnum = 0;  int lcnum = 0;  int ncnum = 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], "-rc")){        if(++i >= argc) usage();        rcnum = tcatoix(argv[i]);      } 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 |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else if(!strcmp(argv[i], "-rnd")){        rnd = true;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = argv[i];    } else {      usage();    }  }  if(!path || !tstr) usage();  int tnum = tcatoix(tstr);  if(tnum < 1) usage();  int rv = procread(path, tnum, rcnum, lcnum, ncnum, xmsiz, omode, rnd);  return rv;}/* parse arguments of remove command */static int runremove(int argc, char **argv){  char *path = NULL;  char *tstr = NULL;  int rcnum = 0;  int lcnum = 0;  int ncnum = 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], "-rc")){        if(++i >= argc) usage();        rcnum = tcatoix(argv[i]);      } 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 |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else if(!strcmp(argv[i], "-rnd")){        rnd = true;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = argv[i];    } else {      usage();    }  }  if(!path || !tstr) usage();  int tnum = tcatoix(tstr);  if(tnum < 1) usage();  int rv = procremove(path, tnum, rcnum, lcnum, ncnum, xmsiz, omode, rnd);  return rv;}/* parse arguments of wicked command */static int runwicked(int argc, char **argv){  char *path = NULL;  char *tstr = NULL;  char *rstr = NULL;  int opts = 0;  int omode = 0;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tl")){        opts |= TDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= TDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= TDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= TDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= TDBTEXCODEC;      } else if(!strcmp(argv[i], "-nl")){        omode |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = argv[i];    } else if(!rstr){      rstr = argv[i];    } else {      usage();    }  }  if(!path || !tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int rv = procwicked(path, tnum, rnum, opts, omode);  return rv;}/* parse arguments of typical command */static int runtypical(int argc, char **argv){  char *path = NULL;  char *tstr = NULL;  char *rstr = NULL;  char *bstr = NULL;  char *astr = NULL;  char *fstr = NULL;  int opts = 0;  int rcnum = 0;  int lcnum = 0;  int ncnum = 0;  int xmsiz = -1;  int omode = 0;  int rratio = -1;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tl")){        opts |= TDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= TDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= TDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= TDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= TDBTEXCODEC;      } else if(!strcmp(argv[i], "-rc")){        if(++i >= argc) usage();        rcnum = tcatoix(argv[i]);      } 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 |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else if(!strcmp(argv[i], "-rr")){        if(++i >= argc) usage();        rratio = tcatoix(argv[i]);      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = 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];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -