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

121 lines
2.6 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-12-12 07:24:56 +00:00
#include <DB/Parsers/queryToString.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)
{
2016-03-25 11:48:45 +00:00
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);
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))
2016-03-25 11:48:45 +00:00
throw Exception{"ReshardingJob: Internal error", ErrorCodes::LOGICAL_ERROR};
2016-01-28 01:00:42 +00:00
2016-03-01 17:47:53 +00:00
readBinary(coordinator_id, buf);
2016-03-25 11:48:45 +00:00
readVarUInt(block_number, buf);
readBinary(do_copy, buf);
2016-03-01 17:47:53 +00:00
2016-03-25 11:48:45 +00:00
size_t s;
readVarUInt(s, buf);
for (size_t i = 0; i < s; ++i)
2016-01-28 01:00:27 +00:00
{
std::string path;
readBinary(path, buf);
UInt64 weight;
2016-03-25 11:48:45 +00:00
readVarUInt(weight, buf);
2016-01-28 01:00:27 +00:00
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-03-25 11:48:45 +00:00
: 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-01-28 01:00:27 +00:00
std::string ReshardingJob::toString() const
{
std::string serialized_job;
2016-03-25 11:48:45 +00:00
WriteBufferFromString buf{serialized_job};
2016-01-28 01:00:27 +00:00
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);
2016-03-25 11:48:45 +00:00
writeVarUInt(block_number, buf);
writeBinary(do_copy, buf);
2016-01-28 01:00:42 +00:00
2016-03-25 11:48:45 +00:00
writeVarUInt(paths.size(), buf);
2016-01-28 01:00:27 +00:00
for (const auto & path : paths)
{
writeBinary(path.first, buf);
2016-03-25 11:48:45 +00:00
writeVarUInt(path.second, buf);
2016-01-28 01:00:27 +00:00
}
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
}