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 {
36 public:
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(std::string id, const std::string& uri, const PoolConfig& pool_config,
46 clients::dns::Resolver* dns_resolver,
47 dynamic_config::Source config_source);
48
49 ~Pool();
50
51 void Start();
52
53 void Stop();
54 /// @endcond
55
56 /// Checks whether a collection exists
57 bool HasCollection(const std::string& name) const;
58
59 /// Returns a handle for the specified collection
60 Collection GetCollection(std::string name) const;
61
62 /// Drops the associated database if it exists. New modifications of
63 /// collections will attempt to re-create the database automatically.
65
66 /// Get a list of all the collection names in the associated database
68
69 void Ping();
70
71 /// Writes pool statistics
72 friend void DumpMetric(utils::statistics::Writer& writer, const Pool& pool);
73
74 private:
75 std::shared_ptr<impl::PoolImpl> impl_;
76};
77
78using PoolPtr = std::shared_ptr<Pool>;
79
80} // namespace storages::mongo
81
82USERVER_NAMESPACE_END