ClickHouse/src/IO/LimitReadBuffer.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.6 KiB
C++
Raw Normal View History

2017-08-14 04:42:04 +00:00
#include <IO/LimitReadBuffer.h>
#include <Common/Exception.h>
2017-08-14 04:42:04 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int LIMIT_EXCEEDED;
2023-02-22 01:34:03 +00:00
extern const int CANNOT_READ_ALL_DATA;
}
2017-08-14 04:42:04 +00:00
bool LimitReadBuffer::nextImpl()
{
2024-03-03 13:22:40 +00:00
chassert(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.
in->position() = position();
2017-08-14 04:42:04 +00:00
if (bytes >= limit)
{
2023-02-22 16:54:35 +00:00
if (exact_limit && bytes == *exact_limit)
2023-02-22 01:34:03 +00:00
return false;
2023-02-22 16:54:35 +00:00
if (exact_limit && bytes != *exact_limit)
throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected data, got {} bytes, expected {}", bytes, *exact_limit);
2023-02-22 01:34:03 +00:00
if (throw_exception)
throw Exception(ErrorCodes::LIMIT_EXCEEDED, "Limit for LimitReadBuffer exceeded: {}", exception_message);
2023-02-22 01:34:03 +00:00
return false;
}
if (!in->next())
2021-02-04 14:46:46 +00:00
{
2023-02-22 16:54:35 +00:00
if (exact_limit && bytes != *exact_limit)
throw Exception(ErrorCodes::CANNOT_READ_ALL_DATA, "Unexpected EOF, got {} of {} bytes", bytes, *exact_limit);
/// Clearing the buffer with existing data.
2024-03-03 13:22:40 +00:00
BufferBase::set(in->position(), 0, 0);
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
2024-03-03 13:22:40 +00:00
BufferBase::set(in->position(), std::min(in->available(), limit - bytes), 0);
2017-08-14 04:42:04 +00:00
return true;
}
2024-03-03 13:22:40 +00:00
LimitReadBuffer::LimitReadBuffer(ReadBuffer * in_, bool owns, size_t limit_, bool throw_exception_,
2023-02-22 16:54:35 +00:00
std::optional<size_t> exact_limit_, std::string exception_message_)
: ReadBuffer(in_ ? in_->position() : nullptr, 0)
, in(in_)
, owns_in(owns)
, limit(limit_)
, throw_exception(throw_exception_)
2023-02-22 01:34:03 +00:00
, exact_limit(exact_limit_)
, exception_message(std::move(exception_message_))
2017-08-15 20:14:15 +00:00
{
2024-03-03 13:22:40 +00:00
chassert(in);
2017-08-15 20:14:15 +00:00
2024-03-03 13:22:40 +00:00
BufferBase::set(in->position(), std::min(in->available(), limit), 0);
}
2024-03-03 13:22:40 +00:00
LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, size_t limit_, bool throw_exception_,
2023-02-22 16:54:35 +00:00
std::optional<size_t> exact_limit_, std::string exception_message_)
2023-02-22 01:34:03 +00:00
: LimitReadBuffer(&in_, false, limit_, throw_exception_, exact_limit_, exception_message_)
{
}
2024-03-03 13:22:40 +00:00
LimitReadBuffer::LimitReadBuffer(std::unique_ptr<ReadBuffer> in_, size_t limit_, bool throw_exception_,
2023-02-22 16:54:35 +00:00
std::optional<size_t> exact_limit_, std::string exception_message_)
2023-02-22 01:34:03 +00:00
: LimitReadBuffer(in_.release(), true, limit_, throw_exception_, exact_limit_, 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())
in->position() = position();
if (owns_in)
delete in;
2017-08-14 04:42:04 +00:00
}
}