3#include <ydb-cpp-sdk/client/table/table.h>
5#include <userver/dynamic_config/source.hpp>
6#include <userver/utils/statistics/fwd.hpp>
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>
14namespace NMonitoring {
18USERVER_NAMESPACE_BEGIN
35enum class IsStreaming :
bool {};
38using ScanQuerySettings = NYdb::NTable::TStreamExecScanQuerySettings;
40class TableClient
final {
45 impl::TableSettings settings,
46 OperationSettings operation_settings,
47 dynamic_config::Source config_source,
48 std::shared_ptr<impl::Driver> driver
57 void MakeDirectory(
const std::string& path);
58 void RemoveDirectory(
const std::string& path);
60 NYdb::NScheme::TDescribePathResult DescribePath(std::string_view path);
61 NYdb::NScheme::TListDirectoryResult ListDirectory(std::string_view path);
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);
79 template <
typename... Args>
80 ExecuteResponse ExecuteDataQuery(
const Query& query, Args&&... args);
82 template <
typename... Args>
83 ExecuteResponse ExecuteDataQuery(OperationSettings settings,
const Query& query, Args&&... args);
85 ExecuteResponse ExecuteDataQuery(OperationSettings settings,
const Query& query, PreparedArgsBuilder&& builder);
87 ExecuteResponse ExecuteDataQuery(
88 QuerySettings query_settings,
89 OperationSettings settings,
91 PreparedArgsBuilder&& builder
101 Transaction Begin(std::string transaction_name, OperationSettings settings = {});
103 Transaction Begin(std::string transaction_name, TransactionMode tx_mode);
110 void BulkUpsert(std::string_view table, NYdb::TValue&& rows, OperationSettings settings = {});
114 template <
typename RangeOfStructs>
115 void BulkUpsert(std::string_view table,
const RangeOfStructs& rows, OperationSettings settings = {});
119 std::string_view table,
120 NYdb::NTable::TReadTableSettings&& read_settings = {},
121 OperationSettings settings = {}
128 template <
typename... Args>
129 ScanQueryResults ExecuteScanQuery(
const Query& query, Args&&... args);
131 template <
typename... Args>
133 ExecuteScanQuery(ScanQuerySettings&& scan_settings, OperationSettings settings,
const Query& query, Args&&... args);
135 ScanQueryResults ExecuteScanQuery(
136 ScanQuerySettings&& scan_settings,
137 OperationSettings settings,
139 PreparedArgsBuilder&& builder
145 friend void DumpMetric(
utils::statistics::Writer& writer,
const TableClient& table_client);
154 utils::RetryBudget& GetRetryBudget();
157 friend class Transaction;
158 friend struct impl::RequestContext;
160 std::string JoinDbPath(std::string_view path)
const;
164 NYdb::NTable::TExecDataQuerySettings ToExecQuerySettings(QuerySettings query_settings)
const;
166 template <
typename... Args>
167 PreparedArgsBuilder MakeBuilder(Args&&... args);
175 template <
typename Settings,
typename Func>
176 auto ExecuteWithPathImpl(
177 std::string_view path,
178 std::string_view operation_name,
179 OperationSettings&& settings,
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_;
192template <
typename... Args>
193PreparedArgsBuilder TableClient::MakeBuilder(Args&&... args) {
195 builder.AddParams(std::forward<Args>(args)...);
199template <
typename... Args>
200ExecuteResponse TableClient::ExecuteDataQuery(
const Query& query, Args&&... args) {
201 return ExecuteDataQuery(OperationSettings{}, query, MakeBuilder(std::forward<Args>(args)...));
204template <
typename... Args>
205ExecuteResponse TableClient::ExecuteDataQuery(OperationSettings settings,
const Query& query, Args&&... args) {
206 return ExecuteDataQuery(settings, query, MakeBuilder(std::forward<Args>(args)...));
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));
216template <
typename... Args>
217ScanQueryResults TableClient::ExecuteScanQuery(
const Query& query, Args&&... args) {
218 return ExecuteScanQuery(ScanQuerySettings{}, OperationSettings{}, query, MakeBuilder(std::forward<Args>(args)...));
221template <
typename... Args>
222ScanQueryResults TableClient::ExecuteScanQuery(
223 ScanQuerySettings&& scan_settings,
224 OperationSettings settings,
228 return ExecuteScanQuery(
229 std::move(scan_settings), std::move(settings), query, MakeBuilder(std::forward<Args>(args)...)