Storage Engine API
mongo::RecordStore Class Referenceabstract

An abstraction used for storing documents in a collection or entries in an index. More...

#include <record_store.h>

Inheritance diagram for mongo::RecordStore:
mongo::DevNullRecordStore mongo::EphemeralForTestRecordStore mongo::HeapRecordStoreBtree mongo::MobileRecordStore mongo::RecordStoreV1Base mongo::WiredTigerRecordStore mongo::CappedRecordStoreV1 mongo::SimpleRecordStoreV1 mongo::PrefixedWiredTigerRecordStore mongo::StandardWiredTigerRecordStore

Detailed Description

An abstraction used for storing documents in a collection or entries in an index.

In storage engines implementing the KVEngine, record stores are also used for implementing catalogs.

Many methods take an OperationContext parameter. This contains the RecoveryUnit, with all RecordStore specific transaction information, as well as the LockState. Methods that take an OperationContext may throw a WriteConflictException.

This class must be thread-safe for document-level locking storage engines. In addition, for storage engines implementing the KVEngine some methods must be thread safe, see KVCatalog. Only for MMAPv1 is this class not thread-safe.

Public Member Functions

 RecordStore (StringData ns)
 
virtual ~RecordStore ()
 
virtual const char * name () const =0
 
virtual const std::string & ns () const
 
virtual long long dataSize (OperationContext *opCtx) const =0
 The dataSize is an approximation of the sum of the sizes (in bytes) of the documents or entries in the recordStore. More...
 
virtual long long numRecords (OperationContext *opCtx) const =0
 Total number of record in the RecordStore. More...
 
virtual bool isCapped () const =0
 
virtual void setCappedCallback (CappedCallback *)
 
virtual int64_t storageSize (OperationContext *opCtx, BSONObjBuilder *extraInfo=NULL, int infoLevel=0) const =0
 
virtual RecordData dataFor (OperationContext *opCtx, const RecordId &loc) const
 Get the RecordData at loc, which must exist. More...
 
virtual bool findRecord (OperationContext *opCtx, const RecordId &loc, RecordData *out) const
 
virtual void deleteRecord (OperationContext *opCtx, const RecordId &dl)=0
 
virtual StatusWith< RecordId > insertRecord (OperationContext *opCtx, const char *data, int len, Timestamp timestamp, bool enforceQuota)=0
 
virtual Status insertRecords (OperationContext *opCtx, std::vector< Record > *records, std::vector< Timestamp > *timestamps, bool enforceQuota)
 
virtual Status insertRecordsWithDocWriter (OperationContext *opCtx, const DocWriter *const *docs, const Timestamp *timestamps, size_t nDocs, RecordId *idsOut=nullptr)=0
 Inserts nDocs documents into this RecordStore using the DocWriter interface. More...
 
StatusWith< RecordId > insertRecordWithDocWriter (OperationContext *opCtx, const DocWriter *doc, Timestamp timestamp)
 A thin wrapper around insertRecordsWithDocWriter() to simplify handling of single DocWriters. More...
 
virtual Status updateRecord (OperationContext *opCtx, const RecordId &oldLocation, const char *data, int len, bool enforceQuota, UpdateNotifier *notifier)=0
 
virtual bool updateWithDamagesSupported () const =0
 
virtual StatusWith< RecordDataupdateWithDamages (OperationContext *opCtx, const RecordId &loc, const RecordData &oldRec, const char *damageSource, const mutablebson::DamageVector &damages)=0
 Updates the record positioned at 'loc' in-place using the deltas described by 'damages'. More...
 
virtual std::unique_ptr< SeekableRecordCursorgetCursor (OperationContext *opCtx, bool forward=true) const =0
 Returns a new cursor over this record store. More...
 
virtual std::unique_ptr< RecordCursorgetCursorForRepair (OperationContext *opCtx) const
 Constructs a cursor over a potentially corrupted store, which can be used to salvage damaged records. More...
 
virtual std::unique_ptr< RecordCursorgetRandomCursor (OperationContext *opCtx) const
 Constructs a cursor over a record store that returns documents in a randomized order, and allows storage engines to provide a more efficient way of random sampling of a record store than MongoDB's default sampling methods, which is used when this method returns {}. More...
 
virtual std::vector< std::unique_ptr< RecordCursor > > getManyCursors (OperationContext *opCtx) const
 Returns many RecordCursors that partition the RecordStore into many disjoint sets. More...
 
virtual Status truncate (OperationContext *opCtx)=0
 removes all Records More...
 
