1 // bsontypes.h
2 
3 
4 /**
5  *    Copyright (C) 2018-present MongoDB, Inc.
6  *
7  *    This program is free software: you can redistribute it and/or modify
8  *    it under the terms of the Server Side Public License, version 1,
9  *    as published by MongoDB, Inc.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    Server Side Public License for more details.
15  *
16  *    You should have received a copy of the Server Side Public License
17  *    along with this program. If not, see
18  *    <http://www.mongodb.com/licensing/server-side-public-license>.
19  *
20  *    As a special exception, the copyright holders give permission to link the
21  *    code of portions of this program with the OpenSSL library under certain
22  *    conditions as described in each individual source file and distribute
23  *    linked combinations including the program with the OpenSSL library. You
24  *    must comply with the Server Side Public License in all respects for
25  *    all of the code used other than as permitted herein. If you modify file(s)
26  *    with this exception, you may extend this exception to your version of the
27  *    file(s), but you are not obligated to do so. If you do not wish to do so,
28  *    delete this exception statement from your version. If you delete this
29  *    exception statement from all source files in the program, then also delete
30  *    it in the license file.
31  */
32 
33 #pragma once
34 
35 #include <iosfwd>
36 
37 #include "mongo/config.h"
38 #include "mongo/platform/decimal128.h"
39 #include "mongo/util/assert_util.h"
40 
41 namespace mongo {
42 
43 class BSONArrayBuilder;
44 class BSONElement;
45 class BSONElementCmpWithoutField;
46 class BSONObj;
47 class BSONObjBuilder;
48 class BSONObjBuilderValueStream;
49 class BSONObjIterator;
50 class Ordering;
51 struct BSONArray;  // empty subclass of BSONObj useful for overloading
52 
53 extern const BSONObj kMaxBSONKey;
54 extern const BSONObj kMinBSONKey;
55 
56 /**
57     determines BSON types considered valid by validate
58 */
59 enum class BSONVersion { kV1_0, kV1_1, kLatest = kV1_1 };
60 
61 /**
62     the complete list of valid BSON types
63     see also bsonspec.org
64 */
65 enum BSONType {
66     /** smaller than all other types */
67     MinKey = -1,
68     /** end of object */
69     EOO = 0,
70     /** double precision floating point value */
71     NumberDouble = 1,
72     /** character string, stored in utf8 */
73     String = 2,
74     /** an embedded object */
75     Object = 3,
76     /** an embedded array */
77     Array = 4,
78     /** binary data */
79     BinData = 5,
80     /** Undefined type */
81     Undefined = 6,
82     /** ObjectId */
83     jstOID = 7,
84     /** boolean type */
85     Bool = 8,
86     /** date type */
87     Date = 9,
88     /** null type */
89     jstNULL = 10,
90     /** regular expression, a pattern with options */
91     RegEx = 11,
92     /** deprecated / will be redesigned */
93     DBRef = 12,
94     /** deprecated / use CodeWScope */
95     Code = 13,
96     /** a programming language (e.g., Python) symbol */
97     Symbol = 14,
98     /** javascript code that can execute on the database server, with SavedContext */
99     CodeWScope = 15,
100     /** 32 bit signed integer */
101     NumberInt = 16,
102     /** Two 32 bit signed integers */
103     bsonTimestamp = 17,
104     /** 64 bit integer */
105     NumberLong = 18,
106     /** 128 bit decimal */
107     NumberDecimal = 19,
108     /** max type that is not MaxKey */
109     JSTypeMax = 19,
110     /** larger than all other types */
111     MaxKey = 127
112 };
113 
114 /**
115  * returns the name of the argument's type
116  */
117 const char* typeName(BSONType type);
118 
119 /**
120  * Prints the name of the argument's type to the given stream.
121  */
122 std::ostream& operator<<(std::ostream& stream, BSONType type);
123 
124 /**
125  * Returns whether or not 'type' can be converted to a valid BSONType.
126  */
127 bool isValidBSONType(int type);
128 
isNumericBSONType(BSONType type)129 inline bool isNumericBSONType(BSONType type) {
130     switch (type) {
131         case NumberDouble:
132         case NumberInt:
133         case NumberLong:
134         case NumberDecimal:
135             return true;
136         default:
137             return false;
138     }
139 }
140 
141 /* subtypes of BinData.
142    bdtCustom and above are ones that the JS compiler understands, but are
143    opaque to the database.
144 */
145 enum BinDataType {
146     BinDataGeneral = 0,
147     Function = 1,
148     ByteArrayDeprecated = 2, /* use BinGeneral instead */
149     bdtUUID = 3,             /* deprecated */
150     newUUID = 4,             /* language-independent UUID format across all drivers */
151     MD5Type = 5,
152     bdtCustom = 128
153 };
154 
155 /**
156  * Return the name of the BinData Type.
157  */
158 const char* typeName(BinDataType type);
159 
160 /** Returns a number for where a given type falls in the sort order.
161  *  Elements with the same return value should be compared for value equality.
162  *  The return value is not a BSONType and should not be treated as one.
163  *  Note: if the order changes, indexes have to be re-built or than can be corruption
164  */
canonicalizeBSONType(BSONType type)165 inline int canonicalizeBSONType(BSONType type) {
166     switch (type) {
167         case MinKey:
168         case MaxKey:
169             return type;
170         case EOO:
171         case Undefined:
172             return 0;
173         case jstNULL:
174             return 5;
175         case NumberDecimal:
176         case NumberDouble:
177         case NumberInt:
178         case NumberLong:
179             return 10;
180         case mongo::String:
181         case Symbol:
182             return 15;
183         case Object:
184             return 20;
185         case mongo::Array:
186             return 25;
187         case BinData:
188             return 30;
189         case jstOID:
190             return 35;
191         case mongo::Bool:
192             return 40;
193         case mongo::Date:
194             return 45;
195         case bsonTimestamp:
196             return 47;
197         case RegEx:
198             return 50;
199         case DBRef:
200             return 55;
201         case Code:
202             return 60;
203         case CodeWScope:
204             return 65;
205         default:
206             verify(0);
207             return -1;
208     }
209 }
210 }
211