Coverage Report

Created: 2026-03-13 19:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/obj_lru_cache.h
Line
Count
Source
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 "runtime/memory/lru_cache_policy.h"
21
#include "util/lru_cache.h"
22
23
namespace doris {
24
25
// A common object cache depends on an Sharded LRU Cache.
26
// It has a certain capacity, which determin how many objects it can cache.
27
// Caller must hold a CacheHandle instance when visiting the cached object.
28
class ObjLRUCache : public LRUCachePolicy {
29
public:
30
    using LRUCachePolicy::insert;
31
32
    struct ObjKey {
33
105
        ObjKey(const std::string& key_) : key(key_) {}
34
35
        std::string key;
36
    };
37
38
    template <typename T>
39
    class ObjValue : public LRUCacheValueBase {
40
    public:
41
38
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueIiEC2EPKi
Line
Count
Source
41
1
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEC2EPKS7_
Line
Count
Source
41
15
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINS_12FileMetaDataEEC2EPKS2_
Line
Count
Source
41
22
        ObjValue(const T* value) : value(value) {}
42
38
        ~ObjValue() override {
43
38
            T* v = (T*)value;
44
38
            delete v;
45
38
        }
_ZN5doris11ObjLRUCache8ObjValueIiED2Ev
Line
Count
Source
42
1
        ~ObjValue() override {
43
1
            T* v = (T*)value;
44
1
            delete v;
45
1
        }
_ZN5doris11ObjLRUCache8ObjValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEED2Ev
Line
Count
Source
42
15
        ~ObjValue() override {
43
15
            T* v = (T*)value;
44
15
            delete v;
45
15
        }
_ZN5doris11ObjLRUCache8ObjValueINS_12FileMetaDataEED2Ev
Line
Count
Source
42
22
        ~ObjValue() override {
43
22
            T* v = (T*)value;
44
22
            delete v;
45
22
        }
46
47
        const T* value;
48
    };
49
50
    class CacheHandle {
51
    public:
52
114
        CacheHandle() = default;
53
        CacheHandle(LRUCachePolicy* cache, Cache::Handle* handle)
54
68
                : _cache(cache), _handle(handle) {}
55
182
        ~CacheHandle() {
56
182
            if (_handle != nullptr) {
57
68
                _cache->release(_handle);
58
68
            }
59
182
        }
60
61
0
        CacheHandle(CacheHandle&& other) noexcept {
62
0
            std::swap(_cache, other._cache);
63
0
            std::swap(_handle, other._handle);
64
0
        }
65
66
68
        CacheHandle& operator=(CacheHandle&& other) noexcept {
67
68
            std::swap(_cache, other._cache);
68
68
            std::swap(_handle, other._handle);
69
68
            return *this;
70
68
        }
71
72
0
        bool valid() { return _cache != nullptr && _handle != nullptr; }
73
74
0
        LRUCachePolicy* cache() const { return _cache; }
75
76
        template <typename T>
77
75
        const T* data() const {
78
75
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
75
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataIiEEPKT_v
Line
Count
Source
77
2
        const T* data() const {
78
2
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
2
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEPKT_v
Line
Count
Source
77
17
        const T* data() const {
78
17
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
17
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINS_12FileMetaDataEEEPKT_v
Line
Count
Source
77
56
        const T* data() const {
78
56
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
56
        }
80
81
    private:
82
        LRUCachePolicy* _cache = nullptr;
83
        Cache::Handle* _handle = nullptr;
84
85
        // Don't allow copy and assign
86
        DISALLOW_COPY_AND_ASSIGN(CacheHandle);
87
    };
88
89
    ObjLRUCache(int64_t capacity, uint32_t num_shards = DEFAULT_LRU_CACHE_NUM_SHARDS);
90
91
    bool lookup(const ObjKey& key, CacheHandle* handle);
92
93
    template <typename T>
94
38
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
38
        if (_enabled) {
96
38
            const std::string& encoded_key = key.key;
97
38
            auto* obj_value = new ObjValue<T>(value);
98
38
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
38
                                                  CachePriority::NORMAL);
100
38
            *cache_handle = CacheHandle {this, handle};
101
38
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
38
    }
_ZN5doris11ObjLRUCache6insertIiEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleE
Line
Count
Source
94
1
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
1
        if (_enabled) {
96
1
            const std::string& encoded_key = key.key;
97
1
            auto* obj_value = new ObjValue<T>(value);
98
1
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
1
                                                  CachePriority::NORMAL);
100
1
            *cache_handle = CacheHandle {this, handle};
101
1
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
1
    }
_ZN5doris11ObjLRUCache6insertINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleE
Line
Count
Source
94
15
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
15
        if (_enabled) {
96
15
            const std::string& encoded_key = key.key;
97
15
            auto* obj_value = new ObjValue<T>(value);
98
15
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
15
                                                  CachePriority::NORMAL);
100
15
            *cache_handle = CacheHandle {this, handle};
101
15
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
15
    }
_ZN5doris11ObjLRUCache6insertINS_12FileMetaDataEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleE
Line
Count
Source
94
22
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
22
        if (_enabled) {
96
22
            const std::string& encoded_key = key.key;
97
22
            auto* obj_value = new ObjValue<T>(value);
98
22
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
22
                                                  CachePriority::NORMAL);
100
22
            *cache_handle = CacheHandle {this, handle};
101
22
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
22
    }
106
107
    void erase(const ObjKey& key);
108
109
    bool exceed_prune_limit() override;
110
111
0
    bool enabled() const { return _enabled; }
112
113
private:
114
    const bool _enabled;
115
};
116
117
} // namespace doris