summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSulabh Mahajan <sulabh.mahajan@mongodb.com>2018-03-12 10:03:42 +1100
committerSulabh Mahajan <sulabh.mahajan@mongodb.com>2018-03-12 10:03:42 +1100
commit421c1e82b30a14b3bca8e2bf5ef9df2745c5ee7a (patch)
treea63ce70d9d793193301cd11f430d736a7c20149d
parentffde025012ca20c0376a22e3a0f52c1887d03a70 (diff)
downloadmongo-421c1e82b30a14b3bca8e2bf5ef9df2745c5ee7a.tar.gz
SERVER-33607 Use kV2Unique version for new Unique indexes only
-rw-r--r--src/mongo/db/catalog/index_key_validate.cpp12
-rw-r--r--src/mongo/db/index/index_descriptor.cpp12
-rw-r--r--src/mongo/db/index/index_descriptor.h3
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<BSONObj> 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<BSONObj> 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<BSONObj> 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.