aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/HashingWriteBuffer.cpp
blob: d2461d4f52cda60815459424fe54e80364dff75c (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
#include <IO/HashingWriteBuffer.h>
#include <iomanip>


namespace DB
{

/// computation of the hash depends on the partitioning of blocks
/// so you need to compute a hash of n complete pieces and one incomplete
template <typename Buffer>
void IHashingBuffer<Buffer>::calculateHash(DB::BufferBase::Position data, size_t len)
{
    if (len)
    {
        /// if the data is less than `block_size`, then put them into buffer and calculate hash later
        if (block_pos + len < block_size)
        {
            memcpy(&BufferWithOwnMemory<Buffer>::memory[block_pos], data, len);
            block_pos += len;
        }
        else
        {
            /// if something is already written to the buffer, then we'll add it
            if (block_pos)
            {
                size_t n = block_size - block_pos;
                memcpy(&BufferWithOwnMemory<Buffer>::memory[block_pos], data, n);
                append(&BufferWithOwnMemory<Buffer>::memory[0]);
                len -= n;
                data += n;
                block_pos = 0;
            }

            while (len >= block_size)
            {
                append(data);
                len -= block_size;
                data += block_size;
            }

            /// write the remainder to its buffer
            if (len)
            {
                memcpy(&BufferWithOwnMemory<Buffer>::memory[0], data, len);
                block_pos = len;
            }
        }
    }
}

template class IHashingBuffer<DB::ReadBuffer>;
template class IHashingBuffer<DB::WriteBuffer>;

}