virtual void cappedTruncateAfter (OperationContext *opCtx, RecordId end, bool inclusive)=0
 Truncate documents newer than the document at 'end' from the capped collection. More...
 
virtual bool compactSupported () const
 does this RecordStore support the compact operation? More...
 
virtual bool compactsInPlace () const
 Does compact() leave RecordIds alone or can they change. More...
 
virtual Status compact (OperationContext *opCtx, RecordStoreCompactAdaptor *adaptor, const CompactOptions *options, CompactStats *stats)
 Attempt to reduce the storage space used by this RecordStore. More...
 
virtual bool isInRecordIdOrder () const
 Does the RecordStore cursor retrieve its document in RecordId Order? More...
 
virtual Status validate (OperationContext *opCtx, ValidateCmdLevel level, ValidateAdaptor *adaptor, ValidateResults *results, BSONObjBuilder *output)=0
 
virtual void appendCustomStats (OperationContext *opCtx, BSONObjBuilder *result, double scale) const =0
 
virtual Status touch (OperationContext *opCtx, BSONObjBuilder *output) const
 Load all data into cache. More...
 
virtual boost::optional< RecordId > oplogStartHack (OperationContext *opCtx, const RecordId &startingPosition) const
 Return the RecordId of an oplog entry as close to startingPosition as possible without being higher. More...
 
virtual Status oplogDiskLocRegister (OperationContext *opCtx, const Timestamp &opTime, bool orderedCommit)
 When we write to an oplog, we call this so that if the storage engine supports doc locking, it can manage the visibility of oplog entries to ensure they are ordered. More...
 
virtual void waitForAllEarlierOplogWritesToBeVisible (OperationContext *opCtx) const =0
 Waits for all writes that completed before this call to be visible to forward scans. More...
 
virtual void updateStatsAfterRepair (OperationContext *opCtx, long long numRecords, long long dataSize)=0
 Called after a repair operation is run with the recomputed numRecords and dataSize. More...
 
virtual Status updateCappedSize (OperationContext *opCtx, long long cappedSize)
 used to support online change oplog size. More...
 

Protected Attributes

std::string _ns
 

Private Member Functions

 MONGO_DISALLOW_COPYING (RecordStore)
 

Constructor & Destructor Documentation

◆ RecordStore()

mongo::RecordStore::RecordStore ( StringData  ns)
inline

◆ ~RecordStore()

virtual mongo::RecordStore::~RecordStore ( )
inlinevirtual

Member Function Documentation

◆ appendCustomStats()

virtual void mongo::RecordStore::appendCustomStats ( OperationContext *  opCtx,
BSONObjBuilder *  result,
double  scale 
) const
pure virtual
Parameters
scaleSize- amount by which to scale size metrics appends any custom stats from the RecordStore or other unique stats

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::MobileRecordStore, and mongo::EphemeralForTestRecordStore.

◆ cappedTruncateAfter()

virtual void mongo::RecordStore::cappedTruncateAfter ( OperationContext *  opCtx,
RecordId  end,
bool  inclusive 
)
pure virtual

Truncate documents newer than the document at 'end' from the capped collection.

The collection cannot be completely emptied using this function. An assertion will be thrown if that is attempted.

Parameters
inclusive- Truncate 'end' as well iff true

Implemented in mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::EphemeralForTestRecordStore, mongo::MobileRecordStore, mongo::CappedRecordStoreV1, and mongo::SimpleRecordStoreV1.

◆ compact()

virtual Status mongo::RecordStore::compact ( OperationContext *  opCtx,
RecordStoreCompactAdaptor adaptor,
const CompactOptions options,
CompactStats stats 
)
inlinevirtual

Attempt to reduce the storage space used by this RecordStore.

Only called if compactSupported() returns true. No RecordStoreCompactAdaptor will be passed if compactsInPlace() returns true.

Reimplemented in mongo::WiredTigerRecordStore, mongo::MobileRecordStore, and mongo::SimpleRecordStoreV1.

◆ compactsInPlace()

virtual bool mongo::RecordStore::compactsInPlace ( ) const
inlinevirtual

Does compact() leave RecordIds alone or can they change.

Only called if compactSupported() returns true.

Reimplemented in mongo::WiredTigerRecordStore, mongo::MobileRecordStore, and mongo::SimpleRecordStoreV1.

◆ compactSupported()

virtual bool mongo::RecordStore::compactSupported ( ) const
inlinevirtual

does this RecordStore support the compact operation?

If you return true, you must provide implementations of all compact methods.

