00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include <set>
00021 #include <list>
00022 #include <vector>
00023 #include "util/builder.h"
00024 #include "stringdata.h"
00025
00026 namespace mongo {
00027
00028 typedef set< BSONElement, BSONElementCmpWithoutField > BSONElementSet;
00029
00068 class BSONObj {
00069 public:
00070
00075 explicit BSONObj(const char *msgdata, bool ifree = false) {
00076 init(msgdata, ifree);
00077 }
00078
00079 explicit BSONObj(const Record *r);
00080
00082 BSONObj();
00083
00084 ~BSONObj() { _objdata = 0; }
00085
00114 bool isOwned() const { return _holder.get() != 0; }
00115
00116
00117 BSONObj getOwned() const;
00118
00120 BSONObj copy() const;
00121
00125 string toString( bool isArray = false, bool full=false ) const;
00126 void toString(StringBuilder& s, bool isArray = false, bool full=false ) const;
00127
00131 string jsonString( JsonStringFormat format = Strict, int pretty = 0 ) const;
00132
00134 int addFields(BSONObj& from, set<string>& fields);
00135
00139 int nFields() const;
00140
00142 int getFieldNames(set<string>& fields) const;
00143
00147 BSONElement getFieldDotted(const char *name) const;
00151 BSONElement getFieldDotted(const string& name) const {
00152 return getFieldDotted( name.c_str() );
00153 }
00154
00157 void getFieldsDotted(const StringData& name, BSONElementSet &ret ) const;
00161 BSONElement getFieldDottedOrArray(const char *&name) const;
00162
00166 BSONElement getField(const StringData& name) const;
00167
00171 BSONElement operator[] (const char *field) const {
00172 return getField(field);
00173 }
00174
00175 BSONElement operator[] (const string& field) const {
00176 return getField(field);
00177 }
00178
00179 BSONElement operator[] (int field) const {
00180 StringBuilder ss;
00181 ss << field;
00182 string s = ss.str();
00183 return getField(s.c_str());
00184 }
00185
00187 bool hasField( const char * name ) const { return ! getField( name ).eoo(); }
00188
00190 const char * getStringField(const char *name) const;
00191
00193 BSONObj getObjectField(const char *name) const;
00194
00196 int getIntField(const char *name) const;
00197
00199 bool getBoolField(const char *name) const;
00200
00206 BSONObj extractFieldsUnDotted(BSONObj pattern) const;
00207
00213 BSONObj extractFields(const BSONObj &pattern , bool fillWithNull=false) const;
00214
00215 BSONObj filterFieldsUndotted(const BSONObj &filter, bool inFilter) const;
00216
00217 BSONElement getFieldUsingIndexNames(const char *fieldName, const BSONObj &indexKey) const;
00218
00220 const char *objdata() const {
00221 return _objdata;
00222 }
00224 int objsize() const { return *(reinterpret_cast<const int*>(objdata())); }
00225
00227 bool isValid();
00228
00232 bool okForStorage() const;
00233
00235 bool isEmpty() const { return objsize() <= 5; }
00236
00237 void dump() const;
00238
00240 string hexDump() const;
00241
00247 int woCompare(const BSONObj& r, const Ordering &o,
00248 bool considerFieldName=true) const;
00249
00255 int woCompare(const BSONObj& r, const BSONObj &ordering = BSONObj(),
00256 bool considerFieldName=true) const;
00257
00258
00259 bool operator<( const BSONObj& other ) const { return woCompare( other ) < 0; }
00260 bool operator<=( const BSONObj& other ) const { return woCompare( other ) <= 0; }
00261 bool operator>( const BSONObj& other ) const { return woCompare( other ) > 0; }
00262 bool operator>=( const BSONObj& other ) const { return woCompare( other ) >= 0; }
00263
00267 int woSortOrder( const BSONObj& r , const BSONObj& sortKey , bool useDotted=false ) const;
00268
00272 bool woEqual(const BSONObj& r) const {
00273 int os = objsize();
00274 if ( os == r.objsize() ) {
00275 return (os == 0 || memcmp(objdata(),r.objdata(),os)==0);
00276 }
00277 return false;
00278 }
00279
00281 BSONElement firstElement() const { return BSONElement(objdata() + 4); }
00282
00284 bool hasElement(const char *name) const;
00285
00291 bool getObjectID(BSONElement& e) const;
00292
00294 int hash() const {
00295 unsigned x = 0;
00296 const char *p = objdata();
00297 for ( int i = 0; i < objsize(); i++ )
00298 x = x * 131 + p[i];
00299 return (x & 0x7fffffff) | 0x8000000;
00300 }
00301
00302
00303
00304
00305
00306 BSONObj clientReadable() const;
00307
00310 BSONObj replaceFieldNames( const BSONObj &obj ) const;
00311
00313 bool valid() const;
00314
00316 string md5() const;
00317
00318 bool operator==( const BSONObj& other ) const {
00319 return woCompare( other ) == 0;
00320 }
00321
00322 enum MatchType {
00323 Equality = 0,
00324 LT = 0x1,
00325 LTE = 0x3,
00326 GTE = 0x6,
00327 GT = 0x4,
00328 opIN = 0x8,
00329 NE = 0x9,
00330 opSIZE = 0x0A,
00331 opALL = 0x0B,
00332 NIN = 0x0C,
00333 opEXISTS = 0x0D,
00334 opMOD = 0x0E,
00335 opTYPE = 0x0F,
00336 opREGEX = 0x10,
00337 opOPTIONS = 0x11,
00338 opELEM_MATCH = 0x12,
00339 opNEAR = 0x13,
00340 opWITHIN = 0x14,
00341 opMAX_DISTANCE=0x15
00342 };
00343
00345 void elems(vector<BSONElement> &) const;
00347 void elems(list<BSONElement> &) const;
00348
00357 template <class T>
00358 void Vals(vector<T> &) const;
00360 template <class T>
00361 void Vals(list<T> &) const;
00362
00364 template <class T>
00365 void vals(vector<T> &) const;
00367 template <class T>
00368 void vals(list<T> &) const;
00369
00370 friend class BSONObjIterator;
00371 typedef BSONObjIterator iterator;
00372
00379 BSONObjIterator begin();
00380
00381 void appendSelfToBufBuilder(BufBuilder& b) const {
00382 assert( objsize() );
00383 b.appendBuf(reinterpret_cast<const void *>( objdata() ), objsize());
00384 }
00385
00386 private:
00387 class Holder {
00388 public:
00389 Holder( const char *objdata ) :
00390 _objdata( objdata ) {
00391 }
00392 ~Holder() {
00393 free((void *)_objdata);
00394 _objdata = 0;
00395 }
00396 private:
00397 const char *_objdata;
00398 };
00399
00400 const char *_objdata;
00401 boost::shared_ptr< Holder > _holder;
00402
00403 void _assertInvalid() const;
00404 void init(const char *data, bool ifree) {
00405 if ( ifree )
00406 _holder.reset( new Holder( data ) );
00407 _objdata = data;
00408 if ( !isValid() )
00409 _assertInvalid();
00410 }
00411 };
00412
00413 ostream& operator<<( ostream &s, const BSONObj &o );
00414 ostream& operator<<( ostream &s, const BSONElement &e );
00415
00416 StringBuilder& operator<<( StringBuilder &s, const BSONObj &o );
00417 StringBuilder& operator<<( StringBuilder &s, const BSONElement &e );
00418
00419
00420 struct BSONArray : BSONObj {
00421
00422 BSONArray(): BSONObj() {}
00423 explicit BSONArray(const BSONObj& obj): BSONObj(obj) {}
00424 };
00425
00426 }