Coverage Report

Created: 2026-03-15 17:28

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
namespace doris {
21
22
3
std::vector<int>* QueryCacheHandle::get_cache_slot_orders() {
23
3
    DCHECK(_handle);
24
3
    auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value;
25
3
    return &((QueryCache::CacheValue*)(result_ptr))->slot_orders;
26
3
}
27
28
3
CacheResult* QueryCacheHandle::get_cache_result() {
29
3
    DCHECK(_handle);
30
3
    auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value;
31
3
    return &((QueryCache::CacheValue*)(result_ptr))->result;
32
3
}
33
34
4
int64_t QueryCacheHandle::get_cache_version() {
35
4
    DCHECK(_handle);
36
4
    auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value;
37
4
    return ((QueryCache::CacheValue*)(result_ptr))->version;
38
4
}
39
40
void QueryCache::insert(const CacheKey& key, int64_t version, CacheResult& res,
41
3
                        const std::vector<int>& slot_orders, int64_t cache_size) {
42
3
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(ExecEnv::GetInstance()->query_cache_mem_tracker());
43
3
    CacheResult cache_result;
44
3
    for (auto& block_data : res) {
45
3
        cache_result.emplace_back(Block::create_unique())->swap(block_data->clone_empty());
46
3
        (void)MutableBlock(cache_result.back().get()).merge(*block_data);
47
3
    }
48
3
    auto cache_value_ptr =
49
3
            std::make_unique<QueryCache::CacheValue>(version, std::move(cache_result), slot_orders);
50
51
3
    QueryCacheHandle(this, LRUCachePolicy::insert(key, (void*)cache_value_ptr.release(), cache_size,
52
3
                                                  cache_size, CachePriority::NORMAL));
53
3
}
54
55
4
bool QueryCache::lookup(const CacheKey& key, int64_t version, doris::QueryCacheHandle* handle) {
56
4
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(ExecEnv::GetInstance()->query_cache_mem_tracker());
57
4
    auto* lru_handle = LRUCachePolicy::lookup(key);
58
4
    if (lru_handle) {
59
2
        QueryCacheHandle tmp_handle(this, lru_handle);
60
2
        if (tmp_handle.get_cache_version() == version) {
61
2
            *handle = std::move(tmp_handle);
62
2
            return true;
63
2
        }
64
2
    }
65
2
    return false;
66
4
}
67
68
} // namespace doris