Esempio n. 1
0
File: jejdb.c Progetto: CowanSM/ejdb
/*
 * Class:     org_ejdb_driver_EJDBCollection
 * Method:    remove
 * Signature: (Lorg/ejdb/bson/types/ObjectId;)V
 */
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_remove (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;
	}

	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;
	}

	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);
	bool status = ejdbrmbson(coll, oid);
	(*env)->ReleaseByteArrayElements(env, joiddata, (jbyte*)oid, 0);
	(*env)->DeleteLocalRef(env, joiddata);

	if (!status) {
		set_ejdb_error(env, db);
		return;
	}
}
Esempio n. 2
0
File: jejdb.c Progetto: CowanSM/ejdb
/*
 * Class:     org_ejdb_driver_EJDBCollection
 * Method:    updateMeta
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_org_ejdb_driver_EJDBCollection_updateMeta (JNIEnv *env, jobject obj) {
	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 = ejdbgetcoll(db, cname);
	(*env)->ReleaseStringUTFChars(env, colname, cname);

	update_coll_meta(env, obj, coll);
};
Esempio n. 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);
}
Esempio n. 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);
}
Esempio n. 5
0
File: jejdb.c Progetto: 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;
};