2017-08-14 04:42:04 +00:00
|
|
|
#include <IO/LimitReadBuffer.h>
|
2021-02-04 14:46:46 +00:00
|
|
|
|
2018-08-20 02:23:35 +00:00
|
|
|
#include <Common/Exception.h>
|
2017-08-14 04:42:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-08-20 02:23:35 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LIMIT_EXCEEDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-14 04:42:04 +00:00
|
|
|
bool LimitReadBuffer::nextImpl()
|
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
assert(position() >= in->position());
|
2021-02-04 14:46:46 +00:00
|
|
|
|
2017-08-14 04:42:04 +00:00
|
|
|
/// Let underlying buffer calculate read bytes in `next()` call.
|
2021-02-19 12:51:26 +00:00
|
|
|
in->position() = position();
|
2017-08-14 04:42:04 +00:00
|
|
|
|
2018-08-20 02:23:35 +00:00
|
|
|
if (bytes >= limit)
|
|
|
|
{
|
|
|
|
if (throw_exception)
|
|
|
|
throw Exception("Limit for LimitReadBuffer exceeded: " + exception_message, ErrorCodes::LIMIT_EXCEEDED);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
if (!in->next())
|
2021-02-04 14:46:46 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
working_buffer = in->buffer();
|
2017-08-14 04:42:04 +00:00
|
|
|
return false;
|
2021-02-04 14:46:46 +00:00
|
|
|
}
|
2017-08-14 04:42:04 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
working_buffer = in->buffer();
|
2017-08-14 04:42:04 +00:00
|
|
|
|
|
|
|
if (limit - bytes < working_buffer.size())
|
|
|
|
working_buffer.resize(limit - bytes);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, UInt64 limit_, bool throw_exception_, std::string exception_message_)
|
|
|
|
: ReadBuffer(in_ ? in_->position() : nullptr, 0)
|
|
|
|
, in(in_)
|
|
|
|
, owns_in(owns)
|
|
|
|
, limit(limit_)
|
|
|
|
, throw_exception(throw_exception_)
|
|
|
|
, exception_message(std::move(exception_message_))
|
2017-08-15 20:14:15 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
assert(in);
|
|
|
|
|
|
|
|
size_t remaining_bytes_in_buffer = in->buffer().end() - in->position();
|
2017-08-15 20:14:15 +00:00
|
|
|
if (remaining_bytes_in_buffer > limit)
|
|
|
|
remaining_bytes_in_buffer = limit;
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
working_buffer = Buffer(in->position(), in->position() + remaining_bytes_in_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, UInt64 limit_, bool throw_exception_, std::string exception_message_)
|
|
|
|
: LimitReadBuffer(&in_, false, limit_, throw_exception_, exception_message_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LimitReadBuffer::LimitReadBuffer(std::unique_ptr<ReadBuffer> in_, UInt64 limit_, bool throw_exception_, std::string exception_message_)
|
|
|
|
: LimitReadBuffer(in_.release(), true, limit_, throw_exception_, exception_message_)
|
|
|
|
{
|
2017-08-15 20:14:15 +00:00
|
|
|
}
|
2017-08-14 04:42:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
LimitReadBuffer::~LimitReadBuffer()
|
|
|
|
{
|
|
|
|
/// Update underlying buffer's position in case when limit wasn't reached.
|
2021-02-04 23:14:17 +00:00
|
|
|
if (!working_buffer.empty())
|
2021-02-19 12:51:26 +00:00
|
|
|
in->position() = position();
|
|
|
|
|
|
|
|
if (owns_in)
|
|
|
|
delete in;
|
2017-08-14 04:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|