فهرست منبع

Merge branch 'feature/mongo_2.6.5_expressions' into feature/mongo_2.6.5_expressions_SetIsSubset

Jake Delaney 11 سال پیش
والد
کامیت
3e6bb25de1
53فایلهای تغییر یافته به همراه1113 افزوده شده و 611 حذف شده
  1. 105 0
      lib/pipeline/DepsTracker.js
  2. 17 4
      lib/pipeline/Document.js
  3. 85 0
      lib/pipeline/ParsedDeps.js
  4. 0 114
      lib/pipeline/documentSources/DocumentSource.js
  5. 3 3
      lib/pipeline/expressions/AddExpression.js
  6. 3 4
      lib/pipeline/expressions/AllElementsTrueExpression.js
  7. 2 2
      lib/pipeline/expressions/AndExpression.js
  8. 10 3
      lib/pipeline/expressions/AnyElementTrueExpression.js
  9. 2 2
      lib/pipeline/expressions/CompareExpression.js
  10. 2 2
      lib/pipeline/expressions/ConcatExpression.js
  11. 7 8
      lib/pipeline/expressions/CondExpression.js
  12. 9 9
      lib/pipeline/expressions/DayOfMonthExpression.js
  13. 9 3
      lib/pipeline/expressions/DayOfWeekExpression.js
  14. 9 3
      lib/pipeline/expressions/DayOfYearExpression.js
  15. 9 3
      lib/pipeline/expressions/DivideExpression.js
  16. 43 41
      lib/pipeline/expressions/Expression.js
  17. 35 0
      lib/pipeline/expressions/FixedArityExpressionT.js
  18. 9 3
      lib/pipeline/expressions/HourExpression.js
  19. 3 3
      lib/pipeline/expressions/IfNullExpression.js
  20. 3 3
      lib/pipeline/expressions/LetExpression.js
  21. 3 3
      lib/pipeline/expressions/MillisecondExpression.js
  22. 3 3
      lib/pipeline/expressions/MinuteExpression.js
  23. 3 3
      lib/pipeline/expressions/ModExpression.js
  24. 3 3
      lib/pipeline/expressions/MonthExpression.js
  25. 2 2
      lib/pipeline/expressions/MultiplyExpression.js
  26. 27 0
      lib/pipeline/expressions/NaryBaseExpressionT.js
  27. 107 89
      lib/pipeline/expressions/NaryExpression.js
  28. 3 3
      lib/pipeline/expressions/NotExpression.js
  29. 2 2
      lib/pipeline/expressions/OrExpression.js
  30. 3 3
      lib/pipeline/expressions/SecondExpression.js
  31. 3 3
      lib/pipeline/expressions/SetDifferenceExpression.js
  32. 2 9
      lib/pipeline/expressions/SetEqualsExpression.js
  33. 2 9
      lib/pipeline/expressions/SetIntersectionExpression.js
  34. 3 3
      lib/pipeline/expressions/SetIsSubsetExpression.js
  35. 2 9
      lib/pipeline/expressions/SetUnionExpression.js
  36. 3 3
      lib/pipeline/expressions/SizeExpression.js
  37. 3 3
      lib/pipeline/expressions/StrcasecmpExpression.js
  38. 3 3
      lib/pipeline/expressions/SubstrExpression.js
  39. 3 3
      lib/pipeline/expressions/SubtractExpression.js
  40. 6 6
      lib/pipeline/expressions/ToLowerExpression.js
  41. 2 2
      lib/pipeline/expressions/ToUpperExpression.js
  42. 3 1
      lib/pipeline/expressions/VariadicExpressionT.js
  43. 3 3
      lib/pipeline/expressions/WeekExpression.js
  44. 3 3
      lib/pipeline/expressions/YearExpression.js
  45. 1 1
      lib/pipeline/expressions/index.js
  46. 87 0
      test/lib/pipeline/DepsTracker_test.js
  47. 74 0
      test/lib/pipeline/ParsedDeps.js
  48. 0 42
      test/lib/pipeline/documentSources/DocumentSource.js
  49. 0 151
      test/lib/pipeline/expressions/NaryExpression.js
  50. 241 0
      test/lib/pipeline/expressions/NaryExpression_test.js
  51. 0 36
      test/lib/pipeline/expressions/VariadicExpressionT_test.js
  52. 46 0
      test/lib/pipeline/expressions/utils.js
  53. 102 0
      test/lib/pipeline/expressions/utils_test.js

+ 105 - 0
lib/pipeline/DepsTracker.js

@@ -0,0 +1,105 @@
+"use strict";
+
+/**
+ * Allows components in an aggregation pipeline to report what they need from their input.
+ *
+ * @class DepsTracker
+ * @namespace mungedb-aggregate.pipeline
+ * @module mungedb-aggregate
+ * @constructor
+ */
+var DepsTracker = module.exports = function DepsTracker() {
+	// fields is a set of strings
+	this.fields = {};
+	this.needWholeDocument = false;
+	this.needTextScore = false;
+}, klass = DepsTracker, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+var ParsedDeps = require("./ParsedDeps");
+
+/**
+ * Returns a projection object covering the dependencies tracked by this class.
+ * @method toProjection
+ * @return {Object} projection of caller's dependencies
+ */
+proto.toProjection = function toProjection() {
+	var proj = {};
+
+	// if(this.needTextScore) {
+		// bb.append(Document::metaFieldTextScore, BSON("$meta" << "textScore"));
+	// }
+
+	if (this.needWholeDocument) {
+		return proj;
+	}
+
+	if (Object.keys(this.fields).length === 0) {
+		// Projection language lacks good a way to say no fields needed. This fakes it.
+		proj._id = 0;
+		proj.$noFieldsNeeded = 1;
+		return proj;
+	}
+
+	var last = "",
+		needId = false;
+
+	Object.keys(this.fields).sort().forEach(function (it) {
+		if (it.slice(0,3) == "_id" && (it.length == 3 || it.charAt(3) == ".")) {
+			// _id and subfields are handled specially due in part to SERVER-7502
+			needId = true;
+			return;
+		}
+
+		if (last !== "" && it.slice(0, last.length) === last) {
+			// we are including a parent of *it so we don't need to include this
+			// field explicitly. In fact, due to SERVER-6527 if we included this
+			// field, the parent wouldn't be fully included. This logic relies
+			// on on set iterators going in lexicographic order so that a string
+			// is always directly before of all fields it prefixes.
+			return;
+		}
+
+		last = it + ".";
+		proj[it] = 1;
+	});
+
+	if (needId)
+		proj._id = 1;
+	else
+		proj._id = 0;
+
+	return proj;
+};
+
+/**
+ * Takes a depsTracker and builds a simple recursive lookup table out of it.
+ * @method toParsedDeps
+ * @return {ParsedDeps}
+ */
+proto.toParsedDeps = function toParsedDeps() {
+	var doc = {};
+
+	if (this.needWholeDocument || this.needTextScore) {
+		// can't use ParsedDeps in this case
+		// TODO: not sure what appropriate equivalent to boost::none is
+		return;
+	}
+
+	var last = "";
+	Object.keys(this.fields).sort().forEach(function (it) {
+		if (last !== "" && it.slice(0, last.length) === last) {
+			// we are including a parent of *it so we don't need to include this
+			// field explicitly. In fact, due to SERVER-6527 if we included this
+			// field, the parent wouldn't be fully included. This logic relies
+			// on on set iterators going in lexicographic order so that a string
+			// is always directly before of all fields it prefixes.
+			return;
+		}
+
+		last = it + ".";
+		// TODO: set nested field to true; i.e. a.b.c = true, not a = true
+		doc[it] = true;
+	});
+
+	return new ParsedDeps(doc);
+};

+ 17 - 4
lib/pipeline/Document.js

