mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-15 19:02:04 +00:00
71 lines
1.9 KiB
C++
71 lines
1.9 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
|
|
#include <Common/CacheBase.h>
|
|
#include <Common/ProfileEvents.h>
|
|
#include <Common/SipHash.h>
|
|
#include <Common/HashTable/Hash.h>
|
|
#include <Interpreters/AggregationCommon.h>
|
|
#include <Formats/MarkInCompressedFile.h>
|
|
|
|
|
|
namespace ProfileEvents
|
|
{
|
|
extern const Event MarkCacheHits;
|
|
extern const Event MarkCacheMisses;
|
|
}
|
|
|
|
namespace DB
|
|
{
|
|
|
|
/// Estimate of number of bytes in cache for marks.
|
|
struct MarksWeightFunction
|
|
{
|
|
/// We spent additional bytes on key in hashmap, linked lists, shared pointers, etc ...
|
|
static constexpr size_t MARK_CACHE_OVERHEAD = 128;
|
|
|
|
size_t operator()(const MarksInCompressedFile & marks) const
|
|
{
|
|
return marks.approximateMemoryUsage() + MARK_CACHE_OVERHEAD;
|
|
}
|
|
};
|
|
|
|
|
|
/** Cache of 'marks' for StorageMergeTree.
|
|
* Marks is an index structure that addresses ranges in column file, corresponding to ranges of primary key.
|
|
*/
|
|
class MarkCache : public CacheBase<UInt128, MarksInCompressedFile, UInt128TrivialHash, MarksWeightFunction>
|
|
{
|
|
private:
|
|
using Base = CacheBase<UInt128, MarksInCompressedFile, UInt128TrivialHash, MarksWeightFunction>;
|
|
|
|
public:
|
|
MarkCache(const String & cache_policy, size_t max_size_in_bytes, double size_ratio)
|
|
: Base(cache_policy, max_size_in_bytes, 0, size_ratio) {}
|
|
|
|
/// Calculate key from path to file and offset.
|
|
static UInt128 hash(const String & path_to_file)
|
|
{
|
|
SipHash hash;
|
|
hash.update(path_to_file.data(), path_to_file.size() + 1);
|
|
return hash.get128();
|
|
}
|
|
|
|
template <typename LoadFunc>
|
|
MappedPtr getOrSet(const Key & key, LoadFunc && load)
|
|
{
|
|
auto result = Base::getOrSet(key, load);
|
|
if (result.second)
|
|
ProfileEvents::increment(ProfileEvents::MarkCacheMisses);
|
|
else
|
|
ProfileEvents::increment(ProfileEvents::MarkCacheHits);
|
|
|
|
return result.first;
|
|
}
|
|
};
|
|
|
|
using MarkCachePtr = std::shared_ptr<MarkCache>;
|
|
|
|
}
|