Github   Telegram
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/storages/secdist/fwd.hpp>
9#include <userver/utils/swappingsmart.hpp>
10
11#include <userver/storages/mongo/pool.hpp>
12#include <userver/storages/mongo/pool_config.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace storages::mongo {
17
18class MultiMongo {
20
21 public:
22 /// Database set builder
23 class PoolSet {
24 public:
25 explicit PoolSet(MultiMongo&);
26
27 PoolSet(const PoolSet&);
28 PoolSet(PoolSet&&) noexcept;
29 PoolSet& operator=(const PoolSet&);
30 PoolSet& operator=(PoolSet&&) noexcept;
31
32 /// Adds all currently enabled databases to the set
33 void AddExistingPools();
34
35 /// @brief Adds a database to the set by its name
36 /// @param dbalias name of the database in secdist config
37 void AddPool(std::string dbalias);
38
39 /// @brief Removes the database with the specified name from the set
40 /// @param dbalias name of the database passed to AddPool
41 /// @returns whether the database was in the set
42 bool RemovePool(const std::string& dbalias);
43
44 /// @brief Replaces the working database set
45 void Activate();
46
47 private:
48 MultiMongo* target_{nullptr};
49 std::shared_ptr<PoolMap> pool_map_ptr_;
50 };
51
52 /// @cond
53 MultiMongo(std::string name, const storages::secdist::Secdist& secdist,
54 storages::mongo::PoolConfig pool_config,
55 clients::dns::Resolver* dns_resolver,
56 dynamic_config::Source config_source);
57 /// @endcond
58
59 /// @brief Client pool accessor
60 /// @param dbalias name previously passed to `AddPool`
61 /// @throws PoolNotFoundException if no such database is enabled
62 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
63
64 /// @brief Adds a database to the working set by its name.
65 /// Equivalent to
66 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
67 /// @param dbalias name of the database in secdist config
68 void AddPool(std::string dbalias);
69
70 /// @brief Removes the database with the specified name from the working set.
71 /// Equivalent to
72 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
73 /// @param dbalias name of the database passed to AddPool
74 /// @returns whether the database was in the working set
75 bool RemovePool(const std::string& dbalias);
76
77 /// Creates an empty database set bound to the current MultiMongo instance
79
80 /// Returns JSON with statistics
81 formats::json::Value GetStatistics(bool verbose) const;
82
83 const std::string& GetName() const { return name_; }
84
85 private:
86 storages::mongo::PoolPtr FindPool(const std::string& dbalias) const;
87
88 const std::string name_;
89 const storages::secdist::Secdist& secdist_;
90 dynamic_config::Source config_source_;
91 const storages::mongo::PoolConfig pool_config_;
92 clients::dns::Resolver* dns_resolver_;
93 utils::SwappingSmart<PoolMap> pool_map_ptr_;
94};
95
96} // namespace storages::mongo
97
98USERVER_NAMESPACE_END