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

📄 tctmgr.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************************************************************* * The command line utility of the table database API *                                                      Copyright (C) 2006-2009 Mikio Hirabayashi * This file is part of Tokyo Cabinet. * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation; either * version 2.1 of the License or any later version.  Tokyo Cabinet is distributed in the hope * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public * License for more details. * You should have received a copy of the GNU Lesser General Public License along with Tokyo * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA. *************************************************************************************************/#include <tcutil.h>#include <tctdb.h>#include "myconf.h"/* global variables */const char *g_progname;                  // program nameint g_dbgfd;                             // debugging output/* function prototypes */int main(int argc, char **argv);static void usage(void);static void printerr(TCTDB *tdb);static int printdata(const char *ptr, int size, bool px);static char *mygetline(FILE *ifp);static int runcreate(int argc, char **argv);static int runinform(int argc, char **argv);static int runput(int argc, char **argv);static int runout(int argc, char **argv);static int runget(int argc, char **argv);static int runlist(int argc, char **argv);static int runsearch(int argc, char **argv);static int runoptimize(int argc, char **argv);static int runsetindex(int argc, char **argv);static int runimporttsv(int argc, char **argv);static int runversion(int argc, char **argv);static int proccreate(const char *path, int bnum, int apow, int fpow, int opts);static int procinform(const char *path, int omode);static int procput(const char *path, const char *pkbuf, int pksiz, TCMAP *cols,                   int omode, int dmode);static int procout(const char *path, const char *pkbuf, int pksiz, int omode);static int procget(const char *path, const char *pkbuf, int pksiz, int omode, bool px, bool pz);static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *fmstr);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);static int procoptimize(const char *path, int bnum, int apow, int fpow, int opts, int omode);static int procsetindex(const char *path, const char *name, int omode, int type);static int procimporttsv(const char *path, const char *file, int omode, bool sc);static int procversion(void);/* main routine */int main(int argc, char **argv){  g_progname = argv[0];  g_dbgfd = -1;  const char *ebuf = getenv("TCDBGFD");  if(ebuf) g_dbgfd = tcatoix(ebuf);  if(argc < 2) usage();  int rv = 0;  if(!strcmp(argv[1], "create")){    rv = runcreate(argc, argv);  } else if(!strcmp(argv[1], "inform")){    rv = runinform(argc, argv);  } else if(!strcmp(argv[1], "put")){    rv = runput(argc, argv);  } else if(!strcmp(argv[1], "out")){    rv = runout(argc, argv);  } else if(!strcmp(argv[1], "get")){    rv = runget(argc, argv);  } else if(!strcmp(argv[1], "list")){    rv = runlist(argc, argv);  } else if(!strcmp(argv[1], "search")){    rv = runsearch(argc, argv);  } else if(!strcmp(argv[1], "optimize")){    rv = runoptimize(argc, argv);  } else if(!strcmp(argv[1], "setindex")){    rv = runsetindex(argc, argv);  } else if(!strcmp(argv[1], "importtsv")){    rv = runimporttsv(argc, argv);  } else if(!strcmp(argv[1], "version") || !strcmp(argv[1], "--version")){    rv = runversion(argc, argv);  } else {    usage();  }  return rv;}/* print the usage and exit */static void usage(void){  fprintf(stderr, "%s: the command line utility of the table database API\n", g_progname);  fprintf(stderr, "\n");  fprintf(stderr, "usage:\n");  fprintf(stderr, "  %s create [-tl] [-td|-tb|-tt|-tx] path [bnum [apow [fpow]]]\n", g_progname);  fprintf(stderr, "  %s inform [-nl|-nb] path\n", g_progname);  fprintf(stderr, "  %s put [-nl|-nb] [-sx] [-dk|-dc|-dai|-dad] path pkey [cols...]\n",          g_progname);  fprintf(stderr, "  %s out [-nl|-nb] [-sx] path pkey\n", g_progname);  fprintf(stderr, "  %s get [-nl|-nb] [-sx] [-px] [-pz] path pkey\n", g_progname);  fprintf(stderr, "  %s list [-nl|-nb] [-m num] [-pv] [-px] [-fm str] path\n", g_progname);  fprintf(stderr, "  %s search [-nl|-nb] [-ord name type] [-m num] [-sk num] [-pv] [-px] [-ph]"          " [-bt num] [-rm] path [name op expr ...]\n", g_progname);  fprintf(stderr, "  %s optimize [-tl] [-td|-tb|-tt|-tx] [-tz] [-nl|-nb] path"          " [bnum [apow [fpow]]]\n", g_progname);  fprintf(stderr, "  %s setindex [-nl|-nb] [-it type] path name\n", g_progname);  fprintf(stderr, "  %s importtsv [-nl|-nb] [-sc] path [file]\n", g_progname);  fprintf(stderr, "  %s version\n", g_progname);  fprintf(stderr, "\n");  exit(1);}/* print error information */static void printerr(TCTDB *tdb){  const char *path = tctdbpath(tdb);  int ecode = tctdbecode(tdb);  fprintf(stderr, "%s: %s: %d: %s\n", g_progname, path ? path : "-", ecode, tctdberrmsg(ecode));}/* print record data */static int printdata(const char *ptr, int size, bool px){  int len = 0;  while(size-- > 0){    if(px){      if(len > 0) putchar(' ');      len += printf("%02X", *(unsigned char *)ptr);    } else {      if(strchr("\t\r\n", *ptr)){        putchar(' ');      } else {        putchar(*ptr);      }      len++;    }    ptr++;  }  return len;}/* read a line from a file descriptor */static char *mygetline(FILE *ifp){  int len = 0;  int blen = 1024;  char *buf = tcmalloc(blen);  bool end = true;  int c;  while((c = fgetc(ifp)) != EOF){    end = false;    if(c == '\0') continue;    if(blen <= len){      blen *= 2;      buf = tcrealloc(buf, blen + 1);    }    if(c == '\n' || c == '\r') c = '\0';    buf[len++] = c;    if(c == '\0') break;  }  if(end){    tcfree(buf);    return NULL;  }  buf[len] = '\0';  return buf;}/* parse arguments of create command */static int runcreate(int argc, char **argv){  char *path = NULL;  char *bstr = NULL;  char *astr = NULL;  char *fstr = NULL;  int opts = 0;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tl")){        opts |= TDBTLARGE;      } else if(!strcmp(argv[i], "-td")){        opts |= TDBTDEFLATE;      } else if(!strcmp(argv[i], "-tb")){        opts |= TDBTBZIP;      } else if(!strcmp(argv[i], "-tt")){        opts |= TDBTTCBS;      } else if(!strcmp(argv[i], "-tx")){        opts |= TDBTEXCODEC;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!bstr){      bstr = argv[i];    } else if(!astr){      astr = argv[i];    } else if(!fstr){      fstr = argv[i];    } else {      usage();    }  }  if(!path) usage();  int bnum = bstr ? tcatoix(bstr) : -1;  int apow = astr ? tcatoix(astr) : -1;  int fpow = fstr ? tcatoix(fstr) : -1;  int rv = proccreate(path, bnum, apow, fpow, opts);  return rv;}/* parse arguments of inform command */static int runinform(int argc, char **argv){  char *path = NULL;  int omode = 0;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-nl")){        omode |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else {      usage();    }  }  if(!path) usage();  int rv = procinform(path, omode);  return rv;}/* parse arguments of put command */static int runput(int argc, char **argv){  char *path = NULL;  char *pkey = NULL;  TCLIST *vals = tcmpoollistnew(tcmpoolglobal());  int omode = 0;  int dmode = 0;  bool sx = false;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-nl")){        omode |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else if(!strcmp(argv[i], "-dk")){        dmode = -1;      } else if(!strcmp(argv[i], "-dc")){        dmode = 1;      } else if(!strcmp(argv[i], "-dai")){        dmode = 10;      } else if(!strcmp(argv[i], "-dad")){        dmode = 11;      } else if(!strcmp(argv[i], "-sx")){        sx = true;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!pkey){      pkey = argv[i];    } else {      tclistpush2(vals, argv[i]);    }  }  if(!path || !pkey) usage();  TCMAP *cols = tcmapnew();  char *pkbuf;  int pksiz;  if(sx){    pkbuf = tchexdecode(pkey, &pksiz);    for(int i = 0; i < tclistnum(vals) - 1; i += 2){      const char *name = tclistval2(vals, i);      const char *value = tclistval2(vals, i + 1);      int nsiz;      char *nbuf = tchexdecode(name, &nsiz);      int vsiz;      char *vbuf = tchexdecode(value, &vsiz);      tcmapput(cols, nbuf, nsiz, vbuf, vsiz);      tcfree(vbuf);      tcfree(nbuf);    }  } else {    pksiz = strlen(pkey);    pkbuf = tcmemdup(pkey, pksiz);    for(int i = 0; i < tclistnum(vals) - 1; i += 2){      const char *name = tclistval2(vals, i);      const char *value = tclistval2(vals, i + 1);      tcmapput2(cols, name, value);    }  }  int rv = procput(path, pkbuf, pksiz, cols, omode, dmode);  tcmapdel(cols);  tcfree(pkbuf);  return rv;}/* parse arguments of out command */static int runout(int argc, char **argv){  char *path = NULL;  char *pkey = NULL;  int omode = 0;  bool sx = false;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-nl")){        omode |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else if(!strcmp(argv[i], "-sx")){        sx = true;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!pkey){      pkey = argv[i];    } else {      usage();    }  }  if(!path || !pkey) usage();  int pksiz;  char *pkbuf;  if(sx){    pkbuf = tchexdecode(pkey, &pksiz);  } else {    pksiz = strlen(pkey);    pkbuf = tcmemdup(pkey, pksiz);  }  int rv = procout(path, pkbuf, pksiz, omode);  tcfree(pkbuf);  return rv;}/* parse arguments of get command */static int runget(int argc, char **argv){  char *path = NULL;  char *pkey = NULL;  int omode = 0;  bool sx = false;  bool px = false;  bool pz = false;  for(int i = 2; i < argc; i++){    if(!path && argv[i][0] == '-'){      if(!strcmp(argv[i], "-nl")){        omode |= TDBONOLCK;      } else if(!strcmp(argv[i], "-nb")){        omode |= TDBOLCKNB;      } else if(!strcmp(argv[i], "-sx")){        sx = true;      } else if(!strcmp(argv[i], "-px")){        px = true;      } else if(!strcmp(argv[i], "-pz")){        pz = true;      } else {        usage();      }    } else if(!path){      path = argv[i];    } else if(!pkey){      pkey = argv[i];    } else {      usage();

⌨️ 快捷键说明

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