2018-06-13 02:52:03 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/formatReadable.h>
|
2020-01-04 05:46:50 +00:00
|
|
|
#include <Common/Exception.h>
|
2022-05-25 14:49:40 +00:00
|
|
|
#include <Common/filesystemHelpers.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/getPageSize.h>
|
2020-01-04 05:46:50 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2018-06-13 02:52:03 +00:00
|
|
|
#include <IO/MMapReadBufferFromFileDescriptor.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-01-04 05:46:50 +00:00
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
|
|
|
extern const int CANNOT_SEEK_THROUGH_FILE;
|
2018-06-13 02:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-26 20:46:04 +00:00
|
|
|
void MMapReadBufferFromFileDescriptor::init()
|
2018-06-13 02:52:03 +00:00
|
|
|
{
|
2021-03-26 20:46:04 +00:00
|
|
|
size_t length = mapped.getLength();
|
|
|
|
BufferBase::set(mapped.getData(), length, 0);
|
2018-06-13 02:52:03 +00:00
|
|
|
|
2021-03-26 20:46:04 +00:00
|
|
|
size_t page_size = static_cast<size_t>(::getPageSize());
|
2022-10-25 11:56:28 +00:00
|
|
|
ReadBuffer::padded = (length % page_size) > 0 && (length % page_size) <= (page_size - (PADDING_FOR_SIMD - 1));
|
2018-06-13 02:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-26 20:46:04 +00:00
|
|
|
MMapReadBufferFromFileDescriptor::MMapReadBufferFromFileDescriptor(int fd, size_t offset, size_t length)
|
|
|
|
: mapped(fd, offset, length)
|
2018-06-13 02:52:03 +00:00
|
|
|
{
|
2021-03-26 20:46:04 +00:00
|
|
|
init();
|
2018-06-13 02:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-26 20:46:04 +00:00
|
|
|
MMapReadBufferFromFileDescriptor::MMapReadBufferFromFileDescriptor(int fd, size_t offset)
|
|
|
|
: mapped(fd, offset)
|
2018-06-13 02:52:03 +00:00
|
|
|
{
|
2021-03-26 20:46:04 +00:00
|
|
|
init();
|
2018-06-13 02:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MMapReadBufferFromFileDescriptor::finish()
|
|
|
|
{
|
2021-03-26 20:46:04 +00:00
|
|
|
mapped.finish();
|
2018-06-13 02:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:46:04 +00:00
|
|
|
|
2020-01-04 05:46:50 +00:00
|
|
|
std::string MMapReadBufferFromFileDescriptor::getFileName() const
|
|
|
|
{
|
2021-03-26 20:46:04 +00:00
|
|
|
return "(fd = " + toString(mapped.getFD()) + ")";
|
2020-01-04 05:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int MMapReadBufferFromFileDescriptor::getFD() const
|
|
|
|
{
|
2021-03-26 20:46:04 +00:00
|
|
|
return mapped.getFD();
|
2020-01-04 05:46:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 14:28:33 +00:00
|
|
|
off_t MMapReadBufferFromFileDescriptor::getPosition()
|
2020-01-04 05:46:50 +00:00
|
|
|
{
|
|
|
|
return count();
|
|
|
|
}
|
|
|
|
|
2020-01-27 18:44:30 +00:00
|
|
|
off_t MMapReadBufferFromFileDescriptor::seek(off_t offset, int whence)
|
2020-01-04 05:46:50 +00:00
|
|
|
{
|
|
|
|
off_t new_pos;
|
|
|
|
if (whence == SEEK_SET)
|
|
|
|
new_pos = offset;
|
|
|
|
else if (whence == SEEK_CUR)
|
|
|
|
new_pos = count() + offset;
|
|
|
|
else
|
2021-03-27 23:11:46 +00:00
|
|
|
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "MMapReadBufferFromFileDescriptor::seek expects SEEK_SET or SEEK_CUR as whence");
|
2020-01-04 05:46:50 +00:00
|
|
|
|
|
|
|
working_buffer = internal_buffer;
|
|
|
|
if (new_pos < 0 || new_pos > off_t(working_buffer.size()))
|
2021-03-27 23:11:46 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_SEEK_THROUGH_FILE,
|
|
|
|
"Cannot seek through file {} because seek position ({}) is out of bounds [0, {}]",
|
|
|
|
getFileName(), new_pos, working_buffer.size());
|
2020-01-04 05:46:50 +00:00
|
|
|
|
|
|
|
position() = working_buffer.begin() + new_pos;
|
|
|
|
return new_pos;
|
|
|
|
}
|
|
|
|
|
2022-05-25 14:49:40 +00:00
|
|
|
size_t MMapReadBufferFromFileDescriptor::getFileSize()
|
|
|
|
{
|
|
|
|
return getSizeFromFileDescriptor(getFD(), getFileName());
|
|
|
|
}
|
2023-05-05 03:11:51 +00:00
|
|
|
|
|
|
|
size_t MMapReadBufferFromFileDescriptor::readBigAt(char * to, size_t n, size_t offset, const std::function<bool(size_t)> &)
|
|
|
|
{
|
|
|
|
if (offset >= mapped.getLength())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
n = std::min(n, mapped.getLength() - offset);
|
|
|
|
memcpy(to, mapped.getData() + offset, n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-06-13 02:52:03 +00:00
|
|
|
}
|