diff options
-rw-r--r-- | src/mongo/db/catalog/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection.h | 32 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_compact.cpp | 43 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_compact.h | 47 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_impl.h | 2 | ||||
-rw-r--r-- | src/mongo/db/catalog/collection_mock.h | 3 | ||||
-rw-r--r-- | src/mongo/db/commands/compact.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/storage/record_store.h | 16 |
8 files changed, 88 insertions, 64 deletions
diff --git a/src/mongo/db/catalog/SConscript b/src/mongo/db/catalog/SConscript index 42c07b8995d..e0400100f20 100644 --- a/src/mongo/db/catalog/SConscript +++ b/src/mongo/db/catalog/SConscript @@ -234,7 +234,6 @@ env.Library( target='catalog_impl', source=[ "catalog_control.cpp", - "collection_compact.cpp", "collection_impl.cpp", "collection_info_cache_impl.cpp", "database_holder_impl.cpp", @@ -288,6 +287,7 @@ env.Library( source=[ 'capped_utils.cpp', 'coll_mod.cpp', + "collection_compact.cpp", 'create_collection.cpp', 'drop_collection.cpp', 'drop_database.cpp', @@ -306,6 +306,7 @@ env.Library( 'collection', 'database', 'index_catalog', + 'index_key_validate', ], ) diff --git a/src/mongo/db/catalog/collection.h b/src/mongo/db/catalog/collection.h index bb38fb4b25b..b89013df2cf 100644 --- a/src/mongo/db/catalog/collection.h +++ b/src/mongo/db/catalog/collection.h @@ -74,30 +74,6 @@ class RecordCursor; class UpdateDriver; class UpdateRequest; -struct CompactOptions { - // padding - enum PaddingMode { PRESERVE, NONE, MANUAL } paddingMode = NONE; - - // only used if _paddingMode == MANUAL - double paddingFactor = 1; // what to multiple document size by - int paddingBytes = 0; // what to add to ducment size after multiplication - - // other - bool validateDocuments = true; - - std::string toString() const; - - unsigned computeRecordSize(unsigned recordSize) const { - recordSize = static_cast<unsigned>(paddingFactor * recordSize); - recordSize += paddingBytes; - return recordSize; - } -}; - -struct CompactStats { - long long corruptDocuments = 0; -}; - /** * Holds information update an update operation. */ @@ -291,9 +267,6 @@ public: const mutablebson::DamageVector& damages, CollectionUpdateArgs* args) = 0; - virtual StatusWith<CompactStats> compact(OperationContext* opCtx, - const CompactOptions* options) = 0; - virtual Status truncate(OperationContext* opCtx) = 0; virtual Status validate(OperationContext* opCtx, @@ -574,11 +547,6 @@ public: // ----------- - inline StatusWith<CompactStats> compact(OperationContext* const opCtx, - const CompactOptions* const options) { - return this->_impl().compact(opCtx, options); - } - /** * removes all documents as fast as possible * indexes before and after will be the same diff --git a/src/mongo/db/catalog/collection_compact.cpp b/src/mongo/db/catalog/collection_compact.cpp index dc339c7f7a8..89d840d5d47 100644 --- a/src/mongo/db/catalog/collection_compact.cpp +++ b/src/mongo/db/catalog/collection_compact.cpp @@ -30,20 +30,15 @@ #define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kStorage -#include "mongo/db/catalog/collection_impl.h" +#include "mongo/db/catalog/collection_compact.h" -#include "mongo/base/counter.h" -#include "mongo/base/owned_pointer_map.h" -#include "mongo/db/catalog/database.h" #include "mongo/db/catalog/document_validation.h" #include "mongo/db/catalog/index_key_validate.h" #include "mongo/db/catalog/multi_index_block.h" -#include "mongo/db/clientcursor.h" -#include "mongo/db/commands/server_status.h" -#include "mongo/db/curop.h" #include "mongo/db/index/index_access_method.h" #include "mongo/db/index/index_descriptor.h" #include "mongo/db/operation_context.h" +#include "mongo/util/assert_util.h" #include "mongo/util/log.h" namespace mongo { @@ -77,29 +72,33 @@ private: } -StatusWith<CompactStats> CollectionImpl::compact(OperationContext* opCtx, - const CompactOptions* compactOptions) { - dassert(opCtx->lockState()->isCollectionLockedForMode(ns().toString(), MODE_X)); +StatusWith<CompactStats> compactCollection(OperationContext* opCtx, + Collection* collection, + const CompactOptions* compactOptions) { + dassert(opCtx->lockState()->isCollectionLockedForMode(collection->ns().toString(), MODE_X)); DisableDocumentValidation validationDisabler(opCtx); - if (!_recordStore->compactSupported()) + auto recordStore = collection->getRecordStore(); + auto indexCatalog = collection->getIndexCatalog(); + + if (!recordStore->compactSupported()) return StatusWith<CompactStats>(ErrorCodes::CommandNotSupported, str::stream() << "cannot compact collection with record store: " - << _recordStore->name()); + << recordStore->name()); - if (_recordStore->compactsInPlace()) { + if (recordStore->compactsInPlace()) { CompactStats stats; - Status status = _recordStore->compact(opCtx, NULL, compactOptions, &stats); + Status status = recordStore->compact(opCtx, nullptr, compactOptions, &stats); if (!status.isOK()) return StatusWith<CompactStats>(status); // Compact all indexes (not including unfinished indexes) - IndexCatalog::IndexIterator ii(_indexCatalog->getIndexIterator(opCtx, false)); + IndexCatalog::IndexIterator ii(indexCatalog->getIndexIterator(opCtx, false)); while (ii.more()) { IndexDescriptor* descriptor = ii.next(); - IndexAccessMethod* index = _indexCatalog->getIndex(descriptor); + IndexAccessMethod* index = indexCatalog->getIndex(descriptor); LOG(1) << "compacting index: " << descriptor->toString(); Status status = index->compact(opCtx); @@ -112,13 +111,13 @@ StatusWith<CompactStats> CollectionImpl::compact(OperationContext* opCtx, return StatusWith<CompactStats>(stats); } - if (_indexCatalog->numIndexesInProgress(opCtx)) + if (indexCatalog->numIndexesInProgress(opCtx)) return StatusWith<CompactStats>(ErrorCodes::BadValue, "cannot compact when indexes in progress"); std::vector<BSONObj> indexSpecs; { - IndexCatalog::IndexIterator ii(_indexCatalog->getIndexIterator(opCtx, false)); + IndexCatalog::IndexIterator ii(indexCatalog->getIndexIterator(opCtx, false)); while (ii.more()) { IndexDescriptor* descriptor = ii.next(); @@ -149,13 +148,13 @@ StatusWith<CompactStats> CollectionImpl::compact(OperationContext* opCtx, // which is important and wanted here WriteUnitOfWork wunit(opCtx); log() << "compact dropping indexes"; - _indexCatalog->dropAllIndexes(opCtx, true); + indexCatalog->dropAllIndexes(opCtx, true); wunit.commit(); } CompactStats stats; - auto indexerPtr = createMultiIndexBlock(opCtx); + auto indexerPtr = collection->createMultiIndexBlock(opCtx); MultiIndexBlock& indexer(*indexerPtr); indexer.allowInterruption(); indexer.ignoreUniqueConstraint(); // in compact we should be doing no checking @@ -164,9 +163,9 @@ StatusWith<CompactStats> CollectionImpl::compact(OperationContext* opCtx, if (!status.isOK()) return StatusWith<CompactStats>(status); - MyCompactAdaptor adaptor(_this, &indexer); + MyCompactAdaptor adaptor(collection, &indexer); - status = _recordStore->compact(opCtx, &adaptor, compactOptions, &stats); + status = recordStore->compact(opCtx, &adaptor, compactOptions, &stats); if (!status.isOK()) return StatusWith<CompactStats>(status); diff --git a/src/mongo/db/catalog/collection_compact.h b/src/mongo/db/catalog/collection_compact.h new file mode 100644 index 00000000000..65ebd6afe4b --- /dev/null +++ b/src/mongo/db/catalog/collection_compact.h @@ -0,0 +1,47 @@ + +/** + * Copyright (C) 2018-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +#include "mongo/base/status_with.h" +#include "mongo/db/catalog/collection.h" +#include "mongo/db/storage/record_store.h" + +namespace mongo { + +/** + * Compacts collection. + * See record_store.h for CompactStats and CompactOptions definitions. + */ +StatusWith<CompactStats> compactCollection(OperationContext* opCtx, + Collection* collection, + const CompactOptions* options); + +} // namespace mongo diff --git a/src/mongo/db/catalog/collection_impl.h b/src/mongo/db/catalog/collection_impl.h index 1eb30230065..a06aec3274f 100644 --- a/src/mongo/db/catalog/collection_impl.h +++ b/src/mongo/db/catalog/collection_impl.h @@ -227,8 +227,6 @@ public: // ----------- - StatusWith<CompactStats> compact(OperationContext* opCtx, const CompactOptions* options) final; - /** * removes all documents as fast as possible * indexes before and after will be the same diff --git a/src/mongo/db/catalog/collection_mock.h b/src/mongo/db/catalog/collection_mock.h index f74550f0f7d..85ab80f2a27 100644 --- a/src/mongo/db/catalog/collection_mock.h +++ b/src/mongo/db/catalog/collection_mock.h @@ -182,9 +182,6 @@ public: std::abort(); } - StatusWith<CompactStats> compact(OperationContext* opCtx, const CompactOptions* options) { - std::abort(); - } Status truncate(OperationContext* opCtx) { std::abort(); } diff --git a/src/mongo/db/commands/compact.cpp b/src/mongo/db/commands/compact.cpp index 93bb813c56e..3c0400a77ea 100644 --- a/src/mongo/db/commands/compact.cpp +++ b/src/mongo/db/commands/compact.cpp @@ -40,6 +40,7 @@ #include "mongo/db/auth/privilege.h" #include "mongo/db/background.h" #include "mongo/db/catalog/collection.h" +#include "mongo/db/catalog/collection_compact.h" #include "mongo/db/catalog/database.h" #include "mongo/db/commands.h" #include "mongo/db/concurrency/d_concurrency.h" @@ -164,12 +165,9 @@ public: log() << "compact " << nss.ns() << " begin, options: " << compactOptions; - StatusWith<CompactStats> status = collection->compact(opCtx, &compactOptions); + StatusWith<CompactStats> status = compactCollection(opCtx, collection, &compactOptions); uassertStatusOK(status.getStatus()); - if (status.getValue().corruptDocuments > 0) - result.append("invalidObjects", status.getValue().corruptDocuments); - log() << "compact " << nss.ns() << " end"; return true; diff --git a/src/mongo/db/storage/record_store.h b/src/mongo/db/storage/record_store.h index 77742b69077..325a196df40 100644 --- a/src/mongo/db/storage/record_store.h +++ b/src/mongo/db/storage/record_store.h @@ -56,6 +56,22 @@ class RecordStore; struct ValidateResults; class ValidateAdaptor; +struct CompactOptions { + // padding + enum PaddingMode { PRESERVE, NONE, MANUAL } paddingMode = NONE; + + // only used if _paddingMode == MANUAL + double paddingFactor = 1; // what to multiple document size by + int paddingBytes = 0; // what to add to ducment size after multiplication + + // other + bool validateDocuments = true; + + std::string toString() const; +}; + +struct CompactStats {}; + /** * Allows inserting a Record "in-place" without creating a copy ahead of time. */ |