35 #include "mongo/base/status.h" 36 #include "mongo/base/status_with.h" 37 #include "mongo/base/string_data.h" 38 #include "mongo/bson/mutable/damage_vector.h" 39 #include "mongo/bson/timestamp.h" 45 #include "mongo/db/cursor_manager.h" 46 #include "mongo/db/exec/collection_scan_common.h" 47 #include "mongo/db/logical_session_id.h" 48 #include "mongo/db/namespace_string.h" 49 #include "mongo/db/op_observer.h" 50 #include "mongo/db/query/collation/collator_interface.h" 51 #include "mongo/db/record_id.h" 52 #include "mongo/db/repl/oplog.h" 56 #include "mongo/stdx/condition_variable.h" 57 #include "mongo/stdx/functional.h" 58 #include "mongo/stdx/mutex.h" 61 class CollectionCatalogEntry;
62 class DatabaseCatalogEntry;
65 class IndexDescriptor;
67 class MatchExpression;
68 class MultiIndexBlock;
70 class OperationContext;
71 struct OplogUpdateEntryArgs;
91 recordSize =
static_cast<unsigned>(paddingFactor * recordSize);
98 long long corruptDocuments = 0;
120 void waitUntil(uint64_t prevVersion, Date_t deadline)
const;
170 virtual void init(OperationContext*
opCtx) = 0;
178 virtual Status aboutToDeleteCapped(OperationContext* opCtx,
182 virtual Status recordStoreGoingToUpdateInPlace(OperationContext* opCtx,
183 const RecordId& loc) = 0;
186 virtual bool ok()
const = 0;
194 virtual const NamespaceString&
ns()
const = 0;
197 virtual void refreshUUID(OperationContext* opCtx) = 0;
199 virtual const IndexCatalog* getIndexCatalog()
const = 0;
202 virtual const RecordStore* getRecordStore()
const = 0;
205 virtual CursorManager* getCursorManager()
const = 0;
207 virtual bool requiresIdIndex()
const = 0;
211 virtual bool findDoc(OperationContext* opCtx,
215 virtual std::unique_ptr<SeekableRecordCursor> getCursor(OperationContext* opCtx,
216 bool forward)
const = 0;
218 virtual std::vector<std::unique_ptr<RecordCursor>> getManyCursors(
219 OperationContext* opCtx)
const = 0;
221 virtual void deleteDocument(OperationContext* opCtx,
229 virtual Status insertDocuments(OperationContext* opCtx,
230 std::vector<InsertStatement>::const_iterator begin,
231 std::vector<InsertStatement>::const_iterator end,
234 bool fromMigrate) = 0;
236 virtual Status insertDocument(OperationContext* opCtx,
237 const InsertStatement& doc,
240 bool fromMigrate) = 0;
242 virtual Status insertDocumentsForOplog(OperationContext* opCtx,
244 Timestamp* timestamps,
247 virtual Status insertDocument(OperationContext* opCtx,
249 const std::vector<MultiIndexBlock*>& indexBlocks,
250 bool enforceQuota) = 0;
252 virtual RecordId updateDocument(OperationContext* opCtx,
253 const RecordId& oldLocation,
255 const BSONObj& newDoc,
257 bool indexesAffected,
259 OplogUpdateEntryArgs* args) = 0;
261 virtual bool updateWithDamagesSupported()
const = 0;
264 OperationContext* opCtx,
267 const char* damageSource,
268 const mutablebson::DamageVector& damages,
269 OplogUpdateEntryArgs* args) = 0;
274 virtual Status truncate(OperationContext* opCtx) = 0;
276 virtual Status validate(OperationContext* opCtx,
279 std::unique_ptr<Lock::CollectionLock> collLk,
281 BSONObjBuilder* output) = 0;
283 virtual Status touch(OperationContext* opCtx,
286 BSONObjBuilder* output)
const = 0;
288 virtual void cappedTruncateAfter(OperationContext* opCtx, RecordId end,
bool inclusive) = 0;
290 virtual StatusWithMatchExpression parseValidator(
291 OperationContext* opCtx,
292 const BSONObj& validator,
293 MatchExpressionParser::AllowedFeatureSet allowedFeatures,
294 boost::optional<ServerGlobalParams::FeatureCompatibility::Version>
295 maxFeatureCompatibilityVersion = boost::none)
const = 0;
297 virtual Status setValidator(OperationContext* opCtx, BSONObj validator) = 0;
299 virtual Status setValidationLevel(OperationContext* opCtx, StringData newLevel) = 0;
300 virtual Status setValidationAction(OperationContext* opCtx, StringData newAction) = 0;
302 virtual StringData getValidationLevel()
const = 0;
303 virtual StringData getValidationAction()
const = 0;
305 virtual Status updateValidator(OperationContext* opCtx,
306 BSONObj newValidator,
308 StringData newAction) = 0;
310 virtual bool isCapped()
const = 0;
312 virtual std::shared_ptr<CappedInsertNotifier> getCappedInsertNotifier()
const = 0;
314 virtual uint64_t numRecords(OperationContext* opCtx)
const = 0;
316 virtual uint64_t dataSize(OperationContext* opCtx)
const = 0;
318 virtual uint64_t getIndexSize(OperationContext* opCtx,
322 virtual boost::optional<Timestamp> getMinimumVisibleSnapshot() = 0;
324 virtual void setMinimumVisibleSnapshot(Timestamp
name) = 0;
326 virtual bool haveCappedWaiters() = 0;
328 virtual void notifyCappedWaitersIfNeeded() = 0;
330 virtual const CollatorInterface* getDefaultCollator()
const = 0;
335 OperationContext*
opCtx,
341 PrivateTo<Collection>)
342 ->std::unique_ptr<Impl>) makeImpl;
351 this, opCtx, fullNS, uuid, details, recordStore, dbce, PrivateCall<
Collection>{})) {
352 this->_impl().init(
opCtx);
356 explicit inline Collection(std::unique_ptr<Impl> mock) : _pimpl(std::move(mock)) {}
360 inline bool ok()
const {
361 return this->_impl().ok();
365 return this->_impl().getCatalogEntry();
369 return this->_impl().getCatalogEntry();
373 return this->_impl().infoCache();
377 return this->_impl().infoCache();
380 inline const NamespaceString&
ns()
const {
381 return this->_impl().ns();
385 return this->_impl().uuid();
389 return this->_impl().refreshUUID(opCtx);
393 return this->_impl().getIndexCatalog();
396 return this->_impl().getIndexCatalog();
400 return this->_impl().getRecordStore();
403 return this->_impl().getRecordStore();
407 return this->_impl().getCursorManager();
411 return this->_impl().requiresIdIndex();
416 this->getRecordStore()->dataFor(opCtx, loc).releaseToBson());
426 return this->_impl().findDoc(opCtx, loc, out);
429 inline std::unique_ptr<SeekableRecordCursor>
getCursor(OperationContext*
const opCtx,
430 const bool forward =
true)
const {
431 return this->_impl().getCursor(opCtx, forward);
439 OperationContext*
const opCtx)
const {
440 return this->_impl().getManyCursors(opCtx);
458 OpDebug*
const opDebug,
459 const bool fromMigrate =
false,
460 const bool noWarn =
false,
462 return this->_impl().deleteDocument(
463 opCtx, stmtId, loc, opDebug, fromMigrate, noWarn, storeDeletedDoc);
474 const std::vector<InsertStatement>::const_iterator begin,
475 const std::vector<InsertStatement>::const_iterator end,
476 OpDebug*
const opDebug,
477 const bool enforceQuota,
478 const bool fromMigrate =
false) {
479 return this->_impl().insertDocuments(opCtx, begin, end, opDebug, enforceQuota, fromMigrate);
490 const InsertStatement& doc,
491 OpDebug*
const opDebug,
492 const bool enforceQuota,
493 const bool fromMigrate =
false) {
494 return this->_impl().insertDocument(opCtx, doc, opDebug, enforceQuota, fromMigrate);
503 Timestamp* timestamps,
504 const size_t nDocs) {
505 return this->_impl().insertDocumentsForOplog(opCtx, docs, timestamps, nDocs);
515 const std::vector<MultiIndexBlock*>& indexBlocks,
516 const bool enforceQuota) {
517 return this->_impl().insertDocument(opCtx, doc, indexBlocks, enforceQuota);
530 const RecordId& oldLocation,
532 const BSONObj& newDoc,
533 const bool enforceQuota,
534 const bool indexesAffected,
535 OpDebug*
const opDebug,
536 OplogUpdateEntryArgs*
const args) {
537 return this->_impl().updateDocument(
538 opCtx, oldLocation, oldDoc, newDoc, enforceQuota, indexesAffected, opDebug, args);
542 return this->_impl().updateWithDamagesSupported();
553 OperationContext*
const opCtx,
556 const char*
const damageSource,
557 const mutablebson::DamageVector& damages,
558 OplogUpdateEntryArgs*
const args) {
559 return this->_impl().updateDocumentWithDamages(
560 opCtx, loc, oldRec, damageSource, damages, args);
567 return this->_impl().compact(opCtx, options);
576 return this->_impl().truncate(opCtx);
587 std::unique_ptr<Lock::CollectionLock> collLk,
589 BSONObjBuilder*
const output) {
590 return this->_impl().validate(opCtx, level, background, std::move(collLk), results, output);
597 const bool touchData,
598 const bool touchIndexes,
599 BSONObjBuilder*
const output)
const {
600 return this->_impl().touch(opCtx, touchData, touchIndexes, output);
612 return this->_impl().cappedTruncateAfter(opCtx, end, inclusive);
619 OperationContext*
opCtx,
620 const BSONObj& validator,
621 MatchExpressionParser::AllowedFeatureSet allowedFeatures,
622 boost::optional<ServerGlobalParams::FeatureCompatibility::Version>
623 maxFeatureCompatibilityVersion)
const {
624 return this->_impl().parseValidator(
625 opCtx, validator, allowedFeatures, maxFeatureCompatibilityVersion);
638 return this->_impl().setValidator(opCtx, validator);
642 return this->_impl().setValidationLevel(opCtx, newLevel);
645 return this->_impl().setValidationAction(opCtx, newAction);
649 return this->_impl().getValidationLevel();
652 return this->_impl().getValidationAction();
656 BSONObj newValidator,
658 StringData newAction) {
659 return this->_impl().updateValidator(opCtx, newValidator, newLevel, newAction);
669 return this->_impl().isCapped();
679 return this->_impl().getCappedInsertNotifier();
683 return this->_impl().numRecords(opCtx);
687 return this->_impl().dataSize(opCtx);
691 uint64_t n = this->numRecords(opCtx);
695 return static_cast<int>(this->dataSize(opCtx) / n);
699 BSONObjBuilder*
const details =
nullptr,
700 const int scale = 1) {
701 return this->_impl().getIndexSize(opCtx,
details, scale);
709 return this->_impl().getMinimumVisibleSnapshot();
713 return this->_impl().setMinimumVisibleSnapshot(name);
717 return this->_impl().haveCappedWaiters();
724 return this->_impl().notifyCappedWaitersIfNeeded();
732 return this->_impl().getDefaultCollator();
738 return this->_impl().dbce();
742 return this->_impl().details();
748 return this->_impl().aboutToDeleteCapped(
opCtx, loc,
data);
752 const RecordId& loc)
final {
753 return this->_impl().recordStoreGoingToUpdateInPlace(
opCtx, loc);
760 static void hook() noexcept;
770 return *this->_pimpl;
775 return *this->_pimpl;
Status insertDocument(OperationContext *const opCtx, const InsertStatement &doc, OpDebug *const opDebug, const bool enforceQuota, const bool fromMigrate=false)
this does NOT modify the doc before inserting i.e.
Definition: collection.h:489
Queries with the awaitData option use this notifier object to wait for more data to be inserted into ...
Definition: collection.h:105
IndexCatalogEntry *const OperationContext *const const StringData CollectionCatalogEntry *const std::unique_ptr< IndexDescriptor > CollectionInfoCache *const infoCache
Definition: index_catalog_entry_impl.cpp:58
ValidateCmdLevel
Definition: record_store.h:93
StoreDeletedDoc
Definition: collection.h:164
uint64_t getIndexSize(OperationContext *const opCtx, BSONObjBuilder *const details=nullptr, const int scale=1)
Definition: collection.h:698
std::shared_ptr< CappedInsertNotifier > getCappedInsertNotifier() const
Get a pointer to a capped insert notifier object.
Definition: collection.h:678
Status setValidationAction(OperationContext *const opCtx, const StringData newAction)
Definition: collection.h:644
Impl & _impl()
Definition: collection.h:773
Definition: snapshot.h:69
Collection *const OperationContext *const const StringData OptionalCollectionUUID CollectionCatalogEntry *const details
Definition: collection_impl.cpp:80
Definition: record_store.h:671
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
std::vector< std::unique_ptr< RecordCursor > > getManyCursors(OperationContext *const opCtx) const
Returns many cursors that partition the Collection into many disjoint sets.
Definition: collection.h:438
bool ok() const
Definition: collection.h:360
Definition: collection.h:79
int paddingBytes
Definition: collection.h:83
uint64_t dataSize(OperationContext *const opCtx) const
Definition: collection.h:686
Definition: collection.h:759
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
Status recordStoreGoingToUpdateInPlace(OperationContext *const opCtx, const RecordId &loc) final
Definition: collection.h:751
void setMinimumVisibleSnapshot(const Timestamp name)
Definition: collection.h:712
int averageObjectSize(OperationContext *const opCtx) const
Definition: collection.h:690
Definition: collection_catalog_entry.h:47
Collection(OperationContext *const opCtx, const StringData fullNS, OptionalCollectionUUID uuid, CollectionCatalogEntry *const details, RecordStore *const recordStore, DatabaseCatalogEntry *const dbce)
Definition: collection.h:344
std::string toString() const
Definition: collection.cpp:81
stdx::mutex _mutex
Definition: collection.h:144
StatusWith< RecordData > updateDocumentWithDamages(OperationContext *const opCtx, const RecordId &loc, const Snapshotted< RecordData > &oldRec, const char *const damageSource, const mutablebson::DamageVector &damages, OplogUpdateEntryArgs *const args)
Not allowed to modify indexes.
Definition: collection.h:552
CollectionCatalogEntry * details() const
Definition: collection.h:741
Collection *const _this
Definition: collection_impl.cpp:80
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
std::unique_ptr< SeekableRecordCursor > getCursor(OperationContext *const opCtx, const bool forward=true) const
Definition: collection.h:429
A replacement for the Record class.
Definition: record_data.h:43
friend Collection
Definition: collection.h:173
Represents a logical database containing Collections.
Definition: database_impl.h:61
how many: 1 per Collection.
Definition: index_catalog_impl.h:56
Status validate(OperationContext *const opCtx, const ValidateCmdLevel level, bool background, std::unique_ptr< Lock::CollectionLock > collLk, ValidateResults *const results, BSONObjBuilder *const output)
Definition: collection.h:584
IndexCatalog * getIndexCatalog()
Definition: collection.h:395
Status insertDocuments(OperationContext *const opCtx, const std::vector< InsertStatement >::const_iterator begin, const std::vector< InsertStatement >::const_iterator end, OpDebug *const opDebug, const bool enforceQuota, const bool fromMigrate=false)
Definition: collection.h:473
Definition: collection.h:166
TUHook() noexcept
Definition: collection.h:762
Definition: record_store.h:78
double paddingFactor
Definition: collection.h:82
Definition: collection.h:162
void cappedTruncateAfter(OperationContext *const opCtx, const RecordId end, const bool inclusive)
Truncate documents newer than the document at 'end' from the capped collection.
Definition: collection.h:609
Collection *const OperationContext *const const StringData OptionalCollectionUUID CollectionCatalogEntry *const RecordStore *const DatabaseCatalogEntry *const dbce
Definition: collection_impl.cpp:80
bool inclusive
Definition: btree_interface.cpp:335
DatabaseCatalogEntry * dbce() const
Definition: collection.h:737
StatusWith< CompactStats > compact(OperationContext *const opCtx, const CompactOptions *const options)
Definition: collection.h:565
Definition: collection.h:79
this is NOT safe through a yield right now.
Definition: collection.h:160
OptionalCollectionUUID uuid() const
Definition: collection.h:384
CollectionInfoCache * infoCache()
Definition: collection.h:372
Definition: collection.h:77
Allows inserting a Record "in-place" without creating a copy ahead of time.
Definition: record_store.h:62
ValidationAction
Definition: collection.h:162
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
bool updateWithDamagesSupported() const
Definition: collection.h:541
Status insertDocumentsForOplog(OperationContext *const opCtx, const DocWriter *const *const docs, Timestamp *timestamps, const size_t nDocs)
Callers must ensure no document validation is performed for this collection when calling this method...
Definition: collection.h:501
how many: 1 per Collection.
Definition: index_catalog.h:62
RecordStore * getRecordStore()
Definition: collection.h:402
Collection *const OperationContext *const const StringData OptionalCollectionUUID CollectionCatalogEntry *const RecordStore *const recordStore
Definition: collection_impl.cpp:80
bool validateDocuments
Definition: collection.h:86
boost::optional< CollectionUUID > OptionalCollectionUUID
Definition: collection_options.h:55
std::unique_ptr< Impl > _pimpl
Definition: collection.h:778
void refreshUUID(OperationContext *opCtx)
Definition: collection.h:388
const CollectionInfoCache * infoCache() const
Definition: collection.h:376
boost::optional< Timestamp > getMinimumVisibleSnapshot()
If return value is not boost::none, reads with majority read concern using an older snapshot must err...
Definition: collection.h:708
Definition: collection.h:79
Collection(std::unique_ptr< Impl > mock)
Definition: collection.h:356
Definition: index_key_validate.h:40
Collection *const OperationContext *const const StringData OptionalCollectionUUID uuid
Definition: collection_impl.cpp:80
Snapshotted< BSONObj > docFor(OperationContext *const opCtx, const RecordId &loc) const
Definition: collection.h:414
Status insertDocument(OperationContext *const opCtx, const BSONObj &doc, const std::vector< MultiIndexBlock *> &indexBlocks, const bool enforceQuota)
Inserts a document into the record store and adds it to the MultiIndexBlocks passed in...
Definition: collection.h:513
StringData getValidationAction() const
Definition: collection.h:651
Status updateValidator(OperationContext *opCtx, BSONObj newValidator, StringData newLevel, StringData newAction)
Definition: collection.h:655
uint64_t getVersion() const
Returns the version for use as an additional wake condition when used above.
Definition: collection.h:125
StatusWithMatchExpression parseValidator(OperationContext *opCtx, const BSONObj &validator, MatchExpressionParser::AllowedFeatureSet allowedFeatures, boost::optional< ServerGlobalParams::FeatureCompatibility::Version > maxFeatureCompatibilityVersion) const
Returns a non-ok Status if validator is not legal for this collection.
Definition: collection.h:618
RecordId updateDocument(OperationContext *const opCtx, const RecordId &oldLocation, const Snapshotted< BSONObj > &oldDoc, const BSONObj &newDoc, const bool enforceQuota, const bool indexesAffected, OpDebug *const opDebug, OplogUpdateEntryArgs *const args)
Updates the document @ oldLocation with newDoc.
Definition: collection.h:529
An abstraction used for storing documents in a collection or entries in an index. ...
Definition: record_store.h:282
unsigned computeRecordSize(unsigned recordSize) const
Definition: collection.h:90
When a capped collection is modified (delete/insert/etc) then certain notifications need to be made...
Definition: capped_callback.h:44
CollectionCatalogEntry * getCatalogEntry()
Definition: collection.h:364
const Impl & _impl() const
Definition: collection.h:768
const IndexCatalog * getIndexCatalog() const
Definition: collection.h:392
bool requiresIdIndex() const
Definition: collection.h:410
Status touch(OperationContext *const opCtx, const bool touchData, const bool touchIndexes, BSONObjBuilder *const output) const
forces data into cache.
Definition: collection.h:596
Definition: database_catalog_entry.h:50
Collection *const OperationContext *const const StringData fullNS
Definition: collection_impl.cpp:80
bool findDoc(OperationContext *const opCtx, const RecordId &loc, Snapshotted< BSONObj > *const out) const
Definition: collection.h:423
stdx::condition_variable _notifier
Definition: collection.h:141
bool haveCappedWaiters()
Returns true if there may be waiters.
Definition: collection.h:716
void deleteDocument(OperationContext *const opCtx, StmtId stmtId, const RecordId &loc, OpDebug *const opDebug, const bool fromMigrate=false, const bool noWarn=false, StoreDeletedDoc storeDeletedDoc=StoreDeletedDoc::Off)
Deletes the document with the given RecordId from the collection.
Definition: collection.h:455
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
const CollatorInterface * getDefaultCollator() const
Get a pointer to the collection's default collator.
Definition: collection.h:731
Status setValidationLevel(OperationContext *const opCtx, const StringData newLevel)
Definition: collection.h:641
Status aboutToDeleteCapped(OperationContext *const opCtx, const RecordId &loc, const RecordData data) final
This will be called right before loc is deleted when wrapping.
Definition: collection.h:745
Definition: collection.h:97
void notifyCappedWaitersIfNeeded()
Notify (capped collection) waiters of data changes, like an insert.
Definition: collection.h:723
Status truncate(OperationContext *const opCtx)
removes all documents as fast as possible indexes before and after will be the same as will other cha...
Definition: collection.h:575
ValidationLevel
Definition: collection.h:163
Database *const OperationContext *const const StringData name
Definition: database_impl.cpp:82
bool isCapped() const
Definition: collection.h:668
const NamespaceString & ns() const
Definition: collection.h:380
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
const CollectionCatalogEntry * getCatalogEntry() const
Definition: collection.h:368
CursorManager * getCursorManager() const
Definition: collection.h:406
const RecordStore * getRecordStore() const
Definition: collection.h:399
this is for storing things that you want to cache about a single collection life cycle is managed for...
Definition: collection_info_cache.h:47
StringData getValidationLevel() const
Definition: collection.h:648
uint64_t numRecords(OperationContext *const opCtx) const
Definition: collection.h:682
Status setValidator(OperationContext *const opCtx, const BSONObj validator)
Sets the validator for this collection.
Definition: collection.h:637
enum mongo::CompactOptions::PaddingMode paddingMode
uint64_t _version
Definition: collection.h:150
PaddingMode
Definition: collection.h:79
bool _dead
Definition: collection.h:153