22 #if defined (SDB_ENGINE)
23 #include "utilPooledObject.hpp"
24 #elif defined( SDB_CLIENT )
34 #include "bsonnoncopyable.h"
35 #include "bsonDecimal.h"
44 #pragma warning( disable : 4355 )
55 const T& value()
const {
return _t; }
56 const string& name()
const {
return _name; }
66 BSONField(
const string& name ,
const string& longName=
"" )
67 : _name(name), _longName(longName) {}
68 const string& name()
const {
return _name; }
69 operator string()
const {
return _name; }
76 {
return query(
"$gt" , t ); }
78 {
return query(
"$lt" , t ); }
98 : isClientReadable(
false ),
105 : isClientReadable( isClientReadable ),
106 isAbbrevMode( isAbbrevMode )
114 isClientReadable = false ;
115 isAbbrevMode = false ;
118 bool isEnabled()
const
120 return isClientReadable || isAbbrevMode ;
124 bool isClientReadable ;
131 #if defined (SDB_ENGINE)
132 class BSONObjBuilder : bsonnoncopyable,
public engine::utilPooledObject{
133 #elif defined( SDB_CLIENT )
142 _buf(initsize + sizeof(unsigned), BSONObjMaxInternalSize, BSON_INFO_STR ),
143 _offset( sizeof(unsigned) ), _s( this ) , _tracker(0) , _doneCalled(false) {
144 _orgReserve = _b.getReserveBytes() ;
149 _b.reserveBytes(1,
true);
157 _buf( 0, BSONObjMaxInternalSize, BSON_INFO_STR ),
158 _offset( baseBuilder.len() ), _s( this ) , _tracker(0) , _doneCalled(false) {
159 _orgReserve = _b.getReserveBytes() ;
162 _b.reserveBytes(1,
true);
166 _buf(tracker.getSize() + sizeof(unsigned), BSONObjMaxInternalSize, BSON_INFO_STR ),
167 _offset( sizeof(unsigned) ),
168 _s( this ) , _tracker( (
BSONSizeTracker*)(&tracker) ) , _doneCalled(false) {
169 _orgReserve = _b.getReserveBytes() ;
173 _b.reserveBytes(1,
true);
180 if ( !_doneCalled && !owned() ) {
184 _b.setlen( _offset ) ;
185 _b.setReserveBytes( _orgReserve ) ;
191 _b.setlen( _offset ) ;
192 _b.setReserveBytes( _orgReserve ) ;
196 _doneCalled = false ;
198 _b.reserveBytes(1,
true) ;
212 bool isEmpty()
const {
214 return ( _b.len() - _offset ) <= (
int)(
sizeof(unsigned) + 4) ?
true : false ;
216 return ( _b.len() - _offset ) <= 4 ?
true :
false ;
220 BSONObjBuilder& appendElements(BSONObj x);
224 BSONObjBuilder& appendElementsWithoutName(BSONObj x);
228 BSONObjBuilder& appendElementsUnique( BSONObj x );
234 _b.appendBuf((
void*) e.rawdata(), e.
size());
240 const StringData& fieldName) {
243 _b.appendNum((
char) e.
type());
244 _b.appendStr(fieldName);
251 _b.appendNum((
char) Object);
252 _b.appendStr(fieldName);
261 return appendEx( element.
fieldName(), element, option ) ;
278 if ( option.isEnabled() )
281 subBuilder.
appendEx( subObject, option ) ;
282 subBuilder.doneFast() ;
286 append( fieldName, subObject ) ;
293 const char * objdata ,
int size = 0 ) {
296 size = *((
int*)objdata);
299 assert( size > 4 && size < 100000000 );
301 _b.appendNum((
char) Object);
302 _b.appendStr(fieldName);
303 _b.appendBuf((
void*)objdata, size );
319 _b.appendNum((
char) Object);
320 _b.appendStr(fieldName);
329 _b.appendNum((
char) Array);
330 _b.appendStr(fieldName);
335 return appendArray(fieldName, arr);
341 _b.appendNum((
char) Array);
342 _b.appendStr(fieldName);
348 _b.appendNum((
char) Bool);
349 _b.appendStr(fieldName);
350 _b.appendNum((
char) (val?1:0));
356 _b.appendNum((
char) Bool);
357 _b.appendStr(fieldName);
358 _b.appendNum((
char) (val?1:0));
364 _b.appendNum((
char) NumberInt);
365 _b.appendStr(fieldName);
372 return append(fieldName, (
int) n);
377 _b.appendNum((
char) NumberLong);
378 _b.appendStr(fieldName);
389 if ( x > -( (numeric_limits<int>::max)() / 2 ) )
390 append( fieldName , (
int)n );
392 append( fieldName , n );
401 return append( fieldName , n );
404 BSONObjBuilder& appendNumber(
const StringData& fieldName ,
double d ) {
405 return append( fieldName , d );
408 BSONObjBuilder& appendNumber(
const StringData& fieldName ,
size_t n ) {
409 static size_t maxInt = (size_t)pow( 2.0 , 30.0 );
412 append( fieldName , (
int)n );
414 append( fieldName , (
long long)n );
418 BSONObjBuilder& appendNumber(
const StringData& fieldName ,
420 static long long maxInt = (int)pow( 2.0 , 30.0 );
421 static long long maxDouble = (
long long)pow( 2.0 , 40.0 );
422 long long x = l > 0 ? -l : l;
424 append( fieldName , (
int)l );
425 else if ( x > -maxDouble )
426 append( fieldName , (
double)l );
428 append( fieldName , l );
434 _b.appendNum((
char) NumberDouble);
435 _b.appendStr(fieldName);
441 const bsonDecimal& decimal )
449 const short *digits = NULL ;
451 weight = decimal.getWeight() ;
452 typemod = decimal.getTypemod() ;
453 scale = decimal.getStorageScale() ;
454 ndigit = decimal.getNdigit() ;
455 digits = decimal.getDigits() ;
456 size = decimal.getSize() ;
459 _b.appendNum( (
char) NumberDecimal ) ;
460 _b.appendStr( fieldName ) ;
461 _b.appendNum( size ) ;
462 _b.appendNum( typemod ) ;
463 _b.appendNum( scale ) ;
464 _b.appendNum( weight ) ;
466 for ( i = 0 ; i < ndigit ; i++ )
468 _b.appendNum( digits[i] ) ;
474 bool appendDecimal(
const StringData& fieldName,
475 const StringData& strDecimal,
476 int precision,
int scale ) ;
478 bool appendDecimal(
const StringData& fieldName,
479 const StringData& strDecimal ) ;
484 bool appendAsNumber(
const StringData& fieldName ,
const StringData& data );
491 bool generateIfBlank =
false ) {
492 _b.appendNum((
char) jstOID);
493 _b.appendStr(fieldName);
495 _b.appendBuf( (
void *) oid, 12 );
498 if ( generateIfBlank )
502 _b.appendBuf( (
void *) &tmp, 12 );
513 _b.appendNum((
char) jstOID);
514 _b.appendStr(fieldName);
515 _b.appendBuf( (
void *) &oid, 12 );
524 return append(
"_id", OID::gen());
532 _b.appendNum((
char) Date);
533 _b.appendStr(fieldName);
534 _b.appendNum(static_cast<unsigned long long>(dt) * 1000);
544 #if defined(_DEBUG) && defined(MONGO_EXPOSE_MACROS)
545 if( dt > 0 && dt <= 0xffffffff ) {
548 log() <<
"DEV WARNING appendDate() called with a tiny "
549 "(but nonzero) date" << endl;
552 _b.appendNum((
char) Date);
553 _b.appendStr(fieldName);
558 return appendDate(fieldName, dt);
566 const StringData& regex,
const StringData& options =
"") {
567 _b.appendNum((
char) RegEx);
568 _b.appendStr(fieldName);
570 _b.appendStr(options);
575 const StringData& code) {
576 _b.appendNum((
char) Code);
577 _b.appendStr(fieldName);
578 _b.appendNum((
int) code.size()+1);
587 _b.appendNum((
char) String);
588 _b.appendStr(fieldName);
589 _b.appendNum((
int)sz);
590 _b.appendBuf(str, sz);
596 BSONObjBuilder& appendStrWithNoTerminating(
const StringData& fieldName,
const char *str,
599 _b.appendNum((
char) String);
600 _b.appendStr(fieldName);
601 _b.appendNum((
int)sz + 1);
602 _b.appendBuf(str, sz);
610 return append(fieldName, str, (
int) strlen(str)+1);
614 return append(fieldName, str.c_str(), (int) str.size()+1);
618 return append(fieldName, str.data(), (int) str.size()+1);
622 const StringData& symbol) {
623 _b.appendNum((
char) Symbol);
624 _b.appendStr(fieldName);
625 _b.appendNum((
int) symbol.size()+1);
626 _b.appendStr(symbol);
632 _b.appendNum( (
char) jstNULL );
633 _b.appendStr( fieldName );
639 _b.appendNum( (
char) MinKey );
640 _b.appendStr( fieldName );
646 _b.appendNum( (
char) Undefined );
647 _b.appendStr( fieldName );
653 _b.appendNum( (
char) MaxKey );
654 _b.appendStr( fieldName );
660 BSONObjBuilder& appendTimestamp(
const StringData& fieldName ) {
661 _b.appendNum( (
char) Timestamp );
662 _b.appendStr( fieldName );
663 _b.appendNum( (
long long) 0 );
678 _b.appendNum( (
char) Timestamp );
679 _b.appendStr( fieldName );
695 long long time ,
unsigned int inc );
702 const StringData& ns,
const OID &oid ) {
703 _b.appendNum( (
char) DBRef );
704 _b.appendStr( fieldName );
705 _b.appendNum( (
int) ns.size() + 1 );
707 _b.appendBuf( (
void *) &oid, 12 );
720 BinDataType type,
const char *data ) {
721 if ( type == ByteArrayDeprecated ) {
722 return appendBinDataArrayDeprecated( fieldName.data(), data, len );
725 _b.appendNum( (
char) BinData );
726 _b.appendStr( fieldName );
728 _b.appendNum( (
char) type );
729 _b.appendBuf( (
void *) data, len );
733 BSONObjBuilder& appendBinData(
const StringData& fieldName,
int len,
734 BinDataType type,
const unsigned char *data ) {
735 return appendBinData(fieldName, len, type, (
const char *) data);
745 const char * data ,
int len ) {
746 _b.appendNum( (
char) BinData );
747 _b.appendStr( fieldName );
748 _b.appendNum( len + 4 );
749 _b.appendNum( (
char)0x2 );
751 _b.appendBuf( (
void *) data, len );
759 const StringData& code,
const BSONObj &scope ) {
760 _b.appendNum( (
char) CodeWScope );
761 _b.appendStr( fieldName );
762 _b.appendNum( (
int )( 4 + 4 + code.size() + 1 + scope.
objsize() ));
763 _b.appendNum( (
int ) code.size() + 1 );
764 _b.appendStr( code );
775 void appendWhere(
const StringData& code,
const BSONObj &scope ) {
776 appendCodeWScope(
"$where" , code , scope );
783 void appendMinForType(
const StringData& fieldName ,
int type );
784 void appendMaxForType(
const StringData& fieldName ,
int type );
788 BSONObjBuilder& append(
const StringData& fieldName,
789 const vector< T >& vals );
792 BSONObjBuilder& append(
const StringData& fieldName,
793 const list< T >& vals );
797 BSONObjBuilder& append(
const StringData& fieldName,
const set< T >& vals );
806 massert( 10335 ,
"builder does not own memory", own );
808 bson_intrusive_ptr<BSONObj::Holder,BufBuilder::myAllocator> h ;
836 _b.setlen(_b.len()-1);
844 char* decouple(
int& l) {
855 void appendKeys(
const BSONObj& keyPattern ,
const BSONObj& values );
857 static string numStr(
int i ) {
886 massert( 10336 ,
"No subobject started", _s.subobjStarted() );
891 BSONObjBuilderValueStream& operator<<( const BSONField<T>& f ) {
892 _s.endField( f.name().c_str() );
897 BSONObjBuilder& operator<<( const BSONFieldValue<T>& v ) {
898 append( v.name().c_str() , v.value() );
905 bool owned()
const {
return &_b == &_buf; }
909 bool hasField(
const StringData& name )
const ;
911 int len()
const {
return _b.len(); }
913 BufBuilder& bb() {
return _b; }
918 return _b.buf() + _offset;
923 _b.claimReservedBytes(1);
924 _b.appendNum((
char) EOO);
926 *(
unsigned*)_b.buf() = 0 ;
928 char *data = _b.buf() + _offset;
929 int size = _b.len() - _offset;
930 *((
int*)data) = size;
932 _tracker->got( size );
936 StringData _genAbbrevStr( StackBufBuilder &builder,
937 const StringData &value ) ;
943 BSONObjBuilderValueStream _s;
944 BSONSizeTracker * _tracker;
947 static const string numStrs[100];
950 #if defined (SDB_ENGINE)
951 class BSONArrayBuilder : bsonnoncopyable,
public engine::utilPooledObject {
952 #elif defined( SDB_CLIENT )
953 class BSONArrayBuilder : bsonnoncopyable,
public SDBObject {
962 template <
typename T>
969 _b.appendTimestamp(num(),val);
974 _b.appendAs(e, num());
981 _b.appendEx( num(), e, option ) ;
985 template <
typename T>
991 _b.appendNull(num());
1000 BSONObj done() {
return _b.done(); }
1002 void doneFast() { _b.doneFast(); }
1004 void abandon() { _b.abandon() ; }
1006 template <
typename T>
1007 BSONArrayBuilder& append(
const StringData& name,
const T& x) {
1014 BufBuilder &subobjStart() {
return _b.subobjStart( num() ); }
1015 BufBuilder &subarrayStart() {
return _b.subarrayStart( num() ); }
1018 BufBuilder &subobjStart(
int pos) {
1020 return _b.subobjStart( num() );
1022 BufBuilder &subarrayStart(
int pos) {
1024 return _b.subarrayStart( num() );
1029 BufBuilder &subobjStart(
const StringData& name ) {
1031 return _b.subobjStart( num() );
1034 BufBuilder &subarrayStart(
const char *name ) {
1036 return _b.subarrayStart( num() );
1039 void appendArray(
const StringData& name, BSONObj subObj ) {
1041 _b.appendArray( num(), subObj );
1044 void appendAs(
const BSONElement &e,
const char *name) {
1049 int len()
const {
return _b.len(); }
1052 void fill(
const StringData& name ) {
1054 long int n = strtol( name.data(), &r, 10 );
1056 uasserted( 13048, (
string)
"can't append to array using string"
1057 " field name [" + name.data() +
"]" );
1061 void fill (
int upTo){
1063 append( nullElt() );
1066 static BSONElement nullElt() {
1067 static BSONObj n = nullObj();
1068 return n.firstElement();
1071 static BSONObj nullObj() {
1073 _b.appendNull(
"" );
1077 string num() {
return _b.numStr(_i++); }
1082 template <
class T >
1084 const vector< T >& vals ) {
1086 for (
unsigned int i = 0; i < vals.size(); ++i )
1087 arrBuilder.
append( numStr( i ), vals[ i ] );
1088 appendArray( fieldName, arrBuilder.
done() );
1092 template <
class L >
1096 for(
typename L::const_iterator i = vals.begin(); i != vals.end(); i++ )
1097 arrBuilder.
append( BSONObjBuilder::numStr(n++), *i );
1102 template <
class T >
1103 inline BSONObjBuilder& BSONObjBuilder::append(
const StringData& fieldName,
const list< T >& vals ) {
1104 return _appendIt< list< T > >( *
this, fieldName, vals );
1107 template <
class T >
1108 inline BSONObjBuilder& BSONObjBuilder::append(
const StringData& fieldName,
const set< T >& vals ) {
1109 return _appendIt< set< T > >( *
this, fieldName, vals );
1116 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c)
1118 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c,
1121 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c,
1122 const BSONObj& d,
const BSONObj& e)
1124 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c,
1125 const BSONObj& d,
const BSONObj& e,
const BSONObj& f)
1126 {
return BSON(
"$or" <<
BSON_ARRAY(a << b << c << d << e << f) ); }