aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Dictionaries/ICacheDictionaryStorage.h
blob: a4990528a4ebdb9961e7628012543cf3ecf998fb (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once

#include <Common/PODArray.h>
#include <Common/HashTable/HashMap.h>
#include <Columns/IColumn.h>
#include <Dictionaries/DictionaryHelpers.h>

namespace DB
{

struct KeyState
{
    enum State: uint8_t
    {
        not_found = 0,
        expired = 1,
        found = 2,
    };

    KeyState(State state_, size_t fetched_column_index_)
        : state(state_)
        , fetched_column_index(fetched_column_index_)
    {}

    KeyState(State state_) /// NOLINT
        : state(state_)
    {}

    inline bool isFound() const { return state == State::found; }
    inline bool isExpired() const { return state == State::expired; }
    inline bool isNotFound() const { return state == State::not_found; }
    inline bool isDefault() const { return is_default; }
    inline void setDefault() { is_default = true; }
    inline void setDefaultValue(bool is_default_value) { is_default = is_default_value; }
    /// Valid only if keyState is found or expired
    inline size_t getFetchedColumnIndex() const { return fetched_column_index; }
    inline void setFetchedColumnIndex(size_t fetched_column_index_value) { fetched_column_index = fetched_column_index_value; }
private:
    State state = not_found;
    size_t fetched_column_index = 0;
    bool is_default = false;
};

/// Result of fetch from CacheDictionaryStorage
template <typename KeyType>
struct KeysStorageFetchResult
{
    /// Fetched column values
    MutableColumns fetched_columns;

    PaddedPODArray<KeyState> key_index_to_state;

    size_t expired_keys_size = 0;

    size_t found_keys_size = 0;

    size_t not_found_keys_size = 0;

    size_t default_keys_size = 0;

};

using SimpleKeysStorageFetchResult = KeysStorageFetchResult<UInt64>;
using ComplexKeysStorageFetchResult = KeysStorageFetchResult<StringRef>;

class ICacheDictionaryStorage
{
public:

    virtual ~ICacheDictionaryStorage() = default;

    /// Necessary if all keys are found we can return result to client without additional aggregation
    virtual bool returnsFetchedColumnsInOrderOfRequestedKeys() const = 0;

    /// Name of storage
    virtual String getName() const = 0;

    /// Does storage support simple keys
    virtual bool supportsSimpleKeys() const = 0;

    /// Fetch columns for keys, this method is not write thread safe
    virtual SimpleKeysStorageFetchResult fetchColumnsForKeys(
        const PaddedPODArray<UInt64> & keys,
        const DictionaryStorageFetchRequest & fetch_request) = 0;

    /// Fetch columns for keys, this method is not write thread safe
    virtual void insertColumnsForKeys(const PaddedPODArray<UInt64> & keys, Columns columns) = 0;

    /// Insert default keys
    virtual void insertDefaultKeys(const PaddedPODArray<UInt64> & keys) = 0;

    /// Return cached simple keys
    virtual PaddedPODArray<UInt64> getCachedSimpleKeys() const = 0;

    /// Does storage support complex keys
    virtual bool supportsComplexKeys() const = 0;

    /// Fetch columns for keys, this method is not write thread safe
    virtual ComplexKeysStorageFetchResult fetchColumnsForKeys(
        const PaddedPODArray<StringRef> & keys,
        const DictionaryStorageFetchRequest & column_fetch_requests) = 0;

    /// Fetch columns for keys, this method is not write thread safe
    virtual void insertColumnsForKeys(const PaddedPODArray<StringRef> & keys, Columns columns) = 0;

    /// Insert default keys
    virtual void insertDefaultKeys(const PaddedPODArray<StringRef> & keys) = 0;

    /// Return cached complex keys.
    /// It is client responsibility to ensure keys proper lifetime.
    virtual PaddedPODArray<StringRef> getCachedComplexKeys() const = 0;

    /// Return size of keys in storage
    virtual size_t getSize() const = 0;

    /// Returns storage load factor
    virtual double getLoadFactor() const = 0;

    /// Return bytes allocated in storage
    virtual size_t getBytesAllocated() const = 0;

};

using CacheDictionaryStoragePtr = std::shared_ptr<ICacheDictionaryStorage>;

}