Переглянути джерело

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

Conflicts:
	lib/pipeline/Document.js
Kyle P Davis 11 роки тому
батько
коміт
ed77b19493
51 змінених файлів з 1077 додано та 585 видалено
  1. 90 45
      lib/pipeline/Document.js
  2. 3 3
      lib/pipeline/expressions/AddExpression.js
  3. 3 4
      lib/pipeline/expressions/AllElementsTrueExpression.js
  4. 2 2
      lib/pipeline/expressions/AndExpression.js
  5. 10 3
      lib/pipeline/expressions/AnyElementTrueExpression.js
  6. 2 2
      lib/pipeline/expressions/CompareExpression.js
  7. 2 2
      lib/pipeline/expressions/ConcatExpression.js
  8. 7 8
      lib/pipeline/expressions/CondExpression.js
  9. 30 34
      lib/pipeline/expressions/ConstantExpression.js
  10. 9 9
      lib/pipeline/expressions/DayOfMonthExpression.js
  11. 9 3
      lib/pipeline/expressions/DayOfWeekExpression.js
  12. 9 3
      lib/pipeline/expressions/DayOfYearExpression.js
  13. 9 3
      lib/pipeline/expressions/DivideExpression.js
  14. 43 41
      lib/pipeline/expressions/Expression.js
  15. 32 0
      lib/pipeline/expressions/FixedArityExpressionT.js
  16. 9 3
      lib/pipeline/expressions/HourExpression.js
  17. 3 3
      lib/pipeline/expressions/IfNullExpression.js
  18. 3 3
      lib/pipeline/expressions/LetExpression.js
  19. 3 3
      lib/pipeline/expressions/MillisecondExpression.js
  20. 3 3
      lib/pipeline/expressions/MinuteExpression.js
  21. 3 3
      lib/pipeline/expressions/ModExpression.js
  22. 3 3
      lib/pipeline/expressions/MonthExpression.js
  23. 2 2
      lib/pipeline/expressions/MultiplyExpression.js
  24. 27 0
      lib/pipeline/expressions/NaryBaseExpressionT.js
  25. 107 89
      lib/pipeline/expressions/NaryExpression.js
  26. 3 3
      lib/pipeline/expressions/NotExpression.js
  27. 2 2
      lib/pipeline/expressions/OrExpression.js
  28. 3 3
      lib/pipeline/expressions/SecondExpression.js
  29. 3 3
      lib/pipeline/expressions/SetDifferenceExpression.js
  30. 2 9
      lib/pipeline/expressions/SetEqualsExpression.js
  31. 2 9
      lib/pipeline/expressions/SetIntersectionExpression.js
  32. 3 3
      lib/pipeline/expressions/SetIsSubsetExpression.js
  33. 2 9
      lib/pipeline/expressions/SetUnionExpression.js
  34. 3 3
      lib/pipeline/expressions/SizeExpression.js
  35. 3 3
      lib/pipeline/expressions/StrcasecmpExpression.js
  36. 3 3
      lib/pipeline/expressions/SubstrExpression.js
  37. 3 3
      lib/pipeline/expressions/SubtractExpression.js
  38. 6 6
      lib/pipeline/expressions/ToLowerExpression.js
  39. 2 2
      lib/pipeline/expressions/ToUpperExpression.js
  40. 1 1
      lib/pipeline/expressions/VariadicExpressionT.js
  41. 3 3
      lib/pipeline/expressions/WeekExpression.js
  42. 3 3
      lib/pipeline/expressions/YearExpression.js
  43. 1 1
      lib/pipeline/expressions/index.js
  44. 110 0
      test/lib/pipeline/Document.js
  45. 0 55
      test/lib/pipeline/expressions/ConstantExpression.js
  46. 107 0
      test/lib/pipeline/expressions/ConstantExpression_test.js
  47. 0 151
      test/lib/pipeline/expressions/NaryExpression.js
  48. 241 0
      test/lib/pipeline/expressions/NaryExpression_test.js
  49. 0 36
      test/lib/pipeline/expressions/VariadicExpressionT_test.js
  50. 46 0
      test/lib/pipeline/expressions/utils.js
  51. 102 0
      test/lib/pipeline/expressions/utils_test.js

+ 90 - 45
lib/pipeline/Document.js

