From 814c6a899deb05a6b343629ffbf81058dbc5caed Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Mon, 2 Jan 2023 11:08:49 +0000 Subject: [PATCH] Simplify the code a bit --- src/Interpreters/Cache/QueryResultCache.cpp | 16 ++++++++-------- src/Interpreters/Cache/QueryResultCache.h | 4 +--- .../System/StorageSystemQueryResultCache.cpp | 4 ++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/Interpreters/Cache/QueryResultCache.cpp b/src/Interpreters/Cache/QueryResultCache.cpp index 07c79ac61cc..ea82f317878 100644 --- a/src/Interpreters/Cache/QueryResultCache.cpp +++ b/src/Interpreters/Cache/QueryResultCache.cpp @@ -180,8 +180,8 @@ try return res; }; - auto result = std::make_shared(to_single_chunk(partial_results)); - new_entry_size_in_bytes = result->allocatedBytes(); // updated because compression potentially affects the size of the single chunk vs the aggregate size of individual chunks + auto result = to_single_chunk(partial_results); + new_entry_size_in_bytes = result.allocatedBytes(); // updated because compression potentially affects the size of the single chunk vs the aggregate size of individual chunks std::lock_guard lock(mutex); @@ -200,7 +200,7 @@ try for (auto it = cache.begin(); it != cache.end();) if (is_stale(it->first)) { - cache_size_in_bytes -= it->second->allocatedBytes(); + cache_size_in_bytes -= it->second.allocatedBytes(); it = cache.erase(it); ++removed_items; } @@ -212,13 +212,13 @@ try /// Insert or replace if enough space if (sufficient_space_in_cache()) { - cache_size_in_bytes += result->allocatedBytes(); + cache_size_in_bytes += result.allocatedBytes(); if (auto it = cache.find(key); it != cache.end()) - cache_size_in_bytes -= it->second->allocatedBytes(); /// key replacement + cache_size_in_bytes -= it->second.allocatedBytes(); /// key replacement /// cache[key] = result; /// does no replacement for unclear reasons cache.erase(key); - cache[key] = result; + cache[key] = std::move(result); LOG_DEBUG(&Poco::Logger::get("QueryResultCache"), "Stored result of query {}", key.queryStringFromAst()); } @@ -260,14 +260,14 @@ QueryResultCache::Reader::Reader(const Cache & cache_, const Key & key, size_t & if (it->first.expires_at < std::chrono::system_clock::now()) { Cache & cache_rw = const_cast(cache_); - cache_size_in_bytes_ -= it->second->allocatedBytes(); + cache_size_in_bytes_ -= it->second.allocatedBytes(); cache_rw.erase(it); LOG_DEBUG(&Poco::Logger::get("QueryResultCache"), "Stale entry found and removed for query {}", key.queryStringFromAst()); return; } LOG_DEBUG(&Poco::Logger::get("QueryResultCache"), "Entry found for query {}", key.queryStringFromAst()); - pipe = Pipe(std::make_shared(key.header, it->second->clone())); + pipe = Pipe(std::make_shared(key.header, it->second.clone())); } bool QueryResultCache::Reader::hasCacheEntryForKey() const diff --git a/src/Interpreters/Cache/QueryResultCache.h b/src/Interpreters/Cache/QueryResultCache.h index 1b8f6d28cf4..fe3981c0dfd 100644 --- a/src/Interpreters/Cache/QueryResultCache.h +++ b/src/Interpreters/Cache/QueryResultCache.h @@ -66,10 +66,8 @@ private: size_t operator()(const Key & key) const; }; - using Entry = std::shared_ptr; - /// query --> query result - using Cache = std::unordered_map; + using Cache = std::unordered_map; /// query --> query execution count using TimesExecutedMap = std::unordered_map; diff --git a/src/Storages/System/StorageSystemQueryResultCache.cpp b/src/Storages/System/StorageSystemQueryResultCache.cpp index 3312475ded5..061fccc7539 100644 --- a/src/Storages/System/StorageSystemQueryResultCache.cpp +++ b/src/Storages/System/StorageSystemQueryResultCache.cpp @@ -38,7 +38,7 @@ void StorageSystemQueryResultCache::fillData(MutableColumns & res_columns, Conte std::lock_guard lock(query_result_cache->mutex); - for (const auto & [key, entry] : query_result_cache->cache) + for (const auto & [key, result] : query_result_cache->cache) { /// Showing other user's queries is considered a security risk if (key.username.has_value() && key.username != username) @@ -50,7 +50,7 @@ void StorageSystemQueryResultCache::fillData(MutableColumns & res_columns, Conte res_columns[3]->insert(key.expires_at < std::chrono::system_clock::now()); res_columns[4]->insert(!key.username.has_value()); res_columns[5]->insert(key.partition_key); - res_columns[6]->insert(entry->allocatedBytes()); + res_columns[6]->insert(result.allocatedBytes()); } }