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

📄 tcfmttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 3 页
字号:
  int id = ((TARGWRITE *)targ)->id;  bool err = false;  int base = id * rnum;  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", base + (rnd ? myrand(i) + 1 : i));    if(!tcfdbput2(fdb, buf, len, buf, len)){      eprint(fdb, "tcfdbput2");      err = true;      break;    }    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  return err ? "error" : NULL;}/* thread the read function */static void *threadread(void *targ){  TCFDB *fdb = ((TARGREAD *)targ)->fdb;  int rnum = ((TARGREAD *)targ)->rnum;  bool wb = ((TARGREAD *)targ)->wb;  bool rnd = ((TARGREAD *)targ)->rnd;  int id = ((TARGREAD *)targ)->id;  bool err = false;  int base = id * rnum;  for(int i = 1; i <= rnum && !err; i++){    uint64_t kid = base + (rnd ? myrandnd(i) + 1 : i);    int vsiz;    if(wb){      char vbuf[RECBUFSIZ];      int vsiz = tcfdbget4(fdb, kid, vbuf, RECBUFSIZ);      if(vsiz < 0 && (!rnd || tcfdbecode(fdb) != TCENOREC)){        eprint(fdb, "tcfdbget4");        err = true;      }    } else {      char *vbuf = tcfdbget(fdb, kid, &vsiz);      if(!vbuf && (!rnd || tcfdbecode(fdb) != TCENOREC)){        eprint(fdb, "tcfdbget");        err = true;      }      tcfree(vbuf);    }    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  return err ? "error" : NULL;}/* thread the remove function */static void *threadremove(void *targ){  TCFDB *fdb = ((TARGREMOVE *)targ)->fdb;  int rnum = ((TARGREMOVE *)targ)->rnum;  bool rnd = ((TARGREMOVE *)targ)->rnd;  int id = ((TARGREMOVE *)targ)->id;  bool err = false;  int base = id * rnum;  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrand(i + 1) + 1 : i));    if(!tcfdbout2(fdb, kbuf, ksiz) && (!rnd || tcfdbecode(fdb) != TCENOREC)){      eprint(fdb, "tcfdbout2");      err = true;      break;    }    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  return err ? "error" : NULL;}/* thread the wicked function */static void *threadwicked(void *targ){  TCFDB *fdb = ((TARGWICKED *)targ)->fdb;  int rnum = ((TARGWICKED *)targ)->rnum;  bool nc = ((TARGWICKED *)targ)->nc;  int id = ((TARGWICKED *)targ)->id;  TCMAP *map = ((TARGWICKED *)targ)->map;  bool err = false;  for(int i = 1; i <= rnum && !err; i++){    uint64_t kid = myrand(rnum * (id + 1)) + 1;    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%llu", (unsigned long long)kid);    char vbuf[RECBUFSIZ];    int vsiz = myrand(RECBUFSIZ);    memset(vbuf, '*', vsiz);    vbuf[vsiz] = '\0';    char *rbuf;    if(!nc) tcglobalmutexlock();    switch(myrand(16)){    case 0:      if(id == 0) iputchar('0');      if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){        eprint(fdb, "tcfdbput2");        err = true;      }      if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);      break;    case 1:      if(id == 0) iputchar('1');      if(!tcfdbput3(fdb, kbuf, vbuf)){        eprint(fdb, "tcfdbput3");        err = true;      }      if(!nc) tcmapput2(map, kbuf, vbuf);      break;    case 2:      if(id == 0) iputchar('2');      if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){        eprint(fdb, "tcfdbputkeep2");        err = true;      }      if(!nc) tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);      break;    case 3:      if(id == 0) iputchar('3');      if(!tcfdbputkeep3(fdb, kbuf, vbuf) && tcfdbecode(fdb) != TCEKEEP){        eprint(fdb, "tcfdbputkeep3");        err = true;      }      if(!nc) tcmapputkeep2(map, kbuf, vbuf);      break;    case 4:      if(id == 0) iputchar('4');      if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){        eprint(fdb, "tcfdbputcat2");        err = true;      }      if(!nc) tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);      break;    case 5:      if(id == 0) iputchar('5');      if(!tcfdbputcat3(fdb, kbuf, vbuf)){        eprint(fdb, "tcfdbputcat3");        err = true;      }      if(!nc) tcmapputcat2(map, kbuf, vbuf);      break;    case 6:      if(id == 0) iputchar('6');      if(myrand(2) == 0){        if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){          eprint(fdb, "tcfdbout2");          err = true;        }        if(!nc) tcmapout(map, kbuf, ksiz);      }      break;    case 7:      if(id == 0) iputchar('7');      if(myrand(2) == 0){        if(!tcfdbout3(fdb, kbuf) && tcfdbecode(fdb) != TCENOREC){          eprint(fdb, "tcfdbout3");          err = true;        }        if(!nc) tcmapout2(map, kbuf);      }      break;    case 8:      if(id == 0) iputchar('8');      if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz))){        if(tcfdbecode(fdb) != TCENOREC){          eprint(fdb, "tcfdbget2");          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(!tcfdbput2(fdb, kbuf, ksiz, rbuf, vsiz)){        eprint(fdb, "tcfdbput2");        err = true;      }      if(!nc) tcmapput(map, kbuf, ksiz, rbuf, vsiz);      tcfree(rbuf);      break;    case 9:      if(id == 0) iputchar('9');      if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz)) && tcfdbecode(fdb) != TCENOREC){        eprint(fdb, "tcfdbget2");        err = true;      }      tcfree(rbuf);      break;    case 10:      if(id == 0) iputchar('A');      if(!(rbuf = tcfdbget3(fdb, kbuf)) && tcfdbecode(fdb) != TCENOREC){        eprint(fdb, "tcfdbge3");        err = true;      }      tcfree(rbuf);      break;    case 11:      if(id == 0) iputchar('B');      if(myrand(1) == 0) vsiz = 1;      if((vsiz = tcfdbget4(fdb, kid, vbuf, vsiz)) < 0 && tcfdbecode(fdb) != TCENOREC){        eprint(fdb, "tcfdbget4");        err = true;      }      break;    case 14:      if(id == 0) iputchar('E');      if(myrand(rnum / 50) == 0){        if(!tcfdbiterinit(fdb)){          eprint(fdb, "tcfdbiterinit");          err = true;        }      }      for(int j = myrand(rnum) / 1000 + 1; j >= 0; j--){        if(tcfdbiternext(fdb) < 1){          int ecode = tcfdbecode(fdb);          if(ecode != TCEINVALID && ecode != TCENOREC){            eprint(fdb, "tcfdbiternext");            err = true;          }        }      }      break;    default:      if(id == 0) iputchar('@');      if(tcfdbtranbegin(fdb)){        if(myrand(2) == 0){          if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){            eprint(fdb, "tcfdbput");            err = true;          }          if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);        } else {          if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){            eprint(fdb, "tcfdbout");            err = true;          }          if(!nc) tcmapout(map, kbuf, ksiz);        }        if(nc && myrand(2) == 0){          if(!tcfdbtranabort(fdb)){            eprint(fdb, "tcfdbtranabort");            err = true;          }        } else {          if(!tcfdbtrancommit(fdb)){            eprint(fdb, "tcfdbtrancommit");            err = true;          }        }      } else {        eprint(fdb, "tcfdbtranbegin");        err = true;      }      if(myrand(1000) == 0){        if(!tcfdbforeach(fdb, iterfunc, NULL)){          eprint(fdb, "tcfdbforeach");          err = true;        }      }      if(myrand(10000) == 0) srand((unsigned int)(tctime() * 1000) % UINT_MAX);      break;    }    if(!nc) tcglobalmutexunlock();    if(id == 0){      if(i % 50 == 0) iprintf(" (%08d)\n", i);      if(id == 0 && i == rnum / 4){        if(!tcfdboptimize(fdb, RECBUFSIZ, -1) && tcfdbecode(fdb) != TCEINVALID){          eprint(fdb, "tcfdboptimize");          err = true;        }        if(!tcfdbiterinit(fdb)){          eprint(fdb, "tcfdbiterinit");          err = true;        }      }    }  }  return err ? "error" : NULL;}/* thread the typical function */static void *threadtypical(void *targ){  TCFDB *fdb = ((TARGTYPICAL *)targ)->fdb;  int rnum = ((TARGTYPICAL *)targ)->rnum;  bool nc = ((TARGTYPICAL *)targ)->nc;  int rratio = ((TARGTYPICAL *)targ)->rratio;  int id = ((TARGTYPICAL *)targ)->id;  bool err = false;  TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL;  int base = id * rnum;  int mrange = tclmax(50 + rratio, 100);  int width = tcfdbwidth(fdb);  for(int i = 1; !err && i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", base + myrandnd(i) + 1);    int rnd = myrand(mrange);    if(rnd < 10){      if(!tcfdbput2(fdb, buf, len, buf, len)){        eprint(fdb, "tcfdbput2");        err = true;      }      if(map) tcmapput(map, buf, len, buf, len);    } else if(rnd < 15){      if(!tcfdbputkeep2(fdb, buf, len, buf, len) && tcfdbecode(fdb) != TCEKEEP){        eprint(fdb, "tcfdbputkeep2");        err = true;      }      if(map) tcmapputkeep(map, buf, len, buf, len);    } else if(rnd < 20){      if(!tcfdbputcat2(fdb, buf, len, buf, len)){        eprint(fdb, "tcfdbputcat2");        err = true;      }      if(map) tcmapputcat(map, buf, len, buf, len);    } else if(rnd < 25){      if(!tcfdbout2(fdb, buf, len) && tcfdbecode(fdb) && tcfdbecode(fdb) != TCENOREC){        eprint(fdb, "tcfdbout");        err = true;      }      if(map) tcmapout(map, buf, len);    } else if(rnd < 26){      if(myrand(10) == 0 && !tcfdbiterinit(fdb) && tcfdbecode(fdb) != TCENOREC){        eprint(fdb, "tcfdbiterinit");        err = true;      }      for(int j = 0; !err && j < 10; j++){        if(tcfdbiternext(fdb) < 1 &&           tcfdbecode(fdb) != TCEINVALID && tcfdbecode(fdb) != TCENOREC){          eprint(fdb, "tcfdbiternext");          err = true;        }      }    } else {      int vsiz;      char *vbuf = tcfdbget2(fdb, buf, len, &vsiz);      if(vbuf){        if(map){          int msiz = 0;          const char *mbuf = tcmapget(map, buf, len, &msiz);          if(msiz > width) msiz = width;          if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){            eprint(fdb, "(validation)");            err = true;          }        }        tcfree(vbuf);      } else {        if(tcfdbecode(fdb) != TCENOREC){          eprint(fdb, "tcfdbget");          err = true;        }        if(map && tcmapget(map, buf, len, &vsiz)){          eprint(fdb, "(validation)");          err = true;        }      }    }    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(map){    tcmapiterinit(map);    int ksiz;    const char *kbuf;    while(!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL){      int vsiz;      char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);      if(vbuf){        int msiz = 0;        const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz);        if(msiz > width) msiz = width;        if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){          eprint(fdb, "(validation)");          err = true;        }        tcfree(vbuf);      } else {        eprint(fdb, "(validation)");        err = true;      }    }    tcmapdel(map);  }  return err ? "error" : NULL;}// END OF FILE

⌨️ 快捷键说明

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