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 /// if not specified, read session chooses event batch size automatically
44 std::vector<NYdb::NTopic::TReadSessionEvent::TEvent> GetEvents(std::optional<std::size_t> max_events_count = {});
45
46 /// @brief Close read session
47 ///
48 /// Waits for all commit acknowledgments to arrive.
49 /// Force close after timeout
50 bool Close(std::chrono::milliseconds timeout);
51
52 /// Get native read session
53 /// @warning Use with care! Facilities from
54 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
55 /// non-blocking wait operations.
56 std::shared_ptr<NYdb::NTopic::IReadSession> GetNativeTopicReadSession();
57
58private:
59 std::shared_ptr<NYdb::NTopic::IReadSession> read_session_;
60};
61
62/// @ingroup userver_clients
63///
64/// @brief YDB Topic Client
65///
66/// @see https://ydb.tech/docs/en/concepts/topic
67class TopicClient final {
68public:
69 /// @cond
70 // For internal use only.
71 TopicClient(std::shared_ptr<impl::Driver> driver, impl::TopicSettings settings);
72 /// @endcond
73
74 ~TopicClient();
75
76 /// Alter topic
77 void AlterTopic(const std::string& path, const NYdb::NTopic::TAlterTopicSettings& settings);
78
79 /// Describe topic
80 NYdb::NTopic::TDescribeTopicResult DescribeTopic(const std::string& path);
81
82 /// Create read session
83 TopicReadSession CreateReadSession(const NYdb::NTopic::TReadSessionSettings& settings);
84
85 /// Get native topic client
86 /// @warning Use with care! Facilities from
87 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
88 /// non-blocking wait operations.
89 NYdb::NTopic::TTopicClient& GetNativeTopicClient();
90
91private:
92 std::shared_ptr<impl::Driver> driver_;
93 NYdb::NTopic::TTopicClient topic_client_;
94};
95
96} // namespace ydb
97
98USERVER_NAMESPACE_END