Skip to content

Commit faf632e

Browse files
authored
Merge pull request #441 from ilandry/clickhouse_client_move
clickhouse client move support
2 parents 67398c7 + 93fa35c commit faf632e

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

clickhouse/client.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ void Client::Impl::SendBlockData(const Block& block) {
446446
if (compression_ == CompressionState::Enable) {
447447
std::unique_ptr<OutputStream> compressed_output = std::make_unique<CompressedOutput>(output_.get(), options_.max_compression_chunk_size, options_.compression_method);
448448
BufferedOutput buffered(std::move(compressed_output), options_.max_compression_chunk_size);
449-
449+
450450
WriteBlock(block, buffered);
451451
} else {
452452
WriteBlock(block, *output_);
@@ -902,7 +902,7 @@ bool Client::Impl::ReadBlock(InputStream& input, Block* block) {
902902
if (!WireFormat::ReadString(input, &type)) {
903903
return false;
904904
}
905-
905+
906906
if (server_info_.revision >= DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION) {
907907
uint8_t custom_format_len;
908908
if (!WireFormat::ReadFixed(input, &custom_format_len)) {
@@ -911,7 +911,7 @@ bool Client::Impl::ReadBlock(InputStream& input, Block* block) {
911911
if (custom_format_len > 0) {
912912
throw UnimplementedError(std::string("unsupported custom serialization"));
913913
}
914-
}
914+
}
915915

916916
if (ColumnRef col = CreateColumnByType(type, create_column_settings)) {
917917
if (num_rows && !col->Load(&input, num_rows)) {
@@ -1101,7 +1101,7 @@ void Client::Impl::SendQuery(const Query& query, bool finalize) {
11011101
}
11021102
WireFormat::WriteString(*output_, std::string()); // empty string after last param
11031103
}
1104-
1104+
11051105
if (finalize) {
11061106
FinalizeQuery();
11071107
}
@@ -1277,21 +1277,23 @@ void Client::Impl::RetryGuard(std::function<void()> func) {
12771277
}
12781278

12791279
Client::Client(const ClientOptions& opts)
1280-
: options_(opts)
1281-
, impl_(new Impl(opts))
1280+
: impl_(new Impl(opts))
12821281
{
12831282
}
12841283

12851284
Client::Client(const ClientOptions& opts,
12861285
std::unique_ptr<SocketFactory> socket_factory)
1287-
: options_(opts)
1288-
, impl_(new Impl(opts, std::move(socket_factory)))
1286+
: impl_(new Impl(opts, std::move(socket_factory)))
12891287
{
12901288
}
12911289

12921290
Client::~Client()
12931291
{ }
12941292

1293+
1294+
Client::Client(Client&&) noexcept = default;
1295+
Client& Client::operator=(Client&&) noexcept = default;
1296+
12951297
void Client::Execute(const Query& query) {
12961298
impl_->ExecuteQuery(query);
12971299
}

clickhouse/client.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ class Client {
246246
std::unique_ptr<SocketFactory> socket_factory);
247247
~Client();
248248

249+
// movable only
250+
Client(Client&&) noexcept;
251+
Client& operator=(Client&&) noexcept;
252+
Client(const Client&) = delete;
253+
Client& operator=(const Client&) = delete;
254+
249255
/// Intends for execute arbitrary queries.
250256
void Execute(const Query& query);
251257

@@ -346,8 +352,6 @@ class Client {
346352
static Version GetVersion();
347353

348354
private:
349-
const ClientOptions options_;
350-
351355
class Impl;
352356
std::unique_ptr<Impl> impl_;
353357
};

ut/client_ut.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ TEST_P(ClientCase, ClientName) {
19981998

19991999
FlushLogs();
20002000

2001-
std::string query_log_query
2001+
std::string query_log_query
20022002
= "SELECT CAST(client_name, 'String') FROM system.query_log WHERE query_id = '" + query_id + "'";
20032003

20042004
size_t total_rows = 0;
@@ -2011,3 +2011,54 @@ TEST_P(ClientCase, ClientName) {
20112011
});
20122012
ASSERT_GT(total_rows, 0UL) << "Query with query_id " << query_id << " is not found";
20132013
}
2014+
2015+
TEST_P(ClientCase, ClientMoveConstructor) {
2016+
Client client{std::move(*client_.get())};
2017+
2018+
size_t total_rows = 0;
2019+
client.Select("SELECT 'foobar'", [&total_rows](const Block& block) {
2020+
total_rows += block.GetRowCount();
2021+
if (block.GetRowCount() == 0) {
2022+
return;
2023+
}
2024+
2025+
ASSERT_EQ(1U, block.GetColumnCount());
2026+
auto col = block[0]->As<ColumnString>();
2027+
ASSERT_TRUE(col);
2028+
ASSERT_EQ(1U, col->Size());
2029+
EXPECT_EQ("foobar", (*col)[0]);
2030+
});
2031+
ASSERT_EQ(total_rows, 1U);
2032+
}
2033+
2034+
TEST_P(ClientCase, ClientMoveAssign) {
2035+
client_->Execute("SET param_session_id = 'initial'");
2036+
2037+
ClientOptions opt = GetParam();
2038+
Client client{opt};
2039+
client.Execute("SET param_session_id = 'new'");
2040+
2041+
size_t total_rows = 0;
2042+
client.Select("SELECT {session_id:String}", [&total_rows](const Block& block) {
2043+
total_rows += block.GetRowCount();
2044+
for (size_t i = 0; i < block.GetRowCount(); ++i) {
2045+
EXPECT_EQ("new", block[0]->AsStrict<ColumnString>()->At(i));
2046+
2047+
}
2048+
});
2049+
ASSERT_EQ(total_rows, 1U);
2050+
2051+
client = std::move(*client_.get());
2052+
2053+
total_rows = 0;
2054+
client.Select("SELECT {session_id:String}", [&total_rows](const Block& block) {
2055+
total_rows += block.GetRowCount();
2056+
for (size_t i = 0; i < block.GetRowCount(); ++i) {
2057+
EXPECT_EQ("initial", block[0]->AsStrict<ColumnString>()->At(i));
2058+
// ^
2059+
// The value has changed to the session id of the original client
2060+
}
2061+
});
2062+
ASSERT_EQ(total_rows, 1U);
2063+
2064+
}

0 commit comments

Comments
 (0)