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

📄 tcatest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 3 页
字号:
    } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(adb, "(validation)");      err = true;    }    tcfree(rbuf);    if(!tcadbout(adb, kbuf, ksiz)){      eprint(adb, "tcadbout");      err = true;    }    if(i % 50 == 0) iprintf(" (%08d)\n", i);  }  int mrnum = tcmaprnum(map);  if(mrnum % 50 > 0) iprintf(" (%08d)\n", mrnum);  if(tcadbrnum(adb) != 0){    eprint(adb, "(validation)");    err = true;  }  iprintf("record number: %llu\n", (unsigned long long)tcadbrnum(adb));  iprintf("size: %llu\n", (unsigned long long)tcadbsize(adb));  sysprint();  tcmapdel(map);  if(!tcadbclose(adb)){    eprint(adb, "tcadbclose");    err = true;  }  tcadbdel(adb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform compare command */static int proccompare(const char *name, int tnum, int rnum){  iprintf("<Comparison Test>\n  seed=%u  name=%s  tnum=%d  rnum=%d\n\n",          g_randseed, name, tnum, rnum);  bool err = false;  double stime = tctime();  char path[PATH_MAX];  TCMDB *mdb = tcmdbnew2(rnum / 2);  TCNDB *ndb = tcndbnew();  TCHDB *hdb = tchdbnew();  tchdbsetdbgfd(hdb, UINT16_MAX);  int hopts = 0;  if(myrand(2) == 1) hopts |= HDBTLARGE;  if(myrand(2) == 1) hopts |= HDBTBZIP;  if(!tchdbtune(hdb, rnum / 2, -1, -1, hopts)){    eprint(NULL, "tchdbtune");    err = true;  }  if(!tchdbsetcache(hdb, rnum / 10)){    eprint(NULL, "tchdbsetcache");    err = true;  }  if(!tchdbsetxmsiz(hdb, 4096)){    eprint(NULL, "tchdbsetxmsiz");    err = true;  }  sprintf(path, "%s.tch", name);  int homode = HDBOWRITER | HDBOCREAT | HDBOTRUNC;  if(myrand(100) == 1) homode |= HDBOTSYNC;  if(!tchdbopen(hdb, path, homode)){    eprint(NULL, "tchdbopen");    err = true;  }  TCBDB *bdb = tcbdbnew();  tcbdbsetdbgfd(bdb, UINT16_MAX);  int bopts = 0;  if(myrand(2) == 1) bopts |= BDBTLARGE;  if(myrand(2) == 1) bopts |= BDBTBZIP;  if(!tcbdbtune(bdb, 5, 5, rnum / 10, -1, -1, bopts)){    eprint(NULL, "tcbdbtune");    err = true;  }  sprintf(path, "%s.tcb", name);  int bomode = BDBOWRITER | BDBOCREAT | BDBOTRUNC;  if(myrand(100) == 1) bomode |= BDBOTSYNC;  if(!tcbdbopen(bdb, path, bomode)){    eprint(NULL, "tcbdbopen");    err = true;  }  BDBCUR *cur = tcbdbcurnew(bdb);  TCADB *adb = tcadbnew();  switch(myrand(4)){  case 0:    sprintf(path, "%s.adb.tch#mode=wct#bnum=%d", name, rnum);    break;  case 1:    sprintf(path, "%s.adb.tcb#mode=wct#lmemb=256#nmemb=512", name);    break;  case 3:    sprintf(path, "+");    break;  default:    sprintf(path, "*");    break;  }  if(!tcadbopen(adb, path)){    eprint(NULL, "tcbdbopen");    err = true;  }  for(int t = 1; !err && t <= tnum; t++){    bool commit = myrand(2) == 0;    iprintf("transaction %d (%s):\n", t, commit ? "commit" : "abort");    if(!tchdbtranbegin(hdb)){      eprint(NULL, "tchdbtranbegin");      err = true;    }    if(!tcbdbtranbegin(bdb)){      eprint(NULL, "tcbdbtranbegin");      err = true;    }    if(myrand(tnum) == 0){      bool all = myrand(2) == 0;      for(int i = 1; !err && i <= rnum; i++){        char kbuf[RECBUFSIZ];        int ksiz = sprintf(kbuf, "%d", all ? i : myrand(i) + 1);        if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){          eprint(NULL, "tchdbout");          err = true;        }        if(!tcbdbout(bdb, kbuf, ksiz) && tcbdbecode(bdb) != TCENOREC){          eprint(NULL, "tcbdbout");          err = true;        }        tcadbout(adb, kbuf, ksiz);        if(tchdbout(hdb, kbuf, ksiz) || tchdbecode(hdb) != TCENOREC){          eprint(NULL, "(validation)");          err = true;        }        if(tcbdbout(bdb, kbuf, ksiz) || tcbdbecode(bdb) != TCENOREC){          eprint(NULL, "(validation)");          err = true;        }        if(tcadbout(adb, kbuf, ksiz)){          eprint(NULL, "(validation)");          err = true;        }        if(commit){          tcmdbout(mdb, kbuf, ksiz);          tcndbout(ndb, kbuf, ksiz);        }        if(rnum > 250 && i % (rnum / 250) == 0){          iputchar('.');          if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);        }      }    } else {      int act = myrand(7);      for(int i = 1; !err && i <= rnum; i++){        if(myrand(10) == 0) act = myrand(7);        char kbuf[RECBUFSIZ];        int ksiz = sprintf(kbuf, "%d", myrand(i) + 1);        char vbuf[RECBUFSIZ+256];        int vsiz = sprintf(vbuf, "%64d:%d:%d", t, i, myrand(i));        switch(act){        case 0:          if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){            eprint(NULL, "tchdbput");            err = true;          }          if(!tcbdbput(bdb, kbuf, ksiz, vbuf, vsiz)){            eprint(NULL, "tcbdbput");            err = true;          }          if(!tcadbput(adb, kbuf, ksiz, vbuf, vsiz)){            eprint(NULL, "tcadbput");            err = true;          }          if(commit){            tcmdbput(mdb, kbuf, ksiz, vbuf, vsiz);            tcndbput(ndb, kbuf, ksiz, vbuf, vsiz);          }          break;        case 1:          if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz) && tchdbecode(hdb) != TCEKEEP){            eprint(NULL, "tchdbputkeep");            err = true;          }          if(!tcbdbputkeep(bdb, kbuf, ksiz, vbuf, vsiz) && tcbdbecode(bdb) != TCEKEEP){            eprint(NULL, "tcbdbputkeep");            err = true;          }          tcadbputkeep(adb, kbuf, ksiz, vbuf, vsiz);          if(commit){            tcmdbputkeep(mdb, kbuf, ksiz, vbuf, vsiz);            tcndbputkeep(ndb, kbuf, ksiz, vbuf, vsiz);          }          break;        case 2:          if(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){            eprint(NULL, "tchdbputcat");            err = true;          }          if(!tcbdbputcat(bdb, kbuf, ksiz, vbuf, vsiz)){            eprint(NULL, "tcbdbputcat");            err = true;          }          if(!tcadbputcat(adb, kbuf, ksiz, vbuf, vsiz)){            eprint(NULL, "tcadbputcat");            err = true;          }          if(commit){            tcmdbputcat(mdb, kbuf, ksiz, vbuf, vsiz);            tcndbputcat(ndb, kbuf, ksiz, vbuf, vsiz);          }          break;        case 3:          if(tchdbaddint(hdb, kbuf, ksiz, 1) == INT_MIN && tchdbecode(hdb) != TCEKEEP){            eprint(NULL, "tchdbaddint");            err = true;          }          if(tcbdbaddint(bdb, kbuf, ksiz, 1) == INT_MIN && tcbdbecode(bdb) != TCEKEEP){            eprint(NULL, "tchdbaddint");            err = true;          }          tcadbaddint(adb, kbuf, ksiz, 1);          if(commit){            tcmdbaddint(mdb, kbuf, ksiz, 1);            tcndbaddint(ndb, kbuf, ksiz, 1);          }          break;        case 4:          if(isnan(tchdbadddouble(hdb, kbuf, ksiz, 1.0)) && tchdbecode(hdb) != TCEKEEP){            eprint(NULL, "tchdbadddouble");            err = true;          }          if(isnan(tcbdbadddouble(bdb, kbuf, ksiz, 1.0)) && tcbdbecode(bdb) != TCEKEEP){            eprint(NULL, "tchdbadddouble");            err = true;          }          tcadbadddouble(adb, kbuf, ksiz, 1.0);          if(commit){            tcmdbadddouble(mdb, kbuf, ksiz, 1.0);            tcndbadddouble(ndb, kbuf, ksiz, 1.0);          }          break;        case 5:          if(myrand(2) == 0){            if(!tchdbputproc(hdb, kbuf, ksiz, vbuf, vsiz, pdprocfunccmp, &i) &&               tchdbecode(hdb) != TCEKEEP){              eprint(NULL, "tchdbputproc");              err = true;            }            if(!tcbdbputproc(bdb, kbuf, ksiz, vbuf, vsiz, pdprocfunccmp, &i) &&               tcbdbecode(bdb) != TCEKEEP){              eprint(NULL, "tcbdbputproc");              err = true;            }            tcadbputproc(adb, kbuf, ksiz, vbuf, vsiz, pdprocfunccmp, &i);            if(commit){              tcmdbputproc(mdb, kbuf, ksiz, vbuf, vsiz, pdprocfunccmp, &i);              tcndbputproc(ndb, kbuf, ksiz, vbuf, vsiz, pdprocfunccmp, &i);            }          } else {            if(!tchdbputproc(hdb, kbuf, ksiz, NULL, 0, pdprocfunccmp, &i) &&               tchdbecode(hdb) != TCEKEEP && tchdbecode(hdb) != TCENOREC){              eprint(NULL, "tchdbputproc");              err = true;            }            if(!tcbdbputproc(bdb, kbuf, ksiz, NULL, 0, pdprocfunccmp, &i) &&               tcbdbecode(bdb) != TCEKEEP && tchdbecode(hdb) != TCENOREC){              eprint(NULL, "tcbdbputproc");              err = true;            }            tcadbputproc(adb, kbuf, ksiz, NULL, 0, pdprocfunccmp, &i);            if(commit){              tcmdbputproc(mdb, kbuf, ksiz, NULL, 0, pdprocfunccmp, &i);              tcndbputproc(ndb, kbuf, ksiz, NULL, 0, pdprocfunccmp, &i);            }          }          break;        default:          if(myrand(20) == 0){            if(!tcbdbcurjump(cur, kbuf, ksiz) && tcbdbecode(bdb) != TCENOREC){              eprint(NULL, "tcbdbcurjump");              err = true;            }            char *cbuf;            int csiz;            while((cbuf = tcbdbcurkey(cur, &csiz)) != NULL){              if(!tchdbout(hdb, cbuf, csiz)){                eprint(NULL, "tchdbout");                err = true;              }              if(!tcbdbout(bdb, cbuf, csiz)){                eprint(NULL, "tcbdbout");                err = true;              }              tcadbout(adb, cbuf, csiz);              if(commit){                tcmdbout(mdb, cbuf, csiz);                tcndbout(ndb, cbuf, csiz);              }              tcfree(cbuf);              if(myrand(10) == 0) break;              switch(myrand(3)){              case 1:                if(!tcbdbcurprev(cur) && tcbdbecode(bdb) != TCENOREC){                  eprint(NULL, "tcbdbcurprev");                  err = true;                }                break;              case 2:                if(!tcbdbcurnext(cur) && tcbdbecode(bdb) != TCENOREC){                  eprint(NULL, "tcbdbcurprev");                  err = true;                }                break;              }            }          } else {            if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){              eprint(NULL, "tchdbout");              err = true;            }            if(!tcbdbout(bdb, kbuf, ksiz) && tcbdbecode(bdb) != TCENOREC){              eprint(NULL, "tcbdbout");              err = true;            }            tcadbout(adb, kbuf, ksiz);            if(commit){              tcmdbout(mdb, kbuf, ksiz);              tcndbout(ndb, kbuf, ksiz);            }          }          break;        }        if(rnum > 250 && i % (rnum / 250) == 0){          iputchar('.');          if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);        }      }    }    if(commit){      if(!tchdbtrancommit(hdb)){        eprint(NULL, "tchdbcommit");        err = true;      }      if(!tcbdbtrancommit(bdb)){        eprint(NULL, "tcbdbcommit");        err = true;      }    } else {      if(myrand(5) == 0){        if(!tchdbclose(hdb)){          eprint(NULL, "tchdbclose");          err = true;        }        sprintf(path, "%s.tch", name);        if(!tchdbopen(hdb, path, HDBOWRITER)){          eprint(NULL, "tchdbopen");          err = true;        }        if(!tcbdbclose(bdb)){          eprint(NULL, "tcbdbclose");          err = true;        }        sprintf(path, "%s.tcb", name);        if(!tcbdbopen(bdb, path, BDBOWRITER)){          eprint(NULL, "tcbdbopen");          err = true;        }      } else {        if(!tchdbtranabort(hdb)){          eprint(NULL, "tchdbtranabort");          err = true;        }        if(!tcbdbtranabort(bdb)){          eprint(NULL, "tcbdbtranabort");          err = true;        }      }    }  }  iprintf("checking consistency of range:\n");  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", i);    int vsiz;    char *vbuf = tcmdbget(mdb, kbuf, ksiz, &vsiz);    if(vbuf){      int rsiz;      char *rbuf = tcndbget(ndb, kbuf, ksiz, &rsiz);      if(rbuf){        tcfree(rbuf);      } else {        eprint(NULL, "tcndbget");        err = true;      }      rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz);      if(rbuf){        tcfree(rbuf);      } else {        eprint(NULL, "tchdbget");        err = true;      }      rbuf = tcbdbget(bdb, kbuf, ksiz, &rsiz);      if(rbuf){        tcfree(rbuf);      } else {        eprint(NULL, "tcbdbget");        err = true;      }      tcfree(vbuf);    } else {      int rsiz;      char *rbuf = tcndbget(ndb, kbuf, ksiz, &rsiz);      if(rbuf){        eprint(NULL, "tcndbget");        tcfree(rbuf);        err = true;      }      rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz);      if(rbuf){        eprint(NULL, "tchdbget");        err = true;        tcfree(rbuf);      }      rbuf = tcbdbget(bdb, kbuf, ksiz, &rsiz);      if(rbuf){        eprint(NULL, "tcbdbget");        err = true;        tcfree(rbuf);      }    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  iprintf("checking consistency on memory:\n");  if(tchdbrnum(hdb) != tcbdbrnum(bdb)){    eprint(NULL, "(validation)");    err = true;  }  int inum = 0;  tcmdbiterinit(mdb);  char *kbuf;  int ksiz;  for(int i = 1; (kbuf = tcmdbiternext(mdb, &ksiz)) != NULL; i++, inum++){    int vsiz;    char *vbuf = tcmdbget(mdb, kbuf, ksiz, &vsiz);    int rsiz;    char *rbuf = tcndbget(ndb, kbuf, ksiz, &rsiz);    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(NULL, "tcndbget");      err = true;    }    tcfree(rbuf);    rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz);    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(NULL, "tchdbget");      err = true;    }    tcfree(rbuf);    rbuf = tcbdbget(bdb, kbuf, ksiz, &rsiz);    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(NULL, "tcbdbget");      err = true;    }    tcfree(rbuf);    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);  iprintf("checking consistency of hash:\n");  inum = 0;  if(!tchdbiterinit(hdb)){    eprint(NULL, "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;    char *rbuf = tcmdbget(mdb, kbuf, ksiz, &rsiz);    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(NULL, "(validation)");      err = true;    }    tcfree(rbuf);    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);  iprintf("checking consistency of tree:\n");  if(!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC){    eprint(NULL, "tcbdbcurfirst");    err = true;  }  for(int i = 1; (kbuf = tcbdbcurkey(cur, &ksiz)) != NULL; i++, inum++){    int vsiz;    char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);    int rsiz;    char *rbuf = tcndbget(ndb, kbuf, ksiz, &rsiz);    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){      eprint(NULL, "(validation)");      err = true;    }    tcfree(rbuf);    tcfree(vbuf);    tcfree(kbuf);    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }    tcbdbcurnext(cur);  }  if(rnum > 250) iprintf(" (%08d)\n", inum);  if(!tcadbclose(adb)){    eprint(NULL, "tcadbclose");    err = true;  }  tcbdbcurdel(cur);  if(!tcbdbclose(bdb)){    eprint(NULL, "tcbdbclose");    err = true;  }  if(!tchdbclose(hdb)){    eprint(NULL, "tcbdbclose");    err = true;  }  tcadbdel(adb);  tcmdbdel(mdb);  tcndbdel(ndb);  tchdbdel(hdb);  tcbdbdel(bdb);  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 + -