From 421c1e82b30a14b3bca8e2bf5ef9df2745c5ee7a Mon Sep 17 00:00:00 2001 From: Sulabh Mahajan Date: Mon, 12 Mar 2018 10:03:42 +1100 Subject: SERVER-33607 Use kV2Unique version for new Unique indexes only --- src/mongo/db/catalog/index_key_validate.cpp | 12 ++++++++++-- src/mongo/db/index/index_descriptor.cpp | 12 ++++++++++-- src/mongo/db/index/index_descriptor.h | 3 ++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/mongo/db/catalog/index_key_validate.cpp b/src/mongo/db/catalog/index_key_validate.cpp index 4c1695069d5..25f7c43910f 100644 --- a/src/mongo/db/catalog/index_key_validate.cpp +++ b/src/mongo/db/catalog/index_key_validate.cpp @@ -220,6 +220,7 @@ StatusWith validateIndexSpec( bool hasNamespaceField = false; bool hasVersionField = false; bool hasCollationField = false; + bool isUniqueIndex = false; auto fieldNamesValidStatus = validateIndexSpecFieldNames(indexSpec); if (!fieldNamesValidStatus.isOK()) { @@ -364,6 +365,13 @@ StatusWith validateIndexSpec( if (!statusWithMatcher.isOK()) { return statusWithMatcher.getStatus(); } + } else if (IndexDescriptor::kUniqueFieldName == indexSpecElemFieldName) { + // Note: Here we only consider whether or not "unique" field is specified and that its + // value evaluates to true. "_id" index for instance is unique, but the index spec for + // it doesn't carry a "unique" field. "isUniqueIndex" being false for "_id" indexes is + // on purpose, and in future if we were to make "_id" index specs include + // "unique:true", then we would need to change the logic here to preserve its behavior. + isUniqueIndex = indexSpecElem.trueValue(); } else { // We can assume field name is valid at this point. Validation of fieldname is handled // prior to this in validateIndexSpecFieldNames(). @@ -372,8 +380,8 @@ StatusWith validateIndexSpec( } if (!resolvedIndexVersion) { - resolvedIndexVersion = - IndexDescriptor::getDefaultIndexVersion(featureCompatibility.getVersion()); + resolvedIndexVersion = IndexDescriptor::getDefaultIndexVersion( + featureCompatibility.getVersion(), isUniqueIndex); } if (!hasKeyPatternField) { diff --git a/src/mongo/db/index/index_descriptor.cpp b/src/mongo/db/index/index_descriptor.cpp index 9c72069f191..ca9ea36ece0 100644 --- a/src/mongo/db/index/index_descriptor.cpp +++ b/src/mongo/db/index/index_descriptor.cpp @@ -130,11 +130,19 @@ Status IndexDescriptor::isIndexVersionAllowedForCreation( } IndexVersion IndexDescriptor::getDefaultIndexVersion( - ServerGlobalParams::FeatureCompatibility::Version featureCompatibilityVersion) { + ServerGlobalParams::FeatureCompatibility::Version featureCompatibilityVersion, + bool isUniqueIndex) { // The gating variable would allow creation of V2 format unique index when set to true. + // Note: Here "isUniqueIndex" only considers whether or not "unique" field is specified + // and that its value evaluates to true. "_id" index for instance is unique, but the + // index spec for it doesn't carry a "unique" field. "isUniqueIndex" being false for + // "_id" indexes is on purpose, and in future if we were to make "_id" index specs + // include "unique:true", then we would need to change the logic here to preserve its + // behavior. const bool useV2UniqueIndexFormat = false; - if (useV2UniqueIndexFormat) + if (useV2UniqueIndexFormat && isUniqueIndex) { return IndexVersion::kV2Unique; + } return IndexVersion::kV2; } diff --git a/src/mongo/db/index/index_descriptor.h b/src/mongo/db/index/index_descriptor.h index f5c8a46b76c..f1135ca3f91 100644 --- a/src/mongo/db/index/index_descriptor.h +++ b/src/mongo/db/index/index_descriptor.h @@ -133,7 +133,8 @@ public: * Returns the index version to use if it isn't specified in the index specification. */ static IndexVersion getDefaultIndexVersion( - ServerGlobalParams::FeatureCompatibility::Version featureCompatibilityVersion); + ServerGlobalParams::FeatureCompatibility::Version featureCompatibilityVersion, + bool isUniqueIndex = false); // // Information about the key pattern. -- cgit v1.2.1