ClickHouse/src/IO/MMapReadBufferFromFile.cpp

80 lines
1.5 KiB
C++
Raw Normal View History

2018-06-13 02:52:03 +00:00
#include <unistd.h>
#include <fcntl.h>
#include <Common/ProfileEvents.h>
#include <Common/formatReadable.h>
#include <IO/MMapReadBufferFromFile.h>
namespace ProfileEvents
{
extern const Event FileOpen;
}
namespace DB
{
namespace ErrorCodes
{
extern const int FILE_DOESNT_EXIST;
extern const int CANNOT_OPEN_FILE;
extern const int CANNOT_CLOSE_FILE;
}
void MMapReadBufferFromFile::open()
2018-06-13 02:52:03 +00:00
{
ProfileEvents::increment(ProfileEvents::FileOpen);
fd = ::open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
2018-06-13 02:52:03 +00:00
if (-1 == fd)
2019-08-07 12:52:47 +00:00
throwFromErrnoWithPath("Cannot open file " + file_name, file_name,
errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
2018-06-13 02:52:03 +00:00
}
std::string MMapReadBufferFromFile::getFileName() const
2018-06-13 02:52:03 +00:00
{
return file_name;
}
MMapReadBufferFromFile::MMapReadBufferFromFile(const std::string & file_name_, size_t offset, size_t length_)
: file_name(file_name_)
{
open();
2021-03-26 20:46:04 +00:00
mapped.set(fd, offset, length_);
2021-03-26 22:28:57 +00:00
init();
2018-06-13 02:52:03 +00:00
}
MMapReadBufferFromFile::MMapReadBufferFromFile(const std::string & file_name_, size_t offset)
: file_name(file_name_)
2018-06-13 02:52:03 +00:00
{
open();
2021-03-26 20:46:04 +00:00
mapped.set(fd, offset);
2021-03-26 22:28:57 +00:00
init();
2018-06-13 02:52:03 +00:00
}
MMapReadBufferFromFile::~MMapReadBufferFromFile()
{
if (fd != -1)
close(); /// Exceptions will lead to std::terminate and that's Ok.
}
void MMapReadBufferFromFile::close()
{
finish();
if (0 != ::close(fd))
throw Exception("Cannot close file", ErrorCodes::CANNOT_CLOSE_FILE);
fd = -1;
metric_increment.destroy();
}
}