@@ -35,10 +35,23 @@ klass.toJson = function toJson(doc) {
 //SKIPPED: toBsonWithMetaData
 //SKIPPED: fromBsonWithMetaData
 
-//SKIPPED: MutableDocument
-
-//SKIPPED: getNestedFieldHelper
-//SKIPPED: getNestedField -- same as getNestedFieldHelper in our code
+//SKIPPED: most of MutableDocument except for getNestedField and setNestedField, squashed into Document here (because that's how they use it)
+function getNestedFieldHelper(obj, path) {
+	// NOTE: DEVIATION FROM MONGO: from MutableDocument, implemented a bit differently here but should be same basic functionality
+	var paths = Array.isArray(path) ? path : path.split(".");
+	for (var i = 0, l = paths.length, o = obj; i < l && o instanceof Object; i++) {
+		o = o[paths[i]];
+	}
+	return o;
+};
+klass.getNestedField = klass.getNestedFieldHelper;  // NOTE: due to ours being static these are the same
+klass.setNestedField = function setNestedField(obj, path, val) {
+	// NOTE: DEVIATION FROM MONGO: from MutableDocument, implemented a bit differently here but should be same basic functionality
+	var paths = Array.isArray(path) ? path : path.split("."),
+		key = paths.pop(),
+		parent = klass.getNestedField(obj, paths);
+	if (parent) parent[key] = val;
+};
 //SKIPPED: getApproximateSize -- not implementing mem usage right now
 //SKIPPED: hash_combine
 

+ 85 - 0
lib/pipeline/ParsedDeps.js

@@ -0,0 +1,85 @@
+"use strict";
+
+/**
+ * This class is designed to quickly extract the needed fields into a Document.
+ * It should only be created by a call to DepsTracker.toParsedDeps.
+ *
+ * @class ParsedDeps
+ * @namespace mungedb-aggregate.pipeline
+ * @module mungedb-aggregate
+ * @constructor
+ * @param {Object} fields	The fields needed in a Document
+ */
+var ParsedDeps = module.exports = function ParsedDeps(fields) {
+	this._fields = fields;
+}, klass = ParsedDeps, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+var Value = require("./Value");
+
+/**
+ * Extracts fields from the input into a new Document, based on the caller.
+ *
+ * @method extractFields
+ * @param {Object} input	The JSON object to extract from
+ * @return {Document}
+ */
+proto.extractFields = function extractFields(input) {
+	return proto._documentHelper(input, this._fields);
+};
+
+/**
+ * Private: Handles array-type values for extractFields()
+ *
+ * @method _arrayHelper
+ * @param {Object} array	Array to iterate over
+ * @param {Object} neededFields
+ * @return {Array}
+ */
+proto._arrayHelper = function _arrayHelper(array, neededFields) {
+	var values = [];
+
+	for (var it in array) {
+		if (it instanceof Array)
+			values.push(_arrayHelper(it, neededFields));
+		else if (it instanceof Object)
+			values.push(proto._documentHelper(it, neededFields));
+	}
+
+	return values;
+};
+
+/**
+ * Private: Handles object-type values for extractFields()
+ *
+ * @method _documentHelper
+ * @param {Object} json	Object to iterate over and filter
+ * @param {Object} neededFields	Fields to not exclude
+ * @return {Document}
+ */
+proto._documentHelper = function _documentHelper(json, neededFields) {
+	var doc = {};
+
+	for (var fieldName in json) {
+		var jsonElement = json[fieldName],
+			isNeeded = neededFields[fieldName];
+
+		if (isNeeded === undefined)
+			continue;
+
+		if (Value.getType(isNeeded) === 'boolean') {
+			doc[fieldName] = jsonElement;
+			continue;
+		}
+
+		if (!isNeeded instanceof Object) throw new Error("dassert failure");
+
+		if (Value.getType(isNeeded) === 'object') {
+			if (jsonElement instanceof Array)
+				doc[fieldName] = proto._arrayHelper(jsonElement, isNeeded);
+			if (jsonElement instanceof Object)
+				doc[fieldName] = proto._documentHelper(jsonElement, isNeeded);
+		}
+	}
+
+	return doc;
+};

+ 0 - 114
lib/pipeline/documentSources/DocumentSource.js

@@ -185,47 +185,6 @@ proto.getDependencies = function getDependencies(deps) {
 	return klass.GetDepsReturn.NOT_SUPPORTED;
 };
 
-/**
- * This takes dependencies from getDependencies and
- * returns a projection that includes all of them
- *
- * @method	depsToProjection
- * @param	{Object} deps	set (unique array) of strings
- * @returns	{Object}	JSONObj
- **/
-klass.depsToProjection = function depsToProjection(deps) {
-	var needId = false,
-		bb = {};
-	if (deps._id === undefined)
-		bb._id = 0;
-
-	var last = "";
-	Object.keys(deps).sort().forEach(function(it){
-		if (it.indexOf('_id') === 0 && (it.length === 3 || it[3] === '.')) {
-			needId = true;
-			return;
-		} else {
-			if (last !== "" && it.slice(0, last.length) === last){
-				// we are including a parent of *it so we don't need to
-				// include this field explicitly. In fact, due to
-				// SERVER-6527 if we included this field, the parent
-				// wouldn't be fully included.
-				return;
-			}
-		}
-		last = it + ".";
-		bb[it] = 1;
-	});
-
-	if (needId) // we are explicit either way
-		bb._id = 1;
-	else
-		bb._id = 0;
-
-
-	return bb;
-};
-
 proto._serialize = function _serialize(explain) {
 	throw new Error("not implemented");
 };
@@ -237,23 +196,6 @@ proto.serializeToArray = function serializeToArray(array, explain) {
 	}
 };
 
-klass.parseDeps = function parseDeps(deps) {
-	var md = {};
-
-	var last,
-		depKeys = Object.keys(deps);
-	for (var i = 0; i < depKeys.length; i++) {
-		var it = depKeys[i],
-			value = deps[it];
-
-		if (!last && it.indexOf(last) >= 0)
-			continue;
-		last = it + '.';
-		md[it] = true;
-	}
-	return md;
-};
-
 /**
  * A function compatible as a getNext for document sources.
  * Does nothing except pass the documents through. To use,
@@ -274,59 +216,3 @@ klass.GET_NEXT_PASS_THROUGH = function GET_NEXT_PASS_THROUGH(callback) {
 	});
 	return out; // For the sync people in da house
 };
-
-klass.documentFromJsonWithDeps = function documentFromJsonWithDeps(bson, neededFields) {
-	var arrayHelper = function(bson, neededFields) {
-		var values = [];
-
-		var bsonKeys = Object.keys(bson);
-		for (var i = 0; i < bsonKeys.length; i++) {
-			var key = bsonKeys[i],
-				bsonElement = bson[key];
-
-			if (bsonElement instanceof Object) {
-				var sub = klass.documentFromJsonWithDeps(bsonElement, isNeeded);
-				values.push(sub);
-			}
-
-			if (bsonElement instanceof Array) {
-				values.push(arrayHelper(bsonElement, neededFields));
-			}
-		}
-
-		return values;
-	};
-
-	var md = {};
-
-	var bsonKeys = Object.keys(bson);
-	for (var i = 0; i < bsonKeys.length; i++) {
-		var fieldName = bsonKeys[i],
-			bsonElement = bson[fieldName],
-			isNeeded = neededFields ? neededFields[fieldName] : null;
-
-		if (!isNeeded)
-			continue;
-
-		if (typeof(isNeeded) === 'boolean') {
-			md[fieldName] = bsonElement;
-			continue;
-		}
-
-		if (!isNeeded instanceof Object)
-			throw new Error("instanceof should be an instance of Object");
-
-		if (bsonElement instanceof Object) {
-			var sub = klass.documentFromJsonWithDeps(bsonElement, isNeeded);
-
-			md[fieldName] = sub;
-		}
-
-		if (bsonElement instanceof Array) {
-			md[fieldName] = arrayHelper(bsonElement, isNeeded);
-		}
-	}
-
-	return md;
-
-};

+ 3 - 3
lib/pipeline/expressions/AddExpression.js

@@ -10,7 +10,7 @@
 var AddExpression = module.exports = function AddExpression(){
 //	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
-}, klass = AddExpression, base = require("./VariadicExpressionT")(klass), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = AddExpression, base = require("./VariadicExpressionT")(AddExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -19,7 +19,7 @@ var Value = require("../Value"),
 // PROTOTYPE MEMBERS
 klass.opName = "$add";
 proto.getOpName = function getOpName(){
-	return klass.opName
+	return klass.opName;
 };
 
 /**
@@ -40,4 +40,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 
 
 /** Register Expression */
-Expression.registerExpression(klass.opName,base.parse(klass));
+Expression.registerExpression(klass.opName,base.parse);

+ 3 - 4
lib/pipeline/expressions/AllElementsTrueExpression.js

@@ -8,12 +8,11 @@
  * @constructor
  **/
 var AllElementsTrueExpression = module.exports = function AllElementsTrueExpression() {
-	this.nargs = 1;
 	base.call(this);
 },
 	klass = AllElementsTrueExpression,
-	NaryExpression = require("./NaryExpression"),
-	base = NaryExpression,
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -46,4 +45,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$allElementsTrue", base.parse(AllElementsTrueExpression));
+Expression.registerExpression("$allElementsTrue", base.parse);

+ 2 - 2
lib/pipeline/expressions/AndExpression.js

@@ -14,7 +14,7 @@
 var AndExpression = module.exports = function AndExpression() {
 //	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
-}, klass = AndExpression, base = require(".VariadicExpressionT")(klass), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
+}, klass = AndExpression, base = require("./VariadicExpressionT")(AndExpression), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -70,6 +70,6 @@ proto.optimize = function optimize() {
 };
 
 /** Register Expression */
-Expression.registerExpression(klass.opName, base.parse(klass));
+Expression.registerExpression(klass.opName, base.parse);
 
 //TODO: proto.toMatcherBson

+ 10 - 3
lib/pipeline/expressions/AnyElementTrueExpression.js

@@ -8,9 +8,16 @@
  * @constructor
  **/
 var AnyElementTrueExpression = module.exports = function AnyElementTrueExpression(){
-	this.nargs = (1);
 	base.call(this);
-}, klass = AnyElementTrueExpression, NaryExpression = require("./NaryExpression"), base = NaryExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+},
+	klass = AnyElementTrueExpression,
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
+	proto = klass.prototype = Object.create(base.prototype,{
+		constructor:{
+			value:klass
+		}
+	});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -38,4 +45,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$anyElementTrue",base.parse(AnyElementTrueExpression));
+Expression.registerExpression("$anyElementTrue",base.parse);

+ 2 - 2
lib/pipeline/expressions/CompareExpression.js

@@ -8,11 +8,11 @@
  * @constructor
  **/
 var CompareExpression = module.exports = function CompareExpression(cmpOp) {
-    this.nargs = 2;
     this.cmpOp = cmpOp;
     base.call(this);
 }, klass = CompareExpression,
-    base = require("./NaryExpression"),
+    FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
     proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass

+ 2 - 2
lib/pipeline/expressions/ConcatExpression.js

@@ -12,7 +12,7 @@ var Expression = require("./Expression");
 var ConcatExpression = module.exports = function ConcatExpression(){
 	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
-}, klass = ConcatExpression, base = require("./VariadicExpressionT")(klass), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = ConcatExpression, base = require("./VariadicExpressionT")(ConcatExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value");
@@ -38,4 +38,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
     }).join("");
 };
 
-Expression.registerExpression(klass.opName, base.parse(klass));
+Expression.registerExpression(klass.opName, base.parse);

+ 7 - 8
lib/pipeline/expressions/CondExpression.js

@@ -8,19 +8,18 @@
  * @constructor
  **/
 var CondExpression = module.exports = function CondExpression(vars) {
-    this.nargs = 3;
     this.pCond = this.evaluateInternal(vars);
     this.idx = this.pCond.coerceToBool() ? 1 : 2;
-
     if (arguments.length !== 3) throw new Error("three args expected");
     base.call(this);
 }, klass = CondExpression,
-    base = require("./NaryExpression"),
-    proto = klass.prototype = Object.create(base.prototype, {
-	constructor: {
-	    value: klass
-	}
-    });
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 3),
+	base = FixedArityExpression,
+	proto = klass.prototype = Object.create(base.prototype, {
+		constructor: {
+			value: klass
+		}
+	});
 
 // DEPENDENCIES
 var Value = require("../Value"),

+ 9 - 9
lib/pipeline/expressions/DayOfMonthExpression.js

@@ -8,15 +8,15 @@
  * @constructor
  **/
 var DayOfMonthExpression = module.exports = function DayOfMonthExpression() {
-    this.nargs = 1;
-    base.call(this);
+	base.call(this);
 }, klass = DayOfMonthExpression,
-    base = require("./NaryExpression"),
-    proto = klass.prototype = Object.create(base.prototype, {
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
+	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
 		}
-    });
+	});
 
 // DEPENDENCIES
 var Expression = require("./Expression");
