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  int getIntField(const char *name) const;
306 
310  bool getBoolField(const char *name) const;
311 
317  BSONObj extractFieldsUnDotted(BSONObj pattern) const;
318 
324  BSONObj extractFields(const BSONObj &pattern , bool fillWithNull=false)
325  const;
326 
327  BSONObj filterFieldsUndotted(const BSONObj &filter, bool inFilter)
328  const;
329 
330  BSONElement getFieldUsingIndexNames(const char *fieldName,
331  const BSONObj &indexKey) const;
332 
334  const char *objdata() const {
335  return _objdata;
336  }
338  int objsize() const
339  { return *(reinterpret_cast<const int*>(objdata())); }
340 
342  bool isValid() const ;
343 
347  bool okForStorage() const;
348 
350  bool isEmpty() const { return objsize() <= 5; }
351 
352  void dump() const;
353 
355  string hexDump() const;
356 
362  int woCompare(const BSONObj& r, const Ordering &o,
363  bool considerFieldName=true) const;
364 
370  int woCompare(const BSONObj& r, const BSONObj &ordering = BSONObj(),
371  bool considerFieldName=true) const;
372 
373  bool operator<( const BSONObj& other ) const
374  { return woCompare( other ) < 0; }
375  bool operator<=( const BSONObj& other ) const
376  { return woCompare( other ) <= 0; }
377  bool operator>( const BSONObj& other ) const
378  { return woCompare( other ) > 0; }
379  bool operator>=( const BSONObj& other ) const
380  { return woCompare( other ) >= 0; }
381 
386  int woSortOrder( const BSONObj& r , const BSONObj& sortKey ,
387  bool useDotted=false ) const;
388 
389  bool equal(const BSONObj& r) const;
390 
394  bool shallowEqual(const BSONObj& r) const {
395  int os = objsize();
396  if ( os == r.objsize() ) {
397  return (os == 0 || memcmp(objdata(),r.objdata(),os)==0);
398  }
399  return false;
400  }
401 
403  BSONElement firstElement() const { return BSONElement(objdata() + 4); }
404 
408  const char * firstElementFieldName() const {
409  const char *p = objdata() + 4;
410  return *p == EOO ? "" : p+1;
411  }
412 
418  bool getObjectID(BSONElement& e) const;
419 
421  int hash() const {
422  unsigned x = 0;
423  const char *p = objdata();
424  for ( int i = 0; i < objsize(); i++ )
425  x = x * 131 + p[i];
426  return (x & 0x7fffffff) | 0x8000000; // must be > 0
427  }
428 
429  // Return a version of this object where top level elements of types
430  // that are not part of the bson wire protocol are replaced with
431  // string identifier equivalents.
432  // TODO Support conversion of element types other than min and max.
433  BSONObj clientReadable() const;
434 
437  BSONObj replaceFieldNames( const BSONObj &obj ) const;
438 
440  bool valid() const;
441 
443  string md5() const;
444 
445  bool operator==( const BSONObj& other ) const { return equal( other ); }
446 
447  enum MatchType {
448  Equality = 0,
449  LT = 0x1,
450  LTE = 0x3,
451  GTE = 0x6,
452  GT = 0x4,
453  opIN = 0x8, // { x : { $in : [1,2,3] } }
454  NE = 0x9,
455  opSIZE = 0x0A,
456  opALL = 0x0B,
457  NIN = 0x0C,
458  opEXISTS = 0x0D,
459  opMOD = 0x0E,
460  opTYPE = 0x0F,
461  opREGEX = 0x10,
462  opOPTIONS = 0x11,
463  opELEM_MATCH = 0x12,
464  opNEAR = 0x13,
465  opWITHIN = 0x14,
466  opMAX_DISTANCE=0x15,
467  opISNULL=0x16
468  };
469 
471  void elems(vector<BSONElement> &) const;
473  void elems(list<BSONElement> &) const;
474 
484  template <class T>
485  void Vals(vector<T> &) const;
488  template <class T>
489  void Vals(list<T> &) const;
490 
493  template <class T>
494  void vals(vector<T> &) const;
497  template <class T>
498  void vals(list<T> &) const;
499 
500  friend class BSONObjIterator;
501  typedef BSONObjIterator iterator;
502 
509  BSONObjIterator begin() const;
510 
511  void appendSelfToBufBuilder(BufBuilder& b) const {
512  assert( objsize() );
513  b.appendBuf(reinterpret_cast<const void *>( objdata() ), objsize());
514  }
515 
518  void init(const char *data, bool check=true ) {
519  _objdata = data;
520  if ( check && !isValid() )
521  _assertInvalid();
522  }
523  public:
530  static void setJSCompatibility(bool compatible) {
531  _jsCompatibility = compatible;
532  }
533 
538  static bool getJSCompatibility() {
539  return _jsCompatibility;
540  }
541 
542  private:
543  SDB_EXPORT static bool _jsCompatibility;
544 
545  private:
546  const char *_objdata ;
547  holder_type _holder ;
548 
549  void _assertInvalid() const;
550 
551  void init( Holder *holder ) {
552  _holder = holder ; // holder is now managed by intrusive_ptr
553  init(_holder->data);
554  }
555 
556  void init( holder_type holder ) {
557  _holder = holder ;
558  init( _holder->data ) ;
559  }
560  };
561 
562  ostream& operator<<( ostream &s, const BSONObj &o );
563  ostream& operator<<( ostream &s, const BSONElement &e );
564 
565  StringBuilder& operator<<( StringBuilder &s, const BSONObj &o );
566  StringBuilder& operator<<( StringBuilder &s, const BSONElement &e );
567 
568 
569  struct BSONArray : BSONObj {
570  // Don't add anything other than forwarding constructors!!!
571  BSONArray(): BSONObj() {}
572  explicit BSONArray(const BSONObj& obj): BSONObj(obj) {}
573  };
574 
575 }