diff options
author | Mathias Stearn <mathias@10gen.com> | 2013-09-10 18:10:15 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2013-10-21 14:05:14 -0400 |
commit | 982d5d281806d470696809d11dceaa098aed4906 (patch) | |
tree | eb6db99de38ddb47d27e69bb04e7b3447c53939d /src/mongo/db/pipeline/accumulator_avg.cpp | |
parent | c187aceff59e7f2997416d9011da0b43e9ae34cb (diff) | |
download | mongo-982d5d281806d470696809d11dceaa098aed4906.tar.gz |
Clean up AccumulatorAvg
Major change is not inheriting from AccumulatorSum
Prep for SERVER-5044 $stdDev accumulator
Diffstat (limited to 'src/mongo/db/pipeline/accumulator_avg.cpp')
-rw-r--r-- | src/mongo/db/pipeline/accumulator_avg.cpp | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/src/mongo/db/pipeline/accumulator_avg.cpp b/src/mongo/db/pipeline/accumulator_avg.cpp index dc0ae953ef5..8dbfc816de1 100644 --- a/src/mongo/db/pipeline/accumulator_avg.cpp +++ b/src/mongo/db/pipeline/accumulator_avg.cpp @@ -42,20 +42,19 @@ namespace { void AccumulatorAvg::processInternal(const Value& input, bool merging) { if (!merging) { - Super::processInternal(input, merging); + // non numeric types have no impact on average + if (!input.numeric()) + return; + + _total += input.getDouble(); + _count += 1; } else { // We expect an object that contains both a subtotal and a count. // This is what getValue(true) produced below. verify(input.getType() == Object); - - Value subTotal = input[subTotalName]; - verify(!subTotal.missing()); - doubleTotal += subTotal.getDouble(); - - Value subCount = input[countName]; - verify(!subCount.missing()); - count += subCount.getLong(); + _total += input[subTotalName].getDouble(); + _count += input[countName].getLong(); } } @@ -65,32 +64,30 @@ namespace { Value AccumulatorAvg::getValue(bool toBeMerged) const { if (!toBeMerged) { - double avg = 0; - if (count) - avg = doubleTotal / static_cast<double>(count); + if (_count == 0) + return Value(0.0); - return Value(avg); + return Value(_total / static_cast<double>(_count)); } else { - MutableDocument out; - out.addField(subTotalName, Value(doubleTotal)); - out.addField(countName, Value(count)); - - return Value(out.freeze()); + return Value(DOC(subTotalName << _total + << countName << _count)); } } - AccumulatorAvg::AccumulatorAvg() { + AccumulatorAvg::AccumulatorAvg() + : _total(0) + , _count(0) + { // This is a fixed size Accumulator so we never need to update this _memUsageBytes = sizeof(*this); } void AccumulatorAvg::reset() { - // All state is in parent - Super::reset(); + _total = 0; + _count = 0; } - const char *AccumulatorAvg::getOpName() const { return "$avg"; } |