int init_suite(void) { assert(QRS < RS); jb = ejdbnew(); if (!ejdbopen(jb, "dbt3", JBOWRITER | JBOCREAT | JBOTRUNC)) { return 1; } srand(tcmstime()); recs = malloc(RS * sizeof (bson)); if (!recs) { return 1; } for (int i = 0; i < RS; ++i) { bson bs; bson_init(&bs); bson_append_long(&bs, "ts", tcmstime()); char str[128]; int len = 0; do { len = rand() % 128; } while (len <= 0); str[0] = 'A' + (i % 26); for (int j = 1; j < len; ++j) { str[j] = 'a' + rand() % 26; } str[len] = '\0'; bson_append_string(&bs, "rstring", str); bson_finish(&bs); recs[i] = bs; } return 0; }
int init_suite(void) { jb = ejdbnew(); if (!ejdbopen(jb, "dbt1", JBOWRITER | JBOCREAT | JBOTRUNC)) { return 1; } return 0; }
/* * Class: org_ejdb_driver_EJDB * Method: open * Signature: (Ljava/lang/String;I)V */ JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDB_open (JNIEnv *env, jobject obj, jstring path, jint mode) { EJDB* db = get_ejdb_from_object(env, obj); if (ejdbisopen(db)) { set_error(env, 0, "EJDB already opened"); return; } db = ejdbnew(); if (!db) { set_error(env, 0, "Could not create EJDB"); return; } const char *dbpath = (*env)->GetStringUTFChars(env, path, NULL); bool status = ejdbopen(db, dbpath, mode); (*env)->ReleaseStringUTFChars(env, path, dbpath); if (!status) { set_ejdb_error(env, db); return; } set_ejdb_to_object(env, obj, db); Java_org_ejdb_driver_EJDB_updateMeta(env, obj); return; };
void testTicket53(void) { EJDB *jb = ejdbnew(); CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_53", JBOWRITER | JBOCREAT)); ejdbclose(jb); ejdbdel(jb); jb = ejdbnew(); CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_53", JBOWRITER)); EJCOLL *coll = ejdbcreatecoll(jb, "mycoll", NULL); if (!coll) { eprint(jb, __LINE__, "testTicket53"); } CU_ASSERT_TRUE(coll != NULL); ejdbclose(jb); ejdbdel(jb); }
collection::collection(std::string const& name) throw(ejdb_exception) { if(name.empty()) { throw ejdb_exception(JBEINVALIDCOLNAME); } std::call_once(bson_oid_setup_flag, std::bind(bson_set_oid_inc, []() -> int { static int i = 0; return i++; })); if(!(_db = db.lock())) { _db = std::shared_ptr<EJDB>(ejdbnew(), ejdbdel); if(!ejdbopen(_db.get(), "meteorpp.db", JBOWRITER | JBOCREAT | JBOTRUNC)) { throw_last_ejdb_exception(); } db = _db; } _coll.reset(ejdbcreatecoll(_db.get(), name.c_str(), nullptr), ejdbsyncoll); }
int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "en_US.UTF-8"); jb = ejdbnew(); if (!ejdbopen(jb, "addressbook", JBOWRITER | JBOCREAT | JBOTRUNC)) { return 1; } //Get or create collection 'contacts' EJCOLL *coll = ejdbcreatecoll(jb, "contacts", NULL); bson bsrec; bson_oid_t oid; //One record bson_init(&bsrec); bson_append_string(&bsrec, "name", "John Travolta"); bson_append_string(&bsrec, "phone", "333-222-333"); bson_append_int(&bsrec, "age", 58); bson_finish(&bsrec); ejdbsavebson(coll, &bsrec, &oid); fprintf(stderr, "\nSaved Travolta"); bson_destroy(&bsrec); //Another record bson_init(&bsrec); bson_append_string(&bsrec, "name", "Bruce Willis"); bson_append_string(&bsrec, "phone", "222-333-222"); bson_append_int(&bsrec, "age", 57); bson_finish(&bsrec); ejdbsavebson(coll, &bsrec, &oid); fprintf(stderr, "\nSaved Bruce Willis"); bson_destroy(&bsrec); //Now select one record. //Query: {'name' : {'$begin' : 'Bru'}} //Name starts with 'Bru' string bson bq1; bson_init_as_query(&bq1); bson_append_start_object(&bq1, "name"); bson_append_string(&bq1, "$begin", "Bru"); bson_append_finish_object(&bq1); bson_finish(&bq1); EJQ *q1 = ejdbcreatequery(jb, &bq1, NULL, 0, NULL); uint32_t count; TCLIST *res = ejdbqryexecute(coll, q1, &count, 0, NULL); fprintf(stderr, "\n\nRecords found: %d\n", count); for (int i = 0; i < TCLISTNUM(res); ++i) { void *bsdata = TCLISTVALPTR(res, i); bson_print_raw((char*) bsdata, 0); } fprintf(stderr, "\n"); //Dispose result set tclistdel(res); //Dispose query ejdbquerydel(q1); bson_destroy(&bq1); //Close database ejdbclose(jb); ejdbdel(jb); getc(stdin); return 0; }
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); }
void testBSONExportImport2(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__, "testBSONExportImport2"); } CU_ASSERT_TRUE(coll != NULL); bson_oid_t oid; const char *log = NULL; 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__, "testBSONExportImport2"); } 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); bson cmd; bson_init(&cmd); bson_append_start_object(&cmd, "export"); bson_append_string(&cmd, "path", "testBSONExportImport2"); bson_append_start_array(&cmd, "cnames"); bson_append_string(&cmd, "0", "col1"); bson_append_string(&cmd, "1", "col2"); bson_append_finish_array(&cmd); bson_append_finish_object(&cmd); bson_finish(&cmd); bson *bret = ejdbcommand(jb, &cmd); CU_ASSERT_PTR_NOT_NULL_FATAL(bret); bson_destroy(&cmd); bson_iterator it; bson_iterator_init(&it, bret); CU_ASSERT_TRUE(bson_find_fieldpath_value("error", &it) == BSON_EOO); bson_iterator_init(&it, bret); CU_ASSERT_TRUE(bson_compare_long(0, bson_data(bret), "errorCode") == 0); bson_iterator_init(&it, bret); CU_ASSERT_TRUE(bson_find_fieldpath_value("log", &it) == BSON_STRING); bson_del(bret); 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); bson_init(&cmd); bson_append_start_object(&cmd, "import"); bson_append_string(&cmd, "path", "testBSONExportImport2"); bson_append_int(&cmd, "mode", JBIMPORTREPLACE); bson_append_start_array(&cmd, "cnames"); bson_append_string(&cmd, "0", "col1"); bson_append_string(&cmd, "1", "col2"); bson_append_finish_array(&cmd); bson_append_finish_object(&cmd); bson_finish(&cmd); bret = ejdbcommand(jb, &cmd); CU_ASSERT_PTR_NOT_NULL_FATAL(bret); bson_destroy(&cmd); bson_iterator_init(&it, bret); CU_ASSERT_TRUE_FATAL(bson_find_fieldpath_value("log", &it) == BSON_STRING); log = bson_iterator_string(&it); CU_ASSERT_PTR_NOT_NULL(strstr(log, "Replacing all data in 'col1'")); CU_ASSERT_PTR_NOT_NULL(strstr(log, "1 objects imported into 'col1'")); CU_ASSERT_PTR_NOT_NULL(strstr(log, "2 objects imported into 'col2'")); bson_del(bret); log = NULL; 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); bson_init(&cmd); bson_append_start_object(&cmd, "import"); bson_append_string(&cmd, "path", "testBSONExportImport2"); bson_append_int(&cmd, "mode", JBIMPORTUPDATE); bson_append_finish_object(&cmd); bson_finish(&cmd); bret = ejdbcommand(jb, &cmd); CU_ASSERT_PTR_NOT_NULL_FATAL(bret); bson_destroy(&cmd); bson_del(bret); 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); }