diff options
author | Spencer Jackson <spencer.jackson@mongodb.com> | 2018-08-20 14:18:30 -0400 |
---|---|---|
committer | Spencer Jackson <spencer.jackson@mongodb.com> | 2018-09-17 17:21:40 -0400 |
commit | f99914d14b76718f1fef879cfaabe23c0c8f0857 (patch) | |
tree | e1da0b70c4d958cd59e671166bec0dc9ce9f3a57 /src/mongo/bson/bsonobj.h | |
parent | d246e38f3dad15b9919773ffe6a2fa59288034f2 (diff) | |
download | mongo-f99914d14b76718f1fef879cfaabe23c0c8f0857.tar.gz |
SERVER-36606: Allow construction of large BSON objects
Diffstat (limited to 'src/mongo/bson/bsonobj.h')
-rw-r--r-- | src/mongo/bson/bsonobj.h | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/mongo/bson/bsonobj.h b/src/mongo/bson/bsonobj.h index 2c9f41ceb60..69a06b2e76f 100644 --- a/src/mongo/bson/bsonobj.h +++ b/src/mongo/bson/bsonobj.h @@ -95,6 +95,13 @@ class BSONObjStlIterator; */ class BSONObj { public: + struct DefaultSizeTrait { + constexpr static int MaxSize = BSONObjMaxInternalSize; + }; + struct LargeSizeTrait { + constexpr static int MaxSize = BufferMaxSize; + }; + // Declared in bsonobj_comparator_interface.h. class ComparatorInterface; @@ -125,8 +132,9 @@ public: /** Construct a BSONObj from data in the proper format. * Use this constructor when something else owns bsonData's buffer */ - explicit BSONObj(const char* bsonData) { - init(bsonData); + template <typename Traits = DefaultSizeTrait> + explicit BSONObj(const char* bsonData, Traits t = Traits{}) { + init<Traits>(bsonData); } explicit BSONObj(ConstSharedBuffer ownedBuffer) @@ -379,9 +387,12 @@ public: } /** performs a cursory check on the object's size only. */ + template <typename Traits = DefaultSizeTrait> bool isValid() const { + static_assert(Traits::MaxSize > 0 && Traits::MaxSize <= std::numeric_limits<int>::max(), + "BSONObj maximum size must be within possible limits"); int x = objsize(); - return x > 0 && x <= BSONObjMaxInternalSize; + return x > 0 && x <= Traits::MaxSize; } /** @@ -564,12 +575,13 @@ public: } private: - void _assertInvalid() const; + void _assertInvalid(int maxSize) const; + template <typename Traits = DefaultSizeTrait> void init(const char* data) { _objdata = data; - if (!isValid()) - _assertInvalid(); + if (!isValid<Traits>()) + _assertInvalid(Traits::MaxSize); } const char* _objdata; |