Reimplemented in mongo::WiredTigerRecordStore, mongo::HeapRecordStoreBtree, mongo::MobileRecordStore, and mongo::SimpleRecordStoreV1.

◆ dataFor()

virtual RecordData mongo::RecordStore::dataFor ( OperationContext *  opCtx,
const RecordId &  loc 
) const
inlinevirtual

Get the RecordData at loc, which must exist.

If unowned data is returned, it is valid until the next modification of this Record or the lock on this collection is released.

In general, prefer findRecord or RecordCursor::seekExact since they can tell you if a record has been removed.

Reimplemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::EphemeralForTestRecordStore, mongo::MobileRecordStore, and mongo::HeapRecordStoreBtree.

◆ dataSize()

virtual long long mongo::RecordStore::dataSize ( OperationContext *  opCtx) const
pure virtual

The dataSize is an approximation of the sum of the sizes (in bytes) of the documents or entries in the recordStore.

Implemented in mongo::RecordStoreV1Base, mongo::HeapRecordStoreBtree, mongo::WiredTigerRecordStore, mongo::MobileRecordStore, mongo::EphemeralForTestRecordStore, and mongo::DevNullRecordStore.

◆ deleteRecord()

virtual void mongo::RecordStore::deleteRecord ( OperationContext *  opCtx,
const RecordId &  dl 
)
pure virtual

◆ findRecord()

virtual bool mongo::RecordStore::findRecord ( OperationContext *  opCtx,
const RecordId &  loc,
RecordData out 
) const
inlinevirtual
Parameters
out- If the record exists, the contents of this are set.
Returns
true iff there is a Record for loc

If unowned data is returned, it is valid until the next modification of this Record or the lock on this collection is released.

In general prefer RecordCursor::seekExact since it can avoid copying data in more storageEngines.

Warning: MMAPv1 cannot detect if RecordIds are valid. Therefore callers should only pass potentially deleted RecordIds to seek methods if they know that MMAPv1 is not the current storage engine. All new storage engines must support detecting the existence of Records.

Reimplemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::EphemeralForTestRecordStore, mongo::MobileRecordStore, and mongo::HeapRecordStoreBtree.

◆ getCursor()

virtual std::unique_ptr<SeekableRecordCursor> mongo::RecordStore::getCursor ( OperationContext *  opCtx,
bool  forward = true 
) const
pure virtual

Returns a new cursor over this record store.

The cursor is logically positioned before the first (or last if !forward) Record in the collection so that Record will be returned on the first call to next(). Implementations are allowed to lazily seek to the first Record when next() is called rather than doing it on construction.

Implemented in mongo::PrefixedWiredTigerRecordStore, mongo::StandardWiredTigerRecordStore, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::EphemeralForTestRecordStore, mongo::MobileRecordStore, mongo::CappedRecordStoreV1, and mongo::SimpleRecordStoreV1.

◆ getCursorForRepair()

virtual std::unique_ptr<RecordCursor> mongo::RecordStore::getCursorForRepair ( OperationContext *  opCtx) const
inlinevirtual

Constructs a cursor over a potentially corrupted store, which can be used to salvage damaged records.

The iterator might return every record in the store if all of them are reachable and not corrupted. Returns NULL if not supported.

Reimplemented in mongo::RecordStoreV1Base.

◆ getManyCursors()

virtual std::vector<std::unique_ptr<RecordCursor> > mongo::RecordStore::getManyCursors ( OperationContext *  opCtx) const
inlinevirtual

Returns many RecordCursors that partition the RecordStore into many disjoint sets.

Iterating all returned RecordCursors is equivalent to iterating the full store.

Reimplemented in mongo::WiredTigerRecordStore, mongo::CappedRecordStoreV1, and mongo::SimpleRecordStoreV1.

◆ getRandomCursor()

virtual std::unique_ptr<RecordCursor> mongo::RecordStore::getRandomCursor ( OperationContext *  opCtx) const
inlinevirtual

Constructs a cursor over a record store that returns documents in a randomized order, and allows storage engines to provide a more efficient way of random sampling of a record store than MongoDB's default sampling methods, which is used when this method returns {}.

This method may be implemented using a pseudo-random walk over B-trees or a similar approach. Different cursors should return documents in a different order. Random cursors may return the same document more than once and, as a result, may return more documents than exist in the record store. Implementations should avoid obvious biases toward older, newer, larger smaller or other specific classes of documents.

Reimplemented in mongo::WiredTigerRecordStore.

◆ insertRecord()

