3#include <ydb-cpp-sdk/client/table/table.h>
5#include <userver/dynamic_config/source.hpp>
6#include <userver/rcu/rcu.hpp>
7#include <userver/utils/impl/source_location.hpp>
8#include <userver/utils/statistics/fwd.hpp>
10#include <userver/ydb/builder.hpp>
11#include <userver/ydb/exceptions.hpp>
12#include <userver/ydb/query.hpp>
13#include <userver/ydb/response.hpp>
14#include <userver/ydb/settings.hpp>
15#include <userver/ydb/transaction.hpp>
16#include <userver/ydb/types.hpp>
18namespace NMonitoring {
22USERVER_NAMESPACE_BEGIN
39enum class IsStreaming :
bool {};
42using ScanQuerySettings = NYdb::NTable::TStreamExecScanQuerySettings;
44class TableClient
final {
48 TableClient(impl::TableSettings settings,
49 OperationSettings operation_settings,
50 dynamic_config::Source config_source,
51 std::shared_ptr<impl::Driver> driver);
59 void MakeDirectory(
const std::string& path);
60 void RemoveDirectory(
const std::string& path);
62 NYdb::NScheme::TDescribePathResult DescribePath(std::string_view path);
63 NYdb::NScheme::TListDirectoryResult ListDirectory(std::string_view path);
65 NYdb::NTable::TDescribeTableResult DescribeTable(std::string_view path);
66 void CreateTable(std::string_view path,
67 NYdb::NTable::TTableDescription&& table_desc);
68 void DropTable(std::string_view path);
82 template <
typename... Args>
83 ExecuteResponse ExecuteDataQuery(
const Query& query, Args&&... args);
85 template <
typename... Args>
86 ExecuteResponse ExecuteDataQuery(OperationSettings settings,
87 const Query& query, Args&&... args);
89 ExecuteResponse ExecuteDataQuery(OperationSettings settings,
91 PreparedArgsBuilder&& builder);
93 ExecuteResponse ExecuteDataQuery(QuerySettings query_settings,
94 OperationSettings settings,
96 PreparedArgsBuilder&& builder);
105 Transaction Begin(std::string transaction_name,
106 OperationSettings settings = {});
108 Transaction Begin(std::string transaction_name, TransactionMode tx_mode);
115 void BulkUpsert(std::string_view table, NYdb::TValue&& rows,
116 OperationSettings settings = {});
120 template <
typename RangeOfStructs>
121 void BulkUpsert(std::string_view table,
const RangeOfStructs& rows,
122 OperationSettings settings = {});
126 std::string_view table,
127 NYdb::NTable::TReadTableSettings&& read_settings = {},
128 OperationSettings settings = {});
134 template <
typename... Args>
135 ScanQueryResults ExecuteScanQuery(
const Query& query, Args&&... args);
137 template <
typename... Args>
138 ScanQueryResults ExecuteScanQuery(ScanQuerySettings&& scan_settings,
139 OperationSettings settings,
140 const Query& query, Args&&... args);
142 ScanQueryResults ExecuteScanQuery(ScanQuerySettings&& scan_settings,
143 OperationSettings settings,
145 PreparedArgsBuilder&& builder);
150 void SetDefaultSettings(OperationSettings settings);
153 friend void DumpMetric(utils::statistics::Writer& writer,
154 const TableClient& table_client);
157 NYdb::NTable::TTableClient& GetNativeTableClient();
158 utils::RetryBudget& GetRetryBudget();
161 friend class Transaction;
162 friend struct impl::RequestContext;
164 std::string JoinDbPath(std::string_view path)
const;
166 static tracing::Span MakeSpan(
const Query& query,
167 const OperationSettings& settings,
168 tracing::Span* custom_parent_span,
169 utils::impl::SourceLocation location);
173 static engine::Deadline GetDeadline(
174 tracing::Span& span,
const dynamic_config::Snapshot& config_snapshot);
176 void PrepareSettings(
const Query& query,
177 const dynamic_config::Snapshot& config_snapshot,
178 OperationSettings& os, impl::IsStreaming)
const;
180 NYdb::NTable::TExecDataQuerySettings ToExecQuerySettings(
181 QuerySettings query_settings)
const;
183 template <
typename... Args>
184 PreparedArgsBuilder MakeBuilder(Args&&... args);
192 template <
typename Settings,
typename Func>
193 auto ExecuteWithPathImpl(std::string_view path,
194 std::string_view operation_name,
195 OperationSettings&& settings, Func&& func);
197 dynamic_config::Source config_source_;
198 rcu::Variable<OperationSettings> default_settings_;
199 const bool keep_in_query_cache_;
200 std::unique_ptr<impl::Stats> stats_;
201 std::shared_ptr<impl::Driver> driver_;
202 std::unique_ptr<NYdb::NScheme::TSchemeClient> scheme_client_;
203 std::unique_ptr<NYdb::NTable::TTableClient> table_client_;
206template <
typename... Args>
207PreparedArgsBuilder TableClient::MakeBuilder(Args&&... args) {
209 builder.AddParams(std::forward<Args>(args)...);
213template <
typename... Args>
214ExecuteResponse TableClient::ExecuteDataQuery(
const Query& query,
216 return ExecuteDataQuery(OperationSettings{}, query,
217 MakeBuilder(std::forward<Args>(args)...));
220template <
typename... Args>
221ExecuteResponse TableClient::ExecuteDataQuery(OperationSettings settings,
224 return ExecuteDataQuery(settings, query,
225 MakeBuilder(std::forward<Args>(args)...));
228template <
typename RangeOfStructs>
229void TableClient::
BulkUpsert(std::string_view table,
const RangeOfStructs& rows,
230 OperationSettings settings) {
231 NYdb::TValueBuilder builder;
232 ydb::Write(builder, rows);
233 BulkUpsert(table, builder.Build(), std::move(settings));
236template <
typename... Args>
237ScanQueryResults TableClient::ExecuteScanQuery(
const Query& query,
239 return ExecuteScanQuery(ScanQuerySettings{}, OperationSettings{}, query,
240 MakeBuilder(std::forward<Args>(args)...));
243template <
typename... Args>
244ScanQueryResults TableClient::ExecuteScanQuery(
245 ScanQuerySettings&& scan_settings, OperationSettings settings,
246 const Query& query, Args&&... args) {
247 return ExecuteScanQuery(std::move(scan_settings), std::move(settings), query,
248 MakeBuilder(std::forward<Args>(args)...));