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

📄 tchmttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
      if(id == 0) iputchar('6');      if(i > rnum / 4 * 3){        if(!tchdbputasync(hdb, kbuf, ksiz, vbuf, vsiz)){          eprint(hdb, "tchdbputasync");          err = true;        }      } else {        if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){          eprint(hdb, "tchdbput");          err = true;        }      }      if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);      break;    case 7:      if(id == 0) iputchar('7');      if(i > rnum / 4 * 3){        if(!tchdbputasync2(hdb, kbuf, vbuf)){          eprint(hdb, "tchdbputasync2");          err = true;        }      } else {        if(!tchdbput2(hdb, kbuf, vbuf)){          eprint(hdb, "tchdbput2");          err = true;        }      }      if(!nc) tcmapput2(map, kbuf, vbuf);      break;    case 8:      if(id == 0) iputchar('8');      if(myrand(2) == 0){        if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbout");          err = true;        }        if(!nc) tcmapout(map, kbuf, ksiz);      }      break;    case 9:      if(id == 0) iputchar('9');      if(myrand(2) == 0){        if(!tchdbout2(hdb, kbuf) && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbout2");          err = true;        }        if(!nc) tcmapout2(map, kbuf);      }      break;    case 10:      if(id == 0) 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;      }      if(!nc) tcmapput(map, kbuf, ksiz, rbuf, vsiz);      tcfree(rbuf);      break;    case 11:      if(id == 0) iputchar('B');      if(!(rbuf = tchdbget(hdb, kbuf, ksiz, &vsiz)) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget");        err = true;      }      tcfree(rbuf);      break;    case 12:      if(id == 0) iputchar('C');      if(!(rbuf = tchdbget2(hdb, kbuf)) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget2");        err = true;      }      tcfree(rbuf);      break;    case 13:      if(id == 0) 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:      if(id == 0) 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)){            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:      if(id == 0) iputchar('@');      if(tchdbtranbegin(hdb)){        if(myrand(2) == 0){          if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){            eprint(hdb, "tchdbput");            err = true;          }          if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);        } else {          if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){            eprint(hdb, "tchdbout");            err = true;          }          if(!nc) tcmapout(map, kbuf, ksiz);        }        if(nc && myrand(2) == 0){          if(!tchdbtranabort(hdb)){            eprint(hdb, "tchdbtranabort");            err = true;          }        } else {          if(!tchdbtrancommit(hdb)){            eprint(hdb, "tchdbtrancommit");            err = true;          }        }      } else {        eprint(hdb, "tchdbtranbegin");        err = true;      }      if(myrand(1000) == 0){        if(!tchdbforeach(hdb, iterfunc, NULL)){          eprint(hdb, "tchdbforeach");          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(!tchdboptimize(hdb, rnum / 50, -1, -1, -1) && tchdbecode(hdb) != TCEINVALID){          eprint(hdb, "tchdboptimize");          err = true;        }        if(!tchdbiterinit(hdb)){          eprint(hdb, "tchdbiterinit");          err = true;        }      }    }  }  return err ? "error" : NULL;}/* thread the typical function */static void *threadtypical(void *targ){  TCHDB *hdb = ((TARGTYPICAL *)targ)->hdb;  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);  for(int i = 1; !err && i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", base + myrandnd(i));    int rnd = myrand(mrange);    if(rnd < 10){      if(!tchdbput(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbput");        err = true;      }      if(map) tcmapput(map, buf, len, buf, len);    } else if(rnd < 15){      if(!tchdbputkeep(hdb, buf, len, buf, len) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep");        err = true;      }      if(map) tcmapputkeep(map, buf, len, buf, len);    } else if(rnd < 20){      if(!tchdbputcat(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputcat");        err = true;      }      if(map) tcmapputcat(map, buf, len, buf, len);    } else if(rnd < 25){      if(i > rnum / 10 * 9){        if(!tchdbputasync(hdb, buf, len, buf, len)){          eprint(hdb, "tchdbputasync");          err = true;        }      } else {        if(!tchdbput(hdb, buf, len, buf, len)){          eprint(hdb, "tchdbput");          err = true;        }      }      if(map) tcmapput(map, buf, len, buf, len);    } else if(rnd < 30){      if(!tchdbout(hdb, buf, len) && tchdbecode(hdb) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbout");        err = true;      }      if(map) tcmapout(map, buf, len);    } else if(rnd < 31){      if(myrand(10) == 0 && !tchdbiterinit(hdb) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbiterinit");        err = true;      }      for(int j = 0; !err && j < 10; j++){        int ksiz;        char *kbuf = tchdbiternext(hdb, &ksiz);        if(kbuf){          tcfree(kbuf);        } else if(tchdbecode(hdb) != TCEINVALID && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbiternext");          err = true;        }      }    } else {      int vsiz;      char *vbuf = tchdbget(hdb, buf, len, &vsiz);      if(vbuf){        if(map){          int msiz;          const char *mbuf = tcmapget(map, buf, len, &msiz);          if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){            eprint(hdb, "(validation)");            err = true;          }        }        tcfree(vbuf);      } else {        if(tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbget");          err = true;        }        if(map && tcmapget(map, buf, len, &vsiz)){          eprint(hdb, "(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 = tchdbget(hdb, kbuf, ksiz, &vsiz);      if(vbuf){        int msiz;        const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz);        if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){          eprint(hdb, "(validation)");          err = true;        }        tcfree(vbuf);      } else {        eprint(hdb, "(validation)");        err = true;      }    }    tcmapdel(map);  }  return err ? "error" : NULL;}/* thread the race function */static void *threadrace(void *targ){  TCHDB *hdb = ((TARGRACE *)targ)->hdb;  int rnum = ((TARGRACE *)targ)->rnum;  int id = ((TARGRACE *)targ)->id;  bool err = false;  int mid = rnum * 2;  for(int i = 1; !err && i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%d", myrandnd(i));    int rnd = myrand(100);    if(rnd < 10){      if(!tchdbputkeep(hdb, buf, len, buf, len) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep");        err = true;      }    } else if(rnd < 20){      if(!tchdbputcat(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputcat");        err = true;      }    } else if(rnd < 30){      if(!tchdbout(hdb, buf, len) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbout");        err = true;      }    } else if(rnd < 31){      if(!tchdbputasync(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputasync");        err = true;      }    } else {      if(myrand(10) == 0){        int rsiz = myrand(256);        char *rbuf = tcmalloc(rsiz + 1);        for(int j = 0; j < rsiz; j++){          rbuf[j] = myrand('z' - 'a') + 'a';        }        if(myrand(2) == 0){          if(!tchdbput(hdb, buf, len, rbuf, rsiz)){            eprint(hdb, "tchdbputcat");            err = true;          }        } else {          if(!tchdbputcat(hdb, buf, len, rbuf, rsiz)){            eprint(hdb, "tchdbputcat");            err = true;          }        }        tcfree(rbuf);      } else {        if(!tchdbput(hdb, buf, len, buf, len)){          eprint(hdb, "tchdbput");          err = true;        }      }    }    if(id == 0){      if(myrand(mid) == 0){        iprintf("[v]");        if(!tchdbvanish(hdb)){          eprint(hdb, "tchdbvanish");          err = true;        }      }      if(myrand(mid) == 0){        iprintf("[o]");        if(!tchdboptimize(hdb, myrand(rnum) + 1, myrand(10), myrand(10), 0)){          eprint(hdb, "tchdboptimize");          err = true;        }      }      if(myrand(mid) == 0){        iprintf("[i]");        if(!tchdbiterinit(hdb)){          eprint(hdb, "tchdbput");          err = true;        }        char *kbuf;        int ksiz;        while((kbuf = tchdbiternext(hdb, &ksiz)) != NULL){          int vsiz;          char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);          if(vbuf){            tcfree(vbuf);          } else if(tchdbecode(hdb) != TCENOREC){            eprint(hdb, "tchdbget");            err = true;          }          tcfree(kbuf);        }        if(tchdbecode(hdb) != TCENOREC){          eprint(hdb, "(validation)");          err = true;        }      }      if(rnum > 250 && i % (rnum / 250) == 0){        iputchar('.');        if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);      }    }  }  return err ? "error" : NULL;}// END OF FILE

⌨️ 快捷键说明

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