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/utils/statistics/fwd.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace storages::mongo {
19
20namespace impl {
21class PoolImpl;
22} // namespace impl
23
24/// @ingroup userver_clients
25///
26/// @brief MongoDB client pool.
27///
28/// Use constructor only for tests, in production the pool should be retrieved
29/// from @ref userver_components "the components" via
30/// components::Mongo::GetPool() or components::MultiMongo::GetPool().
31///
32/// ## Example usage:
33///
34/// @snippet storages/mongo/collection_mongotest.hpp Sample Mongo usage
35class Pool {
36public:
37 Pool(Pool&&) noexcept;
38 Pool& operator=(Pool&&) noexcept;
39 ~Pool();
40
41 /// Checks whether a collection exists
42 bool HasCollection(const std::string& name) const;
43
44 /// Returns a handle for the specified collection
45 Collection GetCollection(std::string name) const;
46
47 /// Drops the associated database if it exists. New modifications of
48 /// collections will attempt to re-create the database automatically.
50
51 /// Get a list of all the collection names in the associated database
53
54 /// @throws storages::mongo::MongoException if failed to connect to the mongo server.
55 void Ping();
56
57 /// @cond
58 // For internal use only
59 Pool(
60 std::string id,
61 const std::string& uri,
62 const PoolConfig& pool_config,
63 clients::dns::Resolver* dns_resolver,
64 dynamic_config::Source config_source
65 );
66
67 // Writes pool statistics
68 friend void DumpMetric(utils::statistics::Writer& writer, const Pool& pool);
69
70 // Sets new dynamic pool settings
71 void SetPoolSettings(const PoolSettings& pool_settings);
72
73 void SetConnectionString(const std::string& connection_string);
74 /// @endcond
75
76private:
77 std::shared_ptr<impl::PoolImpl> impl_;
78};
79
80using PoolPtr = std::shared_ptr<Pool>;
81
82} // namespace storages::mongo
83
84USERVER_NAMESPACE_END