ClickHouse/src/Common/ICachePolicy.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.0 KiB
C++
Raw Normal View History

#pragma once
2023-03-13 09:34:03 +00:00
#include <Common/Exception.h>
2023-03-31 10:37:11 +00:00
#include <Common/ICachePolicyUserQuota.h>
2024-01-09 12:05:02 +00:00
#include <base/UUID.h>
2023-03-13 09:34:03 +00:00
2022-04-30 18:00:13 +00:00
#include <functional>
#include <memory>
2024-01-09 12:05:02 +00:00
#include <optional>
namespace DB
{
template <typename T>
struct EqualWeightFunction
{
size_t operator()(const T &) const
{
return 1;
}
};
template <typename TKey, typename TMapped, typename HashFunction = std::hash<TKey>, typename WeightFunction = EqualWeightFunction<TMapped>>
class ICachePolicy
{
2022-04-28 07:48:15 +00:00
public:
using Key = TKey;
using Mapped = TMapped;
using MappedPtr = std::shared_ptr<Mapped>;
using OnWeightLossFunction = std::function<void(size_t)>;
2023-03-13 15:28:03 +00:00
struct KeyMapped
{
Key key;
MappedPtr mapped;
};
2023-03-31 10:37:11 +00:00
explicit ICachePolicy(CachePolicyUserQuotaPtr user_quotas_) : user_quotas(std::move(user_quotas_)) {}
virtual ~ICachePolicy() = default;
virtual size_t sizeInBytes() const = 0;
virtual size_t count() const = 0;
virtual size_t maxSizeInBytes() const = 0;
virtual void setMaxCount(size_t /*max_count*/) = 0;
virtual void setMaxSizeInBytes(size_t /*max_size_in_bytes*/) = 0;
2024-01-11 13:33:16 +00:00
virtual void setQuotaForUser(const UUID & user_id, size_t max_size_in_bytes, size_t max_entries) { user_quotas->setQuotaForUser(user_id, max_size_in_bytes, max_entries); }
2023-03-13 09:34:03 +00:00
/// HashFunction usually hashes the entire key and the found key will be equal the provided key. In such cases, use get(). It is also
/// possible to store other, non-hashed data in the key. In that case, the found key is potentially different from the provided key.
/// Then use getWithKey() to also return the found key including it's non-hashed data.
virtual MappedPtr get(const Key & key) = 0;
virtual std::optional<KeyMapped> getWithKey(const Key &) = 0;
2023-03-13 15:39:42 +00:00
virtual void set(const Key & key, const MappedPtr & mapped) = 0;
virtual void remove(const Key & key) = 0;
2023-03-13 15:39:42 +00:00
virtual void clear() = 0;
2023-03-13 15:28:03 +00:00
virtual std::vector<KeyMapped> dump() const = 0;
2023-03-31 10:37:11 +00:00
protected:
CachePolicyUserQuotaPtr user_quotas;
};
}