| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| |
| #include "query_cache.h" |
| |
| namespace doris { |
| |
| std::vector<int>* QueryCacheHandle::get_cache_slot_orders() { |
| DCHECK(_handle); |
| auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value; |
| return &((QueryCache::CacheValue*)(result_ptr))->slot_orders; |
| } |
| |
| CacheResult* QueryCacheHandle::get_cache_result() { |
| DCHECK(_handle); |
| auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value; |
| return &((QueryCache::CacheValue*)(result_ptr))->result; |
| } |
| |
| int64_t QueryCacheHandle::get_cache_version() { |
| DCHECK(_handle); |
| auto result_ptr = reinterpret_cast<LRUHandle*>(_handle)->value; |
| return ((QueryCache::CacheValue*)(result_ptr))->version; |
| } |
| |
| void QueryCache::insert(const CacheKey& key, int64_t version, CacheResult& res, |
| const std::vector<int>& slot_orders, int64_t cache_size) { |
| SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(ExecEnv::GetInstance()->query_cache_mem_tracker()); |
| CacheResult cache_result; |
| for (auto& block_data : res) { |
| cache_result.emplace_back(vectorized::Block::create_unique()) |
| ->swap(block_data->clone_empty()); |
| (void)vectorized::MutableBlock(cache_result.back().get()).merge(*block_data); |
| } |
| auto cache_value_ptr = |
| std::make_unique<QueryCache::CacheValue>(version, std::move(cache_result), slot_orders); |
| |
| QueryCacheHandle(this, LRUCachePolicy::insert(key, (void*)cache_value_ptr.release(), cache_size, |
| cache_size, CachePriority::NORMAL)); |
| } |
| |
| bool QueryCache::lookup(const CacheKey& key, int64_t version, doris::QueryCacheHandle* handle) { |
| SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(ExecEnv::GetInstance()->query_cache_mem_tracker()); |
| auto* lru_handle = LRUCachePolicy::lookup(key); |
| if (lru_handle) { |
| QueryCacheHandle tmp_handle(this, lru_handle); |
| if (tmp_handle.get_cache_version() == version) { |
| *handle = std::move(tmp_handle); |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| } // namespace doris |