@@ -11,10 +11,8 @@ var Document = module.exports = function Document(){
 	if(this.constructor == Document) throw new Error("Never create instances! Use static helpers only.");
 }, klass = Document, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-// DEPENDENCIES
 var Value = require("./Value");
 
-// STATIC MEMBERS
 /**
  * Shared "_id"
  * @static
@@ -22,18 +20,58 @@ var Value = require("./Value");
  **/
 klass.ID_PROPERTY_NAME = "_id";
 
+//SKIPPED: DocumentStorage
+
 /**
- * Compare two documents.
+ * Return JSON representation of this Document
+ * @method toJson
+ * @returns {Object} JSON representation of this Document
+ **/
+klass.toJson = function toJson(doc) {
+ 	return JSON.parse(JSON.stringify(doc));
+};
+
+//SKIPPED: metaFieldTextScore
+//SKIPPED: toBsonWithMetaData
+//SKIPPED: fromBsonWithMetaData
+
+//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
+
+/** Compare two documents.
  *
- * BSON document field order is significant, so this just goes through the fields in order.
- * The comparison is done in roughly the same way as strings are compared, but comparing one field at a time instead of one character at a time.
+ *  BSON document field order is significant, so this just goes through
+ *  the fields in order.  The comparison is done in roughly the same way
+ *  as strings are compared, but comparing one field at a time instead
+ *  of one character at a time.
+ *
+ *  Note: This does not consider metadata when comparing documents.
  *
- * @static
  * @method compare
- * @param rL left document
- * @param rR right document
- * @returns an integer less than zero, zero, or an integer greater than zero, depending on whether rL < rR, rL == rR, or rL > rR
- **/
+ * @static
+ * @param l {Object}  left document
+ * @param r {Object} right document
+ * @returns an integer less than zero, zero, or an integer greater than
+ *           zero, depending on whether lhs < rhs, lhs == rhs, or lhs > rhs
+ *  Warning: may return values other than -1, 0, or 1
+ */
 klass.compare = function compare(l, r){	//TODO: might be able to replace this with a straight compare of docs using JSON.stringify()
 	var lPropNames = Object.getOwnPropertyNames(l),
 		lPropNamesLength = lPropNames.length,
@@ -48,58 +86,65 @@ klass.compare = function compare(l, r){	//TODO: might be able to replace this wi
 
 		if (i >= rPropNamesLength) return 1; // right document is shorter
 
-		var nameCmp = Value.compare(lPropNames[i], rPropNames[i]);
+		var rField = rPropNames[i],
+			lField = lPropNames[i];
+		var nameCmp = Value.compare(lField, rField);
 		if (nameCmp !== 0) return nameCmp; // field names are unequal
 
-		var valueCmp = Value.compare(l[lPropNames[i]], r[rPropNames[i]]);
+		var valueCmp = Value.compare(l[lPropNames[i]], r[rField]);
 		if (valueCmp) return valueCmp; // fields are unequal
 	}
+};
+
+//SKIPPED: toString
 
-	/* NOTREACHED */
-	throw new Error("This should never happen");	//verify(false)
-//		return 0;
+klass.serializeForSorter = function serializeForSorter(doc) {
+	//NOTE: DEVIATION FROM MONGO: they take a buffer to output the current instance into, ours is static and takes a doc and returns the serialized output
+	return JSON.stringify(doc);
 };
 
