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

📄 tchtest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
      } 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 = 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 bnum, int apow, int fpow,                     bool mt, int opts, int rcnum, int xmsiz, int omode, bool as, bool rnd){  iprintf("<Writing Test>\n  seed=%u  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",          g_randseed, 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){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  iprintf("record number: %llu\n", (unsigned long long)tchdbrnum(hdb));  iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb));  mprint(hdb);  sysprint();  if(!tchdbclose(hdb)){    eprint(hdb, "tchdbclose");    err = true;  }  tchdbdel(hdb);  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, int rcnum, int xmsiz, int omode,                    bool wb, bool rnd){  iprintf("<Reading Test>\n  seed=%u  path=%s  mt=%d  rcnum=%d  xmsiz=%d  omode=%d"          "  wb=%d  rnd=%d\n\n", g_randseed, path, mt, rcnum, xmsiz, omode, wb, 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(!tchdbsetcache(hdb, rcnum)){    eprint(hdb, "tchdbsetcache");    err = true;  }  if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){    eprint(hdb, "tchdbsetxmsiz");    err = true;  }  if(!tchdbopen(hdb, path, HDBOREADER | omode)){    eprint(hdb, "tchdbopen");    err = true;  }  int rnum = tchdbrnum(hdb);  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i);    int vsiz;    if(wb){      char vbuf[RECBUFSIZ];      int vsiz = tchdbget3(hdb, kbuf, ksiz, vbuf, RECBUFSIZ);      if(vsiz < 0 && !(rnd && tchdbecode(hdb) == TCENOREC)){        eprint(hdb, "tchdbget3");        err = true;        break;      }    } else {      char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);      if(!vbuf && !(rnd && tchdbecode(hdb) == TCENOREC)){        eprint(hdb, "tchdbget");        err = true;        break;      }      tcfree(vbuf);    }    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)tchdbrnum(hdb));  iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb));  mprint(hdb);  sysprint();  if(!tchdbclose(hdb)){    eprint(hdb, "tchdbclose");    err = true;  }  tchdbdel(hdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform remove command */static int procremove(const char *path, bool mt, int rcnum, int xmsiz, int omode, bool rnd){  iprintf("<Removing Test>\n  seed=%u  path=%s  mt=%d  rcnum=%d  xmsiz=%d  omode=%d  rnd=%d\n\n",          g_randseed, path, mt, rcnum, xmsiz, omode, 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(!tchdbsetcache(hdb, rcnum)){    eprint(hdb, "tchdbsetcache");    err = true;  }  if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){    eprint(hdb, "tchdbsetxmsiz");    err = true;  }  if(!tchdbopen(hdb, path, HDBOWRITER | omode)){    eprint(hdb, "tchdbopen");    err = true;  }  int rnum = tchdbrnum(hdb);  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i);    if(!tchdbout(hdb, kbuf, ksiz) && !(rnd && tchdbecode(hdb) == TCENOREC)){      eprint(hdb, "tchdbout");      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)tchdbrnum(hdb));  iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb));  mprint(hdb);  sysprint();  if(!tchdbclose(hdb)){    eprint(hdb, "tchdbclose");    err = true;  }  tchdbdel(hdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform rcat command */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, bool ru){  iprintf("<Random Concatenating Test>\n"          "  seed=%u  path=%s  rnum=%d  bnum=%d  apow=%d  fpow=%d  mt=%d  opts=%d"          "  rcnum=%d  xmsiz=%d  omode=%d  pnum=%d  dai=%d  dad=%d  rl=%d  ru=%d\n\n",          g_randseed, path, rnum, bnum, apow, fpow, mt, opts, rcnum, xmsiz, omode, pnum,          dai, dad, rl, ru);  if(pnum < 1) pnum = rnum;  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(!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | HDBOTRUNC | omode)){    eprint(hdb, "tchdbopen");    err = true;  }  for(int i = 1; i <= rnum; i++){    if(ru){      char fmt[RECBUFSIZ];      sprintf(fmt, "%%0%dd", myrand(RECBUFSIZ));      char kbuf[RECBUFSIZ];      int ksiz = sprintf(kbuf, fmt, myrand(pnum));      switch(myrand(8)){      case 0:        if(!tchdbput(hdb, kbuf, ksiz, kbuf, ksiz)){          eprint(hdb, "tchdbput");          err = true;        }        break;      case 1:        if(!tchdbputkeep(hdb, kbuf, ksiz, kbuf, ksiz) && tchdbecode(hdb) != TCEKEEP){          eprint(hdb, "tchdbputkeep");          err = true;        }        break;      case 2:        if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbout");          err = true;        }        break;      case 3:        if(tchdbaddint(hdb, kbuf, ksiz, 1) == INT_MIN && tchdbecode(hdb) != TCEKEEP){          eprint(hdb, "tchdbaddint");          err = true;        }        break;      case 4:        if(isnan(tchdbadddouble(hdb, kbuf, ksiz, 1.0)) && tchdbecode(hdb) != TCEKEEP){          eprint(hdb, "tchdbadddouble");          err = true;        }        break;      case 5:        if(myrand(2) == 0){          if(!tchdbputproc(hdb, kbuf, ksiz, kbuf, ksiz, pdprocfunc, NULL) &&             tchdbecode(hdb) != TCEKEEP){            eprint(hdb, "tchdbputproc");            err = true;          }        } else {          if(!tchdbputproc(hdb, kbuf, ksiz, NULL, 0, pdprocfunc, NULL) &&             tchdbecode(hdb) != TCEKEEP && tchdbecode(hdb) != TCENOREC){            eprint(hdb, "tchdbputproc");            err = true;          }        }        break;      default:        if(!tchdbputcat(hdb, kbuf, ksiz, kbuf, ksiz)){          eprint(hdb, "tchdbputcat");          err = true;        }        break;      }      if(err) break;    } else {      char kbuf[RECBUFSIZ];      int ksiz = sprintf(kbuf, "%d", myrand(pnum));      if(dai){        if(tchdbaddint(hdb, kbuf, ksiz, myrand(3)) == INT_MIN){          eprint(hdb, "tchdbaddint");          err = true;          break;        }      } else if(dad){        if(isnan(tchdbadddouble(hdb, kbuf, ksiz, myrand(30) / 10.0))){          eprint(hdb, "tchdbadddouble");          err = true;          break;        }      } else if(rl){        char vbuf[PATH_MAX];        int vsiz = myrand(PATH_MAX);        for(int j = 0; j < vsiz; j++){          vbuf[j] = myrand(0x100);        }        if(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){          eprint(hdb, "tchdbputcat");          err = true;          break;        }      } else {        if(!tchdbputcat(hdb, kbuf, ksiz, kbuf, ksiz)){          eprint(hdb, "tchdbputcat");          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)tchdbrnum(hdb));  iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb));  mprint(hdb);  sysprint();  if(!tchdbclose(hdb)){    eprint(hdb, "tchdbclose");    err = true;  }  tchdbdel(hdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform misc command */static int procmisc(const char *path, int rnum, bool mt, int opts, int omode){  iprintf("<Miscellaneous Test>\n  seed=%u  path=%s  rnum=%d  mt=%d  opts=%d  omode=%d\n\n",          g_randseed, path, rnum, mt, opts, omode);  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, rnum / 50, 2, -1, opts)){    eprint(hdb, "tchdbtune");    err = true;  }  if(!tchdbsetcache(hdb, rnum / 10)){    eprint(hdb, "tchdbsetcache");    err = true;  }  if(!tchdbsetxmsiz(hdb, rnum * sizeof(int))){    eprint(hdb, "tchdbsetxmsiz");    err = true;  }  if(!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | HDBOTRUNC | omode)){    eprint(hdb, "tchdbopen");    err = true;  }  iprintf("writing:\n");  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", i);    if(i % 3 == 0){      if(!tchdbputkeep(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputkeep");        err = true;        break;      }    } else {      if(!tchdbputasync(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputasync");        err = true;        break;      }    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  iprintf("reading:\n");  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", i);    int vsiz;    char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);    if(!vbuf){      eprint(hdb, "tchdbget");      err = true;      break;    } else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){      eprint(hdb, "(validation)");      err = true;      tcfree(vbuf);      break;    }    tcfree(vbuf);    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(tchdbrnum(hdb) != rnum){    eprint(hdb, "(validation)");    err = true;  }  iprintf("random writing:\n");  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", myrand(rnum));    char vbuf[RECBUFSIZ];    int vsiz = myrand(RECBUFSIZ);    memset(vbuf, '*', vsiz);    if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){      eprint(hdb, "tchdbput");      err = true;      break;    }    int rsiz;    char *rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz);    if(!rbuf){      eprint(hdb, "tchdbget");      err = true;      break;    }    if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(hdb, "(validation)");      err = true;      tcfree(rbuf);      break;    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }    tcfree(rbuf);  }  iprintf("word writing:\n");  const char *words[] = {    "a", "A", "bb", "BB", "ccc", "CCC", "dddd", "DDDD", "eeeee", "EEEEEE",    "mikio", "hirabayashi", "tokyo", "cabinet", "hyper", "estraier", "19780211", "birth day",    "one", "first", "two", "second", "three", "third", "four", "fourth", "five", "fifth",    "_[1]_", "uno", "_[2]_", "dos", "_[3]_", "tres", "_[4]_", "cuatro", "_[5]_", "cinco",    "[\xe5\xb9\xb3\xe6\x9e\x97\xe5\xb9\xb9\xe9\x9b\x84]", "[\xe9\xa6\xac\xe9\xb9\xbf]", NULL  };  for(int i = 0; words[i] != NULL; i += 2){    const char *kbuf = words[i];    int ksiz = strlen(kbuf);    const char *vbuf = words[i+1];    int vsiz = strlen(vbuf);    if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz)){      eprint(hdb, "tchdbputkeep");

⌨️ 快捷键说明

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