FirstAccumulator.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.getFactory = function getFactory(){
  21. return klass; // using the ctor rather than a separate .create() method
  22. };
  23. proto.processInternal = function processInternal(input, merging) {
  24. /* only remember the first value seen */
  25. if (!this._haveFirst) {
  26. // can't use pValue.missing() since we want the first value even if missing
  27. this._haveFirst = true;
  28. this._first = input;
  29. //this._memUsageBytes = sizeof(*this) + input.getApproximateSize() - sizeof(Value);
  30. }
  31. };
  32. proto.getValue = function getValue(toBeMerged) {
  33. return this._first;
  34. };
  35. proto.reset = function reset() {
  36. this._haveFirst = false;
  37. this._first = undefined;
  38. this._memUsageBytes = 0;
  39. };