2022-04-28 05:56:26 +00:00
|
|
|
#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>
|
2022-04-28 05:56:26 +00:00
|
|
|
#include <memory>
|
2024-01-09 12:05:02 +00:00
|
|
|
#include <optional>
|
2022-04-28 05:56:26 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2023-03-12 13:50:48 +00:00
|
|
|
|
2022-04-28 05:56:26 +00:00
|
|
|
template <typename T>
|
2023-03-12 13:50:48 +00:00
|
|
|
struct EqualWeightFunction
|
2022-04-28 05:56:26 +00:00
|
|
|
{
|
|
|
|
size_t operator()(const T &) const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-13 12:10:22 +00:00
|
|
|
template <typename TKey, typename TMapped, typename HashFunction = std::hash<TKey>, typename WeightFunction = EqualWeightFunction<TMapped>>
|
2022-04-28 05:56:26 +00:00
|
|
|
class ICachePolicy
|
|
|
|
{
|
2022-04-28 07:48:15 +00:00
|
|
|
public:
|
2023-03-13 12:10:22 +00:00
|
|
|
using Key = TKey;
|
|
|
|
using Mapped = TMapped;
|
2022-04-28 05:56:26 +00:00
|
|
|
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;
|
|
|
|
|
2023-08-21 18:05:24 +00:00
|
|
|
virtual size_t sizeInBytes() const = 0;
|
2023-08-21 17:57:30 +00:00
|
|
|
virtual size_t count() const = 0;
|
2023-08-21 18:05:24 +00:00
|
|
|
virtual size_t maxSizeInBytes() const = 0;
|
2022-05-14 04:15:57 +00:00
|
|
|
|
2023-08-21 17:57:30 +00:00
|
|
|
virtual void setMaxCount(size_t /*max_count*/) = 0;
|
2023-08-21 18:05:24 +00:00
|
|
|
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
|
|
|
|
2023-03-13 15:38:36 +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.
|
2023-08-21 17:57:30 +00:00
|
|
|
virtual MappedPtr get(const Key & key) = 0;
|
|
|
|
virtual std::optional<KeyMapped> getWithKey(const Key &) = 0;
|
2023-03-13 15:39:42 +00:00
|
|
|
|
2023-08-21 17:57:30 +00:00
|
|
|
virtual void set(const Key & key, const MappedPtr & mapped) = 0;
|
2022-04-28 05:56:26 +00:00
|
|
|
|
2023-08-21 17:57:30 +00:00
|
|
|
virtual void remove(const Key & key) = 0;
|
2023-03-13 15:39:42 +00:00
|
|
|
|
2023-08-21 17:57:30 +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;
|
2022-04-28 05:56:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|