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

📄 tcftest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
        mt = true;      } else if(!strcmp(argv[i], "-nl")){        omode |= FDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= FDBOLCKNB;      } 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, omode);  return rv;}/* perform write command */static int procwrite(const char *path, int rnum, int width, int64_t limsiz,                     bool mt, int omode, bool rnd){  iprintf("<Writing Test>\n  seed=%u  path=%s  rnum=%d  width=%d  limsiz=%lld  mt=%d  omode=%d"          "  rnd=%d\n\n", g_randseed, path, rnum, width, (long long)limsiz, mt, omode, rnd);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(mt && !tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbtune(fdb, width, limsiz)){    eprint(fdb, "tcfdbtune");    err = true;  }  if(!rnd) omode |= FDBOTRUNC;  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);    if(!tcfdbput2(fdb, buf, len, buf, len)){      eprint(fdb, "tcfdbput");      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)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  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 omode, bool wb, bool rnd){  iprintf("<Reading Test>\n  seed=%u  path=%s  mt=%d  omode=%d  wb=%d  rnd=%d\n\n",          g_randseed, path, mt, omode, wb, rnd);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(mt && !tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOREADER | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  int rnum = tcfdbrnum(fdb);  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 = tcfdbget4(fdb, i, vbuf, RECBUFSIZ);      if(vsiz < 0 && !(rnd && tcfdbecode(fdb) == TCENOREC)){        eprint(fdb, "tcfdbget4");        err = true;        break;      }    } else {      char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);      if(!vbuf && !(rnd && tcfdbecode(fdb) == TCENOREC)){        eprint(fdb, "tcfdbget");        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)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  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 omode, bool rnd){  iprintf("<Removing Test>\n  seed=%u  path=%s  mt=%d  omode=%d  rnd=%d\n\n",          g_randseed, path, mt, omode, rnd);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(mt && !tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  int rnum = tcfdbrnum(fdb);  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i);    if(!tcfdbout2(fdb, kbuf, ksiz) && !(rnd && tcfdbecode(fdb) == TCENOREC)){      eprint(fdb, "tcfdbout");      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)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  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 width, int64_t limsiz,                    bool mt, int omode, int pnum, bool dai, bool dad, bool rl, bool ru){  iprintf("<Random Concatenating Test>\n"          "  seed=%u  path=%s  rnum=%d  width=%d  limsiz=%lld  mt=%d  omode=%d  pnum=%d"          "  dai=%d  dad=%d  rl=%d  ru=%d\n\n",          g_randseed, path, rnum, width, (long long)limsiz, mt, omode, pnum, dai, dad, rl, ru);  if(pnum < 1) pnum = rnum;  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(mt && !tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbtune(fdb, width, limsiz)){    eprint(fdb, "tcfdbtune");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", myrand(pnum) + 1);    if(dai){      if(tcfdbaddint(fdb, tcfdbkeytoid(kbuf, ksiz), myrand(3)) == INT_MIN){        eprint(fdb, "tcfdbaddint");        err = true;        break;      }    } else if(dad){      if(isnan(tcfdbadddouble(fdb, tcfdbkeytoid(kbuf, ksiz), myrand(30) / 10.0))){        eprint(fdb, "tcfdbadddouble");        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(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){        eprint(fdb, "tcfdbputcat");        err = true;        break;      }    } else if(ru){      int id = myrand(pnum) + 1;      switch(myrand(8)){      case 0:        if(!tcfdbput(fdb, id, kbuf, ksiz)){          eprint(fdb, "tcfdbput");          err = true;        }        break;      case 1:        if(!tcfdbputkeep(fdb, id, kbuf, ksiz) && tcfdbecode(fdb) != TCEKEEP){          eprint(fdb, "tcfdbputkeep");          err = true;        }        break;      case 2:        if(!tcfdbout(fdb, id) && tcfdbecode(fdb) != TCENOREC){          eprint(fdb, "tcfdbout");          err = true;        }        break;      case 3:        if(tcfdbaddint(fdb, id, 1) == INT_MIN && tcfdbecode(fdb) != TCEKEEP){          eprint(fdb, "tcfdbaddint");          err = true;        }        break;      case 4:        if(isnan(tcfdbadddouble(fdb, id, 1.0)) && tcfdbecode(fdb) != TCEKEEP){          eprint(fdb, "tcfdbadddouble");          err = true;        }        break;      case 5:        if(myrand(2) == 0){          if(!tcfdbputproc(fdb, id, kbuf, ksiz, pdprocfunc, NULL) &&             tcfdbecode(fdb) != TCEKEEP){            eprint(fdb, "tcfdbputproc");            err = true;          }        } else {          if(!tcfdbputproc(fdb, id, NULL, 0, pdprocfunc, NULL) &&             tcfdbecode(fdb) != TCEKEEP && tcfdbecode(fdb) != TCENOREC){            eprint(fdb, "tcfdbputproc");            err = true;          }        }        break;      default:        if(!tcfdbputcat(fdb, id, kbuf, ksiz)){          eprint(fdb, "tcfdbputcat");          err = true;        }        break;      }      if(err) break;    } else {      if(!tcfdbputcat2(fdb, kbuf, ksiz, kbuf, ksiz)){        eprint(fdb, "tcfdbputcat");        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)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  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 omode){  iprintf("<Miscellaneous Test>\n  seed=%u  path=%s  rnum=%d  mt=%d  omode=%d\n\n",          g_randseed, path, rnum, mt, omode);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(mt && !tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbtune(fdb, RECBUFSIZ, EXHEADSIZ + (RECBUFSIZ + sizeof(int)) * rnum)){    eprint(fdb, "tcfdbtune");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  iprintf("writing:\n");  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", i);    if(!tcfdbputkeep2(fdb, buf, len, buf, len)){      eprint(fdb, "tcfdbputkeep");      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 = tcfdbget2(fdb, kbuf, ksiz, &vsiz);    if(!vbuf){      eprint(fdb, "tcfdbget");      err = true;      break;    } else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){      eprint(fdb, "(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(tcfdbrnum(fdb) != rnum){    eprint(fdb, "(validation)");    err = true;  }  iprintf("random writing:\n");  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);    char vbuf[RECBUFSIZ];    int vsiz = myrand(RECBUFSIZ);    memset(vbuf, '*', vsiz);    if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){      eprint(fdb, "tcfdbput");      err = true;      break;    }    int rsiz;    char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);    if(!rbuf){      eprint(fdb, "tcfdbget");      err = true;      break;    }    if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(fdb, "(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("random erasing:\n");  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);    if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){      eprint(fdb, "tcfdbout");      err = true;      break;    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  iprintf("writing:\n");  for(int i = 1; i <= rnum; i++){

⌨️ 快捷键说明

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