userver: userver/storages/mongo/pool.hpp Source File
Loading...
Searching...
No Matches
pool.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/pool.hpp
4/// @brief @copybrief storages::mongo::Pool
5
6#include <memory>
7#include <string>
8#include <vector>
9
10#include <userver/clients/dns/resolver_fwd.hpp>
11#include <userver/dynamic_config/fwd.hpp>
12#include <userver/storages/mongo/collection.hpp>
13#include <userver/storages/mongo/pool_config.hpp>
14#include <userver/storages/mongo/transaction.hpp>
15#include <userver/utils/statistics/fwd.hpp>
16#include <userver/utils/zstring_view.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace storages::mongo {
21
22namespace impl {
23class PoolImpl;
24} // namespace impl
25
26/// @ingroup userver_clients
27///
28/// @brief MongoDB client pool.
29///
30/// Use constructor only for tests, in production the pool should be retrieved
31/// from @ref userver_components "the components" via
32/// components::Mongo::GetPool() or components::MultiMongo::GetPool().
33///
34/// ## Example usage:
35///
36/// @snippet storages/mongo/collection_mongotest.hpp Sample Mongo usage
37class Pool {
38public:
39 Pool(Pool&&) noexcept;
40 Pool& operator=(Pool&&) noexcept;
41 ~Pool();
42
43 /// Checks whether a collection exists
44 bool HasCollection(utils::zstring_view name) const;
45
46 /// Returns a handle for the specified collection
47 Collection GetCollection(std::string name) const;
48
49 /// Drops the associated database if it exists. New modifications of
50 /// collections will attempt to re-create the database automatically.
52
53 /// Get a list of all the collection names in the associated database
54 std::vector<std::string> ListCollectionNames() const;
55
56 /// @throws storages::mongo::MongoException if failed to connect to the mongo server.
57 void Ping();
58
59 /// @brief Begin a new transaction
60 /// @return Transaction handle for executing operations within transaction context
61 /// @throws MongoException if transaction cannot be started
63
64 /// @cond
65 // For internal use only
66 Pool(
67 std::string id,
68 const std::string& uri,
69 const PoolConfig& pool_config,
70 clients::dns::Resolver* dns_resolver,
71 dynamic_config::Source config_source
72 );
73
74 // Writes pool statistics
75 friend void DumpMetric(utils::statistics::Writer& writer, const Pool& pool);
76
77 // Sets new dynamic pool settings
78 void SetPoolSettings(const PoolSettings& pool_settings);
79
80 void SetConnectionString(const std::string& connection_string);
81 /// @endcond
82
83private:
84 std::shared_ptr<impl::PoolImpl> impl_;
85};
86
87using PoolPtr = std::shared_ptr<Pool>;
88
89} // namespace storages::mongo
90
91USERVER_NAMESPACE_END