#59 #72
authoradam <adamansky@gmail.com>
Sun, 14 Jul 2013 14:08:46 +0000 (21:08 +0700)
committeradam <adamansky@gmail.com>
Sun, 14 Jul 2013 14:08:46 +0000 (21:08 +0700)
tcejdb/bson.c
tcejdb/ejdb.h
tcejdb/testejdb/t4.c

index c07d758..5d73e2e 100644 (file)
@@ -2048,7 +2048,6 @@ static int _bson2json(_BSON2JSONCTX *ctx, bson_iterator *it) {
 
     bson_type bt;
     TCXSTR *out = ctx->out;
-    BSPAD(0);
     tcxstrcat2(ctx->out, "{\n");
     ctx->nlvl += 4;
     int c = 0;
@@ -2082,6 +2081,7 @@ static int _bson2json(_BSON2JSONCTX *ctx, bson_iterator *it) {
                 bson_iterator sit;
                 bson_iterator_subiterator(it, &sit);
                 _bson2json(ctx, &sit);
+                break;
             }
             case BSON_NULL:
                 tcxstrcat2(out, "null");
@@ -2126,8 +2126,10 @@ static int _bson2json(_BSON2JSONCTX *ctx, bson_iterator *it) {
                 break;
         }
     }
+    tcxstrcat2(out, "\n");
     BSPAD(-4);
-    tcxstrcat2(out, "\n}\n");
+    tcxstrcat2(out, "}\n");
+    ctx->nlvl -= 4;
     return 0;
 #undef BSPAD
 }
index 7aaea62..fcaf726 100644 (file)
@@ -507,10 +507,11 @@ enum {
 };
 
 /**
- * Export database data to the specified location. Read lock will be taken on each collection
+ * Export database collections data to the specified location.
+ * Database read lock will be taken on each collection
  * @param path Directory name in which data will exported.
  * @param colnames List of collection names to export. If NULL all collections will be exported.
- * @param flags. Reserved for future use.
+ * @param flags. Can be set to `JBJSONEXPORT` in order to export collection data into JSON instead of BSON.
  * @return on sucess `true`
  */
 EJDB_EXPORT bool ejdbexport(EJDB *jb, const char *path, TCLIST *cnames, int flags);
index 740e7fd..0c2847e 100644 (file)
@@ -2,6 +2,7 @@
 #include "myconf.h"
 #include "ejdb_private.h"
 #include "CUnit/Basic.h"
+
 /*
  * CUnit Test Suite
  */
@@ -30,6 +31,60 @@ void testTicket53() {
     ejdbdel(jb);
 }
 
+void testBSONExportImport() {
+    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);
+    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);
+
+
+    TCLIST* cnames = tclistnew();
+    tclistpush2(cnames, "col1");
+    tclistpush2(cnames, "col2");
+
+    bool rv = ejdbexport(jb, "testBSONExportImport", cnames, 0);
+    if (!rv) {
+        eprint(jb, __LINE__, "testBSONExportImport");
+    }
+    CU_ASSERT_TRUE(rv);
+    ejdbclose(jb);
+    ejdbdel(jb);
+}
+
 int init_suite(void) {
     return 0;
 }
@@ -38,7 +93,6 @@ int clean_suite(void) {
     return 0;
 }
 
-
 int main() {
     setlocale(LC_ALL, "en_US.UTF-8");
     CU_pSuite pSuite = NULL;
@@ -56,7 +110,8 @@ int main() {
 
     /* Add the tests to the suite */
     if (
-            (NULL == CU_add_test(pSuite, "testTicket53", testTicket53))
+            (NULL == CU_add_test(pSuite, "testTicket53", testTicket53)) ||
+            (NULL == CU_add_test(pSuite, "testBSONExportImport", testBSONExportImport))
 
             ) {
         CU_cleanup_registry();