Storage Engine API
mongo::MultiIndexBlock Class Reference

Builds one or more indexes. More...

#include <index_create.h>

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  Impl
 
struct  TUHook
 

Public Member Functions

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

Static Public Member Functions

static MONGO_DECLARE_SHIM ((OperationContext *opCtx, Collection *collection, PrivateTo< MultiIndexBlock >) ->std::unique_ptr< Impl >) makeImpl
 

Private Member Functions

const Impl_impl () const
 
Impl_impl ()
 

Private Attributes

std::unique_ptr< Impl_pimpl
 

Constructor & Destructor Documentation

◆ ~MultiIndexBlock()

mongo::MultiIndexBlock::~MultiIndexBlock ( )
inlinedefault

◆ MultiIndexBlock()

mongo::MultiIndexBlock::MultiIndexBlock ( OperationContext *const  opCtx,
Collection *const  collection 
)
inlineexplicit

Neither pointer is owned.

Member Function Documentation

◆ _impl() [1/2]

const Impl& mongo::MultiIndexBlock::_impl ( ) const
inlineprivate

◆ _impl() [2/2]

Impl& mongo::MultiIndexBlock::_impl ( )
inlineprivate

◆ abortWithoutCleanup()

void mongo::MultiIndexBlock::abortWithoutCleanup ( )
inline

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.

◆ allowBackgroundBuilding()

void mongo::MultiIndexBlock::allowBackgroundBuilding ( )
inline

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.

◆ allowInterruption()

void mongo::MultiIndexBlock::allowInterruption ( )
inline

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

This only affects builds using the insertAllDocumentsInCollection helper.

◆ commit()

void mongo::MultiIndexBlock::commit ( stdx::function< void(const BSONObj &spec)>  onCreateFn = nullptr)
inline

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().

onCreateFn will be called on each index before writes that mark the index as "ready".

Requires holding an exclusive database lock.

◆ doneInserting()

Status mongo::MultiIndexBlock::doneInserting ( std::set< RecordId > *const  dupsOut = nullptr)
inline

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.

Should not be called inside of a WriteUnitOfWork.

◆ getBuildInBackground()

bool mongo::MultiIndexBlock::getBuildInBackground ( ) const
inline

◆ ignoreUniqueConstraint()

void mongo::MultiIndexBlock::ignoreUniqueConstraint ( )
inline

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.

◆ init() [1/2]

StatusWith<std::vector<BSONObj> > mongo::MultiIndexBlock::init ( const std::vector< BSONObj > &  specs)
inline

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.

◆ init() [2/2]

StatusWith<std::vector<BSONObj> > mongo::MultiIndexBlock::init ( const BSONObj &  spec)
inline

◆ insert()

Status mongo::MultiIndexBlock::insert ( const BSONObj &  wholeDocument,
const RecordId &  loc 
)
inline

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

Do not call if you called insertAllDocumentsInCollection();

Should be called inside of a WriteUnitOfWork.

◆ insertAllDocumentsInCollection()

Status mongo::MultiIndexBlock::insertAllDocumentsInCollection ( std::set< RecordId > *const  dupsOut = nullptr)
inline

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.

Should not be called inside of a WriteUnitOfWork.

◆ MONGO_DECLARE_SHIM()

static mongo::MultiIndexBlock::MONGO_DECLARE_SHIM ( (OperationContext *opCtx, Collection *collection, PrivateTo< MultiIndexBlock >) ->std::unique_ptr< Impl )
static

◆ removeExistingIndexes()

void mongo::MultiIndexBlock::removeExistingIndexes ( std::vector< BSONObj > *const  specs) const
inline

Removes pre-existing indexes from 'specs'.

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

Member Data Documentation

◆ _pimpl

std::unique_ptr<Impl> mongo::MultiIndexBlock::_pimpl
private

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