FirstAccumulator.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. /**
  3. * Constructor for FirstAccumulator, wraps Accumulator and adds flag to track whether we have started or not
  4. * @class FirstAccumulator
  5. * @namespace mungedb-aggregate.pipeline.accumulators
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var FirstAccumulator = module.exports = function FirstAccumulator(){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. base.call(this);
  12. this._haveFirst = false;
  13. this._first = undefined;
  14. }, klass = FirstAccumulator, base = require("./Accumulator"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  15. // NOTE: Skipping the create function, using the constructor instead
  16. // MEMBER FUNCTIONS
  17. proto.getOpName = function getOpName(){
  18. return "$first";
  19. };
  20. proto.processInternal = function processInternal(input, merging) {
  21. /* only remember the first value seen */
  22. if (!this._haveFirst) {
  23. // can't use pValue.missing() since we want the first value even if missing
  24. this._haveFirst = true;
  25. this._first = input;
  26. //this._memUsageBytes = sizeof(*this) + input.getApproximateSize() - sizeof(Value);
  27. }
  28. };
  29. proto.getValue = function getValue(toBeMerged) {
  30. return this._first;
  31. };
  32. proto.reset = function reset() {
  33. this._haveFirst = false;
  34. this._first = undefined;
  35. this._memUsageBytes = 0;
  36. };