Files
datastore/programs/disks/CommandReadChecksums.cpp
T

82 lines
3.0 KiB
C++

#include <ICommand.h>
#include <IO/ReadBufferFromFileBase.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/WriteHelpers.h>
#include <Storages/MergeTree/MergeTreeDataPartChecksum.h>
#include <Common/logger_useful.h>
#include <base/hex.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
class CommandReadChecksums final : public ICommand
{
public:
CommandReadChecksums() : ICommand("CommandReadChecksums")
{
command_name = "read-checksums";
description = "Read a `checksums.txt` file of a MergeTree data part and print it as a human-readable table";
options_description.add_options()
("path", po::value<String>(), "path to the `checksums.txt` file (mandatory, positional)");
positional_options_description.add("path", 1);
}
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
{
const auto & disk = client.getCurrentDiskWithPath();
String path_arg = getValueFromCommandLineOptionsThrow<String>(options, "path");
String path = disk.getRelativeFromRoot(path_arg);
auto disk_ptr = disk.getDisk();
if (disk_ptr->existsDirectory(path))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "cannot read checksums of '{}': Is a directory", path_arg);
if (!disk_ptr->existsFile(path))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Path {} on disk {} doesn't exist", path_arg, disk_ptr->getName());
LOG_INFO(log, "Reading checksums from '{}' at disk '{}'", path_arg, disk_ptr->getName());
/// Read the whole file and deserialize it via the canonical entry point, so that the parsing
/// and error behavior matches the rest of the codebase (e.g. `IMergeTreeDataPart::loadChecksums`).
String contents;
{
auto in = disk_ptr->readFile(path, getReadSettings());
readStringUntilEOF(contents, *in);
}
auto checksums = MergeTreeDataPartChecksums::deserializeFrom(contents);
WriteBufferFromFileDescriptor out(STDOUT_FILENO);
writeString("name\tfile_size\tfile_hash\tuncompressed_size\tuncompressed_hash\n", out);
/// The order of `files` is deterministic (lexicographical by name).
for (const auto & [name, checksum] : checksums.files)
{
writeString(name, out);
out.write('\t');
writeIntText(checksum.file_size, out);
out.write('\t');
writeString(getHexUIntLowercase(checksum.file_hash), out);
if (checksum.is_compressed)
{
out.write('\t');
writeIntText(checksum.uncompressed_size, out);
out.write('\t');
writeString(getHexUIntLowercase(checksum.uncompressed_hash), out);
}
out.write('\n');
}
out.finalize();
}
};
CommandPtr makeCommandReadChecksums()
{
return std::make_shared<DB::CommandReadChecksums>();
}
}