aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/HashingReadBuffer.h
blob: a0a029e6f854ea5d954bed2b52620d132a23283e (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
#pragma once

#include <IO/HashingWriteBuffer.h>
#include <IO/ReadBuffer.h>

namespace DB
{

/*
 * Calculates the hash from the read data. When reading, the data is read from the nested ReadBuffer.
 * Small pieces are copied into its own memory.
 */
class HashingReadBuffer : public IHashingBuffer<ReadBuffer>
{
public:
    explicit HashingReadBuffer(ReadBuffer & in_, size_t block_size_ = DBMS_DEFAULT_HASHING_BLOCK_SIZE)
        : IHashingBuffer<ReadBuffer>(block_size_), in(in_)
    {
        working_buffer = in.buffer();
        pos = in.position();
        hashing_begin = pos;
    }

    uint128 getHash()
    {
        if (pos > hashing_begin)
        {
            calculateHash(hashing_begin, pos - hashing_begin);
            hashing_begin = pos;
        }
        return IHashingBuffer<ReadBuffer>::getHash();
    }

private:
    bool nextImpl() override
    {
        if (pos > hashing_begin)
            calculateHash(hashing_begin, pos - hashing_begin);

        in.position() = pos;
        bool res = in.next();
        working_buffer = in.buffer();

        // `pos` may be different from working_buffer.begin() when using sophisticated ReadBuffers.
        pos = in.position();
        hashing_begin = pos;

        return res;
    }

    ReadBuffer & in;
    BufferBase::Position hashing_begin;
};

}