| 1 | #include <IO/HashingWriteBuffer.h> |
|---|---|
| 2 | #include <iomanip> |
| 3 | |
| 4 | |
| 5 | namespace DB |
| 6 | { |
| 7 | |
| 8 | /// computation of the hash depends on the partitioning of blocks |
| 9 | /// so you need to compute a hash of n complete pieces and one incomplete |
| 10 | template <typename Buffer> |
| 11 | void IHashingBuffer<Buffer>::calculateHash(DB::BufferBase::Position data, size_t len) |
| 12 | { |
| 13 | if (len) |
| 14 | { |
| 15 | /// if the data is less than `block_size`, then put them into buffer and calculate hash later |
| 16 | if (block_pos + len < block_size) |
| 17 | { |
| 18 | memcpy(&BufferWithOwnMemory<Buffer>::memory[block_pos], data, len); |
| 19 | block_pos += len; |
| 20 | } |
| 21 | else |
| 22 | { |
| 23 | /// if something is already written to the buffer, then we'll add it |
| 24 | if (block_pos) |
| 25 | { |
| 26 | size_t n = block_size - block_pos; |
| 27 | memcpy(&BufferWithOwnMemory<Buffer>::memory[block_pos], data, n); |
| 28 | append(&BufferWithOwnMemory<Buffer>::memory[0]); |
| 29 | len -= n; |
| 30 | data += n; |
| 31 | block_pos = 0; |
| 32 | } |
| 33 | |
| 34 | while (len >= block_size) |
| 35 | { |
| 36 | append(data); |
| 37 | len -= block_size; |
| 38 | data += block_size; |
| 39 | } |
| 40 | |
| 41 | /// write the remainder to its buffer |
| 42 | if (len) |
| 43 | { |
| 44 | memcpy(&BufferWithOwnMemory<Buffer>::memory[0], data, len); |
| 45 | block_pos = len; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | template class IHashingBuffer<DB::ReadBuffer>; |
| 52 | template class IHashingBuffer<DB::WriteBuffer>; |
| 53 | |
| 54 | } |
| 55 |