2011-10-24 12:10:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2023-03-30 17:02:28 +00:00
|
|
|
#include <Common/Throttler_fwd.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromFileDescriptor.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric OpenFileForWrite;
|
|
|
|
}
|
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#ifndef O_DIRECT
|
|
|
|
#define O_DIRECT 00040000
|
|
|
|
#endif
|
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
namespace DB
|
2016-01-11 21:46:36 +00:00
|
|
|
{
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/** Accepts path to file and opens it, or pre-opened file descriptor.
|
|
|
|
* Closes file by himself (thus "owns" a file descriptor).
|
2011-10-24 12:10:59 +00:00
|
|
|
*/
|
|
|
|
class WriteBufferFromFile : public WriteBufferFromFileDescriptor
|
|
|
|
{
|
2017-02-09 10:10:13 +00:00
|
|
|
protected:
|
2016-01-21 01:47:28 +00:00
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::OpenFileForWrite};
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
public:
|
2022-03-13 11:59:20 +00:00
|
|
|
explicit WriteBufferFromFile(
|
2016-10-25 06:49:24 +00:00
|
|
|
const std::string & file_name_,
|
|
|
|
size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE,
|
|
|
|
int flags = -1,
|
2023-03-30 17:02:28 +00:00
|
|
|
ThrottlerPtr throttler_ = {},
|
2016-10-25 06:49:24 +00:00
|
|
|
mode_t mode = 0666,
|
|
|
|
char * existing_memory = nullptr,
|
|
|
|
size_t alignment = 0);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/// Use pre-opened file descriptor.
|
2022-03-13 11:59:20 +00:00
|
|
|
explicit WriteBufferFromFile(
|
2020-09-30 00:00:45 +00:00
|
|
|
int & fd, /// Will be set to -1 if constructor didn't throw and ownership of file descriptor is passed to the object.
|
2017-02-09 10:10:13 +00:00
|
|
|
const std::string & original_file_name = {},
|
2016-10-25 06:49:24 +00:00
|
|
|
size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE,
|
2023-03-30 17:02:28 +00:00
|
|
|
ThrottlerPtr throttler_ = {},
|
2016-10-25 06:49:24 +00:00
|
|
|
char * existing_memory = nullptr,
|
|
|
|
size_t alignment = 0);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
~WriteBufferFromFile() override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/// Close file before destruction of object.
|
2016-10-25 06:49:24 +00:00
|
|
|
void close();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-03-07 04:31:10 +00:00
|
|
|
std::string getFileName() const override
|
2011-10-24 12:10:59 +00:00
|
|
|
{
|
|
|
|
return file_name;
|
|
|
|
}
|
2021-11-10 22:58:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void finalizeImpl() override;
|
2011-10-24 12:10:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|