summaryrefslogtreecommitdiff
path: root/src/mongo/db/exec/document_value/document_metadata_fields.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/exec/document_value/document_metadata_fields.h')
-rw-r--r--src/mongo/db/exec/document_value/document_metadata_fields.h152
1 files changed, 59 insertions, 93 deletions
diff --git a/src/mongo/db/exec/document_value/document_metadata_fields.h b/src/mongo/db/exec/document_value/document_metadata_fields.h
index 6759675c7ea..12932c29686 100644
--- a/src/mongo/db/exec/document_value/document_metadata_fields.h
+++ b/src/mongo/db/exec/document_value/document_metadata_fields.h
@@ -67,9 +67,6 @@ public:
kSearchScoreDetails,
kTimeseriesBucketMinTime,
kTimeseriesBucketMaxTime,
- kSearchSortValues,
- kVectorSearchScore,
- kSearchSequenceToken,
// New fields must be added before the kNumFields sentinel.
kNumFields
@@ -151,7 +148,11 @@ public:
}
void setTextScore(double score) {
- _setCommon(MetaType::kTextScore);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kTextScore);
_holder->textScore = score;
}
@@ -165,7 +166,11 @@ public:
}
void setRandVal(double val) {
- _setCommon(MetaType::kRandVal);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kRandVal);
_holder->randVal = val;
}
@@ -179,7 +184,11 @@ public:
}
void setSortKey(Value sortKey, bool isSingleElementKey) {
- _setCommon(MetaType::kSortKey);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kSortKey);
_holder->isSingleElementKey = isSingleElementKey;
_holder->sortKey = std::move(sortKey);
}
@@ -198,7 +207,11 @@ public:
}
void setGeoNearDistance(double dist) {
- _setCommon(MetaType::kGeoNearDist);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kGeoNearDist);
_holder->geoNearDistance = dist;
}
@@ -212,7 +225,11 @@ public:
}
void setGeoNearPoint(Value point) {
- _setCommon(MetaType::kGeoNearPoint);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kGeoNearPoint);
_holder->geoNearPoint = std::move(point);
}
@@ -226,7 +243,11 @@ public:
}
void setSearchScore(double score) {
- _setCommon(MetaType::kSearchScore);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kSearchScore);
_holder->searchScore = score;
}
@@ -240,7 +261,11 @@ public:
}
void setSearchHighlights(Value highlights) {
- _setCommon(MetaType::kSearchHighlights);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kSearchHighlights);
_holder->searchHighlights = highlights;
}
@@ -254,7 +279,11 @@ public:
}
void setIndexKey(BSONObj indexKey) {
- _setCommon(MetaType::kIndexKey);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kIndexKey);
_holder->indexKey = indexKey.getOwned();
}
@@ -268,7 +297,11 @@ public:
}
void setRecordId(RecordId rid) {
- _setCommon(MetaType::kRecordId);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+
+ _holder->metaFields.set(MetaType::kRecordId);
_holder->recordId = rid;
}
@@ -282,7 +315,10 @@ public:
}
void setSearchScoreDetails(BSONObj details) {
- _setCommon(MetaType::kSearchScoreDetails);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+ _holder->metaFields.set(MetaType::kSearchScoreDetails);
_holder->searchScoreDetails = details.getOwned();
}
@@ -291,14 +327,15 @@ public:
}
Date_t getTimeseriesBucketMinTime() const {
- tassert(6850100,
- "Document must have timeseries bucket min time metadata field set",
- hasTimeseriesBucketMinTime());
+ invariant(hasTimeseriesBucketMinTime());
return _holder->timeseriesBucketMinTime;
}
void setTimeseriesBucketMinTime(Date_t time) {
- _setCommon(MetaType::kTimeseriesBucketMinTime);
+ if (!_holder) {
+ _holder = std::make_unique<MetadataHolder>();
+ }
+ _holder->metaFields.set(MetaType::kTimeseriesBucketMinTime);
_holder->timeseriesBucketMinTime = time;
}
@@ -307,83 +344,20 @@ public:
}
Date_t getTimeseriesBucketMaxTime() const {
- tassert(6850101,
- "Document must have timeseries bucket max time metadata field set",
- hasTimeseriesBucketMaxTime());
+ invariant(hasTimeseriesBucketMaxTime());
return _holder->timeseriesBucketMaxTime;
}
void setTimeseriesBucketMaxTime(Date_t time) {
- _setCommon(MetaType::kTimeseriesBucketMaxTime);
- _holder->timeseriesBucketMaxTime = time;
- }
-
- bool hasSearchSortValues() const {
- return _holder && _holder->metaFields.test(MetaType::kSearchSortValues);
- }
-
- BSONObj getSearchSortValues() const {
- tassert(7320401, "Document must have $searchSortValues set", hasSearchSortValues());
- return _holder->searchSortValues;
- }
-
- void setSearchSortValues(BSONObj vals) {
- _setCommon(MetaType::kSearchSortValues);
- _holder->searchSortValues = vals.getOwned();
- }
-
- bool hasVectorSearchScore() const {
- return _holder && _holder->metaFields.test(MetaType::kVectorSearchScore);
- }
-
- double getVectorSearchScore() const {
- tassert(7828400, "vectorSearchScore must be present in metadata", hasVectorSearchScore());
- return _holder->vectorSearchScore;
- }
-
- void setVectorSearchScore(double vectorSearchScore) {
- _setCommon(MetaType::kVectorSearchScore);
- _holder->vectorSearchScore = vectorSearchScore;
- }
-
- bool hasSearchSequenceToken() const {
- return _holder && _holder->metaFields.test(MetaType::kSearchSequenceToken);
- }
-
- Value getSearchSequenceToken() const {
- invariant(hasSearchSequenceToken());
- return _holder->searchSequenceToken;
- }
-
- void setSearchSequenceToken(Value details) {
- _setCommon(MetaType::kSearchSequenceToken);
- _holder->searchSequenceToken = details;
- }
-
- void serializeForSorter(BufBuilder& buf) const;
-
- bool isModified() const {
- return _modified;
- }
-
- /**
- * Sets the 'modified' flag to the given value. Necessary for implementing a lazy load
- * optimization for the contained mutable 'DocumentMetadataFields' instance inside the
- * 'Document' class.
- */
- void setModified(bool newValue) {
- _modified = newValue;
- }
-
-private:
- inline void _setCommon(MetaType mt) {
if (!_holder) {
_holder = std::make_unique<MetadataHolder>();
}
- _holder->metaFields.set(mt);
- _modified = true;
+ _holder->metaFields.set(MetaType::kTimeseriesBucketMaxTime);
+ _holder->timeseriesBucketMaxTime = time;
}
+ void serializeForSorter(BufBuilder& buf) const;
+private:
// A simple data struct housing all possible metadata fields.
struct MetadataHolder {
std::bitset<MetaType::kNumFields> metaFields;
@@ -405,18 +379,10 @@ private:
BSONObj searchScoreDetails;
Date_t timeseriesBucketMinTime;
Date_t timeseriesBucketMaxTime;
- BSONObj searchSortValues;
- double vectorSearchScore{0.0};
- Value searchSequenceToken;
};
// Null until the first setter is called, at which point a MetadataHolder struct is allocated.
std::unique_ptr<MetadataHolder> _holder;
-
- // This flag is set to true anytime a 'DocumentMetadataFields' instance is modified. It is used
- // to optimize document conversion to BSON with metadata; i.e. if there are no modifications we
- // can directly return the underlying BSON.
- bool _modified{false};
};
using QueryMetadataBitSet = std::bitset<DocumentMetadataFields::MetaType::kNumFields>;