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