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

📄 tctmgr.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 3 页
字号:
    }    break;  }  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  return err ? 1 : 0;}/* perform out command */static int procout(const char *path, const char *pkbuf, int pksiz, int omode){  TCTDB *tdb = tctdbnew();  if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd);  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);  if(!tctdbopen(tdb, path, TDBOWRITER | omode)){    printerr(tdb);    tctdbdel(tdb);    return 1;  }  bool err = false;  if(!tctdbout(tdb, pkbuf, pksiz)){    printerr(tdb);    err = true;  }  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  return err ? 1 : 0;}/* perform get command */static int procget(const char *path, const char *pkbuf, int pksiz, int omode, bool px, bool pz){  TCTDB *tdb = tctdbnew();  if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd);  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);  if(!tctdbopen(tdb, path, TDBOREADER | omode)){    printerr(tdb);    tctdbdel(tdb);    return 1;  }  bool err = false;  TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);  if(cols){    tcmapiterinit(cols);    const char *kbuf;    int ksiz;    while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){      int vsiz;      const char *vbuf = tcmapiterval(kbuf, &vsiz);      printdata(kbuf, ksiz, px);      putchar('\t');      printdata(vbuf, vsiz, px);      putchar(pz ? '\t' : '\n');    }    tcmapdel(cols);  } else {    printerr(tdb);    err = true;  }  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  return err ? 1 : 0;}/* perform list command */static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *fmstr){  TCTDB *tdb = tctdbnew();  if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd);  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);  if(!tctdbopen(tdb, path, TDBOREADER | omode)){    printerr(tdb);    tctdbdel(tdb);    return 1;  }  bool err = false;  if(fmstr){    TCLIST *pkeys = tctdbfwmkeys2(tdb, fmstr, max);    for(int i = 0; i < tclistnum(pkeys); i++){      int pksiz;      const char *pkbuf = tclistval(pkeys, i, &pksiz);      printdata(pkbuf, pksiz, px);      if(pv){        TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);        if(cols){          tcmapiterinit(cols);          const char *kbuf;          int ksiz;          while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){            int vsiz;            const char *vbuf = tcmapiterval(kbuf, &vsiz);            putchar('\t');            printdata(kbuf, ksiz, px);            putchar('\t');            printdata(vbuf, vsiz, px);          }          tcmapdel(cols);        }      }      putchar('\n');    }    tclistdel(pkeys);  } else {    if(!tctdbiterinit(tdb)){      printerr(tdb);      err = true;    }    char *pkbuf;    int pksiz;    int cnt = 0;    while((pkbuf = tctdbiternext(tdb, &pksiz)) != NULL){      printdata(pkbuf, pksiz, px);      if(pv){        TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);        if(cols){          tcmapiterinit(cols);          const char *kbuf;          int ksiz;          while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){            int vsiz;            const char *vbuf = tcmapiterval(kbuf, &vsiz);            putchar('\t');            printdata(kbuf, ksiz, px);            putchar('\t');            printdata(vbuf, vsiz, px);          }          tcmapdel(cols);        }      }      tcfree(pkbuf);      putchar('\n');      if(max >= 0 && ++cnt >= max) break;    }  }  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  return err ? 1 : 0;}/* perform search command */static int procsearch(const char *path, TCLIST *conds, const char *oname, const char *otype,                      int omode, int max, int skip, bool pv, bool px, bool ph, int bt, bool rm){  TCTDB *tdb = tctdbnew();  if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd);  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);  if(!tctdbopen(tdb, path, (rm ? TDBOWRITER : TDBOREADER) | omode)){    printerr(tdb);    tctdbdel(tdb);    return 1;  }  bool err = false;  TDBQRY *qry = tctdbqrynew(tdb);  int cnum = tclistnum(conds);  for(int i = 0; i < cnum - 2; i += 3){    const char *name = tclistval2(conds, i);    const char *opstr = tclistval2(conds, i + 1);    const char *expr  = tclistval2(conds, i + 2);    int op = tctdbqrystrtocondop(opstr);    if(op >= 0) tctdbqryaddcond(qry, name, op, expr);  }  if(oname){    int type = tctdbqrystrtoordertype(otype);    if(type >= 0) tctdbqrysetorder(qry, oname, type);  }  tctdbqrysetlimit(qry, max, skip);  if(rm){    double stime = tctime();    if(!tctdbqrysearchout(qry)){      printerr(tdb);      err = true;    }    double etime = tctime();    if(ph){      TCLIST *hints = tcstrsplit(tctdbqryhint(qry), "\n");      int hnum = tclistnum(hints);      for(int i = 0; i < hnum; i++){        const char *hint = tclistval2(hints, i);        if(*hint == '\0') continue;        printf("\t:::: %s\n", hint);      }      tclistdel(hints);      printf("\t:::: number of records: %d\n", tctdbqrycount(qry));      printf("\t:::: elapsed time: %.5f\n", etime - stime);    }  } else if(bt > 0){    double sum = 0;    for(int i = 1; i <= bt; i++){      double stime = tctime();      TCLIST *res = tctdbqrysearch(qry);      double etime = tctime();      tclistdel(res);      printf("%d: %.5f sec.\n", i, etime - stime);      sum += etime - stime;    }    printf("----\n");    printf("total: %.5f sec. (%.5f s/q = %.5f q/s)\n", sum, sum / bt, bt / sum);  } else {    double stime = tctime();    TCLIST *res = tctdbqrysearch(qry);    double etime = tctime();    int rnum = tclistnum(res);    for(int i = 0; i < rnum; i++){      int pksiz;      const char *pkbuf = tclistval(res, i, &pksiz);      printdata(pkbuf, pksiz, px);      if(pv){        TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);        if(cols){          tcmapiterinit(cols);          const char *kbuf;          int ksiz;          while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){            int vsiz;            const char *vbuf = tcmapiterval(kbuf, &vsiz);            putchar('\t');            printdata(kbuf, ksiz, px);            putchar('\t');            printdata(vbuf, vsiz, px);          }          tcmapdel(cols);        }      }      putchar('\n');    }    if(ph){      TCLIST *hints = tcstrsplit(tctdbqryhint(qry), "\n");      int hnum = tclistnum(hints);      for(int i = 0; i < hnum; i++){        const char *hint = tclistval2(hints, i);        if(*hint == '\0') continue;        printf("\t:::: %s\n", hint);      }      tclistdel(hints);      printf("\t:::: number of records: %d\n", tctdbqrycount(qry));      printf("\t:::: elapsed time: %.5f\n", etime - stime);    }    tclistdel(res);  }  tctdbqrydel(qry);  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  return err ? 1 : 0;}/* perform optimize command */static int procoptimize(const char *path, int bnum, int apow, int fpow, int opts, int omode){  TCTDB *tdb = tctdbnew();  if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd);  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);  if(!tctdbopen(tdb, path, TDBOWRITER | omode)){    printerr(tdb);    tctdbdel(tdb);    return 1;  }  bool err = false;  if(!tctdboptimize(tdb, bnum, apow, fpow, opts)){    printerr(tdb);    err = true;  }  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  return err ? 1 : 0;}/* perform setindex command */static int procsetindex(const char *path, const char *name, int omode, int type){  TCTDB *tdb = tctdbnew();  if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd);  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);  if(!tctdbopen(tdb, path, TDBOWRITER | omode)){    printerr(tdb);    tctdbdel(tdb);    return 1;  }  bool err = false;  if(!tctdbsetindex(tdb, name, type)){    printerr(tdb);    err = true;  }  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  return err ? 1 : 0;}/* perform importtsv command */static int procimporttsv(const char *path, const char *file, int omode, bool sc){  FILE *ifp = file ? fopen(file, "rb") : stdin;  if(!ifp){    fprintf(stderr, "%s: could not open\n", file ? file : "(stdin)");    return 1;  }  TCTDB *tdb = tctdbnew();  if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd);  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);  if(!tctdbopen(tdb, path, TDBOWRITER | TDBOCREAT | omode)){    printerr(tdb);    tctdbdel(tdb);    if(ifp != stdin) fclose(ifp);    return 1;  }  bool err = false;  char *line, numbuf[TCNUMBUFSIZ];  int cnt = 0;  while(!err && (line = mygetline(ifp)) != NULL){    char *pv = strchr(line, '\t');    if(!pv){      tcfree(line);      continue;    }    *pv = '\0';    if(sc) tcstrtolower(line);    const char *pkey;    if(*line == '\0'){      sprintf(numbuf, "%lld", (long long)tctdbgenuid(tdb));      pkey = numbuf;    } else {      pkey = line;    }    if(!tctdbput3(tdb, pkey, pv + 1)){      printerr(tdb);      err = true;    }    tcfree(line);    if(cnt > 0 && cnt % 100 == 0){      putchar('.');      fflush(stdout);      if(cnt % 5000 == 0) printf(" (%08d)\n", cnt);    }    cnt++;  }  printf(" (%08d)\n", cnt);  if(!tctdbclose(tdb)){    if(!err) printerr(tdb);    err = true;  }  tctdbdel(tdb);  if(ifp != stdin) fclose(ifp);  return err ? 1 : 0;}/* perform version command */static int procversion(void){  printf("Tokyo Cabinet version %s (%d:%s) for %s\n",         tcversion, _TC_LIBVER, _TC_FORMATVER, TCSYSNAME);  printf("Copyright (C) 2006-2009 Mikio Hirabayashi\n");  return 0;}// END OF FILE

⌨️ 快捷键说明

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