summaryrefslogtreecommitdiff
path: root/src/mongo/db/exec/document_value/document.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/exec/document_value/document.cpp')
-rw-r--r--src/mongo/db/exec/document_value/document.cpp102
1 files changed, 47 insertions, 55 deletions
diff --git a/src/mongo/db/exec/document_value/document.cpp b/src/mongo/db/exec/document_value/document.cpp
index dd833b3c28f..4488af81f3b 100644
--- a/src/mongo/db/exec/document_value/document.cpp
+++ b/src/mongo/db/exec/document_value/document.cpp
@@ -88,8 +88,10 @@ const StringDataSet Document::allMetadataFieldNames{Document::metaFieldTextScore
Document::metaFieldGeoNearPoint,
Document::metaFieldSearchScore,
Document::metaFieldSearchHighlights,
+ Document::metaFieldSearchSortValues,
Document::metaFieldIndexKey,
- Document::metaFieldSearchScoreDetails};
+ Document::metaFieldSearchScoreDetails,
+ Document::metaFieldVectorSearchScore};
DocumentStorageIterator::DocumentStorageIterator(DocumentStorage* storage, BSONObjIterator bsonIt)
: _bsonIt(std::move(bsonIt)),
@@ -130,7 +132,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->stripMetadata() && fieldName[0] == '$' &&
+ if (_storage->bsonHasMetadata() && fieldName[0] == '$' &&
Document::allMetadataFieldNames.contains(fieldName)) {
return true;
}
@@ -343,8 +345,8 @@ void DocumentStorage::reserveFields(size_t expectedFields) {
}
intrusive_ptr<DocumentStorage> DocumentStorage::clone() const {
- auto out =
- make_intrusive<DocumentStorage>(_bson, _stripMetadata, _modified, _numBytesFromBSONInCache);
+ auto out = make_intrusive<DocumentStorage>(
+ _bson, _bsonHasMetadata, _modified, _numBytesFromBSONInCache);
if (_cache) {
// Make a copy of the buffer with the fields.
@@ -373,6 +375,7 @@ intrusive_ptr<DocumentStorage> DocumentStorage::clone() const {
out->_haveLazyLoadedMetadata = _haveLazyLoadedMetadata;
out->_metadataFields = _metadataFields;
+ out->_snapshottedSize = _snapshottedSize;
return out;
}
@@ -389,11 +392,12 @@ DocumentStorage::~DocumentStorage() {
}
}
-void DocumentStorage::reset(const BSONObj& bson, bool stripMetadata) {
+void DocumentStorage::reset(const BSONObj& bson, bool bsonHasMetadata) {
_bson = bson;
_numBytesFromBSONInCache = 0;
- _stripMetadata = stripMetadata;
+ _bsonHasMetadata = bsonHasMetadata;
_modified = false;
+ _snapshottedSize = 0;
// Clean cache.
for (auto it = iteratorCacheOnly(); !it.atEnd(); it.advance()) {
@@ -420,6 +424,8 @@ void DocumentStorage::loadLazyMetadata() const {
return;
}
+ bool oldModified = _metadataFields.isModified();
+
BSONObjIterator it(_bson);
while (it.more()) {
BSONElement elem(it.next());
@@ -460,10 +466,15 @@ 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());
}
}
}
+ _metadataFields.setModified(oldModified);
_haveLazyLoadedMetadata = true;
}
@@ -513,22 +524,11 @@ 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 (!storage().isModified() && !storage().stripMetadata()) {
+ if (isTriviallyConvertible()) {
return storage().bsonObj();
- } else {
- return boost::none;
}
+ return boost::none;
}
constexpr StringData Document::metaFieldTextScore;
@@ -539,35 +539,41 @@ constexpr StringData Document::metaFieldGeoNearPoint;
constexpr StringData Document::metaFieldSearchScore;
constexpr StringData Document::metaFieldSearchHighlights;
constexpr StringData Document::metaFieldSearchScoreDetails;
+constexpr StringData Document::metaFieldSearchSortValues;
+constexpr StringData Document::metaFieldVectorSearchScore;
-BSONObj Document::toBsonWithMetaData() const {
- BSONObjBuilder bb;
- toBson(&bb);
+void Document::toBsonWithMetaData(BSONObjBuilder* builder) const {
+ toBson(builder);
if (!metadata()) {
- return bb.obj();
+ return;
}
if (metadata().hasTextScore())
- bb.append(metaFieldTextScore, metadata().getTextScore());
+ builder->append(metaFieldTextScore, metadata().getTextScore());
if (metadata().hasRandVal())
- bb.append(metaFieldRandVal, metadata().getRandVal());
+ builder->append(metaFieldRandVal, metadata().getRandVal());
if (metadata().hasSortKey())
- bb.append(metaFieldSortKey,
- DocumentMetadataFields::serializeSortKey(metadata().isSingleElementKey(),
- metadata().getSortKey()));
+ builder->append(metaFieldSortKey,
+ DocumentMetadataFields::serializeSortKey(metadata().isSingleElementKey(),
+ metadata().getSortKey()));
if (metadata().hasGeoNearDistance())
- bb.append(metaFieldGeoNearDistance, metadata().getGeoNearDistance());
+ builder->append(metaFieldGeoNearDistance, metadata().getGeoNearDistance());
if (metadata().hasGeoNearPoint())
- metadata().getGeoNearPoint().addToBsonObj(&bb, metaFieldGeoNearPoint);
+ metadata().getGeoNearPoint().addToBsonObj(builder, metaFieldGeoNearPoint);
if (metadata().hasSearchScore())
- bb.append(metaFieldSearchScore, metadata().getSearchScore());
+ builder->append(metaFieldSearchScore, metadata().getSearchScore());
if (metadata().hasSearchHighlights())
- metadata().getSearchHighlights().addToBsonObj(&bb, metaFieldSearchHighlights);
+ metadata().getSearchHighlights().addToBsonObj(builder, metaFieldSearchHighlights);
if (metadata().hasIndexKey())
- bb.append(metaFieldIndexKey, metadata().getIndexKey());
+ builder->append(metaFieldIndexKey, metadata().getIndexKey());
if (metadata().hasSearchScoreDetails())
- bb.append(metaFieldSearchScoreDetails, metadata().getSearchScoreDetails());
- return bb.obj();
+ builder->append(metaFieldSearchScoreDetails, metadata().getSearchScoreDetails());
+ if (metadata().hasSearchSortValues()) {
+ builder->append(metaFieldSearchSortValues, metadata().getSearchSortValues());
+ }
+ if (metadata().hasVectorSearchScore()) {
+ builder->append(metaFieldVectorSearchScore, metadata().getVectorSearchScore());
+ }
}
Document Document::fromBsonWithMetaData(const BSONObj& bson) {
@@ -704,31 +710,17 @@ const Value Document::getNestedField(const FieldPath& path, vector<Position>* po
return getNestedFieldHelper(*this, path, positions, 0);
}
-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::getApproximateSize() const {
+ return sizeof(Document) + storage().snapshottedApproximateSize();
}
-size_t Document::getApproximateSize() const {
- return getApproximateSizeWithoutBackingBSON() + storage().bsonObjSize();
+size_t Document::getCurrentApproximateSize() const {
+ return sizeof(Document) + storage().currentApproximateSize();
}
size_t Document::memUsageForSorter() const {
- return getApproximateSizeWithoutBackingBSON() + storage().nonCachedBsonObjSize();
+ return storage().currentApproximateSize() - storage().bsonObjSize() +
+ storage().nonCachedBsonObjSize();
}
void Document::hash_combine(size_t& seed,