ClickHouse/src/IO/MMappedFileDescriptor.h

61 lines
1.4 KiB
C++
Raw Normal View History

2021-03-26 23:22:51 +00:00
#pragma once
#include <cstddef>
#include <Common/CurrentMetrics.h>
namespace CurrentMetrics
{
2021-03-28 19:24:28 +00:00
extern const Metric MMappedFiles;
extern const Metric MMappedFileBytes;
}
2021-03-26 23:22:51 +00:00
namespace DB
{
/// MMaps a region in file (or a whole file) into memory. Unmaps in destructor.
/// Does not open or close file.
2021-03-28 19:24:28 +00:00
class MMappedFileDescriptor
2021-03-26 23:22:51 +00:00
{
public:
2021-03-28 19:24:28 +00:00
MMappedFileDescriptor(int fd_, size_t offset_, size_t length_);
MMappedFileDescriptor(int fd_, size_t offset_);
2021-03-26 23:22:51 +00:00
/// Makes empty object that can be initialized with `set`.
MMappedFileDescriptor() = default;
2021-03-26 23:22:51 +00:00
2021-03-28 19:24:28 +00:00
virtual ~MMappedFileDescriptor();
2021-03-26 23:22:51 +00:00
char * getData() { return data; }
const char * getData() const { return data; }
int getFD() const { return fd; }
size_t getOffset() const { return offset; }
size_t getLength() const { return length; }
/// Unmap memory before call to destructor
void finish();
/// Initialize or reset to another fd.
void set(int fd_, size_t offset_, size_t length_);
void set(int fd_, size_t offset_);
2021-03-28 19:24:28 +00:00
MMappedFileDescriptor(const MMappedFileDescriptor &) = delete;
MMappedFileDescriptor(MMappedFileDescriptor &&) = delete;
2021-03-26 23:22:51 +00:00
protected:
2021-03-26 23:22:51 +00:00
void init();
int fd = -1;
size_t offset = 0;
size_t length = 0;
char * data = nullptr;
2021-03-28 19:24:28 +00:00
CurrentMetrics::Increment files_metric_increment{CurrentMetrics::MMappedFiles, 0};
CurrentMetrics::Increment bytes_metric_increment{CurrentMetrics::MMappedFileBytes, 0};
2021-03-26 23:22:51 +00:00
};
}