diff options
author | Ruoxin Xu <ruoxin.xu@mongodb.com> | 2023-02-13 15:07:09 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-02-13 19:57:50 +0000 |
commit | b7b070ac8a54e06260e4abcbdcb4b34ac6cc4cc4 (patch) | |
tree | b3685300638255901c24765f5ed8d734ac811ade /src/mongo | |
parent | a020219183884dbc5c3188053ad3c31af835e6aa (diff) | |
download | mongo-b7b070ac8a54e06260e4abcbdcb4b34ac6cc4cc4.tar.gz |
SERVER-73733 Format Ordering in SortedDataInterface for compound wildcard indexes
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/catalog/index_catalog_entry_impl.cpp | 5 | ||||
-rw-r--r-- | src/mongo/db/catalog/index_catalog_entry_impl.h | 5 | ||||
-rw-r--r-- | src/mongo/db/index/index_descriptor.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/index/index_descriptor.h | 9 | ||||
-rw-r--r-- | src/mongo/db/index/wildcard_access_method.cpp | 14 | ||||
-rw-r--r-- | src/mongo/db/index/wildcard_access_method.h | 7 | ||||
-rw-r--r-- | src/mongo/db/index/wildcard_key_generator.cpp | 18 | ||||
-rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp | 2 |
8 files changed, 42 insertions, 24 deletions
diff --git a/src/mongo/db/catalog/index_catalog_entry_impl.cpp b/src/mongo/db/catalog/index_catalog_entry_impl.cpp index 65de03edfed..a010b5c61e0 100644 --- a/src/mongo/db/catalog/index_catalog_entry_impl.cpp +++ b/src/mongo/db/catalog/index_catalog_entry_impl.cpp @@ -72,7 +72,6 @@ IndexCatalogEntryImpl::IndexCatalogEntryImpl(OperationContext* const opCtx, : _ident(ident), _descriptor(std::move(descriptor)), _catalogId(collection->getCatalogId()), - _ordering(Ordering::make(_descriptor->keyPattern())), _isReady(false), _isFrozen(isFrozen), _shouldValidateDocument(false), @@ -379,6 +378,10 @@ std::shared_ptr<Ident> IndexCatalogEntryImpl::getSharedIdent() const { return _accessMethod ? _accessMethod->getSharedIdent() : nullptr; } +const Ordering& IndexCatalogEntryImpl::ordering() const { + return _descriptor->ordering(); +} + void IndexCatalogEntryImpl::setIdent(std::shared_ptr<Ident> newIdent) { if (!_accessMethod) return; diff --git a/src/mongo/db/catalog/index_catalog_entry_impl.h b/src/mongo/db/catalog/index_catalog_entry_impl.h index 8955ba64947..00df93261dc 100644 --- a/src/mongo/db/catalog/index_catalog_entry_impl.h +++ b/src/mongo/db/catalog/index_catalog_entry_impl.h @@ -95,9 +95,7 @@ public: _indexBuildInterceptor = interceptor; } - const Ordering& ordering() const final { - return _ordering; - } + const Ordering& ordering() const final; const MatchExpression* getFilterExpression() const final { return _filterExpression.get(); @@ -239,7 +237,6 @@ private: const RecordId _catalogId; // Location in the durable catalog of the collection entry // containing this index entry. - Ordering _ordering; // TODO: this might be b-tree specific bool _isReady; // cache of NamespaceDetails info bool _isFrozen; bool _shouldValidateDocument; diff --git a/src/mongo/db/index/index_descriptor.cpp b/src/mongo/db/index/index_descriptor.cpp index e98a2cced88..1426207c610 100644 --- a/src/mongo/db/index/index_descriptor.cpp +++ b/src/mongo/db/index/index_descriptor.cpp @@ -38,6 +38,7 @@ #include "mongo/db/catalog/index_catalog_entry.h" #include "mongo/db/exec/index_path_projection.h" #include "mongo/db/index/column_key_generator.h" +#include "mongo/db/index/wildcard_access_method.h" #include "mongo/db/index/wildcard_key_generator.h" #include "mongo/db/matcher/expression_parser.h" #include "mongo/db/query/collation/collator_factory_interface.h" @@ -134,7 +135,10 @@ IndexDescriptor::IndexDescriptor(const std::string& accessMethodName, BSONObj in _sparse(infoObj[IndexDescriptor::kSparseFieldName].trueValue()), _unique(_isIdIndex || infoObj[kUniqueFieldName].trueValue()), _hidden(infoObj[kHiddenFieldName].trueValue()), - _partial(!infoObj[kPartialFilterExprFieldName].eoo()) { + _partial(!infoObj[kPartialFilterExprFieldName].eoo()), + _ordering(_indexType == IndexType::INDEX_WILDCARD + ? WildcardAccessMethod::makeOrdering(_keyPattern) + : Ordering::make(_keyPattern)) { BSONElement e = _infoObj[IndexDescriptor::kIndexVersionFieldName]; fassert(50942, e.isNumber()); _version = static_cast<IndexVersion>(e.numberInt()); diff --git a/src/mongo/db/index/index_descriptor.h b/src/mongo/db/index/index_descriptor.h index 349f79bd9a2..26657bf810f 100644 --- a/src/mongo/db/index/index_descriptor.h +++ b/src/mongo/db/index/index_descriptor.h @@ -34,6 +34,7 @@ #include <set> #include <string> +#include "mongo/bson/ordering.h" #include "mongo/db/catalog/index_catalog.h" #include "mongo/db/index/multikey_paths.h" #include "mongo/db/index_names.h" @@ -201,6 +202,11 @@ public: return _version; } + // Return the 'Ordering' of the index keys. + const Ordering& ordering() const { + return _ordering; + } + // May each key only occur once? bool unique() const { return _unique; @@ -332,6 +338,9 @@ private: bool _hidden; bool _partial; IndexVersion _version; + // '_ordering' should be initialized after '_indexType' because different index types may + // require different handling of the Ordering. + Ordering _ordering; BSONObj _collation; BSONObj _partialFilterExpression; bool _prepareUnique = false; diff --git a/src/mongo/db/index/wildcard_access_method.cpp b/src/mongo/db/index/wildcard_access_method.cpp index a3b66caa1d2..08d58cdf5b2 100644 --- a/src/mongo/db/index/wildcard_access_method.cpp +++ b/src/mongo/db/index/wildcard_access_method.cpp @@ -31,6 +31,7 @@ #include "mongo/db/index/index_descriptor.h" #include "mongo/db/index/wildcard_access_method.h" +#include "mongo/db/index_names.h" #include "mongo/db/catalog/index_catalog_entry.h" #include "mongo/db/query/index_bounds_builder.h" @@ -64,4 +65,17 @@ void WildcardAccessMethod::doGetKeys(OperationContext* opCtx, const boost::optional<RecordId>& id) const { _keyGen.generateKeys(pooledBufferBuilder, obj, keys, multikeyMetadataKeys, id); } + +Ordering WildcardAccessMethod::makeOrdering(const BSONObj& pattern) { + BSONObjBuilder newPattern; + for (auto elem : pattern) { + const auto fieldName = elem.fieldNameStringData(); + if (WildcardNames::isWildcardFieldName(fieldName)) { + newPattern.append("$_path", 1); // "$_path" should always be in ascending order. + } + newPattern.append(elem); + } + + return Ordering::make(newPattern.obj()); +} } // namespace mongo diff --git a/src/mongo/db/index/wildcard_access_method.h b/src/mongo/db/index/wildcard_access_method.h index 0e46895513c..3b5e55e3cfa 100644 --- a/src/mongo/db/index/wildcard_access_method.h +++ b/src/mongo/db/index/wildcard_access_method.h @@ -73,6 +73,13 @@ public: return _descriptor->keyPattern(); } + /* + * We should make a new Ordering for wildcard key generator because the index keys generated for + * wildcard indexes include a "$_path" field prior to the wildcard field and the Ordering passed + * in does not account for the "$_path" field. + */ + static Ordering makeOrdering(const BSONObj& pattern); + private: void doGetKeys(OperationContext* opCtx, const CollectionPtr& collection, diff --git a/src/mongo/db/index/wildcard_key_generator.cpp b/src/mongo/db/index/wildcard_key_generator.cpp index 20e9a2e016d..86ee322947e 100644 --- a/src/mongo/db/index/wildcard_key_generator.cpp +++ b/src/mongo/db/index/wildcard_key_generator.cpp @@ -87,22 +87,6 @@ void appendToMultiKeyString(const std::vector<BSONElement>& elems, } } -// We should make a new Ordering for wildcard key generator because the index keys generated for -// wildcard indexes include a "$_path" field prior to the wildcard field and the Ordering passed in -// does not account for the "$_path" field. -Ordering makeOrdering(const BSONObj& pattern) { - BSONObjBuilder newPattern; - for (auto elem : pattern) { - const auto fieldName = elem.fieldNameStringData(); - if (WildcardNames::isWildcardFieldName(fieldName)) { - newPattern.append("$_path", 1); // "$_path" should always be in ascending order. - } - newPattern.append(elem); - } - - return Ordering::make(newPattern.obj()); -} - /** * A helper class for generating all the various types of keys for a wildcard index. * @@ -329,7 +313,7 @@ WildcardKeyGenerator::WildcardKeyGenerator(BSONObj keyPattern, _collator(collator), _keyPattern(keyPattern), _keyStringVersion(keyStringVersion), - _ordering(makeOrdering(keyPattern)), + _ordering(ordering), _rsKeyFormat(rsKeyFormat) { std::vector<const char*> preFields; std::vector<const char*> postFields; diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp index 528f780ce45..8c93f8247ba 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp @@ -214,7 +214,7 @@ WiredTigerIndex::WiredTigerIndex(OperationContext* ctx, bool isLogged) : SortedDataInterface(ident, _handleVersionInfo(ctx, uri, ident, desc, isLogged), - Ordering::make(desc->keyPattern()), + desc->ordering(), rsKeyFormat), _uri(uri), _tableId(WiredTigerSession::genTableId()), |