Coverage Report

Created: 2026-03-13 03:47

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