mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-13 01:41:59 +00:00
85 lines
2.1 KiB
C++
85 lines
2.1 KiB
C++
|
#include <IO/MMapReadBufferFromFileWithCache.h>
|
||
|
|
||
|
|
||
|
namespace DB
|
||
|
{
|
||
|
|
||
|
namespace
|
||
|
{
|
||
|
/// TODO: Move to a better place, make configurable.
|
||
|
MappedFileCache cache(1000);
|
||
|
}
|
||
|
|
||
|
|
||
|
namespace ErrorCodes
|
||
|
{
|
||
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
||
|
extern const int CANNOT_SEEK_THROUGH_FILE;
|
||
|
}
|
||
|
|
||
|
|
||
|
void MMapReadBufferFromFileWithCache::init()
|
||
|
{
|
||
|
size_t length = mapped->getLength();
|
||
|
BufferBase::set(mapped->getData(), length, 0);
|
||
|
|
||
|
size_t page_size = static_cast<size_t>(::getPageSize());
|
||
|
ReadBuffer::padded = (length % page_size) > 0 && (length % page_size) <= (page_size - 15);
|
||
|
}
|
||
|
|
||
|
|
||
|
MMapReadBufferFromFileWithCache::MMapReadBufferFromFileWithCache(
|
||
|
const std::string & file_name, size_t offset, size_t length)
|
||
|
{
|
||
|
mapped = cache.getOrSet(cache.hash(file_name, offset, length), [&]
|
||
|
{
|
||
|
return std::make_shared<MappedFile>(file_name, offset, length);
|
||
|
}).first;
|
||
|
|
||
|
init();
|
||
|
}
|
||
|
|
||
|
MMapReadBufferFromFileWithCache::MMapReadBufferFromFileWithCache(
|
||
|
const std::string & file_name, size_t offset)
|
||
|
{
|
||
|
mapped = cache.getOrSet(cache.hash(file_name, offset, -1), [&]
|
||
|
{
|
||
|
return std::make_shared<MappedFile>(file_name, offset);
|
||
|
}).first;
|
||
|
|
||
|
init();
|
||
|
}
|
||
|
|
||
|
|
||
|
std::string MMapReadBufferFromFileWithCache::getFileName() const
|
||
|
{
|
||
|
return mapped->getFileName();
|
||
|
}
|
||
|
|
||
|
off_t MMapReadBufferFromFileWithCache::getPosition()
|
||
|
{
|
||
|
return count();
|
||
|
}
|
||
|
|
||
|
off_t MMapReadBufferFromFileWithCache::seek(off_t offset, int whence)
|
||
|
{
|
||
|
off_t new_pos;
|
||
|
if (whence == SEEK_SET)
|
||
|
new_pos = offset;
|
||
|
else if (whence == SEEK_CUR)
|
||
|
new_pos = count() + offset;
|
||
|
else
|
||
|
throw Exception("MMapReadBufferFromFileWithCache::seek expects SEEK_SET or SEEK_CUR as whence", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
||
|
|
||
|
working_buffer = internal_buffer;
|
||
|
if (new_pos < 0 || new_pos > off_t(working_buffer.size()))
|
||
|
throw Exception("Cannot seek through file " + getFileName()
|
||
|
+ " because seek position (" + toString(new_pos) + ") is out of bounds [0, " + toString(working_buffer.size()) + "]",
|
||
|
ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
|
||
|
|
||
|
position() = working_buffer.begin() + new_pos;
|
||
|
return new_pos;
|
||
|
}
|
||
|
|
||
|
}
|