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