userver: userver/ydb/topic.hpp Source File
Loading...
Searching...
No Matches
topic.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ydb/topic.hpp
4/// @brief YDB Topic client
5
6#include <chrono>
7#include <memory>
8#include <string>
9
10#include <ydb-cpp-sdk/client/topic/client.h>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace ydb {
15
16namespace impl {
17class Driver;
18struct TopicSettings;
19} // namespace impl
20
21/// @brief Read session used to connect to one or more topics for reading
22///
23/// @see https://ydb.tech/docs/en/reference/ydb-sdk/topic#reading
24///
25/// ## Example usage:
26///
27/// @ref samples/ydb_service/components/topic_reader.hpp
28/// @ref samples/ydb_service/components/topic_reader.cpp
29///
30/// @example samples/ydb_service/components/topic_reader.hpp
31/// @example samples/ydb_service/components/topic_reader.cpp
32class TopicReadSession final {
33public:
34 /// @cond
35 // For internal use only.
36 explicit TopicReadSession(std::shared_ptr<NYdb::NTopic::IReadSession> read_session);
37 /// @endcond
38
39 /// @brief Get read session events
40 ///
41 /// Waits until event occurs
42 /// @param max_events_count maximum events count in batch
43 /// @param max_size_bytes total size limit for data messages in batch
44 /// if not specified, read session chooses event batch size automatically
46 std::optional<std::size_t> max_events_count = {},
47 size_t max_size_bytes = std::numeric_limits<size_t>::max()
48 );
49
50 /// @brief Get read session events
51 ///
52 /// Waits until event occurs
53 /// @param settings ydb native read session settings
55 const NYdb::NTopic::TReadSessionGetEventSettings& settings
56 );
57
58 /// @brief Close read session
59 ///
60 /// Waits for all commit acknowledgments to arrive.
61 /// Force close after timeout
62 bool Close(std::chrono::milliseconds timeout);
63
64 /// Get native read session
65 /// @warning Use with care! Facilities from
66 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
67 /// non-blocking wait operations.
68 std::shared_ptr<NYdb::NTopic::IReadSession> GetNativeTopicReadSession();
69
70private:
71 std::shared_ptr<NYdb::NTopic::IReadSession> read_session_;
72};
73
74/// @ingroup userver_clients
75///
76/// @brief YDB Topic Client
77///
78/// @see https://ydb.tech/docs/en/concepts/topic
79class TopicClient final {
80public:
81 /// @cond
82 // For internal use only.
83 TopicClient(std::shared_ptr<impl::Driver> driver, impl::TopicSettings settings);
84 /// @endcond
85
86 ~TopicClient();
87
88 /// Alter topic
89 void AlterTopic(const std::string& path, const NYdb::NTopic::TAlterTopicSettings& settings);
90
91 /// Describe topic
92 NYdb::NTopic::TDescribeTopicResult DescribeTopic(const std::string& path);
93
94 /// Create read session
95 TopicReadSession CreateReadSession(const NYdb::NTopic::TReadSessionSettings& settings);
96
97 /// Get native topic client
98 /// @warning Use with care! Facilities from
99 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
100 /// non-blocking wait operations.
101 NYdb::NTopic::TTopicClient& GetNativeTopicClient();
102
103private:
104 std::shared_ptr<impl::Driver> driver_;
105 NYdb::NTopic::TTopicClient topic_client_;
106};
107
108} // namespace ydb
109
110USERVER_NAMESPACE_END