Пример #1
0
/* perform get command */
static int procget(const char *path, const char *pkbuf, int pksiz, int omode, bool px, bool pz){
  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(!tctdbopen(tdb, path, TDBOREADER | omode)){
    printerr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  bool err = false;
  TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
  if(cols){
    tcmapiterinit(cols);
    const char *kbuf;
    int ksiz;
    while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){
      int vsiz;
      const char *vbuf = tcmapiterval(kbuf, &vsiz);
      printdata(kbuf, ksiz, px);
      putchar('\t');
      printdata(vbuf, vsiz, px);
      putchar(pz ? '\t' : '\n');
    }
    tcmapdel(cols);
  } else {
    printerr(tdb);
    err = true;
  }
  if(!tctdbclose(tdb)){
    if(!err) printerr(tdb);
    err = true;
  }
  tctdbdel(tdb);
  return err ? 1 : 0;
}
Пример #2
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;
}
Пример #3
0
void get_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
    const char *value, *name;
    char *uri, *json, *hash, *key; 
    struct evkeyvalq args;
    struct json_object *jsobj, *jsobj2, *jsobj3;
    TCMAP *cols;
    
    if (rdb == NULL) {
        evhttp_send_error(req, 503, "database not connected");
        return;
    }

    uri = evhttp_decode_uri(req->uri);
    evhttp_parse_query(uri, &args);
    free(uri);

    hash = (char *)evhttp_find_header(&args, "hash");
    key = (char *)evhttp_find_header(&args, "key");
    
    if (hash == NULL) {
        evhttp_send_error(req, 400, "hash is required");
        evhttp_clear_headers(&args);
        return;
    }
    
    jsobj = json_object_new_object();
    jsobj2 = json_object_new_object();
    cols = tcrdbtblget(rdb, hash, sizeof(hash));
    
    if (cols) {
        tcmapiterinit(cols);
        jsobj3 = json_object_new_object();
        
        
        if (key) {
            value = tcmapget2(cols, key);

            if (!value) {
                value = "";
            }
            
            json_object_object_add(jsobj2, key, json_object_new_string(value));
        } else {
            while ((name = tcmapiternext2(cols)) != NULL) {
                json_object_object_add(jsobj2, name, json_object_new_string(tcmapget2(cols, name)));
            }
        }
     
        json_object_object_add(jsobj, "status", json_object_new_string("ok"));
        json_object_object_add(jsobj, "results", jsobj2);
        
        tcmapdel(cols);
    } else {
        json_object_object_add(jsobj, "status", json_object_new_string("error"));
    }
   
    finalize_json(req, evb, &args, jsobj);
}
Пример #4
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);
}
Пример #5
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;
}
Пример #6
0
/* print system information */
static void sysprint(void){
  TCMAP *info = tcsysinfo();
  if(info){
    tcmapiterinit(info);
    const char *kbuf;
    while((kbuf = tcmapiternext2(info)) != NULL){
      iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf));
    }
    tcmapdel(info);
  }
}
Пример #7
0
extern VALUE maptovhash(TCMAP *map){
  const char *kbuf;
  int ksiz, vsiz;
  VALUE vhash;
  vhash = rb_hash_new();
  tcmapiterinit(map);
  while((kbuf = tcmapiternext(map, &ksiz)) != NULL){
    const char *vbuf = tcmapiterval(kbuf, &vsiz);
    rb_hash_aset(vhash, rb_str_new(kbuf, ksiz), StringRaw(vbuf, vsiz));
  }
  return vhash;
}
Пример #8
0
static PyObject *
tcmap2pydict(TCMAP *map)
{
    const char *kstr, *vstr;
    PyObject *dict, *value;
    
    dict = PyDict_New();
    
    if (dict == NULL)
    {
        PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for dict.");
        return NULL;
    }
    
    tcmapiterinit(map);
    kstr = tcmapiternext2(map);
    
    while (kstr != NULL)
    {
        vstr = tcmapget2(map, kstr);
        
        if (vstr == NULL)
        {
            Py_DECREF(dict);
            PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for map value.");
            return NULL;
        }
        
        value = PyString_FromString(vstr);
        
        if (value == NULL)
        {
            Py_DECREF(dict);
            PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for dict value.");
            return NULL;
        }
        
        if (PyDict_SetItemString(dict, kstr, value) != 0)
        {
            Py_DECREF(value);
            Py_DECREF(dict);
            PyErr_SetString(PyExc_Exception, "Could not set dict item.");
            return NULL;
        }
        
        Py_DECREF(value);
        
        kstr = tcmapiternext2(map);
    }
    
    return dict;
}
Пример #9
0
uint8_t
XTDBForAllIDX(XTDBHandle* handle,IndexFullFn fullFn,IndexDBFn dbFn,IndexDBNoResFn noResFn ,void* args) {
    uint8_t rv = True;

    /*   HashTableIterator iter;
       hash_table_iterate(handle->indexes,&iter);
       while (hash_table_iter_has_more(&iter)) {
          DataBaseBE* be = hash_table_iter_next(&iter);
          rv = dbFn(NULL,db,args);
          if (!rv) {
             return rv;
          }
          //DBClose(be);
          //DBFree(be);
       }*/
    tcmapiterinit(handle->indexes);
    while (1) {
        int len,dataLen;
        DataBaseBE *outBase = NULL;
        const void* key = tcmapiternext(handle->indexes,&len);
        if (!key) {
            break;
        }
        const void *pPtr = tcmapget(handle->indexes,key,len,&dataLen);
        if (!pPtr) {
            continue;
        }
        memcpy(&outBase,pPtr,dataLen);
        if (fullFn) {
            rv = fullFn(handle,key,outBase,args);
        }
        if (dbFn) {
            rv = dbFn(outBase);
            if (rv) {
                // XXX proper error
                handle->error = XTDB_IO_ERR;
            }
        }
        if (noResFn) {
            noResFn(outBase);
            rv = True;
        }
        //free(key);
        //free(pPtr);
        if (!rv) {
            return rv;
        }
    }
    return rv;
}
Пример #10
0
/* getimpl */
JNIEXPORT jobjectArray JNICALL Java_tokyotyrant_TCRDB_getimpl
(JNIEnv *env, jobject self, jbyteArray pkey){
  if(!pkey){
    throwillarg(env);
    return 0;
  }
  TCRDB *tcrdb = (TCRDB *)(intptr_t)(*env)->GetLongField(env, self, tcrdb_fid_ptr);
  jboolean ick;
  jbyte *kbuf = (*env)->GetByteArrayElements(env, pkey, &ick);
  if(!kbuf){
    throwoutmem(env);
    return NULL;
  }
  int ksiz = (*env)->GetArrayLength(env, pkey);
  jobjectArray ary = NULL;
  TCMAP *tcols = tcrdbtblget(tcrdb, kbuf, ksiz);
  if(tcols){
    int anum = tcmaprnum(tcols) * 2;
    ary = (*env)->NewObjectArray(env, anum, (*env)->GetObjectClass(env, pkey), NULL);
    anum = 0;
    tcmapiterinit(tcols);
    const char *nbuf;
    int nsiz;
    while((nbuf = tcmapiternext(tcols, &nsiz)) != NULL){
      int vsiz;
      const char *vbuf = tcmapiterval(nbuf, &vsiz);
      jbyteArray nary = (*env)->NewByteArray(env, nsiz);
      if(!nary){
        throwoutmem(env);
        return NULL;
      }
      jbyteArray vary = (*env)->NewByteArray(env, vsiz);
      if(!vary){
        throwoutmem(env);
        return NULL;
      }
      (*env)->SetByteArrayRegion(env, nary, 0, nsiz, (jbyte *)nbuf);
      (*env)->SetByteArrayRegion(env, vary, 0, vsiz, (jbyte *)vbuf);
      (*env)->SetObjectArrayElement(env, ary, anum++, nary);
      (*env)->SetObjectArrayElement(env, ary, anum++, vary);
      (*env)->DeleteLocalRef(env, vary);
      (*env)->DeleteLocalRef(env, nary);
    }
    tcmapdel(tcols);
  }
  if(ick) (*env)->ReleaseByteArrayElements(env, pkey, kbuf, JNI_ABORT);
  return ary;
}
Пример #11
0
static void sandbox_map_free(TCMAP *map)
{
	int sp;
	TCLIST **p;
	const char *pkbuf;

	// FIXME: free listeners
	tcmapiterinit(map);
	pkbuf = tcmapiternext2(map);
	while (pkbuf) {
		p = (TCLIST **)tcmapget(map, pkbuf, strlen(pkbuf), &sp);
		if (p) {
			tclistdel(*p);
			//free(p);
		}
		pkbuf = tcmapiternext2(map);
	}
	tcmapdel(map);
}
Пример #12
0
/* perform list command */
static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *fmstr){
  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(!tctdbopen(tdb, path, TDBOREADER | omode)){
    printerr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  bool err = false;
  if(fmstr){
    TCLIST *pkeys = tctdbfwmkeys2(tdb, fmstr, max);
    for(int i = 0; i < tclistnum(pkeys); i++){
      int pksiz;
      const char *pkbuf = tclistval(pkeys, i, &pksiz);
      printdata(pkbuf, pksiz, px);
      if(pv){
        TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
        if(cols){
          tcmapiterinit(cols);
          const char *kbuf;
          int ksiz;
          while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){
            int vsiz;
            const char *vbuf = tcmapiterval(kbuf, &vsiz);
            putchar('\t');
            printdata(kbuf, ksiz, px);
            putchar('\t');
            printdata(vbuf, vsiz, px);
          }
          tcmapdel(cols);
        }
      }
      putchar('\n');
    }
    tclistdel(pkeys);
  } else {
    if(!tctdbiterinit(tdb)){
      printerr(tdb);
      err = true;
    }
    int cnt = 0;
    TCMAP *cols;
    while((cols = tctdbiternext3(tdb)) != NULL){
      int pksiz;
      const char *pkbuf = tcmapget(cols, "", 0, &pksiz);
      if(pkbuf){
        printdata(pkbuf, pksiz, px);
        if(pv){
          tcmapiterinit(cols);
          const char *kbuf;
          int ksiz;
          while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){
            if(*kbuf == '\0') continue;
            int vsiz;
            const char *vbuf = tcmapiterval(kbuf, &vsiz);
            putchar('\t');
            printdata(kbuf, ksiz, px);
            putchar('\t');
            printdata(vbuf, vsiz, px);
          }
        }
      }
      tcmapdel(cols);
      putchar('\n');
      if(max >= 0 && ++cnt >= max) break;
    }
  }
  if(!tctdbclose(tdb)){
    if(!err) printerr(tdb);
    err = true;
  }
  tctdbdel(tdb);
  return err ? 1 : 0;
}
Пример #13
0
/* perform misc command */
static int procmisc(const char *path, int rnum, bool mt, int omode){
  my_my_my_iprintf("<Miscellaneous Test>\n  seed=%u  path=%s  rnum=%d  mt=%d  omode=%d\n\n",
          g_randseed, path, rnum, mt, omode);
  bool err = false;
  double stime = tctime();
  TCFDB *fdb = tcfdbnew();
  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);
  if(mt && !tcfdbsetmutex(fdb)){
    eprint(fdb, __LINE__, "tcfdbsetmutex");
    err = true;
  }
  if(!tcfdbtune(fdb, RECBUFSIZ, EXHEADSIZ + (RECBUFSIZ + sizeof(int)) * rnum)){
    eprint(fdb, __LINE__, "tcfdbtune");
    err = true;
  }
  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){
    eprint(fdb, __LINE__, "tcfdbopen");
    err = true;
  }
  if(TCUSEPTHREAD){
    TCFDB *fdbdup = tcfdbnew();
    if(tcfdbopen(fdbdup, path, FDBOREADER)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
    } else if(tcfdbecode(fdbdup) != TCETHREAD){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
    }
    tcfdbdel(fdbdup);
  }
  my_my_my_iprintf("writing:\n");
  for(int i = 1; i <= rnum; i++){
    char buf[RECBUFSIZ];
    int len = sprintf(buf, "%08d", i);
    if(!tcfdbputkeep2(fdb, buf, len, buf, len)){
      eprint(fdb, __LINE__, "tcfdbputkeep");
      err = true;
      break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("reading:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%08d", i);
    int vsiz;
    char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
    if(!vbuf){
      eprint(fdb, __LINE__, "tcfdbget");
      err = true;
      break;
    } else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
      tcfree(vbuf);
      break;
    }
    tcfree(vbuf);
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(tcfdbrnum(fdb) != rnum){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  my_my_my_iprintf("random writing:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
    char vbuf[RECBUFSIZ];
    int vsiz = myrand(RECBUFSIZ);
    memset(vbuf, '*', vsiz);
    if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){
      eprint(fdb, __LINE__, "tcfdbput");
      err = true;
      break;
    }
    int rsiz;
    char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);
    if(!rbuf){
      eprint(fdb, __LINE__, "tcfdbget");
      err = true;
      break;
    }
    if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
      tcfree(rbuf);
      break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
    tcfree(rbuf);
  }
  my_my_my_iprintf("random erasing:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
    if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){
      eprint(fdb, __LINE__, "tcfdbout");
      err = true;
      break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("writing:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "[%d]", i);
    char vbuf[RECBUFSIZ];
    int vsiz = i % RECBUFSIZ;
    memset(vbuf, '*', vsiz);
    if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){
      eprint(fdb, __LINE__, "tcfdbputkeep");
      err = true;
      break;
    }
    if(vsiz < 1){
      char tbuf[PATH_MAX];
      for(int j = 0; j < PATH_MAX; j++){
        tbuf[j] = myrand(0x100);
      }
      if(!tcfdbput2(fdb, kbuf, ksiz, tbuf, PATH_MAX)){
        eprint(fdb, __LINE__, "tcfdbput");
        err = true;
        break;
      }
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("erasing:\n");
  for(int i = 1; i <= rnum; i++){
    if(i % 2 == 1){
      char kbuf[RECBUFSIZ];
      int ksiz = sprintf(kbuf, "[%d]", i);
      if(!tcfdbout2(fdb, kbuf, ksiz)){
        eprint(fdb, __LINE__, "tcfdbout");
        err = true;
        break;
      }
      if(tcfdbout2(fdb, kbuf, ksiz) || tcfdbecode(fdb) != TCENOREC){
        eprint(fdb, __LINE__, "tcfdbout");
        err = true;
        break;
      }
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("random writing and reopening:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
    int vsiz = myrand(RECBUFSIZ);
    char *vbuf = tcmalloc(vsiz + 1);
    memset(vbuf, '*', vsiz);
    switch(myrand(3)){
      case 0:
        if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput");
          err = true;
        }
        break;
      case 1:
        if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbputcat");
          err = true;
        }
        break;
      case 2:
        if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbout");
          err = true;
        }
        break;
    }
    tcfree(vbuf);
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(!tcfdbclose(fdb)){
    eprint(fdb, __LINE__, "tcfdbclose");
    err = true;
  }
  if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){
    eprint(fdb, __LINE__, "tcfdbopen");
    err = true;
  }
  my_my_my_iprintf("checking:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "[%d]", i);
    int vsiz;
    char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
    if(vbuf){
      tcfree(vbuf);
    } else if(tcfdbecode(fdb) != TCENOREC){
      eprint(fdb, __LINE__, "tcfdbget");
      err = true;
      break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("writing:\n");
  for(int i = 1; i <= rnum; i++){
    char buf[RECBUFSIZ];
    int len = sprintf(buf, "%08d", i);
    if(!tcfdbput2(fdb, buf, len, buf, len)){
      eprint(fdb, __LINE__, "tcfdbput");
      err = true;
      break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("reading:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%08d", i);
    int vsiz;
    char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
    if(!vbuf){
      eprint(fdb, __LINE__, "tcfdbget");
      err = true;
      break;
    } else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
      tcfree(vbuf);
      break;
    }
    tcfree(vbuf);
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("checking iterator:\n");
  if(!tcfdbiterinit(fdb)){
    eprint(fdb, __LINE__, "tcfdbiterinit");
    err = true;
  }
  char *kbuf;
  int ksiz;
  int inum = 0;
  for(int i = 1; (kbuf = tcfdbiternext2(fdb, &ksiz)) != NULL; i++, inum++){
    int vsiz;
    char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
    if(!vbuf){
      eprint(fdb, __LINE__, "tcfdbget2");
      err = true;
      tcfree(kbuf);
      break;
    }
    tcfree(vbuf);
    tcfree(kbuf);
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(tcfdbecode(fdb) != TCENOREC || inum != tcfdbrnum(fdb)){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  my_my_my_iprintf("iteration updating:\n");
  if(!tcfdbiterinit(fdb)){
    eprint(fdb, __LINE__, "tcfdbiterinit");
    err = true;
  }
  inum = 0;
  for(int i = 1; (kbuf = tcfdbiternext2(fdb, &ksiz)) != NULL; i++, inum++){
    if(myrand(2) == 0){
      if(!tcfdbputcat2(fdb, kbuf, ksiz, "0123456789", 10)){
        eprint(fdb, __LINE__, "tcfdbputcat2");
        err = true;
        tcfree(kbuf);
        break;
      }
    } else {
      if(!tcfdbout2(fdb, kbuf, ksiz)){
        eprint(fdb, __LINE__, "tcfdbout");
        err = true;
        tcfree(kbuf);
        break;
      }
    }
    tcfree(kbuf);
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(tcfdbecode(fdb) != TCENOREC || inum < tcfdbrnum(fdb)){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  if(myrand(10) == 0 && !tcfdbsync(fdb)){
    eprint(fdb, __LINE__, "tcfdbsync");
    err = true;
  }
  if(!tcfdbvanish(fdb)){
    eprint(fdb, __LINE__, "tcfdbvanish");
    err = true;
  }
  TCMAP *map = tcmapnew();
  my_my_my_iprintf("random writing:\n");
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
    char vbuf[RECBUFSIZ];
    int vsiz = sprintf(vbuf, "%d", myrand(rnum) + 1);
    switch(myrand(7)){
      case 0:
        if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput2");
          err = true;
        }
        tcmapput(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 1:
        if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputkeep2");
          err = true;
        }
        tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 2:
        if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputcat2");
          err = true;
        }
        tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 3:
        if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbout2");
          err = true;
        }
        tcmapout(map, kbuf, ksiz);
        break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  my_my_my_iprintf("checking transaction commit:\n");
  if(!tcfdbtranbegin(fdb)){
    eprint(fdb, __LINE__, "tcfdbtranbegin");
    err = true;
  }
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
    char vbuf[RECBUFSIZ];
    int vsiz = sprintf(vbuf, "[%d]", myrand(rnum) + 1);
    switch(myrand(7)){
      case 0:
        if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput2");
          err = true;
        }
        tcmapput(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 1:
        if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputkeep2");
          err = true;
        }
        tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 2:
        if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbputcat2");
          err = true;
        }
        tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 3:
        if(tcfdbaddint(fdb, tcfdbkeytoid(kbuf, ksiz), 1) == INT_MIN &&
           tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbaddint");
          err = true;
        }
        tcmapaddint(map, kbuf, ksiz, 1);
        break;
      case 4:
        if(isnan(tcfdbadddouble(fdb, tcfdbkeytoid(kbuf, ksiz), 1.0)) &&
           tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbadddouble");
          err = true;
        }
        tcmapadddouble(map, kbuf, ksiz, 1.0);
        break;
      case 5:
        if(myrand(2) == 0){
          void *op = (void *)(intptr_t)(myrand(3) + 1);
          if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), vbuf, vsiz, pdprocfunc, op) &&
             tcfdbecode(fdb) != TCEKEEP){
            eprint(fdb, __LINE__, "tcfdbputproc");
            err = true;
          }
          tcmapputproc(map, kbuf, ksiz, vbuf, vsiz, pdprocfunc, op);
        } else {
          vsiz = myrand(10);
          void *op = (void *)(intptr_t)(myrand(3) + 1);
          if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), NULL, vsiz, pdprocfunc, op) &&
             tcfdbecode(fdb) != TCEKEEP && tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbputproc");
            err = true;
          }
          tcmapputproc(map, kbuf, ksiz, NULL, vsiz, pdprocfunc, op);
        }
        break;
      case 6:
        if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbout2");
          err = true;
        }
        tcmapout(map, kbuf, ksiz);
        break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(!tcfdbtrancommit(fdb)){
    eprint(fdb, __LINE__, "tcfdbtrancommit");
    err = true;
  }
  my_my_my_iprintf("checking transaction abort:\n");
  uint64_t ornum = tcfdbrnum(fdb);
  uint64_t ofsiz = tcfdbfsiz(fdb);
  if(!tcfdbtranbegin(fdb)){
    eprint(fdb, __LINE__, "tcfdbtranbegin");
    err = true;
  }
  for(int i = 1; i <= rnum; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", myrand(rnum) + 1);
    char vbuf[RECBUFSIZ];
    int vsiz = sprintf(vbuf, "[%d]", myrand(rnum) + 1);
    switch(myrand(7)){
      case 0:
        if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput2");
          err = true;
        }
        break;
      case 1:
        if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputkeep2");
          err = true;
        }
        break;
      case 2:
        if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbputcat2");
          err = true;
        }
        break;
      case 3:
        if(tcfdbaddint(fdb, tcfdbkeytoid(kbuf, ksiz), 1) == INT_MIN &&
           tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbaddint");
          err = true;
        }
        break;
      case 4:
        if(isnan(tcfdbadddouble(fdb, tcfdbkeytoid(kbuf, ksiz), 1.0)) &&
           tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbadddouble");
          err = true;
        }
        break;
      case 5:
        if(myrand(2) == 0){
          void *op = (void *)(intptr_t)(myrand(3) + 1);
          if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), vbuf, vsiz, pdprocfunc, op) &&
             tcfdbecode(fdb) != TCEKEEP){
            eprint(fdb, __LINE__, "tcfdbputproc");
            err = true;
          }
        } else {
          vsiz = myrand(10);
          void *op = (void *)(intptr_t)(myrand(3) + 1);
          if(!tcfdbputproc(fdb, tcfdbkeytoid(kbuf, ksiz), NULL, vsiz, pdprocfunc, op) &&
             tcfdbecode(fdb) != TCEKEEP && tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbputproc");
            err = true;
          }
        }
        break;
      case 6:
        if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbout2");
          err = true;
        }
        break;
    }
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(!tcfdbtranabort(fdb)){
    eprint(fdb, __LINE__, "tcfdbtranabort");
    err = true;
  }
  my_my_my_iprintf("checking consistency:\n");
  if(tcfdbrnum(fdb) != ornum || tcfdbfsiz(fdb) != ofsiz || tcfdbrnum(fdb) != tcmaprnum(map)){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  inum = 0;
  tcmapiterinit(map);
  const char *tkbuf;
  int tksiz;
  for(int i = 1; (tkbuf = tcmapiternext(map, &tksiz)) != NULL; i++, inum++){
    int tvsiz;
    const char *tvbuf = tcmapiterval(tkbuf, &tvsiz);
    if(tvsiz > RECBUFSIZ) tvsiz = RECBUFSIZ;
    int rsiz;
    char *rbuf = tcfdbget2(fdb, tkbuf, tksiz, &rsiz);
    if(!rbuf || rsiz != tvsiz || memcmp(rbuf, tvbuf, rsiz)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
      tcfree(rbuf);
      break;
    }
    tcfree(rbuf);
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(rnum > 250) my_my_my_iprintf(" (%08d)\n", inum);
  inum = 0;
  if(!tcfdbiterinit(fdb)){
    eprint(fdb, __LINE__, "tcfdbiterinit");
    err = true;
  }
  for(int i = 1; (kbuf = tcfdbiternext2(fdb, &ksiz)) != NULL; i++, inum++){
    int vsiz;
    char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
    int rsiz;
    const char *rbuf = tcmapget(map, kbuf, ksiz, &rsiz);
    if(rsiz > RECBUFSIZ) rsiz = RECBUFSIZ;
    if(!rbuf || rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
      tcfree(vbuf);
      tcfree(kbuf);
      break;
    }
    tcfree(vbuf);
    tcfree(kbuf);
    if(rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) my_my_my_iprintf(" (%08d)\n", i);
    }
  }
  if(rnum > 250) my_my_my_iprintf(" (%08d)\n", inum);
  tcmapdel(map);
  if(!tcfdbvanish(fdb)){
    eprint(fdb, __LINE__, "tcfdbvanish");
    err = true;
  }
  if(rnum >= 100){
    if(!tcfdbtranbegin(fdb)){
      eprint(fdb, __LINE__, "tcfdbtranbegin");
      err = true;
    }
    if(!tcfdbput3(fdb, "99", "hirabayashi")){
      eprint(fdb, __LINE__, "tcfdbput3");
      err = true;
    }
    for(int i = 0; i < 10; i++){
      char buf[RECBUFSIZ];
      int size = sprintf(buf, "%d", myrand(rnum) + 1);
      if(!tcfdbput2(fdb, buf, size, buf, size)){
        eprint(fdb, __LINE__, "tcfdbput2");
        err = true;
      }
    }
    for(int i = myrand(3) + 1; i < PATH_MAX; i = i * 2 + myrand(3)){
      char vbuf[i];
      memset(vbuf, '@', i - 1);
      vbuf[i-1] = '\0';
      if(!tcfdbput3(fdb, "99", vbuf)){
        eprint(fdb, __LINE__, "tcfdbput3");
        err = true;
      }
    }
    if(!tcfdbforeach(fdb, iterfunc, NULL)){
      eprint(fdb, __LINE__, "tcfdbforeach");
      err = true;
    }
  }
  my_my_my_iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));
  my_my_my_iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));
  mprint(fdb);
  sysprint();
  if(!tcfdbclose(fdb)){
    eprint(fdb, __LINE__, "tcfdbclose");
    err = true;
  }
  tcfdbdel(fdb);
  my_my_my_iprintf("time: %.3f\n", tctime() - stime);
  my_my_my_iprintf("%s\n\n", err ? "error" : "ok");
  return err ? 1 : 0;
}
Пример #14
0
void search_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
    char *uri, *json;
    double lat, lng, distance, minlat, minlng, maxlat, maxlng, miles, lat2, lng2;
    int x1, x2, y1, y2, id, max;
    int total;
    struct evkeyvalq args;
    int ecode, pksiz, i, rsiz;
    char pkbuf[256];
    char minx[8];
    char miny[8];
    char maxx[8];
    char maxy[8];
    const char *rbuf, *name, *buf;
    RDBQRY *query;
    TCLIST *result;
    TCMAP *cols;
    Geo_Result *georesultPtr, **georesults;
    struct json_object *jsobj, *jsobj2, *jsarr;

    if (rdb == NULL) {
        evhttp_send_error(req, 503, "database not connected");
        return;
    }

    uri = evhttp_decode_uri(req->uri);
    evhttp_parse_query(uri, &args);
    free(uri);

    argtof(&args, "lat", &lat, 0);
    argtof(&args, "lng", &lng, 0);
    argtof(&args, "miles", &miles, 0);
    argtoi(&args, "max", &max, 1);
    
    geo_box(lat, lng, miles, &minlat, &minlng, &maxlat, &maxlng);
    
    x1 = (minlat * 10000) + 1800000;
    y1 = (minlng * 10000) + 1800000;
    x2 = (maxlat * 10000) + 1800000;
    y2 = (maxlng * 10000) + 1800000;
    
    sprintf(minx, "%d", x1);
    sprintf(miny, "%d", y1);
    sprintf(maxx, "%d", x2);
    sprintf(maxy, "%d", y2);
        
    query = tcrdbqrynew(rdb);
    tcrdbqryaddcond(query, "x", RDBQCNUMGT, minx);
    tcrdbqryaddcond(query, "x", RDBQCNUMLT, maxx);
    tcrdbqryaddcond(query, "y", RDBQCNUMGT, miny);
    tcrdbqryaddcond(query, "y", RDBQCNUMLT, maxy);
    tcrdbqrysetorder(query, "x", RDBQONUMASC);
    
    cols = tcmapnew();
    result = tcrdbqrysearch(query);
    total = tclistnum(result);
    
    georesults = malloc(sizeof(Geo_Result *) * total);
    
    for(i = 0; i < total; i++){
        rbuf = tclistval(result, i, &rsiz);
        cols = tcrdbtblget(rdb, rbuf, rsiz);
        
        if (cols) {
            georesultPtr = malloc(sizeof(*georesultPtr));
            
            tcmapiterinit(cols);
            buf = tcmapget2(cols, "lat");
            lat2 = atof(buf);
            georesultPtr->latitude = lat2;
            buf = tcmapget2(cols, "lng");
            lng2 = atof(buf);
            georesultPtr->longitude = lng2;
            id = atoi(rbuf);
            georesultPtr->id = id;
            georesultPtr->data = strdup(tcmapget2(cols, "data"));
            distance = geo_distance(lat, lng, lat2, lng2);
            georesultPtr->distance = distance;
            georesults[i] = georesultPtr;
            tcmapdel(cols);
        }
    }
    
    tclistdel(result);
    tcrdbqrydel(query);
    
    qsort(georesults, total, sizeof(Geo_Result *), CmpElem);
    
    jsobj = json_object_new_object();
    jsarr = json_object_new_array();
    
    for(i = 0; i < total; i++){
        georesultPtr = georesults[i];

        if (i < max) {
            jsobj2 = json_object_new_object();
            json_object_object_add(jsobj2, "id", json_object_new_int(georesultPtr->id));
            json_object_object_add(jsobj2, "data", json_object_new_string(georesultPtr->data));
            json_object_object_add(jsobj2, "latitude", json_object_new_double(georesultPtr->latitude));
            json_object_object_add(jsobj2, "longitude", json_object_new_double(georesultPtr->longitude));
            json_object_object_add(jsobj2, "distance", json_object_new_double(georesultPtr->distance));
            json_object_array_add(jsarr, jsobj2);
        }
        free(georesultPtr->data);
        free(georesultPtr);
    }
    
    free(georesults);
    
    json_object_object_add(jsobj, "total", json_object_new_int(total));
    json_object_object_add(jsobj, "results", jsarr);
    
    finalize_json(req, evb, &args, jsobj);
}
Пример #15
0
/* thread the typical function */
static void *threadtypical(void *targ){
  TCFDB *fdb = ((TARGTYPICAL *)targ)->fdb;
  int rnum = ((TARGTYPICAL *)targ)->rnum;
  bool nc = ((TARGTYPICAL *)targ)->nc;
  int rratio = ((TARGTYPICAL *)targ)->rratio;
  int id = ((TARGTYPICAL *)targ)->id;
  bool err = false;
  TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL;
  int base = id * rnum;
  int mrange = tclmax(50 + rratio, 100);
  int width = tcfdbwidth(fdb);
  for(int i = 1; !err && i <= rnum; i++){
    char buf[RECBUFSIZ];
    int len = sprintf(buf, "%08d", base + myrandnd(i) + 1);
    int rnd = myrand(mrange);
    if(rnd < 10){
      if(!tcfdbput2(fdb, buf, len, buf, len)){
        eprint(fdb, __LINE__, "tcfdbput2");
        err = true;
      }
      if(map) tcmapput(map, buf, len, buf, len);
    } else if(rnd < 15){
      if(!tcfdbputkeep2(fdb, buf, len, buf, len) && tcfdbecode(fdb) != TCEKEEP){
        eprint(fdb, __LINE__, "tcfdbputkeep2");
        err = true;
      }
      if(map) tcmapputkeep(map, buf, len, buf, len);
    } else if(rnd < 20){
      if(!tcfdbputcat2(fdb, buf, len, buf, len)){
        eprint(fdb, __LINE__, "tcfdbputcat2");
        err = true;
      }
      if(map) tcmapputcat(map, buf, len, buf, len);
    } else if(rnd < 25){
      if(!tcfdbout2(fdb, buf, len) && tcfdbecode(fdb) && tcfdbecode(fdb) != TCENOREC){
        eprint(fdb, __LINE__, "tcfdbout");
        err = true;
      }
      if(map) tcmapout(map, buf, len);
    } else if(rnd < 26){
      if(myrand(10) == 0 && !tcfdbiterinit(fdb) && tcfdbecode(fdb) != TCENOREC){
        eprint(fdb, __LINE__, "tcfdbiterinit");
        err = true;
      }
      for(int j = 0; !err && j < 10; j++){
        if(tcfdbiternext(fdb) < 1 &&
           tcfdbecode(fdb) != TCEINVALID && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbiternext");
          err = true;
        }
      }
    } else {
      int vsiz;
      char *vbuf = tcfdbget2(fdb, buf, len, &vsiz);
      if(vbuf){
        if(map){
          int msiz = 0;
          const char *mbuf = tcmapget(map, buf, len, &msiz);
          if(msiz > width) msiz = width;
          if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
            eprint(fdb, __LINE__, "(validation)");
            err = true;
          }
        }
        tcfree(vbuf);
      } else {
        if(tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget");
          err = true;
        }
        if(map && tcmapget(map, buf, len, &vsiz)){
          eprint(fdb, __LINE__, "(validation)");
          err = true;
        }
      }
    }
    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
    }
  }
  if(map){
    tcmapiterinit(map);
    int ksiz;
    const char *kbuf;
    while(!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL){
      int vsiz;
      char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz);
      if(vbuf){
        int msiz = 0;
        const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz);
        if(msiz > width) msiz = width;
        if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
          eprint(fdb, __LINE__, "(validation)");
          err = true;
        }
        tcfree(vbuf);
      } else {
        eprint(fdb, __LINE__, "(validation)");
        err = true;
      }
    }
    tcmapdel(map);
  }
  return err ? "error" : NULL;
}
Пример #16
0
/* thread the typical function */
static void *threadtypical(void *targ){
  TCMDB *mdb = ((TARGTYPICAL *)targ)->mdb;
  TCNDB *ndb = ((TARGCOMBO *)targ)->ndb;
  int rnum = ((TARGTYPICAL *)targ)->rnum;
  bool nc = ((TARGTYPICAL *)targ)->nc;
  int rratio = ((TARGTYPICAL *)targ)->rratio;
  int id = ((TARGTYPICAL *)targ)->id;
  bool err = false;
  TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL;
  int base = id * rnum;
  int mrange = tclmax(50 + rratio, 100);
  for(int i = 1; !err && i <= rnum; i++){
    char buf[RECBUFSIZ];
    int len = sprintf(buf, "%08d", base + myrandnd(i));
    int rnd = myrand(mrange);
    if(rnd < 10){
      if(ndb){
        tcndbput(ndb, buf, len, buf, len);
      } else {
        tcmdbput(mdb, buf, len, buf, len);
      }
      if(map) tcmapput(map, buf, len, buf, len);
    } else if(rnd < 15){
      if(ndb){
        tcndbputkeep(ndb, buf, len, buf, len);
      } else {
        tcmdbputkeep(mdb, buf, len, buf, len);
      }
      if(map) tcmapputkeep(map, buf, len, buf, len);
    } else if(rnd < 20){
      if(ndb){
        tcndbputcat(ndb, buf, len, buf, len);
      } else {
        tcmdbputcat(mdb, buf, len, buf, len);
      }
      if(map) tcmapputcat(map, buf, len, buf, len);
    } else if(rnd < 30){
      if(ndb){
        tcndbout(ndb, buf, len);
      } else {
        tcmdbout(mdb, buf, len);
      }
      if(map) tcmapout(map, buf, len);
    } else if(rnd < 31){
      if(myrand(10) == 0) tcmdbiterinit(mdb);
      for(int j = 0; !err && j < 10; j++){
        int ksiz;
        char *kbuf = ndb ? tcndbiternext(ndb, &ksiz) : tcmdbiternext(mdb, &ksiz);
        if(kbuf) tcfree(kbuf);
      }
    } else {
      int vsiz;
      char *vbuf = ndb ? tcndbget(ndb, buf, len, &vsiz) : tcmdbget(mdb, buf, len, &vsiz);
      if(vbuf){
        if(map){
          int msiz;
          const char *mbuf = tcmapget(map, buf, len, &msiz);
          if(msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
            eprint(__LINE__, "(validation)");
            err = true;
          }
        }
        tcfree(vbuf);
      } else {
        if(map && tcmapget(map, buf, len, &vsiz)){
          eprint(__LINE__, "(validation)");
          err = true;
        }
      }
    }
    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){
      iputchar('.');
      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);
    }
  }
  if(map){
    tcmapiterinit(map);
    int ksiz;
    const char *kbuf;
    while(!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL){
      int vsiz;
      char *vbuf = ndb ? tcndbget(ndb, kbuf, ksiz, &vsiz) : tcmdbget(mdb, kbuf, ksiz, &vsiz);
      if(vbuf){
        int msiz;
        const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz);
        if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){
          eprint(__LINE__, "(validation)");
          err = true;
        }
        tcfree(vbuf);
      } else {
        eprint(__LINE__, "(validation)");
        err = true;
      }
    }
    tcmapdel(map);
  }
  return err ? "error" : NULL;
}
Пример #17
0
void 
xtc_mapiterinit(void* map)
{
        tcmapiterinit(map);
}
Пример #18
0
/*
 * Sets up the user_session structure. This contains various bits of
 * information pertaining to the users session.
 */
