Ejemplo n.º 1
0
static void getlist_test(void *db, const char *command, int num, int vsiz,
			int batch, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	struct keygen keygen_for_check;
	TCLIST *list = tclistnew();
	TCLIST *recs;
	int i;

	keygen_init(&keygen, seed);
	keygen_init(&keygen_for_check, seed);

	for (i = 0; i < num; i++) {
		tclistpush2(list, keygen_next_key(&keygen));

		if (tclistnum(list) >= batch) {
			recs = do_tcadbmisc(adb, command, list);
			check_records(recs, &keygen_for_check, vsiz,
					tclistnum(list));
			tclistdel(recs);
			tclistclear(list);
		}
	}
	if (tclistnum(list)) {
		recs = do_tcadbmisc(adb, command, list);
		check_records(recs, &keygen_for_check, vsiz, tclistnum(list));
		tclistdel(recs);
	}

	tclistdel(list);
}
Ejemplo n.º 2
0
static void putlist_test(void *db, const char *command, int num, int vsiz,
			int batch, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	char *value = xmalloc(vsiz);
	TCLIST *list = tclistnew();
	int i;

	keygen_init(&keygen, seed);

	for (i = 0; i < num; i++) {
		tclistpush2(list, keygen_next_key(&keygen));
		tclistpush(list, value, vsiz);

		if (tclistnum(list) / 2 >= batch) {
			tclistdel(do_tcadbmisc(adb, command, list));
			tclistclear(list);
		}
	}
	if (tclistnum(list))
		tclistdel(do_tcadbmisc(adb, command, list));

	tclistdel(list);
	free(value);
}
Ejemplo n.º 3
0
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);
	}
}
Ejemplo n.º 4
0
/* 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;
}
Ejemplo n.º 5
0
static VALUE cQuery_get(VALUE vself){
  int i, num, ksiz;
  const char *name, *col;
  VALUE vqry, vary, vcols;
  RDBQRY *qry;
  TCLIST *res;
  TCMAP *cols;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);

  res = tcrdbqrysearchget(qry);
  num = tclistnum(res);
  vary = rb_ary_new2(num);
  for(i = 0; i < num; i++){
    vcols = rb_hash_new();
    cols = tcrdbqryrescols(res, i);
    if(cols){
      tcmapiterinit(cols);
      while((name = tcmapiternext(cols, &ksiz)) != NULL){
        col = tcmapget2(cols, name);
        if (ksiz == 0) name = "__id";
        rb_hash_aset(vcols, ID2SYM(rb_intern(name)), rb_str_new2(col));
      }
    }
    tcmapdel(cols);
    rb_ary_push(vary, vcols);
  }
  tclistdel(res);
  return vary;
}
Ejemplo n.º 6
0
Archivo: sandbox.c Proyecto: titita/npa
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;
}
Ejemplo n.º 7
0
/* 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;
}
Ejemplo n.º 8
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);
}
Ejemplo n.º 9
0
/* 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;
}
Ejemplo n.º 10
0
/* 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;
}
Ejemplo n.º 11
0
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);
}
Ejemplo n.º 12
0
/*
 * 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;
}
Ejemplo n.º 13
0
int 
Server_react(Server *server, void *socket, zmq_msg_t *msg)
{
	size_t cmdlen, msglen, datalen;
	char *eol, *message, *command;
	TCLIST *args;
	Command *cmd;
	void *data = NULL;
	
	message = zmq_msg_data(msg);
	msglen = zmq_msg_size(msg);
	zmq_msg_close(msg);
	
	eol = strchr(message, '\n');
	
	if (eol) {
		cmdlen = 1 + (eol - message);
		command = (char *)malloc((cmdlen + 1) * sizeof(char));
		memset(command, 0, cmdlen + 1);
		memcpy(command, message, cmdlen);
		args = tcstrsplit(tcstrsqzspc(command), " \t");
		cmd = lookup_command(tclistval2(args, 0));
		if (cmd == NULL) {
			ERROR("protocol error: %s", command);
			send(socket, "-INVALID_COMMAND");
			return 0;
		}
		if (msglen > cmdlen) {
			/* data size will always be last arg */
			datalen = tcatoi((char *)tclistval2(args, tclistnum(args) - 1));
			if ((datalen + cmdlen) != msglen) {
				ERROR("error receiving data or invalid data size");
				send(socket, "-INCORRECT_DATA_SIZE");
				return 0;
			}
			data = malloc(sizeof(char) * (datalen + 1));
			if (data == NULL) {
				ERROR("out of memory");
				send(socket, "-OUT_OF_MEMORY");
				return 0;
			}
			memset(data + datalen, 0, 1);
			memcpy(data, message + cmdlen, datalen);
			DEBUG("data received: %s", data);
		}
		cmd->fn(server, socket, args, data);
		return 1;
	}
	else {
		ERROR("request missing eol");
		send(socket, "-MISSING_EOL");
	}

	return 0;
}
Ejemplo n.º 14
0
/* 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;
}
Ejemplo n.º 15
0
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");
	}
}
Ejemplo n.º 16
0
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);
	}
}
Ejemplo n.º 17
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;
}
Ejemplo n.º 18
0
/* 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;
}
Ejemplo n.º 19
0
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;
}
Ejemplo n.º 20
0
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);
	}
}
Ejemplo n.º 21
0
void parse_conf()
{
	FILE   *fp;
	TCLIST *data;
	TCMAP  *map; 
	
	char       *key, *val, buf[256];
	const char *value;
		
	fp = fopen("./conf/blog.conf", "r");
	if(!fp) exit(0) ;
	
	map = tcmapnew();
	memset(buf, 0, sizeof(buf));

	while ((fgets(buf, 255, fp)) != NULL)
	{
		trim(buf);
		if(strlen(buf) == 0) continue;
		if(buf[0] == '#') continue;
		
		data = explode("=", buf);
		if(tclistnum(data) == 2)
		{
			key = strdup(tclistval2(data, 0));
			val = strdup(tclistval2(data, 1));
			
			trim(key);
			trim(val);

			tcmapput(map, key, strlen(key), val, strlen(val));
			safe_free(key);
			safe_free(val);
		}
		
		tclistdel(data);
		memset(buf, 0, sizeof(buf));
	}
	fclose(fp);
	
	VALIDATE_INT("blog_page_size", value, conf.page.blog)
	VALIDATE_INT("admin_page_size", value, conf.page.admin)
	VALIDATE_INT("comment_page_size", value, conf.page.comment)

	VALIDATE_STRINT("db", value, conf.path)
	VALIDATE_STRINT("username", value, conf.username)
	VALIDATE_STRINT("password", value, conf.password)


	tcmapdel(map);
}
Ejemplo n.º 22
0
Archivo: sandbox.c Proyecto: titita/npa
static int sandbox_listen2(TCLIST *event_list, NOTIFY_HANDLER handler, void *listener)
{
	int idx;
	const char *type;

	if (!event_list || !handler)
		return -1;

	idx = 0;
	while (idx < tclistnum(event_list)) {
		type = tclistval2(event_list, idx);
		sandbox_listen(type, handler, listener);
		idx++;
	}

	return 0;
}
Ejemplo n.º 23
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;
}
Ejemplo n.º 24
0
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;
}
Ejemplo n.º 25
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;
}
Ejemplo n.º 26
0
/* 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;
}
Ejemplo n.º 27
0
static void rangeout_test(void *db, const char *command, int num, int vsiz,
			int batch, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	TCLIST *args = tclistnew();
	char start_key[KEYGEN_PREFIX_SIZE + 1];
	char max[100];
	char end_key[KEYGEN_PREFIX_SIZE + 1];
	char binc[2];

	keygen_init(&keygen, seed);

	keygen_prefix(&keygen, start_key);
	sprintf(max, "%d", batch);
	keygen_prefix(&keygen, end_key);
	end_key[KEYGEN_PREFIX_SIZE - 1] = '-' + 1;
	sprintf(binc, "0");

	tclistpush2(args, start_key);
	tclistpush2(args, max);
	tclistpush2(args, end_key);
	tclistpush2(args, binc);

	while (1) {
		TCLIST *recs;

		recs = do_tcadbmisc(adb, command, args);
		if (tclistnum(recs) == 0)
			break;

		if (debug) {
			const char *num_recs = tclistval2(recs, 0);
			num -= atoi(num_recs);
			if (num != 0 && atoi(num_recs) != batch)
				die("Unexpected number of records are deleted");
		}

		tclistdel(recs);
	}
	if (debug && num != 0)
		die("Unexpected number of records are deleted");

	tclistdel(args);
}
Ejemplo n.º 28
0
/* 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;
}
Ejemplo n.º 29
0
/* 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;
}
Ejemplo n.º 30
0
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);
}