22 #if defined (SDB_ENGINE) || defined (SDB_CLIENT)
30 #include "lib/atomic_int.h"
31 #include "util/builder.h"
32 #include "stringdata.h"
34 #include "bsonnoncopyable.h"
35 #include "bsonintrusiveptr.h"
36 #include "bsonDecimal.h"
43 typedef set< BSONElement, BSONElementCmpWithoutField > BSONElementSet;
46 typedef multiset< BSONElement, BSONElementCmpWithoutField > BSONElementMSet;
89 #if defined (SDB_ENGINE) || defined (SDB_CLIENT)
90 class BSONObj :
public SDBObject {
101 mongo::AtomicUInt refCount ;
105 void zeroRef() { refCount.zero() ; }
106 void addRef () { refCount++ ; }
109 #if defined(_DEBUG) // cant use dassert or DEV here
111 assert( (
int)refCount > 0 ) ;
115 const char* dataptr()
const {
return &data[0] ; }
116 int datasize()
const {
return *(
int*)dataptr() ; }
118 static int refLen() {
return sizeof( mongo::AtomicUInt ) ; }
120 typedef bson_intrusive_ptr< Holder, TrivialAllocator > holder_type ;
127 explicit BSONObj(
const char *msgdata,
bool check=
true) {
129 init(msgdata, check);
191 return _holder.get() != 0;
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
214 void toString(StringBuilder& s,
bool isArray =
false,
bool full=
false )
220 string jsonString( JsonStringFormat format = Strict,
int pretty = 0 )
254 void getFieldsDotted(
const StringData& name, BSONElementSet &ret)
const;
255 void getFieldsDotted(
const StringData& name, BSONElementMSet &ret)
const;
333 BSONObj filterFieldsUndotted(
const BSONObj &filter,
bool inFilter)
336 BSONElement getFieldUsingIndexNames(
const char *fieldName,
337 const BSONObj &indexKey)
const;
345 {
return *(
reinterpret_cast<const int*
>(
objdata())); }
369 bool considerFieldName=
true)
const;
377 bool considerFieldName=
true)
const;
379 bool operator<(
const BSONObj& other )
const
381 bool operator<=(
const BSONObj& other )
const
383 bool operator>(
const BSONObj& other )
const
385 bool operator>=(
const BSONObj& other )
const
393 bool useDotted=
false )
const;
395 bool equal(
const BSONObj& r)
const;
402 if ( os == r.objsize() ) {
403 return (os == 0 || memcmp(
objdata(),r.objdata(),os)==0);
416 return *p == EOO ?
"" : p+1;
430 for (
int i = 0; i <
objsize(); i++ )
432 return (x & 0x7fffffff) | 0x8000000;
439 BSONObj clientReadable()
const;
451 bool operator==(
const BSONObj& other )
const {
return equal( other ); }
477 void elems(vector<BSONElement> &)
const;
479 void elems(list<BSONElement> &)
const;
491 void Vals(vector<T> &)
const;
495 void Vals(list<T> &)
const;
500 void vals(vector<T> &)
const;
504 void vals(list<T> &)
const;
506 friend class BSONObjIterator;
507 typedef BSONObjIterator iterator;
515 BSONObjIterator
begin()
const;
517 void appendSelfToBufBuilder(BufBuilder& b)
const {
519 b.appendBuf(reinterpret_cast<const void *>(
objdata() ),
objsize());
524 void init(
const char *data,
bool check=
true ) {
537 _jsCompatibility = compatible;
545 return _jsCompatibility;
549 SDB_EXPORT
static bool _jsCompatibility;
552 const char *_objdata ;
553 holder_type _holder ;
555 void _assertInvalid()
const;
557 void init( Holder *holder ) {
562 void init( holder_type holder ) {
564 init( _holder->data ) ;
568 ostream& operator<<( ostream &s,
const BSONObj &o );
569 ostream& operator<<( ostream &s,
const BSONElement &e );
571 StringBuilder& operator<<( StringBuilder &s,
const BSONObj &o );
572 StringBuilder& operator<<( StringBuilder &s,
const BSONElement &e );