ClickHouse/src/IO/ReadBufferFromFileDescriptor.h

100 lines
3.1 KiB
C++
Raw Normal View History

2011-10-24 12:10:59 +00:00
#pragma once
#include <IO/ReadBufferFromFileBase.h>
2022-07-20 20:30:16 +00:00
#include <Interpreters/Context_fwd.h>
#include <Common/Throttler_fwd.h>
2020-02-17 14:27:09 +00:00
#include <unistd.h>
2011-10-24 12:10:59 +00:00
namespace DB
{
/** Use ready file descriptor. Does not open or close a file.
2011-10-24 12:10:59 +00:00
*/
class ReadBufferFromFileDescriptor : public ReadBufferFromFileBase
2011-10-24 12:10:59 +00:00
{
protected:
const size_t required_alignment = 0; /// For O_DIRECT both file offsets and memory addresses have to be aligned.
bool use_pread = false; /// To access one fd from multiple threads, use 'pread' syscall instead of 'read'.
size_t file_offset_of_buffer_end = 0; /// What offset in file corresponds to working_buffer.end().
int fd;
ThrottlerPtr throttler;
bool nextImpl() override;
void prefetch(Priority priority) override;
2011-10-24 12:10:59 +00:00
/// Name or some description of file.
std::string getFileName() const override;
2011-10-24 12:10:59 +00:00
/// Does the read()/pread(), with all the metric increments, error handling, throttling, etc.
/// Doesn't seek (`offset` must match fd's position if !use_pread).
/// Stops after min_bytes or eof. Returns 0 if eof.
/// Thread safe.
2024-03-03 13:22:40 +00:00
size_t readImpl(char * to, size_t min_bytes, size_t max_bytes, size_t offset) const;
2011-10-24 12:10:59 +00:00
public:
explicit ReadBufferFromFileDescriptor(
int fd_,
size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE,
char * existing_memory = nullptr,
size_t alignment = 0,
std::optional<size_t> file_size_ = std::nullopt,
ThrottlerPtr throttler_ = {})
: ReadBufferFromFileBase(buf_size, existing_memory, alignment, file_size_)
, required_alignment(alignment)
, fd(fd_)
, throttler(throttler_)
{
}
2020-02-14 14:28:33 +00:00
int getFD() const
{
return fd;
}
2020-02-14 14:28:33 +00:00
off_t getPosition() override
{
2020-07-31 14:53:41 +00:00
return file_offset_of_buffer_end - (working_buffer.end() - pos);
}
2022-05-30 18:35:31 +00:00
size_t getFileOffsetOfBufferEnd() const override { return file_offset_of_buffer_end; }
/// If 'offset' is small enough to stay in buffer after seek, then true seek in file does not happen.
off_t seek(off_t off, int whence) override;
2015-12-13 08:51:28 +00:00
2021-07-04 03:03:49 +00:00
/// Seek to the beginning, discarding already read data if any. Useful to reread file that changes on every read.
void rewind();
2022-05-25 14:49:40 +00:00
size_t getFileSize() override;
bool checkIfActuallySeekable() override;
2024-03-03 13:22:40 +00:00
size_t readBigAt(char * to, size_t n, size_t offset, const std::function<bool(size_t)> &) const override;
bool supportsReadAt() override { return use_pread; }
2011-10-24 12:10:59 +00:00
};
/** Similar to ReadBufferFromFileDescriptor but it is using 'pread' allowing multiple concurrent reads from the same fd.
*/
class ReadBufferFromFileDescriptorPRead : public ReadBufferFromFileDescriptor
{
public:
explicit ReadBufferFromFileDescriptorPRead(
int fd_,
size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE,
char * existing_memory = nullptr,
size_t alignment = 0,
std::optional<size_t> file_size_ = std::nullopt,
ThrottlerPtr throttler_ = {})
: ReadBufferFromFileDescriptor(fd_, buf_size, existing_memory, alignment, file_size_, throttler_)
{
use_pread = true;
}
};
2011-10-24 12:10:59 +00:00
}