userver: userver/storages/mongo/multi_mongo.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
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
22 public:
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(std::string name, const storages::secdist::Secdist& secdist,
55 storages::mongo::PoolConfig pool_config,
56 clients::dns::Resolver* dns_resolver,
57 dynamic_config::Source config_source);
58 /// @endcond
59
60 /// @brief Client pool accessor
61 /// @param dbalias name previously passed to `AddPool`
62 /// @throws PoolNotFoundException if no such database is enabled
63 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
64
65 /// @brief Adds a database to the working set by its name.
66 /// Equivalent to
67 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
68 /// @param dbalias name of the database in secdist config
69 void AddPool(std::string dbalias);
70
71 /// @brief Removes the database with the specified name from the working set.
72 /// Equivalent to
73 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
74 /// @param dbalias name of the database passed to AddPool
75 /// @returns whether the database was in the working set
76 bool RemovePool(const std::string& dbalias);
77
78 /// Creates an empty database set bound to the current MultiMongo instance
80
81 /// Writes statistics
82 friend void DumpMetric(utils::statistics::Writer& writer,
83 const MultiMongo& multi_mongo);
84
85 const std::string& GetName() const { return name_; }
86
87 private:
88 storages::mongo::PoolPtr FindPool(const std::string& dbalias) const;
89
90 const std::string name_;
91 const storages::secdist::Secdist& secdist_;
92 dynamic_config::Source config_source_;
93 const storages::mongo::PoolConfig pool_config_;
94 clients::dns::Resolver* dns_resolver_;
95 rcu::Variable<PoolMap> pool_map_;
96};
97
98} // namespace storages::mongo
99
100USERVER_NAMESPACE_END