@@ -24,7 +24,7 @@ var Expression = require("./Expression");
 
 // PROTOTYPE MEMBERS
 proto.getOpName = function getOpName() {
-    return "$dayOfMonth";
+	return "$dayOfMonth";
 };
 
 /**
@@ -32,9 +32,9 @@ proto.getOpName = function getOpName() {
  * @method evaluate
  **/
 proto.evaluateInternal = function evaluateInternal(vars) {
-    var date = this.operands[0].evaluateInternal(vars);
-    return date.getUTCDate();
+	var date = this.operands[0].evaluateInternal(vars);
+	return date.getUTCDate();
 };
 
 /** Register Expression */
-Expression.registerExpression("$dayOfMonth", base.parse(DayOfMonthExpression));
+Expression.registerExpression("$dayOfMonth", base.parse);

+ 9 - 3
lib/pipeline/expressions/DayOfWeekExpression.js

@@ -8,9 +8,15 @@
  * @constructor
  **/
 var DayOfWeekExpression = module.exports = function DayOfWeekExpression(){
-	this.nargs = 1;
 	base.call(this);
-}, klass = DayOfWeekExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = DayOfWeekExpression,
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
+	proto = klass.prototype = Object.create(base.prototype, {
+		constructor:{
+			value:klass
+		}
+	});
 
 // DEPENDENCIES
 var Expression = require("./Expression");
@@ -30,4 +36,4 @@ proto.evaluateInternal = function evaluateInternal(vars){
 };
 
 /** Register Expression */
-Expression.registerExpression("$dayOfWeek",base.parse(DayOfWeekExpression));
+Expression.registerExpression("$dayOfWeek",base.parse);

+ 9 - 3
lib/pipeline/expressions/DayOfYearExpression.js

@@ -8,9 +8,15 @@
  * @constructor
  **/
 var DayOfYearExpression = module.exports = function DayOfYearExpression(){
-	this.nargs = 1;
 	base.call(this);
-}, klass = DayOfYearExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = DayOfYearExpression,
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
+	proto = klass.prototype = Object.create(base.prototype, {
+		constructor:{
+			value:klass
+		}
+	});
 
 // DEPENDENCIES
 var Expression = require("./Expression");
@@ -38,4 +44,4 @@ klass.getDateDayOfYear = function getDateDayOfYear(d){
 };
 
 /** Register Expression */
-Expression.registerExpression("$dayOfYear",base.parse(DayOfYearExpression));
+Expression.registerExpression("$dayOfYear",base.parse);

+ 9 - 3
lib/pipeline/expressions/DivideExpression.js

@@ -9,9 +9,15 @@
  * @constructor
  **/
 var DivideExpression = module.exports = function DivideExpression(){
-    this.nargs = 2;
     base.call(this);
-}, klass = DivideExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = DivideExpression,
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
+	proto = klass.prototype = Object.create(base.prototype, {
+		constructor:{
+			value:klass
+		}
+	});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -37,4 +43,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$divide",base.parse(DivideExpression));
+Expression.registerExpression("$divide",base.parse);

+ 43 - 41
lib/pipeline/expressions/Expression.js

@@ -12,26 +12,21 @@
  * @namespace mungedb-aggregate.pipeline.expressions
  * @module mungedb-aggregate
  * @constructor
