ClickHouse/src/Common/LRUFileCachePriority.cpp

62 lines
1.7 KiB
C++
Raw Normal View History

2022-06-29 13:28:19 +00:00
#include <Common/LRUFileCachePriority.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
2022-06-29 14:45:34 +00:00
IFileCachePriority::WriteIterator LRUFileCachePriority::add(const Key & key, size_t offset, size_t size, std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
#ifndef NDEBUG
for (const auto & entry : queue)
{
if (entry.key == key && entry.offset == offset)
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Attempt to add duplicate queue entry to queue. (Key: {}, offset: {}, size: {})",
entry.key.toString(),
entry.offset,
entry.size);
}
#endif
auto iter = queue.insert(queue.end(), FileCacheRecord(key, offset, size));
cache_size += size;
return std::make_shared<LRUFileCacheIterator>(this, iter);
}
2022-06-29 14:45:34 +00:00
bool LRUFileCachePriority::contains(const Key & key, size_t offset, std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
for (const auto & record : queue)
{
if (key == record.key && offset == record.offset)
return true;
}
return false;
}
2022-06-29 14:45:34 +00:00
void LRUFileCachePriority::removeAll(std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
queue.clear();
cache_size = 0;
}
2022-06-29 14:45:34 +00:00
IFileCachePriority::ReadIterator LRUFileCachePriority::getLowestPriorityReadIterator(std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
2022-08-10 15:19:26 +00:00
return std::make_unique<const LRUFileCacheIterator>(this, queue.begin());
2022-06-29 13:28:19 +00:00
}
2022-06-29 14:45:34 +00:00
IFileCachePriority::WriteIterator LRUFileCachePriority::getLowestPriorityWriteIterator(std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
return std::make_shared<LRUFileCacheIterator>(this, queue.begin());
}
2022-06-29 14:45:34 +00:00
size_t LRUFileCachePriority::getElementsNum(std::lock_guard<std::mutex> &) const
2022-06-29 13:28:19 +00:00
{
return queue.size();
}
};