MongoDB C++ Driver  legacy-1.0.5
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
bsonmisc.h
1 // @file bsonmisc.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <memory>
21 
22 #include "mongo/bson/bsonelement.h"
23 #include "mongo/client/export_macros.h"
24 
25 namespace mongo {
26 
27  int getGtLtOp(const BSONElement& e);
28 
30  bool operator()( const BSONElement &l, const BSONElement &r ) const {
31  return l.woCompare( r, false ) < 0;
32  }
33  };
34 
35  class BSONObjCmp {
36  public:
37  BSONObjCmp( const BSONObj &order = BSONObj() ) : _order( order ) {}
38  bool operator()( const BSONObj &l, const BSONObj &r ) const {
39  return l.woCompare( r, _order ) < 0;
40  }
41  BSONObj order() const { return _order; }
42  private:
43  BSONObj _order;
44  };
45 
46  typedef std::set<BSONObj,BSONObjCmp> BSONObjSet;
47 
48  enum FieldCompareResult {
49  LEFT_SUBFIELD = -2,
50  LEFT_BEFORE = -1,
51  SAME = 0,
52  RIGHT_BEFORE = 1 ,
53  RIGHT_SUBFIELD = 2
54  };
55 
69 #define BSON(x) (( ::mongo::BSONObjBuilder(64) << x ).obj())
70 
76 #define BSON_ARRAY(x) (( ::mongo::BSONArrayBuilder() << x ).arr())
77 
78  /* Utility class to auto assign object IDs.
79  Example:
80  std::cout << BSON( GENOID << "z" << 3 ); // { _id : ..., z : 3 }
81  */
82  struct MONGO_CLIENT_API GENOIDLabeler { };
83  extern MONGO_CLIENT_API GENOIDLabeler GENOID;
84 
85  /* Utility class to add a Date element with the current time
86  Example:
87  std::cout << BSON( "created" << DATENOW ); // { created : "2009-10-09 11:41:42" }
88  */
89  struct MONGO_CLIENT_API DateNowLabeler { };
90  extern MONGO_CLIENT_API DateNowLabeler DATENOW;
91 
92  /* Utility class to assign a NULL value to a given attribute
93  Example:
94  std::cout << BSON( "a" << BSONNULL ); // { a : null }
95  */
96  struct MONGO_CLIENT_API NullLabeler { };
97  extern MONGO_CLIENT_API NullLabeler BSONNULL;
98 
99  /* Utility class to assign an Undefined value to a given attribute
100  Example:
101  std::cout << BSON( "a" << BSONUndefined ); // { a : undefined }
102  */
103  struct MONGO_CLIENT_API UndefinedLabeler { };
104  extern MONGO_CLIENT_API UndefinedLabeler BSONUndefined;
105 
106  /* Utility class to add the minKey (minus infinity) to a given attribute
107  Example:
108  std::cout << BSON( "a" << MINKEY ); // { "a" : { "$minKey" : 1 } }
109  */
110  struct MONGO_CLIENT_API MinKeyLabeler { };
111  extern MONGO_CLIENT_API MinKeyLabeler MINKEY;
112  struct MONGO_CLIENT_API MaxKeyLabeler { };
113  extern MONGO_CLIENT_API MaxKeyLabeler MAXKEY;
114 
115  // Utility class to implement GT, GTE, etc as described above.
116  class Labeler {
117  public:
118  struct Label {
119  explicit Label( const char *l ) : l_( l ) {}
120  const char *l_;
121  };
122  Labeler( const Label &l, BSONObjBuilderValueStream *s ) : l_( l ), s_( s ) {}
123  template<class T>
124  BSONObjBuilder& operator<<( T value );
125 
126  /* the value of the element e is appended i.e. for
127  "age" << GT << someElement
128  one gets
129  { age : { $gt : someElement's value } }
130  */
131  BSONObjBuilder& operator<<( const BSONElement& e );
132  private:
133  const Label &l_;
135  };
136 
137  // Utility class to allow adding a string to BSON as a Symbol
138  struct BSONSymbol {
139  explicit BSONSymbol(const StringData& sym) :symbol(sym) {}
140  StringData symbol;
141  };
142 
143  // Utility class to allow adding a string to BSON as Code
144  struct BSONCode {
145  explicit BSONCode(const StringData& str) :code(str) {}
146  StringData code;
147  };
148 
149  // Utility class to allow adding CodeWScope to BSON
150  struct BSONCodeWScope {
151  explicit BSONCodeWScope(const StringData& str, const BSONObj& obj) :code(str), scope(obj) {}
152  StringData code;
153  BSONObj scope;
154  };
155 
156  // Utility class to allow adding a RegEx to BSON
157  struct BSONRegEx {
158  explicit BSONRegEx(const StringData& pat, const StringData& f="") :pattern(pat), flags(f) {}
159  StringData pattern;
160  StringData flags;
161  };
162 
163  // Utility class to allow adding binary data to BSON
164  struct BSONBinData {
165  BSONBinData(const void* d, int l, BinDataType t) :data(d), length(l), type(t) {}
166  const void* data;
167  int length;
168  BinDataType type;
169  };
170 
171  // Utility class to allow adding deprecated DBRef type to BSON
172  struct BSONDBRef {
173  BSONDBRef(const StringData& nameSpace, const OID& o) :ns(nameSpace), oid(o) {}
174  StringData ns;
175  OID oid;
176  };
177 
178  extern MONGO_CLIENT_API Labeler::Label GT;
179  extern MONGO_CLIENT_API Labeler::Label GTE;
180  extern MONGO_CLIENT_API Labeler::Label LT;
181  extern MONGO_CLIENT_API Labeler::Label LTE;
182  extern MONGO_CLIENT_API Labeler::Label NE;
183  extern MONGO_CLIENT_API Labeler::Label NIN;
184  extern MONGO_CLIENT_API Labeler::Label BSIZE;
185 
186 
187  // $or helper: OR(BSON("x" << GT << 7), BSON("y" << LT << 6));
188  // becomes : {$or: [{x: {$gt: 7}}, {y: {$lt: 6}}]}
189  inline BSONObj OR(const BSONObj& a, const BSONObj& b);
190  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c);
191  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d);
192  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e);
193  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f);
194  // definitions in bsonobjbuilder.h b/c of incomplete types
195 
196  // Utility class to implement BSON( key << val ) as described above.
197  class MONGO_CLIENT_API BSONObjBuilderValueStream : public boost::noncopyable {
198  public:
199  friend class Labeler;
201 
202  BSONObjBuilder& operator<<( const BSONElement& e );
203 
204  template<class T>
205  BSONObjBuilder& operator<<( T value );
206 
207  BSONObjBuilder& operator<<(const DateNowLabeler& id);
208 
209  BSONObjBuilder& operator<<(const NullLabeler& id);
210  BSONObjBuilder& operator<<(const UndefinedLabeler& id);
211 
212  BSONObjBuilder& operator<<(const MinKeyLabeler& id);
213  BSONObjBuilder& operator<<(const MaxKeyLabeler& id);
214 
215  Labeler operator<<( const Labeler::Label &l );
216 
217  void endField( const StringData& nextFieldName = StringData() );
218  bool subobjStarted() const { return _fieldName != 0; }
219 
220  // The following methods provide API compatibility with BSONArrayBuilder
221  BufBuilder& subobjStart();
222  BufBuilder& subarrayStart();
223 
224  // This method should only be called from inside of implementations of
225  // BSONObjBuilder& operator<<(BSONObjBuilderValueStream&, SOME_TYPE)
226  // to provide the return value.
227  BSONObjBuilder& builder() { return *_builder; }
228  private:
229  StringData _fieldName;
230  BSONObjBuilder * _builder;
231 
232  bool haveSubobj() const { return _subobj.get() != 0; }
233  BSONObjBuilder *subobj();
234  std::auto_ptr< BSONObjBuilder > _subobj;
235  };
236 
241  public:
242  BSONSizeTracker() {
243  _pos = 0;
244  for ( int i=0; i<SIZE; i++ )
245  _sizes[i] = 512; // this is the default, so just be consistent
246  }
247 
248  ~BSONSizeTracker() {
249  }
250 
251  void got( int size ) {
252  _sizes[_pos] = size;
253  _pos = (_pos + 1) % SIZE; // thread safe at least on certain compilers
254  }
255 
259  int getSize() const {
260  int x = 16; // sane min
261  for ( int i=0; i<SIZE; i++ ) {
262  if ( _sizes[i] > x )
263  x = _sizes[i];
264  }
265  return x;
266  }
267 
268  private:
269  enum { SIZE = 10 };
270  int _pos;
271  int _sizes[SIZE];
272  };
273 
274  // considers order
275  bool fieldsMatch(const BSONObj& lhs, const BSONObj& rhs);
276 }
Definition: bsonmisc.h:150
Definition: bsonmisc.h:138
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
Definition: bsonmisc.h:35
Definition: bsonmisc.h:118
Object ID type.
Definition: oid.h:60
int woCompare(const BSONObj &r, const Ordering &o, bool considerFieldName=true) const
wo='well ordered'.
Definition: bsonmisc.h:82
used in conjuction with BSONObjBuilder, allows for proper buffer size to prevent crazy memory usage ...
Definition: bsonmisc.h:240
Definition: bsonmisc.h:29
Definition: bsonmisc.h:96
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:53
Definition: bsonmisc.h:144
Definition: bsonmisc.h:110
Definition: bsonmisc.h:103
int getSize() const
right now choosing largest size
Definition: bsonmisc.h:259
Definition: bsonmisc.h:164
Definition: bsonmisc.h:89
Definition: bsonmisc.h:157
Definition: bsonmisc.h:172
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
Definition: bsonmisc.h:112
int woCompare(const BSONElement &e, bool considerFieldName=true) const
Well ordered comparison.
Definition: bsonmisc.h:116
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
Definition: bsonmisc.h:197