Storage Engine API
mongo::MultiIndexBlockImpl Class Referenceabstract

Builds one or more indexes. More...

#include <index_create_impl.h>

Inheritance diagram for mongo::MultiIndexBlockImpl:
mongo::MultiIndexBlock::Impl

Detailed Description

Builds one or more indexes.

If any method other than insert() returns a not-ok Status, this MultiIndexBlock should be considered failed and must be destroyed.

If a MultiIndexBlock is destroyed before commit() or if commit() is rolled back, it will clean up all traces of the indexes being constructed. MultiIndexBlocks should not be destructed from inside of a WriteUnitOfWork as any cleanup needed should never be rolled back (as it is itself essentially a form of rollback, you don't want to "rollback the rollback").

Classes

class  CleanupIndexesVectorOnRollback
 On rollback in init(), cleans up _indexes so that ~MultiIndexBlock doesn't try to clean up _indexes manually (since the changes were already rolled back). More...
 
struct  IndexToBuild
 
class  SetNeedToCleanupOnRollback
 On rollback sets MultiIndexBlockImpl::_needToCleanup to true. More...
 

Public Member Functions

 MultiIndexBlockImpl (OperationContext *opCtx, Collection *collection)
 Neither pointer is owned. More...
 
 ~MultiIndexBlockImpl () override
 
void allowBackgroundBuilding () override
 By default we ignore the 'background' flag in specs when building an index. More...
 
void allowInterruption () override
 Call this before init() to allow the index build to be interrupted. More...
 
void ignoreUniqueConstraint () override
 By default we enforce the 'unique' flag in specs when building an index by failing. More...
 
void removeExistingIndexes (std::vector< BSONObj > *specs) const override
 Removes pre-existing indexes from 'specs'. More...
 
StatusWith< std::vector< BSONObj > > init (const std::vector< BSONObj > &specs) override
 Prepares the index(es) for building and returns the canonicalized form of the requested index specifications. More...
 
StatusWith< std::vector< BSONObj > > init (const BSONObj &spec) override
 
Status insertAllDocumentsInCollection (std::set< RecordId > *dupsOut=nullptr) override
 Inserts all documents in the Collection into the indexes and logs with timing info. More...
 
Status insert (const BSONObj &wholeDocument, const RecordId &loc) override
 Call this after init() for each document in the collection. More...
 
Status doneInserting (std::set< RecordId > *dupsOut=nullptr) override
 Call this after the last insert(). More...
 
void commit (stdx::function< void(const BSONObj &spec)> onCreateFn) override
 Marks the index ready for use. More...
 
void abortWithoutCleanup () override
 May be called at any time after construction but before a successful commit(). More...
 
bool getBuildInBackground () const override
 
- Public Member Functions inherited from mongo::MultiIndexBlock::Impl
virtual ~Impl ()=0
 

Private Member Functions

 MONGO_DISALLOW_COPYING (MultiIndexBlockImpl)
 
virtual bool initBackgroundIndexFromSpec (const BSONObj &spec) const =0
 

Private Attributes

std::vector< IndexToBuild_indexes
 
std::unique_ptr< BackgroundOperation > _backgroundOperation
 
Collection_collection
 
OperationContext * _opCtx
 
bool _buildInBackground
 
bool _allowInterruption
 
bool _ignoreUnique
 
bool _needToCleanup
 

Constructor & Destructor Documentation

◆ MultiIndexBlockImpl()

mongo::MultiIndexBlockImpl::MultiIndexBlockImpl ( OperationContext *  opCtx,
Collection collection 
)

Neither pointer is owned.

◆ ~MultiIndexBlockImpl()

mongo::MultiIndexBlockImpl::~MultiIndexBlockImpl ( )
override

Member Function Documentation

◆ abortWithoutCleanup()

void mongo::MultiIndexBlockImpl::abortWithoutCleanup ( )
overridevirtual

May be called at any time after construction but before a successful commit().

Suppresses the default behavior on destruction of removing all traces of uncommitted index builds.

The most common use of this is if the indexes were already dropped via some other mechanism such as the whole collection being dropped. In that case, it would be invalid to try to remove the indexes again. Also, replication uses this to ensure that indexes that are being built on shutdown are resumed on startup.

Do not use this unless you are really sure you need to.

Does not matter whether it is called inside of a WriteUnitOfWork. Will not be rolled back.

Implements mongo::MultiIndexBlock::Impl.

◆ allowBackgroundBuilding()

void mongo::MultiIndexBlockImpl::allowBackgroundBuilding ( )
inlineoverridevirtual

By default we ignore the 'background' flag in specs when building an index.

If this is called before init(), we will build the indexes in the background as long as all specs call for background indexing. If any spec calls for foreground indexing all indexes will be built in the foreground, as there is no concurrency benefit to building a subset of indexes in the background, but there is a performance benefit to building all in the foreground.

Implements mongo::MultiIndexBlock::Impl.

◆ allowInterruption()

void mongo::MultiIndexBlockImpl::allowInterruption ( )
inlineoverridevirtual

Call this before init() to allow the index build to be interrupted.

This only affects builds using the insertAllDocumentsInCollection helper.

Implements mongo::MultiIndexBlock::Impl.

◆ commit()

void mongo::MultiIndexBlockImpl::commit ( stdx::function< void(const BSONObj &spec)>  onCreateFn)
overridevirtual

Marks the index ready for use.

Should only be called as the last method after doneInserting() or insertAllDocumentsInCollection() return success.

Should be called inside of a WriteUnitOfWork. If the index building is to be logOp'd, logOp() should be called from the same unit of work as commit().

Requires holding an exclusive database lock.

Implements mongo::MultiIndexBlock::Impl.

◆ doneInserting()

Status mongo::MultiIndexBlockImpl::doneInserting ( std::set< RecordId > *  dupsOut = nullptr)
overridevirtual

Call this after the last insert().

This gives the index builder a chance to do any long-running operations in separate units of work from commit().

Do not call if you called insertAllDocumentsInCollection();

If dupsOut is passed as non-NULL, violators of uniqueness constraints will be added to the set. Documents added to this set are not indexed, so callers MUST either fail this index build or delete the documents from the collection.

Must not be called inside of a WriteUnitOfWork.

Implements mongo::MultiIndexBlock::Impl.

◆ getBuildInBackground()

bool mongo::MultiIndexBlockImpl::getBuildInBackground ( ) const
inlineoverridevirtual

◆ ignoreUniqueConstraint()

void mongo::MultiIndexBlockImpl::ignoreUniqueConstraint ( )
inlineoverridevirtual

By default we enforce the 'unique' flag in specs when building an index by failing.

If this is called before init(), we will ignore unique violations. This has no effect if no specs are unique.

If this is called, any dupsOut sets passed in will never be filled.

Implements mongo::MultiIndexBlock::Impl.

◆ init() [1/2]

StatusWith< std::vector< BSONObj > > mongo::MultiIndexBlockImpl::init ( const std::vector< BSONObj > &  specs)
overridevirtual

Prepares the index(es) for building and returns the canonicalized form of the requested index specifications.

Does not need to be called inside of a WriteUnitOfWork (but can be due to nesting).

Requires holding an exclusive database lock.

Implements mongo::MultiIndexBlock::Impl.

◆ init() [2/2]

StatusWith< std::vector< BSONObj > > mongo::MultiIndexBlockImpl::init ( const BSONObj &  spec)
overridevirtual

◆ initBackgroundIndexFromSpec()

virtual bool mongo::MultiIndexBlockImpl::initBackgroundIndexFromSpec ( const BSONObj &  spec) const
privatepure virtual

◆ insert()

Status mongo::MultiIndexBlockImpl::insert ( const BSONObj &  wholeDocument,
const RecordId &  loc 
)
overridevirtual

Call this after init() for each document in the collection.

Do not call if you called insertAllDocumentsInCollection();

Should be called inside of a WriteUnitOfWork.

Implements mongo::MultiIndexBlock::Impl.

◆ insertAllDocumentsInCollection()

Status mongo::MultiIndexBlockImpl::insertAllDocumentsInCollection ( std::set< RecordId > *  dupsOut = nullptr)
overridevirtual

Inserts all documents in the Collection into the indexes and logs with timing info.

This is a simplified replacement for insert and doneInserting. Do not call this if you are calling either of them.

If dupsOut is passed as non-NULL, violators of uniqueness constraints will be added to the set rather than failing the build. Documents added to this set are not indexed, so callers MUST either fail this index build or delete the documents from the collection.

Can throw an exception if interrupted.

Must not be called inside of a WriteUnitOfWork.

Implements mongo::MultiIndexBlock::Impl.

◆ MONGO_DISALLOW_COPYING()

mongo::MultiIndexBlockImpl::MONGO_DISALLOW_COPYING ( MultiIndexBlockImpl  )
private

◆ removeExistingIndexes()

void mongo::MultiIndexBlockImpl::removeExistingIndexes ( std::vector< BSONObj > *  specs) const
overridevirtual

Removes pre-existing indexes from 'specs'.

If this isn't done, init() may fail with IndexAlreadyExists.

Implements mongo::MultiIndexBlock::Impl.

Member Data Documentation

◆ _allowInterruption

bool mongo::MultiIndexBlockImpl::_allowInterruption
private

◆ _backgroundOperation

std::unique_ptr<BackgroundOperation> mongo::MultiIndexBlockImpl::_backgroundOperation
private

◆ _buildInBackground

bool mongo::MultiIndexBlockImpl::_buildInBackground
private

◆ _collection

Collection* mongo::MultiIndexBlockImpl::_collection
private

◆ _ignoreUnique

bool mongo::MultiIndexBlockImpl::_ignoreUnique
private

◆ _indexes

std::vector<IndexToBuild> mongo::MultiIndexBlockImpl::_indexes
private

◆ _needToCleanup

bool mongo::MultiIndexBlockImpl::_needToCleanup
private

◆ _opCtx

OperationContext* mongo::MultiIndexBlockImpl::_opCtx
private

The documentation for this class was generated from the following files: