SequoiaDB
 All Classes Namespaces Files Functions Macros Pages
bsonmisc.h
Go to the documentation of this file.
1 
6 /* Copyright 2009 10gen Inc.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 #include "bsonnoncopyable.h"
26 namespace bson {
27 
28  int getGtLtOp(const BSONElement& e);
33  bool operator()( const BSONElement &l, const BSONElement &r ) const {
34  return l.woCompare( r, false ) < 0;
35  }
36  };
37 
38  class BSONObjCmp {
39  public:
40  BSONObjCmp( const BSONObj &order = BSONObj() ) : _order( order ) {}
41  bool operator()( const BSONObj &l, const BSONObj &r ) const {
42  return l.woCompare( r, _order ) < 0;
43  }
44  BSONObj order() const { return _order; }
45  private:
46  BSONObj _order;
47  };
48 
49  typedef set<BSONObj,BSONObjCmp> BSONObjSet;
50 
51  enum FieldCompareResult {
52  LEFT_SUBFIELD = -2,
53  LEFT_BEFORE = -1,
54  SAME = 0,
55  RIGHT_BEFORE = 1 ,
56  RIGHT_SUBFIELD = 2
57  };
58 
59  FieldCompareResult
60  compareDottedFieldNames( const char* l , const char* r );
61 
77 #define BSON(x) (( bson::BSONObjBuilder(64) << x ).obj())
78 
85 #define BSON_ARRAY(x) (( bson::BSONArrayBuilder() << x ).arr())
86 
91  extern struct GENOIDLabeler { } GENOID;
92 
93  /* Utility class to add a Date element with the current time
94  Example:
95  cout << BSON( "created" << DATENOW );
96  // { created : "2009-10-09 11:41:42" }
97  */
98  extern struct DateNowLabeler { } DATENOW;
99 
100  /* Utility class to add the minKey (minus infinity) to a given attribute
101  Example:
102  cout << BSON( "a" << MINKEY ); // { "a" : { "$minKey" : 1 } }
103  */
104  extern struct MinKeyLabeler { } MINKEY;
105  extern struct MaxKeyLabeler { } MAXKEY;
106 
107  // Utility class to implement GT, GTE, etc as described above.
108  class Labeler {
109  public:
110  struct Label {
111  Label( const char *l ) : l_( l ) {}
112  const char *l_;
113  };
114  Labeler( const Label &l, BSONObjBuilderValueStream *s )
115  : l_( l ), s_( s ) {}
116 
117  template<class T>
118  BSONObjBuilder& operator<<( T value );
119 
120  /* the value of the element e is appended i.e. for
121  "age" << GT << someElement
122  one gets
123  { age : { $gt : someElement's value } }
124  */
125  BSONObjBuilder& operator<<( const BSONElement& e );
126  private:
127  const Label &l_;
129  };
130 
131  extern Labeler::Label GT;
132  extern Labeler::Label GTE;
133  extern Labeler::Label LT;
134  extern Labeler::Label LTE;
135  extern Labeler::Label NE;
136  extern Labeler::Label SIZE;
137 
138 
139  // $or helper: OR(BSON("x" << GT << 7), BSON("y" << LT << 6));
140  // becomes : {$or: [{x: {$gt: 7}}, {y: {$lt: 6}}]}
141  inline BSONObj OR(const BSONObj& a, const BSONObj& b);
142  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c);
143  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c,
144  const BSONObj& d);
145  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c,
146  const BSONObj& d, const BSONObj& e);
147  inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c,
148  const BSONObj& d, const BSONObj& e, const BSONObj& f);
149  // definitions in bsonobjbuilder.h b/c of incomplete types
150 
151  // Utility class to implement BSON( key << val ) as described above.
152  class BSONObjBuilderValueStream : public bsonnoncopyable {
153  public:
154  friend class Labeler;
156 
157  void reset() ;
158 
159  BSONObjBuilder& operator<<( const BSONElement& e );
160 
161  template<class T>
162  BSONObjBuilder& operator<<( T value );
163 
164  //BSONObjBuilder& operator<<(DateNowLabeler& id);
165 
166  BSONObjBuilder& operator<<(MinKeyLabeler& id);
167  BSONObjBuilder& operator<<(MaxKeyLabeler& id);
168 
169  Labeler operator<<( const Labeler::Label &l );
170 
171  void endField( const char *nextFieldName = 0 );
172  bool subobjStarted() const { return _fieldName != 0; }
173 
174  private:
175  const char * _fieldName;
176  BSONObjBuilder * _builder;
177 
178  bool haveSubobj() const { return _subobj.get() != 0; }
179  BSONObjBuilder *subobj();
180  auto_ptr< BSONObjBuilder > _subobj;
181  };
182 
188  public:
189  BSONSizeTracker() {
190  _pos = 0;
191  for ( int i=0; i<SIZE; i++ )
192  _sizes[i] = 512; // this is the default, so just be consistent
193  }
194 
195  ~BSONSizeTracker() {
196  }
197 
198  void reset() {
199  _pos = 0 ;
200  for ( int i = 0 ; i < SIZE ; ++i )
201  _sizes[i] = 512 ;
202  }
203 
204  void got( int size ) {
205  _sizes[_pos++] = size;
206  if ( _pos >= SIZE )
207  _pos = 0;
208  }
209 
213  int getSize() const {
214  int x = 16; // sane min
215  for ( int i=0; i<SIZE; i++ ) {
216  if ( _sizes[i] > x )
217  x = _sizes[i];
218  }
219  return x;
220  }
221 
222  private:
223  enum { SIZE = 10 };
224  int _pos;
225  int _sizes[SIZE];
226  };
227 
228 }