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
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include <vector>
#include "mongo/base/init.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/pipeline/expression_context.h"
#include "mongo/db/pipeline/value.h"
#include "mongo/stdx/functional.h"
#include "mongo/util/intrusive_counter.h"
namespace mongo {
/**
* Registers a GranularityRounder with the name 'name' and initializer key 'key'. When a
* granularity specification with name 'name' is found, 'rounder' will be called to construct a
* GranularityRounder for that granularity.
*
* Use this macro directly if you want 'name' and 'key' to be different. For example, this might be
* the case if 'name' is not a valid name for a C++ symbol (e.g. foo-bar). Otherwise, use
* REGISTER_GRANULARITY_ROUNDER instead.
*
* As an example, if you are adding a rounding granularity called foo-bar you would add this line:
* REGISTER_GRANULARITY_ROUNDER_GENERAL("foo-bar", foo_bar, GranularityRounderFooBar::create);
*/
#define REGISTER_GRANULARITY_ROUNDER_GENERAL(name, key, rounder) \
MONGO_INITIALIZER(addToGranularityRounderMap_##key)(InitializerContext*) { \
GranularityRounder::registerGranularityRounder(name, rounder); \
return Status::OK(); \
}
/**
* Registers a GranularityRounder to have the name 'key'. When a granularity specification with name
* 'key' is found, 'rounder' will be called to construct a GranularityRounder for that
* granularity.
*
* As an example, if you are adding a rounding granularity called foo you would add this line:
* REGISTER_GRANULARITY_ROUNDER(foo, GranularityRounderFoo::create);
*/
#define REGISTER_GRANULARITY_ROUNDER(key, rounder) \
REGISTER_GRANULARITY_ROUNDER_GENERAL(#key, key, rounder)
/**
* This class provides a way to round a value up or down according to a specific rounding
* granularity.
*/
class GranularityRounder : public RefCountable {
public:
using Rounder = stdx::function<boost::intrusive_ptr<GranularityRounder>(
const boost::intrusive_ptr<ExpressionContext>&)>;
/**
* Registers a GranularityRounder with a parsing function so that when a granularity
* with the given name is encountered, it will call 'rounder' to construct the appropriate
* GranularityRounder.
*
* DO NOT call this method directly. Instead, use the REGISTER_GRANULARITY_ROUNDER or
* REGISTER_GRANULARITY_ROUNDER_GENERAL macro defined in this file.
*/
static void registerGranularityRounder(StringData granularity, Rounder rounder);
/**
* Retrieves the GranularityRounder for the granularity given by 'granularity', and raises an
* error if there is no such granularity registered.
*/
static boost::intrusive_ptr<GranularityRounder> getGranularityRounder(
const boost::intrusive_ptr<ExpressionContext>& expCtx, StringData granularity);
/**
* Rounds up 'value' to the first value greater than 'value' in the granularity series. If
* 'value' is already in the granularity series, then it will be rounded up to the next value in
* the series.
*/
virtual Value roundUp(Value value) = 0;
/**
* Rounds down 'value' to the first value less than 'value' in the granularity series. If
* 'value' is already in the granularity series, then it will be rounded down to the previous
* value in the series.
*/
virtual Value roundDown(Value value) = 0;
/**
* Returns the name of the granularity series that the GranularityRounder is using for rounding.
*/
virtual std::string getName() = 0;
protected:
GranularityRounder(const boost::intrusive_ptr<ExpressionContext>& expCtx) : _expCtx(expCtx) {}
ExpressionContext* getExpCtx() {
return _expCtx.get();
}
private:
boost::intrusive_ptr<ExpressionContext> _expCtx;
};
/**
* This class provides a way to round a value up or down according to a preferred number series.
*
* The series that this class aims to support are specified here:
* https://en.wikipedia.org/wiki/Preferred_number
*/
class GranularityRounderPreferredNumbers final : public GranularityRounder {
public:
/**
* Returns a Granularity rounder that rounds according to the series represented by
* 'baseSeries'. This method requires that baseSeries has at least 2 numbers and is in sorted
* order.
*/
static boost::intrusive_ptr<GranularityRounder> create(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
const std::vector<double> baseSeries,
std::string name);
Value roundUp(Value value);
Value roundDown(Value value);
std::string getName();
/**
* Returns a vector that represents the preferred number series that this
* GranularityRounderPreferredNumbers is using for rounding.
*/
const std::vector<double> getSeries() const;
private:
GranularityRounderPreferredNumbers(const boost::intrusive_ptr<ExpressionContext>& expCtx,
std::vector<double> baseSeries,
std::string name);
// '_baseSeries' is the preferred number series that is used for rounding. A preferred numbers
// series is infinite, but we represent it with a finite vector of numbers. When rounding, we
// scale the numbers in '_baseSeries' by a power of 10 until the number we are rounding falls
// into the range spanned by the scaled '_baseSeries'.
const std::vector<double> _baseSeries;
std::string _name;
};
/**
* This class provides a way to round a value up or down to a power of two.
*/
class GranularityRounderPowersOfTwo final : public GranularityRounder {
public:
static boost::intrusive_ptr<GranularityRounder> create(
const boost::intrusive_ptr<ExpressionContext>& expCtx);
Value roundUp(Value value);
Value roundDown(Value value);
std::string getName();
private:
GranularityRounderPowersOfTwo(const boost::intrusive_ptr<ExpressionContext>& expCtx)
: GranularityRounder(expCtx) {}
std::string _name = "POWERSOF2";
};
} // namespace mongo
|