1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/**
* Copyright (c) 2011 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "mongo/pch.h"
#include <boost/unordered_set.hpp>
#include "db/pipeline/value.h"
#include "db/pipeline/expression.h"
#include "bson/bsontypes.h"
namespace mongo {
class ExpressionContext;
class Accumulator :
public ExpressionNary {
public:
// virtuals from ExpressionNary
virtual void addOperand(const intrusive_ptr<Expression> &pExpression);
virtual void addToBsonObj(
BSONObjBuilder *pBuilder, string fieldName,
bool requireExpression) const;
virtual void addToBsonArray(BSONArrayBuilder *pBuilder) const;
/*
Get the accumulated value.
@returns the accumulated value
*/
virtual intrusive_ptr<const Value> getValue() const = 0;
protected:
Accumulator();
/*
Convenience method for doing this for accumulators. The pattern
is always the same, so a common implementation works, but requires
knowing the operator name.
@param pBuilder the builder to add to
@param fieldName the projected name
@param opName the operator name
*/
void opToBson(
BSONObjBuilder *pBuilder, string fieldName, string opName,
bool requireExpression) const;
};
class AccumulatorAddToSet :
public Accumulator {
public:
// virtuals from Expression
virtual intrusive_ptr<const Value> evaluate(
const intrusive_ptr<Document> &pDocument) const;
virtual intrusive_ptr<const Value> getValue() const;
virtual const char *getOpName() const;
/*
Create an appending accumulator.
@param pCtx the expression context
@returns the created accumulator
*/
static intrusive_ptr<Accumulator> create(
const intrusive_ptr<ExpressionContext> &pCtx);
private:
AccumulatorAddToSet(const intrusive_ptr<ExpressionContext> &pTheCtx);
typedef boost::unordered_set<intrusive_ptr<const Value>, Value::Hash > SetType;
mutable SetType set;
mutable SetType::iterator itr;
intrusive_ptr<ExpressionContext> pCtx;
};
/*
This isn't a finished accumulator, but rather a convenient base class
for others such as $first, $last, $max, $min, and similar. It just
provides a holder for a single Value, and the getter for that. The
holder is protected so derived classes can manipulate it.
*/
class AccumulatorSingleValue :
public Accumulator {
public:
// virtuals from Expression
virtual intrusive_ptr<const Value> getValue() const;
protected:
AccumulatorSingleValue();
mutable intrusive_ptr<const Value> pValue; /* current min/max */
};
class AccumulatorFirst :
public AccumulatorSingleValue {
public:
// virtuals from Expression
virtual intrusive_ptr<const Value> evaluate(
const intrusive_ptr<Document> &pDocument) const;
virtual const char *getOpName() const;
/*
Create the accumulator.
@returns the created accumulator
*/
static intrusive_ptr<Accumulator> create(
const intrusive_ptr<ExpressionContext> &pCtx);
private:
AccumulatorFirst();
};
class AccumulatorLast :
public AccumulatorSingleValue {
public:
// virtuals from Expression
virtual intrusive_ptr<const Value> evaluate(
const intrusive_ptr<Document> &pDocument) const;
virtual const char *getOpName() const;
/*
Create the accumulator.
@returns the created accumulator
*/
static intrusive_ptr<Accumulator> create(
const intrusive_ptr<ExpressionContext> &pCtx);
private:
AccumulatorLast();
};
class AccumulatorSum :
public Accumulator {
public:
// virtuals from Accumulator
virtual intrusive_ptr<const Value> evaluate(
const intrusive_ptr<Document> &pDocument) const;
virtual intrusive_ptr<const Value> getValue() const;
virtual const char *getOpName() const;
/*
Create a summing accumulator.
@param pCtx the expression context
@returns the created accumulator
*/
static intrusive_ptr<Accumulator> create(
const intrusive_ptr<ExpressionContext> &pCtx);
protected: /* reused by AccumulatorAvg */
AccumulatorSum();
mutable BSONType totalType;
mutable long long longTotal;
mutable double doubleTotal;
// count is only used by AccumulatorAvg, but lives here to avoid counting non-numeric values
mutable long long count;
};
class AccumulatorMinMax :
public AccumulatorSingleValue {
public:
// virtuals from Expression
virtual intrusive_ptr<const Value> evaluate(
const intrusive_ptr<Document> &pDocument) const;
virtual const char *getOpName() const;
/*
Create either the max or min accumulator.
@returns the created accumulator
*/
static intrusive_ptr<Accumulator> createMin(
const intrusive_ptr<ExpressionContext> &pCtx);
static intrusive_ptr<Accumulator> createMax(
const intrusive_ptr<ExpressionContext> &pCtx);
private:
AccumulatorMinMax(int theSense);
int sense; /* 1 for min, -1 for max; used to "scale" comparison */
};
class AccumulatorPush :
public Accumulator {
public:
// virtuals from Expression
virtual intrusive_ptr<const Value> evaluate(
const intrusive_ptr<Document> &pDocument) const;
virtual intrusive_ptr<const Value> getValue() const;
virtual const char *getOpName() const;
/*
Create an appending accumulator.
@param pCtx the expression context
@returns the created accumulator
*/
static intrusive_ptr<Accumulator> create(
const intrusive_ptr<ExpressionContext> &pCtx);
private:
AccumulatorPush(const intrusive_ptr<ExpressionContext> &pTheCtx);
mutable vector<intrusive_ptr<const Value> > vpValue;
intrusive_ptr<ExpressionContext> pCtx;
};
class AccumulatorAvg :
public AccumulatorSum {
typedef AccumulatorSum Super;
public:
// virtuals from Accumulator
virtual intrusive_ptr<const Value> evaluate(
const intrusive_ptr<Document> &pDocument) const;
virtual intrusive_ptr<const Value> getValue() const;
virtual const char *getOpName() const;
/*
Create an averaging accumulator.
@param pCtx the expression context
@returns the created accumulator
*/
static intrusive_ptr<Accumulator> create(
const intrusive_ptr<ExpressionContext> &pCtx);
private:
static const char subTotalName[];
static const char countName[];
AccumulatorAvg(const intrusive_ptr<ExpressionContext> &pCtx);
intrusive_ptr<ExpressionContext> pCtx;
};
}
|