SequoiaDB
 All Classes Namespaces Files Functions Macros Pages
bsonobj.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 #if defined (SDB_ENGINE) || defined (SDB_CLIENT)
23 #include "core.hpp"
24 #include "oss.hpp"
25 #endif
26 //#include <boost/intrusive_ptr.hpp>
27 #include <set>
28 #include <list>
29 #include <vector>
30 #include "lib/atomic_int.h"
31 #include "util/builder.h"
32 #include "stringdata.h"
33 #include "bsonelement.h"
34 #include "bsonnoncopyable.h"
35 #include "bsonintrusiveptr.h"
36 #include "bsonDecimal.h"
40 namespace bson {
43  typedef set< BSONElement, BSONElementCmpWithoutField > BSONElementSet;
46  typedef multiset< BSONElement, BSONElementCmpWithoutField > BSONElementMSet;
47 
89 #if defined (SDB_ENGINE) || defined (SDB_CLIENT)
90  class BSONObj : public SDBObject {
91 #else
92  class BSONObj {
93 #endif
94 
95  public:
96 #pragma pack(1)
97  class Holder : bsonnoncopyable
98  {
99  private:
100  Holder() ; // this class should never be explicitly created
101  mongo::AtomicUInt refCount ;
102  public:
103  char data[4]; // start of object
104 
105  void zeroRef() { refCount.zero() ; }
106  void addRef () { refCount++ ; }
107  int decRef ()
108  {
109 #if defined(_DEBUG) // cant use dassert or DEV here
110  // make sure we haven't already freed the buffer
111  assert( (int)refCount > 0 ) ;
112 #endif
113  return --refCount ;
114  }
115  const char* dataptr() const { return &data[0] ; }
116  int datasize() const { return *(int*)dataptr() ; }
117 
118  static int refLen() { return sizeof( mongo::AtomicUInt ) ; }
119  } ;
120  typedef bson_intrusive_ptr< Holder, TrivialAllocator > holder_type ;
121 #pragma pack()
122 
123  public:
127  explicit BSONObj(const char *msgdata, bool check=true) {
128  //_holder = NULL ;
129  init(msgdata, check);
130  }
131 
136  /*explicit BSONObj(Holder* holder) {
137  init( holder ) ;
138  }*/
139 
140  explicit BSONObj( holder_type holder ) {
141  init( holder ) ;
142  }
143 
145  BSONObj();
146 
147  ~BSONObj()
148  {
149  /*defensive:*/
150  _objdata = 0;
151  }
152 
189  bool isOwned() const
190  {
191  return _holder.get() != 0;
192  //return _holder != 0 ;
193  }
194 
198  BSONObj getOwned() const;
199 
201  BSONObj copy() const;
202 
207  string toString( bool isArray = false, bool full=false,
208  bool noThrow = true ) const;
209 #if defined ( SDB_ENGINE ) || defined ( SDB_FMP ) || defined ( SDB_TOOL )
210  ossPoolString toPoolString( bool isArray = false, bool full=false,
211  bool noThrow = true ) const ;
212 #endif //SDB_ENGINE || SDB_FMP || SDB_TOOL
213 
214  void toString(StringBuilder& s, bool isArray = false, bool full=false )
215  const;
216 
220  string jsonString( JsonStringFormat format = Strict, int pretty = 0 )
221  const;
222 
224  int addFields(BSONObj& from, set<string>& fields); /* returns n added */
225 
229  int nFields() const;
230 
233  int getFieldNames(set<string>& fields) const;
234 
239  BSONElement getFieldDotted(const char *name) const;
244  BSONElement getFieldDotted(const string& name) const {
245  return getFieldDotted( name.c_str() );
246  }
247 
251  void getFieldsDotted(const StringData& name, BSONElementSet &ret) const;
252  void getFieldsDotted(const StringData& name, BSONElementMSet &ret)const;
253 
257  BSONElement getFieldDottedOrArray(const char *&name) const;
258 
262  BSONElement getField(const StringData& name) const;
263  BSONElement getField(const char *name) const;
264 
271  void getFields(unsigned n, const char **fieldNames, BSONElement *fields) const;
272 
276  BSONElement operator[] (const char *field) const {
277  return getField(field);
278  }
279 
280  BSONElement operator[] (const string& field) const {
281  return getField(field);
282  }
283 
284  BSONElement operator[] (int field) const {
285  StringBuilder ss;
286  ss << field;
287  string s = ss.str();
288  return getField(s.c_str());
289  }
290 
292  bool hasField( const char * name ) const
293  { return !getField(name).eoo(); }
295  bool hasElement(const char *name) const
296  { return hasField(name); }
297 
299  const char * getStringField(const char *name) const;
300 
302  BSONObj getObjectField(const char *name) const;
303 
305  BSONObj getObjectField( const StringData &name ) const ;
306 
308  int getIntField(const char *name) const;
309 
313  bool getBoolField(const char *name) const;
314 
320  BSONObj extractFieldsUnDotted(BSONObj pattern) const;
321 
327  BSONObj extractFields(const BSONObj &pattern , bool fillWithNull=false)
328  const;
329 
330  BSONObj filterFieldsUndotted(const BSONObj &filter, bool inFilter)
331  const;
332 
333  BSONElement getFieldUsingIndexNames(const char *fieldName,
334  const BSONObj &indexKey) const;
335 
337  const char *objdata() const {
338  return _objdata;
339  }
341  int objsize() const
342  { return *(reinterpret_cast<const int*>(objdata())); }
343 
345  bool isValid() const ;
346 
350  bool okForStorage() const;
351 
353  bool isEmpty() const { return objsize() <= 5; }
354 
355  void dump() const;
356 
358  string hexDump() const;
359 
365  int woCompare(const BSONObj& r, const Ordering &o,
366  bool considerFieldName=true) const;
367 
373  int woCompare(const BSONObj& r, const BSONObj &ordering = BSONObj(),
374  bool considerFieldName=true) const;
375 
376  bool operator<( const BSONObj& other ) const
377  { return woCompare( other ) < 0; }
378  bool operator<=( const BSONObj& other ) const
379  { return woCompare( other ) <= 0; }
380  bool operator>( const BSONObj& other ) const
381  { return woCompare( other ) > 0; }
382  bool operator>=( const BSONObj& other ) const
383  { return woCompare( other ) >= 0; }
384 
389  int woSortOrder( const BSONObj& r , const BSONObj& sortKey ,
390  bool useDotted=false ) const;
391 
392  bool equal(const BSONObj& r) const;
393 
397  bool shallowEqual(const BSONObj& r) const {
398  int os = objsize();
399  if ( os == r.objsize() ) {
400  return (os == 0 || memcmp(objdata(),r.objdata(),os)==0);
401  }
402  return false;
403  }
404 
406  BSONElement firstElement() const { return BSONElement(objdata() + 4); }
407 
411  const char * firstElementFieldName() const {
412  const char *p = objdata() + 4;
413  return *p == EOO ? "" : p+1;
414  }
415 
421  bool getObjectID(BSONElement& e) const;
422 
424  int hash() const {
425  unsigned x = 0;
426  const char *p = objdata();
427  for ( int i = 0; i < objsize(); i++ )
428  x = x * 131 + p[i];
429  return (x & 0x7fffffff) | 0x8000000; // must be > 0
430  }
431 
432  // Return a version of this object where top level elements of types
433  // that are not part of the bson wire protocol are replaced with
434  // string identifier equivalents.
435  // TODO Support conversion of element types other than min and max.
436  BSONObj clientReadable() const;
437 
440  BSONObj replaceFieldNames( const BSONObj &obj ) const;
441 
443  bool valid() const;
444 
446  string md5() const;
447 
448  bool operator==( const BSONObj& other ) const { return equal( other ); }
449 
450  enum MatchType {
451  Equality = 0,
452  LT = 0x1,
453  LTE = 0x3,
454  GTE = 0x6,
455  GT = 0x4,
456  opIN = 0x8, // { x : { $in : [1,2,3] } }
457  NE = 0x9,
458  opSIZE = 0x0A,
459  opALL = 0x0B,
460  NIN = 0x0C,
461  opEXISTS = 0x0D,
462  opMOD = 0x0E,
463  opTYPE = 0x0F,
464  opREGEX = 0x10,
465  opOPTIONS = 0x11,
466  opELEM_MATCH = 0x12,
467  opNEAR = 0x13,
468  opWITHIN = 0x14,
469  opMAX_DISTANCE=0x15,
470  opISNULL=0x16
471  };
472 
474  void elems(vector<BSONElement> &) const;
476  void elems(list<BSONElement> &) const;
477 
487  template <class T>
488  void Vals(vector<T> &) const;
491  template <class T>
492  void Vals(list<T> &) const;
493 
496  template <class T>
497  void vals(vector<T> &) const;
500  template <class T>
501  void vals(list<T> &) const;
502 
503  friend class BSONObjIterator;
504  typedef BSONObjIterator iterator;
505 
512  BSONObjIterator begin() const;
513 
514  void appendSelfToBufBuilder(BufBuilder& b) const {
515  assert( objsize() );
516  b.appendBuf(reinterpret_cast<const void *>( objdata() ), objsize());
517  }
518 
521  void init(const char *data, bool check=true ) {
522  _objdata = data;
523  if ( check && !isValid() )
524  _assertInvalid();
525  }
526  public:
533  static void setJSCompatibility(bool compatible) {
534  _jsCompatibility = compatible;
535  }
536 
541  static bool getJSCompatibility() {
542  return _jsCompatibility;
543  }
544 
545  private:
546  SDB_EXPORT static bool _jsCompatibility;
547 
548  private:
549  const char *_objdata ;
550  holder_type _holder ;
551 
552  void _assertInvalid() const;
553 
554  void init( Holder *holder ) {
555  _holder = holder ; // holder is now managed by intrusive_ptr
556  init(_holder->data);
557  }
558 
559  void init( holder_type holder ) {
560  _holder = holder ;
561  init( _holder->data ) ;
562  }
563  };
564 
565  ostream& operator<<( ostream &s, const BSONObj &o );
566  ostream& operator<<( ostream &s, const BSONElement &e );
567 
568  StringBuilder& operator<<( StringBuilder &s, const BSONObj &o );
569  StringBuilder& operator<<( StringBuilder &s, const BSONElement &e );
570 
571 
572  struct BSONArray : BSONObj {
573  // Don't add anything other than forwarding constructors!!!
574  BSONArray(): BSONObj() {}
575  explicit BSONArray(const BSONObj& obj): BSONObj(obj) {}
576  };
577 
578 }