Coverage Report

Created: 2026-10-27 21:41

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
428
        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
180
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueIiEC2EPKi
Line
Count
Source
41
6
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEC2EPKS7_
Line
Count
Source
41
57
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINS_12FileMetaDataEEC2EPKS2_
Line
Count
Source
41
83
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINS_6format7parquet21NativeParquetMetadataEEC2EPKS4_
Line
Count
Source
41
34
        ObjValue(const T* value) : value(value) {}
42
180
        ~ObjValue() override {
43
180
            T* v = (T*)value;
44
180
            delete v;
45
180
        }
_ZN5doris11ObjLRUCache8ObjValueIiED2Ev
Line
Count
Source
42
6
        ~ObjValue() override {
43
6
            T* v = (T*)value;
44
6
            delete v;
45
6
        }
_ZN5doris11ObjLRUCache8ObjValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEED2Ev
Line
Count
Source
42
57
        ~ObjValue() override {
43
57
            T* v = (T*)value;
44
57
            delete v;
45
57
        }
_ZN5doris11ObjLRUCache8ObjValueINS_12FileMetaDataEED2Ev
Line
Count
Source
42
83
        ~ObjValue() override {
43
83
            T* v = (T*)value;
44
83
            delete v;
45
83
        }
_ZN5doris11ObjLRUCache8ObjValueINS_6format7parquet21NativeParquetMetadataEED2Ev
Line
Count
Source
42
34
        ~ObjValue() override {
43
34
            T* v = (T*)value;
44
34
            delete v;
45
34
        }
46
47
        const T* value;
48
    };
49
50
    class CacheHandle {
51
    public:
52
1.10k
        CacheHandle() = default;
53
        CacheHandle(LRUCachePolicy* cache, Cache::Handle* handle)
54
252
                : _cache(cache), _handle(handle) {}
55
1.36k
        ~CacheHandle() {
56
1.36k
            if (_handle != nullptr) {
57
252
                _cache->release(_handle);
58
252
            }
59
1.36k
        }
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
529
        CacheHandle& operator=(CacheHandle&& other) noexcept {
67
529
            std::swap(_cache, other._cache);
68
529
            std::swap(_handle, other._handle);
69
529
            return *this;
70
529
        }
71
72
2
        bool valid() { return _cache != nullptr && _handle != nullptr; }
73
74
0
        LRUCachePolicy* cache() const { return _cache; }
75
76
        template <typename T>
77
308
        const T* data() const {
78
308
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
308
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataIiEEPKT_v
Line
Count
Source
77
5
        const T* data() const {
78
5
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
5
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEPKT_v
Line
Count
Source
77
20
        const T* data() const {
78
20
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
20
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINS_12FileMetaDataEEEPKT_v
Line
Count
Source
77
183
        const T* data() const {
78
183
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
183
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINS_6format7parquet21NativeParquetMetadataEEEPKT_v
Line
Count
Source
77
100
        const T* data() const {
78
100
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
100
        }
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
                LRUCacheType type = LRUCacheType::NUMBER,
91
                CachePolicy::CacheType cache_type = CachePolicy::CacheType::COMMON_OBJ_LRU_CACHE);
92
93
    bool lookup(const ObjKey& key, CacheHandle* handle);
94
95
    template <typename T>
96
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle,
97
180
                size_t memory_size = sizeof(T)) {
98
180
        if (_enabled) {
99
180
            const std::string& encoded_key = key.key;
100
180
            auto* obj_value = new ObjValue<T>(value);
101
180
            auto* handle = LRUCachePolicy::insert(
102
180
                    encoded_key, obj_value,
103
180
                    _lru_cache_type == LRUCacheType::SIZE ? memory_size + sizeof(ObjValue<T>) : 1,
104
180
                    memory_size + sizeof(ObjValue<T>), CachePriority::NORMAL);
105
180
            *cache_handle = CacheHandle {this, handle};
106
180
        } else {
107
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
108
0
                                   "ObjLRUCache disable, can not insert.");
109
0
        }
110
180
    }
_ZN5doris11ObjLRUCache6insertIiEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleEm
Line
Count
Source
97
6
                size_t memory_size = sizeof(T)) {
98
6
        if (_enabled) {
99
6
            const std::string& encoded_key = key.key;
100
6
            auto* obj_value = new ObjValue<T>(value);
101
6
            auto* handle = LRUCachePolicy::insert(
102
6
                    encoded_key, obj_value,
103
6
                    _lru_cache_type == LRUCacheType::SIZE ? memory_size + sizeof(ObjValue<T>) : 1,
104
6
                    memory_size + sizeof(ObjValue<T>), CachePriority::NORMAL);
105
6
            *cache_handle = CacheHandle {this, handle};
106
6
        } else {
107
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
108
0
                                   "ObjLRUCache disable, can not insert.");
109
0
        }
110
6
    }
_ZN5doris11ObjLRUCache6insertINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleEm
Line
Count
Source
97
57
                size_t memory_size = sizeof(T)) {
98
57
        if (_enabled) {
99
57
            const std::string& encoded_key = key.key;
100
57
            auto* obj_value = new ObjValue<T>(value);
101
57
            auto* handle = LRUCachePolicy::insert(
102
57
                    encoded_key, obj_value,
103
57
                    _lru_cache_type == LRUCacheType::SIZE ? memory_size + sizeof(ObjValue<T>) : 1,
104
57
                    memory_size + sizeof(ObjValue<T>), CachePriority::NORMAL);
105
57
            *cache_handle = CacheHandle {this, handle};
106
57
        } else {
107
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
108
0
                                   "ObjLRUCache disable, can not insert.");
109
0
        }
110
57
    }
_ZN5doris11ObjLRUCache6insertINS_12FileMetaDataEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleEm
Line
Count
Source
97
83
                size_t memory_size = sizeof(T)) {
98
83
        if (_enabled) {
99
83
            const std::string& encoded_key = key.key;
100
83
            auto* obj_value = new ObjValue<T>(value);
101
83
            auto* handle = LRUCachePolicy::insert(
102
83
                    encoded_key, obj_value,
103
83
                    _lru_cache_type == LRUCacheType::SIZE ? memory_size + sizeof(ObjValue<T>) : 1,
104
83
                    memory_size + sizeof(ObjValue<T>), CachePriority::NORMAL);
105
83
            *cache_handle = CacheHandle {this, handle};
106
83
        } else {
107
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
108
0
                                   "ObjLRUCache disable, can not insert.");
109
0
        }
110
83
    }
_ZN5doris11ObjLRUCache6insertINS_6format7parquet21NativeParquetMetadataEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleEm
Line
Count
Source
97
34
                size_t memory_size = sizeof(T)) {
98
34
        if (_enabled) {
99
34
            const std::string& encoded_key = key.key;
100
34
            auto* obj_value = new ObjValue<T>(value);
101
34
            auto* handle = LRUCachePolicy::insert(
102
34
                    encoded_key, obj_value,
103
34
                    _lru_cache_type == LRUCacheType::SIZE ? memory_size + sizeof(ObjValue<T>) : 1,
104
34
                    memory_size + sizeof(ObjValue<T>), CachePriority::NORMAL);
105
34
            *cache_handle = CacheHandle {this, handle};
106
34
        } else {
107
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
108
0
                                   "ObjLRUCache disable, can not insert.");
109
0
        }
110
34
    }
111
112
    void erase(const ObjKey& key);
113
114
    bool exceed_prune_limit() override;
115
116
296
    bool enabled() const { return _enabled; }
117
118
private:
119
    const bool _enabled;
120
};
121
122
} // namespace doris