Explorar o código

EAGLESIX-2651: initial port of AddToSet Accumulator from v2.6.5

Kyle P Davis %!s(int64=11) %!d(string=hai) anos
pai
achega
7bb5213ba7
Modificáronse 1 ficheiros con 29 adicións e 34 borrados
  1. 29 34
      lib/pipeline/accumulators/AddToSetAccumulator.js

+ 29 - 34
lib/pipeline/accumulators/AddToSetAccumulator.js

@@ -7,53 +7,48 @@
  * @module mungedb-aggregate
  * @constructor
 **/
-var AddToSetAccumulator = module.exports = function AddToSetAccumulator(/* ctx */){
+var AddToSetAccumulator = module.exports = function AddToSetAccumulator(){
 	if (arguments.length !== 0) throw new Error("zero args expected");
-	this.set = [];
-	//this.itr = undefined; /* Shoudln't need an iterator for the set */
-	//this.ctx = undefined; /* Not using the context object currently as it is related to sharding */
 	base.call(this);
 }, klass = AddToSetAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-// NOTE: Skipping the create function, using the constructor instead
-
-// DEPENDENCIES
 var Value = require("../Value");
 
-
-// MEMBER FUNCTIONS
-
-proto.getOpName = function getOpName(){
-	return "$addToSet";
-};
-
-proto.getFactory = function getFactory(){
-	return klass;	// using the ctor rather than a separate .create() method
-};
-
-
-proto.contains = function contains(value) {
-	var set = this.set;
-	for (var i = 0, l = set.length; i < l; ++i) {
-		if (Value.compare(set[i], value) === 0) {
-			return true;
+proto.processInternal  = function processInternal(input, merging) {
+    if (!merging) {
+        if (input !== undefined) {
+            this.set[JSON.stringify(input)] = input;
+        }
+    } else {
+        // If we're merging, we need to take apart the arrays we
+        // receive and put their elements into the array we are collecting.
+        // If we didn't, then we'd get an array of arrays, with one array
+        // from each merge source.
+		if (!Array.isArray(input)) throw new Error("Assertion failure");
+
+        for (var i = 0, l = input.length; i < l; i++) {
+			this.set[JSON.stringify(input[i])] = input[i];
 		}
-	}
-	return false;
-};
-
-proto.processInternal = function processInternal(input, merging) {
-	if (! this.contains(input)) {
-		this.set.push(input);
-	}
+    }
 };
 
 proto.getValue = function getValue(toBeMerged) {
-	return this.set;
+	var results = [];
+	for(var key in this.set){
+		// if(!Object.hasOwnProperty(this.set))
+		results.push(this.set[key]);
+	}
+	return results;
 };
 
 proto.reset = function reset() {
-	this.set = [];
+    this.set = {};
 };
 
+klass.create = function create() {
+    return new AddToSetAccumulator();
+};
 
+proto.getOpName = function getOpName() {
+    return "$addToSet";
+};