void set_user_session(void)
{
	TCTDB *tdb;
	TDBQRY *qry;
	TCLIST *res;
	TCMAP *cols;
	int rsize;
	int primary_key_size;
	char pkbuf[256];
	char session_id[SID_LEN + 1];
	char login_at[21];
	char last_seen[21];
	char uid[11];
	char sid[21];
	char restrict_ip[2];
	char capabilities[4];
	char user_hdr[1025];
	char *xss_string;
	const char *rbuf;

	/*
	 * Don't assume the order we get the cookies back is the
	 * same order as we sent them.
	 */
	if (strncmp(env_vars.http_cookie, "session_id", 10) == 0)
		snprintf(session_id, sizeof(session_id), "%s",
						env_vars.http_cookie + 11);
	else
		snprintf(session_id, sizeof(session_id), "%s",
						env_vars.http_cookie + 88);

	tdb = tctdbnew();
	tctdbopen(tdb, SESSION_DB, TDBOREADER | TDBOWRITER);

	/* Get the users stored session */
	qry = tctdbqrynew(tdb);
	tctdbqryaddcond(qry, "session_id", TDBQCSTREQ, session_id);
	res = tctdbqrysearch(qry);

	rbuf = tclistval(res, 0, &rsize);
	cols = tctdbget(tdb, rbuf, rsize);
	tcmapiterinit(cols);

	memset(&user_session, 0, sizeof(user_session));
	snprintf(user_session.tenant, sizeof(user_session.tenant), "%s",
			tcmapget2(cols, "tenant"));
	user_session.sid = strtoull(tcmapget2(cols, "sid"), NULL, 10);
	user_session.uid = atoi(tcmapget2(cols, "uid"));
	user_session.username = strdup(tcmapget2(cols, "username"));
	user_session.name = strdup(tcmapget2(cols, "name"));
	user_session.login_at = atol(tcmapget2(cols, "login_at"));
	user_session.last_seen = time(NULL);
	snprintf(user_session.origin_ip, sizeof(user_session.origin_ip), "%s",
			tcmapget2(cols, "origin_ip"));
	user_session.client_id = strdup(tcmapget2(cols, "client_id"));
	snprintf(user_session.session_id, sizeof(user_session.session_id),
			"%s", tcmapget2(cols, "session_id"));
	snprintf(user_session.csrf_token, sizeof(user_session.csrf_token),
			"%s", tcmapget2(cols, "csrf_token"));
	user_session.restrict_ip = atoi(tcmapget2(cols, "restrict_ip"));
	user_session.capabilities = atoi(tcmapget2(cols, "capabilities"));

	tcmapdel(cols);
	tclistdel(res);
	tctdbqrydel(qry);

	/*
	 * Set the user header banner, which displays the users name, uid and
	 * whether they are an Approver and or Admin.
	 */
	xss_string = xss_safe_string(user_session.name);
	snprintf(user_hdr, sizeof(user_hdr), "<big><big> %s</big></big><small>"
				"<span class = \"lighter\"> (%d) </span>"
				"</small>", xss_string, user_session.uid);
	free(xss_string);
	if (IS_APPROVER() && IS_ADMIN())
		strncat(user_hdr, "<span class = \"t_red\">(Approver / Admin)"
					"</span>", 1024 - strlen(user_hdr));
	else if (IS_APPROVER())
		strncat(user_hdr, "<span class = \"t_red\">(Approver)"
					"</span>", 1024 - strlen(user_hdr));
	else if (IS_ADMIN())
		strncat(user_hdr, "<span class = \"t_red\">(Admin)"
					"</span>", 1024 - strlen(user_hdr));
	strncat(user_hdr, "&nbsp;", 1024 - strlen(user_hdr));
	user_session.user_hdr = strdup(user_hdr);

	/*
	 * We want to update the last_seen timestamp in the users session.
	 * This entails removing the old session first then storing the new
	 * updated session.
	 */
	qry = tctdbqrynew(tdb);
	tctdbqryaddcond(qry, "session_id", TDBQCSTREQ, session_id);
	res = tctdbqrysearch(qry);
	rbuf = tclistval(res, 0, &rsize);
	tctdbout(tdb, rbuf, strlen(rbuf));

	tclistdel(res);
	tctdbqrydel(qry);

	primary_key_size = sprintf(pkbuf, "%ld", (long)tctdbgenuid(tdb));
	snprintf(login_at, sizeof(login_at), "%ld", user_session.login_at);
	snprintf(last_seen, sizeof(last_seen), "%ld",
						user_session.last_seen);
	snprintf(uid, sizeof(uid), "%u", user_session.uid);
	snprintf(sid, sizeof(sid), "%llu", user_session.sid);
	snprintf(restrict_ip, sizeof(restrict_ip), "%d",
						user_session.restrict_ip);
	snprintf(capabilities, sizeof(capabilities), "%d",
						user_session.capabilities);
	cols = tcmapnew3("tenant", user_session.tenant,
			"sid", sid,
			"uid", uid,
			"username", user_session.username,
			"name", user_session.name,
			"login_at", login_at,
			"last_seen", last_seen,
			"origin_ip", user_session.origin_ip,
			"client_id", user_session.client_id,
			"session_id", user_session.session_id,
			"csrf_token", user_session.csrf_token,
			"restrict_ip", restrict_ip,
			"capabilities", capabilities,
			NULL);
	tctdbput(tdb, pkbuf, primary_key_size, cols);

	tcmapdel(cols);

	tctdbclose(tdb);
	tctdbdel(tdb);
}
Пример #19
0
/* process a record in iteration of a query result set */
static int tcprocrec(const void *pkbuf, int pksiz, TCMAP *cols, TCPROCOP *procop){
  JNIEnv *env = procop->env;
  jbyteArray pkey = (*env)->NewByteArray(env, pksiz);
  (*env)->SetByteArrayRegion(env, pkey, 0, pksiz, (jbyte *)pkbuf);
  jobject map = procop->map;
  (*env)->CallVoidMethod(env, map, procop->midmapclear);
  tcmapiterinit(cols);
  const char *nbuf;
  int nsiz;
  while((nbuf = tcmapiternext(cols, &nsiz)) != NULL){
    int vsiz;
    const char *vbuf = tcmapiterval(nbuf, &vsiz);
    jstring name = (*env)->NewStringUTF(env, nbuf);
    if(!name){
      throwoutmem(env);
      return TDBQPSTOP;
    }
    jbyteArray value = (*env)->NewByteArray(env, vsiz);
    if(!value){
      throwoutmem(env);
      return TDBQPSTOP;
    }
    (*env)->SetByteArrayRegion(env, value, 0, vsiz, (jbyte *)vbuf);
    (*env)->CallObjectMethod(env, map, procop->midmapput, name, value);
    (*env)->DeleteLocalRef(env, value);
    (*env)->DeleteLocalRef(env, name);
  }
  int flags = (*env)->CallIntMethod(env, procop->obj, procop->mid, pkey, map);
  if((*env)->ExceptionOccurred(env) != NULL) flags = TDBQPSTOP;
  if(flags & TDBQPPUT){
    tcmapclear(cols);
    jclass clsstring = procop->clsstring;
    jclass clsbyteary = procop->clsbyteary;
    jobject set = (*env)->CallObjectMethod(env, map, procop->midmapentryset);
    jobject iter = (*env)->CallObjectMethod(env, set, procop->midsetiterator);
    while((*env)->CallBooleanMethod(env, iter, procop->miditerhasnext)){
      jobject entry = (*env)->CallObjectMethod(env, iter, procop->miditernext);
      jobject name = (*env)->CallObjectMethod(env, entry, procop->midentrygk);
      jobject value = (*env)->CallObjectMethod(env, entry, procop->midentrygv);
      if((*env)->IsInstanceOf(env, name, clsstring)){
        jobject nary = (*env)->CallObjectMethod(env, name, procop->midstringgb);
        (*env)->DeleteLocalRef(env, name);
        name = nary;
      }
      if((*env)->IsInstanceOf(env, value, clsstring)){
        jobject vary = (*env)->CallObjectMethod(env, value, procop->midstringgb);
        (*env)->DeleteLocalRef(env, value);
        value = vary;
      }
      if(!(*env)->IsInstanceOf(env, name, clsbyteary) ||
         !(*env)->IsInstanceOf(env, value, clsbyteary)){
        throwillarg(env);
        return TDBQPSTOP;
      }
      jboolean icn;
      jbyte *nbuf = (*env)->GetByteArrayElements(env, name, &icn);
      if(!nbuf){
        throwoutmem(env);
        return TDBQPSTOP;
      }
      int nsiz = (*env)->GetArrayLength(env, name);
      jboolean icv;
      jbyte *vbuf = (*env)->GetByteArrayElements(env, value, &icv);
      if(!vbuf){
        throwoutmem(env);
        return TDBQPSTOP;
      }
      int vsiz = (*env)->GetArrayLength(env, value);
      tcmapputkeep(cols, nbuf, nsiz, vbuf, vsiz);
      if(icv) (*env)->ReleaseByteArrayElements(env, value, vbuf, JNI_ABORT);
      if(icn) (*env)->ReleaseByteArrayElements(env, name, nbuf, JNI_ABORT);
      (*env)->DeleteLocalRef(env, value);
      (*env)->DeleteLocalRef(env, name);
      (*env)->DeleteLocalRef(env, entry);
    }
    (*env)->DeleteLocalRef(env, iter);
    (*env)->DeleteLocalRef(env, set);
  }
  (*env)->DeleteLocalRef(env, pkey);
  return flags;
}
Пример #20
0
/* perform search command */
static int procsearch(const char *path, TCLIST *conds, const char *oname, const char *otype,
                      int omode, int max, int skip, bool pv, bool px, bool kw, bool ph, int bt,
                      bool rm, const char *mtype){
  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(!tctdbopen(tdb, path, (rm ? TDBOWRITER : TDBOREADER) | omode)){
    printerr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  bool err = false;
  TDBQRY *qry = tctdbqrynew(tdb);
  int cnum = tclistnum(conds);
  for(int i = 0; i < cnum - 2; i += 3){
    const char *name = tclistval2(conds, i);
    const char *opstr = tclistval2(conds, i + 1);
    const char *expr  = tclistval2(conds, i + 2);
    int op = tctdbqrystrtocondop(opstr);
    if(op >= 0) tctdbqryaddcond(qry, name, op, expr);
  }
  if(oname){
    int type = tctdbqrystrtoordertype(otype);
    if(type >= 0) tctdbqrysetorder(qry, oname, type);
  }
  tctdbqrysetlimit(qry, max, skip);
  if(rm){
    double stime = tctime();
    if(!tctdbqrysearchout(qry)){
      printerr(tdb);
      err = true;
    }
    double etime = tctime();
    if(ph){
      TCLIST *hints = tcstrsplit(tctdbqryhint(qry), "\n");
      int hnum = tclistnum(hints);
      for(int i = 0; i < hnum; i++){
        const char *hint = tclistval2(hints, i);
        if(*hint == '\0') continue;
        printf("\t:::: %s\n", hint);
      }
      tclistdel(hints);
      printf("\t:::: number of records: %d\n", tctdbqrycount(qry));
      printf("\t:::: elapsed time: %.5f\n", etime - stime);
    }
  } else if(bt > 0){
    double sum = 0;
    for(int i = 1; i <= bt; i++){
      double stime = tctime();
      TCLIST *res = tctdbqrysearch(qry);
      double etime = tctime();
      tclistdel(res);
      printf("%d: %.5f sec.\n", i, etime - stime);
      sum += etime - stime;
    }
    printf("----\n");
    printf("total: %.5f sec. (%.5f s/q = %.5f q/s)\n", sum, sum / bt, bt / sum);
  } else {
    double stime = tctime();
    TCLIST *res;
    TCLIST *hints;
    int count;
    int mtnum = mtype ? tctdbmetastrtosettype(mtype) : -1;
    if(mtnum >= 0){
      TDBQRY *qrys[cnum/3+1];
      int qnum = 0;
      for(int i = 0; i < cnum - 2; i += 3){
        const char *name = tclistval2(conds, i);
        const char *opstr = tclistval2(conds, i + 1);
        const char *expr  = tclistval2(conds, i + 2);
        int op = tctdbqrystrtocondop(opstr);
        if(op >= 0){
          qrys[qnum] = tctdbqrynew(tdb);
          tctdbqryaddcond(qrys[qnum], name, op, expr);
          if(oname){
            int type = tctdbqrystrtoordertype(otype);
            if(type >= 0) tctdbqrysetorder(qrys[qnum], oname, type);
          }
          tctdbqrysetlimit(qrys[qnum], max, skip);
          qnum++;
        }
      }
      res = tctdbmetasearch(qrys, qnum, mtnum);
      hints = qnum > 0 ? tcstrsplit(tctdbqryhint(qrys[0]), "\n") : tclistnew2(1);
      count = qnum > 0 ? tctdbqrycount(qrys[0]) : 0;
      for(int i = 0; i < qnum; i++){
        tctdbqrydel(qrys[i]);
      }
    } else {
      res = tctdbqrysearch(qry);
      hints = tcstrsplit(tctdbqryhint(qry), "\n");
      count = tctdbqrycount(qry);
    }
    double etime = tctime();
    if(max < 0) max = INT_MAX;
    int rnum = tclistnum(res);
    for(int i = 0; i < rnum && max > 0; i++){
      int pksiz;
      const char *pkbuf = tclistval(res, i, &pksiz);
      if(kw){
        TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
        if(cols){
          TCLIST *texts = tctdbqrykwic(qry, cols, NULL, 16, TCKWMUTAB);
          int tnum = tclistnum(texts);
          for(int j = 0; j < tnum && max > 0; j++){
            int tsiz;
            const char *text = tclistval(texts, j, &tsiz);
            printdata(pkbuf, pksiz, px);
            putchar('\t');
            printdata(text, tsiz, px);
            putchar('\n');
            max--;
          }
          tclistdel(texts);
          tcmapdel(cols);
        }
      } else {
        printdata(pkbuf, pksiz, px);
        if(pv){
          TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
          if(cols){
            tcmapiterinit(cols);
            const char *kbuf;
            int ksiz;
            while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){
              int vsiz;
              const char *vbuf = tcmapiterval(kbuf, &vsiz);
              putchar('\t');
              printdata(kbuf, ksiz, px);
              putchar('\t');
              printdata(vbuf, vsiz, px);
            }
            tcmapdel(cols);
          }
        }
        putchar('\n');
        max--;
      }
    }
    if(ph){
      int hnum = tclistnum(hints);
      for(int i = 0; i < hnum; i++){
        const char *hint = tclistval2(hints, i);
        if(*hint == '\0') continue;
        printf("\t:::: %s\n", hint);
      }
      printf("\t:::: number of records: %d\n", count);
      printf("\t:::: elapsed time: %.5f\n", etime - stime);
    }
    tclistdel(hints);
    tclistdel(res);
  }
  tctdbqrydel(qry);
  if(!tctdbclose(tdb)){
    if(!err) printerr(tdb);
    err = true;
  }
  tctdbdel(tdb);
  return err ? 1 : 0;
}
Пример #21
-1
/* perform wicked command */
static int procwicked(const char *path, int rnum, bool mt, int omode){
  my_my_my_iprintf("<Wicked Writing Test>\n  seed=%u  path=%s  rnum=%d  mt=%d  omode=%d\n\n",
          g_randseed, path, rnum, mt, omode);
  bool err = false;
  double stime = tctime();
  TCFDB *fdb = tcfdbnew();
  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);
  if(mt && !tcfdbsetmutex(fdb)){
    eprint(fdb, __LINE__, "tcfdbsetmutex");
    err = true;
  }
  if(!tcfdbtune(fdb, RECBUFSIZ * 2, EXHEADSIZ + (RECBUFSIZ * 2 + sizeof(int)) * rnum)){
    eprint(fdb, __LINE__, "tcfdbtune");
    err = true;
  }
  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){
    eprint(fdb, __LINE__, "tcfdbopen");
    err = true;
  }
  if(!tcfdbiterinit(fdb)){
    eprint(fdb, __LINE__, "tcfdbiterinit");
    err = true;
  }
  TCMAP *map = tcmapnew2(rnum / 5);
  for(int i = 1; i <= rnum && !err; i++){
    uint64_t id = myrand(rnum) + 1;
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
    char vbuf[RECBUFSIZ];
    int vsiz = myrand(RECBUFSIZ);
    memset(vbuf, '*', vsiz);
    vbuf[vsiz] = '\0';
    char *rbuf;
    switch(myrand(16)){
      case 0:
        iputchar('0');
        if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput2");
          err = true;
        }
        tcmapput(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 1:
        iputchar('1');
        if(!tcfdbput3(fdb, kbuf, vbuf)){
          eprint(fdb, __LINE__, "tcfdbput3");
          err = true;
        }
        tcmapput2(map, kbuf, vbuf);
        break;
      case 2:
        iputchar('2');
        if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputkeep2");
          err = true;
        }
        tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 3:
        iputchar('3');
        if(!tcfdbputkeep3(fdb, kbuf, vbuf) && tcfdbecode(fdb) != TCEKEEP){
          eprint(fdb, __LINE__, "tcfdbputkeep3");
          err = true;
        }
        tcmapputkeep2(map, kbuf, vbuf);
        break;
      case 4:
        iputchar('4');
        if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbputcat2");
          err = true;
        }
        tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);
        break;
      case 5:
        iputchar('5');
        if(!tcfdbputcat3(fdb, kbuf, vbuf)){
          eprint(fdb, __LINE__, "tcfdbputcat3");
          err = true;
        }
        tcmapputcat2(map, kbuf, vbuf);
        break;
      case 6:
        iputchar('6');
        if(myrand(10) == 0){
          if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbout2");
            err = true;
          }
          tcmapout(map, kbuf, ksiz);
        }
        break;
      case 7:
        iputchar('7');
        if(myrand(10) == 0){
          if(!tcfdbout3(fdb, kbuf) && tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbout3");
            err = true;
          }
          tcmapout2(map, kbuf);
        }
        break;
      case 8:
        iputchar('8');
        if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz))){
          if(tcfdbecode(fdb) != TCENOREC){
            eprint(fdb, __LINE__, "tcfdbget2");
            err = true;
          }
          rbuf = tcsprintf("[%d]", myrand(i + 1));
          vsiz = strlen(rbuf);
        }
        vsiz += myrand(vsiz);
        rbuf = tcrealloc(rbuf, vsiz + 1);
        for(int j = 0; j < vsiz; j++){
          rbuf[j] = myrand(0x100);
        }
        if(!tcfdbput2(fdb, kbuf, ksiz, rbuf, vsiz)){
          eprint(fdb, __LINE__, "tcfdbput2");
          err = true;
        }
        tcmapput(map, kbuf, ksiz, rbuf, vsiz);
        tcfree(rbuf);
        break;
      case 9:
        iputchar('9');
        if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz)) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget2");
          err = true;
        }
        tcfree(rbuf);
        break;
      case 10:
        iputchar('A');
        if(!(rbuf = tcfdbget3(fdb, kbuf)) && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget3");
          err = true;
        }
        tcfree(rbuf);
        break;
      case 11:
        iputchar('B');
        if(myrand(1) == 0) vsiz = 1;
        if((vsiz = tcfdbget4(fdb, id, vbuf, vsiz)) < 0 && tcfdbecode(fdb) != TCENOREC){
          eprint(fdb, __LINE__, "tcfdbget4");
          err = true;
        }
        break;
      case 12:
        iputchar('C');
        if(myrand(rnum / 128) == 0){
          if(myrand(2) == 0){
            if(!tcfdbiterinit(fdb)){
              eprint(fdb, __LINE__, "tcfdbiterinit");
              err = true;
            }
          } else {
            if(!tcfdbiterinit2(fdb, myrand(rnum) + 1) && tcfdbecode(fdb) != TCENOREC){
              eprint(fdb, __LINE__, "tcfdbiterinit2");
              err = true;
            }
          }
        }
        for(int j = myrand(rnum) / 1000 + 1; j >= 0; j--){
          if(tcfdbiternext(fdb) < 0){
            int ecode = tcfdbecode(fdb);
            if(ecode != TCEINVALID && ecode != TCENOREC){
              eprint(fdb, __LINE__, "tcfdbiternext");
              err = true;
            }
          }
        }
        break;
      default:
        iputchar('@');
        if(myrand(10000) == 0) srand((unsigned int)(tctime() * 1000) % UINT_MAX);
        if(myrand(rnum / 16 + 1) == 0){
          int cnt = myrand(30);
          for(int j = 0; j < rnum && !err; j++){
            ksiz = sprintf(kbuf, "%d", i + j);
            if(tcfdbout2(fdb, kbuf, ksiz)){
              cnt--;
            } else if(tcfdbecode(fdb) != TCENOREC){
              eprint(fdb, __LINE__, "tcfdbout2");
              err = true;
            }
            tcmapout(map, kbuf, ksiz);
            if(cnt < 0) break;
          }
        }
        break;
    }
    if(i % 50 == 0) my_my_my_iprintf(" (%08d)\n", i);
    if(i == rnum / 2){
      if(!tcfdbclose(fdb)){
        eprint(fdb, __LINE__, "tcfdbclose");
        err = true;
      }
      if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){
        eprint(fdb, __LINE__, "tcfdbopen");
        err = true;
      }
    } else if(i == rnum / 4){
      char *npath = tcsprintf("%s-tmp", path);
      if(!tcfdbcopy(fdb, npath)){
        eprint(fdb, __LINE__, "tcfdbcopy");
        err = true;
      }
      TCFDB *nfdb = tcfdbnew();
      if(!tcfdbopen(nfdb, npath, FDBOREADER | omode)){
        eprint(nfdb, __LINE__, "tcfdbopen");
        err = true;
      }
      tcfdbdel(nfdb);
      unlink(npath);
      tcfree(npath);
      if(!tcfdboptimize(fdb, RECBUFSIZ, -1)){
        eprint(fdb, __LINE__, "tcfdboptimize");
        err = true;
      }
      if(!tcfdbiterinit(fdb)){
        eprint(fdb, __LINE__, "tcfdbiterinit");
        err = true;
      }
    } else if(i == rnum / 8){
      if(!tcfdbtranbegin(fdb)){
        eprint(fdb, __LINE__, "tcfdbtranbegin");
        err = true;
      }
    } else if(i == rnum / 8 + rnum / 16){
      if(!tcfdbtrancommit(fdb)){
        eprint(fdb, __LINE__, "tcfdbtrancommit");
        err = true;
      }
    }
  }
  if(rnum % 50 > 0) my_my_my_iprintf(" (%08d)\n", rnum);
  if(!tcfdbsync(fdb)){
    eprint(fdb, __LINE__, "tcfdbsync");
    err = true;
  }
  if(tcfdbrnum(fdb) != tcmaprnum(map)){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  for(int i = 1; i <= rnum && !err; i++){
    char kbuf[RECBUFSIZ];
    int ksiz = sprintf(kbuf, "%d", i);
    int vsiz;
    const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz);
    int rsiz;
    char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);
    if(vbuf){
      iputchar('.');
      if(vsiz > RECBUFSIZ) vsiz = RECBUFSIZ;
      if(!rbuf){
        eprint(fdb, __LINE__, "tcfdbget2");
        err = true;
      } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
        eprint(fdb, __LINE__, "(validation)");
        err = true;
      }
    } else {
      iputchar('*');
      if(rbuf || tcfdbecode(fdb) != TCENOREC){
        eprint(fdb, __LINE__, "(validation)");
        err = true;
      }
    }
    tcfree(rbuf);
    if(i % 50 == 0) my_my_my_iprintf(" (%08d)\n", i);
  }
  if(rnum % 50 > 0) my_my_my_iprintf(" (%08d)\n", rnum);
  tcmapiterinit(map);
  int ksiz;
  const char *kbuf;
  for(int i = 1; (kbuf = tcmapiternext(map, &ksiz)) != NULL; i++){
    iputchar('+');
    int vsiz;
    const char *vbuf = tcmapiterval(kbuf, &vsiz);
    if(vsiz > tcfdbwidth(fdb)) vsiz = tcfdbwidth(fdb);
    int rsiz;
    char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);
    if(!rbuf){
      eprint(fdb, __LINE__, "tcfdbget2");
      err = true;
    } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){
      eprint(fdb, __LINE__, "(validation)");
      err = true;
    }
    tcfree(rbuf);
    if(!tcfdbout2(fdb, kbuf, ksiz)){
      eprint(fdb, __LINE__, "tcfdbout2");
      err = true;
    }
    if(i % 50 == 0) my_my_my_iprintf(" (%08d)\n", i);
  }
  int mrnum = tcmaprnum(map);
  if(mrnum % 50 > 0) my_my_my_iprintf(" (%08d)\n", mrnum);
  if(tcfdbrnum(fdb) != 0){
    eprint(fdb, __LINE__, "(validation)");
    err = true;
  }
  my_my_my_iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));
  my_my_my_iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));
  mprint(fdb);
  sysprint();
  tcmapdel(map);
  if(!tcfdbclose(fdb)){
    eprint(fdb, __LINE__, "tcfdbclose");
    err = true;
  }
  tcfdbdel(fdb);
  my_my_my_iprintf("time: %.3f\n", tctime() - stime);
  my_my_my_iprintf("%s\n\n", err ? "error" : "ok");
  return err ? 1 : 0;
}