userver: userver/storages/mongo/pool.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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
9#include <userver/clients/dns/resolver_fwd.hpp>
10#include <userver/dynamic_config/fwd.hpp>
11#include <userver/storages/mongo/collection.hpp>
12#include <userver/storages/mongo/pool_config.hpp>
13#include <userver/utils/statistics/fwd.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace storages::mongo {
18
19namespace impl {
20class PoolImpl;
21} // namespace impl
22
23/// @ingroup userver_clients
24///
25/// @brief MongoDB client pool.
26///
27/// Use constructor only for tests, in production the pool should be retrieved
28/// from @ref userver_components "the components" via
29/// components::Mongo::GetPool() or components::MultiMongo::GetPool().
30///
31/// ## Example usage:
32///
33/// @snippet storages/mongo/collection_mongotest.hpp Sample Mongo usage
34class Pool {
35 public:
36 /// @cond
37
38 /// Client pool constructor, for internal use only
39 /// @param id pool identification string
40 /// @param uri database connection string
41 /// @param pool_config static config
42 /// @param dns_resolver asynchronous resolver or `nullptr`
43 /// @param config_source dynamic config
44 Pool(std::string id, const std::string& uri, const PoolConfig& pool_config,
45 clients::dns::Resolver* dns_resolver,
46 dynamic_config::Source config_source);
47
48 ~Pool();
49
50 void Start();
51 /// @endcond
52
53 /// Checks whether a collection exists
54 bool HasCollection(const std::string& name) const;
55
56 /// Returns a handle for the specified collection
57 Collection GetCollection(std::string name) const;
58
59 /// Drops the associated database if it exists. New modifications of
60 /// collections will attempt to re-create the database automatically.
62
63 void Ping();
64
65 /// Writes pool statistics
66 friend void DumpMetric(utils::statistics::Writer& writer, const Pool& pool);
67
68 private:
69 std::shared_ptr<impl::PoolImpl> impl_;
70};
71
72using PoolPtr = std::shared_ptr<Pool>;
73
74} // namespace storages::mongo
75
76USERVER_NAMESPACE_END