diff options
Diffstat (limited to 'src/mongo/db/exec/document_value')
| -rw-r--r-- | src/mongo/db/exec/document_value/document.cpp | 11 | ||||
| -rw-r--r-- | src/mongo/db/exec/document_value/document.h | 7 | ||||
| -rw-r--r-- | src/mongo/db/exec/document_value/document_internal.h | 9 | ||||
| -rw-r--r-- | src/mongo/db/exec/document_value/document_value_test.cpp | 10 | ||||
| -rw-r--r-- | src/mongo/db/exec/document_value/document_value_test_util.h | 7 | ||||
| -rw-r--r-- | src/mongo/db/exec/document_value/value.cpp | 19 | ||||
| -rw-r--r-- | src/mongo/db/exec/document_value/value.h | 9 |
7 files changed, 46 insertions, 26 deletions
diff --git a/src/mongo/db/exec/document_value/document.cpp b/src/mongo/db/exec/document_value/document.cpp index 7172370e4c3..2bcb637d17b 100644 --- a/src/mongo/db/exec/document_value/document.cpp +++ b/src/mongo/db/exec/document_value/document.cpp @@ -255,7 +255,8 @@ Value& DocumentStorage::appendField(T field, ValueElement::Kind kind) { append(nextCollision); append(nameSize); append(kind); - field.copyTo(dest, true); + dest += field.copy(dest, field.size()); + *dest++ = '\0'; // Like std::string, there is both an explicit size and final NUL byte. // Padding for alignment handled above #undef append @@ -432,6 +433,12 @@ Document DocumentStorage::shred() const { return md.freeze(); } +void DocumentStorage::loadIntoCache() const { + for (DocumentStorageIterator it = iterator(); !it.atEnd(); it.advance()) { + it.get(); + } +} + void DocumentStorage::loadLazyMetadata() const { if (_haveLazyLoadedMetadata) { return; @@ -819,7 +826,7 @@ void Document::serializeForSorter(BufBuilder& buf) const { buf.appendNum(static_cast<int>(numElems)); for (DocumentStorageIterator it = storage().iterator(); !it.atEnd(); it.advance()) { - buf.appendStr(it->nameSD(), /*NUL byte*/ true); + buf.appendCStr(it->nameSD()); it->val.serializeForSorter(buf); } diff --git a/src/mongo/db/exec/document_value/document.h b/src/mongo/db/exec/document_value/document.h index 6114aee792d..4ff1522f3a9 100644 --- a/src/mongo/db/exec/document_value/document.h +++ b/src/mongo/db/exec/document_value/document.h @@ -254,6 +254,13 @@ public: return storage().shred(); } + /** + * Loads the whole document into cache. + */ + void loadIntoCache() const { + return storage().loadIntoCache(); + } + /** Calculate a hash value. * * Meant to be used to create composite hashes suitable for diff --git a/src/mongo/db/exec/document_value/document_internal.h b/src/mongo/db/exec/document_value/document_internal.h index d7fb3b337d6..372130c3e68 100644 --- a/src/mongo/db/exec/document_value/document_internal.h +++ b/src/mongo/db/exec/document_value/document_internal.h @@ -296,8 +296,8 @@ public: return _sd.size(); } - inline void copyTo(char* dest, bool includeEndingNull) const { - return _sd.copyTo(dest, includeEndingNull); + inline size_t copy(char* dest, size_t len) const { + return _sd.copy(dest, len); } constexpr const char* rawData() const noexcept { @@ -385,6 +385,11 @@ public: */ Document shred() const; + /** + * Loads the whole document into cache. + */ + void loadIntoCache() const; + static const DocumentStorage& emptyDoc() { return kEmptyDoc; } diff --git a/src/mongo/db/exec/document_value/document_value_test.cpp b/src/mongo/db/exec/document_value/document_value_test.cpp index 80441c17f25..ca1123a7555 100644 --- a/src/mongo/db/exec/document_value/document_value_test.cpp +++ b/src/mongo/db/exec/document_value/document_value_test.cpp @@ -46,16 +46,6 @@ #include "mongo/dbtests/dbtests.h" #include "mongo/logv2/log.h" -#define ASSERT_DOES_NOT_THROW(EXPRESSION) \ - try { \ - EXPRESSION; \ - } catch (const AssertionException& e) { \ - ::mongo::str::stream err; \ - err << "Threw an exception incorrectly: " << e.toString() \ - << " Exception occured in: " << #EXPRESSION; \ - ::mongo::unittest::TestAssertionFailure(__FILE__, __LINE__, err).stream(); \ - } - namespace DocumentTests { using std::numeric_limits; diff --git a/src/mongo/db/exec/document_value/document_value_test_util.h b/src/mongo/db/exec/document_value/document_value_test_util.h index b6959a7d17f..7b88c9688fa 100644 --- a/src/mongo/db/exec/document_value/document_value_test_util.h +++ b/src/mongo/db/exec/document_value/document_value_test_util.h @@ -59,6 +59,13 @@ #define _ASSERT_DOCVAL_COMPARISON(NAME, a, b) \ ::mongo::unittest::assertComparison_##NAME(__FILE__, __LINE__, #a, #b, a, b) +// TODO SERVER-87736 make these not say "AUTO". +// These are backport-special macros, adapted from the "AUTO" version on more recent branches. The +// automatic functionality doesn't exist on this branch. But the assertions should still pass. +#define ASSERT_VALUE_EQ_AUTO(expected, val) ASSERT_EQ(expected, val.toString()) +#define ASSERT_DOCUMENT_EQ_AUTO(expected, actual) \ + ASSERT_BSONOBJ_EQ(fromjson(expected), actual.toBson()) + namespace mongo { namespace unittest { diff --git a/src/mongo/db/exec/document_value/value.cpp b/src/mongo/db/exec/document_value/value.cpp index 616efb2128a..a4f47313b49 100644 --- a/src/mongo/db/exec/document_value/value.cpp +++ b/src/mongo/db/exec/document_value/value.cpp @@ -117,7 +117,7 @@ void ValueStorage::putString(StringData s) { if (sizeNoNUL <= sizeof(shortStrStorage)) { shortStr = true; shortStrSize = s.size(); - s.copyTo(shortStrStorage, false); // no NUL + s.copy(shortStrStorage, s.size()); // All memory is zeroed before this is called, so we know that // the nulTerminator field will definitely contain a NUL byte. @@ -148,8 +148,9 @@ void ValueStorage::putRegEx(const BSONRegEx& re) { // Need to copy since putString doesn't support scatter-gather. std::unique_ptr<char[]> buf(new char[totalLen]); - re.pattern.copyTo(buf.get(), true); - re.flags.copyTo(buf.get() + patternLen + 1, false); // no NUL + auto dest = buf.get(); + dest = str::copyAsCString(dest, re.pattern); + re.flags.copy(dest, re.flags.size()); // NUL added automatically by putString() putString(StringData(buf.get(), totalLen)); } @@ -1310,7 +1311,7 @@ void Value::serializeForSorter(BufBuilder& buf) const { case Code: { StringData str = getRawData(); buf.appendNum(int(str.size())); - buf.appendStr(str, /*NUL byte*/ false); + buf.appendStrBytes(str); break; } @@ -1318,13 +1319,13 @@ void Value::serializeForSorter(BufBuilder& buf) const { StringData str = getRawData(); buf.appendChar(_storage.binDataType()); buf.appendNum(int(str.size())); - buf.appendStr(str, /*NUL byte*/ false); + buf.appendStrBytes(str); break; } case RegEx: - buf.appendStr(getRegex(), /*NUL byte*/ true); - buf.appendStr(getRegexFlags(), /*NUL byte*/ true); + buf.appendCStr(getRegex()); + buf.appendCStr(getRegexFlags()); break; case Object: @@ -1333,13 +1334,13 @@ void Value::serializeForSorter(BufBuilder& buf) const { case DBRef: buf.appendStruct(_storage.getDBRef()->oid); - buf.appendStr(_storage.getDBRef()->ns, /*NUL byte*/ true); + buf.appendCStr(_storage.getDBRef()->ns); break; case CodeWScope: { intrusive_ptr<const RCCodeWScope> cws = _storage.getCodeWScope(); buf.appendNum(int(cws->code.size())); - buf.appendStr(cws->code, /*NUL byte*/ false); + buf.appendStrBytes(cws->code); cws->scope.serializeForSorter(buf); break; } diff --git a/src/mongo/db/exec/document_value/value.h b/src/mongo/db/exec/document_value/value.h index a69494f1995..62a31f25727 100644 --- a/src/mongo/db/exec/document_value/value.h +++ b/src/mongo/db/exec/document_value/value.h @@ -430,13 +430,16 @@ public: ImplicitValue(T&& arg) : Value(std::forward<T>(arg)) {} ImplicitValue(std::initializer_list<ImplicitValue> values) : Value(convertToValues(values)) {} + ImplicitValue(std::vector<ImplicitValue> values) : Value(convertToValues(values)) {} - ImplicitValue(std::vector<int> values) : Value(convertToValues(values)) {} + template <typename T> + ImplicitValue(std::vector<T> values) : Value(convertToValues(values)) {} - static std::vector<Value> convertToValues(const std::vector<int>& vec) { + template <typename T> + static std::vector<Value> convertToValues(const std::vector<T>& vec) { std::vector<Value> values; values.reserve(vec.size()); - for_each(vec.begin(), vec.end(), ([&](const int& val) { values.emplace_back(val); })); + for_each(vec.begin(), vec.end(), ([&](const T& val) { values.emplace_back(val); })); return values; } |
