From a2a00ec41d88ee36fd3250f493291161d4c0bd91 Mon Sep 17 00:00:00 2001 From: y123456yz <3058078489@qq.com> Date: Tue, 9 Jan 2024 10:53:16 +0800 Subject: [PATCH 1/2] avoid create_duplicate_btree_indexes --- src/mongo/db/index/index_descriptor.cpp | 36 +++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/mongo/db/index/index_descriptor.cpp b/src/mongo/db/index/index_descriptor.cpp index 5db5477942dc9..00e7a34355201 100644 --- a/src/mongo/db/index/index_descriptor.cpp +++ b/src/mongo/db/index/index_descriptor.cpp @@ -247,9 +247,41 @@ IndexDescriptor::Comparison IndexDescriptor::compareIndexOptions( const IndexCatalogEntry* existingIndex) const { auto existingIndexDesc = existingIndex->descriptor(); + //Index check, avoid creating duplicate indexes. + //For example: + // Add two indexes: db.collection.createIndex({a:1}) and db.collection.createIndex({a:11}) + // The two indexes are actually the same, One of them is a useless index + auto dealIndexKeyPattern = [&](const BSONObj& indexKeyPattern) { + if (getIndexType() != INDEX_BTREE) { + return indexKeyPattern; + } + + //Hidden index is not restricted + if (hidden() == true) { + return indexKeyPattern; + } + + BSONObjBuilder build; + BSONObjIterator kpIt(indexKeyPattern); + while (kpIt.more()) { + BSONElement elt = kpIt.next(); + + // The canonical check as to whether a key pattern element is "ascending" or "descending" is + // (elt.number() >= 0). This is defined by the Ordering class. + invariant(elt.isNumber()); + int sortOrder = (elt.number() >= 0) ? 1 : -1; + build.append(elt.fieldName(), sortOrder); + } + + return build.obj(); + }; + + BSONObj newIndexKeyPattern = dealIndexKeyPattern(keyPattern()); + BSONObj existingIndexKeyPattern = dealIndexKeyPattern(existingIndexDesc->keyPattern()); + // We first check whether the key pattern is identical for both indexes. - if (SimpleBSONObjComparator::kInstance.evaluate(keyPattern() != - existingIndexDesc->keyPattern())) { + if (SimpleBSONObjComparator::kInstance.evaluate(newIndexKeyPattern != + existingIndexKeyPattern)) { return Comparison::kDifferent; } From 046e439fa90668d77ae9e8df540cb8f579661acc Mon Sep 17 00:00:00 2001 From: y123456yz <3058078489@qq.com> Date: Mon, 15 Jan 2024 13:49:45 +0800 Subject: [PATCH 2/2] func name perfect --- src/mongo/db/index/index_descriptor.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mongo/db/index/index_descriptor.cpp b/src/mongo/db/index/index_descriptor.cpp index 00e7a34355201..66a727db05666 100644 --- a/src/mongo/db/index/index_descriptor.cpp +++ b/src/mongo/db/index/index_descriptor.cpp @@ -251,7 +251,7 @@ IndexDescriptor::Comparison IndexDescriptor::compareIndexOptions( //For example: // Add two indexes: db.collection.createIndex({a:1}) and db.collection.createIndex({a:11}) // The two indexes are actually the same, One of them is a useless index - auto dealIndexKeyPattern = [&](const BSONObj& indexKeyPattern) { + auto canonizeIndexKeyPattern = [this](const BSONObj& indexKeyPattern) { if (getIndexType() != INDEX_BTREE) { return indexKeyPattern; } @@ -276,8 +276,8 @@ IndexDescriptor::Comparison IndexDescriptor::compareIndexOptions( return build.obj(); }; - BSONObj newIndexKeyPattern = dealIndexKeyPattern(keyPattern()); - BSONObj existingIndexKeyPattern = dealIndexKeyPattern(existingIndexDesc->keyPattern()); + BSONObj newIndexKeyPattern = canonizeIndexKeyPattern(keyPattern()); + BSONObj existingIndexKeyPattern = canonizeIndexKeyPattern(existingIndexDesc->keyPattern()); // We first check whether the key pattern is identical for both indexes. if (SimpleBSONObjComparator::kInstance.evaluate(newIndexKeyPattern !=