瀏覽代碼

Added reset function to sort and project document sources. Fixed/Added top level munge tests. refs #934

http://source.rd.rcg.local/trac/eagle6/changeset/1356/Eagle6_SVN
Philip Murray 12 年之前
父節點
當前提交
25776998ac

+ 5 - 5
lib/pipeline/Pipeline.js

@@ -10,9 +10,9 @@ var Pipeline = module.exports = (function(){
 		MatchDocumentSource = require('./documentSources/MatchDocumentSource'),
 		ProjectDocumentSource = require('./documentSources/ProjectDocumentSource'),
 		SkipDocumentSource = require('./documentSources/SkipDocumentSource'),
-		UnwindDocumentSource = require('./documentSources/UnwindDocumentSource');
-//		GroupDocumentSource = require('./documentSources/GroupDocumentSource'),
-//		SortDocumentSource = require('./documentSources/SortDocumentSource');
+		UnwindDocumentSource = require('./documentSources/UnwindDocumentSource'),
+		GroupDocumentSource = require('./documentSources/GroupDocumentSource'),
+		SortDocumentSource = require('./documentSources/SortDocumentSource');
 	
 	klass.StageDesc = {};//attaching this to the class for test cases
 	klass.StageDesc[LimitDocumentSource.limitName] = LimitDocumentSource.createFromJson;
@@ -20,8 +20,8 @@ var Pipeline = module.exports = (function(){
 	klass.StageDesc[ProjectDocumentSource.projectName] = ProjectDocumentSource.createFromJson;
 	klass.StageDesc[SkipDocumentSource.skipName] = SkipDocumentSource.createFromJson;
 	klass.StageDesc[UnwindDocumentSource.unwindName] = UnwindDocumentSource.createFromJson;
-//	klass.StageDesc[GroupDocumentSource.groupName] = GroupDocumentSource.createFromJson;
-//	klass.StageDesc[SortDocumentSource.sortName] = SortDocumentSource.createFromJson;
+	klass.StageDesc[GroupDocumentSource.groupName] = GroupDocumentSource.createFromJson;
+	klass.StageDesc[SortDocumentSource.sortName] = SortDocumentSource.createFromJson;
 	
     /**
      * Create a pipeline from the command.

+ 10 - 1
lib/pipeline/Value.js

@@ -98,7 +98,16 @@ var Value = module.exports = Value = (function(){
 		// We know the left value isn't Undefined, because of the above. Count a NULL value as greater than an undefined one.
 		if (rt === "undefined" || r === null) return 1;
 		// Numbers
-		if (lt === "number" && rt === "number") return l < r ? -1 : l > r ? 1 : 0;
+		if (lt === "number" && rt === "number"){
+			//this is a deviation from the mongo code because they handle NaN differently
+			if (isNaN(l)){
+				return isNaN(r)?0:-1;
+			}
+			if (isNaN(r)){
+				return 1;
+			}
+			return l < r ? -1 : l > r ? 1 : 0;
+		}
 		// CW TODO for now, only compare like values
 		if (lt !== rt) throw new Error("can't compare values of BSON types [" + lt + " " + l.constructor.name + "] and [" + rt + ":" + r.constructor.name + "]; code 16016");
 		// Compare everything else

+ 10 - 0
lib/pipeline/documentSources/ProjectDocumentSource.js

@@ -136,6 +136,16 @@ var ProjectDocumentSource = module.exports = (function(){
         this.EO.addDependencies(deps, path);
         return base.GetDepsReturn.EXHAUSTIVE;
     };
+    
+    /**
+     * Reset the document source so that it is ready for a new stream of data.
+     * Note that this is a deviation from the mongo implementation.
+     * 
+     * @method	reset
+    **/
+	proto.reset = function reset(){
+		
+	};
 
 	return klass;
 })();

+ 4 - 1
lib/pipeline/documentSources/SortDocumentSource.js

@@ -249,7 +249,10 @@ var SortDocumentSource = module.exports = (function(){
 	 * @method	reset
 	**/
 	proto.reset = function reset(){
-		this.count = 0;
+		this.populated = false;
+		this.current = null;
+		this.docIterator = null; // a number tracking our position in the documents array
+		this.documents = []; // an array of documents
 	};
 
 	return klass;

+ 18 - 20
test/lib/munge.js

@@ -89,13 +89,17 @@ module.exports = {
 			assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
 			assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
 			assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
-		}
+		},
+
 
-/*
 		"should be able to use a $project operator": function(){
-			var i = [{_id:0, e:1}, {_id:1, e:0}, {_id:2, e:1}, {_id:3, e:0}, {_id:4, e:1}, {_id:5, e:0}],
-				p = [{$project:{e:1}}],
-				e = [{_id:0, e:1}, {_id:2, e:1}, {_id:4, e:1}],
+			var i = [{_id:0, e:1, f:23}, {_id:2, e:2, g:34}, {_id:4, e:3}],
+				p = [{$project:{
+						e:1, 
+						a:{$add:["$e", "$e"]}, 
+						b:{$cond:[{$eq:["$e", 2]}, "two", "not two"]}	
+					}}],
+				e = [{_id:0, e:1, b:"not two", a:2}, {_id:2, e:2, b:"two", a:4}, {_id:4, e:3, b:"not two", a:6}],
 				munger = munge(p),
 				a = munger(i);
 			assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
@@ -104,31 +108,25 @@ module.exports = {
 			assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
 		},
 
-//TODO: $project w/ expressions
-
-
 		"should be able to construct an instance with $sort operators properly (ascending)": function(){
 			var i = [
 						{_id:3.14159}, {_id:-273.15},
 						{_id:42}, {_id:11}, {_id:1},
-						{_id:false},{_id:true},
-						{_id:""}, {_id:"a"}, {_id:"A"}, {_id:"Z"}, {_id:"z"},
-						{_id:null}, {_id:NaN},
-						//TODO: test with Objects; e.g., {_id:{a:{b:1}},
-						{_id:new Date("2012-10-22T08:01:21.235Z")}, {_id:new Date("2012-10-15T15:48:55.181Z")}
+						{_id:null}, {_id:NaN}
 					],
 				p = [{$sort:{_id:1}}],
 				e = [
 						{_id:null}, {_id:NaN},
-						{_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42},
-						{_id:""}, {_id:"A"}, {_id:"Z"}, {_id:"a"}, {_id:"z"},
-						{_id:false}, {_id:true},
-						{_id:new Date("2012-10-15T15:48:55.181Z")}, {_id:new Date("2012-10-22T08:01:21.235Z")}
-					];
-			var a = munge(p, i);
+						{_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42}
+					],
+				munger = munge(p),
+				a = munger(i);
 			assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
+			//assert.deepEqual(a, e); //does not work with NaN
+			assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
+			assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
 		}
-*/
+
 	}
 
 };