Coverage Report

Created: 2026-05-26 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/query_cache/query_cache.cpp
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
#include "runtime/query_cache/query_cache.h"
19
20
#include "common/logging.h"
21
22
namespace doris {
23
24
13
std::vector<int>* QueryCacheHandle::get_cache_slot_orders() {
25
13
    DCHECK(_handle);
26
13
    auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value;
27
13
    return &((QueryCache::CacheValue*)(result_ptr))->slot_orders;
28
13
}
29
30
13
CacheResult* QueryCacheHandle::get_cache_result() {
31
13
    DCHECK(_handle);
32
13
    auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value;
33
13
    return &((QueryCache::CacheValue*)(result_ptr))->result;
34
13
}
35
36
24
int64_t QueryCacheHandle::get_cache_version() {
37
24
    DCHECK(_handle);
38
24
    auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value;
39
24
    return ((QueryCache::CacheValue*)(result_ptr))->version;
40
24
}
41
42
void QueryCache::insert(const CacheKey& key, int64_t version, CacheResult& res,
43
7
                        const std::vector<int>& slot_orders, int64_t cache_size) {
44
7
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(ExecEnv::GetInstance()->query_cache_mem_tracker());
45
7
    CacheResult cache_result;
46
7
    for (auto& block_data : res) {
47
6
        cache_result.emplace_back(Block::create_unique())->swap(block_data->clone_empty());
48
6
        ScopedMutableBlock scoped_mutable_block(cache_result.back().get());
49
6
        auto& mutable_block = scoped_mutable_block.mutable_block();
50
6
        auto st = mutable_block.merge(*block_data);
51
6
        DORIS_CHECK(st.ok());
52
6
    }
53
7
    auto cache_value_ptr =
54
7
            std::make_unique<QueryCache::CacheValue>(version, std::move(cache_result), slot_orders);
55
56
7
    QueryCacheHandle(this, LRUCachePolicy::insert(key, (void*)cache_value_ptr.release(), cache_size,
57
7
                                                  cache_size, CachePriority::NORMAL));
58
7
}
59
60
32
bool QueryCache::lookup(const CacheKey& key, int64_t version, doris::QueryCacheHandle* handle) {
61
32
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(ExecEnv::GetInstance()->query_cache_mem_tracker());
62
32
    auto* lru_handle = LRUCachePolicy::lookup(key);
63
32
    if (lru_handle) {
64
22
        QueryCacheHandle tmp_handle(this, lru_handle);
65
22
        if (tmp_handle.get_cache_version() == version) {
66
22
            *handle = std::move(tmp_handle);
67
22
            return true;
68
22
        }
69
22
    }
70
10
    return false;
71
32
}
72
73
} // namespace doris