Coverage Report

Created: 2024-11-22 20:18

/root/doris/be/src/util/lru_cache.hpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <iterator>
21
#include <list>
22
#include <unordered_map>
23
24
namespace doris {
25
26
template <typename Key, typename Value>
27
class LruCache {
28
public:
29
    typedef typename std::pair<Key, Value> KeyValuePair;
30
    typedef typename std::list<KeyValuePair>::iterator ListIterator;
31
32
    class Iterator {
33
    public:
34
        using iterator_category = std::input_iterator_tag;
35
        using value_type = KeyValuePair;
36
        using difference_type = ptrdiff_t;
37
        using pointer = KeyValuePair*;
38
        using reference = KeyValuePair&;
39
40
2
        Iterator(typename std::unordered_map<Key, ListIterator>::iterator it) : _it(it) {}
41
42
10
        Iterator& operator++() {
43
10
            ++_it;
44
10
            return *this;
45
10
        }
46
47
        bool operator==(const Iterator& rhs) const { return _it == rhs._it; }
48
49
11
        bool operator!=(const Iterator& rhs) const { return _it != rhs._it; }
50
51
        KeyValuePair* operator->() { return _it->second.operator->(); }
52
53
10
        KeyValuePair& operator*() { return *_it->second; }
54
55
    private:
56
        typename std::unordered_map<Key, ListIterator>::iterator _it;
57
    };
58
59
3
    LruCache(size_t max_size) : _max_size(max_size) {}
60
61
130
    void put(const Key& key, const Value& value) {
62
130
        auto it = _cache_items_map.find(key);
63
130
        if (it != _cache_items_map.end()) {
64
0
            _cache_items_list.erase(it->second);
65
0
            _cache_items_map.erase(it);
66
0
        }
67
68
130
        _cache_items_list.push_front(KeyValuePair(key, value));
69
130
        _cache_items_map[key] = _cache_items_list.begin();
70
71
130
        if (_cache_items_map.size() > _max_size) {
72
100
            auto last = _cache_items_list.end();
73
100
            last--;
74
100
            _cache_items_map.erase(last->first);
75
100
            _cache_items_list.pop_back();
76
100
        }
77
130
    }
78
79
    void erase(const Key& key) {
80
        auto it = _cache_items_map.find(key);
81
        if (it != _cache_items_map.end()) {
82
            _cache_items_list.erase(it->second);
83
            _cache_items_map.erase(it);
84
        }
85
    }
86
87
    // Must copy value, because value maybe released when caller used
88
30
    bool get(const Key& key, Value* value) {
89
30
        auto it = _cache_items_map.find(key);
90
30
        if (it == _cache_items_map.end()) {
91
0
            return false;
92
0
        }
93
30
        _cache_items_list.splice(_cache_items_list.begin(), _cache_items_list, it->second);
94
30
        *value = it->second->second;
95
30
        return true;
96
30
    }
97
98
    bool exists(const Key& key) const {
99
        return _cache_items_map.find(key) != _cache_items_map.end();
100
    }
101
102
3
    size_t size() const { return _cache_items_map.size(); }
103
104
1
    Iterator begin() { return Iterator(_cache_items_map.begin()); }
105
106
1
    Iterator end() { return Iterator(_cache_items_map.end()); }
107
108
private:
109
    std::list<KeyValuePair> _cache_items_list;
110
    std::unordered_map<Key, ListIterator> _cache_items_map;
111
    size_t _max_size;
112
};
113
114
} // namespace doris