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 {
44 TableClient(impl::TableSettings settings,
45 OperationSettings operation_settings,
46 dynamic_config::Source config_source,
47 std::shared_ptr<impl::Driver> driver);
55 void MakeDirectory(
const std::string& path);
56 void RemoveDirectory(
const std::string& path);
58 NYdb::NScheme::TDescribePathResult DescribePath(std::string_view path);
59 NYdb::NScheme::TListDirectoryResult ListDirectory(std::string_view path);
61 NYdb::NTable::TDescribeTableResult DescribeTable(std::string_view path);
62 void CreateTable(std::string_view path,
63 NYdb::NTable::TTableDescription&& table_desc);
64 void DropTable(std::string_view path);
78 template <
typename... Args>
79 ExecuteResponse ExecuteDataQuery(
const Query& query, Args&&... args);
81 template <
typename... Args>
82 ExecuteResponse ExecuteDataQuery(OperationSettings settings,
83 const Query& query, Args&&... args);
85 ExecuteResponse ExecuteDataQuery(OperationSettings settings,
87 PreparedArgsBuilder&& builder);
89 ExecuteResponse ExecuteDataQuery(QuerySettings query_settings,
90 OperationSettings settings,
92 PreparedArgsBuilder&& builder);
101 Transaction Begin(std::string transaction_name,
102 OperationSettings settings = {});
104 Transaction Begin(std::string transaction_name, TransactionMode tx_mode);
111 void BulkUpsert(std::string_view table, NYdb::TValue&& rows,
112 OperationSettings settings = {});
116 template <
typename RangeOfStructs>
117 void BulkUpsert(std::string_view table,
const RangeOfStructs& rows,
118 OperationSettings settings = {});
122 std::string_view table,
123 NYdb::NTable::TReadTableSettings&& read_settings = {},
124 OperationSettings settings = {});
130 template <
typename... Args>
131 ScanQueryResults ExecuteScanQuery(
const Query& query, Args&&... args);
133 template <
typename... Args>
134 ScanQueryResults ExecuteScanQuery(ScanQuerySettings&& scan_settings,
135 OperationSettings settings,
136 const Query& query, Args&&... args);
138 ScanQueryResults ExecuteScanQuery(ScanQuerySettings&& scan_settings,
139 OperationSettings settings,
141 PreparedArgsBuilder&& builder);
146 friend void DumpMetric(utils::statistics::Writer& writer,
147 const TableClient& table_client);
156 utils::RetryBudget& GetRetryBudget();
159 friend class Transaction;
160 friend struct impl::RequestContext;
162 std::string JoinDbPath(std::string_view path)
const;
166 NYdb::NTable::TExecDataQuerySettings ToExecQuerySettings(
167 QuerySettings query_settings)
const;
169 template <
typename... Args>
170 PreparedArgsBuilder MakeBuilder(Args&&... args);
178 template <
typename Settings,
typename Func>
179 auto ExecuteWithPathImpl(std::string_view path,
180 std::string_view operation_name,
181 OperationSettings&& settings, Func&& func);
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,
202 return ExecuteDataQuery(OperationSettings{}, query,
203 MakeBuilder(std::forward<Args>(args)...));
206template <
typename... Args>
207ExecuteResponse TableClient::ExecuteDataQuery(OperationSettings settings,
210 return ExecuteDataQuery(settings, query,
211 MakeBuilder(std::forward<Args>(args)...));
214template <
typename RangeOfStructs>
215void TableClient::
BulkUpsert(std::string_view table,
const RangeOfStructs& rows,
216 OperationSettings settings) {
217 NYdb::TValueBuilder builder;
218 ydb::Write(builder, rows);
219 BulkUpsert(table, builder.Build(), std::move(settings));
222template <
typename... Args>
223ScanQueryResults TableClient::ExecuteScanQuery(
const Query& query,
225 return ExecuteScanQuery(ScanQuerySettings{}, OperationSettings{}, query,
226 MakeBuilder(std::forward<Args>(args)...));
229template <
typename... Args>
230ScanQueryResults TableClient::ExecuteScanQuery(
231 ScanQuerySettings&& scan_settings, OperationSettings settings,
232 const Query& query, Args&&... args) {
233 return ExecuteScanQuery(std::move(scan_settings), std::move(settings), query,
234 MakeBuilder(std::forward<Args>(args)...));