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

113 lines
2.5 KiB
C++
Raw Normal View History

2016-01-28 01:00:27 +00:00
#include <DB/Storages/MergeTree/ReshardingJob.h>
#include <DB/IO/ReadBufferFromString.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/IO/WriteBufferFromString.h>
#include <DB/IO/WriteHelpers.h>
2016-01-28 01:00:42 +00:00
#include <DB/Parsers/ParserQuery.h>
#include <DB/Parsers/ExpressionListParsers.h>
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:27 +00:00
ReshardingJob::ReshardingJob(const std::string & serialized_job)
{
ReadBufferFromString buf(serialized_job);
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);
IParser::Pos pos = expr.data();
IParser::Pos max_parsed_pos = pos;
const char * end = pos + expr.size();
ParserExpressionWithOptionalAlias parser(false);
Expected expected = "";
if (!parser.parse(pos, end, sharding_key_expr, max_parsed_pos, expected))
throw Exception("ReshardingJob: Internal error", ErrorCodes::LOGICAL_ERROR);
2016-03-01 17:47:53 +00:00
readBinary(coordinator_id, buf);
readBinary(block_number, buf);
2016-01-28 01:00:27 +00:00
while (!buf.eof())
{
std::string path;
readBinary(path, buf);
UInt64 weight;
readBinary(weight, buf);
paths.emplace_back(path, weight);
}
}
ReshardingJob::ReshardingJob(const std::string & database_name_, const std::string & table_name_,
const std::string & partition_, const WeightedZooKeeperPaths & paths_,
2016-03-01 17:47:53 +00:00
const ASTPtr & sharding_key_expr_, const std::string & coordinator_id_)
2016-01-28 01:00:27 +00:00
: database_name(database_name_),
table_name(table_name_),
partition(partition_),
paths(paths_),
2016-03-01 17:47:53 +00:00
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-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);
2016-01-28 01:00:42 +00:00
writeBinary(queryToString(sharding_key_expr), buf);
2016-03-01 17:47:53 +00:00
writeBinary(coordinator_id, buf);
writeBinary(block_number, buf);
2016-01-28 01:00:42 +00:00
2016-01-28 01:00:27 +00:00
for (const auto & path : paths)
{
writeBinary(path.first, buf);
writeBinary(path.second, buf);
}
buf.next();
return serialized_job;
}
2016-03-01 17:47:53 +00:00
bool ReshardingJob::isCoordinated() const
{
return !coordinator_id.empty();
}
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-01-28 01:00:27 +00:00
}