diff options
| author | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-11 15:07:35 -0300 |
|---|---|---|
| committer | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-11 15:07:35 -0300 |
| commit | 4cb8841196d0625dfa3825aa326f071cd27c7b8b (patch) | |
| tree | 1682a647d4463397c119183369ae6f750d5fdcff /src/mongo/db/exec/document_value/document.cpp | |
| parent | aa03c6362cbaa767638e6eed9b031d86dd2643d1 (diff) | |
| parent | 8f0827553e09872941945a093b647a4211a9db7f (diff) | |
Update upstream source from tag 'upstream/6.0.0'master
Update to upstream version '6.0.0'
with Debian dir 5604a80ec1c96ca76f25f40d78e6ef855abec322
Diffstat (limited to 'src/mongo/db/exec/document_value/document.cpp')
| -rw-r--r-- | src/mongo/db/exec/document_value/document.cpp | 128 |
1 files changed, 59 insertions, 69 deletions
diff --git a/src/mongo/db/exec/document_value/document.cpp b/src/mongo/db/exec/document_value/document.cpp index 7172370e4c3..dd833b3c28f 100644 --- a/src/mongo/db/exec/document_value/document.cpp +++ b/src/mongo/db/exec/document_value/document.cpp @@ -88,11 +88,8 @@ const StringDataSet Document::allMetadataFieldNames{Document::metaFieldTextScore Document::metaFieldGeoNearPoint, Document::metaFieldSearchScore, Document::metaFieldSearchHighlights, - Document::metaFieldSearchSortValues, Document::metaFieldIndexKey, - Document::metaFieldSearchScoreDetails, - Document::metaFieldVectorSearchScore, - Document::metaFieldSearchSequenceToken}; + Document::metaFieldSearchScoreDetails}; DocumentStorageIterator::DocumentStorageIterator(DocumentStorage* storage, BSONObjIterator bsonIt) : _bsonIt(std::move(bsonIt)), @@ -133,7 +130,7 @@ bool DocumentStorageIterator::shouldSkipDeleted() { // If we strip the metadata see if a field name matches the known list. All metadata fields // start with '$' so optimize for a quick bailout. - if (_storage->bsonHasMetadata() && !fieldName.empty() && fieldName[0] == '$' && + if (_storage->stripMetadata() && fieldName[0] == '$' && Document::allMetadataFieldNames.contains(fieldName)) { return true; } @@ -346,8 +343,8 @@ void DocumentStorage::reserveFields(size_t expectedFields) { } intrusive_ptr<DocumentStorage> DocumentStorage::clone() const { - auto out = make_intrusive<DocumentStorage>( - _bson, _bsonHasMetadata, _modified, _numBytesFromBSONInCache); + auto out = + make_intrusive<DocumentStorage>(_bson, _stripMetadata, _modified, _numBytesFromBSONInCache); if (_cache) { // Make a copy of the buffer with the fields. @@ -376,7 +373,6 @@ intrusive_ptr<DocumentStorage> DocumentStorage::clone() const { out->_haveLazyLoadedMetadata = _haveLazyLoadedMetadata; out->_metadataFields = _metadataFields; - out->_snapshottedSize = _snapshottedSize; return out; } @@ -393,12 +389,11 @@ DocumentStorage::~DocumentStorage() { } } -void DocumentStorage::reset(const BSONObj& bson, bool bsonHasMetadata) { +void DocumentStorage::reset(const BSONObj& bson, bool stripMetadata) { _bson = bson; _numBytesFromBSONInCache = 0; - _bsonHasMetadata = bsonHasMetadata; + _stripMetadata = stripMetadata; _modified = false; - _snapshottedSize = 0; // Clean cache. for (auto it = iteratorCacheOnly(); !it.atEnd(); it.advance()) { @@ -414,22 +409,10 @@ void DocumentStorage::reset(const BSONObj& bson, bool bsonHasMetadata) { _metadataFields = DocumentMetadataFields{}; } -Document DocumentStorage::shred() const { - MutableDocument md; - // Iterate raw bson if possible. This avoids caching all of the values in a doc that might get - // thrown away. - if (!isModified() && !bsonHasMetadata()) { - for (const auto& elem : _bson) { - md[elem.fieldNameStringData()] = Value(elem).shred(); - } - } else { - for (DocumentStorageIterator it = iterator(); !it.atEnd(); it.advance()) { - const auto& valueElem = it.get(); - md[it.fieldName()] = valueElem.val.shred(); - } +void DocumentStorage::fillCache() const { + for (DocumentStorageIterator it = iterator(); !it.atEnd(); it.advance()) { + it->val.fillCache(); } - md.setMetadata(DocumentMetadataFields(metadata())); - return md.freeze(); } void DocumentStorage::loadLazyMetadata() const { @@ -437,13 +420,11 @@ void DocumentStorage::loadLazyMetadata() const { return; } - bool oldModified = _metadataFields.isModified(); - BSONObjIterator it(_bson); while (it.more()) { BSONElement elem(it.next()); auto fieldName = elem.fieldNameStringData(); - if (!fieldName.empty() && fieldName[0] == '$') { + if (fieldName[0] == '$') { if (fieldName == Document::metaFieldTextScore) { _metadataFields.setTextScore(elem.Double()); } else if (fieldName == Document::metaFieldSearchScore) { @@ -479,17 +460,10 @@ void DocumentStorage::loadLazyMetadata() const { _metadataFields.setIndexKey(elem.Obj()); } else if (fieldName == Document::metaFieldSearchScoreDetails) { _metadataFields.setSearchScoreDetails(elem.Obj()); - } else if (fieldName == Document::metaFieldSearchSortValues) { - _metadataFields.setSearchSortValues(elem.Obj()); - } else if (fieldName == Document::metaFieldVectorSearchScore) { - _metadataFields.setVectorSearchScore(elem.Double()); - } else if (fieldName == Document::metaFieldSearchSequenceToken) { - _metadataFields.setSearchSequenceToken(Value(elem)); } } } - _metadataFields.setModified(oldModified); _haveLazyLoadedMetadata = true; } @@ -539,11 +513,22 @@ void Document::toBson(BSONObjBuilder* builder, size_t recursionLevel) const { } } +BSONObj Document::toBson() const { + if (!storage().isModified() && !storage().stripMetadata()) { + return storage().bsonObj(); + } + + BSONObjBuilder bb; + toBson(&bb); + return bb.obj(); +} + boost::optional<BSONObj> Document::toBsonIfTriviallyConvertible() const { - if (isTriviallyConvertible()) { + if (!storage().isModified() && !storage().stripMetadata()) { return storage().bsonObj(); + } else { + return boost::none; } - return boost::none; } constexpr StringData Document::metaFieldTextScore; @@ -554,44 +539,35 @@ constexpr StringData Document::metaFieldGeoNearPoint; constexpr StringData Document::metaFieldSearchScore; constexpr StringData Document::metaFieldSearchHighlights; constexpr StringData Document::metaFieldSearchScoreDetails; -constexpr StringData Document::metaFieldSearchSortValues; -constexpr StringData Document::metaFieldVectorSearchScore; -void Document::toBsonWithMetaData(BSONObjBuilder* builder) const { - toBson(builder); +BSONObj Document::toBsonWithMetaData() const { + BSONObjBuilder bb; + toBson(&bb); if (!metadata()) { - return; + return bb.obj(); } if (metadata().hasTextScore()) - builder->append(metaFieldTextScore, metadata().getTextScore()); + bb.append(metaFieldTextScore, metadata().getTextScore()); if (metadata().hasRandVal()) - builder->append(metaFieldRandVal, metadata().getRandVal()); + bb.append(metaFieldRandVal, metadata().getRandVal()); if (metadata().hasSortKey()) - builder->append(metaFieldSortKey, - DocumentMetadataFields::serializeSortKey(metadata().isSingleElementKey(), - metadata().getSortKey())); + bb.append(metaFieldSortKey, + DocumentMetadataFields::serializeSortKey(metadata().isSingleElementKey(), + metadata().getSortKey())); if (metadata().hasGeoNearDistance()) - builder->append(metaFieldGeoNearDistance, metadata().getGeoNearDistance()); + bb.append(metaFieldGeoNearDistance, metadata().getGeoNearDistance()); if (metadata().hasGeoNearPoint()) - metadata().getGeoNearPoint().addToBsonObj(builder, metaFieldGeoNearPoint); + metadata().getGeoNearPoint().addToBsonObj(&bb, metaFieldGeoNearPoint); if (metadata().hasSearchScore()) - builder->append(metaFieldSearchScore, metadata().getSearchScore()); + bb.append(metaFieldSearchScore, metadata().getSearchScore()); if (metadata().hasSearchHighlights()) - metadata().getSearchHighlights().addToBsonObj(builder, metaFieldSearchHighlights); + metadata().getSearchHighlights().addToBsonObj(&bb, metaFieldSearchHighlights); if (metadata().hasIndexKey()) - builder->append(metaFieldIndexKey, metadata().getIndexKey()); + bb.append(metaFieldIndexKey, metadata().getIndexKey()); if (metadata().hasSearchScoreDetails()) - builder->append(metaFieldSearchScoreDetails, metadata().getSearchScoreDetails()); - if (metadata().hasSearchSortValues()) { - builder->append(metaFieldSearchSortValues, metadata().getSearchSortValues()); - } - if (metadata().hasSearchSequenceToken()) { - metadata().getSearchSequenceToken().addToBsonObj(builder, metaFieldSearchSequenceToken); - } - if (metadata().hasVectorSearchScore()) { - builder->append(metaFieldVectorSearchScore, metadata().getVectorSearchScore()); - } + bb.append(metaFieldSearchScoreDetails, metadata().getSearchScoreDetails()); + return bb.obj(); } Document Document::fromBsonWithMetaData(const BSONObj& bson) { @@ -728,17 +704,31 @@ const Value Document::getNestedField(const FieldPath& path, vector<Position>* po return getNestedFieldHelper(*this, path, positions, 0); } -size_t Document::getApproximateSize() const { - return sizeof(Document) + storage().snapshottedApproximateSize(); +size_t Document::getApproximateSizeWithoutBackingBSON() const { + size_t size = sizeof(Document); + if (!_storage) + return size; + + size += sizeof(DocumentStorage); + size += storage().allocatedBytes(); + + for (auto it = storage().iteratorCacheOnly(); !it.atEnd(); it.advance()) { + size += it->val.getApproximateSize(); + size -= sizeof(Value); // already accounted for above + } + + // The metadata also occupies space in the document storage that's pre-allocated. + size += storage().getMetadataApproximateSize(); + + return size; } -size_t Document::getCurrentApproximateSize() const { - return sizeof(Document) + storage().currentApproximateSize(); +size_t Document::getApproximateSize() const { + return getApproximateSizeWithoutBackingBSON() + storage().bsonObjSize(); } size_t Document::memUsageForSorter() const { - return storage().currentApproximateSize() - storage().bsonObjSize() + - storage().nonCachedBsonObjSize(); + return getApproximateSizeWithoutBackingBSON() + storage().nonCachedBsonObjSize(); } void Document::hash_combine(size_t& seed, |
