/* perform create command */ static int proccreate(const char *dbpath, int scale, bool fts){ TCTDB *tdb = tctdbnew(); int bnum = (scale > 0) ? scale * 2 : TUNEBNUM; if(!tctdbtune(tdb, bnum, TUNEAPOW, TUNEFPOW, 0)){ printdberr(tdb); tctdbdel(tdb); return 1; } if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(!tctdbsetindex(tdb, "name", TDBITLEXICAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "cdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "mdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "xdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(fts && !tctdbsetindex(tdb, "text", TDBITQGRAM | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }
/* perform create command */ static int proccreate(const char *path, int bnum, int apow, int fpow, int opts){ TCTDB *tdb = tctdbnew(); if(g_dbgfd != INVALID_HANDLE_VALUE) 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 tblwrite command */ int dotblwrite(char *name, int rnum){ TCTDB *tdb; int i, err, pksiz, vsiz; char pkbuf[RECBUFSIZ], vbuf[RECBUFSIZ]; TCMAP *cols; if(showprgr) printf("<Writing Test of Table>\n name=%s rnum=%d\n\n", name, rnum); /* open a database */ tdb = tctdbnew(); tctdbtune(tdb, rnum * 3, 0, 0, 0); tctdbsetxmsiz(tdb, rnum * 80); tctdbsetcache(tdb, -1, rnum / 100, -1); if(!tctdbopen(tdb, name, TDBOWRITER | TDBOCREAT | TDBOTRUNC)){ fprintf(stderr, "tctdbopen failed\n"); tctdbdel(tdb); return 1; } if(!tctdbsetindex(tdb, "s", TDBITLEXICAL)){ fprintf(stderr, "tctdbsetindex failed\n"); tctdbdel(tdb); return 1; } if(!tctdbsetindex(tdb, "n", TDBITDECIMAL)){ fprintf(stderr, "tctdbsetindex failed\n"); tctdbdel(tdb); return 1; } err = FALSE; /* loop for each record */ for(i = 1; i <= rnum; i++){ /* store a record */ pksiz = sprintf(pkbuf, "%d", i); cols = tcmapnew2(7); vsiz = sprintf(vbuf, "%08d", i); tcmapput(cols, "s", 1, vbuf, vsiz); vsiz = sprintf(vbuf, "%08d", myrand() % i); tcmapput(cols, "n", 1, vbuf, vsiz); vsiz = sprintf(vbuf, "%08d", i); tcmapput(cols, "t", 1, vbuf, vsiz); vsiz = sprintf(vbuf, "%08d", myrand() % rnum); tcmapput(cols, "f", 1, vbuf, vsiz); if(!tctdbput(tdb, pkbuf, pksiz, cols)){ fprintf(stderr, "tctdbput failed\n"); err = TRUE; break; } tcmapdel(cols); /* print progression */ if(showprgr && rnum > 250 && i % (rnum / 250) == 0){ putchar('.'); fflush(stdout); if(i == rnum || i % (rnum / 10) == 0){ printf(" (%08d)\n", i); fflush(stdout); } } } /* close the database */ if(!tctdbclose(tdb)){ fprintf(stderr, "tctdbclose failed\n"); tctdbdel(tdb); return 1; } tctdbdel(tdb); if(showprgr && !err) printf("ok\n\n"); return err ? 1 : 0; }
/* perform import command */ static int procimport(const char *dbpath, TCLIST *files, TCLIST *sufs){ TCTDB *tdb = tctdbnew(); if(!tctdbtune(tdb, TUNEBNUM, TUNEAPOW, TUNEFPOW, 0)){ printdberr(tdb); tctdbdel(tdb); return 1; } if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(!tctdbsetindex(tdb, "name", TDBITLEXICAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "cdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "mdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "xdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } tclistinvert(files); char *fpath; while((fpath = tclistpop2(files)) != NULL){ TCLIST *cfiles = tcreaddir(fpath); if(cfiles){ tclistsort(cfiles); for(int i = tclistnum(cfiles) - 1; i >= 0; i--){ const char *cfile = tclistval2(cfiles, i); bool hit = false; for(int j = 0; j < tclistnum(sufs); j++){ if(tcstribwm(cfile, tclistval2(sufs, j))){ hit = true; break; } } if(!hit) continue; char *lpath = tcsprintf("%s/%s", fpath, cfile); tclistpush2(files, lpath); tcfree(lpath); } tclistdel(cfiles); } else { int isiz; char *ibuf = tcreadfile(fpath, IOMAXSIZ, &isiz); if(ibuf){ TCMAP *cols = tcmapnew2(TINYBNUM); wikiload(cols, ibuf); const char *name = tcmapget2(cols, "name"); if(name && *name != '\0'){ int64_t id = tcatoi(tcmapget4(cols, "id", "")); if(dbputart(tdb, id, cols)){ id = tcatoi(tcmapget4(cols, "id", "")); printf("%s: imported: id=%lld name=%s\n", fpath, (long long)id, name); } else { printdberr(tdb); err = true; } } else { printf("%s: ignored because there is no name\n", fpath); } tcmapdel(cols); tcfree(ibuf); } } tcfree(fpath); } if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }