PushAccumulator.js 1.5 KB

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