ClickHouse/dbms/src/Storages/MergeTree/ReshardingJob.cpp

120 lines
2.8 KiB
C++
Raw Normal View History

#include <Storages/MergeTree/ReshardingJob.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/queryToString.h>
2016-12-12 07:24:56 +00:00
2016-01-28 01:00:27 +00:00
namespace DB
{
2016-01-28 01:00:42 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
2016-01-28 01:00:42 +00:00
}
2016-01-28 01:00:27 +00:00
ReshardingJob::ReshardingJob(const std::string & serialized_job)
{
ReadBufferFromString buf{serialized_job};
2016-01-28 01:00:27 +00:00
readBinary(database_name, buf);
readBinary(table_name, buf);
readBinary(partition, buf);
2016-01-28 01:00:42 +00:00
std::string expr;
readBinary(expr, buf);
2016-01-28 01:00:42 +00:00
IParser::Pos pos = expr.data();
IParser::Pos max_parsed_pos = pos;
const char * end = pos + expr.size();
2016-01-28 01:00:42 +00:00
ParserExpression parser;
Expected expected = "";
if (!parser.parse(pos, end, sharding_key_expr, max_parsed_pos, expected))
throw Exception{"ReshardingJob: cannot parse sharding expression.", ErrorCodes::LOGICAL_ERROR};
2016-01-28 01:00:42 +00:00
readBinary(coordinator_id, buf);
readVarUInt(block_number, buf);
readBinary(do_copy, buf);
2016-03-01 17:47:53 +00:00
size_t s;
readVarUInt(s, buf);
2016-03-25 11:48:45 +00:00
for (size_t i = 0; i < s; ++i)
{
std::string path;
readBinary(path, buf);
2016-01-28 01:00:27 +00:00
UInt64 weight;
readVarUInt(weight, buf);
2016-01-28 01:00:27 +00:00
paths.emplace_back(path, weight);
}
2016-01-28 01:00:27 +00:00
}
ReshardingJob::ReshardingJob(const std::string & database_name_, const std::string & table_name_,
const std::string & partition_, const WeightedZooKeeperPaths & paths_,
const ASTPtr & sharding_key_expr_, const std::string & coordinator_id_)
: database_name{database_name_},
table_name{table_name_},
partition{partition_},
paths{paths_},
sharding_key_expr{sharding_key_expr_},
coordinator_id{coordinator_id_}
2016-01-28 01:00:27 +00:00
{
}
2016-03-01 17:47:53 +00:00
ReshardingJob::operator bool() const
{
return !database_name.empty()
&& !table_name.empty()
&& !partition.empty()
&& !paths.empty()
&& (storage != nullptr);
2016-03-01 17:47:53 +00:00
}
2016-01-28 01:00:27 +00:00
std::string ReshardingJob::toString() const
{
std::string serialized_job;
WriteBufferFromString buf{serialized_job};
writeBinary(database_name, buf);
writeBinary(table_name, buf);
writeBinary(partition, buf);
writeBinary(queryToString(sharding_key_expr), buf);
writeBinary(coordinator_id, buf);
writeVarUInt(block_number, buf);
writeBinary(do_copy, buf);
writeVarUInt(paths.size(), buf);
for (const auto & path : paths)
{
writeBinary(path.first, buf);
writeVarUInt(path.second, buf);
}
buf.next();
return serialized_job;
2016-01-28 01:00:27 +00:00
}
2016-03-01 17:47:53 +00:00
bool ReshardingJob::isCoordinated() const
{
return !coordinator_id.empty();
2016-03-01 17:47:53 +00:00
}
void ReshardingJob::clear()
{
database_name.clear();
table_name.clear();
partition.clear();
paths.clear();
coordinator_id.clear();
storage = nullptr;
block_number = 0;
is_aborted = false;
2016-03-01 17:47:53 +00:00
}
2016-01-28 01:00:27 +00:00
}