diff options
author | Gregory Noma <gregory.noma@gmail.com> | 2021-12-16 14:37:12 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-12-16 15:04:03 +0000 |
commit | 4cfcc10775e2cab05a6e30c2516994ab67b9bd7d (patch) | |
tree | b59e0c3fa34a6019638740c592311659e1f78015 /src/mongo/db/index/index_access_method.cpp | |
parent | f7867c703aa0f72e0554ad056a74115a5cacb95b (diff) | |
download | mongo-4cfcc10775e2cab05a6e30c2516994ab67b9bd7d.tar.gz |
SERVER-62056 Improve `Sorter` code structure
Diffstat (limited to 'src/mongo/db/index/index_access_method.cpp')
-rw-r--r-- | src/mongo/db/index/index_access_method.cpp | 82 |
1 files changed, 32 insertions, 50 deletions
diff --git a/src/mongo/db/index/index_access_method.cpp b/src/mongo/db/index/index_access_method.cpp index 516fd55a841..5568a982c33 100644 --- a/src/mongo/db/index/index_access_method.cpp +++ b/src/mongo/db/index/index_access_method.cpp @@ -50,6 +50,7 @@ #include "mongo/db/operation_context.h" #include "mongo/db/repl/replication_coordinator.h" #include "mongo/db/repl/timestamp_block.h" +#include "mongo/db/sorter/factory.h" #include "mongo/db/storage/execution_context.h" #include "mongo/db/storage/storage_options.h" #include "mongo/logv2/log.h" @@ -82,12 +83,12 @@ bool isMultikeyFromPaths(const MultikeyPaths& multikeyPaths) { [](const MultikeyComponents& components) { return !components.empty(); }); } -SortOptions makeSortOptions(size_t maxMemoryUsageBytes, StringData dbName) { - return SortOptions() - .TempDir(storageGlobalParams.dbpath + "/_tmp") - .ExtSortAllowed() - .MaxMemoryUsageBytes(maxMemoryUsageBytes) - .DBName(dbName.toString()); +sorter::Options makeSortOptions(size_t maxMemoryUsageBytes, StringData dbName) { + sorter::Options options; + options.tempDir = storageGlobalParams.dbpath + "/_tmp"; + options.maxMemoryUsageBytes = maxMemoryUsageBytes; + options.dbName = dbName.toString(); + return options; } MultikeyPaths createMultikeyPaths(const std::vector<MultikeyPath>& multikeyPathsVec) { @@ -101,15 +102,13 @@ MultikeyPaths createMultikeyPaths(const std::vector<MultikeyPath>& multikeyPaths return multikeyPaths; } -} // namespace - -struct BtreeExternalSortComparison { - typedef std::pair<KeyString::Value, mongo::NullValue> Data; - int operator()(const Data& l, const Data& r) const { - return l.first.compare(r.first); - } +auto btreeExternalSortComparison = [](const std::pair<KeyString::Value, sorter::NullValue>& lhs, + const std::pair<KeyString::Value, sorter::NullValue>& rhs) { + return lhs.first.compare(rhs.first); }; +} // namespace + AbstractIndexAccessMethod::AbstractIndexAccessMethod(const IndexCatalogEntry* btreeState, std::unique_ptr<SortedDataInterface> btree) : _indexCatalogEntry(btreeState), @@ -521,7 +520,7 @@ public: * Inserts all multikey metadata keys cached during the BulkBuilder's lifetime into the * underlying Sorter, finalizes it, and returns an iterator over the sorted dataset. */ - Sorter::Iterator* done() final; + std::unique_ptr<Sorter::Iterator> done() final; int64_t getKeysInserted() const final; @@ -530,7 +529,7 @@ public: private: void _insertMultikeyMetadataKeysIntoSorter(); - Sorter* _makeSorter( + std::unique_ptr<Sorter> _makeSorter( size_t maxMemoryUsageBytes, StringData dbName, boost::optional<StringData> fileName = boost::none, @@ -645,7 +644,7 @@ Status AbstractIndexAccessMethod::BulkBuilderImpl::insert( } for (const auto& keyString : *keys) { - _sorter->add(keyString, mongo::NullValue()); + _sorter->add(keyString, sorter::NullValue()); ++_keysInserted; } @@ -664,10 +663,10 @@ bool AbstractIndexAccessMethod::BulkBuilderImpl::isMultikey() const { return _isMultiKey; } -IndexAccessMethod::BulkBuilder::Sorter::Iterator* +std::unique_ptr<IndexAccessMethod::BulkBuilder::Sorter::Iterator> AbstractIndexAccessMethod::BulkBuilderImpl::done() { _insertMultikeyMetadataKeysIntoSorter(); - return _sorter->done(); + return _sorter->done(Sorter::Iterator::ReturnPolicy::kCopy); } int64_t AbstractIndexAccessMethod::BulkBuilderImpl::getKeysInserted() const { @@ -682,7 +681,7 @@ AbstractIndexAccessMethod::BulkBuilderImpl::persistDataForShutdown() { void AbstractIndexAccessMethod::BulkBuilderImpl::_insertMultikeyMetadataKeysIntoSorter() { for (const auto& keyString : _multikeyMetadataKeys) { - _sorter->add(keyString, mongo::NullValue()); + _sorter->add(keyString, sorter::NullValue()); ++_keysInserted; } @@ -694,24 +693,27 @@ void AbstractIndexAccessMethod::BulkBuilderImpl::_insertMultikeyMetadataKeysInto AbstractIndexAccessMethod::BulkBuilderImpl::Sorter::Settings AbstractIndexAccessMethod::BulkBuilderImpl::_makeSorterSettings() const { return std::pair<KeyString::Value::SorterDeserializeSettings, - mongo::NullValue::SorterDeserializeSettings>( + sorter::NullValue::SorterDeserializeSettings>( {_indexCatalogEntry->accessMethod()->getSortedDataInterface()->getKeyStringVersion()}, {}); } -AbstractIndexAccessMethod::BulkBuilderImpl::Sorter* +std::unique_ptr<AbstractIndexAccessMethod::BulkBuilderImpl::Sorter> AbstractIndexAccessMethod::BulkBuilderImpl::_makeSorter( size_t maxMemoryUsageBytes, StringData dbName, boost::optional<StringData> fileName, const boost::optional<std::vector<SorterRange>>& ranges) const { - return fileName ? Sorter::makeFromExistingRanges(fileName->toString(), - *ranges, - makeSortOptions(maxMemoryUsageBytes, dbName), - BtreeExternalSortComparison(), - _makeSorterSettings()) - : Sorter::make(makeSortOptions(maxMemoryUsageBytes, dbName), - BtreeExternalSortComparison(), - _makeSorterSettings()); + return fileName ? sorter::makeFromExistingRanges<KeyString::Value, sorter::NullValue>( + fileName->toString(), + *ranges, + makeSortOptions(maxMemoryUsageBytes, dbName), + btreeExternalSortComparison, + _makeSorterSettings()) + : sorter::make<KeyString::Value, sorter::NullValue>( + "index", + makeSortOptions(maxMemoryUsageBytes, dbName), + btreeExternalSortComparison, + _makeSorterSettings()); } void AbstractIndexAccessMethod::_yieldBulkLoad(OperationContext* opCtx, @@ -757,7 +759,7 @@ Status AbstractIndexAccessMethod::commitBulk(OperationContext* opCtx, auto ns = _indexCatalogEntry->getNSSFromCatalog(opCtx); - std::unique_ptr<BulkBuilder::Sorter::Iterator> it(bulk->done()); + auto it = bulk->done(); static constexpr char message[] = "Index Build: inserting keys from external sorter into index"; ProgressMeterHolder pm; @@ -962,23 +964,6 @@ SortedDataInterface* AbstractIndexAccessMethod::getSortedDataInterface() const { return _newInterface.get(); } -/** - * Generates a new file name on each call using a static, atomic and monotonically increasing - * number. Each name is suffixed with a random number generated at startup, to prevent name - * collisions when the index build external sort files are preserved across restarts. - * - * Each user of the Sorter must implement this function to ensure that all temporary files that the - * Sorter instances produce are uniquely identified using a unique file name extension with separate - * atomic variable. This is necessary because the sorter.cpp code is separately included in multiple - * places, rather than compiled in one place and linked, and so cannot provide a globally unique ID. - */ -std::string nextFileName() { - static AtomicWord<unsigned> indexAccessMethodFileCounter; - static const int64_t randomSuffix = SecureRandom().nextInt64(); - return str::stream() << "extsort-index." << indexAccessMethodFileCounter.fetchAndAdd(1) << '-' - << randomSuffix; -} - Status AbstractIndexAccessMethod::_handleDuplicateKey(OperationContext* opCtx, const KeyString::Value& dataKey, const RecordIdHandlerFn& onDuplicateRecord) { @@ -997,6 +982,3 @@ Status AbstractIndexAccessMethod::_handleDuplicateKey(OperationContext* opCtx, _descriptor->collation()); } } // namespace mongo - -#include "mongo/db/sorter/sorter.cpp" -MONGO_CREATE_SORTER(mongo::KeyString::Value, mongo::NullValue, mongo::BtreeExternalSortComparison); |