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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/**
* 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/>.
*
* 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 GNU Affero General 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 "mongo/base/string_data.h"
#include "mongo/db/pipeline/value_internal.h"
#include "mongo/platform/unordered_set.h"
namespace mongo {
class BSONElement;
/** A variant type that can hold any type of data representable in BSON
*
* Small values are stored inline, but some values, such as large strings,
* are heap allocated. It has smart pointer capabilities built-in so it is
* safe and recommended to pass these around and return them by value.
*
* Values are immutable, but can be assigned. This means that once you have
* a Value, you can be assured that none of the data in that Value will
* change. However if you have a non-const Value you replace it with
* operator=. These rules are the same as BSONObj, and similar to
* shared_ptr<const Object> with stronger guarantees of constness. This is
* also the same as Java's std::string type.
*
* Thread-safety: A single Value instance can be safely shared between
* threads as long as there are no writers while other threads are
* accessing the object. Any number of threads can read from a Value
* concurrently. There are no restrictions on how threads access Value
* instances exclusively owned by them, even if they reference the same
* storage as Value in other threads.
*/
class Value {
public:
/**
* Operator overloads for relops return a DeferredComparison which can subsequently be evaluated
* by a ValueComparator.
*/
struct DeferredComparison {
enum class Type {
kLT,
kLTE,
kEQ,
kGT,
kGTE,
kNE,
};
DeferredComparison(Type type, const Value& lhs, const Value& rhs)
: type(type), lhs(lhs), rhs(rhs) {}
Type type;
const Value& lhs;
const Value& rhs;
};
/** Construct a Value
*
* All types not listed will be rejected rather than converted (see private for why)
*
* Note: Currently these are all explicit conversions.
* I'm not sure if we want implicit or not.
* //TODO decide
*/
Value() : _storage() {} // "Missing" value
explicit Value(bool value) : _storage(Bool, value) {}
explicit Value(int value) : _storage(NumberInt, value) {}
explicit Value(long long value) : _storage(NumberLong, value) {}
explicit Value(double value) : _storage(NumberDouble, value) {}
explicit Value(const Decimal128& value) : _storage(NumberDecimal, value) {}
explicit Value(const Timestamp& value) : _storage(bsonTimestamp, value) {}
explicit Value(const OID& value) : _storage(jstOID, value) {}
explicit Value(StringData value) : _storage(String, value) {}
explicit Value(const std::string& value) : _storage(String, StringData(value)) {}
explicit Value(const char* value) : _storage(String, StringData(value)) {}
explicit Value(const Document& doc) : _storage(Object, doc) {}
explicit Value(const BSONObj& obj);
explicit Value(const BSONArray& arr);
explicit Value(const std::vector<BSONObj>& arr);
explicit Value(std::vector<Value> vec) : _storage(Array, new RCVector(std::move(vec))) {}
explicit Value(const BSONBinData& bd) : _storage(BinData, bd) {}
explicit Value(const BSONRegEx& re) : _storage(RegEx, re) {}
explicit Value(const BSONCodeWScope& cws) : _storage(CodeWScope, cws) {}
explicit Value(const BSONDBRef& dbref) : _storage(DBRef, dbref) {}
explicit Value(const BSONSymbol& sym) : _storage(Symbol, sym.symbol) {}
explicit Value(const BSONCode& code) : _storage(Code, code.code) {}
explicit Value(const NullLabeler&) : _storage(jstNULL) {} // BSONNull
explicit Value(const UndefinedLabeler&) : _storage(Undefined) {} // BSONUndefined
explicit Value(const MinKeyLabeler&) : _storage(MinKey) {} // MINKEY
explicit Value(const MaxKeyLabeler&) : _storage(MaxKey) {} // MAXKEY
explicit Value(const Date_t& date) : _storage(Date, date.toMillisSinceEpoch()) {}
// TODO: add an unsafe version that can share storage with the BSONElement
/// Deep-convert from BSONElement to Value
explicit Value(const BSONElement& elem);
/** Construct a long or integer-valued Value.
*
* Used when preforming arithmetic operations with int where the
* result may be too large and need to be stored as long. The Value
* will be an int if value fits, otherwise it will be a long.
*/
static Value createIntOrLong(long long value);
/** A "missing" value indicates the lack of a Value.
* This is similar to undefined/null but should not appear in output to BSON.
* Missing Values are returned by Document when accessing non-existent fields.
*/
bool missing() const {
return _storage.type == EOO;
}
/// true if missing() or type is jstNULL or Undefined
bool nullish() const {
return missing() || _storage.type == jstNULL || _storage.type == Undefined;
}
/// true if type represents a number
bool numeric() const {
return _storage.type == NumberDouble || _storage.type == NumberLong ||
_storage.type == NumberInt || _storage.type == NumberDecimal;
}
/**
* Return true if the Value is an array.
*/
bool isArray() const {
return _storage.type == Array;
}
/**
* Returns true if this value is a numeric type that can be represented as a 32-bit integer,
* and false otherwise.
*/
bool integral() const;
/// Get the BSON type of the field.
BSONType getType() const {
return _storage.bsonType();
}
/** Exact type getters.
* Asserts if the requested value type is not exactly correct.
* See coerceTo methods below for a more type-flexible alternative.
*/
Decimal128 getDecimal() const;
double getDouble() const;
std::string getString() const;
Document getDocument() const;
OID getOid() const;
bool getBool() const;
long long getDate() const; // in milliseconds
Timestamp getTimestamp() const;
const char* getRegex() const;
const char* getRegexFlags() const;
std::string getSymbol() const;
std::string getCode() const;
int getInt() const;
long long getLong() const;
const std::vector<Value>& getArray() const {
return _storage.getArray();
}
size_t getArrayLength() const;
/// Access an element of a subarray. Returns Value() if missing or getType() != Array
Value operator[](size_t index) const;
/// Access a field of a subdocument. Returns Value() if missing or getType() != Object
Value operator[](StringData name) const;
/// Add this value to the BSON object under construction.
void addToBsonObj(BSONObjBuilder* pBuilder, StringData fieldName) const;
/// Add this field to the BSON array under construction.
void addToBsonArray(BSONArrayBuilder* pBuilder) const;
// Support BSONObjBuilder and BSONArrayBuilder "stream" API
friend BSONObjBuilder& operator<<(BSONObjBuilderValueStream& builder, const Value& val);
/** Coerce a value to a bool using BSONElement::trueValue() rules.
*/
bool coerceToBool() const;
/** Coercion operators to extract values with fuzzy type logic.
*
* These currently assert if called on an unconvertible type.
* TODO: decided how to handle unsupported types.
*/
std::string coerceToString() const;
int coerceToInt() const;
long long coerceToLong() const;
double coerceToDouble() const;
Decimal128 coerceToDecimal() const;
Timestamp coerceToTimestamp() const;
long long coerceToDate() const;
time_t coerceToTimeT() const;
tm coerceToTm() const; // broken-out time struct (see man gmtime)
//
// Comparison API.
//
// Value instances can be compared either using Value::compare() or via operator overloads.
// Most callers should prefer operator overloads. Note that the operator overloads return a
// DeferredComparison, which must be subsequently evaluated by a ValueComparator. See
// value_comparator.h for details.
//
/**
* Compare two Values. Most Values should prefer to use ValueComparator instead. See
* value_comparator.h for details.
*
* Pass a non-null StringData::ComparatorInterface if special string comparison semantics are
* required. If the comparator is null, then a simple binary compare is used for strings. This
* comparator is only used for string *values*; field names are always compared using simple
* binary compare.
*
* @returns an integer less than zero, zero, or an integer greater than
* zero, depending on whether lhs < rhs, lhs == rhs, or lhs > rhs
* Warning: may return values other than -1, 0, or 1
*/
static int compare(const Value& lhs,
const Value& rhs,
const StringData::ComparatorInterface* stringComparator);
friend DeferredComparison operator==(const Value& lhs, const Value& rhs) {
return DeferredComparison(DeferredComparison::Type::kEQ, lhs, rhs);
}
friend DeferredComparison operator!=(const Value& lhs, const Value& rhs) {
return DeferredComparison(DeferredComparison::Type::kNE, lhs, rhs);
}
friend DeferredComparison operator<(const Value& lhs, const Value& rhs) {
return DeferredComparison(DeferredComparison::Type::kLT, lhs, rhs);
}
friend DeferredComparison operator<=(const Value& lhs, const Value& rhs) {
return DeferredComparison(DeferredComparison::Type::kLTE, lhs, rhs);
}
friend DeferredComparison operator>(const Value& lhs, const Value& rhs) {
return DeferredComparison(DeferredComparison::Type::kGT, lhs, rhs);
}
friend DeferredComparison operator>=(const Value& lhs, const Value& rhs) {
return DeferredComparison(DeferredComparison::Type::kGTE, lhs, rhs);
}
/// This is for debugging, logging, etc. See getString() for how to extract a string.
std::string toString() const;
friend std::ostream& operator<<(std::ostream& out, const Value& v);
void swap(Value& rhs) {
_storage.swap(rhs._storage);
}
/** Figure out what the widest of two numeric types is.
*
* Widest can be thought of as "most capable," or "able to hold the
* largest or most precise value." The progression is Int, Long, Double.
*/
static BSONType getWidestNumeric(BSONType lType, BSONType rType);
/// Get the approximate memory size of the value, in bytes. Includes sizeof(Value)
size_t getApproximateSize() const;
/**
* Calculate a hash value.
*
* Meant to be used to create composite hashes suitable for hashed container classes such as
* unordered_map<>.
*
* Most callers should prefer the utilities in ValueComparator for hashing and creating function
* objects for computing the hash. See value_comparator.h.
*/
void hash_combine(size_t& seed, const StringData::ComparatorInterface* stringComparator) const;
/// Call this after memcpying to update ref counts if needed
void memcpyed() const {
_storage.memcpyed();
}
/// members for Sorter
struct SorterDeserializeSettings {}; // unused
void serializeForSorter(BufBuilder& buf) const;
static Value deserializeForSorter(BufReader& buf, const SorterDeserializeSettings&);
int memUsageForSorter() const {
return getApproximateSize();
}
Value getOwned() const {
return *this;
}
private:
/** This is a "honeypot" to prevent unexpected implicit conversions to the accepted argument
* types. bool is especially bad since without this it will accept any pointer.
*
* Template argument name was chosen to make produced error easier to read.
*/
template <typename InvalidArgumentType>
explicit Value(const InvalidArgumentType& invalidArgument);
explicit Value(const ValueStorage& storage) : _storage(storage) {}
// does no type checking
StringData getStringData() const; // May contain embedded NUL bytes
ValueStorage _storage;
friend class MutableValue; // gets and sets _storage.genericRCPtr
};
static_assert(sizeof(Value) == 16, "sizeof(Value) == 16");
inline void swap(mongo::Value& lhs, mongo::Value& rhs) {
lhs.swap(rhs);
}
/**
* This class is identical to Value, but supports implicit creation from any of the types explicitly
* supported by Value.
*/
class ImplicitValue : public Value {
public:
template <typename T>
ImplicitValue(T arg) : Value(std::move(arg)) {}
};
}
/* ======================= INLINED IMPLEMENTATIONS ========================== */
namespace mongo {
inline size_t Value::getArrayLength() const {
verify(getType() == Array);
return getArray().size();
}
inline StringData Value::getStringData() const {
return _storage.getString();
}
inline std::string Value::getString() const {
verify(getType() == String);
return _storage.getString().toString();
}
inline OID Value::getOid() const {
verify(getType() == jstOID);
return OID(_storage.oid);
}
inline bool Value::getBool() const {
verify(getType() == Bool);
return _storage.boolValue;
}
inline long long Value::getDate() const {
verify(getType() == Date);
return _storage.dateValue;
}
inline Timestamp Value::getTimestamp() const {
verify(getType() == bsonTimestamp);
return Timestamp(_storage.timestampValue);
}
inline const char* Value::getRegex() const {
verify(getType() == RegEx);
return _storage.getString().rawData(); // this is known to be NUL terminated
}
inline const char* Value::getRegexFlags() const {
verify(getType() == RegEx);
const char* pattern = _storage.getString().rawData(); // this is known to be NUL terminated
const char* flags = pattern + strlen(pattern) + 1; // first byte after pattern's NUL
dassert(flags + strlen(flags) == pattern + _storage.getString().size());
return flags;
}
inline std::string Value::getSymbol() const {
verify(getType() == Symbol);
return _storage.getString().toString();
}
inline std::string Value::getCode() const {
verify(getType() == Code);
return _storage.getString().toString();
}
inline int Value::getInt() const {
verify(getType() == NumberInt);
return _storage.intValue;
}
inline long long Value::getLong() const {
BSONType type = getType();
if (type == NumberInt)
return _storage.intValue;
verify(type == NumberLong);
return _storage.longValue;
}
};
|