Browse Source

EAGLESIX-3083: Fixed bugs

David Aebersold 11 năm trước cách đây
mục cha
commit
409ac1e93a

+ 39 - 23
lib/pipeline/documentSources/SortDocumentSource.js

@@ -327,11 +327,11 @@ proto.extractKey = function extractKey(d){
 /**
  * Compare two documents according to the specified sort key.
  *
- * @param {Object} pL the left side doc
- * @param {Object} pR the right side doc
+ * @param {Object} lhs the left side doc
+ * @param {Object} rhs the right side doc
  * @returns {Number} a number less than, equal to, or greater than zero, indicating pL < pR, pL == pR, or pL > pR, respectively
 **/
-proto.compare = function compare(pL,pR) {
+proto.compare = function compare(lhs,rhs) {
 	/*
 	  populate() already checked that there is a non-empty sort key,
 	  so we shouldn't have to worry about that here.
@@ -341,18 +341,17 @@ proto.compare = function compare(pL,pR) {
 	var n = this.vSortKey.length;
 	if ( n == 1) { // simple fast case
 		if ( this.vAscending[0] ) {
-			return Value.compare(pL, pR);
+			return Value.compare(lhs, rhs);
 		} else {
-			return -Value.compare(pL, pR);
+			return -Value.compare(lhs, rhs);
 		}
 	}
 
 	// compound sort
 	for(var i = 0; i < n; ++i) {
 		/* evaluate the sort keys */
-		var fieldPathVar;
-		var pathExpr = new FieldPathExpression(this.vSortKey[i].getFieldPath(false), fieldPathVar);
-		var left = pathExpr.evaluate(pL), right = pathExpr.evaluate(pR);
+		var pathExpr = FieldPathExpression.create(this.vSortKey[i].getFieldPath(false));
+		var left = pathExpr.evaluate(lhs), right = pathExpr.evaluate(rhs);
 
 		/*
 		Compare the two values; if they differ, return.  If they are
@@ -397,22 +396,36 @@ proto.serializeSortKey = function serializeSortKey(explain) {
 	return keyObj;
 };
 
+/**
+ * Creates a new SortDocumentSource from Json
+ *
+ * @param {Object} elem
+ * @param {Object} expCtx
+ *
+**/
+klass.createFromJson = function createFromJson(elem, expCtx) {
+	if (typeof elem !== "object") throw new Error("code 15973; the " + klass.sortName + " key specification must be an object");
+
+	
+	return this.create(expCtx, elem);
+};
+
 /**
  * Creates a new SortDocumentSource
  *
- * @param {Object} jsonElement
- * @ctx {Object} context
+ * @param {Object} expCtx
+ * @param {object} sortorder 
+ * @param {int} limit
  *
 **/
-klass.createFromJson = function createFromJson(jsonElement, ctx) {
-	if (typeof jsonElement !== "object") throw new Error("code 15973; the " + klass.sortName + " key specification must be an object");
+klass.create = function create(expCtx, sortOrder, limit) {
 
 	var Sort = proto.getFactory(),
-		nextSort = new Sort(ctx);
+		nextSort = new Sort(expCtx);
 
 	/* check for then iterate over the sort object */
 	var sortKeys = 0;
-	for(var keyField in jsonElement) {
+	for(var keyField in sortOrder) {
 		var fieldName = keyField.fieldName;
 
 		if ( fieldName === "$mergePresorted" ){
@@ -429,22 +442,25 @@ klass.createFromJson = function createFromJson(jsonElement, ctx) {
 			continue;
 		}
 
-		if (typeof jsonElement[keyField] !== "number") throw new Error("code 15974; " + klass.sortName + "$sort key ordering must be specified using a number or {$meta: 'text'}");
+		if (typeof sortOrder[keyField] !== "number") throw new Error("code 15974; " + klass.sortName + "$sort key ordering must be specified using a number or {$meta: 'text'}");
 
-		var sortOrder = 0;
-		sortOrder = jsonElement[keyField];
-		if ((sortOrder != 1) && (sortOrder !== -1)) throw new Error("code 15975; " + klass.sortName + " $sort key ordering must be 1 (for ascending) or -1 (for descending)");
+		// RedBeard0531 can the thanked.
+		var sortDirection = 0;
+		sortDirection = sortOrder[keyField];
+		debugger;
+		if ((sortDirection != 1) && (sortDirection !== -1)) throw new Error("code 15975; " + klass.sortName + " $sort key ordering must be 1 (for ascending) or -1 (for descending)");
 
-		nextSort.addKey(keyField, (sortOrder > 0));
+		nextSort.addKey(keyField, (sortDirection > 0));
 		++sortKeys;
 	}
 
 	if (sortKeys <= 0) throw new Error("code 15976; " + klass.sortName + " must have at least one sort key");
+
 	
-	// if ( limit > 0) {
-	// 	var coalesced = nextSort.coalesce( create(ctx, limit));
-	// 	// should always coalesce
-	// }
+	if ( limit > 0) {
+		var coalesced = nextSort.coalesce( create(expCtx, limit));
+		// should always coalesce
+	}
 
 	return nextSort;
 };

+ 47 - 46
test/lib/pipeline/documentSources/SortDocumentSource.js

@@ -13,32 +13,32 @@ module.exports = {
 
 	"SortDocumentSource": {
 
-		"constructor()": {
+		// "constructor()": {
 
-			"should not throw Error when constructing without args": function testConstructor(){
-				assert.doesNotThrow(function(){
-					new SortDocumentSource();
-				});
-			}
+		// 	"should not throw Error when constructing without args": function testConstructor(){
+		// 		assert.doesNotThrow(function(){
+		// 			new SortDocumentSource();
+		// 		});
+		// 	}
 
-		},
+		// },
 
-		"#getSourceName()": {
+		// "#getSourceName()": {
 
-			"should return the correct source name; $sort": function testSourceName(){
-				var sds = new SortDocumentSource();
-				assert.strictEqual(sds.getSourceName(), "$sort");
-			}
+		// 	"should return the correct source name; $sort": function testSourceName(){
+		// 		var sds = new SortDocumentSource();
+		// 		assert.strictEqual(sds.getSourceName(), "$sort");
+		// 	}
 
-		},
+		// },
 
-		"#getFactory()": {
+		// "#getFactory()": {
 
-			"should return the constructor for this class": function factoryIsConstructor(){
-				assert.strictEqual(new SortDocumentSource().getFactory(), SortDocumentSource);
-			}
+		// 	"should return the constructor for this class": function factoryIsConstructor(){
+		// 		assert.strictEqual(new SortDocumentSource().getFactory(), SortDocumentSource);
+		// 	}
 
-		},
+		// },
 
 		"#getNext()": {
 			/** Assert that iterator state accessors consistently report the source is exhausted. */
@@ -125,11 +125,11 @@ module.exports = {
             * created with.
             */
             "should have equal json representation": function serializeToArrayCheck(){
-				var spec = {"sort":1};
-				var sds = new SortDocumentSource.createFromJson(spec, {});
+				var sds = SortDocumentSource.createFromJson({"sort":1}, {});
 
 				var array = [];
 				sds.serializeToArray(array, false);
+
 				assert.deepEqual(array, [ { '$sort': { '[object Object]': 1 } } ]);
 			},
 
@@ -258,33 +258,34 @@ module.exports = {
 				);
 			},
 
-			// "should sort documents with a compound key": function compoundKeySort(next) {
-			// 	var cwc = new CursorDocumentSource.CursorWithContext();
-			// 	var l = [{_id:0, a: 1, b:3}, {_id:5, a:12, b:7}, {_id:1, a:0, b:2}];
-			// 	cwc._cursor = new Cursor( l );
-			// 	var cds = new CursorDocumentSource(cwc);
-			// 	var sds = new SortDocumentSource();
-			// 	sds.addKey("a", false);
-			// 	sds.addKey("b", false);
-			// 	sds.setSource(cds);
+			"should sort documents with a compound key": function compoundKeySort(next) {
+				var cwc = new CursorDocumentSource.CursorWithContext();
+				var l = [{_id:0, a: 1, b:3}, {_id:5, a:12, b:7}, {_id:1, a:0, b:2}];
+				cwc._cursor = new Cursor( l );
+				var cds = new CursorDocumentSource(cwc);
+				var sds = SortDocumentSource.createFromJson({"sort":1});
 
-			// 	var docs = [], i = 0;
-			// 	async.doWhilst(
-			// 		function(cb) {
-			// 			sds.getNext(function(err, val) {
-			// 				docs[i] = val;
-			// 				return cb(err);
-			// 			});
-			// 		},
-			// 		function() {
-			// 			return docs[i++] !== DocumentSource.EOF;
-			// 		},
-			// 		function(err) {
-			// 			assert.deepEqual([{_id:5, a:12, b:7}, {_id:0, a:1, b:3}, {_id:1, a:0, b:2}, DocumentSource.EOF], docs);
-			// 			next();
-			// 		}
-			// 	);
-			// },
+				sds.addKey("a", false);
+				sds.addKey("b", false);
+				sds.setSource(cds);
+
+				var docs = [], i = 0;
+				async.doWhilst(
+					function(cb) {
+						sds.getNext(function(err, val) {
+							docs[i] = val;
+							return cb(err);
+						});
+					},
+					function() {
+						return docs[i++] !== DocumentSource.EOF;
+					},
+					function(err) {
+						assert.deepEqual([{_id:5, a:12, b:7}, {_id:0, a:1, b:3}, {_id:1, a:0, b:2}, DocumentSource.EOF], docs);
+						next();
+					}
+				);
+			},
 
 		// 	"should sort documents with a compound key in ascending order": function compoundAscendingKeySort(next) {
 		// 		var cwc = new CursorDocumentSource.CursorWithContext();