2018-06-13 02:52:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-01-04 05:46:50 +00:00
|
|
|
#include <IO/ReadBufferFromFileBase.h>
|
2018-06-13 02:52:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/** MMap range in a file and represent it as a ReadBuffer.
|
|
|
|
* Please note that mmap is not always the optimal way to read file.
|
|
|
|
* Also you cannot control whether and how long actual IO take place,
|
|
|
|
* so this method is not manageable and not recommended for anything except benchmarks.
|
|
|
|
*/
|
2020-01-04 05:46:50 +00:00
|
|
|
class MMapReadBufferFromFileDescriptor : public ReadBufferFromFileBase
|
2018-06-13 02:52:03 +00:00
|
|
|
{
|
2020-01-27 18:44:30 +00:00
|
|
|
public:
|
|
|
|
off_t seek(off_t off, int whence) override;
|
|
|
|
|
2018-06-13 02:52:03 +00:00
|
|
|
protected:
|
2020-01-04 05:46:50 +00:00
|
|
|
MMapReadBufferFromFileDescriptor() {}
|
2018-06-13 02:52:03 +00:00
|
|
|
void init(int fd_, size_t offset, size_t length_);
|
|
|
|
void init(int fd_, size_t offset);
|
|
|
|
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
MMapReadBufferFromFileDescriptor(int fd_, size_t offset_, size_t length_);
|
2018-06-13 02:52:03 +00:00
|
|
|
|
|
|
|
/// Map till end of file.
|
2019-08-03 11:02:40 +00:00
|
|
|
MMapReadBufferFromFileDescriptor(int fd_, size_t offset_);
|
2018-06-13 02:52:03 +00:00
|
|
|
|
|
|
|
~MMapReadBufferFromFileDescriptor() override;
|
|
|
|
|
|
|
|
/// unmap memory before call to destructor
|
|
|
|
void finish();
|
|
|
|
|
2020-02-14 14:28:33 +00:00
|
|
|
off_t getPosition() override;
|
2020-01-04 05:46:50 +00:00
|
|
|
std::string getFileName() const override;
|
2020-02-14 14:28:33 +00:00
|
|
|
int getFD() const;
|
2020-01-04 05:46:50 +00:00
|
|
|
|
2018-06-13 02:52:03 +00:00
|
|
|
private:
|
|
|
|
size_t length = 0;
|
|
|
|
int fd = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|