userver: userver/ydb/topic.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 Close read session
51 ///
52 /// Waits for all commit acknowledgments to arrive.
53 /// Force close after timeout
54 bool Close(std::chrono::milliseconds timeout);
55
56 /// Get native read session
57 /// @warning Use with care! Facilities from
58 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
59 /// non-blocking wait operations.
60 std::shared_ptr<NYdb::NTopic::IReadSession> GetNativeTopicReadSession();
61
62private:
63 std::shared_ptr<NYdb::NTopic::IReadSession> read_session_;
64};
65
66/// @ingroup userver_clients
67///
68/// @brief YDB Topic Client
69///
70/// @see https://ydb.tech/docs/en/concepts/topic
71class TopicClient final {
72public:
73 /// @cond
74 // For internal use only.
75 TopicClient(std::shared_ptr<impl::Driver> driver, impl::TopicSettings settings);
76 /// @endcond
77
78 ~TopicClient();
79
80 /// Alter topic
81 void AlterTopic(const std::string& path, const NYdb::NTopic::TAlterTopicSettings& settings);
82
83 /// Describe topic
84 NYdb::NTopic::TDescribeTopicResult DescribeTopic(const std::string& path);
85
86 /// Create read session
87 TopicReadSession CreateReadSession(const NYdb::NTopic::TReadSessionSettings& settings);
88
89 /// Get native topic client
90 /// @warning Use with care! Facilities from
91 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
92 /// non-blocking wait operations.
93 NYdb::NTopic::TTopicClient& GetNativeTopicClient();
94
95private:
96 std::shared_ptr<impl::Driver> driver_;
97 NYdb::NTopic::TTopicClient topic_client_;
98};
99
100} // namespace ydb
101
102USERVER_NAMESPACE_END