Storage Engine API
database_holder.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <set>
32 #include <string>
33 
34 #include "mongo/base/shim.h"
35 #include "mongo/base/string_data.h"
36 #include "mongo/db/namespace_string.h"
37 #include "mongo/stdx/functional.h"
38 #include "mongo/stdx/mutex.h"
39 #include "mongo/util/concurrency/mutex.h"
40 #include "mongo/util/string_map.h"
41 
42 namespace mongo {
43 class Database;
44 class OperationContext;
45 
50 public:
51  class Impl {
52  public:
53  virtual ~Impl() = 0;
54 
55  virtual Database* get(OperationContext* opCtx, StringData ns) const = 0;
56 
57  virtual Database* openDb(OperationContext* opCtx, StringData ns, bool* justCreated) = 0;
58 
59  virtual void close(OperationContext* opCtx, StringData ns, const std::string& reason) = 0;
60 
61  virtual void closeAll(OperationContext* opCtx, const std::string& reason) = 0;
62 
63  virtual std::set<std::string> getNamesWithConflictingCasing(StringData name) = 0;
64  };
65 
66 public:
67  static MONGO_DECLARE_SHIM(()->DatabaseHolder&) getDatabaseHolder;
68 
69  static MONGO_DECLARE_SHIM((PrivateTo<DatabaseHolder>)->std::unique_ptr<Impl>) makeImpl;
70 
71  inline ~DatabaseHolder() = default;
72 
73  inline explicit DatabaseHolder() : _pimpl(makeImpl(PrivateCall<DatabaseHolder>{})) {}
74 
79  inline Database* get(OperationContext* const opCtx, const StringData ns) const {
80  return this->_impl().get(opCtx, ns);
81  }
82 
90  inline Database* openDb(OperationContext* const opCtx,
91  const StringData ns,
92  bool* const justCreated = nullptr) {
93  return this->_impl().openDb(opCtx, ns, justCreated);
94  }
95 
100  inline void close(OperationContext* const opCtx,
101  const StringData ns,
102  const std::string& reason) {
103  return this->_impl().close(opCtx, ns, reason);
104  }
105 
112  inline void closeAll(OperationContext* const opCtx, const std::string& reason) {
113  this->_impl().closeAll(opCtx, reason);
114  }
115 
119  inline std::set<std::string> getNamesWithConflictingCasing(const StringData name) {
120  return this->_impl().getNamesWithConflictingCasing(name);
121  }
122 
123 private:
124  // This structure exists to give us a customization point to decide how to force users of this
125  // class to depend upon the corresponding `database_holder.cpp` Translation Unit (TU). All
126  // public forwarding functions call `_impl(), and `_impl` creates an instance of this structure.
127  struct TUHook {
128  static void hook() noexcept;
129 
130  explicit inline TUHook() noexcept {
131  if (kDebugBuild)
132  this->hook();
133  }
134  };
135 
136  inline const Impl& _impl() const {
137  TUHook{};
138  return *this->_pimpl;
139  }
140 
141  inline Impl& _impl() {
142  TUHook{};
143  return *this->_pimpl;
144  }
145 
146  std::unique_ptr<Impl> _pimpl;
147 };
148 } // namespace mongo
virtual Database * get(OperationContext *opCtx, StringData ns) const =0
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
DatabaseHolder()
Definition: database_holder.h:73
const Impl & _impl() const
Definition: database_holder.h:136
TUHook() noexcept
Definition: database_holder.h:130
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
Definition: database_holder.h:127
virtual void closeAll(OperationContext *opCtx, const std::string &reason)=0
Database * openDb(OperationContext *const opCtx, const StringData ns, bool *const justCreated=nullptr)
Retrieves a database reference if it is already opened, or opens it if it hasn&#39;t been opened/created ...
Definition: database_holder.h:90
void close(OperationContext *const opCtx, const StringData ns, const std::string &reason)
Closes the specified database.
Definition: database_holder.h:100
Impl & _impl()
Definition: database_holder.h:141
virtual void close(OperationContext *opCtx, StringData ns, const std::string &reason)=0
Definition: database_holder.h:51
static MONGO_DECLARE_SHIM(() ->DatabaseHolder &) getDatabaseHolder
void closeAll(OperationContext *const opCtx, const std::string &reason)
Closes all opened databases.
Definition: database_holder.h:112
std::unique_ptr< Impl > _pimpl
Definition: database_holder.h:146
Represents a logical database containing Collections.
Definition: database.h:57
std::set< std::string > getNamesWithConflictingCasing(const StringData name)
Returns the set of existing database names that differ only in casing.
Definition: database_holder.h:119
Database *const OperationContext *const const StringData name
Definition: database_impl.cpp:82
virtual Database * openDb(OperationContext *opCtx, StringData ns, bool *justCreated)=0
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
virtual std::set< std::string > getNamesWithConflictingCasing(StringData name)=0
Registry of opened databases.
Definition: database_holder.h:49