forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommandReadChecksums.cpp
More file actions
85 lines (73 loc) · 3.03 KB
/
Copy pathCommandReadChecksums.cpp
File metadata and controls
85 lines (73 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#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());
auto in = disk_ptr->readFile(path, getReadSettings());
MergeTreeDataPartChecksums checksums;
assertString("checksums format version: ", *in);
size_t format_version = 0;
readText(format_version, *in);
assertChar('\n', *in);
if (!checksums.read(*in, format_version))
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"'{}' is not a valid checksums file or uses an unsupported (too old) format", path_arg);
assertEOF(*in);
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>();
}
}