userver: userver/ydb/component.hpp Source File
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ydb/component.hpp
4/// @brief @copybrief ydb::YdbComponent
5
6#include <memory>
7#include <string>
8#include <unordered_map>
9
10#include <userver/components/component_base.hpp>
11#include <userver/dynamic_config/source.hpp>
12#include <userver/formats/json.hpp>
13#include <userver/utils/statistics/fwd.hpp>
14#include <userver/utils/statistics/storage.hpp>
15
16#include <userver/ydb/fwd.hpp>
17
18#include <ydb-cpp-sdk/client/driver/fwd.h>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace ydb {
23
24namespace impl {
25class Driver;
26} // namespace impl
27
28/// @ingroup userver_components
29///
30/// @brief YDB client component
31///
32/// Provides access to @ref ydb::TableClient, @ref ydb::TopicClient, @ref ydb::FederatedTopicClient,
33/// @ref ydb::CoordinationClient.
34///
35/// ## Static options of ydb::YdbComponent :
36/// @include{doc} scripts/docs/en/components_schema/ydb/src/ydb/component.md
37///
38/// Options inherited from @ref components::ComponentBase :
39/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
40class YdbComponent final : public components::ComponentBase {
41public:
42 /// @ingroup userver_component_names
43 /// @brief The default name of ydb::YdbComponent component
44 static constexpr std::string_view kName = "ydb";
45
46 YdbComponent(const components::ComponentConfig&, const components::ComponentContext&);
47
48 ~YdbComponent() override;
49
50 /// Get table client
51 /// @param dbname database name from static config key
52 std::shared_ptr<TableClient> GetTableClient(const std::string& dbname) const;
53
54 /// Get topic client
55 /// @param dbname database name from static config key
56 std::shared_ptr<TopicClient> GetTopicClient(const std::string& dbname) const;
57
58 /// Get topic client
59 /// @param dbname database name from static config key
60 std::shared_ptr<FederatedTopicClient> GetFederatedTopicClient(const std::string& dbname) const;
61
62 /// Get coordination client
63 /// @param dbname database name from static config key
64 std::shared_ptr<CoordinationClient> GetCoordinationClient(const std::string& dbname) const;
65
66 /// Get native driver
67 /// @param dbname database name from static config key
68 /// @warning Use with care! Facilities from
69 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
70 /// non-blocking wait operations.
71 const NYdb::TDriver& GetNativeDriver(const std::string& dbname) const;
72
73 /// Get database path
74 /// @param dbname database name from static config key
75 const std::string& GetDatabasePath(const std::string& dbname) const;
76
77 static yaml_config::Schema GetStaticConfigSchema();
78
79private:
80 struct DatabaseUtils;
81
82 struct Database {
83 std::shared_ptr<impl::Driver> driver;
84 std::shared_ptr<TableClient> table_client;
85 std::shared_ptr<TopicClient> topic_client;
86 std::shared_ptr<FederatedTopicClient> federated_topic_client;
87 std::shared_ptr<CoordinationClient> coordination_client;
88 };
89
90 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
91 void WriteStatistics(utils::statistics::Writer& writer) const;
92 const Database& FindDatabase(const std::string& dbname) const;
93
94 std::unordered_map<std::string, Database> databases_;
95
96 dynamic_config::Source config_;
97
98 // These fields must be the last ones
99 concurrent::AsyncEventSubscriberScope config_subscription_;
100 utils::statistics::Entry statistic_holder_;
101};
102
103} // namespace ydb
104
105template <>
106inline constexpr bool components::kHasValidate<ydb::YdbComponent> = true;
107
108USERVER_NAMESPACE_END