Accumulator.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. /**
  3. * A base class for all pipeline accumulators. Uses NaryExpression as a base class.
  4. *
  5. * @class Accumulator
  6. * @namespace mungedb-aggregate.pipeline.accumulators
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var Accumulator = module.exports = function Accumulator(){
  11. if (arguments.length !== 0) throw new Error("zero args expected");
  12. this._memUsageBytes = 0;
  13. base.call(this);
  14. }, klass = Accumulator, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  15. // DEPENDENCIES
  16. // var Value = require("../Value"),
  17. proto.memUsageForSorter = function memUsageForSorter() {
  18. return this._memUsageBytes;
  19. };
  20. proto.getFactory = function getFactory(){
  21. return klass; // using the ctor rather than a separate .create() method
  22. };
  23. /** Process input and update internal state.
  24. * merging should be true when processing outputs from getValue(true).
  25. */
  26. proto.process = function process(input, merging){
  27. this.processInternal(input, merging);
  28. };
  29. proto.toJSON = function toJSON(isExpressionRequired){
  30. var rep = {};
  31. rep[this.getOpName()] = this.operands[0].toJSON(isExpressionRequired);
  32. return rep;
  33. };
  34. /**
  35. * If this function is not overridden in the sub classes,
  36. * then throw an error
  37. *
  38. **/
  39. proto.getOpName = function getOpName() {
  40. throw new Error("You need to define this function on your accumulator");
  41. };
  42. /**
  43. * If this function is not overridden in the sub classes,
  44. * then throw an error
  45. *
  46. **/
  47. proto.getValue = function getValue(toBeMerged) {
  48. throw new Error("You need to define this function on your accumulator");
  49. };
  50. /**
  51. * If this function is not overridden in the sub classes,
  52. * then throw an error
  53. *
  54. **/
  55. proto.processInternal = function processInternal(input, merging) {
  56. throw new Error("You need to define this function on your accumulator");
  57. };
  58. /**
  59. * If this function is not overridden in the sub classes,
  60. * then throw an error
  61. *
  62. **/
  63. proto.reset = function reset() {
  64. throw new Error("You need to define this function on your accumulator");
  65. };