Fix clang-tidy in utils/examples

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2022-07-29 09:20:44 +03:00
parent b90152b6ec
commit 498c8b3c52
13 changed files with 53 additions and 75 deletions

View File

@ -25,7 +25,7 @@ int main(int argc, char ** argv)
return 0; return 0;
} }
String cache_name = ""; const char * cache_name = "";
if (argc == 4) if (argc == 4)
cache_name = argv[3]; cache_name = argv[3];

View File

@ -130,7 +130,7 @@ public:
private: private:
ReadBuffer & in; ReadBuffer & in;
/// The physical location of the current cell. /// The physical location of the current cell.
Locus locus; Locus locus{};
/// The current position in the file as a cell number. /// The current position in the file as a cell number.
BucketIndex current_bucket_index = 0; BucketIndex current_bucket_index = 0;
/// The number of bytes read. /// The number of bytes read.

View File

@ -12,7 +12,7 @@
#if defined(__clang__) #if defined(__clang__)
#include <experimental/coroutine> #include <experimental/coroutine>
namespace std namespace std // NOLINT(cert-dcl58-cpp)
{ {
using namespace experimental::coroutines_v1; using namespace experimental::coroutines_v1;
} }

View File

@ -1,13 +1,9 @@
#include <iostream>
#define DBMS_HASH_MAP_DEBUG_RESIZES #define DBMS_HASH_MAP_DEBUG_RESIZES
#define DBMS_HASH_MAP_COUNT_COLLISIONS #define DBMS_HASH_MAP_COUNT_COLLISIONS
#include <iostream>
#include <string.h> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <utility> #include <utility>
#include <base/types.h> #include <base/types.h>

View File