- **/
-
-
+ */
 var Expression = module.exports = function Expression() {
 	if (arguments.length !== 0) throw new Error("zero args expected");
-}, klass = Expression,
-    base = Object,
-    proto = klass.prototype = Object.create(base.prototype, {
-		constructor: {
-			value: klass
-		}
-    });
+}, klass = Expression, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+
+var Value = require("../Value"),
+	Document = require("../Document");
 
 
-// NESTED CLASSES
 /**
  * Reference to the `mungedb-aggregate.pipeline.expressions.Expression.ObjectCtx` class
  * @static
  * @property ObjectCtx
- **/
+ */
 var ObjectCtx = Expression.ObjectCtx = (function() {
 	// CONSTRUCTOR
 	/**
@@ -47,7 +42,7 @@ var ObjectCtx = Expression.ObjectCtx = (function() {
 	 *      @param [opts.isDocumentOk]      {Boolean}
 	 *      @param [opts.isTopLevel]        {Boolean}
 	 *      @param [opts.isInclusionOk]     {Boolean}
-	 **/
+	 */
 	var klass = function ObjectCtx(opts /*= {isDocumentOk:..., isTopLevel:..., isInclusionOk:...}*/ ) {
 		if (!(opts instanceof Object && opts.constructor == Object)) throw new Error("opts is required and must be an Object containing named args");
 		for (var k in opts) { // assign all given opts to self so long as they were part of klass.prototype as undefined properties
@@ -83,6 +78,7 @@ klass.removeFieldPrefix = function removeFieldPrefix(prefixedField) {
 	return prefixedField.substr(1);
 };
 
+
 /**
  * Parse an Object.  The object could represent a functional expression or a Document expression.
  *
@@ -97,7 +93,7 @@ klass.removeFieldPrefix = function removeFieldPrefix(prefixedField) {
  * @param ctx   a MiniCtx representing the options above
  * @param vps	Variables Parse State
  * @returns the parsed Expression
- **/
+ */
 klass.parseObject = function parseObject(obj, ctx, vps) {
 	if (!(ctx instanceof ObjectCtx)) throw new Error("ctx must be ObjectCtx");
 
@@ -120,24 +116,27 @@ klass.parseObject = function parseObject(obj, ctx, vps) {
 			if (ctx.isTopLevel)
 				throw new Error("$expressions are not allowed at the top-level of $project; uassert code 16404");
 
-			kind = OPERATOR; //we've determined this "object" is an operator expression
+			// we've determined this "object" is an operator expression
+			kind = OPERATOR;
 
 			expression = Expression.parseExpression(fieldName, obj[fieldName], vps); //NOTE: DEVIATION FROM MONGO: c++ code uses 2 arguments. See #parseExpression
 		} else {
 			if (kind === OPERATOR)
 				throw new Error("this object is already an operator expression, and can't be used as a document expression (at '" + fieldName + "'.; uassert code 15990");
 
-			if (!ctx.isTopLevel && fieldName.indexOf(".") != -1)
+			if (!ctx.isTopLevel && fieldName.indexOf(".") !== -1)
 				throw new Error("dotted field names are only allowed at the top level; uassert code 16405");
 
-			if (expression === undefined) { // if it's our first time, create the document expression
-				if (!ctx.isDocumentOk)
-					throw new Error("Assertion failure"); // CW TODO error: document not allowed in this context
+			// if it's our first time, create the document expression
+			if (expression === undefined) {
+				if (!ctx.isDocumentOk) throw new Error("Assertion failure");
+				// CW TODO error: document not allowed in this context
 
-				expressionObject = new ObjectExpression(); //check for top level? //NOTE: DEVIATION FROM MONGO: the c++ calls createRoot() or create() here.
+				expressionObject = ctx.isTopLevel ? ObjectExpression.createRoot() : ObjectExpression.create();
 				expression = expressionObject;
 
-				kind = NOTOPERATOR; //this "object" is not an operator expression
+				// this "object" is not an operator expression
+				kind = NOTOPERATOR;
 			}
 
 			var fieldValue = obj[fieldName];
@@ -153,8 +152,9 @@ klass.parseObject = function parseObject(obj, ctx, vps) {
 
 					break;
 				case "string":
-					// it's a renamed field         // CW TODO could also be a constant
-					expressionObject.addField(fieldName, new FieldPathExpression.parse(fieldValue, vps));
+					// it's a renamed field
+					// CW TODO could also be a constant
+					expressionObject.addField(fieldName, FieldPathExpression.parse(fieldValue, vps));
 					break;
 				case "boolean":
 				case "number":
@@ -170,7 +170,7 @@ klass.parseObject = function parseObject(obj, ctx, vps) {
 					}
 					break;
 				default:
-					throw new Error("disallowed field type " + (fieldValue instanceof Object ? fieldValue.constructor.name + ":" : typeof fieldValue) + typeof(fieldValue) + " in object expression (at '" + fieldName + "') uassert code 15992");
+					throw new Error("disallowed field type " + Value.getType(fieldValue) + " in object expression (at '" + fieldName + "') uassert code 15992");
 			}
 		}
 	}
@@ -181,10 +181,11 @@ klass.parseObject = function parseObject(obj, ctx, vps) {
 
 klass.expressionParserMap = {};
 
-/** Registers an ExpressionParser so it can be called from parseExpression and friends.
- *
- *  As an example, if your expression looks like {"$foo": [1,2,3]} you would add this line:
- *  REGISTER_EXPRESSION("$foo", ExpressionFoo::parse);
+
+/**
+ * Registers an ExpressionParser so it can be called from parseExpression and friends.
+ * As an example, if your expression looks like {"$foo": [1,2,3]} you would add this line:
+ * REGISTER_EXPRESSION("$foo", ExpressionFoo::parse);
  */
 klass.registerExpression = function registerExpression(key, parserFunc) {
 	if (key in klass.expressionParserMap) {
@@ -194,6 +195,7 @@ klass.registerExpression = function registerExpression(key, parserFunc) {
 	return 1;
 };
 
+
 /**
  * Parses a BSONElement which has already been determined to be functional expression.
  * @static
@@ -202,7 +204,7 @@ klass.registerExpression = function registerExpression(key, parserFunc) {
  *    That is the field name should be the $op for the expression.
  * @param vps the variable parse state
  * @returns the parsed Expression
- **/
+ */
 //NOTE: DEVIATION FROM MONGO: the c++ version has 2 arguments, not 3.	//TODO: could easily fix this inconsistency
 klass.parseExpression = function parseExpression(exprElementKey, exprElementValue, vps) {
 	if (!(exprElementKey in Expression.expressionParserMap)) {
@@ -211,6 +213,7 @@ klass.parseExpression = function parseExpression(exprElementKey, exprElementValu
 	return Expression.expressionParserMap[exprElementKey](exprElementValue, vps);
 };
 
+
 /**
  * Parses a BSONElement which is an operand in an Expression.
  *
@@ -224,7 +227,7 @@ klass.parseExpression = function parseExpression(exprElementKey, exprElementValu
  *    That is the field name should be the $op for the expression.
  * @param vps the variable parse state
  * @returns the parsed operand, as an Expression
- **/
+ */
 klass.parseOperand = function parseOperand(exprElement, vps) {
 	var t = typeof(exprElement);
 	if (t === "string" && exprElement[0] == "$") { //if we got here, this is a field path expression
@@ -238,13 +241,13 @@ klass.parseOperand = function parseOperand(exprElement, vps) {
 	}
 };
 
-// PROTOTYPE MEMBERS
+
 /**
  * Evaluate the Expression using the given document as input.
  *
  * @method evaluate
  * @returns the computed value
- **/
+ */
 proto.evaluateInternal = function evaluateInternal(obj) {
 	throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!");
 };
@@ -256,7 +259,7 @@ proto.evaluateInternal = function evaluateInternal(obj) {
  * While vars is non-const, if properly constructed, subexpressions modifications to it
  * should not effect outer expressions due to unique variable Ids.
  */
-proto.evaluate = function(vars) {
+proto.evaluate = function evaluate(vars) {
 	return this.evaluateInternal(vars);
 };
 
@@ -273,7 +276,7 @@ proto.evaluate = function(vars) {
  *
  * @method optimize
  * @returns the optimized Expression
- **/
+ */
 proto.optimize = function optimize() {
 	throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!");
 };
@@ -287,7 +290,7 @@ proto.optimize = function optimize() {
  * @method addDependencies
  * @param deps  output parameter
  * @param path  path to self if all ancestors are ExpressionObjects.
- **/
+ */
 proto.addDependencies = function addDependencies(deps, path) {
 	throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!");
 };
@@ -295,18 +298,17 @@ proto.addDependencies = function addDependencies(deps, path) {
 /**
  * simple expressions are just inclusion exclusion as supported by ExpressionObject
  * @method getIsSimple
- **/
+ */
 proto.getIsSimple = function getIsSimple() {
 	return false;
 };
 
+
 proto.toMatcherBson = function toMatcherBson() {
 	throw new Error("WAS NOT IMPLEMENTED BY INHERITOR!"); //verify(false && "Expression::toMatcherBson()");
 };
 
 
-// DEPENDENCIES
-var Document = require("../Document");
-var ObjectExpression = require("./ObjectExpression");
-var FieldPathExpression = require("./FieldPathExpression");
-var ConstantExpression = require("./ConstantExpression");
+var ObjectExpression = require("./ObjectExpression"),
+	FieldPathExpression = require("./FieldPathExpression"),
+	ConstantExpression = require("./ConstantExpression");

+ 35 - 0
lib/pipeline/expressions/FixedArityExpressionT.js

@@ -0,0 +1,35 @@
+"use strict";
+
+/**
+ * A factory and base class for expressions that take a fixed number of arguments
+ * @class FixedArityExpressionT
+ * @namespace mungedb-aggregate.pipeline.expressions
+ * @module mungedb-aggregate
+ * @constructor
+ **/
+
+var FixedArityExpressionT = module.exports = function FixedArityExpressionT(SubClass, nArgs) {
+
+	var FixedArityExpression = function FixedArityExpression() {
+		if (arguments.length !== 0) throw new Error(klass.name + "<" + SubClass.name + ">: zero args expected");
+		base.call(this);
+	}, klass = FixedArityExpression, base = require("./NaryBaseExpressionT")(SubClass), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
+
+	/**
+	 * Check that the number of args is what we expected
+	 * @method validateArguments
+	 * @param args Array The array of arguments to the expression
+	 * @throws
+	 **/
+	proto.validateArguments = function validateArguments(args) {
+		if(args.length !== nArgs) {
+			throw new Error("Expression " + this.getOpName() + " takes exactly " +
+				nArgs + " arguments. " + args.length + " were passed in.");
+		}
+	};
+
+	klass.parse = base.parse; 	//NOTE: Need to explicitly bubble static members
+								// in our inheritance chain
+	return FixedArityExpression;
+};
+

+ 9 - 3
lib/pipeline/expressions/HourExpression.js

@@ -9,9 +9,15 @@
  * @constructor
  **/
 var HourExpression = module.exports = function HourExpression(){
-	this.nargs = 1;
 	base.call(this);
-}, klass = HourExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = HourExpression,
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
+	proto = klass.prototype = Object.create(base.prototype, {
+		constructor:{
+			value:klass
+		}
+	});
 
 // PROTOTYPE MEMBERS
 proto.getOpName = function getOpName(){
@@ -32,4 +38,4 @@ proto.evaluateInternal = function evaluateInternal(vars){
 
 
 /** Register Expression */
-Expression.registerExpression("$hour",base.parse(HourExpression));
+Expression.registerExpression("$hour",base.parse);

+ 3 - 3
lib/pipeline/expressions/IfNullExpression.js

@@ -9,11 +9,11 @@
  * @constructor
  **/
 var IfNullExpression = module.exports = function IfNullExpression() {
-	this.nargs = 2;
 	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
 }, klass = IfNullExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -42,4 +42,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$ifNull", base.parse(IfNullExpression));
+Expression.registerExpression("$ifNull", base.parse);

+ 3 - 3
lib/pipeline/expressions/LetExpression.js

@@ -1,12 +1,12 @@
 "use strict";
 
-Expression.registerExpression("$let", LetExpression.parse);
-
 var LetExpression = module.exports = function LetExpression(vars, subExpression){
 	//if (arguments.length !== 2) throw new Error("Two args expected");
 	this._variables = vars;
 	this._subExpression = subExpression;
-}, klass = LetExpression, Expression = require("./FixedArityExpressionT")(klass, 2), base = Expression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = LetExpression, Expression = require("./Expression"), base = Expression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+Expression.registerExpression("$let", LetExpression.parse);
 
 // DEPENDENCIES
 var Variables = require("./Variables"),

+ 3 - 3
lib/pipeline/expressions/MillisecondExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var MillisecondExpression = module.exports = function MillisecondExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = MillisecondExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -39,4 +39,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$millisecond", base.parse(MillisecondExpression));
+Expression.registerExpression("$millisecond", base.parse);

+ 3 - 3
lib/pipeline/expressions/MinuteExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var MinuteExpression = module.exports = function MinuteExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = MinuteExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -37,4 +37,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$minute", base.parse(MinuteExpression));
+Expression.registerExpression("$minute", base.parse);

+ 3 - 3
lib/pipeline/expressions/ModExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var ModExpression = module.exports = function ModExpression() {
-	this.nargs = 2;
 	base.call(this);
 }, klass = ModExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -51,4 +51,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$mod", base.parse(ModExpression));
+Expression.registerExpression("$mod", base.parse);

+ 3 - 3
lib/pipeline/expressions/MonthExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var MonthExpression = module.exports = function MonthExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = MonthExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -37,4 +37,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$month", base.parse(MonthExpression));
+Expression.registerExpression("$month", base.parse);

+ 2 - 2
lib/pipeline/expressions/MultiplyExpression.js

@@ -11,7 +11,7 @@
 var MultiplyExpression = module.exports = function MultiplyExpression(){
 	//if (arguments.length !== 0) throw new Error("Zero args expected");
 	base.call(this);
-}, klass = MultiplyExpression, base = require("./VariadicExpressionT")(klass), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = MultiplyExpression, base = require("./VariadicExpressionT")(MultiplyExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -39,4 +39,4 @@ proto.evaluateInternal = function evaluateInternal(vars){
 };
 
 /** Register Expression */
-Expression.registerExpression(klass.opName, base.parse(klass));
+Expression.registerExpression(klass.opName, base.parse);

+ 27 - 0
lib/pipeline/expressions/NaryBaseExpressionT.js

@@ -0,0 +1,27 @@
+"use strict";
+
+/**
+ * Inherit from ExpressionVariadic or ExpressionFixedArity instead of directly from this class.
+ * @class NaryBaseExpressionT
+ * @namespace mungedb-aggregate.pipeline.expressions
+ * @module mungedb-aggregate
+ * @extends mungedb-aggregate.pipeline.expressions.NaryExpression
+ * @constructor
+ */
+var NaryBaseExpressionT = module.exports = function NaryBaseExpressionT(SubClass) {
+
+	var NaryBaseExpression = function NaryBaseExpression() {
+		if (arguments.length !== 0) throw new Error(klass.name + "<" + SubClass.name + ">: zero args expected");
+		base.call(this);
+	}, klass = NaryBaseExpression, NaryExpression = require("./NaryExpression"), base = NaryExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+	klass.parse = function(objExpr, vps) {
+		var expr = new SubClass(),
+			args = NaryExpression.parseArguments(objExpr, vps);
+		expr.validateArguments(args);
+		expr.operands = args;
+		return expr;
+	};
+
+	return NaryBaseExpression;
+};

+ 107 - 89
lib/pipeline/expressions/NaryExpression.js

@@ -7,128 +7,146 @@
  * @module mungedb-aggregate
  * @extends mungedb-aggregate.pipeline.expressions.Expression
  * @constructor
- **/
-var Expression = require("./Expression");
-
-var NaryExpression = module.exports = function NaryExpression(){
+ */
+var NaryExpression = module.exports = function NaryExpression() {
 	if (arguments.length !== 0) throw new Error("Zero args expected");
 	this.operands = [];
 	base.call(this);
-}, klass = NaryExpression, base = Expression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
-
-klass.parse = function(SubClass) {
-	return function parse(expr, vps) {
-		var outExpr = new SubClass(),
-			args = NaryExpression.parseArguments(expr, vps);
-		outExpr.validateArguments(args);
-		outExpr.operands = args;
-		return outExpr;
-	};
-};
-
-klass.parseArguments = function(exprElement, vps) {
-	var out = [];
-	if(exprElement instanceof Array) {
-		for(var ii = 0; ii < exprElement.length; ii++) {
-			out.push(Expression.parseOperand(exprElement[ii], vps));
-		}
-	} else {
-		out.push(Expression.parseOperand(exprElement, vps));
-	}
-	return out;
-};
-
+}, klass = NaryExpression, Expression = require("./Expression"), base = Expression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-function partitionBy(fn, coll) {
-	var ret = {pass:[],
-			   fail:[]};
-	coll.forEach(function(x) {
-		if(fn(x)) {
-			ret.pass.push(x);
-		} else {
-			ret.fail.push(x);
-		}
-	});
-	return ret;
-}
-// DEPENDENCIES
-var ConstantExpression = require("./ConstantExpression");
+var Variables = require("./Variables"),
+	ConstantExpression = require("./ConstantExpression");
 
-// PROTOTYPE MEMBERS
-proto.evaluate = undefined; // evaluate(doc){ ... defined by inheritor ... }
+proto.optimize = function optimize() {
+	var n = this.operands.length;
 
-proto.getOpName = function getOpName(doc){
-	throw new Error("NOT IMPLEMENTED BY INHERITOR");
-};
+	// optimize sub-expressions and count constants
+	var constCount = 0;
+	for (var i = 0; i < n; ++i) {
+		var optimized = this.operands[i].optimize();
 
-proto.optimize = function optimize(){
-	var n = this.operands.length,
-		constantCount = 0;
+		// substitute the optimized expression
+		this.operands[i] = optimized;
 
-	for(var ii = 0; ii < n; ii++) {
-		if(this.operands[ii] instanceof ConstantExpression) {
-			constantCount++;
-		} else {
-			this.operands[ii] = this.operands[ii].optimize();
-						}
-					}
+		// check to see if the result was a constant
+		if (optimized instanceof ConstantExpression) {
+			constCount++;
+		}
+	}
 
-	if(constantCount === n) {
-		return new ConstantExpression(this.evaluateInternal({}));
-				}
+	// If all the operands are constant, we can replace this expression with a constant. Using
+	// an empty Variables since it will never be accessed.
+	if (constCount === n) {
+		var emptyVars = new Variables(),
+			result = this.evaluateInternal(emptyVars),
+			replacement = ConstantExpression.create(result);
+		return replacement;
+	}
 
-	if(!this.isAssociativeAndCommutative) {
+	// Remaining optimizations are only for associative and commutative expressions.
+	if(!this.isAssociativeAndCommutative()) {
 		return this;
 	}
 
-	// Flatten and inline nested operations of the same type
-
-	var similar = partitionBy(function(x){ return x.getOpName() === this.getOpName();}, this.operands);
-
-	this.operands = similar.fail;
-	similar.pass.forEach(function(x){
-		this.operands.concat(x.operands);
-	});
-
-	// Partial constant folding
+	// Process vpOperand to split it into constant and nonconstant vectors.
+	// This can leave vpOperand in an invalid state that is cleaned up after the loop.
+	var constExprs = [],
+		nonConstExprs = [];
+	for (i = 0; i < this.operands.length; ++i) { // NOTE: vpOperand grows in loop
+		var expr = this.operands[i];
+		if (expr instanceof ConstantExpression) {
+			constExprs.push(expr);
+		} else {
+			// If the child operand is the same type as this, then we can
+			// extract its operands and inline them here because we know
+			// this is commutative and associative.  We detect sameness of
+			// the child operator by checking for equality of the opNames
+			var nary = expr instanceof NaryExpression ? expr : undefined;
+			if (!nary || nary.getOpName() !== this.getOpName) {
+				nonConstExprs.push(expr);
+			} else {
+				// same expression, so flatten by adding to vpOperand which
+				// will be processed later in this loop.
+				Array.prototype.push.apply(this.operands, nary.operands);
+			}
+		}
+	}
 
-	var constantOperands = partitionBy(function(x) {return x instanceof ConstantExpression;}, this.operands);
+	// collapse all constant expressions (if any)
+	var constValue;
+	if (constExprs.length > 0) {
+		this.operands = constExprs;
+		var emptyVars2 = new Variables();
+		constValue = this.evaluateInternal(emptyVars2);
+	}
 
-	this.operands = constantOperands.pass;
-	this.operands = [new ConstantExpression(this.evaluateInternal({}))].concat(constantOperands.fail);
+	// now set the final expression list with constant (if any) at the end
+	this.operands = nonConstExprs;
+	if (constExprs.length > 0) {
+		this.operands.push(ConstantExpression.create(constValue));
+	}
 
 	return this;
 };
 
-proto.addDependencies = function addDependencies(deps){
-	for(var i = 0, l = this.operands.length; i < l; ++i)
+proto.addDependencies = function addDependencies(deps, path) {
+	for (var i = 0, l = this.operands.length; i < l; ++i) {
 		this.operands[i].addDependencies(deps);
+	}
 };
 
 /**
  * Add an operand to the n-ary expression.
  * @method addOperand
- * @param pExpression the expression to add
- **/
+ * @param expr the expression to add
+ */
 proto.addOperand = function addOperand(expr) {
 	this.operands.push(expr);
 };
 
-proto.serialize = function serialize() {
-	var ret = {}, subret = [];
-	for(var ii = 0; ii < this.operands.length; ii++) {
-		subret.push(this.operands[ii].serialize());
+proto.serialize = function serialize(explain) {
+	var nOperand = this.operands.length,
+		array = [];
+	// build up the array
+	for (var i = 0; i < nOperand; i++) {
+		array.push(this.operands[i].serialize(explain));
 	}
-	ret[this.getOpName()] = subret;
-	return ret;
+
+	var obj = {};
+	obj[this.getOpName()] = array;
+	return obj;
+};
+
+proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() {
+	return false;
 };
 
-proto.fixedArity = function(nargs) {
-	this.nargs = nargs;
+/**
+ * Get the name of the operator.
+ * @method getOpName
+ * @returns the name of the operator; this string belongs to the class
+ *  implementation, and should not be deleted
+ *  and should not
+ */
+proto.getOpName = function getOpName() {
+	throw new Error("NOT IMPLEMENTED BY INHERITOR");
 };
 
-proto.validateArguments = function(args) {
-	if(this.nargs !== args.length) {
-		throw new Error("Expression " + this.getOpName() + " takes exactly " + this.nargs + " arguments. " + args.length + " were passed in.");
+/**
+ * Allow subclasses the opportunity to validate arguments at parse time.
+ * @method validateArguments
+ * @param {[type]} args [description]
+ */
+proto.validateArguments = function(args) {};
+
+klass.parseArguments = function(exprElement, vps) {
+	var out = [];
+	if (exprElement instanceof Array) {
+		for (var ii = 0; ii < exprElement.length; ii++) {
+			out.push(Expression.parseOperand(exprElement[ii], vps));
+		}
+	} else {
+		out.push(Expression.parseOperand(exprElement, vps));
 	}
+	return out;
 };

+ 3 - 3
lib/pipeline/expressions/NotExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var NotExpression = module.exports = function NotExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = NotExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -38,4 +38,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$not", base.parse(NotExpression));
+Expression.registerExpression("$not", base.parse);

+ 2 - 2
lib/pipeline/expressions/OrExpression.js

@@ -11,7 +11,7 @@
 var OrExpression = module.exports = function OrExpression(){
 //	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
-}, klass = OrExpression, base = require("./VariadicExpressionT")(klass), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = OrExpression, base = require("./VariadicExpressionT")(OrExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -65,4 +65,4 @@ proto.optimize = function optimize() {
 };
 
 /** Register Expression */
-Expression.registerExpression(klass.opName, base.parse(klass));
+Expression.registerExpression(klass.opName, base.parse);

+ 3 - 3
lib/pipeline/expressions/SecondExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var SecondExpression = module.exports = function SecondExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = SecondExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -39,4 +39,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$second", base.parse(SecondExpression));
+Expression.registerExpression("$second", base.parse);

+ 3 - 3
lib/pipeline/expressions/SetDifferenceExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var SetDifferenceExpression = module.exports = function SetDifferenceExpression() {
-	this.nargs = 2;
 	base.call(this);
 }, klass = SetDifferenceExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -49,4 +49,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$setdifference", base.parse(SetDifferenceExpression));
+Expression.registerExpression("$setdifference", base.parse);

+ 2 - 9
lib/pipeline/expressions/SetEqualsExpression.js

@@ -9,15 +9,8 @@
  * @constructor
  **/
 var SetEqualsExpression = module.exports = function SetEqualsExpression() {
-	this.nargs = 2;
 	base.call(this);
-}, klass = SetEqualsExpression,
-	base = require("./NaryExpression"),
-	proto = klass.prototype = Object.create(base.prototype, {
-		constructor: {
-			value: klass
-		}
-	});
+}, klass = SetEqualsExpression, base = require("./NaryBaseExpressionT")(SetEqualsExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -42,4 +35,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$setequals", base.parse(SetEqualsExpression));
+Expression.registerExpression("$setequals", base.parse);

+ 2 - 9
lib/pipeline/expressions/SetIntersectionExpression.js

@@ -9,15 +9,8 @@
  * @constructor
  **/
 var SetIntersectionExpression = module.exports = function SetIntersectionExpression() {
-	this.nargs = 2;
 	base.call(this);
-}, klass = SetIntersectionExpression,
-	base = require("./NaryExpression"),
-	proto = klass.prototype = Object.create(base.prototype, {
-		constructor: {
-			value: klass
-		}
-	});
+}, klass = SetIntersectionExpression, base = require("./NaryBaseExpressionT")(SetIntersectionExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -44,4 +37,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$setIntersection", base.parse(SetIntersectionExpression));
+Expression.registerExpression("$setIntersection", base.parse);

+ 3 - 3
lib/pipeline/expressions/SetIsSubsetExpression.js

@@ -10,11 +10,11 @@
  **/
 
 var SetIsSubsetExpression = module.exports = function SetIsSubsetExpression() {
-	this.nargs = 2;
 	if (arguments.length !== 2) throw new Error("two args expected");
 	base.call(this);
 }, klass = SetIsSubsetExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -105,4 +105,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$setissubset", base.parse(SetIsSubsetExpression));
+Expression.registerExpression("$setissubset", base.parse);

+ 2 - 9
lib/pipeline/expressions/SetUnionExpression.js

@@ -9,15 +9,8 @@
  * @constructor
  **/
 var SetUnionExpression = module.exports = function SetUnionExpression() {
-	this.nargs = 2;
 	base.call(this);
-}, klass = SetUnionExpression,
-	base = require("./NaryExpression"),
-	proto = klass.prototype = Object.create(base.prototype, {
-		constructor: {
-			value: klass
-		}
-	});
+}, klass = SetUnionExpression, base = require("./NaryBaseExpressionT")(SetUnionExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -50,4 +43,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$setUnion", base.parse(SetUnionExpression));
+Expression.registerExpression("$setUnion", base.parse);

+ 3 - 3
lib/pipeline/expressions/SizeExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var SizeExpression = module.exports = function SizeExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = SizeExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -38,4 +38,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$size", base.parse(SizeExpression));
+Expression.registerExpression("$size", base.parse);

+ 3 - 3
lib/pipeline/expressions/StrcasecmpExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var StrcasecmpExpression = module.exports = function StrcasecmpExpression() {
-	this.nargs = 2;
 	base.call(this);
 }, klass = StrcasecmpExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -43,4 +43,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$strcasecmp", base.parse(StrcasecmpExpression));
+Expression.registerExpression("$strcasecmp", base.parse);

+ 3 - 3
lib/pipeline/expressions/SubstrExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var SubstrExpression = module.exports = function SubstrExpression() {
-	this.nargs = 3;
 	base.call(this);
 }, klass = SubstrExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 3),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -46,4 +46,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$substr", base.parse(SubstrExpression));
+Expression.registerExpression("$substr", base.parse);

+ 3 - 3
lib/pipeline/expressions/SubtractExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var SubtractExpression = module.exports = function SubtractExpression(){
-	this.nargs = 2;
 	base.call(this);
 }, klass = SubtractExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -39,4 +39,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$subtract", base.parse(SubtractExpression));
+Expression.registerExpression("$subtract", base.parse);

+ 6 - 6
lib/pipeline/expressions/ToLowerExpression.js

@@ -1,6 +1,6 @@
 "use strict";
-	
-/** 
+
+/**
  * A $toLower pipeline expression.
  * @see evaluateInternal
  * @class ToLowerExpression
@@ -10,7 +10,7 @@
  **/
 var ToLowerExpression = module.exports = function ToLowerExpression(){
 	base.call(this);
-}, klass = ToLowerExpression, base = require("./FixedArityExpressionT")(klass, 1), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
+}, klass = ToLowerExpression, base = require("./FixedArityExpressionT")(ToLowerExpression, 1), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -23,8 +23,8 @@ proto.getOpName = function getOpName(){
 	return klass.opName;
 };
 
-/** 
-* Takes a single string and converts that string to lowercase, returning the result. All uppercase letters become lowercase. 
+/**
+* Takes a single string and converts that string to lowercase, returning the result. All uppercase letters become lowercase.
 **/
 proto.evaluateInternal = function evaluateInternal(vars) {
 	var val = this.operands[0].evaluateInternal(vars),
@@ -33,4 +33,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression(klass.opName, base.parse(klass));
+Expression.registerExpression(klass.opName, base.parse);

+ 2 - 2
lib/pipeline/expressions/ToUpperExpression.js

@@ -10,7 +10,7 @@
  **/
 var ToUpperExpression = module.exports = function ToUpperExpression() {
 	base.call(this);
-}, klass = ToUpperExpression, base = require("./FixedArityExpressionT")(klass, 1), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass }});
+}, klass = ToUpperExpression, base = require("./FixedArityExpressionT")(ToUpperExpression, 1), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass }});
 
 // DEPENDENCIES
 var Value = require("../Value"),
@@ -33,4 +33,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression(klass.opName, base.parse(klass));
+Expression.registerExpression(klass.opName, base.parse);

+ 3 - 1
lib/pipeline/expressions/VariadicExpressionT.js

@@ -13,7 +13,9 @@ var VariadicExpressionT = module.exports = function VariadicExpressionT(SubClass
 	var VariadicExpression = function VariadicExpression() {
 		if (arguments.length !== 0) throw new Error(klass.name + "<" + SubClass.name + ">: zero args expected");
 		base.call(this);
-	}, klass = VariadicExpression, base = require("./NaryExpressionT")(SubClass), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
+	}, klass = VariadicExpression, base = require("./NaryBaseExpressionT")(SubClass), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
 
+	klass.parse = base.parse; 	//NOTE: Need to explicitly bubble static members
+								// in our inheritance chain
 	return VariadicExpression;
 };

+ 3 - 3
lib/pipeline/expressions/WeekExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var WeekExpression = module.exports = function WeekExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = WeekExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -47,4 +47,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$week", base.parse(WeekExpression));
+Expression.registerExpression("$week", base.parse);

+ 3 - 3
lib/pipeline/expressions/YearExpression.js

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var YearExpression = module.exports = function YearExpression() {
-	this.nargs = 1;
 	base.call(this);
 }, klass = YearExpression,
-	base = require("./NaryExpression"),
+	FixedArityExpression = require("./FixedArityExpressionT")(klass, 1),
+	base = FixedArityExpression,
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -40,4 +40,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$year", base.parse(YearExpression));
+Expression.registerExpression("$year", base.parse);

+ 1 - 1
lib/pipeline/expressions/index.js

@@ -19,7 +19,7 @@ module.exports = {
 	ModExpression: require("./ModExpression.js"),
 	MonthExpression: require("./MonthExpression.js"),
 	MultiplyExpression: require("./MultiplyExpression.js"),
-	NaryExpression: require("./NaryExpression.js"),
+	NaryBaseExpressionT: require("./NaryBaseExpressionT.js"),
 	NotExpression: require("./NotExpression.js"),
 	ObjectExpression: require("./ObjectExpression.js"),
 	OrExpression: require("./OrExpression.js"),

+ 87 - 0
test/lib/pipeline/DepsTracker_test.js

@@ -0,0 +1,87 @@
+"use strict";
+var assert = require("assert"),
+	DepsTracker = require("../../../lib/pipeline/DepsTracker");
+
+// Mocha one-liner to make these tests self-hosted
+if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
+
+exports.DepsTracker = {
+
+	"#toProjection()": {
+
+		"should be able to convert dependencies to a projection": function(){
+			var deps = new DepsTracker(),
+				expected = {_id:0,a:1,b:1};
+			deps.fields = {a:1,b:1};
+			assert.deepEqual(expected, deps.toProjection());
+		},
+
+		"should be able to convert dependencies with subfields to a projection": function(){
+			var deps = new DepsTracker(),
+				expected = {_id:0,a:1};
+			deps.fields = {a:1,"a.b":1};
+			assert.deepEqual(expected, deps.toProjection());
+		},
+
+		"should be able to convert dependencies with _id to a projection": function(){
+			var deps = new DepsTracker(),
+				expected = {a:1,b:1,_id:1};
+			deps.fields = {_id:1,a:1,b:1};
+			assert.deepEqual(expected, deps.toProjection());
+		},
+
+		"should be able to convert dependencies with id and subfields to a projection": function(){
+			var deps = new DepsTracker(),
+				expected = {_id:1,b:1};
+			deps.fields = {"_id.a":1,b:1};
+			assert.deepEqual(expected, deps.toProjection());
+		},
+
+		"should return empty object if needWholeDocument is true": function() {
+			var deps = new DepsTracker(),
+				expected = {};
+			deps.needWholeDocument = true;
+			assert.deepEqual(expected, deps.toProjection());
+		},
+
+		"should return $noFieldsNeeded if there are no dependencies": function() {
+			var deps = new DepsTracker(),
+				expected = {_id:0,$noFieldsNeeded:1};
+			assert.deepEqual(expected, deps.toProjection());
+		},
+
+	},
+
+	"#toParsedDeps()": {
+
+		"should not parse if needWholeDocument is true": function() {
+			var deps = new DepsTracker(),
+				expected; // undefined;
+			deps.needWholeDocument = true;
+			assert.strictEqual(expected, deps.toParsedDeps());
+		},
+
+		"should not parse if needTextScore is true": function() {
+			var deps = new DepsTracker(),
+				expected; // undefined;
+			deps.needTextScore = true;
+			assert.strictEqual(expected, deps.toParsedDeps());
+		},
+
+		"should be able to parse dependencies": function() {
+			var deps = new DepsTracker(),
+				expected = {_fields:{a:true,b:true}};
+			deps.fields = {a:1,b:1};
+			assert.deepEqual(expected, deps.toParsedDeps());
+		},
+
+		"should be able to parse dependencies with subfields": function() {
+			var deps = new DepsTracker(),
+				expected = {_fields:{a:true}};
+			deps.fields = {a:1,"a.b":1};
+			assert.deepEqual(expected, deps.toParsedDeps());
+		},
+
+	},
+
+};

+ 74 - 0
test/lib/pipeline/ParsedDeps.js

@@ -0,0 +1,74 @@
+"use strict";
+var assert = require("assert"),
+	ParsedDeps = require("../../../lib/pipeline/ParsedDeps");
+
+module.exports = {
+	"ParsedDeps": {
+		"#extractFields": {
+			"should be able to convert a document to its projected form": function() {
+				var deps = {'a': true, 'b': true},
+					doc = {a:23, b:64, c:92},
+					parse = new ParsedDeps(deps);
+
+				var proj = parse.extractFields(doc);
+				assert.deepEqual({a:23,b:64}, proj);
+			}
+		},
+		"#_documentHelper": {
+			"should skip fields that are not needed": function() {
+				var json = {'foo':'bar'},
+					neededFields = {},
+					parse = new ParsedDeps(),
+					expected = {};
+				assert.deepEqual(expected, parse._documentHelper(json, neededFields));
+			},
+			"should return values that are booleans": function() {
+				var json = {'foo':'bar'},
+					neededFields = {'foo':true},
+					parse = new ParsedDeps(),
+					expected = {'foo':'bar'};
+				assert.deepEqual(expected, parse._documentHelper(json, neededFields));
+			},
+			"should call _arrayHelper on values that are arrays": function() {
+				var json = {'foo':[{'bar':'baz'}]},
+					neededFields = {'foo':true},
+					parse = new ParsedDeps(),
+					expected = {'foo':true};
+				// TODO: mock out _arrayHelper to return true
+				parse._arrayHelper = function() {
+					return true;
+				};
+				assert.deepEqual(expected, parse._documentHelper(json, neededFields));
+			},
+			"should recurse on values that are objects": function() {
+				var json = {'foo':{'bar':'baz'}},
+					neededFields = {'foo':true},
+					parse = new ParsedDeps(),
+					expected = {'foo':{'bar':'baz'}};
+				assert.deepEqual(expected, parse._documentHelper(json, neededFields));
+			}
+		},
+		"#_arrayHelper": {
+			"should call _documentHelper on values that are objects": function() {
+				var array = [{'foo':'bar'}],
+					neededFields = {'foo':true},
+					parse = new ParsedDeps(),
+					expected = [true];
+				// TODO: mock out _documentHelper to return true
+				parse._documentHelper = function() {
+					return true;
+				};
+				assert.deepEqual(expected, parse._arrayHelper(array, neededFields));
+			},
+			"should recurse on values that are arrays": function() {
+				var array = [[{'foo':'bar'}]],
+					neededFields = {'foo':true},
+					parse = new ParsedDeps(),
+					expected = [[{'foo':'bar'}]];
+				assert.deepEqual(expected, parse._arrayHelper(array, neededFields));
+			}
+		}
+	}
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();

+ 0 - 42
test/lib/pipeline/documentSources/DocumentSource.js

@@ -7,50 +7,8 @@ module.exports = {
 
 	"DocumentSource": {
 
-		"#depsToProjection()": {
-			"should be able to convert dependencies to a projection": function(){
-				var array = {'a':1,'b':1},
-					expected = '{"_id":0,"a":1,"b":1}',
-					proj = DocumentSource.depsToProjection(array);
-
-				assert.equal(expected, JSON.stringify(proj));
-			},
-			"should be able to convert dependencies with subfields to a projection": function(){
-				var array = {'a':1,'a.b':1},
-					expected = '{"_id":0,"a":1}',
-					proj = DocumentSource.depsToProjection(array);
-
-				assert.equal(expected, JSON.stringify(proj));
-			},
-			"should be able to convert dependencies with _id to a projection": function(){
-				var array = {"_id":1,'a':1,'b':1},
-					expected = '{"a":1,"b":1,"_id":1}',
-					proj = DocumentSource.depsToProjection(array);
-
-				assert.equal(expected, JSON.stringify(proj));
-			},
-			"should be able to convert dependencies with id and subfields to a projection": function(){
-				var array = {'_id.a':1,'b':1},
-					expected = '{"_id":1,"b":1}',
-					proj = DocumentSource.depsToProjection(array);
-
-				assert.equal(expected, JSON.stringify(proj));
-			},
-		},
-
-		"#documentFromJsonWithDeps()": {
-			"should be able to convert a document to its projected form": function() {
-				var deps = {'a': true, 'b': true},
-					doc = {a:23, b:64, c:92};
-
-				var proj = DocumentSource.documentFromJsonWithDeps(doc, deps);
-				assert.deepEqual({a:23,b:64}, proj);
-			}
-		}
-
 	}
 
 };
 
 if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();
-

+ 0 - 151
test/lib/pipeline/expressions/NaryExpression.js

@@ -1,151 +0,0 @@
-"use strict";
-var assert = require("assert"),
-	VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
-	VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
-	NaryExpression = require("../../../../lib/pipeline/expressions/NaryExpression"),
-	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
-	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
-	Expression = require("../../../../lib/pipeline/expressions/Expression");
-
-
-// A dummy child of NaryExpression used for testing
-var TestableExpression = (function(){
-	// CONSTRUCTOR
-	var klass = function TestableExpression(operands, haveFactory){
-		base.call(this);
-		if (operands) {
-			var self = this;
-			operands.forEach(function(operand) {
-				self.addOperand(operand);
-			});
-		}
-		this.haveFactory = !!haveFactory;
-	}, base = NaryExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
-
-	// PROTOTYPE MEMBERS
-	proto.evaluateInternal = function evaluateInternal(vps) {
-		// Just put all the values in a list.  This is not associative/commutative so
-		// the results will change if a factory is provided and operations are reordered.
-		return this.operands.map(function(operand) {
-			return operand.evaluateInternal(vps);
-		});
-	};
-
-	proto.isAssociativeAndCommutative = function isAssociativeAndCommutative(){
-		return this.isAssociativeAndCommutative;
-	};
-
-	proto.getOpName = function getOpName() {
-		return "$testable";
-	};
-
-	klass.createFromOperands = function(operands) {
-		var vps = new VariablesParseState(new VariablesIdGenerator()),
-			testable = new TestableExpression();
-		operands.forEach(function(x) {
-			testable.addOperand(Expression.parseOperand(x, vps));
-		});
-		return testable;
-	};
-
-	return klass;
-})();
-
-
-module.exports = {
-
-	"NaryExpression": {
-
-		"constructor()": {
-
-		},
-
-		"#optimize()": {
-
-			"should suboptimize": function() {
-				var testable = TestableExpression.createFromOperands([{"$and": []}, "$abc"], true);
-				testable = testable.optimize();
-				assert.deepEqual(testable.serialize(), {$testable: [true,"$abc"]});
-			},
-			"should fold constants": function() {
-				var testable = TestableExpression.createFromOperands([1,2], true);
-				testable = testable.optimize();
-				assert.deepEqual(testable.serialize(), {$const: [1,2]});
-			},
-
-			"should place constants at the end of operands array": function() {
-				var testable = TestableExpression.createFromOperands([55,65, "$path"], true);
-				testable = testable.optimize();
-				assert.deepEqual(testable.serialize(), {$testable:["$path", [55,66]]});
-			},
-
-			"should flatten two layers" : function() {
-				var testable = TestableExpression.createFromOperands([55, "$path", {$add: [5,6,"$q"]}], true);
-				testable.addOperand(TestableExpression.createFromOperands([99,100,"$another_path"], true));
-				testable = testable.optimize();
-				assert.deepEqual(testable.serialize(), {$testable: ["$path", {$add: [5,6,"$q"]}, "$another_path", [55,66,[99,100]]]});
-			},
-
-			"should flatten three layers": function(){
-				var bottom = TestableExpression.createFromOperands([5,6,"$c"], true),
-					middle = TestableExpression.createFromOperands([3,4,"$b"], true).addOperand(bottom),
-					top = TestableExpression.createFromOperands([1,2,"$a"], true);
-				var testable = top.optimize();
-				assert.deepEqual(testable.serialize(), {$testable: ["$a", "$b", "$c", [1,2,[3,4,[5,6]]]]});
-			}
-
-		},
-
-		"#addOperand() should be able to add operands to expressions": function testAddOperand(){
-			var foo = new TestableExpression([new ConstantExpression(9)]).serialize();
-			var bar = new TestableExpression([new ConstantExpression(9)]).serialize();
-			var baz = {"$testable":[{"$const":9}]};
-
-			assert.deepEqual(foo,bar);
-			assert.deepEqual(foo, baz);
-			assert.deepEqual(baz,foo);
-			assert.deepEqual(new TestableExpression([new ConstantExpression(9)]).serialize(), {"$testable":[{"$const":9}]});
-			assert.deepEqual(new TestableExpression([new FieldPathExpression("ab.c")]).serialize(), {$testable:["$ab.c"]});
-		},
-
-
-		"#serialize() should convert an object to json": function(){
-			var testable = new TestableExpression();
-			testable.addOperand(new ConstantExpression(5));
-			assert.deepEqual({foo: testable.serialize()}, {foo:{$testable:[{$const: 5}]}});
-		},
-
-
-		//the following test case is eagerly awaiting ObjectExpression
-		"#addDependencies()": function testDependencies(){
-			var testableExpr = new TestableExpression();
-			var deps = {};
-			// no arguments
-			testableExpr.addDependencies(deps);
-			assert.deepEqual(deps, {});
-
-			// add a constant argument
-			testableExpr.addOperand(new ConstantExpression(1));
-
-			deps = {};
-			testableExpr.addDependencies(deps);
-			assert.deepEqual(deps, {});
-
-			// add a field path argument
-			testableExpr.addOperand(new FieldPathExpression("ab.c"));
-			deps = {};
-			testableExpr.addDependencies(deps);
-			assert.deepEqual(deps, {"ab.c":1});
-
-			// add an object expression
-			testableExpr.addOperand(Expression.parseObject({a:"$x",q:"$r"}, new Expression.ObjectCtx({isDocumentOk:1})));
-			deps = {};
-			testableExpr.addDependencies(deps);
-			assert.deepEqual(deps, {"ab.c":1, "x":1, "r":1});
-		}
-
-	}
-
-};
-
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);

+ 241 - 0
test/lib/pipeline/expressions/NaryExpression_test.js

@@ -0,0 +1,241 @@
+"use strict";
+
+var assert = require("assert"),
+	VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
+	VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
+	NaryExpression = require("../../../../lib/pipeline/expressions/NaryExpression"),
+	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
+	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression"),
+	utils = require("./utils");
+
+
+// Mocha one-liner to make these tests self-hosted
+if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
+
+
+// A dummy child of NaryExpression used for testing
+var Testable = (function(){
+	// CONSTRUCTOR
+	var klass = function Testable(isAssociativeAndCommutative){
+		this._isAssociativeAndCommutative = isAssociativeAndCommutative;
+		base.call(this);
+	}, base = NaryExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+	// MEMBERS
+	proto.evaluateInternal = function evaluateInternal(vars) {
+		// Just put all the values in a list.  This is not associative/commutative so
+		// the results will change if a factory is provided and operations are reordered.
+		var values = [];
+		for (var i = 0, l = this.operands.length; i < l; i++) {
+			values.push(this.operands[i].evaluateInternal(vars));
+		}
+		return values;
+	};
+
+	proto.getOpName = function getOpName() {
+		return "$testable";
+	};
+
+	proto.isAssociativeAndCommutative = function isAssociativeAndCommutative(){
+		return this._isAssociativeAndCommutative;
+	};
+
+	klass.create = function create(associativeAndCommutative) {
+		return new Testable(Boolean(associativeAndCommutative));
+	};
+
+	klass.factory = function factory() {
+		return new Testable(true);
+	};
+
+	klass.createFromOperands = function(operands, haveFactory) {
+		if (haveFactory === undefined) haveFactory = false;
+		var idGenerator = new VariablesIdGenerator(),
+			vps = new VariablesParseState(idGenerator),
+			testable = Testable.create(haveFactory);
+		operands.forEach(function(element) {
+			testable.addOperand(Expression.parseOperand(element, vps));
+		});
+		return testable;
+	};
+
+	proto.assertContents = function assertContents(expectedContents) {
+		assert.deepEqual(utils.constify({$testable:expectedContents}), utils.expressionToJson(this));
+	};
+
+	return klass;
+})();
+
+
+exports.NaryExpression = {
+
+	".parseArguments()": {
+
+		"should parse a fieldPathExpression": function() {
+			var vps = new VariablesParseState(new VariablesIdGenerator()),
+				parsedArguments = NaryExpression.parseArguments("$field.path.expression", vps);
+			assert.equal(parsedArguments.length, 1);
+			assert(parsedArguments[0] instanceof FieldPathExpression);
+		},
+
+		"should parse an array of fieldPathExpressions": function() {
+			var vps = new VariablesParseState(new VariablesIdGenerator()),
+				parsedArguments = NaryExpression.parseArguments(["$field.path.expression", "$another.FPE"], vps);
+			assert.equal(parsedArguments.length, 2);
+			assert(parsedArguments[0] instanceof FieldPathExpression);
+			assert(parsedArguments[1] instanceof FieldPathExpression);
+		},
+
+	},
+
+	/** Adding operands to the expression. */
+	"AddOperand": function testAddOperand() {
+		var testable = Testable.create();
+		testable.addOperand(ConstantExpression.create(9));
+		testable.assertContents([9]);
+		testable.addOperand(FieldPathExpression.create("ab.c"));
+		testable.assertContents([9, "$ab.c"]);
+	},
+
+	/** Dependencies of the expression. */
+	"Dependencies": function testDependencies() {
+		var testable = Testable.create();
+
+		var assertDependencies = function assertDependencies(expectedDeps, expr) {
+			var deps = {}, //TODO: new DepsTracker
+				depsJson = [];
+			expr.addDependencies(deps);
+			deps.forEach(function(dep) {
+				depsJson.push(dep);
+			});
+			assert.deepEqual(depsJson, expectedDeps);
+			assert.equal(deps.needWholeDocument, false);
+			assert.equal(deps.needTextScore, false);
+		};
+
+		// No arguments.
+		assertDependencies([], testable);
+
+		// Add a constant argument.
+		testable.addOperand(new ConstantExpression(1));
+		assertDependencies([], testable);
+
+		// Add a field path argument.
+		testable.addOperand(new FieldPathExpression("ab.c"));
+		assertDependencies(["ab.c"], testable);
+
+		// Add an object expression.
+		var spec = {a:"$x", q:"$r"},
+			specElement = spec,
+			ctx = new Expression.ObjectCtx({isDocumentOk:true}),
+			vps = new VariablesParseState(new VariablesIdGenerator());
+		testable.addOperand(Expression.parseObject(specElement, ctx, vps));
+		assertDependencies(["ab.c", "r", "x"]);
+	},
+
+	/** Serialize to an object. */
+	"AddToJsonObj": function testAddToJsonObj() {
+		var testable = Testable.create();
+		testable.addOperand(new ConstantExpression(5));
+		assert.deepEqual(
+			{foo:{$testable:[{$const:5}]}},
+			{foo:testable.serialize(false)}
+		);
+	},
+
+	/** Serialize to an array. */
+	"AddToJsonArray": function testAddToJsonArray() {
+		var testable = Testable.create();
+		testable.addOperand(new ConstantExpression(5));
+		assert.deepEqual(
+			[{$testable:[{$const:5}]}],
+			[testable.serialize(false)]
+		);
+	},
+
+	/** One operand is optimized to a constant, while another is left as is. */
+	"OptimizeOneOperand": function testOptimizeOneOperand() {
+		var spec = [{$and:[]}, "$abc"],
+			testable = Testable.createFromOperands(spec);
+		testable.assertContents(spec);
+		assert.deepEqual(testable.serialize(), testable.optimize().serialize());
+		testable.assertContents([true, "$abc"]);
+	},
+
+	/** All operands are constants, and the operator is evaluated with them. */
+	"EvaluateAllConstantOperands": function testEvaluateAllConstantOperands() {
+		var spec = [1, 2],
+			testable = Testable.createFromOperands(spec);
+		testable.assertContents(spec);
+		var optimized = testable.optimize();
+		assert.notDeepEqual(testable.serialize(), optimized.serialize());
+		assert.deepEqual({$const:[1,2]}, utils.expressionToJson(optimized));
+	},
+
+	"NoFactoryOptimize": {
+		// Without factory optimization, optimization will not produce a new expression.
+
+		/** A string constant prevents factory optimization. */
+		"StringConstant": function testStringConstant() {
+			var testable = Testable.createFromOperands(["abc", "def", "$path"], true);
+			assert.strictEqual(testable, testable.optimize());
+		},
+
+		/** A single (instead of multiple) constant prevents optimization.  SERVER-6192 */
+		"SingleConstant": function testSingleConstant() {
+			var testable = Testable.createFromOperands([55, "$path"], true);
+			assert.strictEqual(testable, testable.optimize());
+		},
+
+		/** Factory optimization is not used without a factory. */
+		"NoFactory": function testNoFactory() {
+			var testable = Testable.createFromOperands([55, 66, "$path"], false);
+			assert.strictEqual(testable, testable.optimize());
+		},
+
+	},
+
+	/** Factory optimization separates constant from non constant expressions. */
+	"FactoryOptimize": function testFactoryOptimize() {
+		// The constant expressions are evaluated separately and placed at the end.
+		var testable = Testable.createFromOperands([55, 66, "$path"], true),
+			optimized = testable.optimize();
+		assert.deepEqual(utils.constify({$testable:["$path", [55, 66]]}), utils.expressionToJson(optimized));
+	},
+
+	/** Factory optimization flattens nested operators of the same type. */
+	"FlattenOptimize": function testFlattenOptimize() {
+		var testable = Testable.createFromOperands(
+				[55, "$path", {$add:[5,6,"$q"]}, 66],
+			true);
+		testable.addOperand(Testable.createFromOperands(
+				[99, 100, "$another_path"],
+			true));
+		var optimized = testable.optimize();
+		assert.deepEqual(
+			utils.constify({$testable:[
+					"$path",
+					{$add:["$q", 11]},
+					"$another_path",
+					[55, 66, [99, 100]]
+				]}),
+			utils.expressionToJson(optimized));
+	},
+
+	/** Three layers of factory optimization are flattened. */
+	"FlattenThreeLayers": function testFlattenThreeLayers() {
+		var top = Testable.createFromOperands([1, 2, "$a"], true),
+			nested = Testable.createFromOperands([3, 4, "$b"], true);
+		nested.addOperand(Testable.createFromOperands([5, 6, "$c"], true));
+		top.addOperand(nested);
+		var optimized = top.optimize();
+		assert.deepEqual(
+			utils.constify({
+				$testable: ["$a", "$b", "$c", [1, 2, [3, 4, [5, 6]]]]
+			}),
+			utils.expressionToJson(optimized)
+		);
+	},
+
+};

+ 0 - 36
test/lib/pipeline/expressions/VariadicExpressionT_test.js

@@ -1,36 +0,0 @@
-"use strict";
-
-var assert = require("assert"),
-	VariadicExpressionT = require("../../../../lib/pipeline/expressions/VariadicExpressionT"),
-	NaryExpressionT = require("../../../../lib/pipeline/expressions/NaryExpressionT");
-
-
-//TODO: refactor these test cases using Expression.parseOperand() or something because these could be a whole lot cleaner...
-module.exports = {
-
-	"VariadicExpression": {
-
-		"constructor()": {
-
-			"should not throw Error when constructing without args": function testConstructor() {
-				assert.doesNotThrow(function () {
-					new VariadicExpressionT({});
-				});
-			},
-
-			"should be an instance of NaryExpression": function () {
-				var VariadicExpressionString = VariadicExpressionT(String);
-				assert.doesNotThrow(function() {
-					var ves = new VariadicExpressionString();
-				});
-				var ves = new VariadicExpressionString();
-				assert(ves.addOperand);
-				assert(ves.validateArguments);
-				//.... and so on. These prove we have a NaryExpression
-			}
-		}
-	}
-};
-
-
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);

+ 46 - 0
test/lib/pipeline/expressions/utils.js

@@ -0,0 +1,46 @@
+"use strict";
+
+var utils = module.exports = {
+
+	/**
+	 * Convert BSONObj to a BSONObj with our $const wrappings.
+	 * @method constify
+	 */
+	constify: function constify(obj, parentIsArray) {
+		if (parentIsArray === undefined) parentIsArray = false;
+		var bob = parentIsArray ? [] : {};
+		for (var key in obj) {
+			if (!obj.hasOwnProperty(key)) continue;
+			var elem = obj[key];
+			if (elem instanceof Object && elem.constructor === Object) {
+				bob[key] = utils.constify(elem, false);
+			} else if (Array.isArray(elem) && !parentIsArray) {
+				// arrays within arrays are treated as constant values by the real parser
+				bob[key] = utils.constify(elem, true);
+			} else if (key == "$const" ||
+					(typeof elem == "string" && elem[0] == "$")) {
+				bob[key] = obj[key];
+			} else {
+				bob[key] = {$const: obj[key]};
+			}
+		}
+		return bob;
+	},
+
+	//SKIPPED: assertBinaryEqual
+
+	//SKIPPED: toJson
+
+    /**
+     * Convert Expression to BSON.
+     * @method expressionToJson
+     */
+	expressionToJson: function expressionToJson(expr) {
+		return expr.serialize(false);
+	},
+
+	//SKIPPED: fromJson
+
+	//SKIPPED: valueFromJson
+
+};

+ 102 - 0
test/lib/pipeline/expressions/utils_test.js

@@ -0,0 +1,102 @@
+"use strict";
+
+var assert = require("assert"),
+	utils = require("./utils");
+
+// Mocha one-liner to make these tests self-hosted
+if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
+
+exports.utils = {
+
+	".constify()": {
+
+		"simple": function() {
+			var original = {
+					a: 1,
+					b: "s"
+				},
+				expected = {
+					a: {
+						$const: 1
+					},
+					b: {
+						$const: "s"
+					}
+				};
+			assert.deepEqual(utils.constify(original), expected);
+		},
+
+		"array": function() {
+			var original = {
+					a: ["s"]
+				},
+				expected = {
+					a: [
+						{
+							$const: "s"
+						}
+					]
+				};
+			assert.deepEqual(utils.constify(original), expected);
+		},
+
+		"array2": function() {
+			var original = {
+					a: [
+						"s",
+						[5],
+						{
+							a: 5
+						}
+					]
+				},
+				expected = {
+					a: [{
+							$const: "s"
+					},
+						{
+							$const: [5]
+					},
+						{
+							a: {
+								$const: 5
+							}
+					}]
+				};
+			assert.deepEqual(utils.constify(original), expected);
+		},
+
+		"object": function() {
+			var original = {
+					a: {
+						b: {
+							c: 5
+						},
+						d: "hi"
+					}
+				},
+				expected = {
+					a: {
+						b: {
+							c: {
+								"$const": 5
+							}
+						},
+						d: {
+							"$const": "hi"
+						}
+					}
+				};
+			assert.deepEqual(utils.constify(original), expected);
+		},
+
+		"fieldPathExpression": function() {
+			var original = {
+				a: "$field.path"
+			};
+			assert.deepEqual(utils.constify(original), original);
+		},
+
+	},
+
+};