| 1 | #pragma once |
| 2 | |
| 3 | #include <IO/ReadBufferFromFileDescriptor.h> |
| 4 | #include <Common/CurrentMetrics.h> |
| 5 | |
| 6 | #ifndef O_DIRECT |
| 7 | #define O_DIRECT 00040000 |
| 8 | #endif |
| 9 | |
| 10 | namespace CurrentMetrics |
| 11 | { |
| 12 | extern const Metric OpenFileForRead; |
| 13 | } |
| 14 | |
| 15 | namespace DB |
| 16 | { |
| 17 | |
| 18 | /** Accepts path to file and opens it, or pre-opened file descriptor. |
| 19 | * Closes file by himself (thus "owns" a file descriptor). |
| 20 | */ |
| 21 | class ReadBufferFromFile : public ReadBufferFromFileDescriptor |
| 22 | { |
| 23 | protected: |
| 24 | std::string file_name; |
| 25 | CurrentMetrics::Increment metric_increment{CurrentMetrics::OpenFileForRead}; |
| 26 | |
| 27 | public: |
| 28 | ReadBufferFromFile(const std::string & file_name_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, int flags = -1, |
| 29 | char * existing_memory = nullptr, size_t alignment = 0); |
| 30 | |
| 31 | /// Use pre-opened file descriptor. |
| 32 | ReadBufferFromFile(int fd, const std::string & original_file_name = {}, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, |
| 33 | char * existing_memory = nullptr, size_t alignment = 0); |
| 34 | |
| 35 | ReadBufferFromFile(ReadBufferFromFile &&) = default; |
| 36 | |
| 37 | ~ReadBufferFromFile() override; |
| 38 | |
| 39 | /// Close file before destruction of object. |
| 40 | void close(); |
| 41 | |
| 42 | std::string getFileName() const override |
| 43 | { |
| 44 | return file_name; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | } |
| 49 | |