static void do_mc_list(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum){ ttservlog(g_serv, TTLOGDEBUG, "doing mc_list command"); TCADB *adb = arg->adb; if(tnum < 1){ ttsockprintf(sock, "CLIENT_ERROR error\r\n"); return; } TCXSTR *xstr = tcxstrnew(); TCXSTR * head = tcxstrnew(); pthread_cleanup_push((void (*)(void *))tcxstrdel, xstr); pthread_cleanup_push((void (*)(void *))tcxstrdel, head); tcadbiterinit(adb); char * key; int list_size; while((key=tcadbiternext2(adb))!=NULL){ tcxstrcat(xstr, key, strlen(key)); tcxstrcat(xstr,"\r\n",2); } list_size=tcxstrsize(xstr); tcxstrprintf(head,"LIST %d\r\n",list_size); tcxstrcat(xstr, "END\r\n",5); if( ttsocksend(sock, tcxstrptr(head), tcxstrsize(head)) && ttsocksend(sock,tcxstrptr(xstr),tcxstrsize(xstr)) ){ req->keep = true; } else { ttservlog(g_serv, TTLOGINFO, "do_mc_list: response failed"); return ; } pthread_cleanup_pop(1); pthread_cleanup_pop(1); }
static PyObject * list2(PyObject *self,PyObject *args){ const char *dbname; if (!PyArg_ParseTuple(args, "s", &dbname)) return NULL; TCADB *adb = openadb(dbname); PyObject* pList = PyList_New(0); if(!tcadbiterinit(adb)){ return NULL; } BDBCUR *cur = tcbdbcurnew(adb->bdb); TCXSTR *key = tcxstrnew(); TCXSTR *val = tcxstrnew(); if(!tcbdbcurfirst(cur) && tcbdbecode(adb->bdb) != TCENOREC){ return NULL; } while(tcbdbcurrec(cur, key, val)){ PyObject* pList1 = PyList_New(0); PyList_Append(pList1,Py_BuildValue("s",tcxstrptr(key))); PyList_Append(pList1,Py_BuildValue("s",tcxstrptr(val))); PyList_Append(pList,pList1); if(!tcbdbcurnext(cur) && tcbdbecode(adb->bdb) != TCENOREC){ return NULL; } } tcxstrdel(val); tcxstrdel(key); tcbdbcurdel(cur); closeadb(adb); return Py_BuildValue("O",pList); }
/* perform list command */ static int proclist(const char *path, int omode, bool pv, bool px){ TCHDB *hdb = tchdbnew(); if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd); if(!tchdbopen(hdb, path, HDBOREADER | omode)){ printerr(hdb); tchdbdel(hdb); return 1; } bool err = false; if(!tchdbiterinit(hdb)){ printerr(hdb); err = true; } TCXSTR *key = tcxstrnew(); TCXSTR *val = tcxstrnew(); while(tchdbiternext3(hdb, key, val)){ printdata(tcxstrptr(key), tcxstrsize(key), px); if(pv){ putchar('\t'); printdata(tcxstrptr(val), tcxstrsize(val), px); } putchar('\n'); } tcxstrdel(val); tcxstrdel(key); if(!tchdbclose(hdb)){ if(!err) printerr(hdb); err = true; } tchdbdel(hdb); return err ? 1 : 0; }
static gearman_return_t _libtokyocabinet_add(gearman_server_st *server, void *context, const void *unique, size_t unique_size, const void *function_name, size_t function_name_size, const void *data, size_t data_size, gearman_job_priority_t priority) { gearman_queue_libtokyocabinet_st *queue= (gearman_queue_libtokyocabinet_st *)context; bool rc; TCXSTR *key; TCXSTR *job_data; gearman_log_debug(server->gearman, "libtokyocabinet add: %.*s", (uint32_t)unique_size, (char *)unique); char key_str[GEARMAN_QUEUE_TOKYOCABINET_MAX_KEY_LEN]; size_t key_length= (size_t)snprintf(key_str, GEARMAN_QUEUE_TOKYOCABINET_MAX_KEY_LEN, "%.*s-%.*s", (int)function_name_size, (const char *)function_name, (int)unique_size, (const char *)unique); key= tcxstrnew(); tcxstrcat(key, key_str, (int)key_length); gearman_log_debug(server->gearman, "libtokyocabinet key: %.*s", (int)key_length, key_str); job_data= tcxstrnew(); tcxstrcat(job_data, (const char *)function_name, (int)function_name_size); tcxstrcat(job_data, "\0", 1); tcxstrcat(job_data, (const char *)unique, (int)unique_size); tcxstrcat(job_data, "\0", 1); switch (priority) { case GEARMAN_JOB_PRIORITY_HIGH: case GEARMAN_JOB_PRIORITY_MAX: tcxstrcat2(job_data,"0"); break; case GEARMAN_JOB_PRIORITY_LOW: tcxstrcat2(job_data,"2"); break; case GEARMAN_JOB_PRIORITY_NORMAL: default: tcxstrcat2(job_data,"1"); } tcxstrcat(job_data, (const char *)data, (int)data_size); rc= tcadbput(queue->db, tcxstrptr(key), tcxstrsize(key), tcxstrptr(job_data), tcxstrsize(job_data)); tcxstrdel(key); tcxstrdel(job_data); if (!rc) return GEARMAN_QUEUE_ERROR; return GEARMAN_SUCCESS; }
/* 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; }
void ajax_pager(tpl_t *tpl, double total, double slot, uint pos, uint limit[2]) { TCXSTR *str; uint n, i, cur; str = tcxstrnew(); n = (uint)ceil(total / slot); if(pos < 1) pos = 1; if(pos > n) pos = n; limit[1] = slot; limit[0] = (pos == 1) ? 0 : (pos-1) * limit[1]; for(i=0; i<n; i++) { cur = (i+1); if(cur == pos) tcxstrprintf(str, "<a class='page_link' href='javascript:void(0);'>%d</a> ", cur); else tcxstrprintf(str, "<a href='#' onclick='return loading(%d)'>%d</a> ", cur, cur); } tpl_set_field_global(tpl, "page", tcxstrptr(str), tcxstrsize(str)); tcxstrdel(str); }
int print_bson(lua_State *L) { lua_settop(L, 1); size_t slen = 0; const void *bsdata = luaL_checklstring(L, lua_gettop(L), &slen); if (slen <= 4 || !bsdata) { return luaL_error(L, "Invalid bson string at argument #1"); } TCXSTR *xstr = tcxstrnew(); bson_print_xstr(xstr, bsdata, 0); lua_pushstring(L, TCXSTRPTR(xstr)); tcxstrdel(xstr); return 1; }
void driveInterctiveLoop(FILE *fpin, FILE *fpout, int evalLine){ int maxSize = 10000; char inputs[maxSize]; if(evalLine){ while(fgets(inputs, maxSize, fpin) != NULL){ printf("input: %s\n", inputs); fprintExp(eval(readScheme(inputs), sGlobalEnvironment), fpout); } }else{ TCXSTR *temp = tcxstrnew(); while(fread(inputs, sizeof(char), maxSize, fpin) > 0){ tcxstrcat2(temp, inputs); } fprintExp(eval(readScheme(tcxstrptr(temp)), sGlobalEnvironment), fpout); } }
void testBSONExportImport(void) { EJDB *jb = ejdbnew(); CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_export", JBOWRITER | JBOCREAT | JBOTRUNC)); EJCOLL *coll = ejdbcreatecoll(jb, "col1", NULL); if (!coll) { eprint(jb, __LINE__, "testBSONExportImport"); } CU_ASSERT_TRUE(coll != NULL); bson_oid_t oid; bson bv1; bson_init(&bv1); bson_append_int(&bv1, "a", 1); bson_append_string(&bv1, "c", "d"); bson_finish(&bv1); ejdbsavebson(coll, &bv1, &oid); bson_destroy(&bv1); EJCOLLOPTS copts = {0}; copts.large = true; copts.records = 200000; coll = ejdbcreatecoll(jb, "col2", &copts); if (!coll) { eprint(jb, __LINE__, "testBSONExportImport"); } CU_ASSERT_TRUE(coll != NULL); CU_ASSERT_TRUE(ejdbsetindex(coll, "f", JBIDXSTR | JBIDXNUM)); bson_init(&bv1); bson_append_int(&bv1, "e", 1); bson_append_string(&bv1, "f", "g"); bson_finish(&bv1); ejdbsavebson(coll, &bv1, &oid); bson_destroy(&bv1); bson_init(&bv1); bson_append_int(&bv1, "e", 2); bson_append_string(&bv1, "f", "g2"); bson_finish(&bv1); ejdbsavebson(coll, &bv1, &oid); bson_destroy(&bv1); TCXSTR *log = tcxstrnew(); TCLIST *cnames = tclistnew(); tclistpush2(cnames, "col1"); tclistpush2(cnames, "col2"); bool rv = ejdbexport(jb, "testBSONExportImport", NULL, 0, log); if (!rv) { eprint(jb, __LINE__, "testBSONExportImport"); } CU_ASSERT_TRUE(rv); bson *ometa = ejdbmeta(jb); CU_ASSERT_TRUE_FATAL(ometa != NULL); ejdbclose(jb); ejdbdel(jb); //Restore data: jb = ejdbnew(); CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_export", JBOWRITER | JBOCREAT)); coll = ejdbgetcoll(jb, "col1"); CU_ASSERT_PTR_NOT_NULL_FATAL(coll); bson_init(&bv1); bson_append_int(&bv1, "e", 2); bson_finish(&bv1); CU_ASSERT_TRUE(ejdbsavebson(coll, &bv1, &oid)); bson_destroy(&bv1); rv = ejdbimport(jb, "testBSONExportImport", cnames, JBIMPORTREPLACE, log); CU_ASSERT_TRUE(rv); //fprintf(stderr, "\n\n%s", TCXSTRPTR(log)); CU_ASSERT_PTR_NOT_NULL(strstr(TCXSTRPTR(log), "Replacing all data in 'col1'")); CU_ASSERT_PTR_NOT_NULL(strstr(TCXSTRPTR(log), "1 objects imported into 'col1'")); CU_ASSERT_PTR_NOT_NULL(strstr(TCXSTRPTR(log), "2 objects imported into 'col2'")); bson *nmeta = ejdbmeta(jb); CU_ASSERT_TRUE_FATAL(nmeta != NULL); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.0.name", strlen("collections.0.name")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.0.records", strlen("collections.0.records")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.name", strlen("collections.1.name")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.records", strlen("collections.1.records")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.options.buckets", strlen("collections.1.options.buckets")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.options.large", strlen("collections.1.options.large")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.0.field", strlen("collections.1.indexes.0.field")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.0.type", strlen("collections.1.indexes.0.type")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.0.records", strlen("collections.1.indexes.0.records")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.1.field", strlen("collections.1.indexes.1.field")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.1.type", strlen("collections.1.indexes.1.type")) == 0); CU_ASSERT_TRUE(bson_compare(bson_data(ometa), bson_data(nmeta), "collections.1.indexes.1.records", strlen("collections.1.indexes.1.records")) == 0); ejdbclose(jb); ejdbdel(jb); jb = ejdbnew(); CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_export", JBOWRITER | JBOCREAT | JBOTRUNC)); coll = ejdbcreatecoll(jb, "col1", NULL); CU_ASSERT_PTR_NOT_NULL_FATAL(coll); bson_init(&bv1); bson_append_int(&bv1, "e", 2); bson_finish(&bv1); CU_ASSERT_TRUE(ejdbsavebson(coll, &bv1, &oid)); EJQ *q = ejdbcreatequery(jb, &bv1, NULL, 0, NULL); CU_ASSERT_PTR_NOT_NULL_FATAL(q); uint32_t count = 0; ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL); CU_ASSERT_EQUAL(count, 1); rv = ejdbimport(jb, "testBSONExportImport", NULL, JBIMPORTUPDATE, NULL); CU_ASSERT_TRUE(rv); coll = ejdbcreatecoll(jb, "col1", NULL); ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL); CU_ASSERT_EQUAL(count, 1); ejdbquerydel(q); bson_destroy(&bv1); ejdbclose(jb); ejdbdel(jb); bson_del(ometa); bson_del(nmeta); tcxstrdel(log); tclistdel(cnames); }
static void *threadrace1(void *_tr) { const int iterations = 500; TARGRACE *tr = (TARGRACE*) _tr; bool err = false; bson bq; bson_init_as_query(&bq); bson_append_int(&bq, "tid", tr->id); bson_finish(&bq); bson_type bt; bson_iterator it; void *bsdata; bool saved = false; int lastcnt = 0; EJCOLL *coll = ejdbcreatecoll(jb, "threadrace1", NULL); CU_ASSERT_PTR_NOT_NULL_FATAL(coll); EJQ *q = ejdbcreatequery(jb, &bq, NULL, 0, NULL); TCXSTR *log = tcxstrnew(); for (int i = 0; !err && i < iterations; ++i) { CU_ASSERT_PTR_NOT_NULL_FATAL(q); tcxstrclear(log); bson_oid_t oid2; bson_oid_t *oid = NULL; int cnt = 0; uint32_t count; TCLIST *res = NULL; if (ejdbecode(jb) != 0) { eprint(jb, __LINE__, "threadrace1"); err = true; goto ffinish; } res = ejdbqryexecute(coll, q, &count, 0, log); if (ejdbecode(jb) != 0) { eprint(jb, __LINE__, "threadrace1.ejdbqryexecute"); err = true; goto ffinish; } if (count != 1 && saved) { fprintf(stderr, "%d:COUNT=%d it=%d\n", tr->id, count, i); CU_ASSERT_TRUE(false); goto ffinish; } if (count > 0) { bsdata = TCLISTVALPTR(res, 0); CU_ASSERT_PTR_NOT_NULL_FATAL(bsdata); bt = bson_find_from_buffer(&it, bsdata, "cnt"); CU_ASSERT_EQUAL_FATAL(bt, BSON_INT); cnt = bson_iterator_int(&it); bt = bson_find_from_buffer(&it, bsdata, "_id"); CU_ASSERT_EQUAL_FATAL(bt, BSON_OID); oid = bson_iterator_oid(&it); CU_ASSERT_PTR_NOT_NULL_FATAL(oid); } bson sbs; bson_init(&sbs); if (oid) { bson_append_oid(&sbs, "_id", oid); } bson_append_int(&sbs, "tid", tr->id); bson_append_int(&sbs, "cnt", ++cnt); bson_finish(&sbs); if (!ejdbsavebson(coll, &sbs, &oid2)) { eprint(jb, __LINE__, "threadrace1.ejdbsavebson"); err = true; } saved = true; bson_destroy(&sbs); lastcnt = cnt; ffinish: if (res) tclistdel(res); } if (q) ejdbquerydel(q); if (log) tcxstrdel(log); bson_destroy(&bq); CU_ASSERT_EQUAL(lastcnt, iterations); //fprintf(stderr, "\nThread %d finished", tr->id); return err ? "error" : NULL; }
/* * Class: org_ejdb_driver_EJDBQuery * Method: execute * Signature: (Lorg/ejdb/bson/BSONObject;[Lorg/ejdb/bson/BSONObject;Lorg/ejdb/bson/BSONObject;ILjava/io/OutputStream;)Lorg/ejdb/driver/EJDBQuery$QResult; */ JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBQuery_execute (JNIEnv *env, jobject obj, jobject qobj, jobjectArray qorarrobj, jobject hobj, jint flags, jobject logstream) { jclass jQResultClazz = (*env)->FindClass(env, "org/ejdb/driver/EJDBQuery$QResult"); jmethodID initQResultMethodID = (*env)->GetMethodID(env, jQResultClazz, "<init>", "(IJ)V"); TCXSTR *log = NULL; bson *qbson = NULL; bson *qorbsons = NULL; bson *qhbson = NULL; EJQ *q = NULL; jobject qresult = NULL; EJDB* db = get_ejdb_from_object(env, obj); if (!ejdbisopen(db)) { set_error(env, 0, "EJDB not opened"); goto finish; } qbson = encode_bson(env, qobj, NULL); if (!qbson) { // TODO: ? goto finish; } jsize qorz = NULL != qorarrobj ? (*env)->GetArrayLength(env, qorarrobj) : 0; if (qorz > 0) { qorbsons = (bson*)malloc(qorz * sizeof(bson)); if (!qorbsons) { set_error(env, 0, "Not enought memory"); goto finish; } for (jsize i = 0; i < qorz; ++i) { jobject qorobj = (*env)->GetObjectArrayElement(env, qorarrobj, i); encode_bson(env, qorobj, &qorbsons[i]); } } if (NULL != hobj){ qhbson = encode_bson(env, hobj, NULL); } q = ejdbcreatequery(db, qbson, qorz > 0 ? qorbsons : NULL, qorz, qhbson); if (!q) { set_ejdb_error(env, db); goto finish; } jstring colname = get_coll_name(env, obj); const char *cname = (*env)->GetStringUTFChars(env, colname, NULL); EJCOLL *coll = ejdbgetcoll(db, cname); if (!coll) { bson_iterator it; //If we are in $upsert mode a new collection will be created if (bson_find(&it, qbson, "$upsert") == BSON_OBJECT) { coll = ejdbcreatecoll(db, cname, NULL); (*env)->ReleaseStringUTFChars(env, colname, cname); if (!coll) { set_ejdb_error(env, db); goto finish; } } } else { (*env)->ReleaseStringUTFChars(env, colname, cname); } uint32_t count = 0; TCLIST *qres = NULL; if (!coll) { //No collection -> no results qres = (flags & JBQRYCOUNT) ? NULL : tclistnew2(1); //empty results } else { if (NULL != logstream) { log = tcxstrnew(); } qres = ejdbqryexecute(coll, q, &count, flags, log); if (ejdbecode(db) != TCESUCCESS) { set_ejdb_error(env, db); goto finish; } } qresult = (*env)->NewObject(env, jQResultClazz, initQResultMethodID, (jint)count, (jlong)qres); finish: // clear if (log) { jclass logstreamClazz = (*env)->GetObjectClass(env, logstream); jmethodID writeMethodID = (*env)->GetMethodID(env, logstreamClazz, "write", "([B)V"); jmethodID flushMethodID = (*env)->GetMethodID(env, logstreamClazz, "flush", "()V"); jsize logLength = TCXSTRSIZE(log); jbyteArray jlogdata = (*env)->NewByteArray(env, logLength); (*env)->SetByteArrayRegion(env, jlogdata, 0, logLength, (jbyte*)TCXSTRPTR(log)); (*env)->CallVoidMethod(env, logstream, writeMethodID, jlogdata); (*env)->DeleteLocalRef(env, jlogdata); (*env)->CallVoidMethod(env, logstream, flushMethodID); tcxstrdel(log); } if (qbson) { bson_del(qbson); } if (qorbsons) { for (int i = 0; i < qorz; ++i) { bson_destroy(&qorbsons[i]); } free(qorbsons); } if (qhbson) { bson_del(qhbson); } if (q) { ejdbquerydel(q); } return qresult; };
/* perform list command */ static int proclist(const char *path, TCCMP cmp, int omode, int max, bool pv, bool px, bool bk, const char *jstr, const char *bstr, const char *estr, const char *fmstr) { TCBDB *bdb = tcbdbnew(); if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd); if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb); if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb); if (!tcbdbopen(bdb, path, BDBOREADER | omode)) { printerr(bdb); tcbdbdel(bdb); return 1; } bool err = false; if (bstr || fmstr) { TCLIST *keys = fmstr ? tcbdbfwmkeys2(bdb, fmstr, max) : tcbdbrange(bdb, bstr, strlen(bstr), true, estr, strlen(estr), true, max); int cnt = 0; for (int i = 0; i < tclistnum(keys); i++) { int ksiz; const char *kbuf = tclistval(keys, i, &ksiz); if (pv) { TCLIST *vals = tcbdbget4(bdb, kbuf, ksiz); if (vals) { for (int j = 0; j < tclistnum(vals); j++) { int vsiz; const char *vbuf = tclistval(vals, j, &vsiz); printdata(kbuf, ksiz, px); putchar('\t'); printdata(vbuf, vsiz, px); putchar('\n'); if (max >= 0 && ++cnt >= max) break; } tclistdel(vals); } } else { int num = tcbdbvnum(bdb, kbuf, ksiz); for (int j = 0; j < num; j++) { printdata(kbuf, ksiz, px); putchar('\n'); if (max >= 0 && ++cnt >= max) break; } } if (max >= 0 && cnt >= max) break; } tclistdel(keys); } else { BDBCUR *cur = tcbdbcurnew(bdb); if (bk) { if (jstr) { if (!tcbdbcurjumpback(cur, jstr, strlen(jstr)) && tcbdbecode(bdb) != TCENOREC) { printerr(bdb); err = true; } } else { if (!tcbdbcurlast(cur) && tcbdbecode(bdb) != TCENOREC) { printerr(bdb); err = true; } } } else { if (jstr) { if (!tcbdbcurjump(cur, jstr, strlen(jstr)) && tcbdbecode(bdb) != TCENOREC) { printerr(bdb); err = true; } } else { if (!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC) { printerr(bdb); err = true; } } } TCXSTR *key = tcxstrnew(); TCXSTR *val = tcxstrnew(); int cnt = 0; while (tcbdbcurrec(cur, key, val)) { printdata(tcxstrptr(key), tcxstrsize(key), px); if (pv) { putchar('\t'); printdata(tcxstrptr(val), tcxstrsize(val), px); } putchar('\n'); if (bk) { if (!tcbdbcurprev(cur) && tcbdbecode(bdb) != TCENOREC) { printerr(bdb); err = true; } } else { if (!tcbdbcurnext(cur) && tcbdbecode(bdb) != TCENOREC) { printerr(bdb); err = true; } } if (max >= 0 && ++cnt >= max) break; } tcxstrdel(val); tcxstrdel(key); tcbdbcurdel(cur); } if (!tcbdbclose(bdb)) { if (!err) printerr(bdb); err = true; } tcbdbdel(bdb); return err ? 1 : 0; }
int main(int argc, char **argv) { TCXSTR *pattern, *key_root; int ecode; bool verbose, test, create, extract, optimize, resume; int o; verbose = test = create = extract = optimize = resume = false; pattern = tcxstrnew(); key_root = tcxstrnew(); db_file = tcxstrnew(); tdb_host = tcxstrnew(); tdb_port = 0; while (-1 != (o = getopt(argc, argv, "cxtvrof:p:k:H:P:"))) { switch (o) { case 'f': use_cabinet_lib = true; tcxstrcat2(db_file, optarg); /* create the object */ hdb = tchdbnew(); break; case 'H': use_cabinet_lib = false; tcxstrcat2(tdb_host, optarg); tdb = tcrdbnew(); break; case 'P': tdb_port = strtol(optarg, NULL, 0); break; case 'k': tcxstrcat2(key_root, optarg); // add trailing slash if needed char *last_c; last_c = tcxstrptr(key_root) + tcxstrsize(key_root) - 1; if (*last_c != '/') { tcxstrcat2(key_root, "/"); } break; case 'p': tcxstrcat2(pattern, optarg); break; case 'v': verbose = true; break; case 't': test = true; break; case 'c': create = true; break; case 'x': extract = true; break; case 'r': resume = true; break; case 'o': optimize = true; case '?': default: break; } } if (!create && !extract) { fprintf(stdout, "No action specifed. Use -c to create DB and -x to extract files from dbfile.tch\n"); return 1; } if ((use_cabinet_lib && NULL == hdb) || (!use_cabinet_lib && NULL == tdb)) { fprintf(stdout, "No database specifed. Use -f dbfile.tch\n"); return 1; } if (0 == tcxstrsize(pattern)) { fprintf(stdout, "No pattern is given. Using *. Use -p <glob_pattern> to override\n"); tcxstrcat2(pattern, "*"); } if (create) { glob_t gtree; glob(tcxstrptr(pattern), GLOB_NOSORT, NULL, >ree); size_t found = gtree.gl_pathc; if (use_cabinet_lib && !open_cabinet_db((int64_t) found, optimize)) return 2; if (!use_cabinet_lib && !open_tyrant_db()) return 3; int i; for (i = 0; i < found; i++) { char * fname = gtree.gl_pathv[i]; if (verbose || test) fprintf(stdout, "\n%d of %d - packing file: %s ...", i, found, fname); if (!test) pack_file(fname, key_root, resume); } fprintf(stdout, "Finished. Processed %d items\n", (int) found); globfree(>ree); } else if (extract) { if (!open_cabinet_db(0, false)) return 2; int count; count = unpack_files(NULL, verbose, test); fprintf(stdout, "Finished. Processed %d items\n", count); } /* close the database */ close_db(); /* delete the objects */ tcxstrdel(pattern); tcxstrdel(key_root); return 0; }
nlohmann::json collection::query(nlohmann::json::object_t const& selector, nlohmann::json::object_t const& modifier, int flags) throw(ejdb_exception) { nlohmann::json q1 = selector; nlohmann::json q2 = modifier; bool with_modifier = !q2.empty(); if(with_modifier) { std::swap(q1, q2); } auto* ejdb_query = ejdbcreatequery(_db.get(), convert_to_bson(q1).get(), with_modifier ? convert_to_bson(q2).get() : nullptr, (int)with_modifier, nullptr); if(!ejdb_query) { throw_last_ejdb_exception(); } uint32_t count; std::shared_ptr<TCXSTR> log(tcxstrnew(), tcxstrdel); auto cursor = ejdbqryexecute(_coll.get(), ejdb_query, &count, flags, log.get()); ejdbquerydel(ejdb_query); nlohmann::json updates; nlohmann::json upserts; nlohmann::json dropall; nlohmann::json const json_log = evaluate_log(std::string(log->ptr, log->size)); if(json_log.find("updating_mode") != json_log.end() && json_log["updating_mode"]) { if(json_log.find("$update") != json_log.end()) { updates = json_log["$update"]; if(updates.type() != nlohmann::json::value_t::array) { updates = nlohmann::json::array_t({ updates }); } } if(json_log.find("$upsert") != json_log.end()) { upserts = json_log["$upsert"]; if(upserts.type() != nlohmann::json::value_t::array) { upserts = nlohmann::json::array_t({ upserts }); } for(nlohmann::json::object_t doc: upserts) { std::string const id = doc["_id"]; doc.erase("_id"); document_added(id, doc); } } if(json_log.find("$dropall") != json_log.end()) { for(std::string const& id: json_log["$dropall"]) { dropall.push_back(id); } } } nlohmann::json return_val; if(flags & JBQRYCOUNT) { return_val = count; } else { std::vector<nlohmann::json::object_t> results; results.reserve(count); for(int i = 0; i < count; ++i) { int data_size = 0; auto const& result = convert_to_json(std::shared_ptr<bson>(bson_create_from_buffer(static_cast<char const*>(ejdbqresultbsondata(cursor, i, &data_size)), data_size), bson_destroy)); if(i < updates.size()) { auto const& update = updates[i]; if(result != update) { auto const diff = modified_fields(result, update); document_pre_changed(result["_id"], result, update); document_changed(result["_id"], diff["fields"], diff["cleared"]); } } if(i < dropall.size()) { std::string const& id = dropall[i]; if(result["_id"] == id) { document_pre_removed(id, result); document_removed(id); } } results.push_back(result); } ejdbqresultdispose(cursor); return_val = results; } if(flags & JBQRYFINDONE) { return_val = return_val.empty() ? nlohmann::json::object() : return_val[0]; } return return_val; }
static TCXSTR *getTypeTag(char *name){ TCXSTR *str = tcxstrnew(); tcxstrcat2(str, name); tcxstrcat2(str, "_type"); return str; }