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

📄 tchtest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
    int tvsiz;    const char *tvbuf = tcmapiterval(tkbuf, &tvsiz);    int rsiz;    char *rbuf = tchdbget(hdb, tkbuf, tksiz, &rsiz);    if(!rbuf || rsiz != tvsiz || memcmp(rbuf, tvbuf, rsiz)){      eprint(hdb, "(validation)");      err = true;      tcfree(rbuf);      break;    }    tcfree(rbuf);    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(rnum > 250) iprintf(" (%08d)\n", inum);  inum = 0;  if(!tchdbiterinit(hdb)){    eprint(hdb, "tchdbiterinit");    err = true;  }  for(int i = 1; (kbuf = tchdbiternext(hdb, &ksiz)) != NULL; i++, inum++){    int vsiz;    char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);    int rsiz;    const char *rbuf = tcmapget(map, kbuf, ksiz, &rsiz);    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(hdb, "(validation)");      err = true;      tcfree(vbuf);      tcfree(kbuf);      break;    }    tcfree(vbuf);    tcfree(kbuf);    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(rnum > 250) iprintf(" (%08d)\n", inum);  tcmapdel(map);  if(!tchdbvanish(hdb)){    eprint(hdb, "tchdbvanish");    err = true;  }  for(int i = myrand(3) + 1; i < PATH_MAX; i = i * 2 + myrand(3)){    char vbuf[i];    memset(vbuf, '@', i - 1);    vbuf[i-1] = '\0';    if(!tchdbput2(hdb, "mikio", vbuf)){      eprint(hdb, "tchdbput2");      err = true;    }  }  if(!tchdbput2(hdb, "mikio", "nanashi")){    eprint(hdb, "tchdbput2");    err = true;  }  if(!tchdbtranbegin(hdb)){    eprint(hdb, "tchdbtranbegin");    err = true;  }  if(!tchdbput2(hdb, "mikio", "hirabayashi")){    eprint(hdb, "tchdbput2");    err = true;  }  for(int i = 0; i < 10; i++){    char buf[RECBUFSIZ];    int size = sprintf(buf, "%d", myrand(rnum));    if(!tchdbput(hdb, buf, size, buf, size)){      eprint(hdb, "tchdbput");      err = true;    }  }  for(int i = myrand(3) + 1; i < PATH_MAX; i = i * 2 + myrand(3)){    char vbuf[i];    memset(vbuf, '@', i - 1);    vbuf[i-1] = '\0';    if(!tchdbput2(hdb, "mikio", vbuf)){      eprint(hdb, "tchdbput2");      err = true;    }  }  if(!tchdbforeach(hdb, iterfunc, NULL)){    eprint(hdb, "tchdbforeach");    err = true;  }  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 wicked command */static int procwicked(const char *path, int rnum, bool mt, int opts, int omode){  iprintf("<Wicked Writing 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;  }  if(!tchdbiterinit(hdb)){    eprint(hdb, "tchdbiterinit");    err = true;  }  TCMAP *map = tcmapnew2(rnum / 5);  for(int i = 1; i <= rnum && !err; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", myrand(rnum));    char vbuf[RECBUFSIZ];    int vsiz = myrand(RECBUFSIZ);    memset(vbuf, '*', vsiz);    vbuf[vsiz] = '\0';    char *rbuf;    switch(myrand(16)){    case 0:      iputchar('0');      if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){        eprint(hdb, "tchdbput");        err = true;      }      tcmapput(map, kbuf, ksiz, vbuf, vsiz);      break;    case 1:      iputchar('1');      if(!tchdbput2(hdb, kbuf, vbuf)){        eprint(hdb, "tchdbput2");        err = true;      }      tcmapput2(map, kbuf, vbuf);      break;    case 2:      iputchar('2');      if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep");        err = true;      }      tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);      break;    case 3:      iputchar('3');      if(!tchdbputkeep2(hdb, kbuf, vbuf) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep2");        err = true;      }      tcmapputkeep2(map, kbuf, vbuf);      break;    case 4:      iputchar('4');      if(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){        eprint(hdb, "tchdbputcat");        err = true;      }      tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);      break;    case 5:      iputchar('5');      if(!tchdbputcat2(hdb, kbuf, vbuf)){        eprint(hdb, "tchdbputcat2");        err = true;      }      tcmapputcat2(map, kbuf, vbuf);      break;    case 6:      iputchar('6');      if(!tchdbputasync(hdb, kbuf, ksiz, vbuf, vsiz)){        eprint(hdb, "tchdbputasync");        err = true;      }      tcmapput(map, kbuf, ksiz, vbuf, vsiz);      break;    case 7:      iputchar('7');      if(!tchdbputasync2(hdb, kbuf, vbuf)){        eprint(hdb, "tchdbputasync2");        err = true;      }      tcmapput2(map, kbuf, vbuf);      break;    case 8:      iputchar('8');      if(myrand(10) == 0){        if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbout");          err = true;        }        tcmapout(map, kbuf, ksiz);      }      break;    case 9:      iputchar('9');      if(myrand(10) == 0){        if(!tchdbout2(hdb, kbuf) && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbout2");          err = true;        }        tcmapout2(map, kbuf);      }      break;    case 10:      iputchar('A');      if(!(rbuf = tchdbget(hdb, kbuf, ksiz, &vsiz))){        if(tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbget");          err = true;        }        rbuf = tcsprintf("[%d]", myrand(i + 1));        vsiz = strlen(rbuf);      }      vsiz += myrand(vsiz);      if(myrand(3) == 0) vsiz += PATH_MAX;      rbuf = tcrealloc(rbuf, vsiz + 1);      for(int j = 0; j < vsiz; j++){        rbuf[j] = myrand(0x100);      }      if(!tchdbput(hdb, kbuf, ksiz, rbuf, vsiz)){        eprint(hdb, "tchdbput");        err = true;      }      tcmapput(map, kbuf, ksiz, rbuf, vsiz);      tcfree(rbuf);      break;    case 11:      iputchar('B');      if(!(rbuf = tchdbget(hdb, kbuf, ksiz, &vsiz)) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget");        err = true;      }      tcfree(rbuf);      break;    case 12:      iputchar('C');      if(!(rbuf = tchdbget2(hdb, kbuf)) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget2");        err = true;      }      tcfree(rbuf);      break;    case 13:      iputchar('D');      if(myrand(1) == 0) vsiz = 1;      if((vsiz = tchdbget3(hdb, kbuf, ksiz, vbuf, vsiz)) < 0 && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget3");        err = true;      }      break;    case 14:      iputchar('E');      if(myrand(rnum / 50) == 0){        if(!tchdbiterinit(hdb)){          eprint(hdb, "tchdbiterinit");          err = true;        }      }      TCXSTR *ikey = tcxstrnew();      TCXSTR *ival = tcxstrnew();      for(int j = myrand(rnum) / 1000 + 1; j >= 0; j--){        if(j % 3 == 0){          if(tchdbiternext3(hdb, ikey, ival)){            if(tcxstrsize(ival) != tchdbvsiz(hdb, tcxstrptr(ikey), tcxstrsize(ikey))){              eprint(hdb, "(validation)");              err = true;            }          } else {            int ecode = tchdbecode(hdb);            if(ecode != TCEINVALID && ecode != TCENOREC){              eprint(hdb, "tchdbiternext3");              err = true;            }          }        } else {          int iksiz;          char *ikbuf = tchdbiternext(hdb, &iksiz);          if(ikbuf){            tcfree(ikbuf);          } else {            int ecode = tchdbecode(hdb);            if(ecode != TCEINVALID && ecode != TCENOREC){              eprint(hdb, "tchdbiternext");              err = true;            }          }        }      }      tcxstrdel(ival);      tcxstrdel(ikey);      break;    default:      iputchar('@');      if(myrand(10000) == 0) srand((unsigned int)(tctime() * 1000) % UINT_MAX);      if(myrand(rnum / 16 + 1) == 0){        int cnt = myrand(30);        for(int j = 0; j < rnum && !err; j++){          ksiz = sprintf(kbuf, "%d", i + j);          if(tchdbout(hdb, kbuf, ksiz)){            cnt--;          } else if(tchdbecode(hdb) != TCENOREC){            eprint(hdb, "tchdbout");            err = true;          }          tcmapout(map, kbuf, ksiz);          if(cnt < 0) break;        }      }      break;    }    if(i % 50 == 0) iprintf(" (%08d)\n", i);    if(i == rnum / 2){      if(!tchdbclose(hdb)){        eprint(hdb, "tchdbclose");        err = true;      }      if(!tchdbopen(hdb, path, HDBOWRITER | omode)){        eprint(hdb, "tchdbopen");        err = true;      }    } else if(i == rnum / 4){      char *npath = tcsprintf("%s-tmp", path);      if(!tchdbcopy(hdb, npath)){        eprint(hdb, "tchdbcopy");        err = true;      }      TCHDB *nhdb = tchdbnew();      if(!tchdbsetcodecfunc(nhdb, _tc_recencode, NULL, _tc_recdecode, NULL)){        eprint(nhdb, "tchdbsetcodecfunc");        err = true;      }      if(!tchdbopen(nhdb, npath, HDBOREADER | omode)){        eprint(nhdb, "tchdbopen");        err = true;      }      tchdbdel(nhdb);      unlink(npath);      tcfree(npath);      if(!tchdboptimize(hdb, rnum / 50, -1, -1, -1)){        eprint(hdb, "tchdboptimize");        err = true;      }      if(!tchdbiterinit(hdb)){        eprint(hdb, "tchdbiterinit");        err = true;      }    } else if(i == rnum / 8){      if(!tchdbtranbegin(hdb)){        eprint(hdb, "tchdbtranbegin");        err = true;      }    } else if(i == rnum / 8 + rnum / 16){      if(!tchdbtrancommit(hdb)){        eprint(hdb, "tchdbtrancommit");        err = true;      }    }  }  if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum);  if(!tchdbsync(hdb)){    eprint(hdb, "tchdbsync");    err = true;  }  if(tchdbrnum(hdb) != tcmaprnum(map)){    eprint(hdb, "(validation)");    err = true;  }  for(int i = 1; i <= rnum && !err; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", i - 1);    int vsiz;    const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz);    int rsiz;    char *rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz);    if(vbuf){      iputchar('.');      if(!rbuf){        eprint(hdb, "tchdbget");        err = true;      } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){        eprint(hdb, "(validation)");        err = true;      }    } else {      iputchar('*');      if(rbuf || tchdbecode(hdb) != TCENOREC){        eprint(hdb, "(validation)");        err = true;      }    }    tcfree(rbuf);    if(i % 50 == 0) iprintf(" (%08d)\n", i);  }  if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum);  int inum = 0;  char *cbuf;  int csiz;  if(myrand(2) == 0){    cbuf = tchdbgetnext(hdb, NULL, -1, &csiz);  } else {    const char *cvbuf;    int cvsiz;    cbuf = tchdbgetnext3(hdb, NULL, -1, &csiz, &cvbuf, &cvsiz);  }  while(cbuf){    inum++;    iputchar(':');    int nsiz;    char *nbuf;    if(myrand(2) == 0){      nbuf = tchdbgetnext(hdb, cbuf, csiz, &nsiz);    } else {      const char *cvbuf;      int cvsiz;      nbuf = tchdbgetnext3(hdb, cbuf, csiz, &nsiz, &cvbuf, &cvsiz);    }    tcfree(cbuf);    cbuf = nbuf;    csiz = nsiz;    if(inum % 50 == 0) iprintf(" (%08d)\n", inum);  }  tcfree(cbuf);  if(inum % 50 > 0) iprintf(" (%08d)\n", inum);  if(inum != tchdbrnum(hdb)){    eprint(hdb, "(validation)");    err = true;  }  tcmapiterinit(map);  int ksiz;  const char *kbuf;  for(int i = 1; (kbuf = tcmapiternext(map, &ksiz)) != NULL; i++){    iputchar('+');    int vsiz;    const char *vbuf = tcmapiterval(kbuf, &vsiz);    int rsiz;    char *rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz);    if(!rbuf){      eprint(hdb, "tchdbget");      err = true;    } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(hdb, "(validation)");      err = true;    }    tcfree(rbuf);    if(!tchdbout(hdb, kbuf, ksiz)){      eprint(hdb, "tchdbout");      err = true;    }    if(i % 50 == 0) iprintf(" (%08d)\n", i);  }  int mrnum = tcmaprnum(map);  if(mrnum % 50 > 0) iprintf(" (%08d)\n", mrnum);  if(tchdbrnum(hdb) != 0){    eprint(hdb, "(validation)");    err = true;  }  iprintf("record number: %llu\n", (unsigned long long)tchdbrnum(hdb));  iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb));  mprint(hdb);  sysprint();  tcmapdel(map);  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;}// END OF FILE

⌨️ 快捷键说明

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