Просмотр исходного кода

Refs #5269. Updated FirstAccumulator to 2.5.4 version.

Spencer Rathbun 13 лет назад
Родитель
Сommit
0efcddff5a

+ 22 - 17
lib/pipeline/accumulators/FirstAccumulator.js

@@ -1,16 +1,18 @@
 "use strict";
 
-/** 
- * Constructor for FirstAccumulator, wraps SingleValueAccumulator's constructor and adds flag to track whether we have started or not
+/**
+ * Constructor for FirstAccumulator, wraps Accumulator and adds flag to track whether we have started or not
  * @class FirstAccumulator
  * @namespace mungedb-aggregate.pipeline.accumulators
  * @module mungedb-aggregate
  * @constructor
  **/
 var FirstAccumulator = module.exports = function FirstAccumulator(){
+	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
-	this.started = 0; //TODO: hack to get around falsy values making us keep going
-}, klass = FirstAccumulator, base = require("./SingleValueAccumulator"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+	this._haveFirst = false;
+	this._first = undefined;
+}, klass = FirstAccumulator, base = require("./Accumulator"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // PROTOTYPE MEMBERS
 proto.getOpName = function getOpName(){
@@ -21,19 +23,22 @@ proto.getFactory = function getFactory(){
 	return klass;	// using the ctor rather than a separate .create() method
 };
 
-/** 
- * Takes a document and returns the first value in the document
- * @param {Object} doc the document source
- * @return the first value
- **/
-proto.evaluate = function evaluate(doc){
-	if (this.operands.length != 1) throw new Error("this should never happen");
-
-	// only remember the first value seen
-	if (!base.prototype.getValue.call(this) && this.started === 0) {
-		this.value = this.operands[0].evaluate(doc);
-		this.started = 1;
+proto.processInternal = function processInternal(input, merging) {
+	/* only remember the first value seen */
+	if (!this._haveFirst) {
+		// can't use pValue.missing() since we want the first value even if missing
+		this._haveFirst = true;
+		this._first = input;
+		//this._memUsageBytes = sizeof(*this) + input.getApproximateSize() - sizeof(Value);
 	}
+};
+
+proto.getValue = function getValue(toBeMerged) {
+	return this._first;
+};
 
-	return this.value;
+proto.reset = function reset() {
+	this._haveFirst = false;
+	this._first = undefined;
+	this._memUsageBytes = 0;
 };

+ 14 - 19
test/lib/pipeline/accumulators/FirstAccumulator.js

@@ -1,16 +1,11 @@
 "use strict";
 var assert = require("assert"),
-	FirstAccumulator = require("../../../../lib/pipeline/accumulators/FirstAccumulator"),
-	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
-
+	FirstAccumulator = require("../../../../lib/pipeline/accumulators/FirstAccumulator");
 
 function createAccumulator(){
-	var firstAccumulator = new FirstAccumulator();
-	firstAccumulator.addOperand(new FieldPathExpression("a") );
-	return firstAccumulator;
+	return new FirstAccumulator();
 }
 
-
 module.exports = {
 
 	"FirstAccumulator": {
@@ -41,37 +36,37 @@ module.exports = {
 
 		},
 
-		"#evaluate()": {
+		"#processInternal()": {
 
-			"The accumulator evaluates no documents": function none() {
+			"The accumulator has no value": function none() {
 				// The accumulator returns no value in this case.
 				var acc = createAccumulator();
 				assert.ok(!acc.getValue());
 			},
 
-			"The accumulator evaluates one document and retains its value": function one() {
+			"The accumulator uses processInternal on one input and retains its value": function one() {
 				var acc = createAccumulator();
-				acc.evaluate({a:5});
+				acc.processInternal(5);
 				assert.strictEqual(acc.getValue(), 5);
 			},
 
-			"The accumulator evaluates one document with the field missing retains undefined": function missing() {
+			"The accumulator uses processInternal on one input with the field missing and retains undefined": function missing() {
 				var acc = createAccumulator();
-				acc.evaluate({});
+				acc.processInternal();
 				assert.strictEqual(acc.getValue(), undefined);
 			},
 
-			"The accumulator evaluates two documents and retains the value in the first": function two() {
+			"The accumulator uses processInternal on two inputs and retains the value in the first": function two() {
 				var acc = createAccumulator();
-				acc.evaluate({a:5});
-				acc.evaluate({a:7});
+				acc.processInternal(5);
+				acc.processInternal(7);
 				assert.strictEqual(acc.getValue(), 5);
 			},
 
-			"The accumulator evaluates two documents and retains the undefined value in the first": function firstMissing() {
+			"The accumulator uses processInternal on two inputs and retains the undefined value in the first": function firstMissing() {
 				var acc = createAccumulator();
-				acc.evaluate({});
-				acc.evaluate({a:7});
+				acc.processInternal();
+				acc.processInternal(7);
 				assert.strictEqual(acc.getValue(), undefined);
 			}
 		}