userver: userver/ydb/table.hpp Source File
Loading...
Searching...
No Matches
table.hpp
1#pragma once
2
3#include <ydb-cpp-sdk/client/table/table.h>
4
5#include <userver/dynamic_config/source.hpp>
6#include <userver/utils/statistics/fwd.hpp>
7
8#include <userver/ydb/builder.hpp>
9#include <userver/ydb/query.hpp>
10#include <userver/ydb/response.hpp>
11#include <userver/ydb/settings.hpp>
12#include <userver/ydb/transaction.hpp>
13
14namespace NMonitoring {
15class TMetricRegistry;
16} // namespace NMonitoring
17
18USERVER_NAMESPACE_BEGIN
19
20namespace tracing {
21class Span;
22} // namespace tracing
23
24namespace utils {
25class RetryBudget;
26} // namespace utils
27
28namespace ydb {
29
30namespace impl {
31struct Stats;
32struct TableSettings;
33class Driver;
34struct RequestContext;
35enum class IsStreaming : bool {};
36} // namespace impl
37
38using ScanQuerySettings = NYdb::NTable::TStreamExecScanQuerySettings;
39
40class TableClient final {
41public:
42 /// @cond
43 // For internal use only.
44 TableClient(
45 impl::TableSettings settings,
46 OperationSettings operation_settings,
47 dynamic_config::Source config_source,
48 std::shared_ptr<impl::Driver> driver
49 );
50
51 ~TableClient();
52 /// @endcond
53
54 /// Query for creating/deleting tables
55 void ExecuteSchemeQuery(const std::string& query);
56
57 void MakeDirectory(const std::string& path);
58 void RemoveDirectory(const std::string& path);
59
60 NYdb::NScheme::TDescribePathResult DescribePath(std::string_view path);
61 NYdb::NScheme::TListDirectoryResult ListDirectory(std::string_view path);
62
63 NYdb::NTable::TDescribeTableResult DescribeTable(std::string_view path);
64 void CreateTable(std::string_view path, NYdb::NTable::TTableDescription&& table_desc);
65 void DropTable(std::string_view path);
66
67 /// @name Data queries execution
68 /// Execute a single data query outside of transactions. Query parameters are
69 /// passed in `Args` as "string key - value" pairs:
70 ///
71 /// @code
72 /// client.ExecuteDataQuery(query, "name1", value1, "name2", value2, ...);
73 /// @endcode
74 ///
75 /// Use ydb::PreparedArgsBuilder for storing a generic buffer of query params
76 /// if needed.
77 ///
78 /// @{
79 template <typename... Args>
80 ExecuteResponse ExecuteDataQuery(const Query& query, Args&&... args);
81
82 template <typename... Args>
83 ExecuteResponse ExecuteDataQuery(OperationSettings settings, const Query& query, Args&&... args);
84
85 ExecuteResponse ExecuteDataQuery(OperationSettings settings, const Query& query, PreparedArgsBuilder&& builder);
86
87 ExecuteResponse ExecuteDataQuery(
88 QuerySettings query_settings,
89 OperationSettings settings,
90 const Query& query,
91 PreparedArgsBuilder&& builder
92 );
93 /// @}
94
95 /// @name Transactions
96 /// @brief Begin a transaction with the specified name. The settings are used
97 /// for the `BEGIN` statement.
98 /// @see ydb::Transaction
99 ///
100 /// @{
101 Transaction Begin(std::string transaction_name, OperationSettings settings = {});
102
103 Transaction Begin(std::string transaction_name, TransactionMode tx_mode);
104 /// @}
105
106 /// Builder for storing dynamic query params.
107 PreparedArgsBuilder GetBuilder() const;
108
109 /// Efficiently write large ranges of table data.
110 void BulkUpsert(std::string_view table, NYdb::TValue&& rows, OperationSettings settings = {});
111
112 /// Efficiently write large ranges of table data.
113 /// The passed range of structs is serialized to TValue.
114 template <typename RangeOfStructs>
115 void BulkUpsert(std::string_view table, const RangeOfStructs& rows, OperationSettings settings = {});
116
117 /// Efficiently read large ranges of table data.
118 ReadTableResults ReadTable(
119 std::string_view table,
120 NYdb::NTable::TReadTableSettings&& read_settings = {},
121 OperationSettings settings = {}
122 );
123
124 /// @name Scan queries execution
125 /// A separate data access interface designed primarily for performing
126 /// analytical ad-hoc queries.
127 /// @{
128 template <typename... Args>
129 ScanQueryResults ExecuteScanQuery(const Query& query, Args&&... args);
130
131 template <typename... Args>
132 ScanQueryResults
133 ExecuteScanQuery(ScanQuerySettings&& scan_settings, OperationSettings settings, const Query& query, Args&&... args);
134
135 ScanQueryResults ExecuteScanQuery(
136 ScanQuerySettings&& scan_settings,
137 OperationSettings settings,
138 const Query& query,
139 PreparedArgsBuilder&& builder
140 );
141 /// @}
142
143 /// @cond
144 // For internal use only.
145 friend void DumpMetric(utils::statistics::Writer& writer, const TableClient& table_client);
146 /// @endcond
147
148 /// Get native table client
149 /// @warning Use with care! Facilities from
150 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
151 /// non-blocking wait operations.
153
154 utils::RetryBudget& GetRetryBudget();
155
156private:
157 friend class Transaction;
158 friend struct impl::RequestContext;
159
160 std::string JoinDbPath(std::string_view path) const;
161
162 void Select1();
163
164 NYdb::NTable::TExecDataQuerySettings ToExecQuerySettings(QuerySettings query_settings) const;
165
166 template <typename... Args>
167 PreparedArgsBuilder MakeBuilder(Args&&... args);
168
169 // Func: (TSession, const std::string& full_path, const Settings&)
170 // -> NThreading::TFuture<T>
171 // OR
172 // (TTableClient&, const std::string& full_path, const Settings&)
173 // -> NThreading::TFuture<T>
174 // ExecuteSchemeQueryImpl -> T
175 template <typename Settings, typename Func>
176 auto ExecuteWithPathImpl(
177 std::string_view path,
178 std::string_view operation_name,
179 OperationSettings&& settings,
180 Func&& func
181 );
182
183 dynamic_config::Source config_source_;
184 const OperationSettings default_settings_;
185 const bool keep_in_query_cache_;
186 std::unique_ptr<impl::Stats> stats_;
187 std::shared_ptr<impl::Driver> driver_;
188 std::unique_ptr<NYdb::NScheme::TSchemeClient> scheme_client_;
189 std::unique_ptr<NYdb::NTable::TTableClient> table_client_;
190};
191
192template <typename... Args>
193PreparedArgsBuilder TableClient::MakeBuilder(Args&&... args) {
194 auto builder = GetBuilder();
195 builder.AddParams(std::forward<Args>(args)...);
196 return builder;
197}
198
199template <typename... Args>
200ExecuteResponse TableClient::ExecuteDataQuery(const Query& query, Args&&... args) {
201 return ExecuteDataQuery(OperationSettings{}, query, MakeBuilder(std::forward<Args>(args)...));
202}
203
204template <typename... Args>
205ExecuteResponse TableClient::ExecuteDataQuery(OperationSettings settings, const Query& query, Args&&... args) {
206 return ExecuteDataQuery(settings, query, MakeBuilder(std::forward<Args>(args)...));
207}
208
209template <typename RangeOfStructs>
210void TableClient::BulkUpsert(std::string_view table, const RangeOfStructs& rows, OperationSettings settings) {
211 NYdb::TValueBuilder builder;
212 ydb::Write(builder, rows);
213 BulkUpsert(table, builder.Build(), std::move(settings));
214}
215
216template <typename... Args>
217ScanQueryResults TableClient::ExecuteScanQuery(const Query& query, Args&&... args) {
218 return ExecuteScanQuery(ScanQuerySettings{}, OperationSettings{}, query, MakeBuilder(std::forward<Args>(args)...));
219}
220
221template <typename... Args>
222ScanQueryResults TableClient::ExecuteScanQuery(
223 ScanQuerySettings&& scan_settings,
224 OperationSettings settings,
225 const Query& query,
226 Args&&... args
227) {
228 return ExecuteScanQuery(
229 std::move(scan_settings), std::move(settings), query, MakeBuilder(std::forward<Args>(args)...)
230 );
231}
232
233} // namespace ydb
234
235USERVER_NAMESPACE_END