aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/MergeTree/ReplicatedMergeTreePartHeader.cpp
blob: 24d907dbad6c8d787958b7bd74f9b5bb452915a3 (plain) (blame)
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
#include <Storages/MergeTree/ReplicatedMergeTreePartHeader.h>
#include <Core/NamesAndTypes.h>
#include <IO/WriteHelpers.h>
#include <IO/ReadBufferFromString.h>
#include <Common/SipHash.h>
#include <Common/StringUtils/StringUtils.h>

namespace DB
{

static std::array<char, 16> getSipHash(const String & str)
{
    SipHash hash;
    hash.update(str.data(), str.size());
    return getSipHash128AsArray(hash);
}

ReplicatedMergeTreePartHeader ReplicatedMergeTreePartHeader::fromColumnsAndChecksumsZNodes(
    const String & columns_znode, const String & checksums_znode)
{
    auto columns_hash = getSipHash(columns_znode);
    auto checksums = MinimalisticDataPartChecksums::deserializeFrom(checksums_znode);
    return ReplicatedMergeTreePartHeader(std::move(columns_hash), std::move(checksums));
}

ReplicatedMergeTreePartHeader ReplicatedMergeTreePartHeader::fromColumnsAndChecksums(
    const NamesAndTypesList & columns,
    const MergeTreeDataPartChecksums & full_checksums)
{
    MinimalisticDataPartChecksums checksums;
    checksums.computeTotalChecksums(full_checksums);
    return ReplicatedMergeTreePartHeader(getSipHash(columns.toString()), std::move(checksums));
}

void ReplicatedMergeTreePartHeader::read(ReadBuffer & in)
{
    in >> "part header format version: 1\n";
    in.readStrict(columns_hash.data(), columns_hash.size());
    checksums.deserializeWithoutHeader(in);
}

ReplicatedMergeTreePartHeader ReplicatedMergeTreePartHeader::fromString(const String & str)
{
    ReadBufferFromString in(str);
    ReplicatedMergeTreePartHeader result;
    result.read(in);
    return result;
}

void ReplicatedMergeTreePartHeader::write(WriteBuffer & out) const
{
    writeString("part header format version: 1\n", out);
    out.write(columns_hash.data(), columns_hash.size());
    checksums.serializeWithoutHeader(out);
}

String ReplicatedMergeTreePartHeader::toString() const
{
    WriteBufferFromOwnString out;
    write(out);
    return out.str();
}

}