2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/HashingWriteBuffer.h>
|
2014-06-05 16:59:30 +00:00
|
|
|
#include <iomanip>
|
2017-06-19 21:06:46 +00:00
|
|
|
|
2017-01-16 19:47:11 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-03-25 20:12:56 +00:00
|
|
|
/// computation of the hash depends on the partitioning of blocks
|
|
|
|
/// so you need to compute a hash of n complete pieces and one incomplete
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename Buffer>
|
2017-01-16 19:47:11 +00:00
|
|
|
void IHashingBuffer<Buffer>::calculateHash(DB::BufferBase::Position data, size_t len)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-01-16 19:47:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (len >= block_size)
|
|
|
|
{
|
|
|
|
append(data);
|
|
|
|
len -= block_size;
|
|
|
|
data += block_size;
|
|
|
|
}
|
2017-01-16 19:47:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// write the remainder to its buffer
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
memcpy(&BufferWithOwnMemory<Buffer>::memory[0], data, len);
|
|
|
|
block_pos = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template class IHashingBuffer<DB::ReadBuffer>;
|
|
|
|
template class IHashingBuffer<DB::WriteBuffer>;
|
|
|
|
|
|
|
|
}
|