return true;
}
+static int _cmpcolls(const TCLISTDATUM *d1, const TCLISTDATUM *d2) {
+ EJCOLL *c1 = (EJCOLL*) d1->ptr;
+ EJCOLL *c2 = (EJCOLL*) d2->ptr;
+ return memcmp(c1->cname, c2->cname, MIN(c1->cnamesz, c2->cnamesz));
+}
+
bson* ejdbmeta(EJDB *jb) {
JBENSUREOPENLOCK(jb, false, NULL);
char nbuff[TCNUMBUFSIZ];
bson_init(bs);
bson_append_string(bs, "file", jb->metadb->hdb->path);
bson_append_start_array(bs, "collections"); //collections
+
TCLIST *cols = ejdbgetcolls(jb);
+ tclistsortex(cols, _cmpcolls);
+
for (int i = 0; i < TCLISTNUM(cols); ++i) {
EJCOLL *coll = (EJCOLL*) TCLISTVALPTR(cols, i);
if (!JBCLOCKMETHOD(coll, false)) {
while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
const char *key = bson_iterator_key(&it);
- if (!strcmp("ejdbexport", key) || !strcmp("ejdbimport", key)) {
+ if (!strcmp("export", key) || !strcmp("import", key)) {
xlog = tcxstrnew();
char *path = NULL;
int flags = 0;
ecode = JBEINVALIDCMD;
goto finish;
}
- if (!strcmp("ejdbexport", key)) {
+ if (!strcmp("export", key)) {
rv = ejdbexport(jb, path, cnames, flags, xlog);
- } else { //ejdbimport
+ } else { //import
rv = ejdbimport(jb, path, cnames, flags, xlog);
}
if (!rv) {
}
CU_ASSERT_TRUE(coll != NULL);
bson_oid_t oid;
+ const char *log = NULL;
bson bv1;
bson_init(&bv1);
ejdbsavebson(coll, &bv1, &oid);
bson_destroy(&bv1);
- TCXSTR *log = tcxstrnew();
- TCLIST *cnames = tclistnew();
- tclistpush2(cnames, "col1");
- tclistpush2(cnames, "col2");
-
-
- //TODO!!!
- bool rv = ejdbexport(jb, "testBSONExportImport2", cnames, 0, log);
- if (!rv) {
- eprint(jb, __LINE__, "testBSONExportImport2");
- }
- CU_ASSERT_TRUE(rv);
+ 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);
ejdbdel(jb);
//Restore data:
-
jb = ejdbnew();
CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt4_export", JBOWRITER | JBOCREAT));
CU_ASSERT_TRUE(ejdbsavebson(coll, &bv1, &oid));
bson_destroy(&bv1);
- //TODO!!!
- rv = ejdbimport(jb, "testBSONExportImport2", cnames, JBIMPORTREPLACE, log);
- CU_ASSERT_TRUE(rv);
-
- CU_ASSERT_PTR_NOT_NULL(strstr(TCXSTRPTR(log), "Reading 'testBSONExportImport2/col1.bson'"));
- 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_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, "Reading 'testBSONExportImport2/col1.bson'"));
+ 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);
ejdbqryexecute(coll, q, &count, JBQRYCOUNT, NULL);
CU_ASSERT_EQUAL(count, 1);
- //TODO!!!
- rv = ejdbimport(jb, "testBSONExportImport2", NULL, JBIMPORTUPDATE, NULL);
- CU_ASSERT_TRUE(rv);
+ 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);
bson_del(ometa);
bson_del(nmeta);
- tcxstrdel(log);
- tclistdel(cnames);
}
int init_suite(void) {