|
|
@@ -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;
|
|
|
};
|