Quellcode durchsuchen

Refs #2170. Fixed match not returning dependencies required by magic unwinds.

Spencer Rathbun vor 12 Jahren
Ursprung
Commit
3e93c75732

+ 1 - 0
README.md

@@ -40,6 +40,7 @@ Here is a list of the major items where we have deviated from the MongoDB code a
       * EXTENSIONS: The following are extended `munge`-only expressions that have not been ported back to MongoDB yet
     * `DocumentSource` classes
       * DESIGN: We have implemented a `reset` method for all document sources so that we can reuse them against different streams of data
+	  * MatchDocumentSource: getDependencies is implemented, because magic unwinds depends on it
 
 
 TODO

+ 29 - 1
lib/pipeline/documentSources/MatchDocumentSource.js

@@ -14,6 +14,7 @@ var MatchDocumentSource = module.exports = (function(){
 	var klass = module.exports = MatchDocumentSource = function MatchDocumentSource(query /*, pCtx*/){
 		if(arguments.length !== 1) throw new Error("one arg expected");
 		base.call(this);
+		this.query = query; // save the query, so we can check it for deps later. THIS IS A DEVIATION FROM THE MONGO IMPLEMENTATION
 		this.matcher = sift(query);
 	}, base = require('./FilterBaseDocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
@@ -36,13 +37,40 @@ var MatchDocumentSource = module.exports = (function(){
 	};
 
 	// DEPENDENCIES
-	var sift = require("sift");
+	var sift = require("sift"),
+		traverse = require("traverse");
 
 	klass.matchName = "$match";
 	proto.getSourceName = function getSourceName(){
 		return klass.matchName;
 	};
 
+
+	/**
+	*	Adds dependencies to the contained ObjectExpression
+	*
+	*	THIS IS A DEVIATION FROM THE MONGO IMPLEMENTATION.
+	*
+	*	@param {deps} An object that is treated as a set of strings
+	*	@return A string that is part of the GetDepsReturn enum
+	**/
+	proto.getDependencies = function getDependencies(deps) {
+		var tmpArr = [];
+		// We need to construct a facets for both keys and values if they contain dotted paths
+		traverse(this.query).forEach(function(value) {
+			var key = this.key;
+			if (typeof value == "string" && value[0] == "$") { // If we find a document path in the value
+				tmpArr.push(value.replace(/^\$/, ""));
+			} else if (key && /^[A-Za-z_]/.test(key)) { //if we find a non-operator as a key
+				tmpArr.push(key);
+			}
+		});
+		for (var i = 0; i < tmpArr.length; i++) {
+			deps[tmpArr[i]] = 1;
+		}
+        return "SEE_NEXT";
+    };
+
     /**
 	 * Create an object that represents the document source.  The object
      * will have a single field whose name is the source's name.  This

+ 12 - 1
test/lib/pipeline/documentSources/MatchDocumentSource.js

@@ -65,8 +65,19 @@ module.exports = {
 				assert.strictEqual(t instanceof MatchDocumentSource, true);
 			}
 
+		},
+
+        "#getDependencies()": {
+
+            "should properly detect dependencies in match": function testGetDependencies(){
+				var t = MatchDocumentSource.createFromJson({ someval:{$exists:true} });
+                var dependencies = {};
+                assert.equal("SEE_NEXT", t.getDependencies(dependencies));
+                assert.equal(1, Object.keys(dependencies).length);
+                assert.ok(dependencies.someval);
+            }
 
-		}
+        }
 
 
 	}