@ -85,7 +85,7 @@ try
for (BlockInfo & block : block_infos) for (BlockInfo & block : block_infos)
{ {
Coordination::GetResponse resp = block.contents_future.get(); Coordination::GetResponse resp = block.contents_future.get();
if (resp.error == Coordination::Error::ZOK && lock_holder_paths.count(resp.data)) if (resp.error == Coordination::Error::ZOK && lock_holder_paths.contains(resp.data))
{ {
++total_count; ++total_count;
current_inserts[block.partition].insert(block.number); current_inserts[block.partition].insert(block.number);

View File

@ -1,5 +1,5 @@
#include <lz4.h> #include <lz4.h>
#include <string.h> #include <cstring>
#include <optional> #include <optional>
#include <base/types.h> #include <base/types.h>
@ -60,7 +60,7 @@ protected:
compressed_in->readStrict(reinterpret_cast<char *>(&checksum), sizeof(checksum)); compressed_in->readStrict(reinterpret_cast<char *>(&checksum), sizeof(checksum));
own_compressed_buffer.resize(COMPRESSED_BLOCK_HEADER_SIZE); own_compressed_buffer.resize(COMPRESSED_BLOCK_HEADER_SIZE);
compressed_in->readStrict(&own_compressed_buffer[0], COMPRESSED_BLOCK_HEADER_SIZE); compressed_in->readStrict(own_compressed_buffer.data(), COMPRESSED_BLOCK_HEADER_SIZE);
UInt8 method = own_compressed_buffer[0]; /// See CompressedWriteBuffer.h UInt8 method = own_compressed_buffer[0]; /// See CompressedWriteBuffer.h
@ -90,7 +90,7 @@ protected:
else else
{ {
own_compressed_buffer.resize(size_compressed + (variant == LZ4_REFERENCE ? 0 : LZ4::ADDITIONAL_BYTES_AT_END_OF_BUFFER)); own_compressed_buffer.resize(size_compressed + (variant == LZ4_REFERENCE ? 0 : LZ4::ADDITIONAL_BYTES_AT_END_OF_BUFFER));
compressed_buffer = &own_compressed_buffer[0]; compressed_buffer = own_compressed_buffer.data();
compressed_in->readStrict(compressed_buffer + COMPRESSED_BLOCK_HEADER_SIZE, size_compressed - COMPRESSED_BLOCK_HEADER_SIZE); compressed_in->readStrict(compressed_buffer + COMPRESSED_BLOCK_HEADER_SIZE, size_compressed - COMPRESSED_BLOCK_HEADER_SIZE);
} }
@ -143,7 +143,7 @@ private:
return false; return false;
memory.resize(size_decompressed + LZ4::ADDITIONAL_BYTES_AT_END_OF_BUFFER); memory.resize(size_decompressed + LZ4::ADDITIONAL_BYTES_AT_END_OF_BUFFER);
working_buffer = Buffer(&memory[0], &memory[size_decompressed]); working_buffer = Buffer(memory.data(), &memory[size_decompressed]);
decompress(working_buffer.begin(), size_decompressed, size_compressed_without_checksum); decompress(working_buffer.begin(), size_decompressed, size_compressed_without_checksum);

View File

@ -1,6 +1,6 @@
#include <map> #include <map>
#include <cstdlib> #include <cstdlib>
#include <stdio.h> #include <cstdio>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -59,8 +59,8 @@ std::string randomDate()
int32_t month = rng() % 12 + 1; int32_t month = rng() % 12 + 1;
int32_t day = rng() % 12 + 1; int32_t day = rng() % 12 + 1;
char answer[13]; char answer[13];
sprintf(answer, "'%04u-%02u-%02u'", year, month, day); size_t size = sprintf(answer, "'%04u-%02u-%02u'", year, month, day);
return std::string(answer); return std::string(answer, size);
} }
std::string randomDatetime() std::string randomDatetime()
@ -72,7 +72,7 @@ std::string randomDatetime()
int32_t minutes = rng() % 60; int32_t minutes = rng() % 60;
int32_t seconds = rng() % 60; int32_t seconds = rng() % 60;
char answer[22]; char answer[22];
sprintf( size_t size = sprintf(
answer, answer,
"'%04u-%02u-%02u %02u:%02u:%02u'", "'%04u-%02u-%02u %02u:%02u:%02u'",
year, year,
@ -81,7 +81,7 @@ std::string randomDatetime()
hours, hours,
minutes, minutes,
seconds); seconds);
return std::string(answer); return std::string(answer, size);
} }
TableAndColumn get_table_a_column(const std::string & c) TableAndColumn get_table_a_column(const std::string & c)
{ {

View File

@ -4,7 +4,6 @@
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <system_error>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
@ -15,50 +14,35 @@
#include <Common/Config/ConfigProcessor.h> #include <Common/Config/ConfigProcessor.h>
#include <Common/Exception.h> #include <Common/Exception.h>
#include <Interpreters/Context.h> #include <Interpreters/Context.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/ReadHelpers.h>
using namespace DB; using namespace DB;
static SharedContextHolder shared_context = Context::createShared(); static SharedContextHolder shared_context = Context::createShared();
std::vector<std::string_view> loadMetrics(const std::string & metrics_file) auto loadMetrics(const std::string & metrics_file)
{ {
std::vector<std::string_view> metrics; std::vector<std::string> metrics;
ReadBufferFromFile in(metrics_file);
String line;
FILE * stream; while (!in.eof())
char * line = nullptr;
size_t len = 0;
ssize_t nread;
stream = fopen(metrics_file.c_str(), "r");
if (stream == nullptr)
{ {
throw std::runtime_error(strerror(errno)); readEscapedStringUntilEOL(line, in);
if (!in.eof())
{
++in.position();
} }
if (!line.empty() && line.back() == '\n')
while ((nread = getline(&line, &len, stream)) != -1) /// NOLINT
{ {
size_t l = strlen(line); line.pop_back();
if (l > 0)
{
if (line[l - 1] == '\n')
{
line[l - 1] = '\0';
l--;
} }
if (l > 0) if (!line.empty())
{ {
metrics.emplace_back(std::string_view(strdup(line), l)); metrics.emplace_back(line);
} }
} }
}
free(line);
if (ferror(stream))
{
fclose(stream);
throw std::runtime_error(strerror(errno));
}
fclose(stream);
return metrics; return metrics;
} }
@ -80,7 +64,7 @@ void bench(const std::string & config_path, const std::string & metrics_file, si
Graphite::Params params; Graphite::Params params;
setGraphitePatternsFromConfig(context, "graphite_rollup", params); setGraphitePatternsFromConfig(context, "graphite_rollup", params);
std::vector<std::string_view> metrics = loadMetrics(metrics_file); auto metrics = loadMetrics(metrics_file);
std::vector<double> durations(metrics.size()); std::vector<double> durations(metrics.size());
size_t j, i; size_t j, i;
@ -99,15 +83,14 @@ void bench(const std::string & config_path, const std::string & metrics_file, si
if (j == 0 && verbose) if (j == 0 && verbose)
{ {
std::cout << metrics[i].data() << ": rule with regexp '" << rule.second->regexp_str << "' found\n"; std::cout << metrics[i] << ": rule with regexp '" << rule.second->regexp_str << "' found\n";
} }
} }
} }
for (i = 0; i < metrics.size(); i++) for (i = 0; i < metrics.size(); i++)
{ {
std::cout << metrics[i].data() << " " << durations[i] / n << " ns\n"; std::cout << metrics[i] << " " << durations[i] / n << " ns\n";
free(const_cast<void *>(static_cast<const void *>(metrics[i].data())));
} }
} }

