Accumulator.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var Accumulator = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * A base class for all pipeline accumulators. Uses NaryExpression as a base class.
  6. *
  7. * @class Accumulator
  8. * @namespace munge.pipeline.accumulators
  9. * @module munge
  10. * @constructor
  11. **/
  12. var klass = function Accumulator(){
  13. if(arguments.length !== 0) throw new Error("zero args expected");
  14. base.call(this);
  15. }, base = require("../expressions/NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  16. // DEPENDENCIES
  17. // var Value = require("../Value"),
  18. proto.getFactory = function getFactory(){
  19. return klass; // using the ctor rather than a separate .create() method
  20. };
  21. /**
  22. * Adds the operand after checking the current limit
  23. * The equal is there because it checks *before* adding the requested argument.
  24. * Cannot use checkArgLimit because Accumulator must return a different error code.
  25. *
  26. * @param expr the operand to add
  27. **/
  28. proto.addOperand = function addOperand(expr) {
  29. if (this.operands.length >= 1) throw new Error("code 15943; group accumulator " + this.getOpName() + " only accepts one operand");
  30. base.prototype.addOperand.call(this, expr);
  31. };
  32. /**
  33. * Convenience method for doing this for accumulators. The pattern
  34. * is always the same, so a common implementation works, but requires
  35. * knowing the operator name.
  36. *
  37. * @param {Object} pBuilder the builder to add to
  38. * @param {String} fieldName the projected name
  39. * @param {String} opName the operator name
  40. * @param {Boolean} requireExpression pass down if the expression is needed
  41. **/
  42. // proto.opToBson = function opToBson(pBuilder, opName, fieldName, requireExpression) {
  43. // if (this.operands.length == 1) throw new Error("this should never happen");
  44. // var builder = new BSONObjBuilder();
  45. // this.operands[0].addToBsonObj(builder, opName, requireExpression);
  46. // pBuilder.append(fieldName, builder.done());
  47. // };
  48. /**
  49. * Wrapper around opToBson
  50. *
  51. * @param {Object} pBuilder the builder to add to
  52. * @param {String} fieldName the projected name
  53. * @param {Boolean} requireExpression pass down if the expression is needed
  54. **/
  55. // proto.addToBsonObj = function addToBsonObj(pBuilder, fieldName, requireExpression) {
  56. // this.opToBson(pBuilder, this.getOpName(), fieldName, requireExpression);
  57. // };
  58. /**
  59. * Make sure that nobody adds an accumulator to an array
  60. *
  61. * @param {Object} pBuilder the builder to add to
  62. **/
  63. proto.addToBsonArray = function addToBsonArray(pBuilder) {
  64. if (false) throw new Error("this should never happen"); // these can't appear in arrays
  65. };
  66. /**
  67. * If this function is not overridden in the sub classes,
  68. * then throw an error
  69. *
  70. **/
  71. proto.getValue = function getValue() {
  72. throw new Error("You need to define this function on your accumulator");
  73. };
  74. return klass;
  75. })();