2018-07-11 12:43:55 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeMutationEntry.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
|
|
|
|
#include <Poco/File.h>
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2018-07-11 12:43:55 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
MergeTreeMutationEntry::MergeTreeMutationEntry(MutationCommands commands_, DiskPtr disk_, const String & path_prefix_, Int64 tmp_number)
|
2018-07-11 12:43:55 +00:00
|
|
|
: create_time(time(nullptr))
|
|
|
|
, commands(std::move(commands_))
|
2020-02-27 16:47:40 +00:00
|
|
|
, disk(std::move(disk_))
|
2018-07-11 12:43:55 +00:00
|
|
|
, path_prefix(path_prefix_)
|
|
|
|
, file_name("tmp_mutation_" + toString(tmp_number) + ".txt")
|
|
|
|
, is_temp(true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-02-27 16:47:40 +00:00
|
|
|
auto out = disk->writeFile(path_prefix + file_name);
|
|
|
|
*out << "format version: 1\n"
|
2018-07-11 12:43:55 +00:00
|
|
|
<< "create time: " << LocalDateTime(create_time) << "\n";
|
2020-02-27 16:47:40 +00:00
|
|
|
*out << "commands: ";
|
|
|
|
commands.writeText(*out);
|
|
|
|
*out << "\n";
|
|
|
|
out->sync();
|
2018-07-11 12:43:55 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
removeFile();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergeTreeMutationEntry::commit(Int64 block_number_)
|
|
|
|
{
|
|
|
|
block_number = block_number_;
|
|
|
|
String new_file_name = "mutation_" + toString(block_number) + ".txt";
|
2020-02-27 16:47:40 +00:00
|
|
|
disk->moveFile(path_prefix + file_name, path_prefix + new_file_name);
|
2018-07-11 12:43:55 +00:00
|
|
|
is_temp = false;
|
|
|
|
file_name = new_file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergeTreeMutationEntry::removeFile()
|
|
|
|
{
|
|
|
|
if (!file_name.empty())
|
|
|
|
{
|
2020-02-27 16:47:40 +00:00
|
|
|
if (!disk->exists(path_prefix + file_name))
|
2018-07-11 12:43:55 +00:00
|
|
|
return;
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
disk->remove(path_prefix + file_name);
|
2018-07-11 12:43:55 +00:00
|
|
|
file_name.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
MergeTreeMutationEntry::MergeTreeMutationEntry(DiskPtr disk_, const String & path_prefix_, const String & file_name_)
|
|
|
|
: disk(std::move(disk_))
|
|
|
|
, path_prefix(path_prefix_)
|
2018-07-11 12:43:55 +00:00
|
|
|
, file_name(file_name_)
|
|
|
|
, is_temp(false)
|
|
|
|
{
|
|
|
|
ReadBufferFromString file_name_buf(file_name);
|
|
|
|
file_name_buf >> "mutation_" >> block_number >> ".txt";
|
|
|
|
assertEOF(file_name_buf);
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
auto buf = disk->readFile(path_prefix + file_name);
|
2018-07-11 12:43:55 +00:00
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
*buf >> "format version: 1\n";
|
2018-07-11 12:43:55 +00:00
|
|
|
|
|
|
|
LocalDateTime create_time_dt;
|
2020-02-27 16:47:40 +00:00
|
|
|
*buf >> "create time: " >> create_time_dt >> "\n";
|
2018-07-11 12:43:55 +00:00
|
|
|
create_time = create_time_dt;
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
*buf >> "commands: ";
|
|
|
|
commands.readText(*buf);
|
|
|
|
*buf >> "\n";
|
2018-07-11 12:43:55 +00:00
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
assertEOF(*buf);
|
2018-07-11 12:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MergeTreeMutationEntry::~MergeTreeMutationEntry()
|
|
|
|
{
|
|
|
|
if (is_temp && startsWith(file_name, "tmp_"))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
removeFile();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|