static void check_records(TCLIST *recs, struct keygen *keygen, int vsiz, int batch) { int i; int recnum; if (!debug) return; if (!recs && batch > 0) die("No records returned"); recnum = tclistnum(recs); if (recnum != batch * 2) die("Unexpected list size %d", recnum); for (i = 0; i < recnum; i += 2) { int keysiz; const char *key = tclistval(recs, i, &keysiz); int valsiz; if (strncmp(keygen_next_key(keygen), key, keysiz)) die("Unexpected key"); tclistval(recs, i + 1, &valsiz); if (valsiz != vsiz) die("Unexpected value size %d", valsiz); } }
static void tclist2hash(lua_State *L, TCLIST *list){ int num = tclistnum(list); lua_createtable(L, 0, num/2); int i; for(i=0; i<num-1; i+=2){ int ksize, vsize; const char *key = tclistval(list, i, &ksize); const char *value = tclistval(list, i+1, &vsize); lua_pushlstring(L, value, vsize); lua_setfield(L, -2, key); } }
/* * /logout/ * * HTML is in templates/logout.tmpl * * Clean up a users session. Remove their entry from the sessions db and * set the session_id browser cookie to expired. */ static void logout(void) { TCTDB *tdb; TDBQRY *qry; TCLIST *res; int rsize; const char *rbuf; tdb = tctdbnew(); tctdbopen(tdb, SESSION_DB, TDBOWRITER); qry = tctdbqrynew(tdb); tctdbqryaddcond(qry, "session_id", TDBQCSTREQ, user_session.session_id); res = tctdbqrysearch(qry); rbuf = tclistval(res, 0, &rsize); tctdbout(tdb, rbuf, strlen(rbuf)); tclistdel(res); tctdbqrydel(qry); tctdbclose(tdb); tctdbdel(tdb); /* Immediately expire the session cookies */ printf("Set-Cookie: session_id=deleted; " "expires=Thu, 01 Jan 1970 00:00:01 GMT; " "path=/; httponly\r\n"); send_template("templates/logout.tmpl", NULL, NULL); }
/* metasearch */ JNIEXPORT jobject JNICALL Java_tokyocabinet_TDBQRY_metasearch (JNIEnv *env, jobject self, jobjectArray others, jint type){ TDBQRY *qry = (TDBQRY *)(intptr_t)(*env)->GetLongField(env, self, tdbqry_fid_ptr); int onum = (*env)->GetArrayLength(env, others); TDBQRY *qrys[onum+1]; int qnum = 0; qrys[qnum++] = qry; jclass clsqry = (*env)->GetObjectClass(env, self); for(int i = 0; i < onum; i++){ jobject oqry = (*env)->GetObjectArrayElement(env, others, i); if((*env)->IsInstanceOf(env, oqry, clsqry)) qrys[qnum++] = (TDBQRY *)(intptr_t)(*env)->GetLongField(env, oqry, tdbqry_fid_ptr); } TCLIST *tkeys = tctdbmetasearch(qrys, qnum, type); jclass clslist = (*env)->FindClass(env, CLSARRAYLIST); jmethodID midinit = (*env)->GetMethodID(env, clslist, "<init>", "()V"); jobject pkeys = (*env)->NewObject(env, clslist, midinit); jmethodID midadd = (*env)->GetMethodID(env, clslist, "add", "(L" CLSOBJECT ";)Z"); for(int i = 0; i < tclistnum(tkeys); i++){ int ksiz; const char *kbuf = tclistval(tkeys, i, &ksiz); jbyteArray pkey = (*env)->NewByteArray(env, ksiz); (*env)->SetByteArrayRegion(env, pkey, 0, ksiz, (jbyte *)kbuf); (*env)->CallVoidMethod(env, pkeys, midadd, pkey); (*env)->DeleteLocalRef(env, pkey); } tclistdel(tkeys); return pkeys; }
/* fwmkeys */ JNIEXPORT jobject JNICALL Java_tokyocabinet_BDB_fwmkeys (JNIEnv *env, jobject self, jbyteArray prefix, jint max){ if(!prefix){ throwillarg(env); return NULL; } TCBDB *bdb = (TCBDB *)(intptr_t)(*env)->GetLongField(env, self, bdb_fid_ptr); jboolean icp; jbyte *pbuf = (*env)->GetByteArrayElements(env, prefix, &icp); if(!pbuf){ throwoutmem(env); return NULL; } int psiz = (*env)->GetArrayLength(env, prefix); TCLIST *tkeys = tcbdbfwmkeys(bdb, pbuf, psiz, max); jclass clslist = (*env)->FindClass(env, CLSARRAYLIST); jmethodID midinit = (*env)->GetMethodID(env, clslist, "<init>", "()V"); jobject keys = (*env)->NewObject(env, clslist, midinit); jmethodID midadd = (*env)->GetMethodID(env, clslist, "add", "(L" CLSOBJECT ";)Z"); for(int i = 0; i < tclistnum(tkeys); i++){ int ksiz; const char *kbuf = tclistval(tkeys, i, &ksiz); jbyteArray key = (*env)->NewByteArray(env, ksiz); (*env)->SetByteArrayRegion(env, key, 0, ksiz, (jbyte *)kbuf); (*env)->CallVoidMethod(env, keys, midadd, key); (*env)->DeleteLocalRef(env, key); } tclistdel(tkeys); (*env)->ReleaseByteArrayElements(env, prefix, pbuf, JNI_ABORT); return keys; }
static int _sandbox_notify(const char *type, void *param1, void *param2, void *data, TCMAP *map) { TCLIST **p, *list; NOTIFY_HANDLER *handler; int sp, idx, ret = 0; if (!type) return ret; //DEBUG_PRINT("sandbox_notify(): type=%s\n", type); p = (TCLIST **)tcmapget(map, type, strlen(type), &sp); if (p) { list = *p; //free(p); idx = 0; while (idx < tclistnum(list)) { handler = (NOTIFY_HANDLER *)tclistval(list, idx, &sp); if (handler) { //DEBUG_PRINT("sandbox_notify(): type=%s, handler=%X, s=%d\n", type, *handler, sizeof(handler)); ret = (*handler)(type, param1, param2, data); if (ret) break; } idx++; } } return ret; }
static PyObject *tc_TDBQuery_keys(tc_TDBQuery *self) { log_trace("ENTER"); TCLIST *res; PyObject *pylist, *key; const char *pkbuf; int pksiz, i; res = tctdbqrysearch(self->qry); if ( (pylist = PyList_New( (Py_ssize_t)TCLISTNUM(res) )) == NULL ) { tclistdel(res); return NULL; } for(i = 0; i < TCLISTNUM(res); i++){ pkbuf = tclistval(res, i, &pksiz); if ( (key = PyBytes_FromStringAndSize(pkbuf, pksiz)) == NULL ) { Py_CLEAR(pylist); break; } PyList_SET_ITEM(pylist, i, key); } tclistdel(res); return pylist; }
/* perform tblread command */ int dotblread(char *name, int rnum){ TCTDB *tdb; int i, j, err, pksiz, rsiz; char pkbuf[RECBUFSIZ]; const char *rbuf; TCMAP *cols; TDBQRY *qry; TCLIST *res; if(showprgr) printf("<Reading Test of Table>\n name=%s rnum=%d\n\n", name, rnum); /* open a database */ tdb = tctdbnew(); tctdbsetxmsiz(tdb, rnum * 80); tctdbsetcache(tdb, -1, rnum / 100, -1); if(!tctdbopen(tdb, name, TDBOREADER)){ fprintf(stderr, "tctdbopen failed\n"); tctdbdel(tdb); return 1; } err = FALSE; /* loop for each record */ for(i = 1; i <= rnum; i++){ /* search for a record */ pksiz = sprintf(pkbuf, "%08d", i); qry = tctdbqrynew(tdb); tctdbqryaddcond(qry, "s", TDBQCSTREQ, pkbuf); res = tctdbqrysearch(qry); for(j = 0; j < tclistnum(res); j++){ rbuf = tclistval(res, j, &rsiz); cols = tctdbget(tdb, rbuf, rsiz); if(cols){ tcmapdel(cols); } else { fprintf(stderr, "tctdbget failed\n"); err = TRUE; break; } } tclistdel(res); tctdbqrydel(qry); /* 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; }
void grok_discover_init(grok_discover_t *gdt, grok_t *source_grok) { TCLIST *names = NULL; int i = 0, len = 0; if (dgrok_init == 0) { grok_discover_global_init(); } gdt->complexity_tree = tctreenew2(tccmpint32, NULL); gdt->base_grok = source_grok; gdt->logmask = source_grok->logmask; gdt->logdepth = source_grok->logdepth; names = grok_pattern_name_list(source_grok); len = tclistnum(names); /* for each pattern, create a grok. * Sort by complexity. * loop * for each pattern, try replacement * if no replacements, break */ for (i = 0; i < len; i++) { int namelen = 0; const char *name = tclistval(names, i, &namelen); int *key = malloc(sizeof(int)); grok_t *g = grok_new(); grok_clone(g, source_grok); char *gpattern; //if (asprintf(&gpattern, "%%{%.*s =~ /\\b/}", namelen, name) == -1) { if (asprintf(&gpattern, "%%{%.*s}", namelen, name) == -1) { perror("asprintf failed"); abort(); } grok_compile(g, gpattern); *key = complexity(g); /* Low complexity should be skipped */ if (*key > -20) { free((void *)g->pattern); free(key); grok_free_clone(g); free(g); continue; } *key *= 1000; /* Inflate so we can insert duplicates */ grok_log(gdt, LOG_DISCOVER, "Including pattern: (complexity: %d) %.*s", *(int *)key, namelen, name); while (!tctreeputkeep(gdt->complexity_tree, key, sizeof(int), g, sizeof(grok_t))) { *key--; } //grok_free_clone(g); //free(key); } tclistdel(names); }
/* perform list command */ static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *rlstr, const char *rustr, const char *ristr){ TCFDB *fdb = tcfdbnew(); if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd); if(!tcfdbopen(fdb, path, FDBOREADER | omode)){ printerr(fdb); tcfdbdel(fdb); return 1; } bool err = false; if(rlstr || ristr){ TCLIST *keys = ristr ? tcfdbrange5(fdb, ristr, max) : tcfdbrange3(fdb, rlstr, rustr, max); for(int i = 0; i < tclistnum(keys); i++){ int ksiz; const char *kbuf = tclistval(keys, i, &ksiz); printf("%s", kbuf); if(pv){ int vsiz; char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); if(vbuf){ putchar('\t'); printdata(vbuf, vsiz, px); tcfree(vbuf); } } putchar('\n'); } tclistdel(keys); } else { if(!tcfdbiterinit(fdb)){ printerr(fdb); err = true; } int cnt = 0; uint64_t id; while((id = tcfdbiternext(fdb)) > 0){ printf("%llu", (unsigned long long)id); if(pv){ int vsiz; char *vbuf = tcfdbget(fdb, id, &vsiz); if(vbuf){ putchar('\t'); printdata(vbuf, vsiz, px); tcfree(vbuf); } } putchar('\n'); if(max >= 0 && ++cnt >= max) break; } } if(!tcfdbclose(fdb)){ if(!err) printerr(fdb); err = true; } tcfdbdel(fdb); return err ? 1 : 0; }
bool get(Value & value, int index) { int size = 0; const void * p = tclistval( list_, index, &size ); if (p == NULL) return false; ser::assign(value, p, size); return true; }
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); }
/* * This checks if a user is currently logged in. It is called at the start * of each request. * * There are upto three checks performed: * * 1) The session_id cookie from the browser is checked with the stored * session_id generated at login. * 2) The client_id from the browser (currently the user agent string) is * checked against the stored client_id. * * 4) Optionally (enabled by default on the login screen) a check is made * on the requesting ip address against the stored origin_ip that was * used at login. * * If any of these checks fail, the request is denied and the user is * punted to the login screen. */ bool is_logged_in(void) { char session_id[SID_LEN + 1]; TCTDB *tdb; TDBQRY *qry; TCLIST *res; TCMAP *cols; int rsize; const char *rbuf; bool login_ok = false; if (!env_vars.http_cookie) goto out3; snprintf(session_id, sizeof(session_id), "%s", env_vars.http_cookie + 11); tdb = tctdbnew(); tctdbopen(tdb, SESSION_DB, TDBOREADER); qry = tctdbqrynew(tdb); tctdbqryaddcond(qry, "session_id", TDBQCSTREQ, session_id); res = tctdbqrysearch(qry); if (tclistnum(res) == 0) goto out2; rbuf = tclistval(res, 0, &rsize); cols = tctdbget(tdb, rbuf, rsize); tcmapiterinit(cols); /* restrict_ip */ if (atoi(tcmapget2(cols, "restrict_ip")) == 1) { /* origin_ip */ if (strcmp(tcmapget2(cols, "origin_ip"), env_vars.remote_addr) != 0) goto out; } /* client_id */ if (strcmp(tcmapget2(cols, "client_id"), env_vars.http_user_agent) != 0) goto out; /* We got here, all checks are OK */ login_ok = true; out: tcmapdel(cols); out2: tctdbqrydel(qry); tclistdel(res); tctdbclose(tdb); tctdbdel(tdb); out3: return login_ok; }
/* perform list command */ static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *fmstr){ TCHDB *hdb = tchdbnew(); if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd); if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb); if(!tchdbopen(hdb, path, HDBOREADER | omode)){ printerr(hdb); tchdbdel(hdb); return 1; } bool err = false; if(fmstr){ TCLIST *keys = tchdbfwmkeys2(hdb, fmstr, max); for(int i = 0; i < tclistnum(keys); i++){ int ksiz; const char *kbuf = tclistval(keys, i, &ksiz); printdata(kbuf, ksiz, px); if(pv){ int vsiz; char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz); if(vbuf){ putchar('\t'); printdata(vbuf, vsiz, px); tcfree(vbuf); } } putchar('\n'); } tclistdel(keys); } else { if(!tchdbiterinit(hdb)){ printerr(hdb); err = true; } TCXSTR *key = tcxstrnew(); TCXSTR *val = tcxstrnew(); int cnt = 0; while(tchdbiternext3(hdb, key, val)){ printdata(tcxstrptr(key), tcxstrsize(key), px); if(pv){ putchar('\t'); printdata(tcxstrptr(val), tcxstrsize(val), px); } putchar('\n'); if(max >= 0 && ++cnt >= max) break; } tcxstrdel(val); tcxstrdel(key); } if(!tchdbclose(hdb)){ if(!err) printerr(hdb); err = true; } tchdbdel(hdb); return err ? 1 : 0; }
extern VALUE listtovary(TCLIST *list){ VALUE vary; const char *vbuf; int i, num, vsiz; num = tclistnum(list); vary = rb_ary_new2(num); for(i = 0; i < num; i++){ vbuf = tclistval(list, i, &vsiz); rb_ary_push(vary, rb_str_new(vbuf, vsiz)); } return vary; }
static void tclist2array(lua_State *L, TCLIST *list){ int num = tclistnum(list); lua_createtable(L, num, 0); int i; for(i=0; i<num; i++){ int size; const char *buf = tclistval(list, i, &size); lua_pushlstring(L, buf, size); lua_rawseti(L, -2, i + 1); } }
/* range */ JNIEXPORT jobject JNICALL Java_tokyocabinet_BDB_range (JNIEnv *env, jobject self, jbyteArray bkey, jboolean binc, jbyteArray ekey, jboolean einc, jint max){ TCBDB *bdb = (TCBDB *)(intptr_t)(*env)->GetLongField(env, self, bdb_fid_ptr); jboolean icbk; jbyte *bkbuf; int bksiz; if(bkey){ bkbuf = (*env)->GetByteArrayElements(env, bkey, &icbk); if(!bkbuf){ throwoutmem(env); return NULL; } icbk = false; bksiz = (*env)->GetArrayLength(env, bkey); } else { bkbuf = NULL; bksiz = -1; } jboolean icek; jbyte *ekbuf; int eksiz; if(ekey){ ekbuf = (*env)->GetByteArrayElements(env, ekey, &icek); if(!ekbuf){ throwoutmem(env); return NULL; } icek = false; eksiz = (*env)->GetArrayLength(env, ekey); } else { ekbuf = NULL; eksiz = -1; } TCLIST *tkeys = tcbdbrange(bdb, bkbuf, bksiz, binc, ekbuf, eksiz, einc, max); jclass clslist = (*env)->FindClass(env, CLSARRAYLIST); jmethodID midinit = (*env)->GetMethodID(env, clslist, "<init>", "()V"); jobject keys = (*env)->NewObject(env, clslist, midinit); jmethodID midadd = (*env)->GetMethodID(env, clslist, "add", "(L" CLSOBJECT ";)Z"); for(int i = 0; i < tclistnum(tkeys); i++){ int ksiz; const char *kbuf = tclistval(tkeys, i, &ksiz); jbyteArray key = (*env)->NewByteArray(env, ksiz); (*env)->SetByteArrayRegion(env, key, 0, ksiz, (jbyte *)kbuf); (*env)->CallVoidMethod(env, keys, midadd, key); (*env)->DeleteLocalRef(env, key); } tclistdel(tkeys); (*env)->ReleaseByteArrayElements(env, ekey, ekbuf, JNI_ABORT); (*env)->ReleaseByteArrayElements(env, bkey, bkbuf, JNI_ABORT); return keys; }
extern TCMAP *varytomap(VALUE vary){ int i; TCLIST *keys; TCMAP *recs = tcmapnew(); keys = varytolist(vary); for(i = 0; i < tclistnum(keys); i++){ int ksiz; const char *kbuf = tclistval(keys, i, &ksiz); tcmapput(recs, kbuf, ksiz, "", 0); } tclistdel(keys); return recs; }
const grok_capture *grok_capture_get_by_subname(const grok_t *grok, const char *subname) { int unused_size; const grok_capture *gct; const TCLIST *by_subname_list; by_subname_list = tctreeget(grok->captures_by_subname, subname, strlen(subname), &unused_size); if (by_subname_list == NULL) return NULL; gct = tclistval(by_subname_list, 0, &unused_size); return gct; }
const grok_capture *grok_capture_get_by_name(const grok_t *grok, const char *name) { int unused_size; const grok_capture *gct; const TCLIST *by_name_list; by_name_list = tctreeget(grok->captures_by_name, name, strlen(name), &unused_size); if (by_name_list == NULL) return NULL; /* return the first capture by this name in the list */ gct = tclistval(by_name_list, 0, &unused_size); return gct; }
/* perform misc command */ static int procmisc(const char *name, const char *func, const TCLIST *args, int sep, bool px){ TCADB *adb = tcadbnew(); ADBSKEL skel; if(*name == '@'){ setskeltran(&skel); if(!tcadbsetskel(adb, &skel)){ printerr(adb); skel.del(skel.opq); tcadbdel(adb); return 1; } name++; } else if(*name == '%'){ if(!tcadbsetskelmulti(adb, 8)){ printerr(adb); tcadbdel(adb); return 1; } name++; } if(!tcadbopen(adb, name)){ printerr(adb); tcadbdel(adb); return 1; } bool err = false; TCLIST *res = tcadbmisc(adb, func, args); if(res){ for(int i = 0; i < tclistnum(res); i++){ int rsiz; const char *rbuf = tclistval(res, i, &rsiz); printdata(rbuf, rsiz, px, sep); printf("\n"); } tclistdel(res); } else { printerr(adb); err = true; } if(!tcadbclose(adb)){ if(!err) printerr(adb); err = true; } tcadbdel(adb); return err ? 1 : 0; }
GSLList * tclist_to_gsllist (TCLIST * tclist) { GSLList *list = NULL; int i, sz; int *val; for (i = 0; i < tclistnum (tclist); ++i) { val = (int *) tclistval (tclist, i, &sz); if (list == NULL) list = list_create (int2ptr (*val)); else list = list_insert_prepend (list, int2ptr (*val)); } return list; }
static int is_value_in_tclist (TCLIST * tclist, void *value) { int i, sz; int *val; if (!tclist) return 0; for (i = 0; i < tclistnum (tclist); ++i) { val = (int *) tclistval (tclist, i, &sz); if (find_host_agent_in_list (value, val)) return 1; } return 0; }
/* Remove a record of a q-gram database object. `wdb' specifies the q-gram database object. `id' specifies the ID number of the record. `words' specifies a list object contains the words of the record. If successful, the return value is true, else, it is false. */ static bool tcwdboutimpl(TCWDB *wdb, int64_t id, const TCLIST *words){ assert(wdb && id > 0 && words); char idbuf[TDNUMBUFSIZ*2]; int idsiz; TDSETVNUMBUF64(idsiz, idbuf, id); TCMAP *dtokens = wdb->dtokens; int wn = tclistnum(words); for(int i = 0; i < wn; i++){ int wsiz; const char *word = tclistval(words, i, &wsiz); if(*word != '\0') tcmapputkeep(dtokens, word, wsiz, "", 0); } tcidsetmark(wdb->dids, id); bool err = false; if(tcmapmsiz(dtokens) >= wdb->icsiz && !tcwdbmemsync(wdb, 1)) err = true; return !err; }
/* search */ JNIEXPORT jobject JNICALL Java_tokyocabinet_TDBQRY_search (JNIEnv *env, jobject self){ TDBQRY *qry = (TDBQRY *)(intptr_t)(*env)->GetLongField(env, self, tdbqry_fid_ptr); TCLIST *tkeys = tctdbqrysearch(qry); jclass clslist = (*env)->FindClass(env, CLSARRAYLIST); jmethodID midinit = (*env)->GetMethodID(env, clslist, "<init>", "()V"); jobject pkeys = (*env)->NewObject(env, clslist, midinit); jmethodID midadd = (*env)->GetMethodID(env, clslist, "add", "(L" CLSOBJECT ";)Z"); for(int i = 0; i < tclistnum(tkeys); i++){ int ksiz; const char *kbuf = tclistval(tkeys, i, &ksiz); jbyteArray pkey = (*env)->NewByteArray(env, ksiz); (*env)->SetByteArrayRegion(env, pkey, 0, ksiz, (jbyte *)kbuf); (*env)->CallVoidMethod(env, pkeys, midadd, pkey); (*env)->DeleteLocalRef(env, pkey); } tclistdel(tkeys); return pkeys; }
void grok_matchconfig_close(grok_program_t *gprog, grok_matchconf_t *gmc) { int ret = 0; if (gmc->shellinput != NULL) { /* Don't close stdout */ if (gmc->shellinput != stdout) { ret = fclose(gmc->shellinput); grok_log(gprog, LOG_PROGRAM, "Closing matchconf shell. fclose() = %d", ret); } gmc->shellinput = NULL; } int i = 0; for (i = 0; i < tclistnum(gmc->grok_list); i++) { int len; grok_t *grok = (grok_t *) tclistval(gmc->grok_list, i, &len); grok_free(grok); } tclistdel(gmc->grok_list); }
/* Store a record into a q-gram database object. `wdb' specifies the q-gram database object. `id' specifies the ID number of the record. `words' specifies a list object contains the words of the record. If successful, the return value is true, else, it is false. */ static bool tcwdbputimpl(TCWDB *wdb, int64_t id, const TCLIST *words){ assert(wdb && id > 0 && words); char idbuf[TDNUMBUFSIZ*2]; int idsiz; TDSETVNUMBUF64(idsiz, idbuf, id); TCMAP *cc = wdb->cc; int wn = tclistnum(words); TCMAP *uniq = tcmapnew2(wn + 1); for(int i = 0; i < wn; i++){ int wsiz; const char *word = tclistval(words, i, &wsiz); if(!tcmapputkeep(uniq, word, wsiz, "", 0)) continue; if(*word != '\0') tcmapputcat(cc, word, wsiz, idbuf, idsiz); } tcmapdel(uniq); bool err = false; if(tcmapmsiz(cc) >= wdb->icsiz && !tcwdbmemsync(wdb, 1)) err = true; return !err; }
void grok_matchconfig_exec(grok_program_t *gprog, grok_input_t *ginput, const char *text) { grok_match_t gm; grok_matchconf_t *gmc; int i = 0; int want_break = 0; for (i = 0; i < gprog->nmatchconfigs; i++) { int ret; gmc = &gprog->matchconfigs[i]; int num_groks = tclistnum(gmc->grok_list); int i = 0; if (gmc->is_nomatch) { continue; } for (i = 0; i < num_groks; i++) { grok_t *grok; int unused_len; grok = (grok_t *)tclistval(gmc->grok_list, i, &unused_len); grok_log(gprog, LOG_PROGRAM, "Trying match against pattern %d: %.*s", i, grok->pattern_len, grok->pattern); ret = grok_exec(grok, text, &gm); if (ret == GROK_OK) { grok_matchconfig_react(gprog, ginput, gmc, &gm); if (!gmc->no_reaction) { gprog->reactions += 1; } if (gmc->break_if_match) { want_break = 1; break; } } } if (want_break) { break; } } }
static void check_keys(TCLIST *list, int num, unsigned int seed) { int i; struct keygen keygen; if (!debug) return; keygen_init(&keygen, seed); if (num != tclistnum(list)) die("Unexpected key num: %d", tclistnum(list)); for (i = 0; i < num; i++) { int ksiz; const char *key = tclistval(list, i, &ksiz); if (strncmp(keygen_next_key(&keygen), key, ksiz)) die("Unexpected key"); } }
/* getlist */ JNIEXPORT jobject JNICALL Java_tokyocabinet_BDB_getlist (JNIEnv *env, jobject self, jbyteArray key){ if(!key){ throwillarg(env); return NULL; } TCBDB *bdb = (TCBDB *)(intptr_t)(*env)->GetLongField(env, self, bdb_fid_ptr); jboolean ick; jbyte *kbuf = (*env)->GetByteArrayElements(env, key, &ick); if(!kbuf){ throwoutmem(env); return NULL; } int ksiz = (*env)->GetArrayLength(env, key); TCLIST *tvals = tcbdbget4(bdb, kbuf, ksiz); jobject vals; if(tvals){ jclass clslist = (*env)->FindClass(env, CLSARRAYLIST); jmethodID midinit = (*env)->GetMethodID(env, clslist, "<init>", "()V"); vals = (*env)->NewObject(env, clslist, midinit); jmethodID midadd = (*env)->GetMethodID(env, clslist, "add", "(L" CLSOBJECT ";)Z"); for(int i = 0; i < tclistnum(tvals); i++){ int vsiz; const char *vbuf = tclistval(tvals, i, &vsiz); jbyteArray val = (*env)->NewByteArray(env, vsiz); (*env)->SetByteArrayRegion(env, val, 0, vsiz, (jbyte *)vbuf); (*env)->CallVoidMethod(env, vals, midadd, val); (*env)->DeleteLocalRef(env, val); } tclistdel(tvals); } else { vals = NULL; } (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT); return vals; }