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

📄 tcbtest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 5 页
字号:
/* parse arguments of rcat command */static int runrcat(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;  int pnum = 0;  bool dai = false;  bool dad = false;  bool rl = false;  bool ru = 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], "-pn")){        if(++i >= argc) usage();        pnum = tcatoix(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 if(!strcmp(argv[i], "-ru")){        ru = 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 = procrcat(path, rnum, lmemb, nmemb, bnum, apow, fpow, mt, cmp, opts,                    lcnum, ncnum, xmsiz, lsmax, capnum, omode, pnum, dai, dad, rl, ru);  return rv;}/* parse arguments of queue command */static int runqueue(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;  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 {        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 = procqueue(path, rnum, lmemb, nmemb, bnum, apow, fpow,                     mt, cmp, opts, lcnum, ncnum, xmsiz, lsmax, capnum, omode);  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 |= 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], "-nl")){        omode |= BDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= BDBOLCKNB;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!rstr){      rstr = argv[i];    } else {      usage();    }  }  if(!path || !rstr) usage();  int rnum = tcatoix(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 |= 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], "-nl")){        omode |= BDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= BDBOLCKNB;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!rstr){      rstr = argv[i];    } else {      usage();    }  }  if(!path || !rstr) usage();  int rnum = tcatoix(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 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){  iprintf("<Writing Test>\n  seed=%u  path=%s  rnum=%d  lmemb=%d  nmemb=%d  bnum=%d  apow=%d"          "  fpow=%d  mt=%d  cmp=%p  opts=%d  lcnum=%d  ncnum=%d  xmsiz=%d  lsmax=%d  capnum=%d"          "  omode=%d  rnd=%d\n\n",          g_randseed, path, rnum, lmemb, nmemb, bnum, apow, fpow, mt, (void *)(intptr_t)cmp,          opts, lcnum, ncnum, xmsiz, lsmax, capnum, omode, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(mt && !tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)){    eprint(bdb, "tcbdbsetcmpfunc");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){    eprint(bdb, "tcbdbtune");    err = true;  }  if(!tcbdbsetcache(bdb, lcnum, ncnum)){    eprint(bdb, "tcbdbsetcache");    err = true;  }  if(xmsiz >= 0 && !tcbdbsetxmsiz(bdb, xmsiz)){    eprint(bdb, "tcbdbsetxmsiz");    err = true;  }  if(!tcbdbsetlsmax(bdb, lsmax)){    eprint(bdb, "tcbdbsetlsmax");    err = true;  }  if(!tcbdbsetcapnum(bdb, capnum)){    eprint(bdb, "tcbdbsetcapnum");    err = true;  }  if(!rnd) omode |= BDBOTRUNC;  if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len;    if(cmp == tccmpdecimal){      len = sprintf(buf, "%d", rnd ? myrand(rnum) + 1 : i);    } else if(cmp == tccmpint32){      int32_t lnum = rnd ? myrand(rnum) + 1 : i;      memcpy(buf, &lnum, sizeof(lnum));      len = sizeof(lnum);    } else if(cmp == tccmpint64){      int64_t llnum = rnd ? myrand(rnum) + 1 : i;      memcpy(buf, &llnum, sizeof(llnum));      len = sizeof(llnum);    } else {      len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);    }    if(!tcbdbput(bdb, buf, len, buf, len)){      eprint(bdb, "tcbdbput");      err = true;      break;    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  sysprint();  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform read command */static int procread(const char *path, bool mt, TCCMP cmp, int lcnum, int ncnum, int xmsiz,                    int omode, bool wb, bool rnd){  iprintf("<Reading Test>\n  seed=%u  path=%s  mt=%d  cmp=%p  lcnum=%d  ncnum=%d  xmsiz=%d"          "  omode=%d  wb=%d  rnd=%d\n\n",          g_randseed, path, mt, (void *)(intptr_t)cmp, lcnum, ncnum, xmsiz, omode, wb, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(mt && !tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)){    eprint(bdb, "tcbdbsetcmpfunc");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbsetcache(bdb, lcnum, ncnum)){    eprint(bdb, "tcbdbsetcache");    err = true;  }  if(xmsiz >= 0 && !tcbdbsetxmsiz(bdb, xmsiz)){    eprint(bdb, "tcbdbsetxmsiz");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOREADER | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  int rnum = tcbdbrnum(bdb);  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz;    if(cmp == tccmpdecimal){      ksiz = sprintf(kbuf, "%d", rnd ? myrand(rnum) + 1 : i);    } else if(cmp == tccmpint32){      int32_t lnum = rnd ? myrand(rnum) + 1 : i;      memcpy(kbuf, &lnum, sizeof(lnum));      ksiz = sizeof(lnum);    } else if(cmp == tccmpint64){      int64_t llnum = rnd ? myrand(rnum) + 1 : i;      memcpy(kbuf, &llnum, sizeof(llnum));      ksiz = sizeof(llnum);    } else {      ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i);    }    int vsiz;    if(wb){      int vsiz;      const char *vbuf = tcbdbget3(bdb, kbuf, ksiz, &vsiz);      if(!vbuf && !(rnd && tcbdbecode(bdb) == TCENOREC)){        eprint(bdb, "tcbdbget3");        err = true;        break;      }

⌨️ 快捷键说明

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