summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2021-11-03 21:05:54 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-11-04 02:23:53 +0000
commitd462d6532a5511c5a3c03f23e3d7c044068e8637 (patch)
tree5eb7c01b0564bb7aeed2fb448eee9dbc533b5e7f
parenta52ee5999b0fc7adcbce77b37a7313d4585a4cbf (diff)
downloadmongo-d462d6532a5511c5a3c03f23e3d7c044068e8637.tar.gz
SERVER-61158 add method to Collection to set unique property in index catalog entry
This adds Collection::updateUniqueSetting() and BSONCollectionCatalogEntry::IndexMetaData::updateUniqueSetting().
-rw-r--r--src/mongo/db/catalog/collection.h6
-rw-r--r--src/mongo/db/catalog/collection_impl.cpp9
-rw-r--r--src/mongo/db/catalog/collection_impl.h2
-rw-r--r--src/mongo/db/catalog/collection_mock.h4
-rw-r--r--src/mongo/db/storage/bson_collection_catalog_entry.cpp7
-rw-r--r--src/mongo/db/storage/bson_collection_catalog_entry.h2
6 files changed, 30 insertions, 0 deletions
diff --git a/src/mongo/db/catalog/collection.h b/src/mongo/db/catalog/collection.h
index fa2971473a3..be1d242abd9 100644
--- a/src/mongo/db/catalog/collection.h
+++ b/src/mongo/db/catalog/collection.h
@@ -586,6 +586,12 @@ public:
*/
virtual void updateHiddenSetting(OperationContext* opCtx, StringData idxName, bool hidden) = 0;
+ /*
+ * Converts the the given index to be unique.
+ * This is a one-way transformation - the uniqueness constraint cannot be removed.
+ */
+ virtual void updateUniqueSetting(OperationContext* opCtx, StringData idxName) = 0;
+
/**
* Removes invalid index options on all indexes in this collection. Returns a list of index
* names that contained invalid index options.
diff --git a/src/mongo/db/catalog/collection_impl.cpp b/src/mongo/db/catalog/collection_impl.cpp
index a121b6d67c2..324475d345a 100644
--- a/src/mongo/db/catalog/collection_impl.cpp
+++ b/src/mongo/db/catalog/collection_impl.cpp
@@ -1892,6 +1892,15 @@ void CollectionImpl::updateHiddenSetting(OperationContext* opCtx, StringData idx
});
}
+void CollectionImpl::updateUniqueSetting(OperationContext* opCtx, StringData idxName) {
+ int offset = _metadata->findIndexOffset(idxName);
+ invariant(offset >= 0);
+
+ _writeMetadata(opCtx, [&](BSONCollectionCatalogEntry::MetaData& md) {
+ md.indexes[offset].updateUniqueSetting();
+ });
+}
+
std::vector<std::string> CollectionImpl::removeInvalidIndexOptions(OperationContext* opCtx) {
std::vector<std::string> indexesWithInvalidOptions;
diff --git a/src/mongo/db/catalog/collection_impl.h b/src/mongo/db/catalog/collection_impl.h
index 55684e7e275..d1139a023c4 100644
--- a/src/mongo/db/catalog/collection_impl.h
+++ b/src/mongo/db/catalog/collection_impl.h
@@ -434,6 +434,8 @@ public:
void updateHiddenSetting(OperationContext* opCtx, StringData idxName, bool hidden) final;
+ void updateUniqueSetting(OperationContext* opCtx, StringData idxName) final;
+
std::vector<std::string> removeInvalidIndexOptions(OperationContext* opCtx) final;
void setIsTemp(OperationContext* opCtx, bool isTemp) final;
diff --git a/src/mongo/db/catalog/collection_mock.h b/src/mongo/db/catalog/collection_mock.h
index 3340930e5dc..63e0515b92a 100644
--- a/src/mongo/db/catalog/collection_mock.h
+++ b/src/mongo/db/catalog/collection_mock.h
@@ -403,6 +403,10 @@ public:
std::abort();
}
+ void updateUniqueSetting(OperationContext* opCtx, StringData idxName) {
+ std::abort();
+ }
+
std::vector<std::string> removeInvalidIndexOptions(OperationContext* opCtx) {
std::abort();
}
diff --git a/src/mongo/db/storage/bson_collection_catalog_entry.cpp b/src/mongo/db/storage/bson_collection_catalog_entry.cpp
index 5f474a5c82a..fca3b271332 100644
--- a/src/mongo/db/storage/bson_collection_catalog_entry.cpp
+++ b/src/mongo/db/storage/bson_collection_catalog_entry.cpp
@@ -140,6 +140,13 @@ void BSONCollectionCatalogEntry::IndexMetaData::updateHiddenSetting(bool hidden)
}
+void BSONCollectionCatalogEntry::IndexMetaData::updateUniqueSetting() {
+ BSONObjBuilder b(spec);
+ b.appendBool("unique", true);
+ spec = b.obj();
+}
+
+
// --------------------------
int BSONCollectionCatalogEntry::MetaData::getTotalIndexCount() const {
diff --git a/src/mongo/db/storage/bson_collection_catalog_entry.h b/src/mongo/db/storage/bson_collection_catalog_entry.h
index 0d7125445ef..65f08aa3c95 100644
--- a/src/mongo/db/storage/bson_collection_catalog_entry.h
+++ b/src/mongo/db/storage/bson_collection_catalog_entry.h
@@ -97,6 +97,8 @@ public:
void updateHiddenSetting(bool hidden);
+ void updateUniqueSetting();
+
StringData nameStringData() const {
return spec["name"].valueStringDataSafe();
}