#pragma once #include #include #include #include #include namespace ProfileEvents { extern const Event UncompressedCacheHits; extern const Event UncompressedCacheMisses; extern const Event UncompressedCacheWeightLost; } namespace DB { struct UncompressedCacheCell { Memory<> data; size_t compressed_size; UInt32 additional_bytes; }; struct UncompressedSizeWeightFunction { size_t operator()(const UncompressedCacheCell & x) const { return x.data.size(); } }; /** Cache of decompressed blocks for implementation of CachedCompressedReadBuffer. thread-safe. */ class UncompressedCache : public LRUCache { private: using Base = LRUCache; public: UncompressedCache(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, size_t offset) { UInt128 key; SipHash hash; hash.update(path_to_file.data(), path_to_file.size() + 1); hash.update(offset); hash.get128(key.low, key.high); return key; } MappedPtr get(const Key & key) { MappedPtr res = Base::get(key); if (res) ProfileEvents::increment(ProfileEvents::UncompressedCacheHits); else ProfileEvents::increment(ProfileEvents::UncompressedCacheMisses); return res; } private: void onRemoveOverflowWeightLoss(size_t weight_loss) override { ProfileEvents::increment(ProfileEvents::UncompressedCacheWeightLost, weight_loss); } }; using UncompressedCachePtr = std::shared_ptr; }