Storage Engine API
sorted_data_interface.h
Go to the documentation of this file.
1 
29 #include <boost/optional/optional.hpp>
30 #include <boost/optional/optional_io.hpp>
31 #include <memory>
32 
33 #include "mongo/db/jsobj.h"
34 #include "mongo/db/operation_context.h"
35 #include "mongo/db/record_id.h"
37 
38 #pragma once
39 
40 namespace mongo {
41 
42 class BSONObjBuilder;
43 class BucketDeletionNotification;
44 class SortedDataBuilderInterface;
45 struct ValidateResults;
46 
65 public:
66  virtual ~SortedDataInterface() {}
67 
68  //
69  // Data changes
70  //
71 
84  virtual SortedDataBuilderInterface* getBulkBuilder(OperationContext* opCtx,
85  bool dupsAllowed) = 0;
86 
99  virtual Status insert(OperationContext* opCtx,
100  const BSONObj& key,
101  const RecordId& loc,
102  bool dupsAllowed) = 0;
103 
111  virtual void unindex(OperationContext* opCtx,
112  const BSONObj& key,
113  const RecordId& loc,
114  bool dupsAllowed) = 0;
115 
124  virtual Status dupKeyCheck(OperationContext* opCtx,
125  const BSONObj& key,
126  const RecordId& loc) = 0;
127 
132  virtual Status compact(OperationContext* opCtx) {
133  return Status::OK();
134  }
135 
136  //
137  // Information about the tree
138  //
139 
143  virtual void fullValidate(OperationContext* opCtx,
144  long long* numKeysOut,
145  ValidateResults* fullResults) const = 0;
146 
147  virtual bool appendCustomStats(OperationContext* opCtx,
148  BSONObjBuilder* output,
149  double scale) const = 0;
150 
151 
159  virtual long long getSpaceUsedBytes(OperationContext* opCtx) const = 0;
160 
164  virtual bool isEmpty(OperationContext* opCtx) = 0;
165 
174  virtual Status touch(OperationContext* opCtx) const {
175  return Status(ErrorCodes::CommandNotSupported,
176  "this storage engine does not support touch");
177  }
178 
185  virtual long long numEntries(OperationContext* opCtx) const {
186  long long x = -1;
187  fullValidate(opCtx, &x, NULL);
188  return x;
189  }
190 
223  class Cursor {
224  public:
235  // Only usable part of the return is whether it is engaged or not.
237  // Key must be filled in.
238  kWantKey = 1,
239  // Loc must be fulled in.
240  kWantLoc = 2,
241  // Both must be returned.
243  };
244 
245  virtual ~Cursor() = default;
246 
247 
257  virtual void setEndPosition(const BSONObj& key, bool inclusive) = 0;
258 
263  virtual boost::optional<IndexKeyEntry> next(RequestedInfo parts = kKeyAndLoc) = 0;
264 
265  //
266  // Seeking
267  //
268 
277  virtual boost::optional<IndexKeyEntry> seek(const BSONObj& key,
278  bool inclusive,
279  RequestedInfo parts = kKeyAndLoc) = 0;
280 
287  virtual boost::optional<IndexKeyEntry> seek(const IndexSeekPoint& seekPoint,
288  RequestedInfo parts = kKeyAndLoc) = 0;
289 
295  virtual boost::optional<IndexKeyEntry> seekExact(const BSONObj& key,
296  RequestedInfo parts = kKeyAndLoc) {
297  auto kv = seek(key, true, kKeyAndLoc);
298  if (kv && kv->key.woCompare(key, BSONObj(), /*considerFieldNames*/ false) == 0)
299  return kv;
300  return {};
301  }
302 
303  //
304  // Saving and restoring state
305  //
306 
314  virtual void save() = 0;
315 
326  virtual void saveUnpositioned() {
327  save();
328  }
329 
338  virtual void restore() = 0;
339 
347  virtual void detachFromOperationContext() = 0;
348 
355  virtual void reattachToOperationContext(OperationContext* opCtx) = 0;
356  };
357 
363  virtual std::unique_ptr<Cursor> newCursor(OperationContext* opCtx,
364  bool isForward = true) const = 0;
365 
380  virtual std::unique_ptr<Cursor> newRandomCursor(OperationContext* opCtx) const {
381  return {};
382  }
383 
384  //
385  // Index creation
386  //
387 
388  virtual Status initAsEmpty(OperationContext* opCtx) = 0;
389 };
390 
395 public:
397 
404  virtual Status addKey(const BSONObj& key, const RecordId& loc) = 0;
405 
415  virtual void commit(bool mayInterrupt) {}
416 };
417 
418 } // namespace mongo
virtual std::unique_ptr< Cursor > newCursor(OperationContext *opCtx, bool isForward=true) const =0
Returns an unpositioned cursor over &#39;this&#39; index.
virtual long long getSpaceUsedBytes(OperationContext *opCtx) const =0
Return the number of bytes consumed by &#39;this&#39; index.
virtual void restore()=0
Recovers from potential state changes in underlying data.
virtual ~SortedDataBuilderInterface()
Definition: sorted_data_interface.h:396
Definition: record_store.h:671
virtual bool appendCustomStats(OperationContext *opCtx, BSONObjBuilder *output, double scale) const =0
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
virtual void unindex(OperationContext *opCtx, const BSONObj &key, const RecordId &loc, bool dupsAllowed)=0
Remove the entry from the index with the specified key and RecordId.
virtual boost::optional< IndexKeyEntry > next(RequestedInfo parts=kKeyAndLoc)=0
Moves forward and returns the new data or boost::none if there is no more data.
Navigates over the sorted data.
Definition: sorted_data_interface.h:223
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
BSONObj key
Definition: btree_interface.cpp:334
bool inclusive
Definition: btree_interface.cpp:335
virtual SortedDataBuilderInterface * getBulkBuilder(OperationContext *opCtx, bool dupsAllowed)=0
Return a bulk builder for &#39;this&#39; index.
RequestedInfo
Tells methods that return an IndexKeyEntry what part of the data the caller is interested in...
Definition: sorted_data_interface.h:234
virtual Status insert(OperationContext *opCtx, const BSONObj &key, const RecordId &loc, bool dupsAllowed)=0
Insert an entry into the index with the specified key and RecordId.
virtual boost::optional< IndexKeyEntry > seekExact(const BSONObj &key, RequestedInfo parts=kKeyAndLoc)
Seeks to a key with a hint to the implementation that you only want exact matches.
Definition: sorted_data_interface.h:295
virtual void detachFromOperationContext()=0
Detaches from the OperationContext and releases any storage-engine state.
A version-hiding wrapper around the bulk builder for the Btree.
Definition: sorted_data_interface.h:394
virtual ~SortedDataInterface()
Definition: sorted_data_interface.h:66
virtual void commit(bool mayInterrupt)
Do any necessary work to finish building the tree.
Definition: sorted_data_interface.h:415
virtual std::unique_ptr< Cursor > newRandomCursor(OperationContext *opCtx) const
Constructs a cursor over an index that returns entries in a randomized order, and allows storage engi...
Definition: sorted_data_interface.h:380
virtual void saveUnpositioned()
Prepares for state changes in underlying data without necessarily saving the current state...
Definition: sorted_data_interface.h:326
Definition: sorted_data_interface.h:238
Definition: sorted_data_interface.h:240
virtual Status initAsEmpty(OperationContext *opCtx)=0
virtual void reattachToOperationContext(OperationContext *opCtx)=0
Reattaches to the OperationContext and reacquires any storage-engine state.
virtual Status dupKeyCheck(OperationContext *opCtx, const BSONObj &key, const RecordId &loc)=0
Return ErrorCodes::DuplicateKey if &#39;key&#39; already exists in &#39;this&#39; index at a RecordId other than &#39;loc...
virtual bool isEmpty(OperationContext *opCtx)=0
Return true if &#39;this&#39; index is empty, and false otherwise.
Definition: sorted_data_interface.h:242
virtual Status touch(OperationContext *opCtx) const
Attempt to bring the entirety of &#39;this&#39; index into memory.
Definition: sorted_data_interface.h:174
virtual boost::optional< IndexKeyEntry > seek(const BSONObj &key, bool inclusive, RequestedInfo parts=kKeyAndLoc)=0
Seeks to the provided key and returns current position.
Describes a query that can be compared against an IndexKeyEntry in a way that allows expressing exclu...
Definition: index_entry_comparison.h:104
virtual long long numEntries(OperationContext *opCtx) const
Return the number of entries in &#39;this&#39; index.
Definition: sorted_data_interface.h:185
virtual Status compact(OperationContext *opCtx)
Attempt to reduce the storage space used by this index via compaction.
Definition: sorted_data_interface.h:132
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
Definition: sorted_data_interface.h:236
virtual void save()=0
Prepares for state changes in underlying data in a way that allows the cursor&#39;s current position to b...
virtual void setEndPosition(const BSONObj &key, bool inclusive)=0
Sets the position to stop scanning.
virtual void fullValidate(OperationContext *opCtx, long long *numKeysOut, ValidateResults *fullResults) const =0
TODO: expose full set of args for testing?
This interface is a work in progress.
Definition: sorted_data_interface.h:64