Esempio n. 1
0
void testTransactions1() {
    EJCOLL *coll = ejdbcreatecoll(jb, "trans1", NULL);
    bson bs;
    bson_init(&bs);
    bson_append_string(&bs, "foo", "bar");
    bson_finish(&bs);

    bson_oid_t oid;
    CU_ASSERT_TRUE(ejdbtranbegin(coll));
    ejdbsavebson(coll, &bs, &oid);
    CU_ASSERT_TRUE(ejdbtrancommit(coll));

    bson *bres = ejdbloadbson(coll, &oid);
    CU_ASSERT_PTR_NOT_NULL(bres);
    if (bres) {
        bson_del(bres);
    }

    CU_ASSERT_TRUE(ejdbtranbegin(coll));
    ejdbsavebson(coll, &bs, &oid);
    CU_ASSERT_TRUE(ejdbtranabort(coll));

    bres = ejdbloadbson(coll, &oid);
    CU_ASSERT_PTR_NULL(bres);
    if (bres) {
        bson_del(bres);
    }

    bson_destroy(&bs);
}
Esempio n. 2
0
File: jejdb.c Progetto: CowanSM/ejdb
/*
 * Class:     org_ejdb_driver_EJDBCollection
 * Method:    load
 * Signature: (Lorg/ejdb/bson/types/ObjectId;)Lorg/ejdb/bson/BSONObject;
 */
JNIEXPORT jobject JNICALL Java_org_ejdb_driver_EJDBCollection_load (JNIEnv *env, jobject obj, jobject joid) {
	EJDB* db = get_ejdb_from_object(env, obj);		
	if (!ejdbisopen(db)) {
		set_error(env, 0, "EJDB not opened");
		return NULL;
	}

	jstring colname = get_coll_name(env, obj);

	const char *cname = (*env)->GetStringUTFChars(env, colname, NULL);
	EJCOLL * coll = ejdbgetcoll(db, cname);
	(*env)->ReleaseStringUTFChars(env, colname, cname);

	if (!coll) {
		return NULL;
	}

	jclass jObjectIdClazz = (*env)->FindClass(env, "org/ejdb/bson/types/ObjectId");
	jmethodID encodeMethodID = (*env)->GetMethodID(env, jObjectIdClazz, "toByteArray", "()[B");
	jbyteArray joiddata = (*env)->CallObjectMethod(env, joid, encodeMethodID);
	bson_oid_t *oid = (bson_oid_t*)(*env)->GetByteArrayElements(env, joiddata, NULL);
	bson* bson = ejdbloadbson(coll, oid);
	(*env)->ReleaseByteArrayElements(env, joiddata, (jbyte*)oid, 0);
	(*env)->DeleteLocalRef(env, joiddata);

	if (!bson) {
		return NULL;
	}

	jobject result = decode_bson(env, bson);
	bson_del(bson);

	return result;
}
Esempio n. 3
0
File: t1.c Progetto: CowanSM/ejdb
void testSaveLoad() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    bson_oid_t oid;
    EJCOLL *ccoll = ejdbcreatecoll(jb, "contacts", NULL);
    CU_ASSERT_PTR_NOT_NULL(ccoll);

    //Save record
    bson a1;
    bson_init(&a1);

    bson_append_string(&a1, "name", "Петров Петр");
    bson_append_string(&a1, "phone", "333-222-333");
    bson_append_int(&a1, "age", 33);
    bson_append_long(&a1, "longage", 0xFFFFFFFFFF01LL);
    bson_append_double(&a1, "doubleage", 0.333333);
    bson_finish(&a1);
    ejdbsavebson(ccoll, &a1, &oid);
    bson_destroy(&a1);


    bson *lbson = ejdbloadbson(ccoll, &oid);
    CU_ASSERT_PTR_NOT_NULL(lbson);
    bson_iterator it1;
    bson_iterator_init(&it1, lbson);

    int btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_OID);
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_STRING);
    CU_ASSERT(!strcmp("name", bson_iterator_key(&it1)));
    CU_ASSERT(!strcmp("Петров Петр", bson_iterator_string(&it1)));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_STRING);
    CU_ASSERT(!strcmp("phone", bson_iterator_key(&it1)));
    CU_ASSERT(!strcmp("333-222-333", bson_iterator_string(&it1)));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_INT);
    CU_ASSERT(!strcmp("age", bson_iterator_key(&it1)));
    CU_ASSERT(33 == bson_iterator_int(&it1));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_LONG);
    CU_ASSERT(!strcmp("longage", bson_iterator_key(&it1)));
    CU_ASSERT(0xFFFFFFFFFF01LL == bson_iterator_long(&it1));
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_DOUBLE);
    CU_ASSERT(!strcmp("doubleage", bson_iterator_key(&it1)));
    CU_ASSERT_DOUBLE_EQUAL(bson_iterator_double(&it1), 0.3, 0.1);
    btype = bson_iterator_next(&it1);
    CU_ASSERT(btype == BSON_EOO);
    bson_del(lbson);
}