Storage Engine API
index_catalog_impl.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <vector>
32 
34 
36 #include "mongo/db/index/multikey_paths.h"
37 #include "mongo/db/jsobj.h"
38 #include "mongo/db/operation_context.h"
39 #include "mongo/db/record_id.h"
40 #include "mongo/db/server_options.h"
42 
43 namespace mongo {
44 
45 class Client;
46 class Collection;
47 
48 class IndexDescriptor;
49 class IndexAccessMethod;
50 struct InsertDeleteOptions;
51 
57 public:
61  ~IndexCatalogImpl() override;
62 
63  // must be called before used
64  Status init(OperationContext* opCtx) override;
65 
66  bool ok() const override;
67 
68  // ---- accessors -----
69 
70  bool haveAnyIndexes() const override;
71  int numIndexesTotal(OperationContext* opCtx) const override;
72  int numIndexesReady(OperationContext* opCtx) const override;
73  int numIndexesInProgress(OperationContext* opCtx) const {
74  return numIndexesTotal(opCtx) - numIndexesReady(opCtx);
75  }
76 
82  bool haveIdIndex(OperationContext* opCtx) const override;
83 
87  BSONObj getDefaultIdIndexSpec() const override;
88 
89  IndexDescriptor* findIdIndex(OperationContext* opCtx) const override;
90 
96  IndexDescriptor* findIndexByName(OperationContext* opCtx,
97  StringData name,
98  bool includeUnfinishedIndexes = false) const override;
99 
110  IndexDescriptor* findIndexByKeyPatternAndCollationSpec(
111  OperationContext* opCtx,
112  const BSONObj& key,
113  const BSONObj& collationSpec,
114  bool includeUnfinishedIndexes = false) const override;
115 
122  void findIndexesByKeyPattern(OperationContext* opCtx,
123  const BSONObj& key,
125  std::vector<IndexDescriptor*>* matches) const override;
126 
140  IndexDescriptor* findShardKeyPrefixedIndex(OperationContext* opCtx,
141  const BSONObj& shardKey,
142  bool requireSingleKey) const override;
143 
144  void findIndexByType(OperationContext* opCtx,
145  const std::string& type,
146  std::vector<IndexDescriptor*>& matches,
147  bool includeUnfinishedIndexes = false) const override;
148 
149 
161  const IndexDescriptor* refreshEntry(OperationContext* opCtx,
162  const IndexDescriptor* oldDesc) override;
163 
164  // never returns NULL
165  const IndexCatalogEntry* getEntry(const IndexDescriptor* desc) const override;
166 
167  IndexAccessMethod* getIndex(const IndexDescriptor* desc) override;
168  const IndexAccessMethod* getIndex(const IndexDescriptor* desc) const override;
169 
174  Status checkUnfinished() const override;
175 
177  public:
178  IndexIteratorImpl(OperationContext* opCtx,
179  const IndexCatalog* cat,
180  bool includeUnfinishedIndexes);
181 
182  bool more() override;
183  IndexDescriptor* next() override;
184 
185  // returns the access method for the last return IndexDescriptor
186  IndexAccessMethod* accessMethod(const IndexDescriptor* desc) override;
187 
188  // returns the IndexCatalogEntry for the last return IndexDescriptor
189  IndexCatalogEntry* catalogEntry(const IndexDescriptor* desc) override;
190 
191  private:
192  IndexIteratorImpl* clone_impl() const override;
193 
194  void _advance();
195 
197 
198  OperationContext* const _opCtx;
201 
202  bool _start; // only true before we've called next() or more()
203 
206 
207  friend class IndexCatalog;
208  };
209 
211 
212  // ---- index set modifiers ------
213 
219  StatusWith<BSONObj> createIndexOnEmptyCollection(OperationContext* opCtx,
220  BSONObj spec) override;
221 
222  StatusWith<BSONObj> prepareSpecForCreate(OperationContext* opCtx,
223  const BSONObj& original) const override;
224 
230  void dropAllIndexes(OperationContext* opCtx,
231  bool includingIdIndex,
232  stdx::function<void(const IndexDescriptor*)> onDropFn = nullptr) override;
233 
234  Status dropIndex(OperationContext* opCtx, IndexDescriptor* desc) override;
235 
240  std::vector<BSONObj> getAndClearUnfinishedIndexes(OperationContext* opCtx) override;
241 
242 
244  std::string ns;
245  std::string name;
246  BSONObj key;
247  };
248 
249  // ---- modify single index
250 
254  bool isMultikey(OperationContext* opCtx, const IndexDescriptor* idx) override;
255 
265  MultikeyPaths getMultikeyPaths(OperationContext* opCtx, const IndexDescriptor* idx) override;
266 
267  // --- these probably become private?
268 
269 
280  MONGO_DISALLOW_COPYING(IndexBuildBlock);
281 
282  public:
283  IndexBuildBlock(OperationContext* opCtx, Collection* collection, const BSONObj& spec);
284 
285  ~IndexBuildBlock();
286 
290  Status init();
291 
295  void success();
296 
302  void fail();
303 
305  return _entry;
306  }
307 
308  const std::string& getIndexName() const {
309  return _indexName;
310  }
311 
312  const BSONObj& getSpec() const {
313  return _spec;
314  }
315 
316  private:
319  const std::string _ns;
320 
321  BSONObj _spec;
322 
323  std::string _indexName;
324  std::string _indexNamespace;
325 
328 
329  OperationContext* _opCtx;
330  };
331 
332  // ----- data modifiers ------
333 
340  Status indexRecords(OperationContext* opCtx,
341  const std::vector<BsonRecord>& bsonRecords,
342  int64_t* keysInsertedOut) override;
343 
348  void unindexRecord(OperationContext* opCtx,
349  const BSONObj& obj,
350  const RecordId& loc,
351  bool noWarn,
352  int64_t* keysDeletedOut) override;
353 
354  // ------- temp internal -------
355 
356  inline std::string getAccessMethodName(OperationContext* opCtx,
357  const BSONObj& keyPattern) override {
358  return _getAccessMethodName(opCtx, keyPattern);
359  }
360 
361  Status _upgradeDatabaseMinorVersionIfNeeded(OperationContext* opCtx,
362  const std::string& newPluginName) override;
363 
364  // public static helpers
365 
366  static BSONObj fixIndexKey(const BSONObj& key);
367 
372  static void prepareInsertDeleteOptions(OperationContext* opCtx,
373  const IndexDescriptor* desc,
374  InsertDeleteOptions* options);
375 
376 private:
377  static const BSONObj _idObj; // { _id : 1 }
378 
379  bool _shouldOverridePlugin(OperationContext* opCtx, const BSONObj& keyPattern) const;
380 
386  std::string _getAccessMethodName(OperationContext* opCtx, const BSONObj& keyPattern) const;
387 
388  void _checkMagic() const;
389 
390  Status _indexFilteredRecords(OperationContext* opCtx,
391  IndexCatalogEntry* index,
392  const std::vector<BsonRecord>& bsonRecords,
393  int64_t* keysInsertedOut);
394 
395  Status _indexRecords(OperationContext* opCtx,
396  IndexCatalogEntry* index,
397  const std::vector<BsonRecord>& bsonRecords,
398  int64_t* keysInsertedOut);
399 
400  Status _unindexRecord(OperationContext* opCtx,
401  IndexCatalogEntry* index,
402  const BSONObj& obj,
403  const RecordId& loc,
404  bool logIfError,
405  int64_t* keysDeletedOut);
406 
407  inline const IndexCatalogEntryContainer& _getEntries() const override {
408  return this->_entries;
409  }
410 
412  return this->_entries;
413  }
414 
418  Status _dropIndex(OperationContext* opCtx, IndexCatalogEntry* entry) override;
419 
420  // just does disk hanges
421  // doesn't change memory state, etc...
422  void _deleteIndexFromDisk(OperationContext* opCtx,
423  const std::string& indexName,
424  const std::string& indexNamespace);
425 
426  // descriptor ownership passes to _setupInMemoryStructures
427  // initFromDisk: Avoids registering a change to undo this operation when set to true.
428  // You must set this flag if calling this function outside of a UnitOfWork.
429  IndexCatalogEntry* _setupInMemoryStructures(OperationContext* opCtx,
430  std::unique_ptr<IndexDescriptor> descriptor,
431  bool initFromDisk);
432 
433  // Apply a set of transformations to the user-provided index object 'spec' to make it
434  // conform to the standard for insertion. This function adds the 'v' field if it didn't
435  // exist, removes the '_id' field if it exists, applies plugin-level transformations if
436  // appropriate, etc.
437  static StatusWith<BSONObj> _fixIndexSpec(OperationContext* opCtx,
438  Collection* collection,
439  const BSONObj& spec);
440 
441  Status _isSpecOk(OperationContext* opCtx, const BSONObj& spec) const;
442 
443  Status _doesSpecConflictWithExisting(OperationContext* opCtx, const BSONObj& spec) const;
444 
445  inline const Collection* _getCollection() const override {
446  return this->_collection;
447  }
448 
449  inline Collection* _getCollection() override {
450  return this->_collection;
451  }
452 
453 
454  int _magic;
457 
459 
460  // These are the index specs of indexes that were "leftover".
461  // "Leftover" means they were unfinished when a mongod shut down.
462  // Certain operations are prohibited until someone fixes.
463  // Retrieve by calling getAndClearUnfinishedIndexes().
464  std::vector<BSONObj> _unfinishedIndexes;
465 
467 
468 
470  IndexCatalog* const this_,
471  OperationContext* const opCtx,
472  std::unique_ptr<IndexDescriptor> descriptor,
473  const bool initFromDisk) {
474  return this_->_setupInMemoryStructures(opCtx, std::move(descriptor), initFromDisk);
475  }
476 
477  inline static Status _dropIndex(IndexCatalog* const this_,
478  OperationContext* const opCtx,
479  IndexCatalogEntry* const desc) {
480  return this_->_dropIndex(opCtx, desc);
481  }
482 };
483 } // namespace mongo
IndexCatalog *const _catalog
Definition: index_catalog_impl.h:318
bool _inProgress
Definition: index_catalog_impl.h:327
static const BSONObj _idObj
Definition: index_catalog_impl.h:377
Definition: index_catalog_entry.h:272
MultikeyPaths getMultikeyPaths(OperationContext *opCtx, const IndexDescriptor *idx) override
Returns the path components that cause the index &#39;idx&#39; to be multikey if the index supports path-leve...
Definition: index_catalog_impl.cpp:1101
bool ok() const override
Definition: index_catalog_impl.cpp:202
Collection *const _collection
Definition: index_catalog_impl.h:317
OperationContext *const _opCtx
Definition: index_catalog_impl.h:198
const IndexCatalogEntryContainer & _getEntries() const override
Definition: index_catalog_impl.h:407
bool _shouldOverridePlugin(OperationContext *opCtx, const BSONObj &keyPattern) const
Definition: index_catalog_impl.cpp:224
const IndexKeyEntry _entry
Definition: ephemeral_for_test_btree_impl.cpp:499
bool haveAnyIndexes() const override
Definition: index_catalog_impl.cpp:1110
static StatusWith< BSONObj > _fixIndexSpec(OperationContext *opCtx, Collection *collection, const BSONObj &spec)
Definition: index_catalog_impl.cpp:1516
Status indexRecords(OperationContext *opCtx, const std::vector< BsonRecord > &bsonRecords, int64_t *keysInsertedOut) override
When &#39;keysInsertedOut&#39; is not null, it will be set to the number of index keys inserted by this opera...
Definition: index_catalog_impl.cpp:1451
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
IndexDescriptor * findIndexByName(OperationContext *opCtx, StringData name, bool includeUnfinishedIndexes=false) const override
Find index by name.
Definition: index_catalog_impl.cpp:1233
IndexDescriptor * findIdIndex(OperationContext *opCtx) const override
Definition: index_catalog_impl.cpp:1223
bool isMultikey(OperationContext *opCtx, const IndexDescriptor *idx) override
Returns true if the index &#39;idx&#39; is multikey, and returns false otherwise.
Definition: index_catalog_impl.cpp:1095
Database *const this_
Definition: database_impl.cpp:82
void findIndexesByKeyPattern(OperationContext *opCtx, const BSONObj &key, bool includeUnfinishedIndexes, std::vector< IndexDescriptor *> *matches) const override
Find indexes with a matching key pattern, putting them into the vector &#39;matches&#39;. ...
Definition: index_catalog_impl.cpp:1262
Collection *const collection
Definition: collection_info_cache_impl.cpp:53
Status _indexFilteredRecords(OperationContext *opCtx, IndexCatalogEntry *index, const std::vector< BsonRecord > &bsonRecords, int64_t *keysInsertedOut)
Definition: index_catalog_impl.cpp:1371
void dropAllIndexes(OperationContext *opCtx, bool includingIdIndex, stdx::function< void(const IndexDescriptor *)> onDropFn=nullptr) override
Drops all indexes in the index catalog, optionally dropping the id index depending on the &#39;includingI...
Definition: index_catalog_impl.cpp:895
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
IndexDescriptor * findIndexByKeyPatternAndCollationSpec(OperationContext *opCtx, const BSONObj &key, const BSONObj &collationSpec, bool includeUnfinishedIndexes=false) const override
Find index by matching key pattern and collation spec.
Definition: index_catalog_impl.cpp:1245
void _deleteIndexFromDisk(OperationContext *opCtx, const std::string &indexName, const std::string &indexNamespace)
Definition: index_catalog_impl.cpp:1069
static IndexCatalogEntry * _setupInMemoryStructures(IndexCatalog *const this_, OperationContext *const opCtx, std::unique_ptr< IndexDescriptor > descriptor, const bool initFromDisk)
Definition: index_catalog_impl.h:469
Status checkUnfinished() const override
Returns a not-ok Status if there are any unfinished index builds.
Definition: index_catalog_impl.cpp:214
BSONObj key
Definition: btree_interface.cpp:334
how many: 1 per Collection.
Definition: index_catalog_impl.h:56
bool _includeUnfinishedIndexes
Definition: index_catalog_impl.h:196
const int _maxNumIndexesAllowed
Definition: index_catalog_impl.h:456
IndexCatalogEntry * catalogEntry(const IndexDescriptor *desc) override
Definition: index_catalog_impl.cpp:1191
disk creation order 1) system.indexes entry 2) collection&#39;s NamespaceDetails a) info + head b) _index...
Definition: index_catalog_impl.h:279
StatusWith< BSONObj > prepareSpecForCreate(OperationContext *opCtx, const BSONObj &original) const override
Definition: index_catalog_impl.cpp:309
this is NOT safe through a yield right now.
Definition: collection.h:160
IndexCatalogEntryContainer::const_iterator _iterator
Definition: index_catalog_impl.h:200
IndexDescriptor * findShardKeyPrefixedIndex(OperationContext *opCtx, const BSONObj &shardKey, bool requireSingleKey) const override
Returns an index suitable for shard key range scans.
Definition: index_catalog_impl.cpp:1276
const IndexDescriptor * idx
Definition: coll_mod.cpp:70
std::string _getAccessMethodName(OperationContext *opCtx, const BSONObj &keyPattern) const
This differs from IndexNames::findPluginName in that returns the plugin name we should use...
Definition: index_catalog_impl.cpp:257
const IndexCatalogEntry * getEntry(const IndexDescriptor *desc) const override
Definition: index_catalog_impl.cpp:1325
int numIndexesReady(OperationContext *opCtx) const override
Definition: index_catalog_impl.cpp:1120
void _checkMagic() const
Definition: index_catalog_impl.cpp:206
int numIndexesTotal(OperationContext *opCtx) const override
Definition: index_catalog_impl.cpp:1114
IndexCatalogEntry * _setupInMemoryStructures(OperationContext *opCtx, std::unique_ptr< IndexDescriptor > descriptor, bool initFromDisk)
Definition: index_catalog_impl.cpp:165
IndexCatalogEntry * getEntry()
Definition: index_catalog_impl.h:304
how many: 1 per Collection.
Definition: index_catalog.h:62
const std::string _ns
Definition: index_catalog_impl.h:319
const std::string & getIndexName() const
Definition: index_catalog_impl.h:308
Status _isSpecOk(OperationContext *opCtx, const BSONObj &spec) const
Definition: index_catalog_impl.cpp:541
IndexCatalogEntryContainer & _getEntries() override
Definition: index_catalog_impl.h:411
IndexCatalog *const Collection *const const int maxNumIndexesAllowed
Definition: index_catalog_impl.cpp:75
IndexIteratorImpl * clone_impl() const override
Definition: index_catalog_impl.cpp:1166
StatusWith< BSONObj > createIndexOnEmptyCollection(OperationContext *opCtx, BSONObj spec) override
Call this only on an empty collection from inside a WriteUnitOfWork.
Definition: index_catalog_impl.cpp:332
Definition: index_catalog.h:155
int numIndexesInProgress(OperationContext *opCtx) const
Definition: index_catalog_impl.h:73
IndexCatalogEntry * _prev
Definition: index_catalog_impl.h:204
Definition: index_catalog.h:66
void _advance()
Definition: index_catalog_impl.cpp:1196
std::string ns
Definition: index_catalog_impl.h:244
Collection *const _collection
Definition: index_catalog_impl.h:455
OperationContext *const const IndexCatalog *const const bool includeUnfinishedIndexes
Definition: index_catalog_impl.cpp:84
std::vector< BSONObj > getAndClearUnfinishedIndexes(OperationContext *opCtx) override
will drop all incompleted indexes and return specs after this, the indexes can be rebuilt ...
Definition: index_catalog_impl.cpp:1081
IndexCatalogEntry * _entry
Definition: index_catalog_impl.h:326
Definition: index_key_validate.h:40
Definition: index_catalog_entry.h:56
IndexAccessMethod * accessMethod(const IndexDescriptor *desc) override
Definition: index_catalog_impl.cpp:1186
Status init(OperationContext *opCtx) override
Definition: index_catalog_impl.cpp:131
Definition: index_catalog_impl.h:243
const IndexDescriptor * refreshEntry(OperationContext *opCtx, const IndexDescriptor *oldDesc) override
Reload the index definition for &#39;oldDesc&#39; from the CollectionCatalogEntry.
Definition: index_catalog_impl.cpp:1332
static BSONObj fixIndexKey(const BSONObj &key)
Definition: index_catalog_impl.cpp:1487
bool _start
Definition: index_catalog_impl.h:202
Status _unindexRecord(OperationContext *opCtx, IndexCatalogEntry *index, const BSONObj &obj, const RecordId &loc, bool logIfError, int64_t *keysDeletedOut)
Definition: index_catalog_impl.cpp:1417
Status _dropIndex(OperationContext *const opCtx, IndexCatalogEntry *const desc)
Definition: index_catalog.h:570
IndexCatalog *const _this
Definition: index_catalog_impl.h:466
OperationContext *const const IndexCatalog *const cat
Definition: index_catalog_impl.cpp:84
Status _dropIndex(OperationContext *opCtx, IndexCatalogEntry *entry) override
this does no sanity checks
Definition: index_catalog_impl.cpp:1022
Definition: index_catalog_impl.h:176
bool more() override
Definition: index_catalog_impl.cpp:1170
IndexCatalogEntry *const OperationContext *const const StringData CollectionCatalogEntry *const std::unique_ptr< IndexDescriptor > descriptor
Definition: index_catalog_entry_impl.cpp:58
Status _doesSpecConflictWithExisting(OperationContext *opCtx, const BSONObj &spec) const
Definition: index_catalog_impl.cpp:779
~IndexCatalogImpl() override
Definition: index_catalog_impl.cpp:123
Status _indexRecords(OperationContext *opCtx, IndexCatalogEntry *index, const std::vector< BsonRecord > &bsonRecords, int64_t *keysInsertedOut)
Definition: index_catalog_impl.cpp:1400
int _magic
Definition: index_catalog_impl.h:454
std::string _indexNamespace
Definition: index_catalog_impl.h:324
std::string name
Definition: index_catalog_impl.h:245
static void prepareInsertDeleteOptions(OperationContext *opCtx, const IndexDescriptor *desc, InsertDeleteOptions *options)
Fills out &#39;options&#39; in order to indicate whether to allow dups or relax index constraints, as needed by replication.
Definition: index_catalog_impl.cpp:1497
Definition: index_catalog.h:64
IndexCatalogImpl(IndexCatalog *this_, Collection *collection, int maxNumIndexesAllowed)
Definition: index_catalog_impl.cpp:115
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
std::vector< std::unique_ptr< IndexCatalogEntry > >::const_iterator const_iterator
Definition: index_catalog_entry.h:274
IndexCatalogEntry * _next
Definition: index_catalog_impl.h:205
std::string _indexName
Definition: index_catalog_impl.h:323
BSONObj key
Definition: index_catalog_impl.h:246
bool haveIdIndex(OperationContext *opCtx) const override
this is in "alive" until the Collection goes away in which case everything from this tree has to go a...
Definition: index_catalog_impl.cpp:1151
OperationContext * _opCtx
Definition: index_catalog_impl.h:329
const IndexCatalog * _catalog
Definition: index_catalog_impl.h:199
IndexAccessMethod * getIndex(const IndexDescriptor *desc) override
Definition: index_catalog_impl.cpp:1315
static Status _dropIndex(IndexCatalog *const this_, OperationContext *const opCtx, IndexCatalogEntry *const desc)
Definition: index_catalog_impl.h:477
Status _upgradeDatabaseMinorVersionIfNeeded(OperationContext *opCtx, const std::string &newPluginName) override
Definition: index_catalog_impl.cpp:269
const BSONObj & getSpec() const
Definition: index_catalog_impl.h:312
Database *const OperationContext *const const StringData name
Definition: database_impl.cpp:82
IndexCatalogEntry * _setupInMemoryStructures(OperationContext *opCtx, std::unique_ptr< IndexDescriptor > descriptor, bool initFromDisk)
Definition: index_catalog.cpp:44
std::vector< BSONObj > _unfinishedIndexes
Definition: index_catalog_impl.h:464
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
BSONObj _spec
Definition: index_catalog_impl.h:321
const Collection * _getCollection() const override
Definition: index_catalog_impl.h:445
std::string getAccessMethodName(OperationContext *opCtx, const BSONObj &keyPattern) override
Definition: index_catalog_impl.h:356
BSONObj getDefaultIdIndexSpec() const override
Returns the spec for the id index to create by default for this collection.
Definition: index_catalog_impl.cpp:878
OperationContext const IndexDescriptor * desc
Definition: index_catalog_impl.cpp:97
Collection * _getCollection() override
Definition: index_catalog_impl.h:449
IndexDescriptor * next() override
Definition: index_catalog_impl.cpp:1178
IndexIteratorImpl(OperationContext *opCtx, const IndexCatalog *cat, bool includeUnfinishedIndexes)
Definition: index_catalog_impl.cpp:1155
void unindexRecord(OperationContext *opCtx, const BSONObj &obj, const RecordId &loc, bool noWarn, int64_t *keysDeletedOut) override
When &#39;keysDeletedOut&#39; is not null, it will be set to the number of index keys removed by this operati...
Definition: index_catalog_impl.cpp:1468
void findIndexByType(OperationContext *opCtx, const std::string &type, std::vector< IndexDescriptor *> &matches, bool includeUnfinishedIndexes=false) const override
Definition: index_catalog_impl.cpp:1302
IndexCatalogEntryContainer _entries
Definition: index_catalog_impl.h:458
Status dropIndex(OperationContext *opCtx, IndexDescriptor *desc) override
Definition: index_catalog_impl.cpp:971