Fixed #89
authoradam <adamansky@gmail.com>
Tue, 6 Aug 2013 18:40:00 +0000 (01:40 +0700)
committeradam <adamansky@gmail.com>
Tue, 6 Aug 2013 18:40:00 +0000 (01:40 +0700)
tcejdb/bson.c
tcejdb/ejdb.h
tcejdb/testejdb/t2.c

index de44623..577dae3 100644 (file)
@@ -2006,9 +2006,17 @@ static bson_visitor_cmd_t bson_merge_array_sets_tf(const char *fpath, int fpathl
         return (BSON_VCMD_SKIP_AFTER);
     }
     if (bt == BSON_ARRAY) {
+        if (after) {
+            bson_append_finish_array(ctx->bsout);
+            return (BSON_VCMD_OK);
+        }
         bson_iterator_from_buffer(&mit, ctx->mbuf);
         bt = bson_find_fieldpath_value2(fpath, fpathlen, &mit);
-        if (bt == BSON_EOO || (ctx->expandall && bt != BSON_ARRAY)) {
+        if (bt == BSON_EOO) {
+            bson_append_start_array(ctx->bsout, key);
+            return (BSON_VCMD_OK);
+        }
+        if (ctx->expandall && bt != BSON_ARRAY) {
             bson_append_field_from_iterator(it, ctx->bsout);
             return (BSON_VCMD_SKIP_NESTED | BSON_VCMD_SKIP_AFTER);
         }
@@ -2097,9 +2105,9 @@ int bson_merge_array_sets(const void *mbuf, const void *inbuf, bool pull, bool e
     }
     bson_iterator_from_buffer(&it, inbuf);
     if (pull) {
-        bson_visit_fields(&it, BSON_TRAVERSE_ARRAYS_EXCLUDED, bson_merge_array_sets_pull_tf, &ctx);
+        bson_visit_fields(&it, 0, bson_merge_array_sets_pull_tf, &ctx);
     } else {
-        bson_visit_fields(&it, BSON_TRAVERSE_ARRAYS_EXCLUDED, bson_merge_array_sets_tf, &ctx);
+        bson_visit_fields(&it, 0, bson_merge_array_sets_tf, &ctx);
     }
     if (ctx.mfields == 0 || pull) {
         return ctx.ecode;
index 96ab328..099a7b5 100644 (file)
@@ -472,9 +472,9 @@ EJDB_EXPORT void ejdbqresultdispose(EJQRESULT qr);
  * `$set` and `$inc` operations are supported:
  *
  * `$set` Field set operation:
- *      - {some fields for selection, '$set' : {'field1' : {obj}, ...,  'field1' : {obj}}}
+ *      - {some fields for selection, '$set' : {'fpath1' : {obj}, ...,  'fpathN' : {obj}}}
  * `$inc` Increment operation. Only number types are supported.
- *      - {some fields for selection, '$inc' : {'field1' : number, ...,  'field1' : {number}}
+ *      - {some fields for selection, '$inc' : {'fpath1' : number, ...,  'fpathN' : {number}}
  *
  * @return Number of updated records
  */
index 1fac05a..dcd2023 100644 (file)
@@ -3145,7 +3145,7 @@ void testTicket88() { //https://github.com/Softmotions/ejdb/issues/88
 
     bson r;
     bson_oid_t oid;
-    for (int i = 0; i < 1000; ++i) {
+    for (int i = 0; i < 10; ++i) {
         bson_init(&r);
         bson_append_start_array(&r, "arr1");
         bson_append_start_object(&r, "0");
@@ -3174,7 +3174,7 @@ void testTicket88() { //https://github.com/Softmotions/ejdb/issues/88
     uint32_t count = 0;
     ejdbqryexecute(ccoll, q1, &count, JBQRYCOUNT, NULL);
     CU_ASSERT_TRUE(ejdbecode(jb) == 0);
-    CU_ASSERT_EQUAL(count, 1000);
+    CU_ASSERT_EQUAL(count, 10);
     ejdbquerydel(q1);
     bson_destroy(&bsq1);
 
@@ -3186,11 +3186,60 @@ void testTicket88() { //https://github.com/Softmotions/ejdb/issues/88
     bson_finish(&bsq2);
     q1 = ejdbcreatequery(jb, &bsq2, NULL, 0, NULL);
     ejdbqryexecute(ccoll, q1, &count, JBQRYCOUNT, NULL);
-    CU_ASSERT_EQUAL(count, 1000);
+    CU_ASSERT_EQUAL(count, 10);
     ejdbquerydel(q1);
     bson_destroy(&bsq2);
 }
 
+void testTicket89() { //https://github.com/Softmotions/ejdb/issues/89
+    EJCOLL *ccoll = ejdbcreatecoll(jb, "ticket89", NULL);
+    CU_ASSERT_PTR_NOT_NULL(ccoll);
+
+    bson r;
+    bson_oid_t oid;
+
+    bson_init(&r);
+    //{"test":[["aaa"],["bbb"]]}
+    bson_append_start_array(&r, "test");
+    bson_append_start_array(&r, "0");
+    bson_append_string(&r, "0", "aaa");
+    bson_append_finish_array(&r);
+    bson_append_start_array(&r, "1");
+    bson_append_string(&r, "0", "bbb");
+    bson_append_finish_array(&r);
+    bson_append_finish_array(&r);
+    bson_finish(&r);
+    CU_ASSERT_TRUE(ejdbsavebson(ccoll, &r, &oid));
+    bson_destroy(&r);
+
+    bson bsq1;
+    bson_init_as_query(&bsq1);
+    bson_append_start_object(&bsq1, "$addToSet");
+    bson_append_string(&bsq1, "test.0", "bbb");
+    bson_append_finish_object(&bsq1);
+    bson_finish(&bsq1);
+
+    EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL);
+    CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+    uint32_t count = 0;
+    ejdbqryexecute(ccoll, q1, &count, JBQRYCOUNT, NULL);
+    CU_ASSERT_TRUE(ejdbecode(jb) == 0);
+    CU_ASSERT_EQUAL(count, 1);
+    ejdbquerydel(q1);
+    bson_destroy(&bsq1);
+
+    bson_init_as_query(&bsq1);
+    bson_append_string(&bsq1, "test.0.1", "bbb");
+    bson_finish(&bsq1);
+    q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, NULL);
+    CU_ASSERT_PTR_NOT_NULL_FATAL(q1);
+    ejdbqryexecute(ccoll, q1, &count, JBQRYCOUNT, NULL);
+    CU_ASSERT_TRUE(ejdbecode(jb) == 0);
+    CU_ASSERT_EQUAL(count, 1);
+    ejdbquerydel(q1);
+    bson_destroy(&bsq1);
+}
+
 void testQueryBool() {
     EJCOLL *coll = ejdbcreatecoll(jb, "contacts", NULL);
     CU_ASSERT_PTR_NOT_NULL_FATAL(coll);
@@ -4350,6 +4399,7 @@ int main() {
             (NULL == CU_add_test(pSuite, "testUpdate1", testUpdate1)) ||
             (NULL == CU_add_test(pSuite, "testUpdate2", testUpdate2)) ||
             (NULL == CU_add_test(pSuite, "testTicket88", testTicket88)) ||
+            (NULL == CU_add_test(pSuite, "testTicket89", testTicket89)) ||
             (NULL == CU_add_test(pSuite, "testQueryBool", testQueryBool)) ||
             (NULL == CU_add_test(pSuite, "testDropAll", testDropAll)) ||
             (NULL == CU_add_test(pSuite, "testTokens$begin", testTokens$begin)) ||