📄 tctmgr.c
字号:
} } if(!path || !pkey) usage(); int pksiz; char *pkbuf; if(sx){ pkbuf = tchexdecode(pkey, &pksiz); } else { pksiz = strlen(pkey); pkbuf = tcmemdup(pkey, pksiz); } int rv = procget(path, pkbuf, pksiz, omode, px, pz); tcfree(pkbuf); return rv;}/* parse arguments of list command */static int runlist(int argc, char **argv){ char *path = NULL; int omode = 0; int max = -1; bool pv = false; bool px = false; char *fmstr = NULL; 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], "-m")){ if(++i >= argc) usage(); max = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-pv")){ pv = true; } else if(!strcmp(argv[i], "-px")){ px = true; } else if(!strcmp(argv[i], "-fm")){ if(++i >= argc) usage(); fmstr = argv[i]; } else { usage(); } } else if(!path){ path = argv[i]; } else { usage(); } } if(!path) usage(); int rv = proclist(path, omode, max, pv, px, fmstr); return rv;}/* parse arguments of search command */static int runsearch(int argc, char **argv){ char *path = NULL; TCLIST *conds = tcmpoollistnew(tcmpoolglobal()); char *oname = NULL; char *otype = NULL; int omode = 0; int max = -1; int skip = -1; bool pv = false; bool px = false; bool ph = false; int bt = 0; bool rm = 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], "-ord")){ if(++i >= argc) usage(); oname = argv[i]; if(++i >= argc) usage(); otype = argv[i]; } else if(!strcmp(argv[i], "-m")){ if(++i >= argc) usage(); max = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-sk")){ if(++i >= argc) usage(); skip = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-pv")){ pv = true; } else if(!strcmp(argv[i], "-px")){ px = true; } else if(!strcmp(argv[i], "-ph")){ ph = true; } else if(!strcmp(argv[i], "-bt")){ if(++i >= argc) usage(); bt = tcatoix(argv[i]); } else if(!strcmp(argv[i], "-rm")){ rm = true; } else { usage(); } } else if(!path){ path = argv[i]; } else { tclistpush2(conds, argv[i]); } } if(!path || tclistnum(conds) % 3 != 0) usage(); int rv = procsearch(path, conds, oname, otype, omode, max, skip, pv, px, ph, bt, rm); return rv;}/* parse arguments of optimize command */static int runoptimize(int argc, char **argv){ char *path = NULL; char *bstr = NULL; char *astr = NULL; char *fstr = NULL; int opts = UINT8_MAX; int omode = 0; for(int i = 2; i < argc; i++){ if(!path && argv[i][0] == '-'){ if(!strcmp(argv[i], "-tl")){ if(opts == UINT8_MAX) opts = 0; opts |= TDBTLARGE; } else if(!strcmp(argv[i], "-td")){ if(opts == UINT8_MAX) opts = 0; opts |= TDBTDEFLATE; } else if(!strcmp(argv[i], "-tb")){ if(opts == UINT8_MAX) opts = 0; opts |= TDBTBZIP; } else if(!strcmp(argv[i], "-tt")){ if(opts == UINT8_MAX) opts = 0; opts |= TDBTTCBS; } else if(!strcmp(argv[i], "-tx")){ if(opts == UINT8_MAX) opts = 0; opts |= TDBTEXCODEC; } else if(!strcmp(argv[i], "-tz")){ if(opts == UINT8_MAX) opts = 0; } else if(!strcmp(argv[i], "-nl")){ omode |= TDBONOLCK; } else if(!strcmp(argv[i], "-nb")){ omode |= TDBOLCKNB; } 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 = procoptimize(path, bnum, apow, fpow, opts, omode); return rv;}/* parse arguments of setindex command */static int runsetindex(int argc, char **argv){ char *path = NULL; char *name = NULL; int omode = 0; int type = TDBITLEXICAL; 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], "-it")){ if(++i >= argc) usage(); type = tctdbstrtoindextype(argv[i]); } else { usage(); } } else if(!path){ path = argv[i]; } else if(!name){ name = argv[i]; } else { usage(); } } if(!path || !name) usage(); if(type < 0) usage(); int rv = procsetindex(path, name, omode, type); return rv;}/* parse arguments of importtsv command */static int runimporttsv(int argc, char **argv){ char *path = NULL; char *file = NULL; int omode = 0; bool sc = 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], "-sc")){ sc = true; } else { usage(); } } else if(!path){ path = argv[i]; } else if(!file){ file = argv[i]; } else { usage(); } } if(!path) usage(); int rv = procimporttsv(path, file, omode, sc); return rv;}/* parse arguments of version command */static int runversion(int argc, char **argv){ int rv = procversion(); return rv;}/* perform create command */static int proccreate(const char *path, int bnum, int apow, int fpow, int opts){ TCTDB *tdb = tctdbnew(); if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd); if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb); if(!tctdbtune(tdb, bnum, apow, fpow, opts)){ printerr(tdb); tctdbdel(tdb); return 1; } if(!tctdbopen(tdb, path, TDBOWRITER | TDBOCREAT | TDBOTRUNC)){ printerr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(!tctdbclose(tdb)){ printerr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0;}/* perform inform command */static int procinform(const char *path, int omode){ TCTDB *tdb = tctdbnew(); if(g_dbgfd >= 0) tctdbsetdbgfd(tdb, g_dbgfd); tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL); if(!tctdbopen(tdb, path, TDBOREADER | omode)){ printerr(tdb); tctdbdel(tdb); return 1; } bool err = false; const char *npath = tctdbpath(tdb); if(!npath) npath = "(unknown)"; printf("path: %s\n", npath); printf("database type: table\n"); uint8_t flags = tctdbflags(tdb); printf("additional flags:"); if(flags & TDBFOPEN) printf(" open"); if(flags & TDBFFATAL) printf(" fatal"); printf("\n"); printf("bucket number: %llu\n", (unsigned long long)tctdbbnum(tdb)); if(tdb->hdb->cnt_writerec >= 0) printf("used bucket number: %lld\n", (long long)tctdbbnumused(tdb)); printf("alignment: %u\n", tctdbalign(tdb)); printf("free block pool: %u\n", tctdbfbpmax(tdb)); printf("index number: %d\n", tctdbinum(tdb)); TDBIDX *idxs = tdb->idxs; int inum = tdb->inum; for(int i = 0; i < inum; i++){ TDBIDX *idxp = idxs + i; switch(idxp->type){ case TDBITLEXICAL: printf(" name=%s, type=lexical, rnum=%lld, fsiz=%lld\n", idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); break; case TDBITDECIMAL: printf(" name=%s, type=decimal, rnum=%lld, fsiz=%lld\n", idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); break; } } printf("unique ID seed: %lld\n", (long long)tctdbuidseed(tdb)); printf("inode number: %lld\n", (long long)tctdbinode(tdb)); char date[48]; tcdatestrwww(tctdbmtime(tdb), INT_MAX, date); printf("modified time: %s\n", date); uint8_t opts = tctdbopts(tdb); printf("options:"); if(opts & TDBTLARGE) printf(" large"); if(opts & TDBTDEFLATE) printf(" deflate"); if(opts & TDBTBZIP) printf(" bzip"); if(opts & TDBTTCBS) printf(" tcbs"); if(opts & TDBTEXCODEC) printf(" excodec"); printf("\n"); printf("record number: %llu\n", (unsigned long long)tctdbrnum(tdb)); printf("file size: %llu\n", (unsigned long long)tctdbfsiz(tdb)); if(!tctdbclose(tdb)){ if(!err) printerr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0;}/* perform put command */static int procput(const char *path, const char *pkbuf, int pksiz, TCMAP *cols, int omode, int dmode){ 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; char pknumbuf[TCNUMBUFSIZ]; if(pksiz < 1){ pksiz = sprintf(pknumbuf, "%lld", (long long)tctdbgenuid(tdb)); pkbuf = pknumbuf; } const char *vbuf; switch(dmode){ case -1: if(!tctdbputkeep(tdb, pkbuf, pksiz, cols)){ printerr(tdb); err = true; } break; case 1: if(!tctdbputcat(tdb, pkbuf, pksiz, cols)){ printerr(tdb); err = true; } break; case 10: vbuf = tcmapget2(cols, "_num"); if(!vbuf) vbuf = "1"; if(tctdbaddint(tdb, pkbuf, pksiz, tcatoi(vbuf)) == INT_MIN){ printerr(tdb); err = true; } break; case 11: vbuf = tcmapget2(cols, "_num"); if(!vbuf) vbuf = "1.0"; if(isnan(tctdbadddouble(tdb, pkbuf, pksiz, tcatof(vbuf)))){ printerr(tdb); err = true; } break; default: if(!tctdbput(tdb, pkbuf, pksiz, cols)){ printerr(tdb); err = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -