34 #include "mongo/base/clonable_ptr.h" 35 #include "mongo/base/shim.h" 37 #include "mongo/db/index/multikey_paths.h" 38 #include "mongo/db/jsobj.h" 39 #include "mongo/db/operation_context.h" 40 #include "mongo/db/record_id.h" 41 #include "mongo/db/server_options.h" 48 class IndexDescriptor;
49 class IndexAccessMethod;
50 struct InsertDeleteOptions;
71 virtual Impl* clone_impl()
const = 0;
74 inline std::unique_ptr<Impl>
clone()
const {
75 return std::unique_ptr<Impl>{this->clone_impl()};
78 virtual bool more() = 0;
79 virtual IndexDescriptor* next() = 0;
81 virtual IndexAccessMethod* accessMethod(
const IndexDescriptor*
desc) = 0;
86 static MONGO_DECLARE_SHIM((OperationContext *
opCtx,
89 PrivateTo<IndexIterator>)
90 ->std::unique_ptr<Impl>) makeImpl;
95 const bool includeUnfinishedIndexes)
96 : _pimpl(makeImpl(opCtx, cat, includeUnfinishedIndexes, PrivateCall<
IndexIterator>{})) {
109 return this->_impl().more();
112 inline IndexDescriptor*
next() {
113 return this->_impl().next();
118 return this->_impl().accessMethod(desc);
123 return this->_impl().catalogEntry(desc);
132 static void hook() noexcept;
142 return *this->_pimpl;
147 return *this->_pimpl;
161 virtual bool ok()
const = 0;
163 virtual bool haveAnyIndexes()
const = 0;
165 virtual int numIndexesTotal(OperationContext* opCtx)
const = 0;
167 virtual int numIndexesReady(OperationContext* opCtx)
const = 0;
169 virtual bool haveIdIndex(OperationContext* opCtx)
const = 0;
171 virtual BSONObj getDefaultIdIndexSpec()
const = 0;
173 virtual IndexDescriptor* findIdIndex(OperationContext* opCtx)
const = 0;
175 virtual IndexDescriptor* findIndexByName(OperationContext* opCtx,
179 virtual IndexDescriptor* findIndexByKeyPatternAndCollationSpec(
180 OperationContext* opCtx,
182 const BSONObj& collationSpec,
183 bool includeUnfinishedIndexes)
const = 0;
185 virtual void findIndexesByKeyPattern(OperationContext* opCtx,
187 bool includeUnfinishedIndexes,
188 std::vector<IndexDescriptor*>* matches)
const = 0;
190 virtual IndexDescriptor* findShardKeyPrefixedIndex(OperationContext* opCtx,
191 const BSONObj& shardKey,
192 bool requireSingleKey)
const = 0;
194 virtual void findIndexByType(OperationContext* opCtx,
195 const std::string& type,
196 std::vector<IndexDescriptor*>& matches,
197 bool includeUnfinishedIndexes)
const = 0;
199 virtual const IndexDescriptor* refreshEntry(OperationContext* opCtx,
200 const IndexDescriptor* oldDesc) = 0;
204 virtual IndexAccessMethod* getIndex(
const IndexDescriptor* desc) = 0;
206 virtual const IndexAccessMethod* getIndex(
const IndexDescriptor* desc)
const = 0;
208 virtual Status checkUnfinished()
const = 0;
214 const BSONObj& original)
const = 0;
216 virtual void dropAllIndexes(
217 OperationContext* opCtx,
218 bool includingIdIndex,
219 stdx::function<
void(
const IndexDescriptor*)> onDropFn =
nullptr) = 0;
221 virtual Status dropIndex(OperationContext* opCtx, IndexDescriptor* desc) = 0;
223 virtual std::vector<BSONObj> getAndClearUnfinishedIndexes(OperationContext* opCtx) = 0;
225 virtual bool isMultikey(OperationContext* opCtx,
const IndexDescriptor*
idx) = 0;
227 virtual MultikeyPaths getMultikeyPaths(OperationContext* opCtx,
228 const IndexDescriptor* idx) = 0;
230 virtual Status indexRecords(OperationContext* opCtx,
231 const std::vector<BsonRecord>& bsonRecords,
232 int64_t* keysInsertedOut) = 0;
234 virtual void unindexRecord(OperationContext* opCtx,
238 int64_t* keysDeletedOut) = 0;
240 virtual std::string getAccessMethodName(OperationContext* opCtx,
241 const BSONObj& keyPattern) = 0;
243 virtual Status _upgradeDatabaseMinorVersionIfNeeded(OperationContext* opCtx,
244 const std::string& newPluginName) = 0;
247 virtual const Collection* _getCollection()
const = 0;
251 OperationContext* opCtx,
253 bool initFromDisk) = 0;
259 virtual void _deleteIndexFromDisk(OperationContext*
const opCtx,
260 const std::string& indexName,
261 const std::string& indexNamespace) = 0;
270 PrivateTo<IndexCatalog>)
271 ->std::unique_ptr<Impl>) makeImpl;
276 : _pimpl(makeImpl(this, collection, maxNumIndexesAllowed, PrivateCall<
IndexCatalog>{})) {}
283 return this->_impl().init(opCtx);
286 inline bool ok()
const {
287 return this->_impl().ok();
293 return this->_impl().haveAnyIndexes();
297 return this->_impl().numIndexesTotal(opCtx);
301 return this->_impl().numIndexesReady(opCtx);
305 return numIndexesTotal(opCtx) - numIndexesReady(opCtx);
314 return this->_impl().haveIdIndex(opCtx);
321 return this->_impl().getDefaultIdIndexSpec();
325 return this->_impl().findIdIndex(opCtx);
334 const StringData
name,
350 OperationContext*
const opCtx,
352 const BSONObj& collationSpec,
354 return this->_impl().findIndexByKeyPatternAndCollationSpec(
367 std::vector<IndexDescriptor*>*
const matches)
const {
368 return this->_impl().findIndexesByKeyPattern(opCtx, key, includeUnfinishedIndexes, matches);
385 const BSONObj& shardKey,
386 const bool requireSingleKey)
const {
387 return this->_impl().findShardKeyPrefixedIndex(opCtx, shardKey, requireSingleKey);
391 const std::string& type,
392 std::vector<IndexDescriptor*>& matches,
409 const IndexDescriptor*
const oldDesc) {
410 return this->_impl().refreshEntry(opCtx, oldDesc);
415 return this->_impl().getEntry(desc);
418 inline IndexAccessMethod*
getIndex(
const IndexDescriptor*
const desc) {
419 return this->_impl().getIndex(desc);
422 inline const IndexAccessMethod*
getIndex(
const IndexDescriptor*
const desc)
const {
423 return this->_impl().getIndex(desc);
431 return this->_impl().checkUnfinished();
447 const BSONObj spec) {
448 return this->_impl().createIndexOnEmptyCollection(opCtx, spec);
452 const BSONObj& original)
const {
453 return this->_impl().prepareSpecForCreate(opCtx, original);
462 bool includingIdIndex,
463 stdx::function<
void(
const IndexDescriptor*)> onDropFn =
nullptr) {
464 this->_impl().dropAllIndexes(opCtx, includingIdIndex, onDropFn);
468 return this->_impl().dropIndex(opCtx, desc);
476 return this->_impl().getAndClearUnfinishedIndexes(opCtx);
484 inline bool isMultikey(OperationContext*
const opCtx,
const IndexDescriptor*
const idx) {
485 return this->_impl().isMultikey(opCtx, idx);
498 const IndexDescriptor*
const idx) {
499 return this->_impl().getMultikeyPaths(opCtx, idx);
513 const std::vector<BsonRecord>& bsonRecords,
514 int64_t*
const keysInsertedOut) {
515 return this->_impl().indexRecords(opCtx, bsonRecords, keysInsertedOut);
526 int64_t*
const keysDeletedOut) {
527 return this->_impl().unindexRecord(opCtx, obj, loc, noWarn, keysDeletedOut);
533 const BSONObj& keyPattern) {
534 return this->_impl().getAccessMethodName(opCtx, keyPattern);
543 std::string::size_type getLongestIndexNameLength(OperationContext* opCtx)
const;
547 static MONGO_DECLARE_SHIM((
const BSONObj&
key)->BSONObj) fixIndexKey;
553 static MONGO_DECLARE_SHIM(
554 (OperationContext * opCtx,
const IndexDescriptor*
desc, InsertDeleteOptions*
options)->
void)
555 prepareInsertDeleteOptions;
559 return this->_impl()._getCollection();
563 return this->_impl()._getCollection();
571 return this->_impl()._dropIndex(opCtx, desc);
575 const std::string& newPluginName) {
576 return this->_impl()._upgradeDatabaseMinorVersionIfNeeded(opCtx, newPluginName);
580 return this->_impl()._getEntries();
584 return this->_impl()._getEntries();
596 const std::string& indexName,
597 const std::string& indexNamespace) {
598 return this->_impl()._deleteIndexFromDisk(opCtx, indexName, indexNamespace);
602 OperationContext*
const opCtx,
603 const std::string& indexName,
604 const std::string& indexNamespace) {
612 static void hook() noexcept;
622 return *this->_pimpl;
627 return *this->_pimpl;
Status init(OperationContext *const opCtx)
Definition: index_catalog.h:282
const IndexCatalogEntry * getEntry(const IndexDescriptor *const desc) const
Definition: index_catalog.h:414
void findIndexesByKeyPattern(OperationContext *const opCtx, const BSONObj &key, const bool includeUnfinishedIndexes, std::vector< IndexDescriptor *> *const matches) const
Find indexes with a matching key pattern, putting them into the vector 'matches'. ...
Definition: index_catalog.h:364
IndexAccessMethod * getIndex(const IndexDescriptor *const desc)
Definition: index_catalog.h:418
bool haveAnyIndexes() const
Definition: index_catalog.h:292
Definition: index_catalog.h:131
int numIndexesReady(OperationContext *const opCtx) const
Definition: index_catalog.h:300
Definition: index_catalog_entry.h:272
void findIndexByType(OperationContext *const opCtx, const std::string &type, std::vector< IndexDescriptor *> &matches, const bool includeUnfinishedIndexes=false) const
Definition: index_catalog.h:390
Collection * _getCollection()
Definition: index_catalog.h:562
IndexIterator getIndexIterator(OperationContext *const opCtx, const bool includeUnfinishedIndexes) const
Definition: index_catalog.h:434
Status checkUnfinished() const
Returns a not-ok Status if there are any unfinished index builds.
Definition: index_catalog.h:430
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
StatusWith< BSONObj > createIndexOnEmptyCollection(OperationContext *const opCtx, const BSONObj spec)
Call this only on an empty collection from inside a WriteUnitOfWork.
Definition: index_catalog.h:446
Database *const this_
Definition: database_impl.cpp:82
Collection *const collection
Definition: collection_info_cache_impl.cpp:53
const IndexDescriptor * refreshEntry(OperationContext *const opCtx, const IndexDescriptor *const oldDesc)
Reload the index definition for 'oldDesc' from the CollectionCatalogEntry.
Definition: index_catalog.h:408
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
IndexIterator(OperationContext *const opCtx, const IndexCatalog *const cat, const bool includeUnfinishedIndexes)
Definition: index_catalog.h:93
Status _upgradeDatabaseMinorVersionIfNeeded(OperationContext *const opCtx, const std::string &newPluginName)
Definition: index_catalog.h:574
bool more()
Definition: index_catalog.h:108
IndexCatalogEntryContainer & _getEntries()
Definition: index_catalog.h:583
BSONObj key
Definition: btree_interface.cpp:334
how many: 1 per Collection.
Definition: index_catalog_impl.h:56
bool ok() const
Definition: index_catalog.h:286
BSONObj getDefaultIdIndexSpec() const
Returns the spec for the id index to create by default for this collection.
Definition: index_catalog.h:320
void unindexRecord(OperationContext *const opCtx, const BSONObj &obj, const RecordId &loc, const bool noWarn, int64_t *const keysDeletedOut)
When 'keysDeletedOut' is not null, it will be set to the number of index keys removed by this operati...
Definition: index_catalog.h:522
void _deleteIndexFromDisk(OperationContext *const opCtx, const std::string &indexName, const std::string &indexNamespace)
Definition: index_catalog.h:595
std::unique_ptr< Impl > _pimpl
Definition: index_catalog.h:630
const Impl & _impl() const
Definition: index_catalog.h:620
Definition: index_catalog.h:52
const IndexAccessMethod * getIndex(const IndexDescriptor *const desc) const
Definition: index_catalog.h:422
std::string getAccessMethodName(OperationContext *const opCtx, const BSONObj &keyPattern)
Definition: index_catalog.h:532
friend IndexCatalog
Definition: index_catalog.h:152
friend IndexCatalogEntry
Definition: index_catalog.h:632
this is NOT safe through a yield right now.
Definition: collection.h:160
bool isMultikey(OperationContext *const opCtx, const IndexDescriptor *const idx)
Returns true if the index 'idx' is multikey, and returns false otherwise.
Definition: index_catalog.h:484
Impl & _impl()
Definition: index_catalog.h:145
const IndexDescriptor * idx
Definition: coll_mod.cpp:70
static void _deleteIndexFromDisk(IndexCatalog *const this_, OperationContext *const opCtx, const std::string &indexName, const std::string &indexNamespace)
Definition: index_catalog.h:601
how many: 1 per Collection.
Definition: index_catalog.h:62
Definition: index_catalog.h:611
Impl & _impl()
Definition: index_catalog.h:625
IndexCatalog *const Collection *const const int maxNumIndexesAllowed
Definition: index_catalog_impl.cpp:75
void dropAllIndexes(OperationContext *opCtx, bool includingIdIndex, stdx::function< void(const IndexDescriptor *)> onDropFn=nullptr)
Drops all indexes in the index catalog, optionally dropping the id index depending on the 'includingI...
Definition: index_catalog.h:461
Status dropIndex(OperationContext *const opCtx, IndexDescriptor *const desc)
Definition: index_catalog.h:467
Definition: index_catalog.h:155
Definition: index_catalog.h:66
TUHook() noexcept
Definition: index_catalog.h:614
OperationContext *const const IndexCatalog *const const bool includeUnfinishedIndexes
Definition: index_catalog_impl.cpp:84
Definition: index_key_validate.h:40
Definition: index_catalog_entry.h:56
Builds one or more indexes.
Definition: index_create_impl.h:63
Timestamp ts
Definition: index_catalog.h:54
IndexDescriptor * findShardKeyPrefixedIndex(OperationContext *const opCtx, const BSONObj &shardKey, const bool requireSingleKey) const
Returns an index suitable for shard key range scans.
Definition: index_catalog.h:384
Status _dropIndex(OperationContext *const opCtx, IndexCatalogEntry *const desc)
Definition: index_catalog.h:570
OperationContext *const const IndexCatalog *const cat
Definition: index_catalog_impl.cpp:84
const BSONObj * docPtr
Definition: index_catalog.h:55
std::unique_ptr< Impl > clone() const
Definition: index_catalog.h:74
IndexDescriptor * findIdIndex(OperationContext *const opCtx) const
Definition: index_catalog.h:324
int numIndexesTotal(OperationContext *const opCtx) const
Definition: index_catalog.h:296
TUHook() noexcept
Definition: index_catalog.h:134
IndexCatalogEntry *const OperationContext *const const StringData CollectionCatalogEntry *const std::unique_ptr< IndexDescriptor > descriptor
Definition: index_catalog_entry_impl.cpp:58
friend IndexCatalog
Definition: index_catalog.h:263
const Impl & _impl() const
Definition: index_catalog.h:140
Definition: index_catalog.h:64
bool haveIdIndex(OperationContext *const opCtx) const
this is in "alive" until the Collection goes away in which case everything from this tree has to go a...
Definition: index_catalog.h:313
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
IndexCatalogEntry * catalogEntry(const IndexDescriptor *const desc)
Definition: index_catalog.h:122
static IndexCatalogEntryContainer & _getEntries(IndexCatalog *const this_)
Definition: index_catalog.h:587
IndexDescriptor * next()
Definition: index_catalog.h:112
RecordId id
Definition: index_catalog.h:53
MultikeyPaths getMultikeyPaths(OperationContext *const opCtx, const IndexDescriptor *const idx)
Returns the path components that cause the index 'idx' to be multikey if the index supports path-leve...
Definition: index_catalog.h:497
IndexDescriptor * findIndexByKeyPatternAndCollationSpec(OperationContext *const opCtx, const BSONObj &key, const BSONObj &collationSpec, const bool includeUnfinishedIndexes=false) const
Find index by matching key pattern and collation spec.
Definition: index_catalog.h:349
Status indexRecords(OperationContext *const opCtx, const std::vector< BsonRecord > &bsonRecords, int64_t *const keysInsertedOut)
When 'keysInsertedOut' is not null, it will be set to the number of index keys inserted by this opera...
Definition: index_catalog.h:512
Database *const OperationContext *const const StringData name
Definition: database_impl.cpp:82
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
std::vector< BSONObj > getAndClearUnfinishedIndexes(OperationContext *const opCtx)
will drop all incompleted indexes and return specs after this, the indexes can be rebuilt...
Definition: index_catalog.h:475
StatusWith< BSONObj > prepareSpecForCreate(OperationContext *const opCtx, const BSONObj &original) const
Definition: index_catalog.h:451
OperationContext const IndexDescriptor * desc
Definition: index_catalog_impl.cpp:97
int numIndexesInProgress(OperationContext *const opCtx) const
Definition: index_catalog.h:304
IndexDescriptor * findIndexByName(OperationContext *const opCtx, const StringData name, const bool includeUnfinishedIndexes=false) const
Find index by name.
Definition: index_catalog.h:333
const Collection * _getCollection() const
Definition: index_catalog.h:558
static const IndexCatalogEntryContainer & _getEntries(const IndexCatalog *const this_)
Definition: index_catalog.h:591
const IndexCatalogEntryContainer & _getEntries() const
Definition: index_catalog.h:579
clonable_ptr< Impl > _pimpl
Definition: index_catalog.h:150
IndexCatalog(Collection *const collection, const int maxNumIndexesAllowed)
Definition: index_catalog.h:275
IndexAccessMethod * accessMethod(const IndexDescriptor *const desc)
Definition: index_catalog.h:117