2014-02-11 13:30:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2022-04-30 11:53:59 +00:00
|
|
|
#include <Common/CacheBase.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/SipHash.h>
|
|
|
|
#include <Interpreters/AggregationCommon.h>
|
2021-10-15 20:18:20 +00:00
|
|
|
#include <Formats/MarkInCompressedFile.h>
|
2014-02-11 13:30:42 +00:00
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event MarkCacheHits;
|
|
|
|
extern const Event MarkCacheMisses;
|
|
|
|
}
|
2014-02-11 13:30:42 +00:00
|
|
|
|
2015-08-16 07:01:41 +00:00
|
|
|
namespace DB
|
2014-02-11 13:30:42 +00:00
|
|
|
{
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/// Estimate of number of bytes in cache for marks.
|
2014-02-11 13:30:42 +00:00
|
|
|
struct MarksWeightFunction
|
|
|
|
{
|
2020-11-27 13:17:10 +00:00
|
|
|
/// We spent additional bytes on key in hashmap, linked lists, shared pointers, etc ...
|
|
|
|
static constexpr size_t MARK_CACHE_OVERHEAD = 128;
|
|
|
|
|
2014-02-11 13:30:42 +00:00
|
|
|
size_t operator()(const MarksInCompressedFile & marks) const
|
|
|
|
{
|
2020-11-27 13:17:10 +00:00
|
|
|
return marks.size() * sizeof(MarkInCompressedFile) + MARK_CACHE_OVERHEAD;
|
2014-02-11 13:30:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/** Cache of 'marks' for StorageMergeTree.
|
|
|
|
* Marks is an index structure that addresses ranges in column file, corresponding to ranges of primary key.
|
2014-02-11 13:30:42 +00:00
|
|
|
*/
|
2022-04-30 11:53:59 +00:00
|
|
|
class MarkCache : public CacheBase<UInt128, MarksInCompressedFile, UInt128TrivialHash, MarksWeightFunction>
|
2014-02-11 13:30:42 +00:00
|
|
|
{
|
|
|
|
private:
|
2022-04-30 11:53:59 +00:00
|
|
|
using Base = CacheBase<UInt128, MarksInCompressedFile, UInt128TrivialHash, MarksWeightFunction>;
|
2014-02-11 13:30:42 +00:00
|
|
|
|
|
|
|
public:
|
2022-08-07 17:16:07 +00:00
|
|
|
explicit MarkCache(size_t max_size_in_bytes, const String & mark_cache_policy = "")
|
2022-08-08 20:53:02 +00:00
|
|
|
: Base(max_size_in_bytes, 0, mark_cache_policy) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/// Calculate key from path to file and offset.
|
2014-02-11 13:30:42 +00:00
|
|
|
static UInt128 hash(const String & path_to_file)
|
|
|
|
{
|
|
|
|
UInt128 key;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-02-11 13:30:42 +00:00
|
|
|
SipHash hash;
|
|
|
|
hash.update(path_to_file.data(), path_to_file.size() + 1);
|
2021-01-27 00:54:57 +00:00
|
|
|
hash.get128(key);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-02-11 13:30:42 +00:00
|
|
|
return key;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename LoadFunc>
|
2017-01-31 12:17:42 +00:00
|
|
|
MappedPtr getOrSet(const Key & key, LoadFunc && load)
|
2014-02-11 13:30:42 +00:00
|
|
|
{
|
2017-01-31 12:17:42 +00:00
|
|
|
auto result = Base::getOrSet(key, load);
|
|
|
|
if (result.second)
|
2014-02-11 13:30:42 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::MarkCacheMisses);
|
2017-01-31 12:17:42 +00:00
|
|
|
else
|
|
|
|
ProfileEvents::increment(ProfileEvents::MarkCacheHits);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-01-31 12:17:42 +00:00
|
|
|
return result.first;
|
2014-02-11 13:30:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using MarkCachePtr = std::shared_ptr<MarkCache>;
|
2014-02-11 13:30:42 +00:00
|
|
|
|
|
|
|
}
|