ClickHouse/src/IO/UncompressedCache.h

84 lines
2.0 KiB
C++
Raw Normal View History

#pragma once
#include <Common/LRUCache.h>
#include <Common/SipHash.h>
#include <Common/ProfileEvents.h>
2021-01-27 00:54:57 +00:00
#include <Common/HashTable/Hash.h>
#include <IO/BufferWithOwnMemory.h>
namespace ProfileEvents
{
extern const Event UncompressedCacheHits;
extern const Event UncompressedCacheMisses;
extern const Event UncompressedCacheWeightLost;
}
namespace DB
{
struct UncompressedCacheCell
{
2019-04-06 20:53:25 +00:00
Memory<> data;
size_t compressed_size;
UInt32 additional_bytes;
};
2014-03-28 14:36:24 +00:00
struct UncompressedSizeWeightFunction
{
size_t operator()(const UncompressedCacheCell & x) const
{
return x.data.size();
}
2014-03-28 14:36:24 +00:00
};
/** Cache of decompressed blocks for implementation of CachedCompressedReadBuffer. thread-safe.
*/
2014-03-28 14:36:24 +00:00
class UncompressedCache : public LRUCache<UInt128, UncompressedCacheCell, UInt128TrivialHash, UncompressedSizeWeightFunction>
{
private:
using Base = LRUCache<UInt128, UncompressedCacheCell, UInt128TrivialHash, UncompressedSizeWeightFunction>;
public:
UncompressedCache(size_t max_size_in_bytes)
: Base(max_size_in_bytes) {}
/// Calculate key from path to file and offset.
static UInt128 hash(const String & path_to_file, size_t offset)
{
UInt128 key;
SipHash hash;
hash.update(path_to_file.data(), path_to_file.size() + 1);
2018-03-03 15:36:20 +00:00
hash.update(offset);
2021-01-27 00:54:57 +00:00
hash.get128(key);
return key;
}
template <typename LoadFunc>
MappedPtr getOrSet(const Key & key, LoadFunc && load)
{
2021-03-28 19:42:34 +00:00
auto result = Base::getOrSet(key, std::forward<LoadFunc>(load));
if (result.second)
ProfileEvents::increment(ProfileEvents::UncompressedCacheMisses);
else
ProfileEvents::increment(ProfileEvents::UncompressedCacheHits);
return result.first;
}
private:
void onRemoveOverflowWeightLoss(size_t weight_loss) override
{
ProfileEvents::increment(ProfileEvents::UncompressedCacheWeightLost, weight_loss);
}
};
using UncompressedCachePtr = std::shared_ptr<UncompressedCache>;
}