2020-09-15 12:28:42 +00:00
|
|
|
#include <IO/MySQLBinlogEventReadBuffer.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-11-19 04:02:43 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-11-16 13:46:36 +00:00
|
|
|
MySQLBinlogEventReadBuffer::MySQLBinlogEventReadBuffer(ReadBuffer & in_, size_t checksum_signature_length_)
|
|
|
|
: ReadBuffer(nullptr, 0, 0), in(in_), checksum_signature_length(checksum_signature_length_)
|
2020-09-15 12:28:42 +00:00
|
|
|
{
|
2020-11-19 04:02:43 +00:00
|
|
|
if (checksum_signature_length > MAX_CHECKSUM_SIGNATURE_LENGTH)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR,
|
|
|
|
"LOGICAL ERROR: checksum_signature_length must be less than MAX_CHECKSUM_SIGNATURE_LENGTH. "
|
|
|
|
"It is a bug.");
|
2020-11-16 13:46:36 +00:00
|
|
|
|
2020-09-15 14:59:21 +00:00
|
|
|
nextIfAtEnd();
|
2020-09-15 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MySQLBinlogEventReadBuffer::nextImpl()
|
|
|
|
{
|
|
|
|
if (hasPendingData())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (in.eof())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (checksum_buff_size == checksum_buff_limit)
|
|
|
|
{
|
2020-11-16 13:46:36 +00:00
|
|
|
if (likely(in.available() > checksum_signature_length))
|
2020-09-15 14:22:23 +00:00
|
|
|
{
|
2020-11-16 13:46:36 +00:00
|
|
|
working_buffer = ReadBuffer::Buffer(in.position(), in.buffer().end() - checksum_signature_length);
|
2020-09-15 14:22:23 +00:00
|
|
|
in.ignore(working_buffer.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-16 13:46:36 +00:00
|
|
|
in.readStrict(checksum_buf, checksum_signature_length);
|
|
|
|
checksum_buff_size = checksum_buff_limit = checksum_signature_length;
|
2020-09-15 12:28:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t index = 0; index < checksum_buff_size - checksum_buff_limit; ++index)
|
|
|
|
checksum_buf[index] = checksum_buf[checksum_buff_limit + index];
|
|
|
|
|
|
|
|
checksum_buff_size -= checksum_buff_limit;
|
2020-11-16 13:46:36 +00:00
|
|
|
size_t read_bytes = checksum_signature_length - checksum_buff_size;
|
|
|
|
in.readStrict(checksum_buf + checksum_buff_size, read_bytes); /// Minimum checksum_signature_length bytes
|
|
|
|
checksum_buff_size = checksum_buff_limit = checksum_signature_length;
|
2020-09-15 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in.eof())
|
|
|
|
return false;
|
|
|
|
|
2020-11-16 13:46:36 +00:00
|
|
|
if (in.available() < checksum_signature_length)
|
2020-09-15 12:28:42 +00:00
|
|
|
{
|
2020-11-16 13:46:36 +00:00
|
|
|
size_t left_move_size = checksum_signature_length - in.available();
|
2020-09-15 12:28:42 +00:00
|
|
|
checksum_buff_limit = checksum_buff_size - left_move_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
working_buffer = ReadBuffer::Buffer(checksum_buf, checksum_buf + checksum_buff_limit);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:13:17 +00:00
|
|
|
MySQLBinlogEventReadBuffer::~MySQLBinlogEventReadBuffer()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-11-19 04:02:43 +00:00
|
|
|
/// ignore last checksum_signature_length bytes
|
2020-09-16 03:13:17 +00:00
|
|
|
nextIfAtEnd();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:28:42 +00:00
|
|
|
}
|