// TODO: merge out and mout?
static VALUE mTokyoMessenger_outlist(int argc, VALUE *argv, VALUE vself){
  VALUE vkeys, vary, vvalue;
  TCLIST *list, *result;
  TCRDB *db = mTokyoMessenger_getdb(vself);
  rb_scan_args(argc, argv, "*", &vkeys);

  // I really hope there is a better way to do this
  if (RARRAY_LEN(vkeys) == 1) {
    vvalue = rb_ary_entry(vkeys, 0);
    switch (TYPE(vvalue)){
      case T_STRING:
      case T_FIXNUM:
        break;
      case T_ARRAY:
        vkeys = vvalue;
        break;
      case T_OBJECT:
        vkeys = rb_convert_type(vvalue, T_ARRAY, "Array", "to_a");
        break;
    }
  }
  Check_Type(vkeys, T_ARRAY);

  list = varytolist(vkeys);
  result = tcrdbmisc(db, "outlist", 0, list);
  tclistdel(list);
  vary = listtovary(result);
  tclistdel(result);
  return vary;
}
示例#2
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);
}
示例#3
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);
}
示例#4
0
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;
}
示例#5
0
static VALUE cDB_mput(VALUE vself, VALUE vhash){
  VALUE vary;
  TCRDB *db;
  TCLIST *list, *args;
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);

  args = vhashtolist(vhash);
  list = tcrdbmisc(db, "putlist", 0, args);
  vary = listtovary(list);
  tclistdel(args);
  tclistdel(list);
  return vary;
}
示例#6
0
/* Remove a record with a text string of a word database object. */
bool tcwdbout2(TCWDB *wdb, int64_t id, const char *text, const char *delims){
  assert(wdb && id > 0 && text);
  TCLIST *words = tcstrsplit(text, delims ? delims : WDBSPCCHARS);
  bool rv = tcwdbout(wdb, id, words);
  tclistdel(words);
  return rv;
}
示例#7
0
/*
 * /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);
}
示例#8
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;
}
示例#9
0
文件: ui.c 项目: charpty/goaccess
int
set_host_agents (const char *addr, void (*func) (void *, void *, int),
                 void *arr)
{
  TCLIST *tclist;
  GSLList *list;
  int key, count = 0;

  key = ht_get_keymap (HOSTS, addr);
  if (key == 0)
    return 1;

  tclist = ht_get_host_agent_tclist (HOSTS, key);
  if (!tclist)
    return 1;

  list = tclist_to_gsllist (tclist);
  if ((count = list_count (list)) == 0) {
    free (list);
    return 1;
  }

  func (list, arr, count);

  list_remove_nodes (list);
  tclistdel (tclist);

  return 0;
}
示例#10
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;
}
示例#11
0
文件: ui.c 项目: NinjaOSX/goaccess
int
set_host_agents (const char *addr, void (*func) (void *, void *, int),
                 void *arr)
{
  TCLIST *tclist;
  GSLList *list;
  int data_nkey, count = 0;

  data_nkey = get_int_from_keymap (addr, HOSTS);
  if (data_nkey == 0)
    return 1;

  tclist = get_host_agent_list (data_nkey);
  if (!tclist)
    return 1;

  list = tclist_to_gsllist (tclist);
  if ((count = list_count (list)) == 0) {
    free (list);
    return 1;
  }

  func (list, arr, count);

  list_remove_nodes (list);
  tclistdel (tclist);

  return 0;
}
示例#12
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;
}
示例#13
0
/* RTDBIterValuesValsType.tp_iternext */
static PyObject *
RTDBIterValuesVals_tp_iternext(DBIter *self)
{
    RDBBase *rdbbase = (RDBBase *)self->db;
    void *key;
    int key_size;
    TCMAP *value;
    TCLIST *valuevals;
    PyObject *pyvaluevals;

    if (rdbbase->changed) {
        return set_error(Error, "DB changed during iteration");
    }
    Py_BEGIN_ALLOW_THREADS
    key = tcrdbiternext(rdbbase->rdb, &key_size);
    if (key) {
        value = tcrdbtblget(rdbbase->rdb, key, key_size);
    }
    Py_END_ALLOW_THREADS
    if (!key) {
        if (tcrdbecode(rdbbase->rdb) == TTENOREC) {
            return set_stopiteration_error();
        }
        return set_rdb_error(rdbbase->rdb, NULL);
    }
    valuevals = tcmapvals(value);
    pyvaluevals = tclist_to_tuple(valuevals);
    tcmapdel(value);
    tclistdel(valuevals);
    return pyvaluevals;
}
示例#14
0
文件: tctest.c 项目: Fleurer/nanodb
/* 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;
}
示例#15
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);
}
示例#16
0
文件: tcamgr.c 项目: Fleurer/nanodb
/* perform map command */
static int procmap(const char *name, const char *dest, const char *fmstr){
  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;
  TCBDB *bdb = tcbdbnew();
  if(!tcbdbopen(bdb, dest, BDBOWRITER | BDBOCREAT | BDBOTRUNC)){
    printerr(adb);
    tcbdbdel(bdb);
    tcadbdel(adb);
    return 1;
  }
  if(fmstr){
    TCLIST *keys = tcadbfwmkeys2(adb, fmstr, -1);
    if(!tcadbmapbdb(adb, keys, bdb, mapbdbproc, NULL, -1)){
      printerr(adb);
      err = true;
    }
    tclistdel(keys);
  } else {
    if(!tcadbmapbdb(adb, NULL, bdb, mapbdbproc, NULL, -1)){
      printerr(adb);
      err = true;
    }
  }
  if(!tcbdbclose(bdb)){
    printerr(adb);
    err = true;
  }
  tcbdbdel(bdb);
  if(!tcadbclose(adb)){
    printerr(adb);
    err = true;
  }
  tcadbdel(adb);
  return err ? 1 : 0;
}
示例#17
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);
}
示例#18
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;
}
示例#19
0
文件: ltyrant.c 项目: MengJie/solid
static int
rdb_out(lua_State *L){
	TCRDB *rdb = rdb_getrdb(L, 1);
	int type = lua_type(L, 2);
	if(type==LUA_TSTRING || type==LUA_TNUMBER){
		size_t ksiz = 0;
		const char *key = rdb_getarg(L, 2, &ksiz);
		if(!tcrdbout(rdb, key, ksiz)) return rdb_error(L, rdb);
	} else if(type==LUA_TTABLE){
		TCLIST *list = array2tclist(L, 2);
		int opts = luaL_optint(L, 3, 0);
		TCLIST *useless = tcrdbmisc(rdb, "outlist", opts, list);
		tclistdel(list);
		tclistdel(useless);
	} else return luaL_argerror(L, 2, "must be table, string, or number");
	lua_pushboolean(L, 1);
	return 1;
}
示例#20
0
文件: pycabinet.c 项目: Frish/ntfs
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);
}
示例#21
0
static void range_atomic_test(void *db, 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;
		int num_recs;

		recs = do_tcadbmisc(adb, "range_atomic", args);
		num_recs = tclistnum(recs) / 2;
		if (!num_recs)
			break;

		check_records(recs, &keygen, vsiz, num < batch ? num : batch);
		tclistover2(args, 0, tclistval2(recs, 2 * (num_recs - 1)));
		tclistdel(recs);
		num -= num_recs;
	}
	if (debug && num)
		die("Unexpected record num: %d", num);

	tclistdel(args);
}
示例#22
0
文件: tchmgr.c 项目: Fleurer/nanodb
/* 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;
}
示例#23
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;
}
示例#24
0
文件: jejdb.c 项目: CowanSM/ejdb
/*
* Class:     org_ejdb_driver_EJDBResultSet
* Method:    close
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBResultSet_close (JNIEnv *env, jobject obj) {
	TCLIST *rs = get_rs_from_object(env, obj);

	if (!rs) {
		return;
	}

	tclistdel(rs);

	set_rs_to_object(env, obj, NULL);
};
示例#25
0
文件: ltyrant.c 项目: MengJie/solid
static int
rdb_fwmkeys(lua_State *L){
	TCRDB *rdb = rdb_getrdb(L, 1);
	size_t psiz = 0;
	const char *prefix = rdb_getarg(L, 2, &psiz);
	int max = luaL_optint(L, 3, -1);
	TCLIST *keys = tcrdbfwmkeys(rdb, prefix, psiz, max);
	tclist2array(L, keys);
	tclistdel(keys);
	return 1;
}
示例#26
0
static VALUE cQuery_search(VALUE vself){
  VALUE vqry, vary;
  RDBQRY *qry;
  TCLIST *res;
  vqry = rb_iv_get(vself, RDBQRYVNDATA);
  Data_Get_Struct(vqry, RDBQRY, qry);
  res = tcrdbqrysearch(qry);
  vary = listtovary(res);
  tclistdel(res);
  return vary;
}
示例#27
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;
}
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;
}
示例#29
0
static PyObject *
RTDBQuery_search(RTDBQuery *self)
{
    TCLIST *result;
    PyObject *pyresult;

    Py_BEGIN_ALLOW_THREADS
    result = tcrdbqrysearch(self->rqry);
    Py_END_ALLOW_THREADS
    pyresult = tclist_to_tuple(result);
    tclistdel(result);
    return pyresult;
}
示例#30
0
文件: conf.c 项目: flashjay/c-blog
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);
}