/* 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 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); }
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); }
/* perform convert command */ static int procconvert(const char *ibuf, int isiz, int fmt, const char *buri, const char *duri, bool page){ TCMAP *cols = tcmapnew2(TINYBNUM); wikiload(cols, ibuf); if(fmt == FMTWIKI){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); wikidump(rbuf, cols); fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); } else if(fmt == FMTTEXT){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); if(page) tcxstrprintf(rbuf, "------------------------ Tokyo Promenade ------------------------\n"); wikidumptext(rbuf, cols); if(page) tcxstrprintf(rbuf, "-----------------------------------------------------------------\n"); fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); } else if(fmt == FMTHTML){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); if(page){ const char *name = tcmapget2(cols, "name"); tcxstrprintf(rbuf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); tcxstrprintf(rbuf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"); tcxstrprintf(rbuf, "<html xmlns=\"http://www.w3.org/1999/xhtml\"" " xml:lang=\"en\" lang=\"en\">\n"); tcxstrprintf(rbuf, "<head>\n"); tcxstrprintf(rbuf, "<meta http-equiv=\"Content-Type\"" " content=\"text/html; charset=UTF-8\" />\n"); tcxstrprintf(rbuf, "<link rel=\"contents\" href=\"%@\" />\n", buri); tcxstrprintf(rbuf, "<title>%@</title>\n", name ? name : "Tokyo Promenade"); tcxstrprintf(rbuf, "</head>\n"); tcxstrprintf(rbuf, "<body>\n"); } wikidumphtml(rbuf, cols, buri, 0, duri); if(page){ tcxstrprintf(rbuf, "</body>\n"); tcxstrprintf(rbuf, "</html>\n"); } fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); } tcmapdel(cols); return 0; }
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 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; }
int pack_file(char *file, TCXSTR *root_key, bool resume) { struct stat st; int ecode; TCXSTR *key; key = tcxstrdup(root_key); size_t file_path_len; file_path_len = strlen(file); char *file_name; file_name = get_file_name(file, file_path_len); tcxstrcat2(key, file_name); if (resume && key_exists(tcxstrptr(key))) { fprintf(stdout, "already exists"); return; } if (-1 == stat(file, &st)) { return 1; } if (!S_ISREG(st.st_mode)) { fprintf(stdout, "Not regular file: %s", file); return 2; } int fd; if (-1 == (fd = open(file, O_RDONLY))) { fprintf(stdout, "***Failed open file %s", file); return 1; } void *fmap; if (MAP_FAILED == (fmap = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))) { fprintf(stdout, "mmaping failed for %s", file); close(fd); return -1; } /* store records */ if (use_cabinet_lib) { if (!tchdbput(hdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) { ecode = tchdbecode(hdb); fprintf(stdout, "put error: %s", tchdberrmsg(ecode)); } } else { if (!tcrdbput(tdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) { ecode = tcrdbecode(tdb); fprintf(stdout, "put error: %s", tcrdberrmsg(ecode)); } } fprintf(stdout, "%d bytes", st.st_size); munmap(fmap, st.st_size); close(fd); tcxstrdel(key); }
/* perform export command */ static int procexport(const char *dbpath, int64_t id, const char *dirpath){ TCTDB *tdb = tctdbnew(); if(!tctdbopen(tdb, dbpath, TDBOREADER)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(id > 0){ char pkbuf[NUMBUFSIZ]; int pksiz = sprintf(pkbuf, "%lld", (long long)id); TCMAP *cols = tctdbget(tdb, pkbuf, pksiz); if(cols){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); wikidump(rbuf, cols); fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); tcmapdel(cols); } else { printdberr(tdb); err = true; } } else { if(!dirpath) dirpath = "."; if(!tctdbiterinit(tdb)){ printdberr(tdb); err = true; } char *pkbuf; int pksiz; while((pkbuf = tctdbiternext(tdb, &pksiz)) != NULL){ TCMAP *cols = tctdbget(tdb, pkbuf, pksiz); if(cols){ char *name = tcstrdup(tcmapget4(cols, "name", "")); tcstrcututf(name, 32); char *enc = pathencode(name); char *path = tcsprintf("%s/%s-%s.tpw", dirpath, pkbuf, enc); TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); wikidump(rbuf, cols); if(tcwritefile(path, tcxstrptr(rbuf), tcxstrsize(rbuf))){ printf("%s: exported: id=%s name=%s\n", path, pkbuf, name); } else { printf("%s: writing failed\n", path); err = true; } tcxstrdel(rbuf); tcfree(path); tcfree(enc); tcfree(name); tcmapdel(cols); } else { printdberr(tdb); err = true; } tcfree(pkbuf); } } if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }