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