Skip to content

Commit 86bcb53

Browse files
GazizonokiJlucblu
authored andcommitted
Replace Sprintf, Printf and GetEnv (#117)
1 parent 34b09cd commit 86bcb53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+184
-1095
lines changed

.github/workflows/pr_check.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ jobs:
5959
shell: bash
6060
run: |
6161
cd ../build
62-
examples/basic_example/basic_example -e localhost:2136 -d /local
62+
examples/basic_example/basic_example -e localhost:2136 -d /local -p /local/basic
63+
examples/bulk_upsert_simple/bulk_upsert_simple -e localhost:2136 -d /local -p /local/bulk
64+
examples/pagination/pagination -e localhost:2136 -d /local -p /local/pagination
65+
examples/ttl/ttl -e localhost:2136 -d /local -p /local/ttl

clang.toolchain

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
set(CMAKE_C_COMPILER clang)
22
set(CMAKE_CXX_COMPILER clang++)
3-
# set(CMAKE_CXX_FLAGS "-stdlib=libc++")
43
set(CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld -rdynamic")
54
set(CMAKE_SHARED_LINKER_FLAGS "-fuse-ld=lld")
65
set(CMAKE_C_STANDARD_LIBRARIES "-lc -lm")

client/ydb_persqueue_core/impl/read_session.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <util/generic/utility.h>
1515
#include <util/generic/yexception.h>
1616
#include <util/stream/mem.h>
17-
#include <util/system/env.h>
1817

1918
#include <variant>
2019

client/ydb_persqueue_core/impl/read_session.ipp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <util/generic/utility.h>
2121
#include <util/generic/yexception.h>
2222
#include <util/stream/mem.h>
23-
#include <util/system/env.h>
2423

2524
#include <utility>
2625
#include <variant>
@@ -41,7 +40,7 @@ namespace NYdb::NTopic::NCompressionDetails {
4140

4241
namespace NYdb::NPersQueue {
4342

44-
static const bool RangesMode = !GetEnv("PQ_OFFSET_RANGES_MODE").empty();
43+
static const bool RangesMode = !std::string{std::getenv("PQ_OFFSET_RANGES_MODE") ? std::getenv("PQ_OFFSET_RANGES_MODE") : ""}.empty();
4544

4645
template <typename TReaderCounters>
4746
void MakeCountersNotNull(TReaderCounters& counters);

client/ydb_persqueue_core/ut/basic_usage_ut.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <util/stream/str.h>
1212

1313
#include <atomic>
14+
#include <format>
1415

1516
using namespace NThreading;
1617
using namespace NKikimr;
@@ -123,7 +124,7 @@ Y_UNIT_TEST_SUITE(BasicUsage) {
123124

124125
UNIT_ASSERT_VALUES_EQUAL(previousOffset + 1, committedOffset);
125126
UNIT_ASSERT_VALUES_EQUAL(readMessageCount, messageCount);
126-
log.Write(TLOG_INFO, Sprintf("Time took to write and read %u messages, %lu [MiB] in total is %lu [s]",
127+
log.Write(TLOG_INFO, std::format("Time took to write and read {} messages, {} [MiB] in total is {} [s]",
127128
messageCount, (totalSize / 1_MB), (TInstant::Now() - start).Seconds()));
128129
}
129130

cmake/common.cmake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
find_package(Python3 REQUIRED)
44

5-
add_compile_definitions(CATBOOST_OPENSOURCE=yes)
6-
75
# assumes ToolName is always both the binary and the target name
86
function(get_built_tool_path OutBinPath SrcPath ToolName)
97
set(${OutBinPath} "${CMAKE_BINARY_DIR}/${SrcPath}/${ToolName}${CMAKE_EXECUTABLE_SUFFIX}" PARENT_SCOPE)

cmake/global_flags.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ string(APPEND ${COMMON_C_CXX_FLAGS} "\
1212
-ffunction-sections \
1313
-fdata-sections \
1414
-Wall \
15+
-Wunused \
1516
-Wextra \
1617
-Wno-parentheses \
1718
-Wno-implicit-const-int-float-conversion \

examples/basic_example/basic_example.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "basic_example.h"
22

33
#include <util/folder/pathsplit.h>
4-
#include <util/string/printf.h>
4+
5+
#include <format>
56

67
using namespace NYdb;
78
using namespace NYdb::NTable;
@@ -104,8 +105,8 @@ static void DescribeTable(TTableClient client, const std::string& path, const st
104105

105106
//! Fills sample tables with data in single parameterized data query.
106107
static TStatus FillTableDataTransaction(TSession session, const std::string& path) {
107-
auto query = Sprintf(R"(
108-
PRAGMA TablePathPrefix("%s");
108+
auto query = std::format(R"(
109+
PRAGMA TablePathPrefix("{}");
109110
110111
DECLARE $seriesData AS List<Struct<
111112
series_id: Uint64,
@@ -152,7 +153,7 @@ static TStatus FillTableDataTransaction(TSession session, const std::string& pat
152153
title,
153154
CAST(air_date AS Uint16) AS air_date
154155
FROM AS_TABLE($episodesData);
155-
)", path.c_str());
156+
)", path);
156157

157158
auto params = GetTablesDataParams();
158159

@@ -166,13 +167,13 @@ static TStatus FillTableDataTransaction(TSession session, const std::string& pat
166167
static TStatus SelectSimpleTransaction(TSession session, const std::string& path,
167168
std::optional<TResultSet>& resultSet)
168169
{
169-
auto query = Sprintf(R"(
170-
PRAGMA TablePathPrefix("%s");
170+
auto query = std::format(R"(
171+
PRAGMA TablePathPrefix("{}");
171172
172173
SELECT series_id, title, CAST(CAST(release_date AS Date) AS String) AS release_date
173174
FROM series
174175
WHERE series_id = 1;
175-
)", path.c_str());
176+
)", path);
176177

177178
auto txControl =
178179
// Begin new transaction with SerializableRW mode
@@ -193,13 +194,13 @@ static TStatus SelectSimpleTransaction(TSession session, const std::string& path
193194

194195
//! Shows basic usage of mutating operations.
195196
static TStatus UpsertSimpleTransaction(TSession session, const std::string& path) {
196-
auto query = Sprintf(R"(
197+
auto query = std::format(R"(
197198
--!syntax_v1
198-
PRAGMA TablePathPrefix("%s");
199+
PRAGMA TablePathPrefix("{}");
199200
200201
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES
201202
(2, 6, 1, "TBD");
202-
)", path.c_str());
203+
)", path);
203204

204205
return session.ExecuteDataQuery(query,
205206
TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx()).GetValueSync();
@@ -209,9 +210,9 @@ static TStatus UpsertSimpleTransaction(TSession session, const std::string& path
209210
static TStatus SelectWithParamsTransaction(TSession session, const std::string& path,
210211
ui64 seriesId, ui64 seasonId, std::optional<TResultSet>& resultSet)
211212
{
212-
auto query = Sprintf(R"(
213+
auto query = std::format(R"(
213214
--!syntax_v1
214-
PRAGMA TablePathPrefix("%s");
215+
PRAGMA TablePathPrefix("{}");
215216
216217
DECLARE $seriesId AS Uint64;
217218
DECLARE $seasonId AS Uint64;
@@ -221,7 +222,7 @@ static TStatus SelectWithParamsTransaction(TSession session, const std::string&
221222
INNER JOIN series AS sr
222223
ON sa.series_id = sr.series_id
223224
WHERE sa.series_id = $seriesId AND sa.season_id = $seasonId;
224-
)", path.c_str());
225+
)", path);
225226

226227
// Type of parameter values should be exactly the same as in DECLARE statements.
227228
auto params = session.GetParamsBuilder()
@@ -251,9 +252,9 @@ static TStatus PreparedSelectTransaction(TSession session, const std::string& pa
251252
{
252253
// Once prepared, query data is stored in the session and identified by QueryId.
253254
// Local query cache is used to keep track of queries, prepared in current session.
254-
auto query = Sprintf(R"(
255+
auto query = std::format(R"(
255256
--!syntax_v1
256-
PRAGMA TablePathPrefix("%s");
257+
PRAGMA TablePathPrefix("{}");
257258
258259
DECLARE $seriesId AS Uint64;
259260
DECLARE $seasonId AS Uint64;
@@ -262,7 +263,7 @@ static TStatus PreparedSelectTransaction(TSession session, const std::string& pa
262263
SELECT *
263264
FROM episodes
264265
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
265-
)", path.c_str());
266+
)", path);
266267

267268
// Prepare query or get result from query cache
268269
auto prepareResult = session.PrepareDataQuery(query).GetValueSync();
@@ -302,16 +303,16 @@ static TStatus PreparedSelectTransaction(TSession session, const std::string& pa
302303
static TStatus MultiStepTransaction(TSession session, const std::string& path, ui64 seriesId, ui64 seasonId,
303304
std::optional<TResultSet>& resultSet)
304305
{
305-
auto query1 = Sprintf(R"(
306+
auto query1 = std::format(R"(
306307
--!syntax_v1
307-
PRAGMA TablePathPrefix("%s");
308+
PRAGMA TablePathPrefix("{}");
308309
309310
DECLARE $seriesId AS Uint64;
310311
DECLARE $seasonId AS Uint64;
311312
312313
SELECT first_aired AS from_date FROM seasons
313314
WHERE series_id = $seriesId AND season_id = $seasonId;
314-
)", path.c_str());
315+
)", path);
315316

316317
auto params1 = session.GetParamsBuilder()
317318
.AddParam("$seriesId")
@@ -350,17 +351,17 @@ static TStatus MultiStepTransaction(TSession session, const std::string& path, u
350351
TInstant toDate = userFunc(fromDate);
351352

352353
// Construct next query based on the results of client logic
353-
auto query2 = Sprintf(R"(
354+
auto query2 = std::format(R"(
354355
--!syntax_v1
355-
PRAGMA TablePathPrefix("%s");
356+
PRAGMA TablePathPrefix("{}");
356357
357358
DECLARE $seriesId AS Uint64;
358359
DECLARE $fromDate AS Uint64;
359360
DECLARE $toDate AS Uint64;
360361
361362
SELECT season_id, episode_id, title, air_date FROM episodes
362363
WHERE series_id = $seriesId AND air_date >= $fromDate AND air_date <= $toDate;
363-
)", path.c_str());
364+
)", path);
364365

365366
auto params2 = session.GetParamsBuilder()
366367
.AddParam("$seriesId")
@@ -401,14 +402,14 @@ static TStatus ExplicitTclTransaction(TSession session, const std::string& path,
401402
// Get newly created transaction id
402403
auto tx = beginResult.GetTransaction();
403404

404-
auto query = Sprintf(R"(
405+
auto query = std::format(R"(
405406
--!syntax_v1
406-
PRAGMA TablePathPrefix("%s");
407+
PRAGMA TablePathPrefix("{}");
407408
408409
DECLARE $airDate AS Date;
409410
410411
UPDATE episodes SET air_date = CAST($airDate AS Uint16) WHERE title = "TBD";
411-
)", path.c_str());
412+
)", path);
412413

413414
auto params = session.GetParamsBuilder()
414415
.AddParam("$airDate")
@@ -512,16 +513,16 @@ void ExplicitTcl(TTableClient client, const std::string& path) {
512513
}
513514

514515
void ScanQuerySelect(TTableClient client, const std::string& path) {
515-
auto query = Sprintf(R"(
516+
auto query = std::format(R"(
516517
--!syntax_v1
517-
PRAGMA TablePathPrefix("%s");
518+
PRAGMA TablePathPrefix("{}");
518519
519520
DECLARE $series AS List<UInt64>;
520521
521522
SELECT series_id, season_id, title, CAST(CAST(first_aired AS Date) AS String) AS first_aired
522523
FROM seasons
523524
WHERE series_id IN $series
524-
)", path.c_str());
525+
)", path);
525526

526527
auto parameters = TParamsBuilder()
527528
.AddParam("$series")

examples/basic_example/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

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

5-
#include <util/system/env.h>
65
#include <util/stream/file.h>
76

7+
#include <cstdlib>
8+
89
using namespace NLastGetopt;
910
using namespace NYdb;
1011

@@ -41,7 +42,7 @@ int main(int argc, char** argv) {
4142
auto driverConfig = TDriverConfig()
4243
.SetEndpoint(endpoint)
4344
.SetDatabase(database)
44-
.SetAuthToken(GetEnv("YDB_TOKEN"));
45+
.SetAuthToken(std::getenv("YDB_TOKEN") ? std::getenv("YDB_TOKEN") : "");
4546

4647
if (!certPath.empty()) {
4748
std::string cert = TFileInput(certPath).ReadAll();

examples/bulk_upsert_simple/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

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

5-
#include <util/system/env.h>
65
#include <util/folder/pathsplit.h>
76

87
Y_DECLARE_OUT_SPEC(, NYdb::TStatus, stream, value) {
@@ -151,7 +150,7 @@ int main(int argc, char** argv) {
151150
auto driverConfig = NYdb::TDriverConfig()
152151
.SetEndpoint(endpoint)
153152
.SetDatabase(database)
154-
.SetAuthToken(GetEnv("YDB_TOKEN"));
153+
.SetAuthToken(std::getenv("YDB_TOKEN") ? std::getenv("YDB_TOKEN") : "");
155154

156155
NYdb::TDriver driver(driverConfig);
157156

examples/pagination/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "pagination.h"
22

33
#include <library/cpp/getopt/last_getopt.h>
4-
#include <util/system/env.h>
54

65
using namespace NLastGetopt;
76
using namespace NYdb;
@@ -35,7 +34,7 @@ int main(int argc, char** argv) {
3534
auto driverConfig = TDriverConfig()
3635
.SetEndpoint(endpoint)
3736
.SetDatabase(database)
38-
.SetAuthToken(GetEnv("YDB_TOKEN"));
37+
.SetAuthToken(std::getenv("YDB_TOKEN") ? std::getenv("YDB_TOKEN") : "");
3938
TDriver driver(driverConfig);
4039

4140
if (!Run(driver, path)) {

examples/pagination/pagination.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "pagination.h"
22

33
#include <util/folder/pathsplit.h>
4-
#include <util/string/printf.h>
54

65
using namespace NYdb;
76
using namespace NYdb::NTable;
@@ -54,9 +53,9 @@ static void CreateTable(TTableClient client, const std::string& path) {
5453

5554
//! Fills sample tables with data in single parameterized data query.
5655
static TStatus FillTableDataTransaction(TSession& session, const std::string& path) {
57-
auto query = Sprintf(R"(
56+
auto query = std::format(R"(
5857
--!syntax_v1
59-
PRAGMA TablePathPrefix("%s");
58+
PRAGMA TablePathPrefix("{}");
6059
6160
DECLARE $schoolsData AS List<Struct<
6261
city: Utf8,
@@ -69,7 +68,7 @@ static TStatus FillTableDataTransaction(TSession& session, const std::string& pa
6968
number,
7069
address
7170
FROM AS_TABLE($schoolsData);
72-
)", path.c_str());
71+
)", path);
7372

7473
auto params = GetTablesDataParams();
7574

@@ -83,9 +82,9 @@ static TStatus FillTableDataTransaction(TSession& session, const std::string& pa
8382
static TStatus SelectPagingTransaction(TSession& session, const std::string& path,
8483
ui64 pageLimit, const std::string& lastCity, ui32 lastNumber, std::optional<TResultSet>& resultSet)
8584
{
86-
auto query = Sprintf(R"(
85+
auto query = std::format(R"(
8786
--!syntax_v1
88-
PRAGMA TablePathPrefix("%s");
87+
PRAGMA TablePathPrefix("{}");
8988
9089
DECLARE $limit AS Uint64;
9190
DECLARE $lastCity AS Utf8;
@@ -111,7 +110,7 @@ static TStatus SelectPagingTransaction(TSession& session, const std::string& pat
111110
112111
SELECT * FROM $union
113112
ORDER BY city, number LIMIT $limit;
114-
)", path.c_str());
113+
)", path);
115114

116115
auto params = session.GetParamsBuilder()
117116
.AddParam("$limit")

examples/secondary_index/main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "secondary_index.h"
22

3-
#include <util/system/env.h>
4-
53
using namespace NLastGetopt;
64
using namespace NYdb;
75

@@ -42,7 +40,7 @@ int main(int argc, char** argv) {
4240
auto config = TDriverConfig()
4341
.SetEndpoint(endpoint)
4442
.SetDatabase(database)
45-
.SetAuthToken(GetEnv("YDB_TOKEN"));
43+
.SetAuthToken(std::getenv("YDB_TOKEN") ? std::getenv("YDB_TOKEN") : "");
4644
TDriver driver(config);
4745

4846
try {

examples/secondary_index/secondary_index.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55

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

8-
#include <string>
9-
#include <util/generic/yexception.h>
10-
#include <util/stream/output.h>
11-
#include <util/string/builder.h>
12-
#include <util/string/printf.h>
13-
148
////////////////////////////////////////////////////////////////////////////////
159

1610
#define TABLE_SERIES "series"

0 commit comments

Comments
 (0)