Storage Engine API
collection_impl.h
Go to the documentation of this file.
1 /*-
2  * Copyright (C) 2017 MongoDB Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License, version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * As a special exception, the copyright holders give permission to link the
17  * code of portions of this program with the OpenSSL library under certain
18  * conditions as described in each individual source file and distribute
19  * linked combinations including the program with the OpenSSL library. You
20  * must comply with the GNU Affero General Public License in all respects for
21  * all of the code used other than as permitted herein. If you modify file(s)
22  * with this exception, you may extend this exception to your version of the
23  * file(s), but you are not obligated to do so. If you do not wish to do so,
24  * delete this exception statement from your version. If you delete this
25  * exception statement from all source files in the program, then also delete
26  * it in the license file.
27  */
28 
29 #pragma once
30 
31 #include "mongo/bson/timestamp.h"
36 
37 namespace mongo {
38 class IndexConsistency;
39 class UUIDCatalog;
40 class CollectionImpl final : virtual public Collection::Impl,
41  virtual CappedCallback,
42  virtual UpdateNotifier {
43 private:
44  static const int kMagicNumber = 1357924;
45 
46 public:
48  OperationContext* opCtx,
49  StringData fullNS,
51  CollectionCatalogEntry* details, // does not own
52  RecordStore* recordStore, // does not own
53  DatabaseCatalogEntry* dbce); // does not own
54 
56 
57  void init(OperationContext* opCtx) final;
58 
59  bool ok() const final {
60  return _magic == kMagicNumber;
61  }
62 
64  return _details;
65  }
66 
67  const CollectionCatalogEntry* getCatalogEntry() const final {
68  return _details;
69  }
70 
72  return &_infoCache;
73  }
74 
75  const CollectionInfoCache* infoCache() const final {
76  return &_infoCache;
77  }
78 
79  const NamespaceString& ns() const final {
80  return _ns;
81  }
82 
84  return _uuid;
85  }
86 
87  void refreshUUID(OperationContext* opCtx) final;
88 
89  const IndexCatalog* getIndexCatalog() const final {
90  return &_indexCatalog;
91  }
92 
94  return &_indexCatalog;
95  }
96 
97  const RecordStore* getRecordStore() const final {
98  return _recordStore;
99  }
100 
102  return _recordStore;
103  }
104 
105  CursorManager* getCursorManager() const final {
106  return &_cursorManager;
107  }
108 
109  bool requiresIdIndex() const final;
110 
111  Snapshotted<BSONObj> docFor(OperationContext* opCtx, const RecordId& loc) const final {
112  return Snapshotted<BSONObj>(opCtx->recoveryUnit()->getSnapshotId(),
113  _recordStore->dataFor(opCtx, loc).releaseToBson());
114  }
115 
120  bool findDoc(OperationContext* opCtx,
121  const RecordId& loc,
122  Snapshotted<BSONObj>* out) const final;
123 
124  std::unique_ptr<SeekableRecordCursor> getCursor(OperationContext* opCtx,
125  bool forward = true) const final;
126 
131  std::vector<std::unique_ptr<RecordCursor>> getManyCursors(OperationContext* opCtx) const final;
132 
148  void deleteDocument(
149  OperationContext* opCtx,
150  StmtId stmtId,
151  const RecordId& loc,
152  OpDebug* opDebug,
153  bool fromMigrate = false,
154  bool noWarn = false,
156 
157  /*
158  * Inserts all documents inside one WUOW.
159  * Caller should ensure vector is appropriately sized for this.
160  * If any errors occur (including WCE), caller should retry documents individually.
161  *
162  * 'opDebug' Optional argument. When not null, will be used to record operation statistics.
163  */
164  Status insertDocuments(OperationContext* opCtx,
165  std::vector<InsertStatement>::const_iterator begin,
166  std::vector<InsertStatement>::const_iterator end,
167  OpDebug* opDebug,
168  bool enforceQuota,
169  bool fromMigrate = false) final;
170 
178  Status insertDocument(OperationContext* opCtx,
179  const InsertStatement& doc,
180  OpDebug* opDebug,
181  bool enforceQuota,
182  bool fromMigrate = false) final;
183 
188  Status insertDocumentsForOplog(OperationContext* opCtx,
189  const DocWriter* const* docs,
190  Timestamp* timestamps,
191  size_t nDocs) final;
192 
198  Status insertDocument(OperationContext* opCtx,
199  const BSONObj& doc,
200  const std::vector<MultiIndexBlock*>& indexBlocks,
201  bool enforceQuota) final;
202 
212  RecordId updateDocument(OperationContext* opCtx,
213  const RecordId& oldLocation,
214  const Snapshotted<BSONObj>& oldDoc,
215  const BSONObj& newDoc,
216  bool enforceQuota,
217  bool indexesAffected,
218  OpDebug* opDebug,
219  OplogUpdateEntryArgs* args) final;
220 
221  bool updateWithDamagesSupported() const final;
222 
230  StatusWith<RecordData> updateDocumentWithDamages(OperationContext* opCtx,
231  const RecordId& loc,
232  const Snapshotted<RecordData>& oldRec,
233  const char* damageSource,
234  const mutablebson::DamageVector& damages,
235  OplogUpdateEntryArgs* args) final;
236 
237  // -----------
238 
239  StatusWith<CompactStats> compact(OperationContext* opCtx, const CompactOptions* options) final;
240 
246  Status truncate(OperationContext* opCtx) final;
247 
253  Status validate(OperationContext* opCtx,
254  ValidateCmdLevel level,
255  bool background,
256  std::unique_ptr<Lock::CollectionLock> collLk,
257  ValidateResults* results,
258  BSONObjBuilder* output) final;
259 
263  Status touch(OperationContext* opCtx,
264  bool touchData,
265  bool touchIndexes,
266  BSONObjBuilder* output) const final;
267 
274  void cappedTruncateAfter(OperationContext* opCtx, RecordId end, bool inclusive) final;
275 
277 
279 
283  StatusWithMatchExpression parseValidator(
284  OperationContext* opCtx,
285  const BSONObj& validator,
286  MatchExpressionParser::AllowedFeatureSet allowedFeatures,
287  boost::optional<ServerGlobalParams::FeatureCompatibility::Version>
288  maxFeatureCompatibilityVersion = boost::none) const final;
289 
292 
299  Status setValidator(OperationContext* opCtx, BSONObj validator) final;
300 
301  Status setValidationLevel(OperationContext* opCtx, StringData newLevel) final;
302  Status setValidationAction(OperationContext* opCtx, StringData newAction) final;
303 
304  StringData getValidationLevel() const final;
305  StringData getValidationAction() const final;
306 
312  Status updateValidator(OperationContext* opCtx,
313  BSONObj newValidator,
314  StringData newLevel,
315  StringData newAction) final;
316 
317  // -----------
318 
319  //
320  // Stats
321  //
322 
323  bool isCapped() const final;
324 
331  std::shared_ptr<CappedInsertNotifier> getCappedInsertNotifier() const final;
332 
333  uint64_t numRecords(OperationContext* opCtx) const final;
334 
335  uint64_t dataSize(OperationContext* opCtx) const final;
336 
337  inline int averageObjectSize(OperationContext* opCtx) const {
338  uint64_t n = numRecords(opCtx);
339 
340  if (n == 0)
341  return 5;
342  return static_cast<int>(dataSize(opCtx) / n);
343  }
344 
345  uint64_t getIndexSize(OperationContext* opCtx,
346  BSONObjBuilder* details = NULL,
347  int scale = 1) final;
348 
353  boost::optional<Timestamp> getMinimumVisibleSnapshot() final {
354  return _minVisibleSnapshot;
355  }
356 
357  void setMinimumVisibleSnapshot(Timestamp name) final {
359  }
360 
361  bool haveCappedWaiters() final;
362 
366  void notifyCappedWaitersIfNeeded() final;
367 
372  const CollatorInterface* getDefaultCollator() const final;
373 
374 private:
375  inline DatabaseCatalogEntry* dbce() const final {
376  return this->_dbce;
377  }
378 
379  inline CollectionCatalogEntry* details() const final {
380  return this->_details;
381  }
382 
386  Status checkValidation(OperationContext* opCtx, const BSONObj& document) const;
387 
388  Status recordStoreGoingToUpdateInPlace(OperationContext* opCtx, const RecordId& loc);
389 
390  Status aboutToDeleteCapped(OperationContext* opCtx, const RecordId& loc, RecordData data);
391 
397  Status _insertDocument(OperationContext* opCtx, const BSONObj& doc, bool enforceQuota);
398 
399  Status _insertDocuments(OperationContext* opCtx,
400  std::vector<InsertStatement>::const_iterator begin,
401  std::vector<InsertStatement>::const_iterator end,
402  bool enforceQuota,
403  OpDebug* opDebug);
404 
405 
409  StatusWith<RecordId> _updateDocumentWithMove(OperationContext* opCtx,
410  const RecordId& oldLocation,
411  const Snapshotted<BSONObj>& oldDoc,
412  const BSONObj& newDoc,
413  bool enforceQuota,
414  OpDebug* opDebug,
415  OplogUpdateEntryArgs* args,
416  const SnapshotId& sid);
417 
418  bool _enforceQuota(bool userEnforeQuota) const;
419 
420  int _magic;
421 
422  const NamespaceString _ns;
427  const bool _needCappedLock;
430 
431 
432  // The default collation which is applied to operations and indices which have no collation of
433  // their own. The collection's validator will respect this collation.
434  //
435  // If null, the default collation is simple binary compare.
436  std::unique_ptr<CollatorInterface> _collator;
437 
438  // Empty means no filter.
439  BSONObj _validatorDoc;
440 
441  // Points into _validatorDoc. Null means no filter.
442  std::unique_ptr<MatchExpression> _validator;
443 
446 
447  // this is mutable because read only users of the Collection class
448  // use it keep state. This seems valid as const correctness of Collection
449  // should be about the data.
450  mutable CursorManager _cursorManager;
451 
452  // Notifier object for awaitData. Threads polling a capped collection for new data can wait
453  // on this object until notified of the arrival of new data.
454  //
455  // This is non-null if and only if the collection is a capped collection.
456  const std::shared_ptr<CappedInsertNotifier> _cappedNotifier;
457 
458  // The earliest snapshot that is allowed to use this collection.
459  boost::optional<Timestamp> _minVisibleSnapshot;
460 
462 
463  friend class NamespaceDetails;
464 };
465 } // namespace mongo
Queries with the awaitData option use this notifier object to wait for more data to be inserted into ...
Definition: collection.h:105
ValidateCmdLevel
Definition: record_store.h:93
StoreDeletedDoc
Definition: collection.h:164
CursorManager _cursorManager
Definition: collection_impl.h:450
StatusWith< CompactStats > compact(OperationContext *opCtx, const CompactOptions *options) final
Definition: collection_compact.cpp:108
ValidationLevel _validationLevel
Definition: collection_impl.h:445
Definition: snapshot.h:69
Status setValidationLevel(OperationContext *opCtx, StringData newLevel) final
Definition: collection_impl.cpp:994
Definition: record_store.h:671
std::unique_ptr< SeekableRecordCursor > getCursor(OperationContext *opCtx, bool forward=true) const final
Definition: collection_impl.cpp:223
Status _insertDocuments(OperationContext *opCtx, std::vector< InsertStatement >::const_iterator begin, std::vector< InsertStatement >::const_iterator end, bool enforceQuota, OpDebug *opDebug)
Definition: collection_impl.cpp:460
ValidationAction _validationAction
Definition: collection_impl.h:444
StringData getValidationAction() const final
Definition: collection_impl.cpp:984
Status aboutToDeleteCapped(OperationContext *opCtx, const RecordId &loc, RecordData data)
This will be called right before loc is deleted when wrapping.
Definition: collection_impl.cpp:535
bool requiresIdIndex() const final
Definition: collection_impl.cpp:207
const NamespaceString _ns
Definition: collection_impl.h:422
CollectionCatalogEntry * details() const final
Definition: collection_impl.h:379
RecordStore * getRecordStore() final
Definition: collection_impl.h:101
static StatusWith< ValidationAction > parseValidationAction(StringData)
Definition: collection_impl.cpp:958
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
bool _enforceQuota(bool userEnforeQuota) const
Definition: collection_impl.cpp:807
Definition: collection_catalog_entry.h:47
Definition: collection_impl.h:40
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
void refreshUUID(OperationContext *opCtx) final
Definition: collection_impl.cpp:198
A replacement for the Record class.
Definition: record_data.h:43
const NamespaceString & ns() const final
Definition: collection_impl.h:79
Status _insertDocument(OperationContext *opCtx, const BSONObj &doc, bool enforceQuota)
same semantics as insertDocument, but doesn&#39;t do:
int _magic
Definition: collection_impl.h:420
std::unique_ptr< CollatorInterface > _collator
Definition: collection_impl.h:436
const CollatorInterface * getDefaultCollator() const final
Get a pointer to the collection&#39;s default collator.
Definition: collection_impl.cpp:1074
Status setValidator(OperationContext *opCtx, BSONObj validator) final
Sets the validator for this collection.
Definition: collection_impl.cpp:913
Definition: collection.h:166
CollectionCatalogEntry * getCatalogEntry() final
Definition: collection_impl.h:63
Definition: record_store.h:78
BSONObj _validatorDoc
Definition: collection_impl.h:439
Status insertDocument(OperationContext *opCtx, const InsertStatement &doc, OpDebug *opDebug, bool enforceQuota, bool fromMigrate=false) final
this does NOT modify the doc before inserting i.e.
Definition: collection_impl.cpp:390
RecordStore *const _recordStore
Definition: collection_impl.h:425
Snapshotted< BSONObj > docFor(OperationContext *opCtx, const RecordId &loc) const final
Definition: collection_impl.h:111
bool inclusive
Definition: btree_interface.cpp:335
CollectionCatalogEntry *const _details
Definition: collection_impl.h:424
CollectionInfoCache * infoCache() final
Definition: collection_impl.h:71
int averageObjectSize(OperationContext *opCtx) const
Definition: collection_impl.h:337
this is NOT safe through a yield right now.
Definition: collection.h:160
bool findDoc(OperationContext *opCtx, const RecordId &loc, Snapshotted< BSONObj > *out) const final
Definition: collection_impl.cpp:239
Definition: collection.h:77
Allows inserting a Record "in-place" without creating a copy ahead of time.
Definition: record_store.h:62
StatusWith< RecordId > _updateDocumentWithMove(OperationContext *opCtx, const RecordId &oldLocation, const Snapshotted< BSONObj > &oldDoc, const BSONObj &newDoc, bool enforceQuota, OpDebug *opDebug, OplogUpdateEntryArgs *args, const SnapshotId &sid)
Perform update when document move will be required.
Definition: collection_impl.cpp:712
ValidationAction
Definition: collection.h:162
Definition: namespace_details.h:47
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
Status touch(OperationContext *opCtx, bool touchData, bool touchIndexes, BSONObjBuilder *output) const final
forces data into cache
Definition: collection_impl.cpp:1352
RecordId updateDocument(OperationContext *opCtx, const RecordId &oldLocation, const Snapshotted< BSONObj > &oldDoc, const BSONObj &newDoc, bool enforceQuota, bool indexesAffected, OpDebug *opDebug, OplogUpdateEntryArgs *args) final
Updates the document @ oldLocation with newDoc.
Definition: collection_impl.cpp:593
Status checkValidation(OperationContext *opCtx, const BSONObj &document) const
Returns a non-ok Status if document does not pass this collection&#39;s validator.
Definition: collection_impl.cpp:251
const CollectionInfoCache * infoCache() const final
Definition: collection_impl.h:75
how many: 1 per Collection.
Definition: index_catalog.h:62
uint64_t getIndexSize(OperationContext *opCtx, BSONObjBuilder *details=NULL, int scale=1) final
Definition: collection_impl.cpp:840
bool updateWithDamagesSupported() const final
Definition: collection_impl.cpp:775
const bool _needCappedLock
Definition: collection_impl.h:427
bool isCapped() const final
Definition: collection_impl.cpp:823
Collection *const OperationContext *const const StringData OptionalCollectionUUID CollectionCatalogEntry *const RecordStore *const recordStore
Definition: collection_impl.cpp:80
CollectionInfoCache _infoCache
Definition: collection_impl.h:428
Status insertDocuments(OperationContext *opCtx, std::vector< InsertStatement >::const_iterator begin, std::vector< InsertStatement >::const_iterator end, OpDebug *opDebug, bool enforceQuota, bool fromMigrate=false) final
Definition: collection_impl.cpp:338
boost::optional< CollectionUUID > OptionalCollectionUUID
Definition: collection_options.h:55
static const int kMagicNumber
Definition: collection_impl.h:44
bool haveCappedWaiters() final
Returns true if there may be waiters.
Definition: collection_impl.cpp:522
IndexCatalog _indexCatalog
Definition: collection_impl.h:429
OptionalCollectionUUID uuid() const
Definition: collection_impl.h:83
Definition: index_key_validate.h:40
static StatusWith< ValidationLevel > parseValidationLevel(StringData)
Definition: collection_impl.cpp:942
void init(OperationContext *opCtx) final
Definition: collection_impl.cpp:169
virtual RecordData dataFor(OperationContext *opCtx, const RecordId &loc) const
Get the RecordData at loc, which must exist.
Definition: record_store.h:337
An abstraction used for storing documents in a collection or entries in an index. ...
Definition: record_store.h:282
When a capped collection is modified (delete/insert/etc) then certain notifications need to be made...
Definition: capped_callback.h:44
Status validate(OperationContext *opCtx, ValidateCmdLevel level, bool background, std::unique_ptr< Lock::CollectionLock > collLk, ValidateResults *results, BSONObjBuilder *output) final
Definition: collection_impl.cpp:1281
std::vector< std::unique_ptr< RecordCursor > > getManyCursors(OperationContext *opCtx) const final
Returns many cursors that partition the Collection into many disjoint sets.
Definition: collection_impl.cpp:231
Status truncate(OperationContext *opCtx) final
removes all documents as fast as possible indexes before and after will be the same as will other cha...
Definition: collection_impl.cpp:869
BSONObj releaseToBson()
Definition: record_data.h:74
StringData getValidationLevel() const final
Definition: collection_impl.cpp:972
uint64_t numRecords(OperationContext *opCtx) const final
Definition: collection_impl.cpp:832
~CollectionImpl()
Definition: collection_impl.cpp:178
IndexCatalog * getIndexCatalog() final
Definition: collection_impl.h:93
Status insertDocumentsForOplog(OperationContext *opCtx, const DocWriter *const *docs, Timestamp *timestamps, size_t nDocs) final
Callers must ensure no document validation is performed for this collection when calling this method...
Definition: collection_impl.cpp:315
CursorManager * getCursorManager() const final
Definition: collection_impl.h:105
Definition: database_catalog_entry.h:50
Collection *const OperationContext *const const StringData fullNS
Definition: collection_impl.cpp:80
std::shared_ptr< CappedInsertNotifier > getCappedInsertNotifier() const final
Get a pointer to a capped insert notifier object.
Definition: collection_impl.cpp:827
boost::optional< Timestamp > _minVisibleSnapshot
Definition: collection_impl.h:459
Collection * _this
Definition: collection_impl.h:461
Status setValidationAction(OperationContext *opCtx, StringData newAction) final
Definition: collection_impl.cpp:1012
DatabaseCatalogEntry *const _dbce
Definition: collection_impl.h:426
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
const IndexCatalog * getIndexCatalog() const final
Definition: collection_impl.h:89
Status updateValidator(OperationContext *opCtx, BSONObj newValidator, StringData newLevel, StringData newAction) final
Sets the validator to exactly what&#39;s provided.
Definition: collection_impl.cpp:1030
void cappedTruncateAfter(OperationContext *opCtx, RecordId end, bool inclusive) final
Truncate documents newer than the document at &#39;end&#39; from the capped collection.
Definition: collection_impl.cpp:903
StatusWithMatchExpression parseValidator(OperationContext *opCtx, const BSONObj &validator, MatchExpressionParser::AllowedFeatureSet allowedFeatures, boost::optional< ServerGlobalParams::FeatureCompatibility::Version > maxFeatureCompatibilityVersion=boost::none) const final
Returns a non-ok Status if validator is not legal for this collection.
Definition: collection_impl.cpp:273
Status recordStoreGoingToUpdateInPlace(OperationContext *opCtx, const RecordId &loc)
Definition: collection_impl.cpp:767
boost::optional< Timestamp > getMinimumVisibleSnapshot() final
If return value is not boost::none, reads with majority read concern using an older snapshot must err...
Definition: collection_impl.h:353
Definition: snapshot.h:37
ValidationLevel
Definition: collection.h:163
bool ok() const final
Definition: collection_impl.h:59
uint64_t dataSize(OperationContext *opCtx) const final
Definition: collection_impl.cpp:836
Database *const OperationContext *const const StringData name
Definition: database_impl.cpp:82
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
CollectionImpl(Collection *_this, OperationContext *opCtx, StringData fullNS, OptionalCollectionUUID uuid, CollectionCatalogEntry *details, RecordStore *recordStore, DatabaseCatalogEntry *dbce)
Definition: collection_impl.cpp:141
OptionalCollectionUUID _uuid
Definition: collection_impl.h:423
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
DatabaseCatalogEntry * dbce() const final
Definition: collection_impl.h:375
const RecordStore * getRecordStore() const final
Definition: collection_impl.h:97
void setMinimumVisibleSnapshot(Timestamp name) final
Definition: collection_impl.h:357
const CollectionCatalogEntry * getCatalogEntry() const final
Definition: collection_impl.h:67
void notifyCappedWaitersIfNeeded() final
Notify (capped collection) waiters of data changes, like an insert.
Definition: collection_impl.cpp:528
void deleteDocument(OperationContext *opCtx, StmtId stmtId, const RecordId &loc, OpDebug *opDebug, bool fromMigrate=false, bool noWarn=false, Collection::StoreDeletedDoc storeDeletedDoc=Collection::StoreDeletedDoc::Off) final
Deletes the document with the given RecordId from the collection.
Definition: collection_impl.cpp:554
StatusWith< RecordData > updateDocumentWithDamages(OperationContext *opCtx, const RecordId &loc, const Snapshotted< RecordData > &oldRec, const char *damageSource, const mutablebson::DamageVector &damages, OplogUpdateEntryArgs *args) final
Not allowed to modify indexes.
Definition: collection_impl.cpp:782
std::unique_ptr< MatchExpression > _validator
Definition: collection_impl.h:442
const std::shared_ptr< CappedInsertNotifier > _cappedNotifier
Definition: collection_impl.h:456