瀏覽代碼

Refs #3045 updated MinMaxAccumulator to 2.5.4

Mike McCarty 11 年之前
父節點
當前提交
61ff1a4d22

+ 13 - 13
lib/pipeline/accumulators/MinMaxAccumulator.js

@@ -12,7 +12,7 @@ var MinMaxAccumulator = module.exports = function MinMaxAccumulator(sense){
 	base.call(this);
 	this.sense = sense; /* 1 for min, -1 for max; used to "scale" comparison */
 	if (this.sense !== 1 && this.sense !== -1) throw new Error("this should never happen");
-}, klass = MinMaxAccumulator, base = require("./SingleValueAccumulator"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = MinMaxAccumulator, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 // DEPENDENCIES
 var Value = require("../Value");
@@ -31,23 +31,23 @@ klass.createMax = function createMax(){
 	return new MinMaxAccumulator(-1);
 };
 
-/** 
- * Takes a document and returns the first value in the document
- * @param {Object} doc the document source
- * @return the first value
- **/
-proto.evaluate = function evaluate(doc){
-	if (this.operands.length != 1) throw new Error("this should never happen");
-	var prhs = this.operands[0].evaluate(doc);
+proto.reset = function reset() {
+	this.value = undefined;
+};
 
+proto.getValue = function getValue(toBeMerged) {
+	return this.value;
+};
+
+proto.processInternal = function processInternal(input, merging) {
 	// if this is the first value, just use it
 	if (!this.hasOwnProperty('value')) {
-		this.value = prhs;
+		this.value = input;
 	} else {
 		// compare with the current value; swap if appropriate
-		var cmp = Value.compare(this.value, prhs) * this.sense;
-		if (cmp > 0) this.value = prhs;
+		var cmp = Value.compare(this.value, input) * this.sense;
+		if (cmp > 0) this.value = input;
 	}
 
 	return this.value;
-};
+};

+ 9 - 13
test/lib/pipeline/accumulators/MaxAccumulator.js

@@ -1,13 +1,9 @@
 "use strict";
 var assert = require("assert"),
-	MaxAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator"),
-	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
-
+	MaxAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator");
 
 function createAccumulator(){
-	var maxAccumulator = MaxAccumulator.createMax();
-	maxAccumulator.addOperand(new FieldPathExpression("a") );
-	return maxAccumulator;
+	return MaxAccumulator.createMax();
 }
 
 
@@ -40,7 +36,7 @@ module.exports = {
 
 		},
 
-		"#evaluate()": {
+		"#processInternal()": {
 
 			"The accumulator evaluates no documents": function none() {
 				// The accumulator returns no value in this case.
@@ -50,27 +46,27 @@ module.exports = {
 
 			"The accumulator evaluates one document and retains its value": function one() {
 				var acc = createAccumulator();
-				acc.evaluate({a:5});
+				acc.processInternal(5);
 				assert.strictEqual(acc.getValue(), 5);
 			},
 
 			"The accumulator evaluates one document with the field missing retains undefined": function missing() {
 				var acc = createAccumulator();
-				acc.evaluate({});
+				acc.processInternal();
 				assert.strictEqual(acc.getValue(), undefined);
 			},
 
 			"The accumulator evaluates two documents and retains the maximum": function two() {
 				var acc = createAccumulator();
-				acc.evaluate({a:5});
-				acc.evaluate({a:7});
+				acc.processInternal(5);
+				acc.processInternal(7);
 				assert.strictEqual(acc.getValue(), 7);
 			},
 
 			"The accumulator evaluates two documents and retains the defined value in the first": function lastMissing() {
 				var acc = createAccumulator();
-				acc.evaluate({a:7});
-				acc.evaluate({});
+				acc.processInternal(7);
+				acc.processInternal();
 				assert.strictEqual(acc.getValue(), 7);
 			}
 		}

+ 9 - 13
test/lib/pipeline/accumulators/MinAccumulator.js

@@ -1,13 +1,9 @@
 "use strict";
 var assert = require("assert"),
-	MinAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator"),
-	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
-
+	MinAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator");
 
 function createAccumulator(){
-	var minAccumulator = MinAccumulator.createMin();
-	minAccumulator.addOperand(new FieldPathExpression("a") );
-	return minAccumulator;
+	return MinAccumulator.createMin();
 }
 
 
@@ -40,7 +36,7 @@ module.exports = {
 
 		},
 
-		"#evaluate()": {
+		"#processInternal()": {
 
 			"The accumulator evaluates no documents": function none() {
 				// The accumulator returns no value in this case.
@@ -50,27 +46,27 @@ module.exports = {
 
 			"The accumulator evaluates one document and retains its value": function one() {
 				var acc = createAccumulator();
-				acc.evaluate({a:5});
+				acc.processInternal(5);
 				assert.strictEqual(acc.getValue(), 5);
 			},
 
 			"The accumulator evaluates one document with the field missing retains undefined": function missing() {
 				var acc = createAccumulator();
-				acc.evaluate({});
+				acc.processInternal();
 				assert.strictEqual(acc.getValue(), undefined);
 			},
 
 			"The accumulator evaluates two documents and retains the minimum": function two() {
 				var acc = createAccumulator();
-				acc.evaluate({a:5});
-				acc.evaluate({a:7});
+				acc.processInternal(5);
+				acc.processInternal(7);
 				assert.strictEqual(acc.getValue(), 5);
 			},
 
 			"The accumulator evaluates two documents and retains the undefined value in the last": function lastMissing() {
 				var acc = createAccumulator();
-				acc.evaluate({a:7});
-				acc.evaluate({});
+				acc.processInternal(7);
+				acc.processInternal();
 				assert.strictEqual(acc.getValue(), undefined);
 			}
 		}