client.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // Copyright 2014-present MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <memory>
  16. #include <mongocxx/client_session.hpp>
  17. #include <mongocxx/database.hpp>
  18. #include <mongocxx/options/client.hpp>
  19. #include <mongocxx/options/client_encryption.hpp>
  20. #include <mongocxx/options/client_session.hpp>
  21. #include <mongocxx/read_concern.hpp>
  22. #include <mongocxx/read_preference.hpp>
  23. #include <mongocxx/stdx.hpp>
  24. #include <mongocxx/uri.hpp>
  25. #include <mongocxx/write_concern.hpp>
  26. #include <mongocxx/config/prelude.hpp>
  27. ///
  28. /// Top level namespace for the MongoDB C++ driver.
  29. ///
  30. namespace mongocxx {
  31. MONGOCXX_INLINE_NAMESPACE_BEGIN
  32. class client_session;
  33. ///
  34. /// Class representing a client connection to MongoDB.
  35. ///
  36. /// Acts as a logical gateway for working with databases contained within a MongoDB server.
  37. ///
  38. /// Databases that are created via this client inherit the @c read_concern, @c read_preference, and
  39. /// @c write_concern settings of this client when they are created. The lifetimes of objects created
  40. /// via a client object (databases, collections, cursors, etc...) @b must be a subset of the
  41. /// lifetime of the client that created them.
  42. ///
  43. /// Example:
  44. /// @code
  45. /// mongocxx::client mongo_client{mongocxx::uri{}};
  46. /// mongocxx::client mongo_client{mongocxx::uri{"mongodb://localhost:27017"}};
  47. /// @endcode
  48. ///
  49. /// Note that client is not thread-safe. See
  50. /// https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/thread-safety/ for more details.
  51. class MONGOCXX_API client {
  52. public:
  53. ///
  54. /// Default constructs a new client. The client is not connected and is equivalent to the
  55. /// state of a moved-from client. The only valid actions to take with a default constructed
  56. /// 'client' are to assign to it, or destroy it.
  57. ///
  58. client() noexcept;
  59. ///
  60. /// Creates a new client connection to MongoDB.
  61. ///
  62. /// @param mongodb_uri
  63. /// A MongoDB URI representing the connection parameters
  64. /// @param options
  65. /// Additional options that cannot be specified via the mongodb_uri
  66. ///
  67. /// @throws mongocxx::exception if invalid options are provided
  68. /// (whether from the URI or provided client options).
  69. ///
  70. client(const class uri& mongodb_uri, const options::client& options = options::client());
  71. ///
  72. /// Move constructs a client.
  73. ///
  74. client(client&&) noexcept;
  75. ///
  76. /// Move assigns a client.
  77. ///
  78. client& operator=(client&&) noexcept;
  79. ///
  80. /// Destroys a client.
  81. ///
  82. ~client();
  83. ///
  84. /// Returns true if the client is valid, meaning it was not default constructed
  85. /// or moved from.
  86. ///
  87. explicit operator bool() const noexcept;
  88. ///
  89. /// Sets the read concern for this client.
  90. ///
  91. /// Modifications at this level do not affect existing database instances that have been
  92. /// created by this client but do affect new ones as databases inherit the @c read_concern
  93. /// settings of their parent upon instantiation.
  94. ///
  95. /// @deprecated
  96. /// This method is deprecated. Read concerns should be set either in the URI or directly on
  97. /// database or collection objects.
  98. ///
  99. /// @param rc
  100. /// The new @c read_concern
  101. ///
  102. /// @see https://docs.mongodb.com/master/reference/read-concern/
  103. ///
  104. MONGOCXX_DEPRECATED void read_concern(class read_concern rc);
  105. void read_concern_deprecated(class read_concern rc);
  106. ///
  107. /// Returns the current read concern for this client.
  108. ///
  109. /// @return The current @c read_concern
  110. ///
  111. class read_concern read_concern() const;
  112. ///
  113. /// Sets the read preference for this client.
  114. ///
  115. /// Modifications at this level do not affect existing database instances that have been
  116. /// created by this client but do affect new ones as databases inherit the @c read_preference
  117. /// settings of their parent upon instantiation.
  118. ///
  119. /// @deprecated
  120. /// This method is deprecated. Read preferences should be set either in the URI or directly on
  121. /// database or collection objects.
  122. ///
  123. /// @param rp
  124. /// The new @c read_preference
  125. ///
  126. /// @see https://docs.mongodb.com/master/core/read-preference/
  127. ///
  128. MONGOCXX_DEPRECATED void read_preference(class read_preference rp);
  129. void read_preference_deprecated(class read_preference rp);
  130. ///
  131. /// Returns the current read preference for this client.
  132. ///
  133. /// @return The current @c read_preference
  134. ///
  135. /// @see https://docs.mongodb.com/master/core/read-preference/
  136. ///
  137. class read_preference read_preference() const;
  138. ///
  139. /// Returns the current uri for this client.
  140. ///
  141. /// @return The @c uri that this client was created with.
  142. ///
  143. class uri uri() const;
  144. ///
  145. /// Sets the write concern for this client.
  146. ///
  147. /// @note Modifications at this level do not affect existing databases or collection instances
  148. /// that have come from this client but do affect new ones as databases will receive a copy of
  149. /// this client's @c write_concern upon instantiation.
  150. ///
  151. /// @deprecated
  152. /// This method is deprecated. Write concerns should be set either in the URI or directly on
  153. /// database or collection objects.
  154. ///
  155. /// @param wc
  156. /// The new write concern
  157. ///
  158. MONGOCXX_DEPRECATED void write_concern(class write_concern wc);
  159. void write_concern_deprecated(class write_concern wc);
  160. ///
  161. /// Returns the current write concern for this client.
  162. ///
  163. /// @return the current @c write_concern
  164. class write_concern write_concern() const;
  165. ///
  166. /// Obtains a database that represents a logical grouping of collections on a MongoDB server.
  167. ///
  168. /// @note A database cannot be obtained from a temporary client object.
  169. ///
  170. /// @param name
  171. /// The name of the database to get
  172. ///
  173. /// @return The database
  174. ///
  175. class database database(bsoncxx::string::view_or_value name) const&;
  176. class database database(bsoncxx::string::view_or_value name) const&& = delete;
  177. ///
  178. /// Allows the syntax @c client["db_name"] as a convenient shorthand for the client::database()
  179. /// method by implementing the array subscript operator.
  180. ///
  181. /// @note A database cannot be obtained from a temporary client object.
  182. ///
  183. /// @param name
  184. /// The name of the database.
  185. ///
  186. /// @return Client side representation of a server side database
  187. ///
  188. MONGOCXX_INLINE class database operator[](bsoncxx::string::view_or_value name) const&;
  189. MONGOCXX_INLINE class database operator[](bsoncxx::string::view_or_value name) const&& = delete;
  190. ///
  191. /// @{
  192. ///
  193. /// Enumerates the databases in the client.
  194. ///
  195. /// @return A mongocxx::cursor containing a BSON document for each
  196. /// database. Each document contains a name field with the database
  197. /// name, a sizeOnDisk field with the total size of the database file on
  198. /// disk in bytes, and an empty field specifying whether the database
  199. /// has any data.
  200. ///
  201. /// @throws mongocxx::operation_exception if the underlying 'listDatabases' command fails.
  202. ///
  203. /// @see https://docs.mongodb.com/master/reference/command/listDatabases
  204. ///
  205. cursor list_databases() const;
  206. ///
  207. /// Enumerates the databases in the client.
  208. ///
  209. /// @param session
  210. /// The mongocxx::client_session with which to perform the aggregation.
  211. ///
  212. /// @return A mongocxx::cursor containing a BSON document for each
  213. /// database. Each document contains a name field with the database
  214. /// name, a sizeOnDisk field with the total size of the database file on
  215. /// disk in bytes, and an empty field specifying whether the database
  216. /// has any data.
  217. ///
  218. /// @throws mongocxx::operation_exception if the underlying 'listDatabases' command fails.
  219. ///
  220. /// @see https://docs.mongodb.com/master/reference/command/listDatabases
  221. ///
  222. cursor list_databases(const client_session& session) const;
  223. ///
  224. /// Enumerates the databases in the client.
  225. ///
  226. /// @param filter
  227. /// The filter that documents must match in order to be listed.
  228. ///
  229. /// @return A mongocxx::cursor containing a BSON document for each
  230. /// database. Each document contains a name field with the database
  231. /// name, a sizeOnDisk field with the total size of the database file on
  232. /// disk in bytes, and an empty field specifying whether the database
  233. /// has any data.
  234. ///
  235. /// @throws mongocxx::operation_exception if the underlying 'listDatabases' command fails.
  236. ///
  237. /// @see https://docs.mongodb.com/master/reference/command/listDatabases
  238. ///
  239. cursor list_databases(const bsoncxx::document::view_or_value filter) const;
  240. ///
  241. /// Queries the MongoDB server for a list of known databases.
  242. ///
  243. /// @param filter
  244. /// An optional query expression to filter the returned database names.
  245. ///
  246. /// @return std::vector<std::string> containing the database names.
  247. ///
  248. /// @throws mongocxx::operation_exception if the underlying 'listDatabases'
  249. /// command fails.
  250. ///
  251. /// @see https://docs.mongodb.com/master/reference/command/listDatabases
  252. ///
  253. std::vector<std::string> list_database_names(
  254. const bsoncxx::document::view_or_value filter = {}) const;
  255. ///
  256. /// @}
  257. ///
  258. ///
  259. /// Create a client session for a sequence of operations.
  260. ///
  261. /// @return A client_session object. See `mongocxx::client_session` for more information.
  262. ///
  263. /// @throws mongocxx::operation_exception if the driver is not built with crypto support, if
  264. /// options is misconfigured, or if the session is configured with options that the server does
  265. /// not support.
  266. ///
  267. client_session start_session(const options::client_session& options = {});
  268. ///
  269. /// @{
  270. ///
  271. /// Gets a change stream on this client with an empty pipeline.
  272. /// Change streams are only supported with a "majority" read concern or no read concern.
  273. ///
  274. /// @param options
  275. /// The options to use when creating the change stream.
  276. ///
  277. /// @return
  278. /// A change stream on this client.
  279. ///
  280. /// @see https://docs.mongodb.com/manual/changeStreams/
  281. ///
  282. change_stream watch(const options::change_stream& options = {});
  283. ///
  284. /// @param session
  285. /// The mongocxx::client_session with which to perform the watch operation.
  286. /// @param options
  287. /// The options to use when creating the change stream.
  288. ///
  289. /// @return
  290. /// A change stream on this client.
  291. ///
  292. /// @see https://docs.mongodb.com/manual/changeStreams/
  293. ///
  294. change_stream watch(const client_session& session, const options::change_stream& options = {});
  295. ///
  296. /// Gets a change stream on this client.
  297. /// Change streams are only supported with a "majority" read concern or no read concern.
  298. ///
  299. /// @param pipe
  300. /// The aggregation pipeline to be used on the change notifications.
  301. /// Only a subset of pipeline operations are supported for change streams. For more
  302. /// information see the change streams documentation.
  303. /// @param options
  304. /// The options to use when creating the change stream.
  305. ///
  306. /// @return
  307. /// A change stream on this client.
  308. ///
  309. /// @see https://docs.mongodb.com/manual/changeStreams/
  310. ///
  311. change_stream watch(const pipeline& pipe, const options::change_stream& options = {});
  312. ///
  313. /// Gets a change stream on this client.
  314. ///
  315. /// @param session
  316. /// The mongocxx::client_session with which to perform the watch operation.
  317. /// @param pipe
  318. /// The aggregation pipeline to be used on the change notifications.
  319. /// @param options
  320. /// The options to use when creating the change stream.
  321. ///
  322. /// @return
  323. /// A change stream on this client.
  324. ///
  325. /// @see https://docs.mongodb.com/manual/changeStreams/
  326. ///
  327. change_stream watch(const client_session& session,
  328. const pipeline& pipe,
  329. const options::change_stream& options = {});
  330. ///
  331. /// @}
  332. ///
  333. ///
  334. /// Prevents resource cleanup in the child process from interfering
  335. /// with the parent process after forking.
  336. ///
  337. /// Clients should not be reused after forking. Call this method in the
  338. /// child after forking to safely destroy the client. This method should
  339. /// not be used with multi-threaded clients.
  340. ///
  341. /// This method causes the client to clear its session pool without sending
  342. /// endSessions. It also increments an internal generation counter on the
  343. /// given client. After this method is called, cursors from
  344. /// previous generations will not issue a killCursors command when
  345. /// they are destroyed. Client sessions from previous generations
  346. /// cannot be used and should be destroyed.
  347. ///
  348. void reset();
  349. private:
  350. friend class collection;
  351. friend class database;
  352. friend class pool;
  353. friend class client_session;
  354. friend class options::auto_encryption;
  355. friend class options::client_encryption;
  356. MONGOCXX_PRIVATE explicit client(void* implementation);
  357. MONGOCXX_PRIVATE change_stream _watch(const client_session* session,
  358. const pipeline& pipe,
  359. const options::change_stream& options);
  360. class MONGOCXX_PRIVATE impl;
  361. MONGOCXX_PRIVATE impl& _get_impl();
  362. MONGOCXX_PRIVATE const impl& _get_impl() const;
  363. std::unique_ptr<impl> _impl;
  364. };
  365. MONGOCXX_INLINE database client::operator[](bsoncxx::string::view_or_value name) const& {
  366. return database(name);
  367. }
  368. MONGOCXX_INLINE_NAMESPACE_END
  369. } // namespace mongocxx
  370. #include <mongocxx/config/postlude.hpp>