2020-01-28 12:48:01 +00:00
|
|
|
#include "ReadBufferFromMemory.h"
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-01-28 13:01:08 +00:00
|
|
|
extern const int CANNOT_SEEK_THROUGH_FILE;
|
|
|
|
extern const int SEEK_POSITION_OUT_OF_BOUND;
|
2020-01-28 12:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t ReadBufferFromMemory::seek(off_t offset, int whence)
|
|
|
|
{
|
2020-01-28 13:01:08 +00:00
|
|
|
if (whence == SEEK_SET)
|
|
|
|
{
|
2022-02-04 19:47:46 +00:00
|
|
|
if (offset >= 0 && internal_buffer.begin() + offset < internal_buffer.end())
|
2020-01-28 13:01:08 +00:00
|
|
|
{
|
2022-02-04 19:47:46 +00:00
|
|
|
pos = internal_buffer.begin() + offset;
|
|
|
|
working_buffer = internal_buffer; /// We need to restore `working_buffer` in case the position was at EOF before this seek().
|
|
|
|
return size_t(pos - internal_buffer.begin());
|
2020-01-28 13:01:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(
|
|
|
|
"Seek position is out of bounds. "
|
|
|
|
"Offset: "
|
2022-02-04 19:47:46 +00:00
|
|
|
+ std::to_string(offset) + ", Max: " + std::to_string(size_t(internal_buffer.end() - internal_buffer.begin())),
|
2020-01-28 13:01:08 +00:00
|
|
|
ErrorCodes::SEEK_POSITION_OUT_OF_BOUND);
|
|
|
|
}
|
|
|
|
else if (whence == SEEK_CUR)
|
|
|
|
{
|
|
|
|
Position new_pos = pos + offset;
|
2022-02-04 19:47:46 +00:00
|
|
|
if (new_pos >= internal_buffer.begin() && new_pos < internal_buffer.end())
|
2020-01-28 13:01:08 +00:00
|
|
|
{
|
|
|
|
pos = new_pos;
|
2022-02-04 19:47:46 +00:00
|
|
|
working_buffer = internal_buffer; /// We need to restore `working_buffer` in case the position was at EOF before this seek().
|
|
|
|
return size_t(pos - internal_buffer.begin());
|
2020-01-28 13:01:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(
|
|
|
|
"Seek position is out of bounds. "
|
|
|
|
"Offset: "
|
2022-02-04 19:47:46 +00:00
|
|
|
+ std::to_string(offset) + ", Max: " + std::to_string(size_t(internal_buffer.end() - internal_buffer.begin())),
|
2020-01-28 13:01:08 +00:00
|
|
|
ErrorCodes::SEEK_POSITION_OUT_OF_BOUND);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Only SEEK_SET and SEEK_CUR seek modes allowed.", ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
|
2020-01-28 12:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 14:28:33 +00:00
|
|
|
off_t ReadBufferFromMemory::getPosition()
|
|
|
|
{
|
2022-02-04 19:47:46 +00:00
|
|
|
return pos - internal_buffer.begin();
|
2020-02-14 14:28:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 12:48:01 +00:00
|
|
|
}
|