Accumulator.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. /**
  3. * A base class for all pipeline accumulators.
  4. * @class Accumulator
  5. * @namespace mungedb-aggregate.pipeline.accumulators
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var Accumulator = module.exports = function Accumulator(){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. base.call(this);
  12. }, klass = Accumulator, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. /** Process input and update internal state.
  14. * merging should be true when processing outputs from getValue(true).
  15. * @method process
  16. * @param input {Value}
  17. * @param merging {Boolean}
  18. */
  19. proto.process = function process(input, merging) {
  20. this.processInternal(input, merging);
  21. };
  22. /** Marks the end of the evaluate() phase and return accumulated result.
  23. * toBeMerged should be true when the outputs will be merged by process().
  24. * @method getValue
  25. * @param toBeMerged {Boolean}
  26. * @return {Value}
  27. */
  28. proto.getValue = function getValue(toBeMerged) {
  29. throw new Error("You need to define this function on your accumulator");
  30. };
  31. /**
  32. * The name of the op as used in a serialization of the pipeline.
  33. * @method getOpName
  34. * @return {String}
  35. */
  36. proto.getOpName = function getOpName() {
  37. throw new Error("You need to define this function on your accumulator");
  38. };
  39. //NOTE: DEVIATION FROM MONGO: not implementing this
  40. //int memUsageForSorter() const {}
  41. /**
  42. * Reset this accumulator to a fresh state ready to receive input.
  43. * @method reset
  44. */
  45. proto.reset = function reset() {
  46. throw new Error("You need to define this function on your accumulator");
  47. };
  48. /**
  49. * Update subclass's internal state based on input
  50. * @method processInternal
  51. * @param input {Value}
  52. * @param merging {Boolean}
  53. */
  54. proto.processInternal = function processInternal(input, merging) {
  55. throw new Error("You need to define this function on your accumulator");
  56. };
  57. //NOTE: DEVIATION FROM MONGO: not implementing this
  58. // /// subclasses are expected to update this as necessary
  59. // int _memUsageBytes;