Beispiel #1
0
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;
}
Beispiel #2
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);
}
Beispiel #3
0
Datei: t1.c Projekt: CowanSM/ejdb
void testBuildQuery1() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    /*
     Query = {
        "name" : Петров Петр,
        "age"  : 33,
        "family" : {
            "wife" : {
                "name"  : "Jeniffer",
                "age"   : {"$gt" : 25},
                "phone" : "444-111"
            },
            "children" : [
                {
                    "name" : "Dasha",
                    "age" : {"$in" : [1, 4, 10]}
                }
            ]
         }
     */
    bson q1;
    bson_init_as_query(&q1);
    bson_append_string(&q1, "name", "Петров Петр");
    bson_append_int(&q1, "age", 33);

    bson q1family_wife;
    bson_init_as_query(&q1family_wife);
    bson_append_string(&q1family_wife, "name", "Jeniffer");
    bson_append_start_object(&q1family_wife, "age");
    bson_append_int(&q1family_wife, "$gt", 25);
    bson_append_finish_object(&q1family_wife);

    bson_append_string(&q1family_wife, "phone", "444-111");
    bson_finish(&q1family_wife);

    bson q1family_child;
    bson_init_as_query(&q1family_child);
    bson_append_string(&q1family_child, "name", "Dasha");

    //"age" : {"$in" : [1, 4, 10]}
    bson q1family_child_age_IN;
    bson_init_as_query(&q1family_child_age_IN);
    bson_append_start_array(&q1family_child_age_IN, "$in");
    bson_append_int(&q1family_child_age_IN, "0", 1);
    bson_append_int(&q1family_child_age_IN, "1", 4);
    bson_append_int(&q1family_child_age_IN, "2", 10);
    bson_append_finish_array(&q1family_child_age_IN);
    bson_finish(&q1family_child_age_IN);
    bson_append_bson(&q1family_child, "age", &q1family_child_age_IN);
    bson_finish(&q1family_child);

    bson q1family;
    bson_init_as_query(&q1family);
    bson_append_bson(&q1family, "wife", &q1family_wife);
    bson_append_start_array(&q1family, "children");
    bson_append_bson(&q1family, "0", &q1family_child);
    bson_append_finish_array(&q1family);
    bson_finish(&q1family);

    bson_append_bson(&q1, "family", &q1family);
    bson_finish(&q1);

    CU_ASSERT_FALSE_FATAL(q1.err);
    CU_ASSERT_FALSE_FATAL(q1family.err);
    CU_ASSERT_FALSE_FATAL(q1family_wife.err);
    CU_ASSERT_FALSE_FATAL(q1family_child.err);
    CU_ASSERT_FALSE_FATAL(q1family_child_age_IN.err);

    EJQ *ejq = ejdbcreatequery(jb, &q1, NULL, 0, NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(ejq);

    bson_destroy(&q1);
    bson_destroy(&q1family);
    bson_destroy(&q1family_wife);
    bson_destroy(&q1family_child);
    bson_destroy(&q1family_child_age_IN);

    CU_ASSERT_PTR_NOT_NULL_FATAL(ejq->qobjlist);
    TCLIST *qmap = ejq->qobjlist;
    CU_ASSERT_EQUAL(qmap->num, 7);

    for (int i = 0; i < TCLISTNUM(qmap); ++i) {

        const EJQF *qf = TCLISTVALPTR(qmap, i);
        CU_ASSERT_PTR_NOT_NULL_FATAL(qf);
        const char* key = qf->fpath;

        switch (i) {
            case 0:
            {
                CU_ASSERT_STRING_EQUAL(key, "name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Петров Петр");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 1:
            {
                CU_ASSERT_STRING_EQUAL(key, "age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "33");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMEQ);
                break;
            }
            case 2:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Jeniffer");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 3:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "25");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMGT);
                break;
            }
            case 4:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.phone");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "444-111");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 5:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.children.0.name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Dasha");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 6:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.children.0.age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_EQUAL(qf->ftype, BSON_ARRAY);
                TCLIST *al = tclistload(qf->expr, qf->exprsz);
                char* als = tcstrjoin(al, ',');
                CU_ASSERT_STRING_EQUAL(als, "1,4,10");
                TCFREE(als);
                tclistdel(al);
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMOREQ);
                break;
            }
        }
    }

    ejdbquerydel(ejq);
}
Beispiel #4
0
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);
}
Beispiel #5
0
void testPerf1() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    CU_ASSERT_PTR_NOT_NULL_FATAL(recs);
    EJCOLL *coll = ejdbcreatecoll(jb, "pcoll1", NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(coll);
    unsigned long st = tcmstime();
    for (int i = 0; i < RS; ++i) {
        bson_oid_t oid;
        ejdbsavebson(coll, recs + i, &oid);
    }
    ejdbsyncoll(coll);
    fprintf(stderr, "\ntestPerf1(): SAVED %d BSON OBJECTS, TIME %lu ms\n", RS, tcmstime() - st);

    st = tcmstime();
    uint32_t acount = 0;
    int i;
    for (i = 0; i < QRS; ++i) {
        int idx = rand() % QRS;
        bson *bs = recs + idx;
        assert(bs);
        EJQ *q = ejdbcreatequery(jb, bs, NULL, 0, NULL);
        assert(q);
        uint32_t count;
        ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
        assert(count);
        if (count != 1) {
            fprintf(stderr, "CNT=%u\n", count);
        }
        acount += count;
        ejdbquerydel(q);
    }
    CU_ASSERT_TRUE(i <= acount);
    fprintf(stderr, "testPerf1(): %u QUERIES, TIME: %lu ms, PER QUERY TIME: %lu ms\n",
            i, tcmstime() - st, (unsigned long) ((tcmstime() - st) / QRS));

    st = tcmstime();
    CU_ASSERT_TRUE(ejdbsetindex(coll, "rstring", JBIDXSTR));
    fprintf(stderr, "testPerf1(): SET INDEX 'rstring' TIME: %lu ms\n", tcmstime() - st);

    st = tcmstime();
    acount = 0;
    for (i = 0; i < QRS; ++i) {
        int idx = rand() % QRS;
        bson *bs = recs + idx;
        assert(bs);
        EJQ *q = ejdbcreatequery(jb, bs, NULL, 0, NULL);
        assert(q);
        uint32_t count;
        ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
        assert(count);
        acount += count;
        ejdbquerydel(q);
    }
    CU_ASSERT_TRUE(i <= acount);
    fprintf(stderr, "testPerf1(): %u QUERIES WITH 'rstring' INDEX, TIME: %lu ms, PER QUERY TIME: %lu ms\n",
            i, tcmstime() - st, (unsigned long) ((tcmstime() - st) / QRS));

    bson bsq1;
    bson_init_as_query(&bsq1);
    bson_append_start_object(&bsq1, "$set");
    bson_append_int(&bsq1, "intv", 1);
    bson_append_finish_object(&bsq1);
    bson_finish(&bsq1);

    EJQ *q = ejdbcreatequery(jb, &bsq1, NULL, JBQRYCOUNT, NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(q);
    uint32_t count;
    st = tcmstime(); //$set op
    ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
    if (ejdbecode(jb) != 0) {
        eprint(jb, __LINE__, "$set test");
        CU_ASSERT_TRUE(false);
    }
    CU_ASSERT_EQUAL(count, RS);
    fprintf(stderr, "testPerf1(): {'$set' : {'intv' : 1}} FOR %u OBJECTS, TIME %lu ms\n", count, tcmstime() - st);
    ejdbquerydel(q);
    bson_destroy(&bsq1);

    bson_init_as_query(&bsq1);
    bson_append_start_object(&bsq1, "$inc");
    bson_append_int(&bsq1, "intv", 1);
    bson_append_finish_object(&bsq1);
    bson_finish(&bsq1);

    q = ejdbcreatequery(jb, &bsq1, NULL, JBQRYCOUNT, NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(q);
    st = tcmstime(); //$inc op
    ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
    if (ejdbecode(jb) != 0) {
        eprint(jb, __LINE__, "$inc test");
        CU_ASSERT_TRUE(false);
    }
    CU_ASSERT_EQUAL(count, RS);
    fprintf(stderr, "testPerf1(): {'$inc' : {'intv' : 1}} FOR %u OBJECTS, TIME %lu ms\n", count, tcmstime() - st);
    ejdbquerydel(q);
    bson_destroy(&bsq1);

    bson_init_as_query(&bsq1);
    bson_append_int(&bsq1, "intv", 2);
    bson_finish(&bsq1);
    q = ejdbcreatequery(jb, &bsq1, NULL, JBQRYCOUNT, NULL);
    ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
    CU_ASSERT_EQUAL(count, RS);
    ejdbquerydel(q);
    bson_destroy(&bsq1);

    ejdbrmcoll(jb, coll->cname, true);
}
Beispiel #6
0
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;
}
Beispiel #7
0
/*
 * 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;
};
Beispiel #8
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;
    }