Browse Source

Refs #5121: Fix depsToProjection

Chris Sexton 12 years ago
parent
commit
3b2641384d

+ 18 - 38
lib/pipeline/documentSources/DocumentSource.js

@@ -176,23 +176,35 @@ proto.getDependencies = function getDependencies(deps) {
  * @returns	{Object}	JSONObj
  **/
 klass.depsToProjection = function depsToProjection(deps) {
-	var bb = {};
+	var needId = false,
+		bb = {};
 	if (deps._id === undefined)
 		bb._id = 0;
 
 	var last = "";
 	Object.keys(deps).sort().forEach(function(it){
-		if (last !== "" && it.slice(0, last.length) === last){
-			// we are including a parent of *it so we don't need to
-			// include this field explicitly. In fact, due to
-			// SERVER-6527 if we included this field, the parent
-			// wouldn't be fully included.
+		if (it.indexOf('_id') === 0 && (it.length === 3 || it[3] === '.')) {
+			needId = true;
 			return;
+		} else {
+			if (last !== "" && it.slice(0, last.length) === last){
+				// we are including a parent of *it so we don't need to
+				// include this field explicitly. In fact, due to
+				// SERVER-6527 if we included this field, the parent
+				// wouldn't be fully included.
+				return;
+			}
 		}
 		last = it + ".";
 		bb[it] = 1;
 	});
 
+	if (needId) // we are explicit either way
+		bb._id = 1;
+	else
+		bb._id = 0;
+
+
 	return bb;
 };
 
@@ -207,38 +219,6 @@ proto.serializeToArray = function serializeToArray(array, explain) {
 	}
 };
 
-proto.depsToProjection = function depsToProjection(deps) {
-	var needId = false,
-		last,
-		bb = {};
-
-	for (var i = 0; i < deps.length; i++) {
-		var it = deps[i];
-		if (it.starsWith('_id') && (it.length === 3 || it[3] === '.')) {
-			needId = true;
-			continue;
-		} else {
-			if (!last && it.starsWith(last)) {
-                // we are including a parent of *it so we don't need to include this field
-                // explicitly. In fact, due to SERVER-6527 if we included this field, the parent
-                // wouldn't be fully included.  This logic relies on on set iterators going in
-                // lexicographic order so that a string is always directly before of all fields it
-                // prefixes.
-				continue;
-			}
-			last = it + '.';
-			bb[it] = 1;
-		}
-	}
-
-	if (needId) // we are explicit either way
-		bb._id = 1;
-	else
-		bb._id = 0;
-
-	return JSON.stringy(bb);
-};
-
 // Taken as a whole, these three functions should produce the same output document given the
 // same deps set as mongo::Projection::transform would on the output of depsToProjection. The
 // only exceptions are that we correctly handle the case where no fields are needed and we don't

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

@@ -0,0 +1,25 @@
+"use strict";
+var assert = require("assert"),
+	DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource");
+
+
+module.exports = {
+
+	"DocumentSource": {
+
+		"depsToProjection": {
+			"should be able to convert dependencies to a projection": function(){
+				var array = {'a':1,'b':1},
+					expected = '{"_id":0,"a":1,"b":1}',
+					proj = DocumentSource.depsToProjection(array);
+
+				assert.equal(expected, JSON.stringify(proj));
+			},
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();
+