ClickHouse/dbms/src/Storages/MarkCache.h

72 lines
1.8 KiB
C++
Raw Normal View History

2014-02-11 13:30:42 +00:00
#pragma once
2015-04-16 06:12:35 +00:00
#include <memory>
#include <Common/LRUCache.h>
#include <Common/ProfileEvents.h>
#include <Common/SipHash.h>
#include <Interpreters/AggregationCommon.h>
#include <DataStreams/MarkInCompressedFile.h>
2014-02-11 13:30:42 +00:00
namespace ProfileEvents
{
extern const Event MarkCacheHits;
extern const Event MarkCacheMisses;
}
2014-02-11 13:30:42 +00:00
namespace DB
2014-02-11 13:30:42 +00:00
{
/// Estimate of number of bytes in cache for marks.
2014-02-11 13:30:42 +00:00
struct MarksWeightFunction
{
size_t operator()(const MarksInCompressedFile & marks) const
{
/// NOTE Could add extra 100 bytes for overhead of std::vector, cache structures and allocator.
return marks.size() * sizeof(MarkInCompressedFile);
}
2014-02-11 13:30:42 +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
*/
2015-05-07 12:28:09 +00:00
class MarkCache : public LRUCache<UInt128, MarksInCompressedFile, UInt128TrivialHash, MarksWeightFunction>
2014-02-11 13:30:42 +00:00
{
private:
using Base = LRUCache<UInt128, MarksInCompressedFile, UInt128TrivialHash, MarksWeightFunction>;
2014-02-11 13:30:42 +00:00
public:
MarkCache(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)
{
UInt128 key;
SipHash hash;
hash.update(path_to_file.data(), path_to_file.size() + 1);
2017-07-04 16:10:36 +00:00
hash.get128(key.low, key.high);
return key;
}
2017-09-15 12:16:12 +00:00
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;
}
2014-02-11 13:30:42 +00:00
};
using MarkCachePtr = std::shared_ptr<MarkCache>;
2014-02-11 13:30:42 +00:00
}