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>
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-02-11 11:05:02 +00:00
|
|
|
/** Кэш разжатых блоков для CachedCompressedReadBuffer. thread-safe.
|
|
|
|
*/
|
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:
|
2014-03-28 14:36:24 +00:00
|
|
|
typedef LRUCache<UInt128, UncompressedCacheCell, UInt128TrivialHash, UncompressedSizeWeightFunction> Base;
|
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
|
|
|
|
|
|
|
/// Посчитать ключ от пути к файлу и смещения.
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
typedef Poco::SharedPtr<UncompressedCache> UncompressedCachePtr;
|
|
|
|
|
|
|
|
}
|