AddToSetAccumulator.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. /**
  3. * Create an expression that finds the sum of n operands.
  4. * @class AddSoSetAccumulator
  5. * @namespace mungedb-aggregate.pipeline.accumulators
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var AddToSetAccumulator = module.exports = function AddToSetAccumulator(/* ctx */){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. this.set = [];
  12. //this.itr = undefined; /* Shoudln't need an iterator for the set */
  13. //this.ctx = undefined; /* Not using the context object currently as it is related to sharding */
  14. base.call(this);
  15. }, klass = AddToSetAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, 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. proto.contains = function contains(value) {
  22. var set = this.set;
  23. for (var i = 0, l = set.length; i < l; ++i) {
  24. if (Value.compare(set[i], value) === 0) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. };
  30. proto.processInternal = function processInternal(input, merging) {
  31. if (! this.contains(input)) {
  32. this.set.push(input);
  33. }
  34. };
  35. proto.getValue = function getValue(toBeMerged) {
  36. return this.set;
  37. };
  38. proto.reset = function reset() {
  39. this.set = [];
  40. };
  41. // PROTOTYPE MEMBERS
  42. proto.getOpName = function getOpName(){
  43. return "$addToSet";
  44. };