1#include <Storages/MergeTree/ReplicatedMergeTreePartHeader.h>
2#include <Core/NamesAndTypes.h>
3#include <IO/WriteHelpers.h>
4#include <IO/ReadBufferFromString.h>
5#include <Common/SipHash.h>
6#include <Common/StringUtils/StringUtils.h>
7
8namespace DB
9{
10
11static std::array<char, 16> getSipHash(const String & str)
12{
13 SipHash hash;
14 hash.update(str.data(), str.size());
15 std::array<char, 16> result;
16 hash.get128(result.data());
17 return result;
18}
19
20ReplicatedMergeTreePartHeader ReplicatedMergeTreePartHeader::fromColumnsAndChecksumsZNodes(
21 const String & columns_znode, const String & checksums_znode)
22{
23 auto columns_hash = getSipHash(columns_znode);
24 auto checksums = MinimalisticDataPartChecksums::deserializeFrom(checksums_znode);
25 return ReplicatedMergeTreePartHeader(std::move(columns_hash), std::move(checksums));
26}
27
28ReplicatedMergeTreePartHeader ReplicatedMergeTreePartHeader::fromColumnsAndChecksums(
29 const NamesAndTypesList & columns,
30 const MergeTreeDataPartChecksums & full_checksums)
31{
32 MinimalisticDataPartChecksums checksums;
33 checksums.computeTotalChecksums(full_checksums);
34 return ReplicatedMergeTreePartHeader(getSipHash(columns.toString()), std::move(checksums));
35}
36
37void ReplicatedMergeTreePartHeader::read(ReadBuffer & in)
38{
39 in >> "part header format version: 1\n";
40 in.readStrict(columns_hash.data(), columns_hash.size());
41 checksums.deserializeWithoutHeader(in);
42}
43
44ReplicatedMergeTreePartHeader ReplicatedMergeTreePartHeader::fromString(const String & str)
45{
46 ReadBufferFromString in(str);
47 ReplicatedMergeTreePartHeader result;
48 result.read(in);
49 return result;
50}
51
52void ReplicatedMergeTreePartHeader::write(WriteBuffer & out) const
53{
54 writeString("part header format version: 1\n", out);
55 out.write(columns_hash.data(), columns_hash.size());
56 checksums.serializeWithoutHeader(out);
57}
58
59String ReplicatedMergeTreePartHeader::toString() const
60{
61 WriteBufferFromOwnString out;
62 write(out);
63 return out.str();
64}
65
66}
67