summaryrefslogtreecommitdiff
path: root/src/mongo/db/record_id_helpers.cpp
diff options
context:
space:
mode:
authorDaniel Gómez Ferro <daniel.gomezferro@mongodb.com>2022-02-07 15:29:29 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-02-07 18:50:45 +0000
commit7a2d86c376488cc756c0325e6edaf3406a86ec5d (patch)
tree660a74392777be30a2d629bde1951b21c3a61883 /src/mongo/db/record_id_helpers.cpp
parent59e19dc9bff797fc1cff45d56942e5e61da36f1a (diff)
downloadmongo-7a2d86c376488cc756c0325e6edaf3406a86ec5d.tar.gz
SERVER-61939 Tighter bounds for clustered collection scans
Diffstat (limited to 'src/mongo/db/record_id_helpers.cpp')
-rw-r--r--src/mongo/db/record_id_helpers.cpp38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/mongo/db/record_id_helpers.cpp b/src/mongo/db/record_id_helpers.cpp
index 35925a75019..02bad4bad4e 100644
--- a/src/mongo/db/record_id_helpers.cpp
+++ b/src/mongo/db/record_id_helpers.cpp
@@ -51,20 +51,21 @@ StatusWith<RecordId> keyForOptime(const Timestamp& opTime) {
// don't sort differently when put in a RecordId. It also avoids issues with Null/Invalid
// RecordIds
if (opTime.getSecs() > uint32_t(std::numeric_limits<int32_t>::max()))
- return StatusWith<RecordId>(ErrorCodes::BadValue, "ts secs too high");
+ return {ErrorCodes::BadValue, "ts secs too high"};
if (opTime.getInc() > uint32_t(std::numeric_limits<int32_t>::max()))
- return StatusWith<RecordId>(ErrorCodes::BadValue, "ts inc too high");
+ return {ErrorCodes::BadValue, "ts inc too high"};
- const RecordId out = RecordId(opTime.getSecs(), opTime.getInc());
+ const auto out = RecordId(opTime.getSecs(), opTime.getInc());
if (out <= RecordId::minLong())
- return StatusWith<RecordId>(ErrorCodes::BadValue, "ts too low");
+ return {ErrorCodes::BadValue, "ts too low"};
if (out >= RecordId::maxLong())
- return StatusWith<RecordId>(ErrorCodes::BadValue, "ts too high");
+ return {ErrorCodes::BadValue, "ts too high"};
- return StatusWith<RecordId>(out);
+ return out;
}
+
/**
* data and len must be the arguments from RecordStore::insert() on an oplog collection.
*/
@@ -77,9 +78,9 @@ StatusWith<RecordId> extractKeyOptime(const char* data, int len) {
const BSONObj obj(data);
const BSONElement elem = obj["ts"];
if (elem.eoo())
- return StatusWith<RecordId>(ErrorCodes::BadValue, "no ts field");
+ return {ErrorCodes::BadValue, "no ts field"};
if (elem.type() != bsonTimestamp)
- return StatusWith<RecordId>(ErrorCodes::BadValue, "ts must be a Timestamp");
+ return {ErrorCodes::BadValue, "ts must be a Timestamp"};
return keyForOptime(elem.timestamp());
}
@@ -96,25 +97,28 @@ StatusWith<RecordId> keyForDoc(const BSONObj& doc,
str::stream() << "Document " << redact(doc) << " is missing the '"
<< clusterKeyField << "' field"};
}
+ if (collator) {
+ BSONObjBuilder out;
+ CollationIndexKey::collationAwareIndexKeyAppend(keyElement, collator, &out);
+ return keyForElem(out.done().firstElement());
+ }
- return keyForElem(keyElement, collator);
+ return keyForElem(keyElement);
}
-RecordId keyForElem(const BSONElement& elem, const CollatorInterface* collator) {
+RecordId keyForElem(const BSONElement& elem) {
// Intentionally discard the TypeBits since the type information will be stored in the cluster
// key of the original document. The consequence of this behavior is that cluster key values
// that compare similarly, but are of different types may not be used concurrently.
KeyString::Builder keyBuilder(KeyString::Version::kLatestVersion);
- if (collator) {
- BSONObjBuilder out;
- CollationIndexKey::collationAwareIndexKeyAppend(elem, collator, &out);
- keyBuilder.appendBSONElement(out.done().firstElement());
- } else {
- keyBuilder.appendBSONElement(elem);
- }
+ keyBuilder.appendBSONElement(elem);
return RecordId(keyBuilder.getBuffer(), keyBuilder.getSize());
}
+RecordId keyForObj(const BSONObj& obj) {
+ return keyForElem(obj.firstElement());
+}
+
RecordId keyForOID(OID oid) {
KeyString::Builder keyBuilder(KeyString::Version::kLatestVersion);
keyBuilder.appendOID(oid);