예제 #1
0
파일: jejdb.c 프로젝트: CowanSM/ejdb
/*
* Class:     org_ejdb_driver_EJDBCollection
* Method:    setIndex
* Signature: (Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_setIndex (JNIEnv *env, jobject obj, jstring pathstr, jint flags) {
	EJDB* db = get_ejdb_from_object(env, obj);
	if (!ejdbisopen(db)) {
		set_error(env, 0, "EJDB not opened");
		return;
	}

	jstring colname = get_coll_name(env, obj);
	const char *cname = (*env)->GetStringUTFChars(env, colname, NULL);
	EJCOLL * coll = ejdbcreatecoll(db, cname, NULL);
	(*env)->ReleaseStringUTFChars(env, colname, cname);

	if (!coll) {
		set_ejdb_error(env, db);
		return;
	}

	const char * path = (*env)->GetStringUTFChars(env, pathstr, NULL);
	bool status = ejdbsetindex(coll, path, flags);
	(*env)->ReleaseStringUTFChars(env, pathstr, path);

	if (!status) {
		set_ejdb_error(env, db);
		return;
	}
};
예제 #2
0
void testRace2() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    bool err = false;
    TARGRACE targs[tnum];
    pthread_t threads[tnum];

    ejdbrmcoll(jb, "threadrace1", true);
    EJCOLL *coll = ejdbcreatecoll(jb, "threadrace1", NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(coll);
    if (!ejdbsetindex(coll, "tid", JBIDXDROPALL)) { //NO INDEX
        eprint(jb, __LINE__, "testRace2");
        err = true;
    }
    if (err) {
        goto finish;
    }

    for (int i = 0; i < tnum; i++) {
        targs[i].jb = jb;
        targs[i].id = i;
        if (pthread_create(threads + i, NULL, threadrace1, targs + i) != 0) {
            eprint(jb, __LINE__, "pthread_create");
            targs[i].id = -1;
            err = true;
        }
    }
    for (int i = 0; i < tnum; i++) {
        if (targs[i].id == -1) continue;
        void *rv;
        if (pthread_join(threads[i], &rv) != 0) {
            eprint(jb, __LINE__, "pthread_join");
            err = true;
        } else if (rv) {
            err = true;
        }
    }

finish:
    CU_ASSERT_FALSE(err);
}
예제 #3
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);
}
예제 #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);
}
예제 #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);
}