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

📄 tchmttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
        opts |= HDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= HDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= HDBTEXCODEC;      } else if(!strcmp(argv[i], "-nl")){        omode |= HDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= HDBOLCKNB;      } else if(!strcmp(argv[i], "-nc")){        nc = true;      } else {        usage();      }    } 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 *bstr = NULL;  char *astr = NULL;  char *fstr = NULL;  int opts = 0;  int rcnum = 0;  int xmsiz = -1;  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 |= HDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= HDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= HDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= HDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= HDBTEXCODEC;      } else if(!strcmp(argv[i], "-rc")){        if(++i >= argc) usage();        rcnum = tcatoix(argv[i]);      } else if(!strcmp(argv[i], "-xm")){        if(++i >= argc) usage();        xmsiz = tcatoix(argv[i]);      } else if(!strcmp(argv[i], "-nl")){        omode |= HDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= HDBOLCKNB;      } 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(!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 bnum = bstr ? tcatoix(bstr) : -1;  int apow = astr ? tcatoix(astr) : -1;  int fpow = fstr ? tcatoix(fstr) : -1;  int rv = proctypical(path, tnum, rnum, bnum, apow, fpow, opts, rcnum, xmsiz, 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 *bstr = NULL;  char *astr = NULL;  char *fstr = NULL;  int opts = 0;  int xmsiz = -1;  int omode = 0;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tl")){        opts |= HDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= HDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= HDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= HDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= HDBTEXCODEC;      } else if(!strcmp(argv[i], "-xm")){        if(++i >= argc) usage();        xmsiz = tcatoix(argv[i]);      } else if(!strcmp(argv[i], "-nl")){        omode |= HDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= HDBOLCKNB;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!tstr){      tstr = argv[i];    } else if(!rstr){      rstr = 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 bnum = bstr ? tcatoix(bstr) : -1;  int apow = astr ? tcatoix(astr) : -1;  int fpow = fstr ? tcatoix(fstr) : -1;  int rv = procrace(path, tnum, rnum, bnum, apow, fpow, opts, xmsiz, omode);  return rv;}/* perform write command */static int procwrite(const char *path, int tnum, int rnum, int bnum, int apow, int fpow,                     int opts, int rcnum, int xmsiz, int omode, bool as, bool rnd){  iprintf("<Writing Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  bnum=%d  apow=%d  fpow=%d"          "  opts=%d  rcnum=%d  xmsiz=%d  omode=%d  as=%d  rnd=%d\n\n",          g_randseed, path, tnum, rnum, bnum, apow, fpow, opts, rcnum, xmsiz, omode, as, rnd);  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;  }  TARGWRITE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].hdb = hdb;    targs[0].rnum = rnum;    targs[0].as = as;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadwrite(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].hdb = hdb;      targs[i].rnum = rnum;      targs[i].as = as;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadwrite, 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 read command */static int procread(const char *path, int tnum, int rcnum, int xmsiz, int omode,                    bool wb, bool rnd){  iprintf("<Reading Test>\n  seed=%u  path=%s  tnum=%d  rcnum=%d  xmsiz=%d  omode=%d"          "  wb=%d  rnd=%d\n\n", g_randseed, path, tnum, rcnum, xmsiz, omode, wb, rnd);  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(!tchdbsetcache(hdb, rcnum)){    eprint(hdb, "tchdbsetcache");    err = true;  }  if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){    eprint(hdb, "tchdbsetxmsiz");    err = true;  }  if(!tchdbopen(hdb, path, HDBOREADER | omode)){    eprint(hdb, "tchdbopen");    err = true;  }  int rnum = tchdbrnum(hdb) / tnum;  TARGREAD targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].hdb = hdb;    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].hdb = hdb;      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(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 remove command */static int procremove(const char *path, int tnum, int rcnum, int xmsiz, int omode, bool rnd){  iprintf("<Removing Test>\n  seed=%u  path=%s  tnum=%d  rcnum=%d  xmsiz=%d  omode=%d"          "  rnd=%d\n\n", g_randseed, path, tnum, rcnum, xmsiz, omode, rnd);  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(!tchdbsetcache(hdb, rcnum)){    eprint(hdb, "tchdbsetcache");    err = true;  }  if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){    eprint(hdb, "tchdbsetxmsiz");    err = true;  }  if(!tchdbopen(hdb, path, HDBOWRITER | omode)){    eprint(hdb, "tchdbopen");    err = true;  }  int rnum = tchdbrnum(hdb) / tnum;  TARGREMOVE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].hdb = hdb;    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].hdb = hdb;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadremove, 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 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();  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, rnum / 50, 2, -1, opts)){    eprint(hdb, "tchdbtune");    err = true;  }  if(!tchdbsetcache(hdb, rnum / 2)){

⌨️ 快捷键说明

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