aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/ICachePolicy.h
blob: 189af4db19be237f0a21cdba4aea17e8e1fc4210 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once

#include <Common/Exception.h>
#include <Common/ICachePolicyUserQuota.h>

#include <functional>
#include <memory>
#include <mutex>

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
{
public:
    using Key = TKey;
    using Mapped = TMapped;
    using MappedPtr = std::shared_ptr<Mapped>;
    using OnWeightLossFunction = std::function<void(size_t)>;

    struct KeyMapped
    {
        Key key;
        MappedPtr mapped;
    };

    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;
    virtual void setQuotaForUser(const String & user_name, size_t max_size_in_bytes, size_t max_entries) { user_quotas->setQuotaForUser(user_name, max_size_in_bytes, max_entries); }

    /// 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;

    virtual void set(const Key & key, const MappedPtr & mapped) = 0;

    virtual void remove(const Key & key) = 0;

    virtual void clear() = 0;
    virtual std::vector<KeyMapped> dump() const = 0;

protected:
    CachePolicyUserQuotaPtr user_quotas;
};

}