2013-09-08 05:53:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#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>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/BufferWithOwnMemory.h>
|
2013-09-08 05:53:10 +00:00
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event UncompressedCacheHits;
|
|
|
|
extern const Event UncompressedCacheMisses;
|
|
|
|
extern const Event UncompressedCacheWeightLost;
|
2016-10-24 02:02:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2014-02-11 11:05:02 +00:00
|
|
|
struct UncompressedCacheCell
|
2013-09-08 05:53:10 +00:00
|
|
|
{
|
2019-04-06 20:53:25 +00:00
|
|
|
Memory<> data;
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t compressed_size;
|
2019-04-05 09:42:44 +00:00
|
|
|
UInt32 additional_bytes;
|
2014-02-11 11:05:02 +00:00
|
|
|
};
|
2013-09-08 05:53:10 +00:00
|
|
|
|
2014-03-28 14:36:24 +00:00
|
|
|
struct UncompressedSizeWeightFunction
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator()(const UncompressedCacheCell & x) const
|
|
|
|
{
|
|
|
|
return x.data.size();
|
|
|
|
}
|
2014-03-28 14:36:24 +00:00
|
|
|
};
|
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/** Cache of decompressed blocks for implementation of CachedCompressedReadBuffer. thread-safe.
|
2014-02-11 11:05:02 +00:00
|
|
|
*/
|
2014-03-28 14:36:24 +00:00
|
|
|
class UncompressedCache : public LRUCache<UInt128, UncompressedCacheCell, UInt128TrivialHash, UncompressedSizeWeightFunction>
|
2014-02-11 11:05:02 +00:00
|
|
|
{
|
2013-09-08 05:53:10 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Base = LRUCache<UInt128, UncompressedCacheCell, UInt128TrivialHash, UncompressedSizeWeightFunction>;
|
2013-09-08 05:53:10 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2021-03-28 18:30:43 +00:00
|
|
|
template <typename LoadFunc>
|
|
|
|
MappedPtr getOrSet(const Key & key, LoadFunc && load)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-03-28 19:42:34 +00:00
|
|
|
auto result = Base::getOrSet(key, std::forward<LoadFunc>(load));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-03-28 18:30:43 +00:00
|
|
|
if (result.second)
|
2017-04-01 07:20:54 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::UncompressedCacheMisses);
|
2021-03-28 18:30:43 +00:00
|
|
|
else
|
|
|
|
ProfileEvents::increment(ProfileEvents::UncompressedCacheHits);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-03-28 18:30:43 +00:00
|
|
|
return result.first;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 23:07:39 +00:00
|
|
|
private:
|
|
|
|
void onRemoveOverflowWeightLoss(size_t weight_loss) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-03-08 23:07:39 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::UncompressedCacheWeightLost, weight_loss);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2013-09-08 05:53:10 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using UncompressedCachePtr = std::shared_ptr<UncompressedCache>;
|
2013-09-08 05:53:10 +00:00
|
|
|
|
|
|
|
}
|