diff options
author | Josef Ahmad <josef.ahmad@mongodb.com> | 2021-10-25 10:52:16 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-10-25 11:38:11 +0000 |
commit | 11e1a414386c0d4c670dfc306b8fdfd52a9e68e0 (patch) | |
tree | 8510195e51449a0c6db4ae3ae0419f1afde79e5b /src/mongo/db/record_id_helpers.cpp | |
parent | 3b168ad0f99534125d7457aa34a743a3cdce0765 (diff) | |
download | mongo-11e1a414386c0d4c670dfc306b8fdfd52a9e68e0.tar.gz |
SERVER-60537 Support clustering non-explicitly replicated collections by arbitrary keys
Diffstat (limited to 'src/mongo/db/record_id_helpers.cpp')
-rw-r--r-- | src/mongo/db/record_id_helpers.cpp | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/mongo/db/record_id_helpers.cpp b/src/mongo/db/record_id_helpers.cpp index e3dee4e66bc..75663948cf6 100644 --- a/src/mongo/db/record_id_helpers.cpp +++ b/src/mongo/db/record_id_helpers.cpp @@ -35,6 +35,7 @@ #include "mongo/bson/bson_validate.h" #include "mongo/bson/timestamp.h" +#include "mongo/db/catalog/clustered_collection_util.h" #include "mongo/db/jsobj.h" #include "mongo/db/record_id.h" #include "mongo/db/storage/key_string.h" @@ -82,22 +83,24 @@ StatusWith<RecordId> extractKeyOptime(const char* data, int len) { return keyForOptime(elem.timestamp()); } -StatusWith<RecordId> keyForDoc(const BSONObj& doc) { - // Build a KeyString as the RecordId using the "_id" field. - BSONElement idElem; - bool foundId = doc.getObjectID(idElem); - if (!foundId) { +StatusWith<RecordId> keyForDoc(const BSONObj& doc, const ClusteredIndexSpec& indexSpec) { + // Get the collection's cluster key field name + const auto clusterKeyField = clustered_util::getClusterKeyFieldName(indexSpec); + // Build a RecordId using the cluster key. + const BSONElement keyElement = doc.getField(clusterKeyField); + if (keyElement.eoo()) { return {ErrorCodes::BadValue, - str::stream() << "Document " << redact(doc) << " is missing the '_id' field"}; + str::stream() << "Document " << redact(doc) << " is missing the '" + << clusterKeyField << "' field"}; } - return keyForElem(idElem); + return keyForElem(keyElement); } RecordId keyForElem(const BSONElement& elem) { - // Intentionally discard the TypeBits since the type information will be stored in the _id of - // the original document. The consequence of this behavior is that _id values that compare - // similarly, but are of different types may not be used concurrently. + // 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); keyBuilder.appendBSONElement(elem); return RecordId(keyBuilder.getBuffer(), keyBuilder.getSize()); |