2022-05-25 18:28:46 +00:00
|
|
|
#include "BoundedReadBuffer.h"
|
2023-01-19 17:44:29 +00:00
|
|
|
#include <IO/SwapHelper.h>
|
2022-05-25 18:28:46 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2024-02-13 11:02:46 +00:00
|
|
|
BoundedReadBuffer::BoundedReadBuffer(std::unique_ptr<SeekableReadBuffer> impl_)
|
|
|
|
: ReadBufferFromFileDecorator(std::move(impl_))
|
2022-05-25 20:45:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundedReadBuffer::setReadUntilPosition(size_t position)
|
|
|
|
{
|
|
|
|
read_until_position = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundedReadBuffer::setReadUntilEnd()
|
|
|
|
{
|
|
|
|
read_until_position.reset();
|
|
|
|
}
|
2022-05-25 18:28:46 +00:00
|
|
|
|
2022-05-30 18:35:31 +00:00
|
|
|
off_t BoundedReadBuffer::getPosition()
|
|
|
|
{
|
|
|
|
return file_offset_of_buffer_end - (working_buffer.end() - pos);
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:28:46 +00:00
|
|
|
bool BoundedReadBuffer::nextImpl()
|
|
|
|
{
|
|
|
|
if (read_until_position && file_offset_of_buffer_end == *read_until_position)
|
|
|
|
return false;
|
|
|
|
|
2023-01-19 17:44:29 +00:00
|
|
|
bool result;
|
|
|
|
{
|
|
|
|
SwapHelper swap(*this, *impl);
|
|
|
|
result = impl->next();
|
|
|
|
}
|
2023-01-20 15:25:02 +00:00
|
|
|
chassert(file_offset_of_buffer_end + available() == impl->getFileOffsetOfBufferEnd());
|
2022-06-13 14:32:07 +00:00
|
|
|
if (result && read_until_position)
|
2022-05-25 18:28:46 +00:00
|
|
|
{
|
|
|
|
size_t remaining_size_to_read = *read_until_position - file_offset_of_buffer_end;
|
|
|
|
if (working_buffer.size() > remaining_size_to_read)
|
|
|
|
{
|
|
|
|
/// file: [______________________________]
|
|
|
|
/// working buffer: [_______________]
|
|
|
|
/// ^
|
|
|
|
/// read_until_position
|
|
|
|
/// ^
|
|
|
|
/// file_offset_of_buffer_end
|
|
|
|
working_buffer.resize(remaining_size_to_read);
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 18:35:31 +00:00
|
|
|
file_offset_of_buffer_end += available();
|
2022-05-25 18:28:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t BoundedReadBuffer::seek(off_t off, int whence)
|
|
|
|
{
|
|
|
|
swap(*impl);
|
|
|
|
auto result = impl->seek(off, whence);
|
|
|
|
swap(*impl);
|
|
|
|
|
2023-01-19 17:44:29 +00:00
|
|
|
file_offset_of_buffer_end = impl->getFileOffsetOfBufferEnd();
|
2022-05-25 18:28:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|