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 /// @cond
38
39 /// Client pool constructor, for internal use only
40 /// @param id pool identification string
41 /// @param uri database connection string
42 /// @param pool_config static config
43 /// @param dns_resolver asynchronous resolver or `nullptr`
44 /// @param config_source dynamic config
45 Pool(
46 std::string id,
47 const std::string& uri,
48 const PoolConfig& pool_config,
49 clients::dns::Resolver* dns_resolver,
50 dynamic_config::Source config_source
51 );
52
53 ~Pool();
54
55 void Start();
56
57 void Stop();
58 /// @endcond
59
60 /// Checks whether a collection exists
61 bool HasCollection(const std::string& name) const;
62
63 /// Returns a handle for the specified collection
64 Collection GetCollection(std::string name) const;
65
66 /// Drops the associated database if it exists. New modifications of
67 /// collections will attempt to re-create the database automatically.
69
70 /// Get a list of all the collection names in the associated database
72
73 void Ping();
74
75 /// Writes pool statistics
76 friend void DumpMetric(utils::statistics::Writer& writer, const Pool& pool);
77
78 /// Sets new dynamic pool settings
79 void SetPoolSettings(const PoolSettings& pool_settings);
80
81private:
82 std::shared_ptr<impl::PoolImpl> impl_;
83};
84
85using PoolPtr = std::shared_ptr<Pool>;
86
87} // namespace storages::mongo
88
89USERVER_NAMESPACE_END