Przeglądaj źródła

Refs #5272. Updated the push accumulator and tests to mongo 2.5.4

Spencer Rathbun 11 lat temu
rodzic
commit
1f0acdd586

+ 27 - 8
lib/pipeline/accumulators/PushAccumulator.js

@@ -1,6 +1,6 @@
 "use strict";
 
-/** 
+/**
  * Constructor for PushAccumulator. Pushes items onto an array.
  * @class PushAccumulator
  * @namespace mungedb-aggregate.pipeline.accumulators
@@ -12,17 +12,36 @@ var PushAccumulator = module.exports = function PushAccumulator(){
 	base.call(this);
 }, klass = PushAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-proto.evaluate = function evaluate(doc){
-	if (this.operands.length != 1) throw new Error("this should never happen");
-	var v = this.operands[0].evaluate(doc);
-	if (v !== undefined) this.values.push(v);
-	return null;
+proto.getValue = function getValue(toBeMerged){
+	return this.values;
 };
 
-proto.getValue = function getValue(){
-	return this.values;
+proto.getFactory = function getFactory(){
+	return klass;	// using the ctor rather than a separate .create() method
 };
 
 proto.getOpName = function getOpName(){
 	return "$push";
 };
+
+proto.processInternal = function processInternal(input, merging) {
+	if (!merging) {
+		if (input !== undefined) {
+			this.values.push(input);
+			//_memUsageBytes += input.getApproximateSize();
+		}
+	}
+	else {
+		// If we're merging, we need to take apart the arrays we
+		// receive and put their elements into the array we are collecting.
+		// If we didn't, then we'd get an array of arrays, with one array
+		// from each merge source.
+		if (typeof input !== Array) throw new Error("input is not an Array during merge in PushAccumulator:35");
+
+		this.values = this.values.concat(input);
+
+		//for (size_t i=0; i < vec.size(); i++) {
+			//_memUsageBytes += vec[i].getApproximateSize();
+		//}
+	}
+};

+ 26 - 22
test/lib/pipeline/accumulators/PushAccumulator.js

@@ -1,16 +1,12 @@
 "use strict";
 var assert = require("assert"),
-	PushAccumulator = require("../../../../lib/pipeline/accumulators/PushAccumulator"),
-	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
+	PushAccumulator = require("../../../../lib/pipeline/accumulators/PushAccumulator");
 
 
 function createAccumulator(){
-	var accumulator = new PushAccumulator();
-	accumulator.addOperand(new FieldPathExpression("b") );
-	return accumulator;
+	return new PushAccumulator();
 }
 
-
 module.exports = {
 
 	"PushAccumulator": {
@@ -33,44 +29,52 @@ module.exports = {
 
 		},
 
-		"#evaluate()": {
+		"#getFactory()": {
+
+			"should return the constructor for this class": function factoryIsConstructor(){
+				assert.strictEqual(new PushAccumulator().getFactory(), PushAccumulator);
+			}
+
+		},
+
+		"#processInternal()": {
 
-			"should evaluate no documents and return []": function testEvaluate_None(){
+			"should processInternal no documents and return []": function testprocessInternal_None(){
 				var accumulator = createAccumulator();
 				assert.deepEqual(accumulator.getValue(), []);
 			},
 
-			"should evaluate a 1 and return [1]": function testEvaluate_One(){
+			"should processInternal a 1 and return [1]": function testprocessInternal_One(){
 				var accumulator = createAccumulator();
-				accumulator.evaluate({b:1});
+				accumulator.processInternal(1);
 				assert.deepEqual(accumulator.getValue(), [1]);
 			},
 
-			"should evaluate a 1 and a 2 and return [1,2]": function testEvaluate_OneTwo(){
+			"should processInternal a 1 and a 2 and return [1,2]": function testprocessInternal_OneTwo(){
 				var accumulator = createAccumulator();
-				accumulator.evaluate({b:1});
-				accumulator.evaluate({b:2});
+				accumulator.processInternal(1);
+				accumulator.processInternal(2);
 				assert.deepEqual(accumulator.getValue(), [1,2]);
 			},
 
-			"should evaluate a 1 and a null and return [1,null]": function testEvaluate_OneNull(){
+			"should processInternal a 1 and a null and return [1,null]": function testprocessInternal_OneNull(){
 				var accumulator = createAccumulator();
-				accumulator.evaluate({b:1});
-				accumulator.evaluate({b:null});
+				accumulator.processInternal(1);
+				accumulator.processInternal(null);
 				assert.deepEqual(accumulator.getValue(), [1, null]);
 			},
 
-			"should evaluate a 1 and an undefined and return [1]": function testEvaluate_OneUndefined(){
+			"should processInternal a 1 and an undefined and return [1]": function testprocessInternal_OneUndefined(){
 				var accumulator = createAccumulator();
-				accumulator.evaluate({b:1});
-				accumulator.evaluate({b:undefined});
+				accumulator.processInternal(1);
+				accumulator.processInternal(undefined);
 				assert.deepEqual(accumulator.getValue(), [1]);
 			},
 
-			"should evaluate a 1 and a 0 and return [1,0]": function testEvaluate_OneZero(){
+			"should processInternal a 1 and a 0 and return [1,0]": function testprocessInternal_OneZero(){
 				var accumulator = createAccumulator();
-				accumulator.evaluate({b:1});
-				accumulator.evaluate({b:0});
+				accumulator.processInternal(1);
+				accumulator.processInternal(0);
 				assert.deepEqual(accumulator.getValue(), [1, 0]);
 			}