Storage Engine API
ephemeral_for_test_record_store.h
Go to the documentation of this file.
1 // ephemeral_for_test_record_store.h
2 
31 #pragma once
32 
33 #include <boost/shared_array.hpp>
34 #include <map>
35 
39 #include "mongo/stdx/mutex.h"
40 
41 namespace mongo {
42 
49 public:
50  explicit EphemeralForTestRecordStore(StringData ns,
51  std::shared_ptr<void>* dataInOut,
52  bool isCapped = false,
53  int64_t cappedMaxSize = -1,
54  int64_t cappedMaxDocs = -1,
55  CappedCallback* cappedCallback = nullptr);
56 
57  virtual const char* name() const;
58 
59  const std::string& getIdent() const override {
60  return ns();
61  }
62 
63  virtual RecordData dataFor(OperationContext* opCtx, const RecordId& loc) const;
64 
65  virtual bool findRecord(OperationContext* opCtx, const RecordId& loc, RecordData* rd) const;
66 
67  virtual void deleteRecord(OperationContext* opCtx, const RecordId& dl);
68 
70  OperationContext* opCtx, const char* data, int len, Timestamp, bool enforceQuota);
71 
72  virtual Status insertRecordsWithDocWriter(OperationContext* opCtx,
73  const DocWriter* const* docs,
74  const Timestamp*,
75  size_t nDocs,
76  RecordId* idsOut);
77 
78  virtual Status updateRecord(OperationContext* opCtx,
79  const RecordId& oldLocation,
80  const char* data,
81  int len,
82  bool enforceQuota,
83  UpdateNotifier* notifier);
84 
85  virtual bool updateWithDamagesSupported() const;
86 
87  virtual StatusWith<RecordData> updateWithDamages(OperationContext* opCtx,
88  const RecordId& loc,
89  const RecordData& oldRec,
90  const char* damageSource,
91  const mutablebson::DamageVector& damages);
92 
93  std::unique_ptr<SeekableRecordCursor> getCursor(OperationContext* opCtx,
94  bool forward) const final;
95 
96  virtual Status truncate(OperationContext* opCtx);
97 
98  virtual void cappedTruncateAfter(OperationContext* opCtx, RecordId end, bool inclusive);
99 
100  virtual Status validate(OperationContext* opCtx,
101  ValidateCmdLevel level,
102  ValidateAdaptor* adaptor,
103  ValidateResults* results,
104  BSONObjBuilder* output);
105 
106  virtual void appendCustomStats(OperationContext* opCtx,
107  BSONObjBuilder* result,
108  double scale) const;
109 
110  virtual Status touch(OperationContext* opCtx, BSONObjBuilder* output) const;
111 
112  virtual void increaseStorageSize(OperationContext* opCtx, int size, bool enforceQuota);
113 
114  virtual int64_t storageSize(OperationContext* opCtx,
115  BSONObjBuilder* extraInfo = NULL,
116  int infoLevel = 0) const;
117 
118  virtual long long dataSize(OperationContext* opCtx) const {
119  return _data->dataSize;
120  }
121 
122  virtual long long numRecords(OperationContext* opCtx) const {
123  return _data->records.size();
124  }
125 
126  virtual boost::optional<RecordId> oplogStartHack(OperationContext* opCtx,
127  const RecordId& startingPosition) const;
128 
129  void waitForAllEarlierOplogWritesToBeVisible(OperationContext* opCtx) const override {}
130 
131  virtual void updateStatsAfterRepair(OperationContext* opCtx,
132  long long numRecords,
133  long long dataSize) {
134  invariant(_data->records.size() == size_t(numRecords));
136  }
137 
138 protected:
141  EphemeralForTestRecord(int size) : size(size), data(new char[size]) {}
142 
144  return RecordData(data.get(), size);
145  }
146 
147  int size;
148  boost::shared_array<char> data;
149  };
150 
151  virtual const EphemeralForTestRecord* recordFor(const RecordId& loc) const;
152  virtual EphemeralForTestRecord* recordFor(const RecordId& loc);
153 
154 public:
155  //
156  // Not in RecordStore interface
157  //
158 
159  typedef std::map<RecordId, EphemeralForTestRecord> Records;
160 
161  bool isCapped() const {
162  return _isCapped;
163  }
165  _cappedCallback = cb;
166  }
167 
168 private:
169  class InsertChange;
170  class RemoveChange;
171  class TruncateChange;
172 
173  class Cursor;
174  class ReverseCursor;
175 
176  StatusWith<RecordId> extractAndCheckLocForOplog(const char* data, int len) const;
177 
178  RecordId allocateLoc();
179  bool cappedAndNeedDelete_inlock(OperationContext* opCtx) const;
180  void cappedDeleteAsNeeded_inlock(OperationContext* opCtx);
181  void deleteRecord_inlock(OperationContext* opCtx, const RecordId& dl);
182 
183  // TODO figure out a proper solution to metadata
184  const bool _isCapped;
185  const int64_t _cappedMaxSize;
186  const int64_t _cappedMaxDocs;
188 
189  // This is the "persistent" data.
190  struct Data {
191  Data(StringData ns, bool isOplog)
192  : dataSize(0), recordsMutex(), nextId(1), isOplog(isOplog) {}
193 
194  int64_t dataSize;
195  stdx::recursive_mutex recordsMutex;
197  int64_t nextId;
198  const bool isOplog;
199  };
200 
201  Data* const _data;
202 };
203 
204 } // namespace mongo
virtual bool findRecord(OperationContext *opCtx, const RecordId &loc, RecordData *rd) const
Definition: ephemeral_for_test_record_store.cpp:327
ValidateCmdLevel
Definition: record_store.h:93
virtual Status validate(OperationContext *opCtx, ValidateCmdLevel level, ValidateAdaptor *adaptor, ValidateResults *results, BSONObjBuilder *output)
Definition: ephemeral_for_test_record_store.cpp:577
void cappedDeleteAsNeeded_inlock(OperationContext *opCtx)
Definition: ephemeral_for_test_record_store.cpp:367
virtual Status insertRecordsWithDocWriter(OperationContext *opCtx, const DocWriter *const *docs, const Timestamp *, size_t nDocs, RecordId *idsOut)
Inserts nDocs documents into this RecordStore using the DocWriter interface.
Definition: ephemeral_for_test_record_store.cpp:431
Definition: record_store.h:673
A RecordStore that stores all data in-memory.
Definition: ephemeral_for_test_record_store.h:48
virtual void appendCustomStats(OperationContext *opCtx, BSONObjBuilder *result, double scale) const
Definition: ephemeral_for_test_record_store.cpp:605
const int64_t _cappedMaxDocs
Definition: ephemeral_for_test_record_store.h:186
virtual long long dataSize(OperationContext *opCtx) const
The dataSize is an approximation of the sum of the sizes (in bytes) of the documents or entries in th...
Definition: ephemeral_for_test_record_store.h:118
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
bool cappedAndNeedDelete_inlock(OperationContext *opCtx) const
Definition: ephemeral_for_test_record_store.cpp:354
A replacement for the Record class.
Definition: record_data.h:43
CappedCallback * _cappedCallback
Definition: ephemeral_for_test_record_store.h:187
virtual const EphemeralForTestRecord * recordFor(const RecordId &loc) const
Definition: ephemeral_for_test_record_store.cpp:305
const bool isOplog
Definition: ephemeral_for_test_record_store.h:198
virtual Status truncate(OperationContext *opCtx)
removes all Records
Definition: ephemeral_for_test_record_store.cpp:550
EphemeralForTestRecord(int size)
Definition: ephemeral_for_test_record_store.h:141
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
RecordId allocateLoc()
Definition: ephemeral_for_test_record_store.cpp:638
const bool _isCapped
Definition: ephemeral_for_test_record_store.h:184
void deleteRecord_inlock(OperationContext *opCtx, const RecordId &dl)
Definition: ephemeral_for_test_record_store.cpp:346
bool inclusive
Definition: btree_interface.cpp:335
virtual StatusWith< RecordId > insertRecord(OperationContext *opCtx, const char *data, int len, Timestamp, bool enforceQuota)
Definition: ephemeral_for_test_record_store.cpp:400
virtual void deleteRecord(OperationContext *opCtx, const RecordId &dl)
Definition: ephemeral_for_test_record_store.cpp:340
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
Data(StringData ns, bool isOplog)
Definition: ephemeral_for_test_record_store.h:191
virtual int64_t storageSize(OperationContext *opCtx, BSONObjBuilder *extraInfo=NULL, int infoLevel=0) const
Definition: ephemeral_for_test_record_store.cpp:630
stdx::recursive_mutex recordsMutex
Definition: ephemeral_for_test_record_store.h:195
virtual bool updateWithDamagesSupported() const
Definition: ephemeral_for_test_record_store.cpp:505
Records records
Definition: ephemeral_for_test_record_store.h:196
Definition: ephemeral_for_test_record_store.h:190
OperationContext Database StringData CollectionOptions bool const BSONObj &idIndex Status
Definition: database_impl.cpp:955
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.
Definition: ephemeral_for_test_record_store.cpp:644
EphemeralForTestRecord()
Definition: ephemeral_for_test_record_store.h:140
virtual RecordData dataFor(OperationContext *opCtx, const RecordId &loc) const
Get the RecordData at loc, which must exist.
Definition: ephemeral_for_test_record_store.cpp:299
virtual void increaseStorageSize(OperationContext *opCtx, int size, bool enforceQuota)
Definition: ephemeral_for_test_record_store.cpp:623
Definition: index_key_validate.h:40
virtual StatusWith< RecordData > updateWithDamages(OperationContext *opCtx, const RecordId &loc, const RecordData &oldRec, const char *damageSource, const mutablebson::DamageVector &damages)
Updates the record positioned at 'loc' in-place using the deltas described by 'damages'.
Definition: ephemeral_for_test_record_store.cpp:509
EphemeralForTestRecordStore(StringData ns, std::shared_ptr< void > *dataInOut, bool isCapped=false, int64_t cappedMaxSize=-1, int64_t cappedMaxDocs=-1, CappedCallback *cappedCallback=nullptr)
Definition: ephemeral_for_test_record_store.cpp:269
Definition: ephemeral_for_test_record_store.h:139
virtual void updateStatsAfterRepair(OperationContext *opCtx, long long numRecords, long long dataSize)
Called after a repair operation is run with the recomputed numRecords and dataSize.
Definition: ephemeral_for_test_record_store.h:131
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
virtual const std::string & ns() const
Definition: record_store.h:295
void setCappedCallback(CappedCallback *cb)
Definition: ephemeral_for_test_record_store.h:164
int64_t nextId
Definition: ephemeral_for_test_record_store.h:197
const int64_t _cappedMaxSize
Definition: ephemeral_for_test_record_store.h:185
virtual Status touch(OperationContext *opCtx, BSONObjBuilder *output) const
Load all data into cache.
Definition: ephemeral_for_test_record_store.cpp:615
RecordData toRecordData() const
Definition: ephemeral_for_test_record_store.h:143
boost::shared_array< char > data
Definition: ephemeral_for_test_record_store.h:148
virtual long long numRecords(OperationContext *opCtx) const
Total number of record in the RecordStore.
Definition: ephemeral_for_test_record_store.h:122
virtual void cappedTruncateAfter(OperationContext *opCtx, RecordId end, bool inclusive)
Truncate documents newer than the document at 'end' from the capped collection.
Definition: ephemeral_for_test_record_store.cpp:557
Data *const _data
Definition: ephemeral_for_test_record_store.h:201
void waitForAllEarlierOplogWritesToBeVisible(OperationContext *opCtx) const override
Waits for all writes that completed before this call to be visible to forward scans.
Definition: ephemeral_for_test_record_store.h:129
bool isCapped() const
Definition: ephemeral_for_test_record_store.h:161
StatusWith< RecordId > extractAndCheckLocForOplog(const char *data, int len) const
Definition: ephemeral_for_test_record_store.cpp:382
int size
Definition: ephemeral_for_test_record_store.h:147
std::unique_ptr< SeekableRecordCursor > getCursor(OperationContext *opCtx, bool forward) const final
Returns a new cursor over this record store.
Definition: ephemeral_for_test_record_store.cpp:543
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
virtual Status updateRecord(OperationContext *opCtx, const RecordId &oldLocation, const char *data, int len, bool enforceQuota, UpdateNotifier *notifier)
Definition: ephemeral_for_test_record_store.cpp:472
const std::string & getIdent() const override
Definition: ephemeral_for_test_record_store.h:59
virtual const char * name() const
Definition: ephemeral_for_test_record_store.cpp:295
int64_t dataSize
Definition: ephemeral_for_test_record_store.h:194
std::map< RecordId, EphemeralForTestRecord > Records
Definition: ephemeral_for_test_record_store.h:159