static PyObject * search(PyObject *self, PyObject *args){ TCTDB *tdb; int ecode, i, rsiz; const char *rbuf, *name; TCMAP *cols; TDBQRY *qry; TCLIST *res; const char *dbname; const char *sfield; const char *stext; const int *max; PyObject* pDict = PyDict_New(); PyObject* pList = PyList_New(0); if (!PyArg_ParseTuple(args, "sssi", &dbname, &sfield, &stext,&max)) return NULL; tdb = tctdbnew(); if(!tctdbopen(tdb, dbname, TDBONOLCK | TDBOREADER)){ ecode = tctdbecode(tdb); fprintf(stderr, "open error: %s\n", tctdberrmsg(ecode)); } qry = tctdbqrynew(tdb); tctdbqryaddcond(qry, sfield, TDBQCSTREQ, stext); tctdbqrysetorder(qry, "savedate", TDBQOSTRDESC); tctdbqrysetlimit(qry, max, 0); res = tctdbqrysearch(qry); for(i = 0; i < tclistnum(res); i++){ rbuf = tclistval(res, i, &rsiz); cols = tctdbget(tdb, rbuf, rsiz); if(cols){ tcmapiterinit(cols); PyDict_SetItemString(pDict, "kid", Py_BuildValue("s",rbuf)); while((name = tcmapiternext2(cols)) != NULL){ PyDict_SetItemString(pDict, name, Py_BuildValue("s", tcmapget2(cols, name))); } PyList_Append(pList,pDict); pDict = PyDict_New(); tcmapdel(cols); } } tclistdel(res); tctdbqrydel(qry); if(!tctdbclose(tdb)){ ecode = tctdbecode(tdb); fprintf(stderr, "close error: %s\n", tctdberrmsg(ecode)); } tctdbdel(tdb); return Py_BuildValue("O",pList); }
static void _set_tdb_error(TCTDB *db) { log_trace("ENTER"); int ecode = tctdbecode(db); const char *msg = tctdberrmsg(ecode); if (ecode == TCENOREC) { PyErr_SetString(PyExc_KeyError, msg); } else { log_debug("TDB error: %s", msg); tc_Error_SetCodeAndString(ecode, msg); } }
/* 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; }
static void raise_table_error(TCTDB *db) { int code = tctdbecode(db); const char *msg = tctdberrmsg(code); if (code == TCENOREC) { PyErr_SetString(PyExc_KeyError, msg); } else { PyErr_SetString(TableError, msg); } }
/* 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)); }
/* 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; }
/* print error information */ static void printdberr(TCTDB *tdb){ const char *path = tctdbpath(tdb); int ecode = tctdbecode(tdb); eprintf("%s: %d: %s\n", path ? path : "-", ecode, tctdberrmsg(ecode)); }