userver: userver/storages/mongo/multi_mongo.hpp Source File
Loading...
Searching...
No Matches
multi_mongo.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/multi_mongo.hpp
4/// @brief @copybrief storages::mongo::MultiMongo
5
6#include <memory>
7#include <string>
8#include <unordered_map>
9
10#include <userver/dynamic_config/source.hpp>
11#include <userver/rcu/rcu.hpp>
12#include <userver/storages/secdist/fwd.hpp>
13#include <userver/utils/resource_scopes_fwd.hpp>
14#include <userver/utils/statistics/fwd.hpp>
15
16#include <userver/storages/mongo/pool.hpp>
17#include <userver/storages/mongo/pool_config.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace storages::mongo {
22
23/// @brief Dynamic set of named MongoDB connection pools (secdist + dynamic config)
25 using PoolMap = std::unordered_map<std::string, storages::mongo::PoolPtr>;
26
27public:
28 /// Database set builder
29 class PoolSet {
30 public:
31 explicit PoolSet(MultiMongo&);
32
33 PoolSet(const PoolSet&);
34 PoolSet(PoolSet&&) noexcept;
35 PoolSet& operator=(const PoolSet&);
36 PoolSet& operator=(PoolSet&&) noexcept;
37
38 /// Adds all currently enabled databases to the set
40
41 /// @brief Adds a database to the set by its name
42 /// @param dbalias name of the database in secdist config
43 void AddPool(std::string dbalias);
44
45 /// @brief Removes the database with the specified name from the set
46 /// @param dbalias name of the database passed to AddPool
47 /// @returns whether the database was in the set
48 bool RemovePool(const std::string& dbalias);
49
50 /// @brief Replaces the working database set
51 void Activate();
52
53 private:
54 MultiMongo* target_{nullptr};
55 std::shared_ptr<PoolMap> pool_map_ptr_;
56 };
57
58 /// @cond
59 MultiMongo(
60 utils::ResourceScopeStorage& scopes,
61 std::string name,
62 storages::secdist::Secdist& secdist,
63 storages::mongo::PoolConfig pool_config,
64 clients::dns::Resolver* dns_resolver,
65 dynamic_config::Source config_source
66 );
67 /// @endcond
68
69 /// @brief Client pool accessor
70 /// @param dbalias name previously passed to `AddPool`
71 /// @throws PoolNotFoundException if no such database is enabled
72 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
73
74 /// @brief Adds a database to the working set by its name.
75 /// Equivalent to
76 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
77 /// @param dbalias name of the database in secdist config
78 void AddPool(std::string dbalias);
79
80 /// @brief Removes the database with the specified name from the working set.
81 /// Equivalent to
82 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
83 /// @param dbalias name of the database passed to AddPool
84 /// @returns whether the database was in the working set
85 bool RemovePool(const std::string& dbalias);
86
87 /// Creates an empty database set bound to the current MultiMongo instance
89
90 /// Writes statistics
91 friend void DumpMetric(utils::statistics::Writer& writer, const MultiMongo& multi_mongo);
92
93 const std::string& GetName() const { return name_; }
94
95private:
96 void OnConfigUpdate(const dynamic_config::Snapshot& config);
97
98 void OnSecdistUpdate(const storages::secdist::SecdistConfig& secdist);
99
100 storages::mongo::PoolPtr FindPool(const std::string& dbalias) const;
101
102 const std::string name_;
103 const storages::secdist::Secdist& secdist_;
104 dynamic_config::Source config_source_;
105 const storages::mongo::PoolConfig pool_config_;
106 clients::dns::Resolver* dns_resolver_;
107 rcu::Variable<PoolMap> pool_map_;
108};
109
110} // namespace storages::mongo
111
112USERVER_NAMESPACE_END