View File

@ -15,8 +15,7 @@
#include <vector> #include <vector>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <ctime>
#include <time.h>
#include <unistd.h> #include <unistd.h>
@ -54,7 +53,7 @@ void thread(int fd, int mode, size_t min_offset, size_t max_offset, size_t block
if ((mode & MODE_DIRECT)) if ((mode & MODE_DIRECT))
buf = direct_buf.data(); buf = direct_buf.data();
else else
buf = &simple_buf[0]; buf = simple_buf.data();
pcg64 rng(randomSeed()); pcg64 rng(randomSeed());

View File

@ -110,12 +110,12 @@ void thread(int fd, int mode, size_t min_offset, size_t max_offset, size_t block
} }
/// Send queries. /// Send queries.
if (io_submit(ctx.ctx, query_cbs.size(), &query_cbs[0]) < 0) if (io_submit(ctx.ctx, query_cbs.size(), query_cbs.data()) < 0)
throwFromErrno("io_submit failed", ErrorCodes::CANNOT_IO_SUBMIT); throwFromErrno("io_submit failed", ErrorCodes::CANNOT_IO_SUBMIT);
/// Receive answers. If we have something else to send, then receive at least one answer (after that send them), otherwise wait all answers. /// Receive answers. If we have something else to send, then receive at least one answer (after that send them), otherwise wait all answers.
memset(&events[0], 0, buffers_count * sizeof(events[0])); memset(events.data(), 0, buffers_count * sizeof(events[0]));
int evs = io_getevents(ctx.ctx, (blocks_sent < count ? 1 : in_progress), buffers_count, &events[0], nullptr); int evs = io_getevents(ctx.ctx, (blocks_sent < count ? 1 : in_progress), buffers_count, events.data(), nullptr);
if (evs < 0) if (evs < 0)
throwFromErrno("io_getevents failed", ErrorCodes::CANNOT_IO_GETEVENTS); throwFromErrno("io_getevents failed", ErrorCodes::CANNOT_IO_GETEVENTS);

View File

@ -13,8 +13,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <poll.h> #include <poll.h>
#include <stdlib.h> #include <cstdlib>
#include <time.h> #include <ctime>
#include <unistd.h> #include <unistd.h>
#if defined (OS_LINUX) #if defined (OS_LINUX)
@ -101,7 +101,7 @@ int mainImpl(int argc, char ** argv)
size_t ops = 0; size_t ops = 0;
while (ops < count) while (ops < count)
{ {
if (poll(&polls[0], descriptors, -1) <= 0) if (poll(polls.data(), descriptors, -1) <= 0)
throwFromErrno("poll failed", ErrorCodes::SYSTEM_ERROR); throwFromErrno("poll failed", ErrorCodes::SYSTEM_ERROR);
for (size_t i = 0; i < descriptors; ++i) for (size_t i = 0; i < descriptors; ++i)
{ {
@ -123,12 +123,12 @@ int mainImpl(int argc, char ** argv)
if (mode == MODE_READ) if (mode == MODE_READ)
{ {
if (static_cast<int>(block_size) != pread(fds[i], &buf[0], block_size, offset)) if (static_cast<int>(block_size) != pread(fds[i], buf.data(), block_size, offset))
throwFromErrno("Cannot read", ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR); throwFromErrno("Cannot read", ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR);
} }
else else
{ {
if (static_cast<int>(block_size) != pwrite(fds[i], &buf[0], block_size, offset)) if (static_cast<int>(block_size) != pwrite(fds[i], buf.data(), block_size, offset))
throwFromErrno("Cannot write", ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR); throwFromErrno("Cannot write", ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
} }
} }

View File

@ -179,8 +179,8 @@ void setCurrentBlockNumber(zkutil::ZooKeeper & zk, const std::string & path, Int
if (number != current_block_number) if (number != current_block_number)
{ {
char suffix[11] = ""; char suffix[11] = "";
sprintf(suffix, "%010lld", current_block_number); size_t size = sprintf(suffix, "%010lld", current_block_number);
std::string expected_path = block_prefix + suffix; std::string expected_path = block_prefix + std::string(suffix, size);
std::cerr << "\t" << path_created << ": Ephemeral node has been created with an unexpected path (expected something like " std::cerr << "\t" << path_created << ": Ephemeral node has been created with an unexpected path (expected something like "
<< expected_path << ")." << std::endl; << expected_path << ")." << std::endl;
return false; return false;

View File

@ -238,9 +238,9 @@ std::string currentDateTime()
tstruct = *localtime(&now); tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format // for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tstruct); size_t size = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tstruct);
return buf; return std::string(buf, size);
} }