2011-10-24 12:10:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
2021-07-23 22:50:14 +00:00
|
|
|
#include <IO/OpenedFileCache.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#ifndef O_DIRECT
|
|
|
|
#define O_DIRECT 00040000
|
|
|
|
#endif
|
2011-10-24 12:10:59 +00:00
|
|
|
|
2021-07-23 22:50:14 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric OpenFileForRead;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
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 ReadBufferFromFile : public ReadBufferFromFileDescriptor
|
|
|
|
{
|
2017-02-09 10:10:13 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string file_name;
|
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::OpenFileForRead};
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
public:
|
2021-02-16 12:37:49 +00:00
|
|
|
explicit ReadBufferFromFile(const std::string & file_name_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, int flags = -1,
|
2017-04-01 07:20:54 +00:00
|
|
|
char * existing_memory = nullptr, size_t alignment = 0);
|
2011-10-24 12:10:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Use pre-opened file descriptor.
|
2021-02-16 12:37:49 +00:00
|
|
|
explicit ReadBufferFromFile(
|
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.
|
|
|
|
const std::string & original_file_name = {},
|
|
|
|
size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE,
|
2017-04-01 07:20:54 +00:00
|
|
|
char * existing_memory = nullptr, size_t alignment = 0);
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
~ReadBufferFromFile() override;
|
2015-12-13 08:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Close file before destruction of object.
|
|
|
|
void close();
|
2011-10-24 12:10:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string getFileName() const override
|
|
|
|
{
|
|
|
|
return file_name;
|
|
|
|
}
|
2011-10-24 12:10:59 +00:00
|
|
|
};
|
|
|
|
|
2021-07-12 01:12:34 +00:00
|
|
|
|
|
|
|
/** Similar to ReadBufferFromFile but it is using 'pread' instead of 'read'.
|
|
|
|
*/
|
|
|
|
class ReadBufferFromFilePRead : public ReadBufferFromFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ReadBufferFromFilePRead(const std::string & file_name_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, int flags = -1,
|
|
|
|
char * existing_memory = nullptr, size_t alignment = 0)
|
|
|
|
: ReadBufferFromFile(file_name_, buf_size, flags, existing_memory, alignment)
|
|
|
|
{
|
|
|
|
use_pread = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-23 22:50:14 +00:00
|
|
|
|
|
|
|
/** Similar to ReadBufferFromFilePRead but also transparently shares open file descriptors.
|
|
|
|
*/
|
|
|
|
class ReadBufferFromFilePReadWithCache : public ReadBufferFromFileDescriptorPRead
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static OpenedFileCache cache;
|
|
|
|
|
|
|
|
std::string file_name;
|
|
|
|
OpenedFileCache::OpenedFilePtr file;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ReadBufferFromFilePReadWithCache(const std::string & file_name_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, int flags = -1,
|
|
|
|
char * existing_memory = nullptr, size_t alignment = 0)
|
|
|
|
: ReadBufferFromFileDescriptorPRead(-1, buf_size, existing_memory, alignment),
|
|
|
|
file_name(file_name_)
|
|
|
|
{
|
|
|
|
file = cache.get(file_name, flags);
|
|
|
|
fd = file->getFD();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getFileName() const override
|
|
|
|
{
|
|
|
|
return file_name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
}
|