ClickHouse/src/IO/SeekableReadBuffer.h

67 lines
1.9 KiB
C++
Raw Normal View History

#pragma once
#include <IO/ReadBuffer.h>
2022-04-26 12:57:02 +00:00
#include <IO/WithFileSize.h>
2021-10-31 19:53:24 +00:00
#include <optional>
2020-01-27 19:17:22 +00:00
namespace DB
{
2022-02-12 22:20:05 +00:00
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
2020-01-27 19:17:22 +00:00
class SeekableReadBuffer : public ReadBuffer
{
public:
SeekableReadBuffer(Position ptr, size_t size)
: ReadBuffer(ptr, size) {}
SeekableReadBuffer(Position ptr, size_t size, size_t offset)
: ReadBuffer(ptr, size, offset) {}
2020-01-27 19:51:48 +00:00
/**
* Shifts buffer current position to given offset.
* @param off Offset.
* @param whence Seek mode (@see SEEK_SET, @see SEEK_CUR).
2021-07-12 21:07:33 +00:00
* @return New position from the beginning of underlying buffer / file.
2020-01-27 19:51:48 +00:00
*/
virtual off_t seek(off_t off, int whence) = 0;
2020-02-14 14:28:33 +00:00
2021-02-06 16:30:46 +00:00
/**
* Keep in mind that seekable buffer may encounter eof() once and the working buffer
* may get into inconsistent state. Don't forget to reset it on the first nextImpl()
* after seek().
*/
2020-02-14 14:28:33 +00:00
/**
2020-02-19 19:26:33 +00:00
* @return Offset from the begin of the underlying buffer / file corresponds to the buffer current position.
2020-02-14 14:28:33 +00:00
*/
virtual off_t getPosition() = 0;
2022-02-12 22:20:05 +00:00
2022-02-19 18:18:52 +00:00
struct Range
{
size_t left;
std::optional<size_t> right;
};
/**
* Returns a struct, where `left` is current read position in file and `right` is the
* last included offset for reading according to setReadUntilPosition() or setReadUntilEnd().
* E.g. next nextImpl() call will read within range [left, right].
*/
virtual Range getRemainingReadRange() const
{
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getRemainingReadRange() not implemented");
}
2022-02-12 22:20:05 +00:00
virtual String getInfoForLog() { return ""; }
2022-02-19 18:18:52 +00:00
virtual size_t getFileOffsetOfBufferEnd() const { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getFileOffsetOfBufferEnd() not implemented"); }
};
2021-09-17 15:27:43 +00:00
using SeekableReadBufferPtr = std::shared_ptr<SeekableReadBuffer>;
2021-10-31 19:53:24 +00:00
}