25 #include <boost/static_assert.hpp>
30 #include "mongo/base/data_cursor.h"
31 #include "mongo/base/parse_number.h"
32 #include "mongo/bson/bson_field.h"
33 #include "mongo/bson/bsonelement.h"
34 #include "mongo/bson/bsonmisc.h"
35 #include "mongo/bson/bsonobj.h"
36 #include "mongo/bson/timestamp.h"
37 #include "mongo/client/export_macros.h"
43 #pragma warning( disable : 4355 )
54 , _buf(sizeof(
BSONObj::Holder) + initsize)
55 , _offset(sizeof(
BSONObj::Holder))
58 , _doneCalled(false) {
71 , _offset(baseBuilder.len())
74 , _doneCalled(false) {
82 , _buf(sizeof(
BSONObj::Holder) + tracker.getSize())
83 , _offset(sizeof(
BSONObj::Holder))
86 , _doneCalled(false) {
97 if ( !_doneCalled && _b.buf() && _buf.getSize() == 0 ) {
103 BSONObjBuilder& appendElements(BSONObj x);
106 BSONObjBuilder& appendElementsUnique( BSONObj x );
111 _b.appendBuf((
void*) e.rawdata(), e.
size());
118 _b.appendNum((
char) e.
type());
119 _b.appendStr(fieldName);
126 _b.appendNum((
char)
Object);
127 _b.appendStr(fieldName);
136 size = ConstDataView(objdata).readLE<
int>();
139 verify( size > 4 && size < 100000000 );
141 _b.appendNum((
char)
Object);
142 _b.appendStr(fieldName);
143 _b.appendBuf((
void*)objdata, size );
159 _b.appendNum((
char)
Object);
160 _b.appendStr(fieldName);
168 _b.appendNum((
char)
Array);
169 _b.appendStr(fieldName);
174 return appendArray(fieldName, arr);
180 _b.appendNum((
char)
Array);
181 _b.appendStr(fieldName);
187 _b.appendNum((
char)
Bool);
188 _b.appendStr(fieldName);
189 _b.appendNum((
char) (val?1:0));
195 _b.appendNum((
char)
Bool);
196 _b.appendStr(fieldName);
197 _b.appendNum((
char) (val?1:0));
204 _b.appendStr(fieldName);
211 return append(fieldName, (
int) n);
217 _b.appendStr(fieldName);
225 static const long long maxInt = (std::numeric_limits<int>::max)() / 2;
226 static const long long minInt = -maxInt;
227 if ( minInt < n && n < maxInt ) {
228 append( fieldName , static_cast<int>( n ) );
231 append( fieldName , n );
241 return append( fieldName , n );
244 BSONObjBuilder& appendNumber(
const StringData& fieldName ,
double d ) {
245 return append( fieldName , d );
248 BSONObjBuilder& appendNumber(
const StringData& fieldName ,
size_t n ) {
249 static const size_t maxInt = ( 1 << 30 );
252 append( fieldName, static_cast<int>( n ) );
254 append( fieldName, static_cast<long long>( n ) );
258 BSONObjBuilder& appendNumber(
const StringData& fieldName,
long long llNumber ) {
259 static const long long maxInt = ( 1LL << 30 );
260 static const long long minInt = -maxInt;
261 static const long long maxDouble = ( 1LL << 40 );
262 static const long long minDouble = -maxDouble;
264 if ( minInt < llNumber && llNumber < maxInt ) {
265 append( fieldName, static_cast<int>( llNumber ) );
267 else if ( minDouble < llNumber && llNumber < maxDouble ) {
268 append( fieldName, static_cast<double>( llNumber ) );
271 append( fieldName, llNumber );
280 _b.appendStr(fieldName);
288 bool appendAsNumber(
const StringData& fieldName ,
const std::string& data );
295 _b.appendNum((
char)
jstOID);
296 _b.appendStr(fieldName);
298 _b.appendBuf( oid->view().view(), OID::kOIDSize );
301 if ( generateIfBlank )
305 _b.appendBuf( tmp.view().view(), OID::kOIDSize );
316 _b.appendNum((
char)
jstOID);
317 _b.appendStr(fieldName);
318 _b.appendBuf( oid.view().view(), OID::kOIDSize );
327 return append(
"_id", OID::gen());
335 _b.appendNum((
char)
Date);
336 _b.appendStr(fieldName);
337 _b.appendNum(static_cast<unsigned long long>(dt) * 1000);
346 return appendDate(fieldName, dt);
354 _b.appendNum((
char)
RegEx);
355 _b.appendStr(fieldName);
357 _b.appendStr(options);
362 return appendRegex(fieldName, regex.pattern, regex.flags);
365 BSONObjBuilder& appendCode(
const StringData& fieldName,
const StringData& code) {
366 _b.appendNum((
char)
Code);
367 _b.appendStr(fieldName);
368 _b.appendNum((
int) code.size()+1);
373 BSONObjBuilder& append(
const StringData& fieldName,
const BSONCode& code) {
374 return appendCode(fieldName, code.code);
380 _b.appendNum((
char)
String);
381 _b.appendStr(fieldName);
382 _b.appendNum((
int)sz);
383 _b.appendBuf(str, sz);
388 return append(fieldName, str, (
int) strlen(str)+1);
392 return append(fieldName, str.c_str(), (int) str.size()+1);
396 _b.appendNum((
char)
String);
397 _b.appendStr(fieldName);
398 _b.appendNum((
int)str.size()+1);
399 _b.appendStr(str,
true);
403 BSONObjBuilder& appendSymbol(
const StringData& fieldName,
const StringData& symbol) {
404 _b.appendNum((
char)
Symbol);
405 _b.appendStr(fieldName);
406 _b.appendNum((
int) symbol.size()+1);
407 _b.appendStr(symbol);
411 BSONObjBuilder& append(
const StringData& fieldName,
const BSONSymbol& symbol) {
412 return appendSymbol(fieldName, symbol.symbol);
417 msgasserted(16234,
"Invalid call to appendNull in BSONObj Builder.");
422 _b.appendNum( (
char)
jstNULL );
423 _b.appendStr( fieldName );
429 _b.appendNum( (
char)
MinKey );
430 _b.appendStr( fieldName );
434 BSONObjBuilder& appendMaxKey(
const StringData& fieldName ) {
435 _b.appendNum( (
char)
MaxKey );
436 _b.appendStr( fieldName );
443 _b.appendStr( fieldName );
445 char buf[2 *
sizeof(uint32_t)];
447 cur.writeLEAndAdvance<>(ts.increment());
448 cur.writeLEAndAdvance<>(ts.seconds());
450 _b.appendBuf(buf,
sizeof(buf));
455 return appendTimestamp(fieldName, ts);
462 BSONObjBuilder& appendDBRef(
const StringData& fieldName,
const StringData& ns,
const OID &oid ) {
463 _b.appendNum( (
char)
DBRef );
464 _b.appendStr( fieldName );
465 _b.appendNum( (
int) ns.size() + 1 );
467 _b.appendBuf( oid.view().view(), OID::kOIDSize );
471 BSONObjBuilder& append(
const StringData& fieldName,
const BSONDBRef& dbref) {
472 return appendDBRef(fieldName, dbref.ns, dbref.oid);
483 _b.appendNum( (
char)
BinData );
484 _b.appendStr( fieldName );
486 _b.appendNum( (
char) type );
487 _b.appendBuf( data, len );
492 return appendBinData(fieldName, bd.length, bd.type, bd.data);
502 _b.appendNum( (
char)
BinData );
503 _b.appendStr( fieldName );
504 _b.appendNum( len + 4 );
505 _b.appendNum( (
char)0x2 );
507 _b.appendBuf( data, len );
516 _b.appendStr( fieldName );
517 _b.appendNum( (
int )( 4 + 4 + code.size() + 1 + scope.
objsize() ) );
518 _b.appendNum( (
int ) code.size() + 1 );
519 _b.appendStr( code );
525 return appendCodeWScope(fieldName, cws.code, cws.scope);
528 void appendUndefined(
const StringData& fieldName ) {
530 _b.appendStr( fieldName );
534 void appendWhere(
const StringData& code,
const BSONObj &scope ) {
535 appendCodeWScope(
"$where" , code , scope );
541 void appendMinForType(
const StringData& fieldName ,
int type );
542 void appendMaxForType(
const StringData& fieldName ,
int type );
546 BSONObjBuilder& append(
const StringData& fieldName,
const std::vector< T >& vals );
549 BSONObjBuilder& append(
const StringData& fieldName,
const std::list< T >& vals );
553 BSONObjBuilder& append(
const StringData& fieldName,
const std::set< T >& vals );
559 template <
class K,
class T >
560 BSONObjBuilder& append(
const StringData& fieldName,
const std::map< K, T >& vals );
568 massert( 10335 ,
"builder does not own memory", owned() );
570 char* buf = _b.buf();
595 _b.setlen(_b.len()-1);
615 void appendKeys(
const BSONObj& keyPattern ,
const BSONObj& values );
617 static std::string MONGO_CLIENT_FUNC numStr(
int i ) {
618 if (i>=0 && i<100 && numStrsReady)
635 massert( 10336 ,
"No subobject started", _s.subobjStarted() );
640 BSONObjBuilderValueStream& operator<<( const BSONField<T>& f ) {
641 _s.endField( f.name() );
646 BSONObjBuilder& operator<<( const BSONFieldValue<T>& v ) {
647 append( v.name(), v.value() );
651 BSONObjBuilder& operator<<(
const BSONElement& e ){
661 bool owned()
const {
return &_b == &_buf; }
665 bool hasField(
const StringData& name )
const ;
667 int len()
const {
return _b.len(); }
669 BufBuilder& bb() {
return _b; }
674 return _b.buf() + _offset;
678 _b.appendNum((
char)
EOO);
679 char *data = _b.buf() + _offset;
680 int size = _b.len() - _offset;
681 DataView(data).writeLE(size);
683 _tracker->got( size );
690 BSONObjBuilderValueStream _s;
691 BSONSizeTracker * _tracker;
694 static const std::string numStrs[100];
695 static bool numStrsReady;
704 template <
typename T>
711 _b.appendAs(e, num());
719 template <
typename T>
721 _b << num().c_str() << x;
726 _b.appendNull(num());
729 void appendUndefined() {
730 _b.appendUndefined(num());
738 BSONObj obj() {
return _b.obj(); }
740 BSONObj done() {
return _b.done(); }
742 void doneFast() { _b.doneFast(); }
745 BSONArrayBuilder& append(
const std::list< T >& vals );
748 BSONArrayBuilder& append(
const std::set< T >& vals );
751 BufBuilder &subobjStart() {
return _b.subobjStart( num() ); }
752 BufBuilder &subarrayStart() {
return _b.subarrayStart( num() ); }
754 BSONArrayBuilder& appendRegex(
const StringData& regex,
const StringData& options =
"") {
755 _b.appendRegex(num(), regex, options);
759 BSONArrayBuilder& appendBinData(
int len, BinDataType type,
const void* data) {
760 _b.appendBinData(num(), len, type, data);
764 BSONArrayBuilder& appendCode(
const StringData& code) {
765 _b.appendCode(num(), code);
769 BSONArrayBuilder& appendCodeWScope(
const StringData& code,
const BSONObj& scope) {
770 _b.appendCodeWScope(num(), code, scope);
774 BSONArrayBuilder& appendTimeT(time_t dt) {
775 _b.appendTimeT(num(), dt);
779 BSONArrayBuilder& appendDate(Date_t dt) {
780 _b.appendDate(num(), dt);
784 BSONArrayBuilder& appendBool(
bool val) {
785 _b.appendBool(num(), val);
789 bool isArray()
const {
793 int len()
const {
return _b.
len(); }
794 int arrSize()
const {
return _i; }
796 BufBuilder& bb() {
return _b.bb(); }
800 std::string num() {
return _b.numStr(_i++); }
808 for (
unsigned int i = 0; i < vals.size(); ++i )
809 arrBuilder.
append( numStr( i ), vals[ i ] );
818 for(
typename L::const_iterator i = vals.begin(); i != vals.end(); i++ )
819 arrBuilder.
append( BSONObjBuilder::numStr(n++), *i );
825 inline BSONObjBuilder&
BSONObjBuilder::append(
const StringData& fieldName,
const std::list< T >& vals ) {
826 return _appendIt< std::list< T > >( *
this, fieldName, vals );
831 return _appendIt< std::set< T > >( *
this, fieldName, vals );
834 template <
class K,
class T >
837 for(
typename std::map<K,T>::const_iterator i = vals.begin(); i != vals.end(); ++i ){
838 bob.
append(i->first, i->second);
847 for(
typename L::const_iterator i = vals.begin(); i != vals.end(); i++ )
853 inline BSONArrayBuilder& BSONArrayBuilder::append(
const std::list< T >& vals ) {
854 return _appendArrayIt< std::list< T > >( *
this, vals );
858 inline BSONArrayBuilder& BSONArrayBuilder::append(
const std::set< T >& vals ) {
859 return _appendArrayIt< std::set< T > >( *
this, vals );
863 inline BSONFieldValue<BSONObj> BSONField<T>::query(
const char * q ,
const T& t )
const {
866 return BSONFieldValue<BSONObj>( _name , b.obj() );
870 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b)
871 {
return BSON(
"$or" << BSON_ARRAY(a << b) ); }
872 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c)
873 {
return BSON(
"$or" << BSON_ARRAY(a << b << c) ); }
874 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c,
const BSONObj& d)
875 {
return BSON(
"$or" << BSON_ARRAY(a << b << c << d) ); }
876 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c,
const BSONObj& d,
const BSONObj& e)
877 {
return BSON(
"$or" << BSON_ARRAY(a << b << c << d << e) ); }
878 inline BSONObj OR(
const BSONObj& a,
const BSONObj& b,
const BSONObj& c,
const BSONObj& d,
const BSONObj& e,
const BSONObj& f)
879 {
return BSON(
"$or" << BSON_ARRAY(a << b << c << d << e << f) ); }
void abandon()
Make it look as if "done" has been called, so that our destructor is a no-op.
Definition: bsonobjbuilder.h:607
BufBuilder & subobjStart(const StringData &fieldName)
add header for a new subobject and return bufbuilder for writing to the subobject's body ...
Definition: bsonobjbuilder.h:158
BSONObjBuilder & append(const StringData &fieldName, long long n)
Append a NumberLong.
Definition: bsonobjbuilder.h:215
Definition: timestamp.h:23
end of object
Definition: bsontypes.h:42
Definition: bsonmisc.h:150
BSONArray arr()
destructive - ownership moves to returned BSONArray
Definition: bsonobjbuilder.h:737
int objsize() const
Definition: bsonobj.h:304
larger than all other types
Definition: bsontypes.h:82
BSONObjBuilder & appendOID(const StringData &fieldName, OID *oid=0, bool generateIfBlank=false)
Append a BSON Object ID (OID type).
Definition: bsonobjbuilder.h:294
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
int size(int maxLen) const
Size of the element.
Definition: bsonmisc.h:118
static BSONObj takeOwnership(char *holderPrefixedData)
Given a pointer to a region of un-owned memory containing BSON data, prefixed by sufficient space for...
Definition: bsonobj.h:524
BSONObj obj()
destructive The returned BSONObj will free the buffer when it is finished.
Definition: bsonobjbuilder.h:567
Object ID type.
Definition: oid.h:60
BSONObjBuilder & appendTimestamp(const StringData &fieldName, const Timestamp_t &ts=Timestamp_t())
Append a Timestamp element to the object.
Definition: bsonobjbuilder.h:441
const char * objdata() const
Definition: bsonobj.h:299
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition: bsonobjbuilder.h:109
Definition: bsonmisc.h:82
Updated to a Date with value next OpTime on insert.
Definition: bsontypes.h:76
BSONObjBuilder & appendBool(const StringData &fieldName, int val)
Append a boolean element.
Definition: bsonobjbuilder.h:186
BufBuilder & subarrayStart(const StringData &fieldName)
add header for a new subarray and return bufbuilder for writing to the subarray's body ...
Definition: bsonobjbuilder.h:179
BSONObjBuilder & append(const StringData &fieldName, const char *str)
Append a string element.
Definition: bsonobjbuilder.h:387
BSONObjBuilder & appendBinDataArrayDeprecated(const char *fieldName, const void *data, int len)
Subtype 2 is deprecated.
Definition: bsonobjbuilder.h:501
used in conjuction with BSONObjBuilder, allows for proper buffer size to prevent crazy memory usage ...
Definition: bsonmisc.h:240
BSONObjBuilder & appendNull(const StringData &fieldName)
Append a Null element to the object.
Definition: bsonobjbuilder.h:421
date type
Definition: bsontypes.h:60
boolean type
Definition: bsontypes.h:58
BSONObjBuilder & append(const StringData &fieldName, double n)
Append a double element.
Definition: bsonobjbuilder.h:278
smaller than all other types
Definition: bsontypes.h:40
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:49
BSONObj done()
Fetch the object we have built.
Definition: bsonobjbuilder.h:580
BSONObj asTempObj()
Peek at what is in the builder, but leave the builder ready for more appends.
Definition: bsonobjbuilder.h:593
const char * value() const
raw data of the element's value (so be careful).
Definition: bsonelement.h:162
BSONType type() const
Returns the type of the element.
Definition: bsonelement.h:109
Definition: time_support.h:39
ObjectId.
Definition: bsontypes.h:56
an embedded object
Definition: bsontypes.h:48
BSONObjBuilder & append(const StringData &fieldName, const char *str, int sz)
Append a string element.
Definition: bsonobjbuilder.h:379
32 bit signed integer
Definition: bsontypes.h:74
double precision floating point value
Definition: bsontypes.h:44
null type
Definition: bsontypes.h:62
a programming language (e.g., Python) symbol
Definition: bsontypes.h:70
javascript code that can execute on the database server, with SavedContext
Definition: bsontypes.h:72
iterator for a BSONObj
Definition: bsonobjiterator.h:37
void init()
sets the contents to a new oid / randomized value
MONGO_CLIENT_API bool isArray(const StringData &str)
Tests whether the JSON string is an Array.
Definition: shared_buffer.h:67
BSONObjBuilder & appendNumber(const StringData &fieldName, int n)
appendNumber is a series of method for appending the smallest sensible type mostly for JS ...
Definition: bsonobjbuilder.h:240
Definition: bsonmisc.h:164
int valuesize() const
size in bytes of the element's value (when applicable).
Definition: bsonelement.h:166
Undefined type.
Definition: bsontypes.h:54
BSONObjBuilder & append(const StringData &fieldName, OID oid)
Append a BSON Object ID.
Definition: bsonobjbuilder.h:315
BSONObjBuilder & genOID()
Generate and assign an object id for the _id field.
Definition: bsonobjbuilder.h:326
Definition: bsonmisc.h:157
bool eoo() const
Indicates if it is the end-of-object element, which is present at the end of every BSON object...
Definition: bsonelement.h:125
BSONObjBuilder & appendRegex(const StringData &fieldName, const StringData ®ex, const StringData &options="")
Append a regular expression value.
Definition: bsonobjbuilder.h:353
BSONObjBuilder & appendTimeT(const StringData &fieldName, time_t dt)
Append a time_t date.
Definition: bsonobjbuilder.h:334
BSONObjBuilder & appendCodeWScope(const StringData &fieldName, const StringData &code, const BSONObj &scope)
Append to the BSON object a field of type CodeWScope.
Definition: bsonobjbuilder.h:514
void appendNull()
Implements builder interface but no-op in ObjBuilder.
Definition: bsonobjbuilder.h:416
regular expression, a pattern with options
Definition: bsontypes.h:64
64 bit integer
Definition: bsontypes.h:78
BSONObjBuilder & appendArray(const StringData &fieldName, const BSONObj &subObj)
add a subobject as a member with type Array.
Definition: bsonobjbuilder.h:167
BSONObjBuilder & appendAs(const BSONElement &e, const StringData &fieldName)
append an element but with a new name
Definition: bsonobjbuilder.h:116
an embedded array
Definition: bsontypes.h:50
BSONObjBuilder & append(const StringData &fieldName, unsigned n)
Append a 32 bit unsigned element - cast to a signed int.
Definition: bsonobjbuilder.h:210
deprecated / use CodeWScope
Definition: bsontypes.h:68
BSONObjBuilder & appendObject(const StringData &fieldName, const char *objdata, int size=0)
add a subobject as a member
Definition: bsonobjbuilder.h:133
BSONObjBuilder(int initsize=512)
Definition: bsonobjbuilder.h:52
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
deprecated / will be redesigned
Definition: bsontypes.h:66
BSONObjBuilderValueStream & operator<<(const StringData &name)
Stream oriented way to add field names and values.
Definition: bsonobjbuilder.h:626
BSONObjBuilder & append(const StringData &fieldName, int n)
Append a 32 bit integer element.
Definition: bsonobjbuilder.h:202
Definition: bsonobj.h:559
BSONObjBuilder & appendBinData(const StringData &fieldName, int len, BinDataType type, const void *data)
Append a binary data element.
Definition: bsonobjbuilder.h:482
BSONObjBuilder & appendIntOrLL(const StringData &fieldName, long long n)
appends a number.
Definition: bsonobjbuilder.h:223
BSONObjBuilder & append(const StringData &fieldName, const StringData &str)
Append a string element.
Definition: bsonobjbuilder.h:395
int len() const
Definition: builder.h:199
Definition: bsonmisc.h:116
Definition: bsonobjbuilder.h:698
bool owned() const
Definition: bsonobjbuilder.h:661
BSONObjBuilder & append(const StringData &fieldName, BSONObj subObj)
add a subobject as a member
Definition: bsonobjbuilder.h:125
BSONObjBuilder & operator<<(GENOIDLabeler)
Stream oriented way to add field names and values.
Definition: bsonobjbuilder.h:632
binary data
Definition: bsontypes.h:52
BSONObjBuilder & append(const StringData &fieldName, bool val)
Append a boolean element.
Definition: bsonobjbuilder.h:194
void clear()
initialize to 'null'
Definition: oid.h:92
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
BSONObjBuilder & append(const StringData &fieldName, const std::string &str)
Append a string element.
Definition: bsonobjbuilder.h:391
Definition: bsonmisc.h:197
BSONObjBuilder(BufBuilder &baseBuilder)
Definition: bsonobjbuilder.h:68
character string, stored in utf8
Definition: bsontypes.h:46