Skip to content

Commit

Permalink
Supported SDK work in ydb repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Gazizonoki committed Aug 22, 2024
1 parent e4f06d5 commit ade87e8
Show file tree
Hide file tree
Showing 190 changed files with 8,358 additions and 2,960 deletions.
3 changes: 2 additions & 1 deletion cmake/protos_public_headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ src/api/protos/ydb_export.pb.h
src/api/protos/ydb_coordination.pb.h
src/api/protos/ydb_status_codes.pb.h
src/api/protos/draft/ydb_replication.pb.h
src/library/yql/public/issue/protos/issue_severity.pb.h
src/library/operation_id/protos/operation_id.pb.h
src/library/yql_common/issue/protos/issue_severity.pb.h
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ add_subdirectory(secondary_index)
add_subdirectory(secondary_index_builtin)
add_subdirectory(topic_reader)
add_subdirectory(ttl)
add_subdirectory(vector_index)
72 changes: 46 additions & 26 deletions examples/basic_example/basic_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using namespace NYdb;
using namespace NYdb::NTable;

namespace {

class TYdbErrorException : public yexception {
public:
TYdbErrorException(const TStatus& status)
Expand All @@ -16,18 +18,34 @@ class TYdbErrorException : public yexception {
TStatus Status;
};

static void ThrowOnError(const TStatus& status) {
void ThrowOnError(const TStatus& status) {
if (!status.IsSuccess()) {
throw TYdbErrorException(status) << status;
}
}

static void PrintStatus(const TStatus& status) {
void PrintStatus(const TStatus& status) {
std::cerr << "Status: " << ToString(status.GetStatus()) << std::endl;
std::cerr << status.GetIssues().ToString();
}

static std::string JoinPath(const std::string& basePath, const std::string& path) {
template <class T>
std::string OptionalToString(const std::optional<T>& opt) {
if (opt.has_value()) {
return std::to_string(opt.value());
}
return "(NULL)";
}

template <>
std::string OptionalToString<std::string>(const std::optional<std::string>& opt) {
if (opt.has_value()) {
return opt.value();
}
return "(NULL)";
}

std::string JoinPath(const std::string& basePath, const std::string& path) {
if (basePath.empty()) {
return path;
}
Expand All @@ -41,7 +59,7 @@ static std::string JoinPath(const std::string& basePath, const std::string& path
///////////////////////////////////////////////////////////////////////////////

//! Creates sample tables with CrateTable API.
static void CreateTables(TTableClient client, const std::string& path) {
void CreateTables(TTableClient client, const std::string& path) {
ThrowOnError(client.RetryOperationSync([path](TSession session) {
auto seriesDesc = TTableBuilder()
.AddNullableColumn("series_id", EPrimitiveType::Uint64)
Expand Down Expand Up @@ -83,7 +101,7 @@ static void CreateTables(TTableClient client, const std::string& path) {
}

//! Describe existing table.
static void DescribeTable(TTableClient client, const std::string& path, const std::string& name) {
void DescribeTable(TTableClient client, const std::string& path, const std::string& name) {
std::optional<TTableDescription> desc;

ThrowOnError(client.RetryOperationSync([path, name, &desc](TSession session) {
Expand All @@ -105,7 +123,7 @@ static void DescribeTable(TTableClient client, const std::string& path, const st
///////////////////////////////////////////////////////////////////////////////

//! Fills sample tables with data in single parameterized data query.
static TStatus FillTableDataTransaction(TSession session, const std::string& path) {
TStatus FillTableDataTransaction(TSession session, const std::string& path) {
auto query = std::format(R"(
PRAGMA TablePathPrefix("{}");
Expand Down Expand Up @@ -165,7 +183,7 @@ static TStatus FillTableDataTransaction(TSession session, const std::string& pat
}

//! Shows basic usage of YDB data queries and transactions.
static TStatus SelectSimpleTransaction(TSession session, const std::string& path,
TStatus SelectSimpleTransaction(TSession session, const std::string& path,
std::optional<TResultSet>& resultSet)
{
auto query = std::format(R"(
Expand Down Expand Up @@ -194,7 +212,7 @@ static TStatus SelectSimpleTransaction(TSession session, const std::string& path
}

//! Shows basic usage of mutating operations.
static TStatus UpsertSimpleTransaction(TSession session, const std::string& path) {
TStatus UpsertSimpleTransaction(TSession session, const std::string& path) {
auto query = std::format(R"(
--!syntax_v1
PRAGMA TablePathPrefix("{}");
Expand All @@ -208,7 +226,7 @@ static TStatus UpsertSimpleTransaction(TSession session, const std::string& path
}

//! Shows usage of parameters in data queries.
static TStatus SelectWithParamsTransaction(TSession session, const std::string& path,
TStatus SelectWithParamsTransaction(TSession session, const std::string& path,
uint64_t seriesId, uint64_t seasonId, std::optional<TResultSet>& resultSet)
{
auto query = std::format(R"(
Expand Down Expand Up @@ -248,7 +266,7 @@ static TStatus SelectWithParamsTransaction(TSession session, const std::string&
}

//! Shows usage of prepared queries.
static TStatus PreparedSelectTransaction(TSession session, const std::string& path,
TStatus PreparedSelectTransaction(TSession session, const std::string& path,
uint64_t seriesId, uint64_t seasonId, uint64_t episodeId, std::optional<TResultSet>& resultSet)
{
// Once prepared, query data is stored in the session and identified by QueryId.
Expand Down Expand Up @@ -301,7 +319,7 @@ static TStatus PreparedSelectTransaction(TSession session, const std::string& pa
}

//! Shows usage of transactions consisting of multiple data queries with client logic between them.
static TStatus MultiStepTransaction(TSession session, const std::string& path, uint64_t seriesId, uint64_t seasonId,
TStatus MultiStepTransaction(TSession session, const std::string& path, uint64_t seriesId, uint64_t seasonId,
std::optional<TResultSet>& resultSet)
{
auto query1 = std::format(R"(
Expand Down Expand Up @@ -394,7 +412,7 @@ static TStatus MultiStepTransaction(TSession session, const std::string& path, u
// Show usage of explicit Begin/Commit transaction control calls.
// In most cases it's better to use transaction control settings in ExecuteDataQuery calls instead
// to avoid additional hops to YDB cluster and allow more efficient execution of queries.
static TStatus ExplicitTclTransaction(TSession session, const std::string& path, const TInstant& airDate) {
TStatus ExplicitTclTransaction(TSession session, const std::string& path, const TInstant& airDate) {
auto beginResult = session.BeginTransaction(TTxSettings::SerializableRW()).GetValueSync();
if (!beginResult.IsSuccess()) {
return beginResult;
Expand Down Expand Up @@ -432,6 +450,8 @@ static TStatus ExplicitTclTransaction(TSession session, const std::string& path,
return tx.Commit().GetValueSync();
}

}

///////////////////////////////////////////////////////////////////////////////

void SelectSimple(TTableClient client, const std::string& path) {
Expand All @@ -443,9 +463,9 @@ void SelectSimple(TTableClient client, const std::string& path) {
TResultSetParser parser(*resultSet);
if (parser.TryNextRow()) {
std::cout << "> SelectSimple:" << std::endl << "Series"
<< ", Id: " << ToString(parser.ColumnParser("series_id").GetOptionalUint64())
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
<< ", Release date: " << ToString(parser.ColumnParser("release_date").GetOptionalString())
<< ", Id: " << OptionalToString(parser.ColumnParser("series_id").GetOptionalUint64())
<< ", Title: " << OptionalToString(parser.ColumnParser("title").GetOptionalUtf8())
<< ", Release date: " << OptionalToString(parser.ColumnParser("release_date").GetOptionalString())
<< std::endl;
}
}
Expand All @@ -465,8 +485,8 @@ void SelectWithParams(TTableClient client, const std::string& path) {
TResultSetParser parser(*resultSet);
if (parser.TryNextRow()) {
std::cout << "> SelectWithParams:" << std::endl << "Season"
<< ", Title: " << ToString(parser.ColumnParser("season_title").GetOptionalUtf8())
<< ", Series title: " << ToString(parser.ColumnParser("series_title").GetOptionalUtf8())
<< ", Title: " << OptionalToString(parser.ColumnParser("season_title").GetOptionalUtf8())
<< ", Series title: " << OptionalToString(parser.ColumnParser("series_title").GetOptionalUtf8())
<< std::endl;
}
}
Expand All @@ -481,8 +501,8 @@ void PreparedSelect(TTableClient client, const std::string& path, uint32_t serie
if (parser.TryNextRow()) {
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());

std::cout << "> PreparedSelect:" << std::endl << "Episode " << ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
std::cout << "> PreparedSelect:" << std::endl << "Episode " << OptionalToString(parser.ColumnParser("episode_id").GetOptionalUint64())
<< ", Title: " << OptionalToString(parser.ColumnParser("title").GetOptionalUtf8())
<< ", Air date: " << airDate.FormatLocalTime("%a %b %d, %Y")
<< std::endl;
}
Expand All @@ -499,9 +519,9 @@ void MultiStep(TTableClient client, const std::string& path) {
while (parser.TryNextRow()) {
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());

std::cout << "Episode " << ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
<< ", Season: " << ToString(parser.ColumnParser("season_id").GetOptionalUint64())
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
std::cout << "Episode " << OptionalToString(parser.ColumnParser("episode_id").GetOptionalUint64())
<< ", Season: " << OptionalToString(parser.ColumnParser("season_id").GetOptionalUint64())
<< ", Title: " << OptionalToString(parser.ColumnParser("title").GetOptionalUtf8())
<< ", Air date: " << airDate.FormatLocalTime("%a %b %d, %Y")
<< std::endl;
}
Expand Down Expand Up @@ -561,10 +581,10 @@ void ScanQuerySelect(TTableClient client, const std::string& path) {
TResultSetParser parser(rs);
while (parser.TryNextRow()) {
std::cout << "Season"
<< ", SeriesId: " << ToString(parser.ColumnParser("series_id").GetOptionalUint64())
<< ", SeasonId: " << ToString(parser.ColumnParser("season_id").GetOptionalUint64())
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
<< ", Air date: " << ToString(parser.ColumnParser("first_aired").GetOptionalString())
<< ", SeriesId: " << OptionalToString(parser.ColumnParser("series_id").GetOptionalUint64())
<< ", SeasonId: " << OptionalToString(parser.ColumnParser("season_id").GetOptionalUint64())
<< ", Title: " << OptionalToString(parser.ColumnParser("title").GetOptionalUtf8())
<< ", Air date: " << OptionalToString(parser.ColumnParser("first_aired").GetOptionalString())
<< std::endl;
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/bulk_upsert_simple/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void GetLogBatch(uint64_t logOffset, std::vector<TLogMessage>& logBatch) {
logBatch.clear();
for (size_t i = 0; i < BATCH_SIZE; ++i) {
TLogMessage message;
message.App = "App_" + ToString(logOffset % 10);
message.Host = "192.168.0." + ToString(logOffset % 11);
message.App = "App_" + std::to_string(logOffset % 10);
message.Host = "192.168.0." + std::to_string(logOffset % 11);
message.Timestamp = TInstant::Now() + TDuration::MilliSeconds(i % 1000);
message.HttpCode = 200;
message.Message = i % 2 ? "GET / HTTP/1.1" : "GET /images/logo.png HTTP/1.1";
Expand Down
3 changes: 2 additions & 1 deletion examples/pagination/pagination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <util/string/cast.h>

#include <filesystem>
#include <format>

using namespace NYdb;
using namespace NYdb::NTable;
Expand Down Expand Up @@ -152,7 +153,7 @@ bool SelectPaging(TTableClient client, const std::string& path, uint64_t pageLim
do {
lastCity = parser.ColumnParser("city").GetOptionalUtf8().value();
lastNumber = parser.ColumnParser("number").GetOptionalUint32().value();
std::cout << lastCity << ", Школа №" << lastNumber << ", Адрес: " << ToString(parser.ColumnParser("address").GetOptionalUtf8()) << std::endl;
std::cout << lastCity << ", Школа №" << lastNumber << ", Адрес: " << parser.ColumnParser("address").GetOptionalUtf8().value_or("(NULL)") << std::endl;
} while (parser.TryNextRow());
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions examples/secondary_index/secondary_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <library/cpp/getopt/last_getopt.h>

#include <format>

////////////////////////////////////////////////////////////////////////////////

#define TABLE_SERIES "series"
Expand Down
2 changes: 2 additions & 0 deletions examples/secondary_index_builtin/secondary_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <library/cpp/getopt/last_getopt.h>

#include <format>

#define TABLE_USERS "users"
#define TABLE_SERIES "series"

Expand Down
41 changes: 32 additions & 9 deletions examples/ttl/ttl.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
#include "ttl.h"
#include "util.h"

#include <format>

using namespace NExample;
using namespace NYdb;
using namespace NYdb::NTable;

constexpr uint32_t DOC_TABLE_PARTITION_COUNT = 4;
constexpr uint32_t EXPIRATION_QUEUE_COUNT = 4;

namespace {

template <class T>
std::string OptionalToString(const std::optional<T>& opt) {
if (opt.has_value()) {
return std::to_string(opt.value());
}
return "(NULL)";
}

template <>
std::string OptionalToString<std::string>(const std::optional<std::string>& opt) {
if (opt.has_value()) {
return opt.value();
}
return "(NULL)";
}

//! Creates Documents table and multiple ExpirationQueue tables
static void CreateTables(TTableClient client, const std::string& path) {
void CreateTables(TTableClient client, const std::string& path) {
// Documents table stores the contents of web pages.
// The table is partitioned by hash(Url) in order to evenly distribute the load.
ThrowOnError(client.RetryOperationSync([path](TSession session) {
Expand Down Expand Up @@ -48,7 +68,7 @@ static void CreateTables(TTableClient client, const std::string& path) {
///////////////////////////////////////////////////////////////////////////////

//! Insert or replaces a document.
static TStatus AddDocumentTransaction(TSession session, const std::string& path,
TStatus AddDocumentTransaction(TSession session, const std::string& path,
const std::string& url, const std::string& html, uint64_t timestamp)
{
// Add an entry to a random expiration queue in order to evenly distribute the load
Expand Down Expand Up @@ -88,7 +108,7 @@ static TStatus AddDocumentTransaction(TSession session, const std::string& path,
}

//! Reads document contents.
static TStatus ReadDocumentTransaction(TSession session, const std::string& path,
TStatus ReadDocumentTransaction(TSession session, const std::string& path,
const std::string& url, std::optional<TResultSet>& resultSet)
{
auto query = std::format(R"(
Expand Down Expand Up @@ -121,7 +141,7 @@ static TStatus ReadDocumentTransaction(TSession session, const std::string& path
}

//! Reads a batch of entries from expiration queue
static TStatus ReadExpiredBatchTransaction(TSession session, const std::string& path, const uint32_t queue,
TStatus ReadExpiredBatchTransaction(TSession session, const std::string& path, const uint32_t queue,
const uint64_t timestamp, const uint64_t prevTimestamp, const uint64_t prevDocId, std::optional<TResultSet>& resultSet)
{
auto query = std::format(R"(
Expand Down Expand Up @@ -177,7 +197,7 @@ static TStatus ReadExpiredBatchTransaction(TSession session, const std::string&
}

//! Deletes an expired document
static TStatus DeleteDocumentWithTimestamp(TSession session, const std::string& path, const uint32_t queue,
TStatus DeleteDocumentWithTimestamp(TSession session, const std::string& path, const uint32_t queue,
const uint64_t docId, const uint64_t timestamp)
{
auto query = std::format(R"(
Expand Down Expand Up @@ -206,6 +226,9 @@ static TStatus DeleteDocumentWithTimestamp(TSession session, const std::string&

return result;
}

}

///////////////////////////////////////////////////////////////////////////////

void AddDocument(TTableClient client, const std::string& path, const std::string& url,
Expand All @@ -230,10 +253,10 @@ void ReadDocument(TTableClient client, const std::string& path, const std::strin

TResultSetParser parser(*resultSet);
if (parser.TryNextRow()) {
std::cout << " DocId: " << ToString(parser.ColumnParser("doc_id").GetOptionalUint64()) << std::endl
<< " Url: " << ToString(parser.ColumnParser("url").GetOptionalUtf8()) << std::endl
<< " Timestamp: " << ToString(parser.ColumnParser("timestamp").GetOptionalUint64()) << std::endl
<< " Html: " << ToString(parser.ColumnParser("html").GetOptionalUtf8()) << std::endl;
std::cout << " DocId: " << OptionalToString(parser.ColumnParser("doc_id").GetOptionalUint64()) << std::endl
<< " Url: " << OptionalToString(parser.ColumnParser("url").GetOptionalUtf8()) << std::endl
<< " Timestamp: " << OptionalToString(parser.ColumnParser("timestamp").GetOptionalUint64()) << std::endl
<< " Html: " << OptionalToString(parser.ColumnParser("html").GetOptionalUtf8()) << std::endl;
} else {
std::cout << " Not found" << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vector_index/vector_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <ydb-cpp-sdk/client/driver/driver.h>
#include <ydb-cpp-sdk/client/table/table.h>

#include <src/library/getopt/last_getopt.h>
#include <library/cpp/getopt/last_getopt.h>

enum class ECommand {
DropIndex,
Expand Down
2 changes: 1 addition & 1 deletion include/ydb-cpp-sdk/client/datastreams/datastreams.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <ydb-cpp-sdk/client/table/table.h>

#include <src/api/grpc/draft/ydb_datastreams_v1.grpc.pb.h>
#include <ydb/public/api/grpc/draft/ydb_datastreams_v1.grpc.pb.h>

namespace NYdb::NDataStreams::V1 {

Expand Down
8 changes: 4 additions & 4 deletions include/ydb-cpp-sdk/client/discovery/discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ class TNodeRegistrationResult : public TStatus {
public:
TNodeRegistrationResult() : TStatus(EStatus::GENERIC_ERROR, NYql::TIssues()) {}
TNodeRegistrationResult(TStatus&& status, const Ydb::Discovery::NodeRegistrationResult& proto);
const uint32_t& GetNodeId() const;
uint32_t GetNodeId() const;
const std::string& GetDomainPath() const;
const uint64_t& GetExpire() const;
const uint64_t& GetScopeTabletId() const;
uint64_t GetExpire() const;
uint64_t GetScopeTabletId() const;
bool HasScopeTabletId() const;
const uint64_t& GetScopePathId() const;
uint64_t GetScopePathId() const;
bool HasScopePathId() const;
const std::string& GetNodeName() const;
bool HasNodeName() const;
Expand Down
Loading

0 comments on commit ade87e8

Please sign in to comment.