FirstAccumulator.js 1.3 KB

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