virtual StatusWith<RecordId> mongo::RecordStore::insertRecord ( OperationContext *  opCtx,
const char *  data,
int  len,
Timestamp  timestamp,
bool  enforceQuota 
)
pure virtual

◆ insertRecords()

virtual Status mongo::RecordStore::insertRecords ( OperationContext *  opCtx,
std::vector< Record > *  records,
std::vector< Timestamp > *  timestamps,
bool  enforceQuota 
)
inlinevirtual

Reimplemented in mongo::WiredTigerRecordStore.

◆ insertRecordsWithDocWriter()

virtual Status mongo::RecordStore::insertRecordsWithDocWriter ( OperationContext *  opCtx,
const DocWriter *const *  docs,
const Timestamp *  timestamps,
size_t  nDocs,
RecordId *  idsOut = nullptr 
)
pure virtual

Inserts nDocs documents into this RecordStore using the DocWriter interface.

This allows the storage engine to reserve space for a record and have it built in-place rather than building the record then copying it into its destination.

On success, if idsOut is non-null the RecordIds of the inserted records will be written into it. It must have space for nDocs RecordIds.

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::EphemeralForTestRecordStore, mongo::MobileRecordStore, and mongo::HeapRecordStoreBtree.

◆ insertRecordWithDocWriter()

StatusWith<RecordId> mongo::RecordStore::insertRecordWithDocWriter ( OperationContext *  opCtx,
const DocWriter doc,
Timestamp  timestamp 
)
inline

A thin wrapper around insertRecordsWithDocWriter() to simplify handling of single DocWriters.

◆ isCapped()

◆ isInRecordIdOrder()

virtual bool mongo::RecordStore::isInRecordIdOrder ( ) const
inlinevirtual

Does the RecordStore cursor retrieve its document in RecordId Order?

If a subclass overrides the default value to true, the RecordStore cursor must retrieve its documents in RecordId order.

This enables your storage engine to run collection validation in the background.

Reimplemented in mongo::WiredTigerRecordStore.

◆ MONGO_DISALLOW_COPYING()

mongo::RecordStore::MONGO_DISALLOW_COPYING ( RecordStore  )
private

◆ name()

◆ ns()

virtual const std::string& mongo::RecordStore::ns ( ) const
inlinevirtual

◆ numRecords()

virtual long long mongo::RecordStore::numRecords ( OperationContext *  opCtx) const
pure virtual

Total number of record in the RecordStore.

You may need to cache it, so this call takes constant time, as it is called often.

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::MobileRecordStore, mongo::EphemeralForTestRecordStore, mongo::DevNullRecordStore, and mongo::HeapRecordStoreBtree.

◆ oplogDiskLocRegister()

virtual Status mongo::RecordStore::oplogDiskLocRegister ( OperationContext *  opCtx,
const Timestamp &  opTime,
bool  orderedCommit 
)
inlinevirtual

When we write to an oplog, we call this so that if the storage engine supports doc locking, it can manage the visibility of oplog entries to ensure they are ordered.

Since this is called inside of a WriteUnitOfWork while holding a std::mutex, it is illegal to acquire any LockManager locks inside of this function.

If orderedCommit is true, the storage engine can assume the input opTime has become visible in the oplog. Otherwise the storage engine must continue to maintain its own visibility management. Calls with orderedCommit true will not be concurrent with calls of orderedCommit false.

Reimplemented in mongo::WiredTigerRecordStore.

◆ oplogStartHack()

virtual boost::optional<RecordId> mongo::RecordStore::oplogStartHack ( OperationContext *  opCtx,
const RecordId &  startingPosition 
) const
inlinevirtual

Return the RecordId of an oplog entry as close to startingPosition as possible without being higher.

If there are no entries <= startingPosition, return RecordId().

If you don't implement the oplogStartHack, just use the default implementation which returns boost::none.

Reimplemented in mongo::WiredTigerRecordStore, mongo::MobileRecordStore, and mongo::EphemeralForTestRecordStore.

◆ setCappedCallback()

◆ storageSize()

virtual int64_t mongo::RecordStore::storageSize ( OperationContext *  opCtx,
BSONObjBuilder *  extraInfo = NULL,
int  infoLevel = 0 
) const
pure virtual
Parameters
extraInfo- optional more debug info
level- optional, level of debug info to put in (higher is more)
Returns
total estimate size (in bytes) on stable storage

Implemented in mongo::RecordStoreV1Base, mongo::HeapRecordStoreBtree, mongo::WiredTigerRecordStore, mongo::MobileRecordStore, mongo::EphemeralForTestRecordStore, and mongo::DevNullRecordStore.

