Storage Engine API
index_create.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <memory>
32 #include <set>
33 #include <string>
34 #include <vector>
35 
36 #include "mongo/base/disallow_copying.h"
37 #include "mongo/base/status.h"
40 #include "mongo/db/index/index_access_method.h"
41 #include "mongo/db/record_id.h"
42 #include "mongo/stdx/functional.h"
43 
44 namespace mongo {
45 class BackgroundOperation;
46 class BSONObj;
47 class Collection;
48 class OperationContext;
49 
62 public:
63  class Impl {
64  public:
65  virtual ~Impl() = 0;
66 
67  virtual void allowBackgroundBuilding() = 0;
68 
69  virtual void allowInterruption() = 0;
70 
71  virtual void ignoreUniqueConstraint() = 0;
72 
73  virtual void removeExistingIndexes(std::vector<BSONObj>* specs) const = 0;
74 
75  virtual StatusWith<std::vector<BSONObj>> init(const std::vector<BSONObj>& specs) = 0;
76 
77  virtual StatusWith<std::vector<BSONObj>> init(const BSONObj& spec) = 0;
78 
79  virtual Status insertAllDocumentsInCollection(std::set<RecordId>* dupsOut = NULL) = 0;
80 
81  virtual Status insert(const BSONObj& wholeDocument, const RecordId& loc) = 0;
82 
83  virtual Status doneInserting(std::set<RecordId>* dupsOut = NULL) = 0;
84 
85  virtual void commit(stdx::function<void(const BSONObj& spec)> onCreateFn) = 0;
86 
87  virtual void abortWithoutCleanup() = 0;
88 
89  virtual bool getBuildInBackground() const = 0;
90  };
91 
92 private:
93  std::unique_ptr<Impl> _pimpl;
94 
95  // This structure exists to give us a customization point to decide how to force users of this
96  // class to depend upon the corresponding `index_create.cpp` Translation Unit (TU). All public
97  // forwarding functions call `_impl(), and `_impl` creates an instance of this structure.
98  struct TUHook {
99  static void hook() noexcept;
100 
101  explicit inline TUHook() noexcept {
102  if (kDebugBuild)
103  this->hook();
104  }
105  };
106 
107  inline const Impl& _impl() const {
108  TUHook{};
109  return *this->_pimpl;
110  }
111 
112  inline Impl& _impl() {
113  TUHook{};
114  return *this->_pimpl;
115  }
116 
117 public:
118  static MONGO_DECLARE_SHIM((OperationContext * opCtx,
120  PrivateTo<MultiIndexBlock>)
121  ->std::unique_ptr<Impl>) makeImpl;
122 
123  inline ~MultiIndexBlock() = default;
124 
128  inline explicit MultiIndexBlock(OperationContext* const opCtx, Collection* const collection)
129  : _pimpl(makeImpl(opCtx, collection, PrivateCall<MultiIndexBlock>{})) {}
130 
139  inline void allowBackgroundBuilding() {
140  return this->_impl().allowBackgroundBuilding();
141  }
142 
147  inline void allowInterruption() {
148  return this->_impl().allowInterruption();
149  }
150 
158  inline void ignoreUniqueConstraint() {
159  return this->_impl().ignoreUniqueConstraint();
160  }
161 
166  inline void removeExistingIndexes(std::vector<BSONObj>* const specs) const {
167  return this->_impl().removeExistingIndexes(specs);
168  }
169 
178  inline StatusWith<std::vector<BSONObj>> init(const std::vector<BSONObj>& specs) {
179  return this->_impl().init(specs);
180  }
181 
182  inline StatusWith<std::vector<BSONObj>> init(const BSONObj& spec) {
183  return this->_impl().init(spec);
184  }
185 
200  inline Status insertAllDocumentsInCollection(std::set<RecordId>* const dupsOut = nullptr) {
201  return this->_impl().insertAllDocumentsInCollection(dupsOut);
202  }
203 
211  inline Status insert(const BSONObj& wholeDocument, const RecordId& loc) {
212  return this->_impl().insert(wholeDocument, loc);
213  }
214 
227  inline Status doneInserting(std::set<RecordId>* const dupsOut = nullptr) {
228  return this->_impl().doneInserting(dupsOut);
229  }
230 
242  inline void commit(stdx::function<void(const BSONObj& spec)> onCreateFn = nullptr) {
243  return this->_impl().commit(onCreateFn);
244  }
245 
260  inline void abortWithoutCleanup() {
261  return this->_impl().abortWithoutCleanup();
262  }
263 
264  inline bool getBuildInBackground() const {
265  return this->_impl().getBuildInBackground();
266  }
267 };
268 } // namespace mongo
const Impl & _impl() const
Definition: index_create.h:107
void removeExistingIndexes(std::vector< BSONObj > *const specs) const
Removes pre-existing indexes from &#39;specs&#39;.
Definition: index_create.h:166
virtual Status doneInserting(std::set< RecordId > *dupsOut=NULL)=0
Status doneInserting(std::set< RecordId > *const dupsOut=nullptr)
Call this after the last insert().
Definition: index_create.h:227
void allowBackgroundBuilding()
By default we ignore the &#39;background&#39; flag in specs when building an index.
Definition: index_create.h:139
Status insert(const BSONObj &wholeDocument, const RecordId &loc)
Call this after init() for each document in the collection.
Definition: index_create.h:211
void commit(stdx::function< void(const BSONObj &spec)> onCreateFn=nullptr)
Marks the index ready for use.
Definition: index_create.h:242
virtual void allowBackgroundBuilding()=0
virtual void allowInterruption()=0
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
virtual bool getBuildInBackground() const =0
Collection *const collection
Definition: collection_info_cache_impl.cpp:53
TUHook() noexcept
Definition: index_create.h:101
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
void abortWithoutCleanup()
May be called at any time after construction but before a successful commit().
Definition: index_create.h:260
Definition: index_create.h:98
virtual StatusWith< std::vector< BSONObj > > init(const std::vector< BSONObj > &specs)=0
this is NOT safe through a yield right now.
Definition: collection.h:160
Impl & _impl()
Definition: index_create.h:112
virtual Status insert(const BSONObj &wholeDocument, const RecordId &loc)=0
StatusWith< std::vector< BSONObj > > init(const std::vector< BSONObj > &specs)
Prepares the index(es) for building and returns the canonicalized form of the requested index specifi...
Definition: index_create.h:178
Definition: index_create.h:63
void ignoreUniqueConstraint()
By default we enforce the &#39;unique&#39; flag in specs when building an index by failing.
Definition: index_create.h:158
Definition: index_key_validate.h:40
virtual void ignoreUniqueConstraint()=0
std::unique_ptr< Impl > _pimpl
Definition: index_create.h:93
virtual Status insertAllDocumentsInCollection(std::set< RecordId > *dupsOut=NULL)=0
MultiIndexBlock(OperationContext *const opCtx, Collection *const collection)
Neither pointer is owned.
Definition: index_create.h:128
Builds one or more indexes.
Definition: index_create.h:61
virtual void abortWithoutCleanup()=0
virtual void commit(stdx::function< void(const BSONObj &spec)> onCreateFn)=0
void allowInterruption()
Call this before init() to allow the index build to be interrupted.
Definition: index_create.h:147
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
Status insertAllDocumentsInCollection(std::set< RecordId > *const dupsOut=nullptr)
Inserts all documents in the Collection into the indexes and logs with timing info.
Definition: index_create.h:200
bool getBuildInBackground() const
Definition: index_create.h:264
StatusWith< std::vector< BSONObj > > init(const BSONObj &spec)
Definition: index_create.h:182
static MONGO_DECLARE_SHIM((OperationContext *opCtx, Collection *collection, PrivateTo< MultiIndexBlock >) ->std::unique_ptr< Impl >) makeImpl
virtual void removeExistingIndexes(std::vector< BSONObj > *specs) const =0