PushAccumulator.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. /**
  3. * Constructor for PushAccumulator. Pushes items onto an array.
  4. * @class PushAccumulator
  5. * @namespace mungedb-aggregate.pipeline.accumulators
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var PushAccumulator = module.exports = function PushAccumulator(){
  10. this.values = [];
  11. base.call(this);
  12. }, klass = PushAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // NOTE: Skipping the create function, using the constructor instead
  14. // MEMBER FUNCTIONS
  15. proto.getValue = function getValue(toBeMerged){
  16. return this.values;
  17. };
  18. proto.getOpName = function getOpName(){
  19. return "$push";
  20. };
  21. proto.processInternal = function processInternal(input, merging) {
  22. if (!merging) {
  23. if (input !== undefined) {
  24. this.values.push(input);
  25. //_memUsageBytes += input.getApproximateSize();
  26. }
  27. }
  28. else {
  29. // If we're merging, we need to take apart the arrays we
  30. // receive and put their elements into the array we are collecting.
  31. // If we didn't, then we'd get an array of arrays, with one array
  32. // from each merge source.
  33. if (!(input instanceof Array)) throw new Error("input is not an Array during merge in PushAccumulator:35");
  34. this.values = this.values.concat(input);
  35. //for (size_t i=0; i < vec.size(); i++) {
  36. //_memUsageBytes += vec[i].getApproximateSize();
  37. //}
  38. }
  39. };