2013-09-08 05:53:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-02-11 11:05:02 +00:00
|
|
|
#include <DB/Common/LRUCache.h>
|
2013-09-08 05:53:10 +00:00
|
|
|
#include <DB/Common/SipHash.h>
|
2014-01-03 08:20:13 +00:00
|
|
|
#include <DB/Common/ProfileEvents.h>
|
2013-09-08 05:53:10 +00:00
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
|
|
|
#include <DB/Interpreters/AggregationCommon.h>
|
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event UncompressedCacheHits;
|
|
|
|
extern const Event UncompressedCacheMisses;
|
|
|
|
extern const Event UncompressedCacheWeightLost;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-02-11 11:05:02 +00:00
|
|
|
Memory data;
|
|
|
|
size_t compressed_size;
|
|
|
|
};
|
2013-09-08 05:53:10 +00:00
|
|
|
|
2014-03-28 14:36:24 +00:00
|
|
|
struct UncompressedSizeWeightFunction
|
|
|
|
{
|
|
|
|
size_t operator()(const UncompressedCacheCell & x) const
|
|
|
|
{
|
|
|
|
return x.data.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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:
|
2016-05-28 10:35:44 +00:00
|
|
|
using Base = LRUCache<UInt128, UncompressedCacheCell, UInt128TrivialHash, UncompressedSizeWeightFunction>;
|
2013-09-08 05:53:10 +00:00
|
|
|
|
|
|
|
public:
|
2014-03-28 14:36:24 +00:00
|
|
|
UncompressedCache(size_t max_size_in_bytes)
|
|
|
|
: Base(max_size_in_bytes) {}
|
2013-09-08 05:53:10 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/// Calculate key from path to file and offset.
|
2013-09-08 05:53:10 +00:00
|
|
|
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);
|
|
|
|
hash.update(reinterpret_cast<const char *>(&offset), sizeof(offset));
|
|
|
|
hash.get128(key.first, key.second);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2014-02-11 11:05:02 +00:00
|
|
|
MappedPtr get(const Key & key)
|
2013-09-08 05:53:10 +00:00
|
|
|
{
|
2014-02-11 11:05:02 +00:00
|
|
|
MappedPtr res = Base::get(key);
|
2013-09-08 05:53:10 +00:00
|
|
|
|
2014-02-11 11:05:02 +00:00
|
|
|
if (res)
|
2014-01-03 08:20:13 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::UncompressedCacheHits);
|
2013-09-08 05:53:10 +00:00
|
|
|
else
|
2014-01-03 08:20:13 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::UncompressedCacheMisses);
|
2013-09-08 05:53:10 +00:00
|
|
|
|
2014-02-11 11:05:02 +00:00
|
|
|
return res;
|
2013-09-08 05:53:10 +00:00
|
|
|
}
|
2014-04-17 15:46:58 +00:00
|
|
|
|
|
|
|
void set(const Key & key, MappedPtr mapped)
|
|
|
|
{
|
|
|
|
Base::set(key, mapped);
|
|
|
|
ProfileEvents::increment(ProfileEvents::UncompressedCacheWeightLost, current_weight_lost);
|
|
|
|
current_weight_lost = 0;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
}
|