aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Coordination/ACLMap.h
blob: e1b2ce1eff620a0f0c4548c69b1f2d2f5e4e9788 (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
#pragma once
#include <Common/ZooKeeper/ZooKeeperCommon.h>
#include <Common/ZooKeeper/IKeeper.h>
#include <unordered_map>


namespace DB
{

/// Simple mapping of different ACLs to sequentially growing numbers
/// Allows to store single number instead of vector of ACLs on disk and in memory.
class ACLMap
{
private:
    struct ACLsHash
    {
        size_t operator()(const Coordination::ACLs & acls) const;
    };

    struct ACLsComparator
    {
        bool operator()(const Coordination::ACLs & left, const Coordination::ACLs & right) const;
    };

    using ACLToNumMap = std::unordered_map<Coordination::ACLs, uint64_t, ACLsHash, ACLsComparator>;

    using NumToACLMap = std::unordered_map<uint64_t, Coordination::ACLs>;

    using UsageCounter = std::unordered_map<uint64_t, uint64_t>;

    ACLToNumMap acl_to_num;
    NumToACLMap num_to_acl;
    UsageCounter usage_counter;
    uint64_t max_acl_id{1};
public:

    /// Convert ACL to number. If it's new ACL than adds it to map
    /// with new id.
    uint64_t convertACLs(const Coordination::ACLs & acls);

    /// Convert number to ACL vector. If number is unknown for map
    /// than throws LOGICAL ERROR
    Coordination::ACLs convertNumber(uint64_t acls_id) const;
    /// Mapping from numbers to ACLs vectors. Used during serialization.
    const NumToACLMap & getMapping() const { return num_to_acl; }

    /// Add mapping to ACLMap. Used during deserialization from snapshot.
    void addMapping(uint64_t acls_id, const Coordination::ACLs & acls);

    /// Add/remove usage of some id. Used to remove unused ACLs.
    void addUsage(uint64_t acl_id);
    void removeUsage(uint64_t acl_id);
};

}