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  RecordData dataFor(OperationContext* opCtx, const RecordId& recId) const override;
57 
58  bool findRecord(OperationContext* opCtx, const RecordId& recId, RecordData* rd) const override;
59 
60  void deleteRecord(OperationContext* opCtx, const RecordId& dl) override;
61 
62  StatusWith<RecordId> insertRecord(OperationContext* opCtx,
63  const char* data,
64  int len,
65  Timestamp timestamp,
66  bool enforceQuota) override;
67 
68  Status insertRecordsWithDocWriter(OperationContext* opCtx,
69  const DocWriter* const* docs,
70  const Timestamp* timestamps,
71  size_t nDocs,
72  RecordId* idsOut) override;
73 
74  Status updateRecord(OperationContext* opCtx,
75  const RecordId& oldLocation,
76  const char* data,
77  int len,
78  bool enforceQuota,
79  UpdateNotifier* notifier) override;
80 
81  bool updateWithDamagesSupported() const override;
82 
83  StatusWith<RecordData> updateWithDamages(OperationContext* opCtx,
84  const RecordId& recId,
85  const RecordData& oldRec,
86  const char* damageSource,
87  const mutablebson::DamageVector& damages) override;
88 
89  std::unique_ptr<SeekableRecordCursor> getCursor(OperationContext* opCtx,
90  bool forward) const override;
91 
92  Status truncate(OperationContext* opCtx) override;
93 
94  void cappedTruncateAfter(OperationContext* opCtx, RecordId end, bool inclusive) override;
95 
96  bool compactSupported() const override {
97  return true;
98  }
99 
100  bool compactsInPlace() const override {
101  return true;
102  }
103 
104  Status compact(OperationContext* opCtx,
105  RecordStoreCompactAdaptor* adaptor,
106  const CompactOptions* options,
107  CompactStats* stats) override;
108 
109  Status validate(OperationContext* opCtx,
110  ValidateCmdLevel level,
111  ValidateAdaptor* adaptor,
112  ValidateResults* results,
113  BSONObjBuilder* output) override;
114 
115  void appendCustomStats(OperationContext* opCtx,
116  BSONObjBuilder* result,
117  double scale) const override;
118 
119  Status touch(OperationContext* opCtx, BSONObjBuilder* output) const override;
120 
121  int64_t storageSize(OperationContext* opCtx,
122  BSONObjBuilder* extraInfo = NULL,
123  int infoLevel = 0) const override;
124 
125  long long dataSize(OperationContext* opCtx) const override;
126 
127  long long numRecords(OperationContext* opCtx) const override;
128 
129  boost::optional<RecordId> oplogStartHack(OperationContext* opCtx,
130  const RecordId& startingPosition) const override;
131 
132  void waitForAllEarlierOplogWritesToBeVisible(OperationContext* opCtx) const override {}
133 
134  void updateStatsAfterRepair(OperationContext* opCtx,
135  long long numRecords,
136  long long dataSize) override {}
137 
138  bool isCapped() const override {
139  return _isCapped;
140  }
141 
142  void setCappedCallback(CappedCallback* cb) override {
143  _cappedCallback = cb;
144  }
145 
146  Status updateCappedSize(OperationContext* opCtx, long long cappedSize) override;
147 
148  // Not in record store API.
149 
154  static void create(OperationContext* opCtx, const std::string& ident);
155 
156 private:
157  class InsertChange;
158  class RemoveChange;
159  class TruncateChange;
160 
161  class NumRecsChange;
162  class DataSizeChange;
163 
164  class Cursor;
165 
166  RecordId _nextId();
167 
168  const std::string _path;
169  const std::string _ident;
170 
171  // True if the namespace of this record store starts with "local.oplog.", and false otherwise.
172  const bool _isOplog;
173 
177  bool _isCappedAndNeedsDelete(int64_t numRecs, int64_t numBytes);
178 
183  void _notifyCappedCallbackIfNeeded_inlock(OperationContext* opCtx,
184  RecordId recId,
185  const RecordData& recData);
186 
191  void _doCappedDelete(OperationContext* opCtx,
192  SqliteStatement& stmt,
193  const std::string& direction,
194  int64_t startRecId = 0);
195 
199  void _cappedDeleteIfNeeded(OperationContext* opCtx);
200 
201  const bool _isCapped;
202  int64_t _cappedMaxSize;
203  const int64_t _cappedMaxDocs;
204  // Mutex that protects _cappedCallback
205  stdx::mutex _cappedCallbackMutex;
207 
208  AtomicInt64 _nextIdNum;
209 
214  void _initNumRecsIfNeeded_inlock(OperationContext* opCtx) const;
215 
219  void _changeNumRecs(OperationContext* opCtx, int64_t diff);
220 
225  bool _resetNumRecsIfNeeded(OperationContext* opCtx, int64_t newNumRecs);
226 
227  mutable int64_t _numRecs;
228  mutable stdx::mutex _numRecsMutex;
229  mutable bool _isNumRecsInitialized = false;
230 
235  void _initDataSizeIfNeeded_inlock(OperationContext* opCtx) const;
236 
240  void _changeDataSize(OperationContext* opCtx, int64_t diff);
241 
246  bool _resetDataSizeIfNeeded(OperationContext* opCtx, int64_t newDataSize);
247 
248  mutable int64_t _dataSize;
249  mutable stdx::mutex _dataSizeMutex;
250  mutable bool _isDataSizeInitialized = false;
251 };
252 
253 } // namespace mongo
Definition: record_store.h:663
bool _resetDataSizeIfNeeded(OperationContext *opCtx, int64_t newDataSize)
Resets _dataSize to the new value.
Definition: mobile_record_store.cpp:740
bool _resetNumRecsIfNeeded(OperationContext *opCtx, int64_t newNumRecs)
Resets _numRecords to the new value.
Definition: mobile_record_store.cpp:703
void _initNumRecsIfNeeded_inlock(OperationContext *opCtx) const
Fetches the number of records from the database.
Definition: mobile_record_store.cpp:248
Status touch(OperationContext *opCtx, BSONObjBuilder *output) const override
Load all data into cache.
Definition: mobile_record_store.cpp:657
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:242
Keeps track of the total data size.
Definition: mobile_record_store.cpp:717
long long numRecords(OperationContext *opCtx) const override
Total number of record in the RecordStore.
Definition: mobile_record_store.cpp:265
Definition: record_store.h:671
const bool _isCapped
Definition: mobile_record_store.h:201
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:557
bool compactsInPlace() const override
Does compact() leave RecordIds alone or can they change.
Definition: mobile_record_store.h:100
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:696
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:386
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
A replacement for the Record class.
Definition: record_data.h:43
int64_t _cappedMaxSize
Definition: mobile_record_store.h:202
void setCappedCallback(CappedCallback *cb) override
Definition: mobile_record_store.h:142
void _changeDataSize(OperationContext *opCtx, int64_t diff)
Updates _dataSize.
Definition: mobile_record_store.cpp:733
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:546
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:319
const std::string _path
Definition: mobile_record_store.h:168
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:685
Definition: record_store.h:78
stdx::mutex _cappedCallbackMutex
Definition: mobile_record_store.h:205
std::unique_ptr< SeekableRecordCursor > getCursor(OperationContext *opCtx, bool forward) const override
Returns a new cursor over this record store.
Definition: mobile_record_store.cpp:494
Status updateRecord(OperationContext *opCtx, const RecordId &oldLocation, const char *data, int len, bool enforceQuota, UpdateNotifier *notifier) override
Definition: mobile_record_store.cpp:449
bool inclusive
Definition: btree_interface.cpp:335
int64_t _dataSize
Definition: mobile_record_store.h:248
StatusWith< RecordId > insertRecord(OperationContext *opCtx, const char *data, int len, Timestamp timestamp, bool enforceQuota) override
Definition: mobile_record_store.cpp:400
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:250
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
void appendCustomStats(OperationContext *opCtx, BSONObjBuilder *result, double scale) const override
Definition: mobile_record_store.cpp:647
stdx::mutex _dataSizeMutex
Definition: mobile_record_store.h:249
bool isCapped() const override
Definition: mobile_record_store.h:138
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:425
RecordData dataFor(OperationContext *opCtx, const RecordId &recId) const override
Get the RecordData at loc, which must exist.
Definition: mobile_record_store.cpp:271
Status updateCappedSize(OperationContext *opCtx, long long cappedSize) override
used to support online change oplog size.
Definition: mobile_record_store.cpp:752
void deleteRecord(OperationContext *opCtx, const RecordId &dl) override
Definition: mobile_record_store.cpp:299
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:521
Definition: mobile_record_store.cpp:55
void _doCappedDelete(OperationContext *opCtx, SqliteStatement &stmt, const std::string &direction, int64_t startRecId=0)
Performs the capped deletion.
Definition: mobile_record_store.cpp:337
Keeps track of the changes to the number of records.
Definition: mobile_record_store.cpp:680
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:757
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:665
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:169
virtual const std::string & ns() const
Definition: record_store.h:295
CappedCallback * _cappedCallback
Definition: mobile_record_store.h:206
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:327
RecordId _nextId()
Definition: mobile_record_store.cpp:671
SqliteStatement is a wrapper around the sqlite3_stmt object.
Definition: mobile_sqlite_statement.h:42
int64_t _numRecs
Definition: mobile_record_store.h:227
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:132
const int64_t _cappedMaxDocs
Definition: mobile_record_store.h:203
Status truncate(OperationContext *opCtx) override
SQLite does not directly support truncate.
Definition: mobile_record_store.cpp:503
const bool _isOplog
Definition: mobile_record_store.h:172
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
Definition: collection.h:97
AtomicInt64 _nextIdNum
Definition: mobile_record_store.h:208
bool updateWithDamagesSupported() const override
Definition: mobile_record_store.cpp:481
bool compactSupported() const override
does this RecordStore support the compact operation?
Definition: mobile_record_store.h:96
void _initDataSizeIfNeeded_inlock(OperationContext *opCtx) const
Fetches the data size from the database.
Definition: mobile_record_store.cpp:226
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:278
stdx::mutex _numRecsMutex
Definition: mobile_record_store.h:228
StatusWith< RecordData > updateWithDamages(OperationContext *opCtx, const RecordId &recId, const RecordData &oldRec, const char *damageSource, const mutablebson::DamageVector &damages) override
Updates the record positioned at &#39;loc&#39; in-place using the deltas described by &#39;damages&#39;.
Definition: mobile_record_store.cpp:485
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:766
bool _isNumRecsInitialized
Definition: mobile_record_store.h:229
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:134