1#pragma once
2
3#include <port/unistd.h>
4#include <IO/ReadBufferFromFileBase.h>
5#include <IO/ReadBuffer.h>
6
7
8namespace DB
9{
10
11/** Use ready file descriptor. Does not open or close a file.
12 */
13class ReadBufferFromFileDescriptor : public ReadBufferFromFileBase
14{
15protected:
16 int fd;
17 off_t pos_in_file; /// What offset in file corresponds to working_buffer.end().
18
19 bool nextImpl() override;
20
21 /// Name or some description of file.
22 std::string getFileName() const override;
23
24public:
25 ReadBufferFromFileDescriptor(int fd_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = nullptr, size_t alignment = 0)
26 : ReadBufferFromFileBase(buf_size, existing_memory, alignment), fd(fd_), pos_in_file(0) {}
27
28 ReadBufferFromFileDescriptor(ReadBufferFromFileDescriptor &&) = default;
29
30 int getFD() const override
31 {
32 return fd;
33 }
34
35 off_t getPositionInFile() override
36 {
37 return pos_in_file - (working_buffer.end() - pos);
38 }
39
40private:
41 /// If 'offset' is small enough to stay in buffer after seek, then true seek in file does not happen.
42 off_t doSeek(off_t offset, int whence) override;
43
44 /// Assuming file descriptor supports 'select', check that we have data to read or wait until timeout.
45 bool poll(size_t timeout_microseconds);
46};
47
48}
49