Coverage Report

Created: 2026-10-09 18:00

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
680
        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
270
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueIiEC2EPKi
Line
Count
Source
41
2
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEC2EPKS7_
Line
Count
Source
41
92
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINS_6format7parquet21NativeParquetMetadataEEC2EPKS4_
Line
Count
Source
41
64
        ObjValue(const T* value) : value(value) {}
_ZN5doris11ObjLRUCache8ObjValueINS_12FileMetaDataEEC2EPKS2_
Line
Count
Source
41
112
        ObjValue(const T* value) : value(value) {}
42
270
        ~ObjValue() override {
43
270
            T* v = (T*)value;
44
270
            delete v;
45
270
        }
_ZN5doris11ObjLRUCache8ObjValueIiED2Ev
Line
Count
Source
42
2
        ~ObjValue() override {
43
2
            T* v = (T*)value;
44
2
            delete v;
45
2
        }
_ZN5doris11ObjLRUCache8ObjValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEED2Ev
Line
Count
Source
42
92
        ~ObjValue() override {
43
92
            T* v = (T*)value;
44
92
            delete v;
45
92
        }
_ZN5doris11ObjLRUCache8ObjValueINS_6format7parquet21NativeParquetMetadataEED2Ev
Line
Count
Source
42
64
        ~ObjValue() override {
43
64
            T* v = (T*)value;
44
64
            delete v;
45
64
        }
_ZN5doris11ObjLRUCache8ObjValueINS_12FileMetaDataEED2Ev
Line
Count
Source
42
112
        ~ObjValue() override {
43
112
            T* v = (T*)value;
44
112
            delete v;
45
112
        }
46
47
        const T* value;
48
    };
49
50
    class CacheHandle {
51
    public:
52
1.93k
        CacheHandle() = default;
53
        CacheHandle(LRUCachePolicy* cache, Cache::Handle* handle)
54
408
                : _cache(cache), _handle(handle) {}
55
2.34k
        ~CacheHandle() {
56
2.34k
            if (_handle != nullptr) {
57
408
                _cache->release(_handle);
58
408
            }
59
2.34k
        }
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
880
        CacheHandle& operator=(CacheHandle&& other) noexcept {
67
880
            std::swap(_cache, other._cache);
68
880
            std::swap(_handle, other._handle);
69
880
            return *this;
70
880
        }
71
72
0
        bool valid() { return _cache != nullptr && _handle != nullptr; }
73
74
0
        LRUCachePolicy* cache() const { return _cache; }
75
76
        template <typename T>
77
428
        const T* data() const {
78
428
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
428
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataIiEEPKT_v
Line
Count
Source
77
4
        const T* data() const {
78
4
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
4
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEPKT_v
Line
Count
Source
77
38
        const T* data() const {
78
38
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
38
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINS_6format7parquet21NativeParquetMetadataEEEPKT_v
Line
Count
Source
77
128
        const T* data() const {
78
128
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
128
        }
_ZNK5doris11ObjLRUCache11CacheHandle4dataINS_12FileMetaDataEEEPKT_v
Line
Count
Source
77
258
        const T* data() const {
78
258
            return ((ObjValue<T>*)_cache->value(_handle))->value;
79
258
        }
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
270
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
270
        if (_enabled) {
96
270
            const std::string& encoded_key = key.key;
97
270
            auto* obj_value = new ObjValue<T>(value);
98
270
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
270
                                                  CachePriority::NORMAL);
100
270
            *cache_handle = CacheHandle {this, handle};
101
270
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
270
    }
_ZN5doris11ObjLRUCache6insertIiEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleE
Line
Count
Source
94
2
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
2
        if (_enabled) {
96
2
            const std::string& encoded_key = key.key;
97
2
            auto* obj_value = new ObjValue<T>(value);
98
2
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
2
                                                  CachePriority::NORMAL);
100
2
            *cache_handle = CacheHandle {this, handle};
101
2
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
2
    }
_ZN5doris11ObjLRUCache6insertINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleE
Line
Count
Source
94
92
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
92
        if (_enabled) {
96
92
            const std::string& encoded_key = key.key;
97
92
            auto* obj_value = new ObjValue<T>(value);
98
92
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
92
                                                  CachePriority::NORMAL);
100
92
            *cache_handle = CacheHandle {this, handle};
101
92
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
92
    }
_ZN5doris11ObjLRUCache6insertINS_6format7parquet21NativeParquetMetadataEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleE
Line
Count
Source
94
64
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
64
        if (_enabled) {
96
64
            const std::string& encoded_key = key.key;
97
64
            auto* obj_value = new ObjValue<T>(value);
98
64
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
64
                                                  CachePriority::NORMAL);
100
64
            *cache_handle = CacheHandle {this, handle};
101
64
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
64
    }
_ZN5doris11ObjLRUCache6insertINS_12FileMetaDataEEEvRKNS0_6ObjKeyEPKT_PNS0_11CacheHandleE
Line
Count
Source
94
112
    void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
95
112
        if (_enabled) {
96
112
            const std::string& encoded_key = key.key;
97
112
            auto* obj_value = new ObjValue<T>(value);
98
112
            auto* handle = LRUCachePolicy::insert(encoded_key, obj_value, 1, sizeof(T),
99
112
                                                  CachePriority::NORMAL);
100
112
            *cache_handle = CacheHandle {this, handle};
101
112
        } else {
102
0
            throw doris::Exception(ErrorCode::INTERNAL_ERROR,
103
0
                                   "ObjLRUCache disable, can not insert.");
104
0
        }
105
112
    }
106
107
    void erase(const ObjKey& key);
108
109
    bool exceed_prune_limit() override;
110
111
192
    bool enabled() const { return _enabled; }
112
113
private:
114
    const bool _enabled;
115
};
116
117
} // namespace doris