#11
authoradam <anton@adamansky.com>
Wed, 12 Dec 2012 12:23:56 +0000 (19:23 +0700)
committeradam <anton@adamansky.com>
Wed, 12 Dec 2012 12:23:56 +0000 (19:23 +0700)
tcejdb/bson.c
tcejdb/ejdb.c

index b6c0138..c3b0995 100644 (file)
@@ -354,8 +354,8 @@ static void bson_visit_fields_impl(bson_traverse_flags_t flags, char* pstack, in
         if (curr > 0) {
             curr--; //remove leading dot
         }
+        }
     }
-}
 
 EJDB_EXPORT void bson_visit_fields(bson_iterator *it, bson_traverse_flags_t flags, BSONVISITOR visitor, void *op) {
     char pstack[BSON_MAX_FPATH_LEN + 1];
@@ -868,14 +868,16 @@ static int bson_append_estart2(bson *b, int type, const char *name, int namelen,
         return BSON_ERROR;
     }
 
-    if (bson_check_field_name(b, (const char *) name, len - 1,
+    if (bson_check_field_name(b, (const char *) name, namelen,
             !(b->flags & BSON_FLAG_QUERY_MODE), !(b->flags & BSON_FLAG_QUERY_MODE)) == BSON_ERROR) {
         bson_builder_error(b);
         return BSON_ERROR;
     }
-
     bson_append_byte(b, (char) type);
-    bson_append(b, name, len);
+    memcpy(b->cur, name, namelen);
+    b->cur += namelen;
+    *(b->cur) = '\0';
+    b->cur += 1;
     return BSON_OK;
 }
 
@@ -1719,7 +1721,10 @@ EJDB_EXPORT int bson_merge_array_sets(const void *mbuf, const void *inbuf, bson
                 break;
             }
             pdp = (fpath + i);
-            while (*pdp == '.') ++pdp;
+            while (*pdp == '.') {
+                ++pdp;
+                ++i;
+            }
         }
         for (; lvl > 0; --lvl) {
             bson_append_finish_object(bsout);
index f822284..7ae1ba7 100644 (file)
@@ -1670,6 +1670,7 @@ static bool _qryupdate(EJCOLL *jcoll, const EJQ *ejq, void *bsbuf, int bsbufsz,
     bson bsout;
     bsout.data = NULL;
     bsout.dataSize = 0;
+    bson_reset(&bsout);
 
     const EJQF *setqf = NULL;
     const EJQF *incqf = NULL;
@@ -1752,7 +1753,7 @@ static bool _qryupdate(EJCOLL *jcoll, const EJQ *ejq, void *bsbuf, int bsbufsz,
 
     if (addsetqf) { //$addToSet
         char* inbuf = (bsout.finished) ? bsout.data : bsbuf;
-        if (bson_find_unmerged_array_sets(bson_data(setqf->updateobj), inbuf)) {
+        if (bson_find_unmerged_array_sets(bson_data(addsetqf->updateobj), inbuf)) {
             //Missing $addToSet element in some array field found
             if (bsout.finished) {
                 //reinit `bsout`, `inbuf` already points to `bsout.data` and will be freed later
@@ -1762,7 +1763,7 @@ static bool _qryupdate(EJCOLL *jcoll, const EJQ *ejq, void *bsbuf, int bsbufsz,
                 bson_init_size(&bsout, bsbufsz);
             }
             //$addToSet merge
-            if (bson_merge_array_sets(bson_data(setqf->updateobj), inbuf, &bsout)) {
+            if (bson_merge_array_sets(bson_data(addsetqf->updateobj), inbuf, &bsout)) {
                 rv = false;
                 _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
             }
@@ -1771,6 +1772,7 @@ static bool _qryupdate(EJCOLL *jcoll, const EJQ *ejq, void *bsbuf, int bsbufsz,
             }
             bson_finish(&bsout);
             update = true;
+            //bson_print(stderr, &bsout);
         }
     }
 
@@ -3160,7 +3162,7 @@ static int _parse_qobj_impl(EJDB *jb, EJQ *q, bson_iterator *it, TCLIST *qlist,
                         assert(qf.updateobj == NULL);
                         qf.q->flags |= EJQUPDATING;
                         qf.updateobj = bson_create();
-                        bson_init(qf.updateobj);
+                        bson_init_as_query(qf.updateobj);
                         bson_type sbt;
                         bson_iterator sit;
                         bson_iterator_subiterator(it, &sit);