20 #include <boost/noncopyable.hpp>
27 #include "mongo/bson/bsonelement.h"
28 #include "mongo/base/data_view.h"
29 #include "mongo/base/string_data.h"
30 #include "mongo/bson/util/builder.h"
31 #include "mongo/client/export_macros.h"
32 #include "mongo/platform/atomic_word.h"
33 #include "mongo/util/shared_buffer.h"
37 typedef std::set<BSONElement, BSONElementCmpWithoutField> BSONElementSet;
38 typedef std::multiset<BSONElement, BSONElementCmpWithoutField> BSONElementMSet;
85 static const char kEmptyObjectPrototype[] = { 5, 0, 0, 0, 0};
86 _objdata = kEmptyObjectPrototype;
97 : _objdata(ownedBuffer.get() ? ownedBuffer.get() :
BSONObj().objdata()),
98 _ownedBuffer(ownedBuffer.moveFrom()) {}
100 #if __cplusplus >= 201103L
102 BSONObj(BSONObj&& other)
103 : _objdata(std::move(other._objdata)), _ownedBuffer(std::move(other._ownedBuffer)) {
104 other._objdata = BSONObj()._objdata;
111 BSONObj(
const BSONObj&) =
default;
118 this->swap(otherCopy);
125 swap(_objdata, other._objdata);
126 swap(_ownedBuffer, other._ownedBuffer);
158 return _ownedBuffer.get() != 0;
172 enum { maxToStringRecursionDepth = 100 };
174 std::string toString(
bool isArray =
false,
bool full =
false)
const;
175 void toString(StringBuilder& s,
bool isArray =
false,
bool full =
false,
int depth = 0)
const;
185 int addFields(BSONObj& from, std::set<std::string>& fields);
190 BSONObj removeField(
const StringData& name)
const;
198 int getFieldNames(std::set<std::string>& fields)
const;
204 BSONElement getFieldDotted(
const StringData& name)
const;
210 void getFieldsDotted(
const StringData& name,
212 bool expandLastArray =
true)
const;
213 void getFieldsDotted(
const StringData& name,
214 BSONElementMSet& ret,
215 bool expandLastArray =
true)
const;
220 BSONElement getFieldDottedOrArray(
const char*& name)
const;
225 BSONElement getField(
const StringData& name)
const;
233 void getFields(
unsigned n,
const char** fieldNames, BSONElement* fields)
const;
239 return getField(field);
245 std::string s = ss.str();
246 return getField(s.c_str());
251 return !getField(name).eoo();
255 return hasField(name);
259 const char* getStringField(
const StringData& name)
const;
265 int getIntField(
const StringData& name)
const;
270 bool getBoolField(
const StringData& name)
const;
291 BSONObj extractFields(
const BSONObj& pattern,
bool fillWithNull =
false)
const;
293 BSONObj filterFieldsUndotted(
const BSONObj& filter,
bool inFilter)
const;
300 bool couldBeArray()
const;
315 return x > 0 && x <= BSONObjMaxInternalSize;
325 return _okForStorage(
false,
true).isOK();
335 return _okForStorage(
true,
true).isOK();
347 return _okForStorage(
false, deep);
359 return _okForStorage(
true, deep);
364 return objsize() <= 5;
370 std::string hexDump()
const;
377 int woCompare(
const BSONObj& r,
const Ordering& o,
bool considerFieldName =
true)
const;
384 int woCompare(
const BSONObj& r,
386 bool considerFieldName =
true)
const;
388 bool operator<(
const BSONObj& other)
const {
389 return woCompare(other) < 0;
391 bool operator<=(
const BSONObj& other)
const {
392 return woCompare(other) <= 0;
394 bool operator>(
const BSONObj& other)
const {
395 return woCompare(other) > 0;
397 bool operator>=(
const BSONObj& other)
const {
398 return woCompare(other) >= 0;
404 int woSortOrder(
const BSONObj& r,
const BSONObj& sortKey,
bool useDotted =
false)
const;
406 bool equal(
const BSONObj& r)
const;
414 size_t operator()(
const BSONObj& obj)
const;
423 bool isPrefixOf(
const BSONObj& otherObj)
const;
431 bool isFieldNamePrefixOf(
const BSONObj& otherObj)
const;
439 return (os == 0 || memcmp(objdata(), r.
objdata(), os) == 0);
453 const char* p = objdata() + 4;
454 return *p ==
EOO ?
"" : p + 1;
458 const char* p = objdata() + 4;
467 bool getObjectID(BSONElement& e)
const;
473 BSONObj clientReadable()
const;
477 BSONObj replaceFieldNames(
const BSONObj& obj)
const;
482 bool operator==(
const BSONObj& other)
const {
485 bool operator!=(
const BSONObj& other)
const {
486 return !operator==(other);
508 opMAX_DISTANCE = 0x15,
509 opGEO_INTERSECTS = 0x16,
513 void elems(std::vector<BSONElement>&)
const;
515 void elems(std::list<BSONElement>&)
const;
517 friend class BSONObjIterator;
518 typedef BSONObjIterator iterator;
526 BSONObjIterator begin()
const;
528 void appendSelfToBufBuilder(BufBuilder& b)
const {
530 b.appendBuf(objdata(), objsize());
533 template <
typename T>
534 bool coerceVector(std::vector<T>* out)
const;
536 typedef SharedBuffer::Holder Holder;
550 void _assertInvalid()
const;
552 void init(
const char* data) {
564 Status _okForStorage(
bool root,
bool deep)
const;
566 const char* _objdata;
567 SharedBuffer _ownedBuffer;
570 MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC operator<<(std::ostream& s,
const BSONObj& o);
571 MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC operator<<(std::ostream& s,
const BSONElement& e);
573 MONGO_CLIENT_API StringBuilder& MONGO_CLIENT_FUNC operator<<(StringBuilder& s,
const BSONObj& o);
574 MONGO_CLIENT_API StringBuilder& MONGO_CLIENT_FUNC
575 operator<<(StringBuilder& s,
const BSONElement& e);
577 inline void swap(BSONObj& l, BSONObj& r) {
end of object
Definition: bsontypes.h:42
JsonStringFormat
Formatting mode for generating JSON from BSON.
Definition: oid.h:205
Status represents an error state or the absence thereof.
Definition: status.h:50
BSONElement firstElement() const
Definition: bsonobj.h:445
bool isOwned() const
A BSONObj can use a buffer it "owns" or one it does not.
Definition: bsonobj.h:157
std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:53
int objsize() const
Definition: bsonobj.h:308
Definition: shared_buffer.h:25
bool hasField(const StringData &name) const
Definition: bsonobj.h:250
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
BSONObj(const char *bsonData)
Construct a BSONObj from data in the proper format.
Definition: bsonobj.h:92
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:545
const char * objdata() const
Definition: bsonobj.h:303
Definition: data_view.h:30
bool okForStorage() const
Definition: bsonobj.h:324
bool hasElement(const StringData &name) const
Definition: bsonobj.h:254
strict RFC format
Definition: oid.h:207
BSONObj & operator=(BSONObj otherCopy)
Provide assignment semantics.
Definition: bsonobj.h:117
void swap(BSONObj &other)
Swap this BSONObj with 'other'.
Definition: bsonobj.h:123
bool isValid() const
performs a cursory check on the object's size only.
Definition: bsonobj.h:313
BSONType
the complete list of valid BSON types see also bsonspec.org
Definition: bsontypes.h:38
Status storageValid(const bool deep=true) const
Validates that this can be stored as a document (in a collection) See details above in okForStorageAs...
Definition: bsonobj.h:358
MONGO_CLIENT_API bool isArray(const StringData &str)
Tests whether the JSON string is an Array.
bool okForStorageAsRoot() const
Same as above with the following extra restrictions Not valid if:
Definition: bsonobj.h:334
static SharedBuffer takeOwnership(char *holderPrefixedData)
Given a pointer to a region of un-owned data, prefixed by sufficient space for a SharedBuffer::Holder...
Definition: shared_buffer.h:54
const char * firstElementFieldName() const
faster than firstElement().fieldName() - for the first element we can easily find the fieldname witho...
Definition: bsonobj.h:452
Status storageValidEmbedded(const bool deep=true) const
Validates that this can be stored as an embedded document See details above in okForStorage.
Definition: bsonobj.h:346
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
Definition: bsonobj.h:581
BSONElement operator[](const StringData &field) const
Get the field of the specified name.
Definition: bsonobj.h:238
bool isEmpty() const
Definition: bsonobj.h:363
bool binaryEqual(const BSONObj &r) const
This is "shallow equality" – ints and doubles won't match.
Definition: bsonobj.h:436
BSONObj()
Construct an empty BSONObj – that is, {}.
Definition: bsonobj.h:81
A precomputation of a BSON index or sort key pattern.
Definition: ordering.h:32
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject ...
Definition: bsonobj.h:413