◆ touch()

virtual Status mongo::RecordStore::touch ( OperationContext *  opCtx,
BSONObjBuilder *  output 
) const
inlinevirtual

Load all data into cache.

What cache depends on implementation.

If the underlying storage engine does not support the operation, returns ErrorCodes::CommandNotSupported

Parameters
output(optional) - where to put detailed stats

Reimplemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::MobileRecordStore, mongo::EphemeralForTestRecordStore, and mongo::HeapRecordStoreBtree.

◆ truncate()

virtual Status mongo::RecordStore::truncate ( OperationContext *  opCtx)
pure virtual

◆ updateCappedSize()

virtual Status mongo::RecordStore::updateCappedSize ( OperationContext *  opCtx,
long long  cappedSize 
)
inlinevirtual

used to support online change oplog size.

Reimplemented in mongo::WiredTigerRecordStore, and mongo::MobileRecordStore.

◆ updateRecord()

virtual Status mongo::RecordStore::updateRecord ( OperationContext *  opCtx,
const RecordId &  oldLocation,
const char *  data,
int  len,
bool  enforceQuota,
UpdateNotifier notifier 
)
pure virtual
Parameters
notifier- Only used by record stores which do not support doc-locking. Called only in the case of an in-place update. Called just before the in-place write occurs.
Returns
Status - If a document move is required (MMAPv1 only) then a status of ErrorCodes::NeedsDocumentMove will be returned. On receipt of this status no update will be performed. It is the caller's responsibility to:
  1. Remove the existing document and associated index keys.
  2. Insert a new document and index keys.

For capped record stores, the record size will never change.

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::EphemeralForTestRecordStore, and mongo::MobileRecordStore.

◆ updateStatsAfterRepair()

virtual void mongo::RecordStore::updateStatsAfterRepair ( OperationContext *  opCtx,
long long  numRecords,
long long  dataSize 
)
pure virtual

Called after a repair operation is run with the recomputed numRecords and dataSize.

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::MobileRecordStore, and mongo::EphemeralForTestRecordStore.

◆ updateWithDamages()

virtual StatusWith<RecordData> mongo::RecordStore::updateWithDamages ( OperationContext *  opCtx,
const RecordId &  loc,
const RecordData oldRec,
const char *  damageSource,
const mutablebson::DamageVector &  damages 
)
pure virtual

Updates the record positioned at 'loc' in-place using the deltas described by 'damages'.

The 'damages' vector describes contiguous ranges of 'damageSource' from which to copy and apply byte-level changes to the data.

Returns
the updated version of the record. If unowned data is returned, then it is valid until the next modification of this Record or the lock on the collection has been released.

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::EphemeralForTestRecordStore, and mongo::MobileRecordStore.

◆ updateWithDamagesSupported()

virtual bool mongo::RecordStore::updateWithDamagesSupported ( ) const
pure virtual
Returns
Returns 'false' if this record store does not implement 'updatewithDamages'. If this method returns false, 'updateWithDamages' must not be called, and all updates must be routed through 'updateRecord' above. This allows the update framework to avoid doing the work of damage tracking if the underlying record store cannot utilize that information.

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::EphemeralForTestRecordStore, and mongo::MobileRecordStore.

◆ validate()

virtual Status mongo::RecordStore::validate ( OperationContext *  opCtx,
ValidateCmdLevel  level,
ValidateAdaptor adaptor,
ValidateResults results,
BSONObjBuilder *  output 
)
pure virtual
Returns
OK if the validate run successfully OK will be returned even if corruption is found deatils will be in result

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::MobileRecordStore, and mongo::EphemeralForTestRecordStore.

◆ waitForAllEarlierOplogWritesToBeVisible()

virtual void mongo::RecordStore::waitForAllEarlierOplogWritesToBeVisible ( OperationContext *  opCtx) const
pure virtual

Waits for all writes that completed before this call to be visible to forward scans.

See the comment on RecordCursor for more details about the visibility rules.

It is only legal to call this on an oplog. It is illegal to call this inside a WriteUnitOfWork.

Implemented in mongo::RecordStoreV1Base, mongo::WiredTigerRecordStore, mongo::DevNullRecordStore, mongo::HeapRecordStoreBtree, mongo::MobileRecordStore, and mongo::EphemeralForTestRecordStore.

Member Data Documentation

◆ _ns

std::string mongo::RecordStore::_ns
protected

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