PushAccumulator.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. proto.getValue = function getValue(toBeMerged){
  14. return this.values;
  15. };
  16. proto.getFactory = function getFactory(){
  17. return klass; // using the ctor rather than a separate .create() method
  18. };
  19. proto.getOpName = function getOpName(){
  20. return "$push";
  21. };
  22. proto.processInternal = function processInternal(input, merging) {
  23. if (!merging) {
  24. if (input !== undefined) {
  25. this.values.push(input);
  26. //_memUsageBytes += input.getApproximateSize();
  27. }
  28. }
  29. else {
  30. // If we're merging, we need to take apart the arrays we
  31. // receive and put their elements into the array we are collecting.
  32. // If we didn't, then we'd get an array of arrays, with one array
  33. // from each merge source.
  34. if (!(input instanceof Array)) throw new Error("input is not an Array during merge in PushAccumulator:35");
  35. this.values = this.values.concat(input);
  36. //for (size_t i=0; i < vec.size(); i++) {
  37. //_memUsageBytes += vec[i].getApproximateSize();
  38. //}
  39. }
  40. };