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

📄 tcbmttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = argv[i];    } else if(!rstr){      rstr = argv[i];    } else {      usage();    }  }  if(!path || !tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int rv = procwicked(path, tnum, rnum, opts, omode, nc);  return rv;}/* parse arguments of typical command */static int runtypical(int argc, char **argv){  char *path = NULL;  char *tstr = NULL;  char *rstr = NULL;  char *lmstr = NULL;  char *nmstr = NULL;  char *bstr = NULL;  char *astr = NULL;  char *fstr = NULL;  int opts = 0;  int omode = 0;  int rratio = -1;  bool nc = false;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tl")){        opts |= BDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= BDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= BDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= BDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= BDBTEXCODEC;      } else if(!strcmp(argv[i], "-nl")){        omode |= BDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= BDBOLCKNB;      } else if(!strcmp(argv[i], "-nc")){        nc = true;      } else if(!strcmp(argv[i], "-rr")){        if(++i >= argc) usage();        rratio = tcatoix(argv[i]);      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = argv[i];    } else if(!rstr){      rstr = argv[i];    } else if(!lmstr){      lmstr = argv[i];    } else if(!nmstr){      nmstr = argv[i];    } else if(!bstr){      bstr = argv[i];    } else if(!astr){      astr = argv[i];    } else if(!fstr){      fstr = argv[i];    } else {      usage();    }  }  if(!path || !tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int lmemb = lmstr ? tcatoix(lmstr) : -1;  int nmemb = nmstr ? tcatoix(nmstr) : -1;  int bnum = bstr ? tcatoix(bstr) : -1;  int apow = astr ? tcatoix(astr) : -1;  int fpow = fstr ? tcatoix(fstr) : -1;  int rv = proctypical(path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, nc, rratio);  return rv;}/* parse arguments of race command */static int runrace(int argc, char **argv){  char *path = NULL;  char *tstr = NULL;  char *rstr = NULL;  char *lmstr = NULL;  char *nmstr = NULL;  char *bstr = NULL;  char *astr = NULL;  char *fstr = NULL;  int opts = 0;  int omode = 0;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tl")){        opts |= BDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= BDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= BDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= BDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= BDBTEXCODEC;      } else if(!strcmp(argv[i], "-nl")){        omode |= BDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= BDBOLCKNB;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = argv[i];    } else if(!rstr){      rstr = argv[i];    } else if(!lmstr){      lmstr = argv[i];    } else if(!nmstr){      nmstr = argv[i];    } else if(!bstr){      bstr = argv[i];    } else if(!astr){      astr = argv[i];    } else if(!fstr){      fstr = argv[i];    } else {      usage();    }  }  if(!path || !tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int lmemb = lmstr ? tcatoix(lmstr) : -1;  int nmemb = nmstr ? tcatoix(nmstr) : -1;  int bnum = bstr ? tcatoix(bstr) : -1;  int apow = astr ? tcatoix(astr) : -1;  int fpow = fstr ? tcatoix(fstr) : -1;  int rv = procrace(path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode);  return rv;}/* perform write command */static int procwrite(const char *path, int tnum, int rnum, int lmemb, int nmemb,                     int bnum, int apow, int fpow, int opts, int omode, bool rnd){  iprintf("<Writing Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  lmemb=%d  nmemb=%d"          "  bnum=%d  apow=%d  fpow=%d  opts=%d  omode=%d  rnd=%d\n\n",          g_randseed, path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){    eprint(bdb, "tcbdbtune");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  TARGWRITE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].bdb = bdb;    targs[0].rnum = rnum;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadwrite(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].bdb = bdb;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadwrite, targs + i) != 0){        eprint(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  sysprint();  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform read command */static int procread(const char *path, int tnum, int omode, bool wb, bool rnd){  iprintf("<Reading Test>\n  seed=%u  path=%s  tnum=%d  omode=%d  wb=%d  rnd=%d\n\n",          g_randseed, path, tnum, omode, wb, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOREADER | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  int rnum = tcbdbrnum(bdb) / tnum;  TARGREAD targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].bdb = bdb;    targs[0].rnum = rnum;    targs[0].wb = wb;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadread(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].bdb = bdb;      targs[i].rnum = rnum;      targs[i].wb = wb;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadread, targs + i) != 0){        eprint(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  sysprint();  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform remove command */static int procremove(const char *path, int tnum, int omode, bool rnd){  iprintf("<Removing Test>\n  seed=%u  path=%s  tnum=%d  omode=%d  rnd=%d\n\n",          g_randseed, path, tnum, omode, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOWRITER | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  int rnum = tcbdbrnum(bdb) / tnum;  TARGREMOVE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].bdb = bdb;    targs[0].rnum = rnum;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadremove(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].bdb = bdb;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadremove, targs + i) != 0){        eprint(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  sysprint();  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  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 tnum, int rnum, int opts, int omode, bool nc){  iprintf("<Writing Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  opts=%d  omode=%d  nc=%d\n\n",          g_randseed, path, tnum, rnum, opts, omode, nc);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbtune(bdb, 10, 10, rnum / 50, 10, -1, opts)){    eprint(bdb, "tcbdbtune");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  TARGWICKED targs[tnum];  pthread_t threads[tnum];  TCMAP *map = tcmapnew();  if(tnum == 1){    targs[0].bdb = bdb;    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].bdb = bdb;      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(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  if(!nc){    if(!tcbdbsync(bdb)){      eprint(bdb, "tcbdbsync");

⌨️ 快捷键说明

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