00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 namespace mongo {
00021
00022 int getGtLtOp(const BSONElement& e);
00023
00024 struct BSONElementCmpWithoutField {
00025 bool operator()( const BSONElement &l, const BSONElement &r ) const {
00026 return l.woCompare( r, false ) < 0;
00027 }
00028 };
00029
00030 class BSONObjCmp {
00031 public:
00032 BSONObjCmp( const BSONObj &_order = BSONObj() ) : order( _order ) {}
00033 bool operator()( const BSONObj &l, const BSONObj &r ) const {
00034 return l.woCompare( r, order ) < 0;
00035 }
00036 private:
00037 BSONObj order;
00038 };
00039
00040 class BSONObjCmpDefaultOrder : public BSONObjCmp {
00041 public:
00042 BSONObjCmpDefaultOrder() : BSONObjCmp( BSONObj() ) {}
00043 };
00044
00045 typedef set< BSONObj, BSONObjCmpDefaultOrder > BSONObjSetDefaultOrder;
00046
00047 enum FieldCompareResult {
00048 LEFT_SUBFIELD = -2,
00049 LEFT_BEFORE = -1,
00050 SAME = 0,
00051 RIGHT_BEFORE = 1 ,
00052 RIGHT_SUBFIELD = 2
00053 };
00054
00055 FieldCompareResult compareDottedFieldNames( const string& l , const string& r );
00056
00070 #define BSON(x) (( mongo::BSONObjBuilder(64) << x ).obj())
00071
00077 #define BSON_ARRAY(x) (( mongo::BSONArrayBuilder() << x ).arr())
00078
00079
00080
00081
00082
00083 extern struct GENOIDLabeler { } GENOID;
00084
00085
00086
00087
00088
00089 extern struct DateNowLabeler { } DATENOW;
00090
00091
00092
00093
00094
00095 extern struct MinKeyLabeler { } MINKEY;
00096 extern struct MaxKeyLabeler { } MAXKEY;
00097
00098
00099 class Labeler {
00100 public:
00101 struct Label {
00102 Label( const char *l ) : l_( l ) {}
00103 const char *l_;
00104 };
00105 Labeler( const Label &l, BSONObjBuilderValueStream *s ) : l_( l ), s_( s ) {}
00106 template<class T>
00107 BSONObjBuilder& operator<<( T value );
00108
00109
00110
00111
00112
00113
00114 BSONObjBuilder& operator<<( const BSONElement& e );
00115 private:
00116 const Label &l_;
00117 BSONObjBuilderValueStream *s_;
00118 };
00119
00120 extern Labeler::Label GT;
00121 extern Labeler::Label GTE;
00122 extern Labeler::Label LT;
00123 extern Labeler::Label LTE;
00124 extern Labeler::Label NE;
00125 extern Labeler::Label SIZE;
00126
00127
00128
00129
00130 inline BSONObj OR(const BSONObj& a, const BSONObj& b);
00131 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c);
00132 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d);
00133 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e);
00134 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f);
00135
00136
00137
00138 class BSONObjBuilderValueStream : public boost::noncopyable {
00139 public:
00140 friend class Labeler;
00141 BSONObjBuilderValueStream( BSONObjBuilder * builder );
00142
00143 BSONObjBuilder& operator<<( const BSONElement& e );
00144
00145 template<class T>
00146 BSONObjBuilder& operator<<( T value );
00147
00148 BSONObjBuilder& operator<<(DateNowLabeler& id);
00149
00150 BSONObjBuilder& operator<<(MinKeyLabeler& id);
00151 BSONObjBuilder& operator<<(MaxKeyLabeler& id);
00152
00153 Labeler operator<<( const Labeler::Label &l );
00154
00155 void endField( const char *nextFieldName = 0 );
00156 bool subobjStarted() const { return _fieldName != 0; }
00157
00158 private:
00159 const char * _fieldName;
00160 BSONObjBuilder * _builder;
00161
00162 bool haveSubobj() const { return _subobj.get() != 0; }
00163 BSONObjBuilder *subobj();
00164 auto_ptr< BSONObjBuilder > _subobj;
00165 };
00166
00170 class BSONSizeTracker {
00171 public:
00172 BSONSizeTracker() {
00173 _pos = 0;
00174 for ( int i=0; i<SIZE; i++ )
00175 _sizes[i] = 512;
00176 }
00177
00178 ~BSONSizeTracker() {
00179 }
00180
00181 void got( int size ) {
00182 _sizes[_pos++] = size;
00183 if ( _pos >= SIZE )
00184 _pos = 0;
00185 }
00186
00190 int getSize() const {
00191 int x = 16;
00192 for ( int i=0; i<SIZE; i++ ) {
00193 if ( _sizes[i] > x )
00194 x = _sizes[i];
00195 }
00196 return x;
00197 }
00198
00199 private:
00200 enum { SIZE = 10 };
00201 int _pos;
00202 int _sizes[SIZE];
00203 };
00204
00205 }