Sfoglia il codice sorgente

Refs #3045: Update AddToSetAccumulator js bug fixes

Mike McCarty 11 anni fa
parent
commit
fd77329457

+ 23 - 10
lib/pipeline/accumulators/AddToSetAccumulator.js

@@ -9,28 +9,41 @@
 **/
 var AddToSetAccumulator = module.exports = function AddToSetAccumulator(/* ctx */){
 	if (arguments.length !== 0) throw new Error("zero args expected");
-	this.set = {};
+	this.set = [];
 	//this.itr = undefined; /* Shoudln't need an iterator for the set */
 	//this.ctx = undefined; /* Not using the context object currently as it is related to sharding */
 	base.call(this);
 }, klass = AddToSetAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-proto.processInternal = function processInternal(doc, merging) {
+//DEPENDENCIES
+var Value = require("../Value");
+
+proto.getFactory = function getFactory(){
+	return klass;	// using the ctor rather than a separate .create() method
+};
+
+proto.contains = function contains(value) {
 	var set = this.set;
-	Object.keys(doc).map(function(d) {
-		set[JSON.stringify(doc[d])] = doc[d];
-	});
+	for (var i = 0, l = set.length; i < l; ++i) {
+		if (Value.compare(set[i], value) === 0) {
+			return true;
+		}
+	}
+	return false;
+};
+
+proto.processInternal = function processInternal(input, merging) {
+	if (! this.contains(input)) {
+		this.set.push(input);
+	}
 };
 
 proto.getValue = function getValue(toBeMerged) {
-	var set = this.set;
-	return Object.keys(set).map(function(k) {
-		return set[k];
-	});
+	return this.set;
 };
 
 proto.reset = function reset() {
-    this.set = {};
+    this.set = [];
 };
 
 // PROTOTYPE MEMBERS

+ 18 - 20
test/lib/pipeline/accumulators/AddToSetAccumulator.js

@@ -1,15 +1,12 @@
 "use strict";
 var assert = require("assert"),
-	AddToSetAccumulator = require("../../../../lib/pipeline/accumulators/AddToSetAccumulator"),
-	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
-	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
+	AddToSetAccumulator = require("../../../../lib/pipeline/accumulators/AddToSetAccumulator");
 
 
 var createAccumulator = function createAccumulator() {
 	return new AddToSetAccumulator();
 };
 
-
 //TODO: refactor these test cases using Expression.parseOperand() or something because these could be a whole lot cleaner...
 module.exports = {
 
@@ -31,10 +28,18 @@ module.exports = {
 
 		},
 
+		"#getFactory()": {
+
+			"should return the constructor for this class": function factoryIsConstructor(){
+				assert.strictEqual(new AddToSetAccumulator().getFactory(), AddToSetAccumulator);
+			}
+
+		},
+
 		"#processInternal()" : {
 			"should add input to set": function testAddsToSet() {
 				var acc = createAccumulator();
-				acc.processInternal({b:5});
+				acc.processInternal(5);
 				var value = acc.getValue();
 				assert.deepEqual(JSON.stringify(value), JSON.stringify([5]));
 			}
@@ -52,39 +57,32 @@ module.exports = {
 
 			"should return array with one element that equals 5": function test5InSet() {
 				var acc = createAccumulator();
-				acc.processInternal({b:5});
-				acc.processInternal({b:5});
+				acc.processInternal(5);
+				console.log(acc.set);
+				acc.processInternal(5);
 				var value = acc.getValue();
 				assert.deepEqual(JSON.stringify(value), JSON.stringify([5]));
 			},
 
 			"should produce value that is an array of multiple elements": function testMultipleItems() {
 				var acc = createAccumulator();
-				acc.processInternal({b:5});
-				acc.processInternal({b:{key: "value"}});
+				acc.processInternal(5);
+				acc.processInternal({key: "value"});
 				var value = acc.getValue();
 				assert.deepEqual(JSON.stringify(value), JSON.stringify([5, {key: "value"}]));
 			},
 
 			"should return array with one element that is an object containing a key/value pair": function testKeyValue() {
 				var acc = createAccumulator();
-				acc.processInternal({b:{key: "value"}});
+				acc.processInternal({key: "value"});
 				var value = acc.getValue();
 				assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
 			},
 
-			"should not require defining values": function testKeyValue() {
-				var acc = createAccumulator();
-				acc.processInternal({b:{key: "value"}});
-				acc.processInternal({a:5});
-				var value = acc.getValue();
-				assert.deepEqual(JSON.stringify(value), JSON.stringify([5, {key: "value"}]));
-			},
-
 			"should coalesce different instances of equivalent objects": function testGetValue_() {
 				var acc = createAccumulator();
-				acc.processInternal({b:{key: "value"}});
-				acc.processInternal({b:{key: "value"}});
+				acc.processInternal({key: "value"});
+				acc.processInternal({key: "value"});
 				var value = acc.getValue();
 				assert.deepEqual(JSON.stringify(value), JSON.stringify([{key: "value"}]));
 			}