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

📄 tchmttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
    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;  }  TARGWICKED targs[tnum];  pthread_t threads[tnum];  TCMAP *map = tcmapnew();  if(tnum == 1){    targs[0].hdb = hdb;    targs[0].rnum = rnum;    targs[0].nc = nc;    targs[0].id = 0;    targs[0].map = map;    if(threadwicked(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].hdb = hdb;      targs[i].rnum = rnum;      targs[i].nc = nc;      targs[i].id = i;      targs[i].map = map;      if(pthread_create(threads + i, NULL, threadwicked, targs + i) != 0){        eprint(hdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(hdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  if(!nc){    if(!tchdbsync(hdb)){      eprint(hdb, "tchdbsync");      err = true;    }    if(tchdbrnum(hdb) != tcmaprnum(map)){      eprint(hdb, "(validation)");      err = true;    }    int end = rnum * tnum;    for(int i = 1; i <= end && !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);  }  tcmapdel(map);  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 typical command */static int proctypical(const char *path, int tnum, int rnum, int bnum, int apow, int fpow,                       int opts, int rcnum, int xmsiz, int omode, bool nc, int rratio){  iprintf("<Typical Access Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  bnum=%d  apow=%d"          "  fpow=%d  opts=%d  rcnum=%d  xmsiz=%d  omode=%d  nc=%d  rratio=%d\n\n",          g_randseed, path, tnum, rnum, bnum, apow, fpow, opts, rcnum, xmsiz, omode, nc, rratio);  bool err = false;  double stime = tctime();  TCHDB *hdb = tchdbnew();  if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd);  if(!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;  }  TARGTYPICAL targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].hdb = hdb;    targs[0].rnum = rnum;    targs[0].nc = nc;    targs[0].rratio = rratio;    targs[0].id = 0;    if(threadtypical(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].hdb = hdb;      targs[i].rnum = rnum;      targs[i].nc = nc;      targs[i].rratio= rratio;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadtypical, targs + i) != 0){        eprint(hdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(hdb, "pthread_join");        err = true;      } else if(rv){        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 race command */static int procrace(const char *path, int tnum, int rnum, int bnum, int apow, int fpow,                    int opts, int xmsiz, int omode){  iprintf("<Race Condition Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  bnum=%d  apow=%d"          "  fpow=%d  opts=%d  xmsiz=%d  omode=%d\n\n",          g_randseed, path, tnum, rnum, bnum, apow, fpow, opts, xmsiz, omode);  bool err = false;  double stime = tctime();  TCHDB *hdb = tchdbnew();  if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd);  if(!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(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){    eprint(hdb, "tchdbsetxmsiz");    err = true;  }  if(!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | HDBOTRUNC | omode)){    eprint(hdb, "tchdbopen");    err = true;  }  TARGRACE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].hdb = hdb;    targs[0].rnum = rnum;    targs[0].id = 0;    if(threadrace(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].hdb = hdb;      targs[i].rnum = rnum;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadrace, targs + i) != 0){        eprint(hdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(hdb, "pthread_join");        err = true;      } else if(rv){        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;}/* thread the write function */static void *threadwrite(void *targ){  TCHDB *hdb = ((TARGWRITE *)targ)->hdb;  int rnum = ((TARGWRITE *)targ)->rnum;  bool as = ((TARGWRITE *)targ)->as;  bool rnd = ((TARGWRITE *)targ)->rnd;  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) : i));    if(as){      if(!tchdbputasync(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputasync");        err = true;        break;      }    } else {      if(!tchdbput(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbput");        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){  TCHDB *hdb = ((TARGREAD *)targ)->hdb;  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++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrandnd(i) : 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;      }    } else {      char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);      if(!vbuf && (!rnd || tchdbecode(hdb) != TCENOREC)){        eprint(hdb, "tchdbget");        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){  TCHDB *hdb = ((TARGREMOVE *)targ)->hdb;  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) : i));    if(!tchdbout(hdb, kbuf, ksiz) && (!rnd || tchdbecode(hdb) != TCENOREC)){      eprint(hdb, "tchdbout");      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){  TCHDB *hdb = ((TARGWICKED *)targ)->hdb;  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++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", myrand(rnum * (id + 1)));    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(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){        eprint(hdb, "tchdbput");        err = true;      }      if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);      break;    case 1:      if(id == 0) iputchar('1');      if(!tchdbput2(hdb, kbuf, vbuf)){        eprint(hdb, "tchdbput2");        err = true;      }      if(!nc) tcmapput2(map, kbuf, vbuf);      break;    case 2:      if(id == 0) iputchar('2');      if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep");        err = true;      }      if(!nc) tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);      break;    case 3:      if(id == 0) iputchar('3');      if(!tchdbputkeep2(hdb, kbuf, vbuf) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep2");        err = true;      }      if(!nc) tcmapputkeep2(map, kbuf, vbuf);      break;    case 4:      if(id == 0) iputchar('4');      if(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){        eprint(hdb, "tchdbputcat");        err = true;      }      if(!nc) tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);      break;    case 5:      if(id == 0) iputchar('5');      if(!tchdbputcat2(hdb, kbuf, vbuf)){        eprint(hdb, "tchdbputcat2");        err = true;      }      if(!nc) tcmapputcat2(map, kbuf, vbuf);      break;    case 6:

⌨️ 快捷键说明

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