Accumulator.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. proto.toJson = function toJson(isExpressionRequired){
  33. var rep = {};
  34. rep[this.getOpName()] = this.operands[0].toJson(isExpressionRequired);
  35. return rep;
  36. };
  37. /**
  38. * Convenience method for doing this for accumulators. The pattern
  39. * is always the same, so a common implementation works, but requires
  40. * knowing the operator name.
  41. *
  42. * @param {Object} pBuilder the builder to add to
  43. * @param {String} fieldName the projected name
  44. * @param {String} opName the operator name
  45. * @param {Boolean} requireExpression pass down if the expression is needed
  46. **/
  47. // proto.opToBson = function opToBson(pBuilder, opName, fieldName, requireExpression) {
  48. // if (this.operands.length == 1) throw new Error("this should never happen");
  49. // var builder = new BSONObjBuilder();
  50. // this.operands[0].addToBsonObj(builder, opName, requireExpression);
  51. // pBuilder.append(fieldName, builder.done());
  52. // };
  53. /**
  54. * Wrapper around opToBson
  55. *
  56. * @param {Object} pBuilder the builder to add to
  57. * @param {String} fieldName the projected name
  58. * @param {Boolean} requireExpression pass down if the expression is needed
  59. **/
  60. // proto.addToBsonObj = function addToBsonObj(pBuilder, fieldName, requireExpression) {
  61. // this.opToBson(pBuilder, this.getOpName(), fieldName, requireExpression);
  62. // };
  63. /**
  64. * Make sure that nobody adds an accumulator to an array
  65. *
  66. * @param {Object} pBuilder the builder to add to
  67. **/
  68. proto.addToBsonArray = function addToBsonArray(pBuilder) {
  69. if (false) throw new Error("this should never happen"); // these can't appear in arrays
  70. };
  71. /**
  72. * If this function is not overridden in the sub classes,
  73. * then throw an error
  74. *
  75. **/
  76. proto.getValue = function getValue() {
  77. throw new Error("You need to define this function on your accumulator");
  78. };
  79. return klass;
  80. })();