Storage Engine API
mobile_record_store.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <memory>
32 #include <string>
33 
35 #include "mongo/db/operation_context.h"
39 #include "mongo/platform/atomic_word.h"
40 
41 namespace mongo {
42 
47 public:
48  explicit MobileRecordStore(OperationContext* opCtx,
49  StringData ns,
50  const std::string& path,
51  const std::string& ident,
52  const CollectionOptions& options);
53 
54  const char* name() const override;
55 
56  const std::string& getIdent() const override;
57 
58  RecordData dataFor(OperationContext* opCtx, const RecordId& recId) const override;
59 
60  bool findRecord(OperationContext* opCtx, const RecordId& recId, RecordData* rd) const override;
61 
62  void deleteRecord(OperationContext* opCtx, const RecordId& dl) override;
63 
64  StatusWith<RecordId> insertRecord(OperationContext* opCtx,
65  const char* data,
66  int len,
67  Timestamp timestamp,
68  bool enforceQuota) override;
69 
70  Status insertRecordsWithDocWriter(OperationContext* opCtx,
71  const DocWriter* const* docs,
72  const Timestamp* timestamps,
73  size_t nDocs,
74  RecordId* idsOut) override;
75 
76  Status updateRecord(OperationContext* opCtx,
77  const RecordId& oldLocation,
78  const char* data,
79  int len,
80  bool enforceQuota,
81  UpdateNotifier* notifier) override;
82 
83  bool updateWithDamagesSupported() const override;
84 
86  const RecordId& recId,
87  const RecordData& oldRec,
88  const char* damageSource,
89  const mutablebson::DamageVector& damages) override;
90 
91  std::unique_ptr<SeekableRecordCursor> getCursor(OperationContext* opCtx,
92  bool forward) const override;
93 
94  Status truncate(OperationContext* opCtx) override;
95 
96  void cappedTruncateAfter(OperationContext* opCtx, RecordId end, bool inclusive) override;
97 
98  bool compactSupported() const override {
99  return true;
100  }
101 
102  bool compactsInPlace() const override {
103  return true;
104  }
105 
106  Status compact(OperationContext* opCtx,
107  RecordStoreCompactAdaptor* adaptor,
108  const CompactOptions* options,
109  CompactStats* stats) override;
110 
111  Status validate(OperationContext* opCtx,
112  ValidateCmdLevel level,
113  ValidateAdaptor* adaptor,
114  ValidateResults* results,
115  BSONObjBuilder* output) override;
116 
117  void appendCustomStats(OperationContext* opCtx,
118  BSONObjBuilder* result,
119  double scale) const override;
120 
121  Status touch(OperationContext* opCtx, BSONObjBuilder* output) const override;
122 
123  int64_t storageSize(OperationContext* opCtx,
124  BSONObjBuilder* extraInfo = NULL,
125  int infoLevel = 0) const override;
126 
127  long long dataSize(OperationContext* opCtx) const override;
128 
129  long long numRecords(OperationContext* opCtx) const override;
130 
131  boost::optional<RecordId> oplogStartHack(OperationContext* opCtx,
132  const RecordId& startingPosition) const override;
133 
134  void waitForAllEarlierOplogWritesToBeVisible(OperationContext* opCtx) const override {}
135 
136  void updateStatsAfterRepair(OperationContext* opCtx,
137  long long numRecords,
138  long long dataSize) override {}
139 
140  bool isCapped() const override {
141  return _isCapped;
142  }
143 
144  void setCappedCallback(CappedCallback* cb) override {
145  _cappedCallback = cb;
146  }
147 
148  Status updateCappedSize(OperationContext* opCtx, long long cappedSize) override;
149 
150  // Not in record store API.
151 
156  static void create(OperationContext* opCtx, const std::string& ident);
157 
158 private:
159  class InsertChange;
160  class RemoveChange;
161  class TruncateChange;
162 
163  class NumRecsChange;
164  class DataSizeChange;
165 
166  class Cursor;
167 
168  RecordId _nextId();
169 
170  const std::string _path;
171  const std::string _ident;
172 
173  // True if the namespace of this record store starts with "local.oplog.", and false otherwise.
174  const bool _isOplog;
175 
179  bool _isCappedAndNeedsDelete(int64_t numRecs, int64_t numBytes);
180 
185  void _notifyCappedCallbackIfNeeded_inlock(OperationContext* opCtx,
186  RecordId recId,
187  const RecordData& recData);
188 
193  void _doCappedDelete(OperationContext* opCtx,
194  SqliteStatement& stmt,
195  const std::string& direction,
196  int64_t startRecId = 0);
197 
201  void _cappedDeleteIfNeeded(OperationContext* opCtx);
202 
203  const bool _isCapped;
204  int64_t _cappedMaxSize;
205  const int64_t _cappedMaxDocs;
206  // Mutex that protects _cappedCallback
207  stdx::mutex _cappedCallbackMutex;
209 
210  AtomicInt64 _nextIdNum;
211 
216  void _initNumRecsIfNeeded_inlock(OperationContext* opCtx) const;
217 
221  void _changeNumRecs(OperationContext* opCtx, int64_t diff);
222 
227  bool _resetNumRecsIfNeeded(OperationContext* opCtx, int64_t newNumRecs);
228 
229  mutable int64_t _numRecs;
230  mutable stdx::mutex _numRecsMutex;
231  mutable bool _isNumRecsInitialized = false;
232 
237  void _initDataSizeIfNeeded_inlock(OperationContext* opCtx) const;
238 
242  void _changeDataSize(OperationContext* opCtx, int64_t diff);
243 
248  bool _resetDataSizeIfNeeded(OperationContext* opCtx, int64_t newDataSize);
249 
250  mutable int64_t _dataSize;
251  mutable stdx::mutex _dataSizeMutex;
252  mutable bool _isDataSizeInitialized = false;
253 };
254 
255 } // namespace mongo
Definition: record_store.h:665
bool _resetDataSizeIfNeeded(OperationContext *opCtx, int64_t newDataSize)
Resets _dataSize to the new value.
Definition: mobile_record_store.cpp:743
bool _resetNumRecsIfNeeded(OperationContext *opCtx, int64_t newNumRecs)
Resets _numRecords to the new value.
Definition: mobile_record_store.cpp:706
void _initNumRecsIfNeeded_inlock(OperationContext *opCtx) const
Fetches the number of records from the database.
Definition: mobile_record_store.cpp:252
Status touch(OperationContext *opCtx, BSONObjBuilder *output) const override
Load all data into cache.
Definition: mobile_record_store.cpp:660
AtomicLockStats stats
Definition: lock_state.cpp:92
ValidateCmdLevel
Definition: record_store.h:93
long long dataSize(OperationContext *opCtx) const override
The dataSize is an approximation of the sum of the sizes (in bytes) of the documents or entries in th...
Definition: mobile_record_store.cpp:246
long long numRecords(OperationContext *opCtx) const override
Total number of record in the RecordStore.
Definition: mobile_record_store.cpp:269
Definition: record_store.h:673
const bool _isCapped
Definition: mobile_record_store.h:203
Status validate(OperationContext *opCtx, ValidateCmdLevel level, ValidateAdaptor *adaptor, ValidateResults *results, BSONObjBuilder *output) override
Note: on full validation, this validates the entire database file, not just the table used by this re...
Definition: mobile_record_store.cpp:560
bool compactsInPlace() const override
Does compact() leave RecordIds alone or can they change.
Definition: mobile_record_store.h:102
A RecordStore that stores all data in SQLite.
Definition: mobile_record_store.h:46
Definition: collection_options.h:57
void _changeNumRecs(OperationContext *opCtx, int64_t diff)
Updates _numRecords.
Definition: mobile_record_store.cpp:699
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
void _cappedDeleteIfNeeded(OperationContext *opCtx)
Deletes records from a capped database if the cap is exceeded.
Definition: mobile_record_store.cpp:389
A replacement for the Record class.
Definition: record_data.h:43
int64_t _cappedMaxSize
Definition: mobile_record_store.h:204
void setCappedCallback(CappedCallback *cb) override
Definition: mobile_record_store.h:144
void _changeDataSize(OperationContext *opCtx, int64_t diff)
Updates _dataSize.
Definition: mobile_record_store.cpp:736
Status compact(OperationContext *opCtx, RecordStoreCompactAdaptor *adaptor, const CompactOptions *options, CompactStats *stats) override
Attempt to reduce the storage space used by this RecordStore.
Definition: mobile_record_store.cpp:549
bool _isCappedAndNeedsDelete(int64_t numRecs, int64_t numBytes)
Returns true if the collection is capped and exceeds the size or document cap.
Definition: mobile_record_store.cpp:323
const std::string _path
Definition: mobile_record_store.h:170
This is so when a RecordStore is validating all records it can call back to someone to check if a rec...
Definition: record_store.h:687
Definition: record_store.h:78
stdx::mutex _cappedCallbackMutex
Definition: mobile_record_store.h:207
std::unique_ptr< SeekableRecordCursor > getCursor(OperationContext *opCtx, bool forward) const override
Returns a new cursor over this record store.
Definition: mobile_record_store.cpp:497
Status updateRecord(OperationContext *opCtx, const RecordId &oldLocation, const char *data, int len, bool enforceQuota, UpdateNotifier *notifier) override
Definition: mobile_record_store.cpp:452
bool inclusive
Definition: btree_interface.cpp:335
int64_t _dataSize
Definition: mobile_record_store.h:250
StatusWith< RecordId > insertRecord(OperationContext *opCtx, const char *data, int len, Timestamp timestamp, bool enforceQuota) override
Definition: mobile_record_store.cpp:403
MobileRecordStore(OperationContext *opCtx, StringData ns, const std::string &path, const std::string &ident, const CollectionOptions &options)
Definition: mobile_record_store.cpp:193
bool _isDataSizeInitialized
Definition: mobile_record_store.h:252
Definition: collection.h:77
Allows inserting a Record "in-place" without creating a copy ahead of time.
Definition: record_store.h:62
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
const std::string & getIdent() const override
Definition: mobile_record_store.cpp:226
void appendCustomStats(OperationContext *opCtx, BSONObjBuilder *result, double scale) const override
Definition: mobile_record_store.cpp:650
stdx::mutex _dataSizeMutex
Definition: mobile_record_store.h:251
bool isCapped() const override
Definition: mobile_record_store.h:140
Status insertRecordsWithDocWriter(OperationContext *opCtx, const DocWriter *const *docs, const Timestamp *timestamps, size_t nDocs, RecordId *idsOut) override
Inserts nDocs documents into this RecordStore using the DocWriter interface.
Definition: mobile_record_store.cpp:428
RecordData dataFor(OperationContext *opCtx, const RecordId &recId) const override
Get the RecordData at loc, which must exist.
Definition: mobile_record_store.cpp:275
OperationContext Database StringData CollectionOptions bool const BSONObj &idIndex Status
Definition: database_impl.cpp:955
Status updateCappedSize(OperationContext *opCtx, long long cappedSize) override
used to support online change oplog size.
Definition: mobile_record_store.cpp:755
void deleteRecord(OperationContext *opCtx, const RecordId &dl) override
Definition: mobile_record_store.cpp:303
Definition: index_key_validate.h:40
void cappedTruncateAfter(OperationContext *opCtx, RecordId end, bool inclusive) override
The method throws an assertion if the capped truncate results in an emptied table.
Definition: mobile_record_store.cpp:524
void _doCappedDelete(OperationContext *opCtx, SqliteStatement &stmt, const std::string &direction, int64_t startRecId=0)
Performs the capped deletion.
Definition: mobile_record_store.cpp:341
boost::optional< RecordId > oplogStartHack(OperationContext *opCtx, const RecordId &startingPosition) const override
Return the RecordId of an oplog entry as close to startingPosition as possible without being higher.
Definition: mobile_record_store.cpp:760
int64_t storageSize(OperationContext *opCtx, BSONObjBuilder *extraInfo=NULL, int infoLevel=0) const override
Note: does not accurately return the size of the table on disk.
Definition: mobile_record_store.cpp:668
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
const std::string _ident
Definition: mobile_record_store.h:171
virtual const std::string & ns() const
Definition: record_store.h:295
CappedCallback * _cappedCallback
Definition: mobile_record_store.h:208
void _notifyCappedCallbackIfNeeded_inlock(OperationContext *opCtx, RecordId recId, const RecordData &recData)
Notifies the capped callback that a capped collection is about to delete a record.
Definition: mobile_record_store.cpp:331
RecordId _nextId()
Definition: mobile_record_store.cpp:674
SqliteStatement is a wrapper around the sqlite3_stmt object.
Definition: mobile_sqlite_statement.h:43
int64_t _numRecs
Definition: mobile_record_store.h:229
void waitForAllEarlierOplogWritesToBeVisible(OperationContext *opCtx) const override
Waits for all writes that completed before this call to be visible to forward scans.
Definition: mobile_record_store.h:134
const int64_t _cappedMaxDocs
Definition: mobile_record_store.h:205
Status truncate(OperationContext *opCtx) override
SQLite does not directly support truncate.
Definition: mobile_record_store.cpp:507
const bool _isOplog
Definition: mobile_record_store.h:174
Definition: collection.h:97
AtomicInt64 _nextIdNum
Definition: mobile_record_store.h:210
bool updateWithDamagesSupported() const override
Definition: mobile_record_store.cpp:484
bool compactSupported() const override
does this RecordStore support the compact operation?
Definition: mobile_record_store.h:98
void _initDataSizeIfNeeded_inlock(OperationContext *opCtx) const
Fetches the data size from the database.
Definition: mobile_record_store.cpp:230
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
bool findRecord(OperationContext *opCtx, const RecordId &recId, RecordData *rd) const override
Definition: mobile_record_store.cpp:282
stdx::mutex _numRecsMutex
Definition: mobile_record_store.h:230
StatusWith< RecordData > updateWithDamages(OperationContext *opCtx, const RecordId &recId, const RecordData &oldRec, const char *damageSource, const mutablebson::DamageVector &damages) override
Updates the record positioned at 'loc' in-place using the deltas described by 'damages'.
Definition: mobile_record_store.cpp:488
const char * name() const override
Definition: mobile_record_store.cpp:222
static void create(OperationContext *opCtx, const std::string &ident)
Creates a new record store inside SQLite.
Definition: mobile_record_store.cpp:769
bool _isNumRecsInitialized
Definition: mobile_record_store.h:231
void updateStatsAfterRepair(OperationContext *opCtx, long long numRecords, long long dataSize) override
Called after a repair operation is run with the recomputed numRecords and dataSize.
Definition: mobile_record_store.h:136