Example #1
0
static void eprint(EJDB *jb, int line, const char *func) {
    int ecode = ejdbecode(jb);
    fprintf(stderr, "%d: %s: error: %d: %s\n",
            line, func, ecode, ejdberrmsg(ecode));
}
Example #2
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);
}
Example #3
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;
}
Example #4
0
File: jejdb.c Project: CowanSM/ejdb
/*
 * 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;
};
Example #5
0
File: jejdb.c Project: CowanSM/ejdb
static void set_ejdb_error(JNIEnv * env, EJDB *db) {
	int code = ejdbecode(db);
	set_error(env, code, ejdberrmsg(code));
};
Example #6
0
 void collection::throw_last_ejdb_exception() throw(ejdb_exception)
 {
     throw ejdb_exception(ejdbecode(_db.get()));
 }