aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/Cache/SchemaCache.cpp
blob: 299dd292772e149182f5f018aa2f0f2d47164710 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <Storages/Cache/SchemaCache.h>
#include <Common/ProfileEvents.h>
#include <ctime>

namespace ProfileEvents
{
    extern const Event SchemaInferenceCacheHits;
    extern const Event SchemaInferenceCacheSchemaHits;
    extern const Event SchemaInferenceCacheNumRowsHits;
    extern const Event SchemaInferenceCacheMisses;
    extern const Event SchemaInferenceCacheSchemaMisses;
    extern const Event SchemaInferenceCacheNumRowsMisses;
    extern const Event SchemaInferenceCacheEvictions;
    extern const Event SchemaInferenceCacheInvalidations;
}

namespace DB
{

SchemaCache::SchemaCache(size_t max_elements_) : max_elements(max_elements_)
{
}

void SchemaCache::addColumns(const Key & key, const ColumnsDescription & columns)
{
    std::lock_guard lock(mutex);
    addUnlocked(key, columns, std::nullopt);
}


void SchemaCache::addManyColumns(const Keys & keys, const ColumnsDescription & columns)
{
    std::lock_guard lock(mutex);
    for (const auto & key : keys)
        addUnlocked(key, columns, std::nullopt);
}

void SchemaCache::addNumRows(const DB::SchemaCache::Key & key, size_t num_rows)
{
    std::lock_guard lock(mutex);
    addUnlocked(key, std::nullopt, num_rows);
}

void SchemaCache::addUnlocked(const Key & key, const std::optional<ColumnsDescription> & columns, std::optional<size_t> num_rows)
{
    /// Update columns/num_rows with new values if this key is already in cache.
    if (auto it = data.find(key); it != data.end())
    {
        if (columns)
            it->second.schema_info.columns = columns;
        if (num_rows)
            it->second.schema_info.num_rows = num_rows;
        return;
    }

    time_t now = std::time(nullptr);
    auto it = queue.insert(queue.end(), key);
    data[key] = {SchemaInfo{columns, num_rows, now}, it};
    checkOverflow();
}

void SchemaCache::checkOverflow()
{
    if (queue.size() <= max_elements)
        return;

    auto key = queue.front();
    data.erase(key);
    queue.pop_front();
    ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheEvictions);
}

std::optional<ColumnsDescription> SchemaCache::tryGetColumns(const DB::SchemaCache::Key & key, DB::SchemaCache::LastModificationTimeGetter get_last_mod_time)
{
    auto schema_info = tryGetImpl(key, get_last_mod_time);
    if (!schema_info)
        return std::nullopt;

    if (schema_info->columns)
        ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheSchemaHits);
    else
        ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheSchemaMisses);

    return schema_info->columns;
}

std::optional<size_t> SchemaCache::tryGetNumRows(const DB::SchemaCache::Key & key, DB::SchemaCache::LastModificationTimeGetter get_last_mod_time)
{
    auto schema_info = tryGetImpl(key, get_last_mod_time);
    if (!schema_info)
        return std::nullopt;

    if (schema_info->num_rows)
        ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheNumRowsHits);
    else
        ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheNumRowsMisses);

    return schema_info->num_rows;
}

std::optional<SchemaCache::SchemaInfo> SchemaCache::tryGetImpl(const Key & key, LastModificationTimeGetter get_last_mod_time)
{
    std::lock_guard lock(mutex);
    auto it = data.find(key);
    if (it == data.end())
    {
        ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheMisses);
        return std::nullopt;
    }

    ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheHits);

    auto & schema_info = it->second.schema_info;
    auto & queue_iterator = it->second.iterator;
    if (get_last_mod_time)
    {
        /// It's important to call get_last_mod_time only if we have key in cache,
        /// because this function can do some heavy operations.
        auto last_mod_time = get_last_mod_time();
        /// If get_last_mod_time function was provided but it returned nullopt, it means that
        /// it failed to get last modification time, so we cannot safely use value from cache.
        if (!last_mod_time)
            return std::nullopt;

        if (*last_mod_time >= schema_info.registration_time)
        {
            /// Object was modified after it was added in cache.
            /// So, stored value is no more valid and we should remove it.
            ProfileEvents::increment(ProfileEvents::SchemaInferenceCacheInvalidations);
            queue.erase(queue_iterator);
            data.erase(key);
            return std::nullopt;
        }
    }

    /// Move key to the end of queue.
    queue.splice(queue.end(), queue, queue_iterator);
    return schema_info;
}

void SchemaCache::clear()
{
    std::lock_guard lock(mutex);
    data.clear();
    queue.clear();
}

std::unordered_map<SchemaCache::Key, SchemaCache::SchemaInfo, SchemaCache::KeyHash> SchemaCache::getAll()
{
    std::lock_guard lock(mutex);
    std::unordered_map<Key, SchemaCache::SchemaInfo, SchemaCache::KeyHash> result;
    for (const auto & [key, value] : data)
        result[key] = value.schema_info;

    return result;
}

}