+klass.deserializeForSorter = function deserializeForSorter(docStr, sorterDeserializeSettings) {
+	JSON.parse(docStr);
+};
+
+//SKIPPED: swap
+//SKIPPED: []
+//SKIPPED: getField -- inline as:  obj[key]
+//SKIPPED: getNestedField -- use fieldPath? might need to implement this...
+//SKIPPED: size -- need this? Number of fields in this document. O(n) -- recursive
+klass.empty = function(obj) {
+	return Object.keys(obj).length === 0;
+};
+//SKIPPED: operator <<
+//SKIPPED: positionOf
+
 /**
  * Clone a document
  * @static
  * @method clone
- * @param document
- **/
-klass.clone = function(document){
+ * @param doc
+ */
+klass.clone = function clone(doc) {
 	var obj = {};
-	for(var key in document){
-		if(document.hasOwnProperty(key)){
-			var withObjVal = document[key];
-			if(withObjVal === null) { // necessary to handle null values without failing
-				obj[key] = withObjVal;
-			}
-			else if(withObjVal.constructor === Object){
-				obj[key] = Document.clone(withObjVal);
-			}else{
-				obj[key] = withObjVal;
+	for (var key in doc) {
+		if (doc.hasOwnProperty(key)) {
+			var val = doc[key];
+			if (val === undefined || val === null) { // necessary to handle null values without failing
+				obj[key] = val;
+			} else if (val instanceof Object && val.constructor === Object) {
+				obj[key] = Document.clone(val);
+			} else {
+				obj[key] = val;
 			}
 		}
 	}
 	return obj;
 };
 
-//	proto.addField = function addField(){ throw new Error("Instead of `Document#addField(key,val)` you should just use `obj[key] = val`"); }
-//	proto.setField = function addField(){ throw new Error("Instead of `Document#setField(key,val)` you should just use `obj[key] = val`"); }
-//  proto.getField = function getField(){ throw new Error("Instead of `Document#getField(key)` you should just use `var val = obj[key];`"); }
+//SKIPPED: hasTextScore
+//SKIPPED: getTextScore
 
-// NOTE: from MutableDocument, implemented a bit differently here but should be same basic functionality
-klass.getNestedField = function getNestedField(obj, path) {
-	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;
-};
+//SKIPPED: memUsageForSorter -- not implementing mem usage right now
+//SKIPPED: getOwned -- not implementing mem usage right now
 
-klass.setNestedField = function setNestedField(obj, path, val) {
-	var paths = Array.isArray(path) ? path : path.split("."),
-		key = paths.pop(),
-		parent = klass.getNestedField(obj, paths);
-	if (parent) parent[key] = val;
-};
+//SKIPPED: getPtr

+ 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"),

+ 30 - 34
lib/pipeline/expressions/ConstantExpression.js

@@ -1,64 +1,60 @@
 "use strict";
 
+var Value = require("../Value"),
+    Expression = require("./Expression");
+
 /**
  * Internal expression for constant values
  * @class ConstantExpression
  * @namespace mungedb-aggregate.pipeline.expressions
  * @module mungedb-aggregate
  * @constructor
- **/
+ */
 var ConstantExpression = module.exports = function ConstantExpression(value){
-    if (arguments.length !== 1) throw new Error("args expected: value");
-    this.value = value; //TODO: actually make read-only in terms of JS?
+    if (arguments.length !== 1) throw new Error(klass.name + ": args expected: value");
+    this.value = value;
     base.call(this);
-}, klass = ConstantExpression, base = require("./Expression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
-
-
-// DEPENDENCIES
-var Value = require("../Value"),
-    Expression = require("./Expression");
+}, klass = ConstantExpression, base = require("./FixedArityExpressionT")(klass,1), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-// PROTOTYPE MEMBERS
-proto.getOpName = function getOpName(){
-	return "$const";
+klass.parse = function parse(exprElement, vps) {
+	return new ConstantExpression(exprElement);
 };
 
-/**
- * Get the constant value represented by this Expression.
- * @method getValue
- * @returns the value
- **/
-proto.getValue = function getValue(){   //TODO: convert this to an instance field rather than a property
-    return this.value;
+klass.create = function create(value) {
+	var constExpr = new ConstantExpression(value);
+	return constExpr;
 };
 
-proto.addDependencies = function addDependencies(deps, path) {
+proto.optimize = function optimize() {
 	// nothing to do
+	return this;
 };
 
-klass.parse = function parse(expr, vps){
-    return new ConstantExpression(expr);
+proto.addDependencies = function addDependencies(deps, path) {
+	// nothing to do
 };
 
 /**
  * Get the constant value represented by this Expression.
  * @method evaluate
- **/
-proto.evaluateInternal = function evaluateInternal(vars){
+ */
+proto.evaluateInternal = function evaluateInternal(vars) {
 	return this.value;
 };
 
-proto.optimize = function optimize() {
-	return this; // nothing to do
-};
+/// Helper function to easily wrap constants with $const.
+function serializeConstant(val) {
+    return {$const: val};
+}
 
-proto.serialize = function(rawValue){
-	return rawValue ? {$const: this.value} : this.value;
+proto.serialize = function serialize(explain) {
+	return serializeConstant(this.value);
 };
 
-//TODO: proto.addToBsonObj   --- may be required for $project to work -- my hope is that we can implement toJSON methods all around and use that instead
-//TODO: proto.addToBsonArray
+Expression.registerExpression("$const", klass.parse);
+
+Expression.registerExpression("$literal", klass.parse); // alias
 
-/** Register Expression */
-Expression.registerExpression("$const",klass.parse(ConstantExpression));
-Expression.registerExpression("$literal", klass.parse(ConstantExpression)); // alias
+proto.getOpName = function getOpName() {
+	return "$const";
+};

+ 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");

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

@@ -0,0 +1,32 @@
+"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.");
+		}
+	};
+	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

@@ -9,11 +9,11 @@
  * @constructor
  **/
 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
@@ -85,4 +85,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);

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

@@ -13,7 +13,7 @@ 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}});
 
 	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"),

+ 110 - 0
test/lib/pipeline/Document.js

@@ -0,0 +1,110 @@
+"use strict";
+var assert = require("assert"),
+	Document = require("../../../lib/pipeline/Document");
+
+// 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.Document = {
+
+	"Json conversion": {
+
+		"convert to Json": function toJson() {
+			var aDocument = {"prop1":0},
+				result = Document.toJson(aDocument);
+			assert.equal(result, '{"prop1":0}');
+		},
+
+		"convert to Json with metadata": function toJsonWithMetaData() {
+			var aDocument = {"prop1": 0,"metadata":"stuff"},
+				result = Document.toJsonWithMetaData(aDocument);
+			assert.equal(result, '{"prop1":0,"metadata":"stuff"}');
+		},
+
+		"convert from Json": function fromJsonWithMetaData() {
+			var aDocumentString = '{\"prop1\":0,\"metadata\":1}',
+				jsonDocument = {"prop1":0,"metadata":1},
+				result = Document.fromJsonWithMetaData(aDocumentString);
+			assert.deepEqual(result, jsonDocument);
+		},
+
+	},
+
+	"compare 2 Documents": {
+
+		"should return 0 if Documents are identical": function compareDocumentsIdentical() {
+			var lDocument = {"prop1": 0},
+				rDocument = {"prop1": 0},
+				result = Document.compare(lDocument, rDocument);
+			assert.equal(result, 0);
+		},
+
+		"should return -1 if left Document is shorter": function compareLeftDocumentShorter() {
+			var lDocument = {"prop1": 0},
+				rDocument = {"prop1": 0, "prop2": 0},
+				result = Document.compare(lDocument, rDocument);
+			assert.equal(result, -1);
+		},
+
+		"should return 1 if right Document is shorter": function compareRightDocumentShorter() {
+			var lDocument = {"prop1": 0, "prop2": 0},
+				rDocument = {"prop1": 0},
+				result = Document.compare(lDocument, rDocument);
+			assert.equal(result, 1);
+		},
+
+		"should return nameCmp result -1 if left Document field value is less": function compareLeftDocumentFieldLess() {
+			var lDocument = {"prop1": 0},
+				rDocument = {"prop1": 1},
+				result = Document.compare(lDocument, rDocument);
+			assert.equal(result, -1);
+		},
+
+		"should return nameCmp result 1 if right Document field value is less": function compareRightDocumentFieldLess() {
+			var lDocument = {"prop1": 1},
+				rDocument = {"prop1": 0},
+				result = Document.compare(lDocument, rDocument);
+			assert.equal(result, 1);
+		},
+
+	},
+
+	"clone a Document": {
+
+		"should return same field and value from cloned Document ": function clonedDocumentSingleFieldValue() {
+			var doc = {"prop1": 17},
+				res = Document.clone(doc);
+			assert(res instanceof Object);
+			assert.deepEqual(doc, res);
+			assert.equal(res.prop1, 17);
+		},
+
+		"should return same fields and values from cloned Document ": function clonedDocumentMultiFieldValue() {
+			var doc = {"prop1": 17, "prop2": "a string"},
+				res = Document.clone(doc);
+			assert.deepEqual(doc, res);
+			assert(res instanceof Object);
+			assert.equal(res.prop1, 17);
+			assert.equal(res.prop2, "a string");
+		},
+
+	},
+
+	"serialize and deserialize for sorter": {
+
+		"should return a string": function serializeDocument() {
+			var doc = {"prop1":1},
+				res = Document.serializeForSorter(doc);
+			assert.equal(res, "{\"prop1\":1}");
+		},
+
+		"should return a Document": function deserializeToDocument() {
+			var str = "{\"prop1\":1}",
+				doc = {"prop1":1},
+				res = Document.deserializeForSorter(str);
+			assert.deepEqual(res, doc);
+		},
+
+	},
+
+};

+ 0 - 55
test/lib/pipeline/expressions/ConstantExpression.js

@@ -1,55 +0,0 @@
-"use strict";
-var assert = require("assert"),
-	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
-
-
-module.exports = {
-
-	"ConstantExpression": {
-
-		"constructor() / #evaluate": {
-
-			"should be able to construct from a value type": function testCreate(){
-				assert.strictEqual(new ConstantExpression(5).evaluateInternal({}), 5);
-			}
-
-			//TODO: CreateFromBsonElement ? ?? ???
-
-		},
-
-// TODO: the constructor() tests this so not really needed here
-//		"#evaluate()": {
-//		},
-
-		"#optimize()": {
-
-			"should not optimize anything": function testOptimize(){
-				var expr = new ConstantExpression(5);
-				assert.strictEqual(expr, expr.optimize());
-			}
-
-		},
-
-		"#addDependencies()": {
-
-			"should return nothing": function testDependencies(){
-				assert.strictEqual(new ConstantExpression(5).addDependencies(), undefined);
-			}
-
-		},
-
-		"#toJSON()": {
-
-			"should output proper JSON": function testJson(){
-				var expr = new ConstantExpression(5);
-				assert.strictEqual(expr.serialize(), 5);
-				assert.deepEqual(expr.serialize(true), {$const:5});
-			}
-
-		}
-
-	}
-
-};
-
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);

+ 107 - 0
test/lib/pipeline/expressions/ConstantExpression_test.js

@@ -0,0 +1,107 @@
+"use strict";
+var assert = require("assert"),
+	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
+	VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
+	VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState");
+
+// 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.ConstantExpression = {
+
+	".constructor()": {
+
+		"should accept one argument": function() {
+			new ConstantExpression(5);
+		},
+
+		"should not accept 0 arguments": function() {
+			assert.throws(function() {
+				 new ConstantExpression();
+			});
+		},
+
+		"should not accept 2 arguments": function() {
+			assert.throws(function() {
+				new ConstantExpression(1, 2);
+			});
+		},
+
+	},
+
+	".parse()": {
+
+		"should create an expression from a json element": function testCreateFromBsonElement() {
+			var idGenerator = new VariablesIdGenerator(),
+				vps = new VariablesParseState(idGenerator),
+				expression = ConstantExpression.parse("foo", vps);
+			assert.deepEqual("foo", expression.evaluate({}));
+		},
+
+	},
+
+	".create()": {
+
+		"should create an expression": function testCreate() {
+			assert(ConstantExpression.create() instanceof ConstantExpression);
+		},
+
+		//SKIPPED: testCreateFronBsonElement
+
+	},
+
+	"#optimize()": {
+
+		"should not optimize anything": function testOptimize() {
+			var expr = new ConstantExpression(5);
+			assert.strictEqual(expr, expr.optimize());
+		},
+
+	},
+
+	"#addDependencies()": {
+
+		"should return nothing": function testDependencies() {
+			var expr = ConstantExpression.create(5),
+				deps = {}; //TODO: new DepsTracker
+			expr.addDependencies(deps);
+			assert.strictEqual(deps.fields.length, 0);
+			assert.strictEqual(deps.needWholeDocument, false);
+			assert.strictEqual(deps.needTextScore, false);
+		},
+
+	},
+
+	//TODO: AddToBsonObj
+
+	//TODO: AddToBsonArray
+
+	"#evaluate()": {
+
+		"should do what comes natural with an int": function() {
+			var c = 567;
+			var expr = new ConstantExpression(c);
+			assert.deepEqual(expr.evaluate(), c);
+		},
+
+		"should do what comes natural with a float": function() {
+			var c = 567.123;
+			var expr = new ConstantExpression(c);
+			assert.deepEqual(expr.evaluate(), c);
+		},
+
+		"should do what comes natural with a String": function() {
+			var c = "Quoth the raven";
+			var expr = new ConstantExpression(c);
+			assert.deepEqual(expr.evaluate(), c);
+		},
+
+		"should do what comes natural with a date": function() {
+			var c = new Date();
+			var expr = new ConstantExpression(c);
+			assert.deepEqual(expr.evaluate(), c);
+		},
+
+	},
+
+};

+ 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);
+		},
+
+	},
+
+};