diff --git a/src/query/frame_change.hpp b/src/query/frame_change.hpp index 8da63c4f5..b522e5920 100644 --- a/src/query/frame_change.hpp +++ b/src/query/frame_change.hpp @@ -43,23 +43,22 @@ struct CachedValue { return cache_.get_allocator().GetMemoryResource(); } - // Func to check if cache_ contains value - bool CacheValue(const TypedValue &value) { - if (!value.IsList()) { + bool CacheValue(const TypedValue &maybe_list) { + if (!maybe_list.IsList()) { return false; } - const auto &list = value.ValueList(); + const auto &list = maybe_list.ValueList(); TypedValue::Hash hash{}; - for (const TypedValue &element : list) { - const auto key = hash(element); - auto &vector_values = cache_[key]; - if (!IsValueInVec(vector_values, element)) { - vector_values.push_back(element); + for (const TypedValue &list_elem : list) { + const auto list_elem_key = hash(list_elem); + auto &vector_values = cache_[list_elem_key]; + if (!IsValueInVec(vector_values, list_elem)) { + vector_values.push_back(list_elem); } } return true; } - // Func to cache_value inside cache_ + bool ContainsValue(const TypedValue &value) const { TypedValue::Hash hash{}; const auto key = hash(value); @@ -70,7 +69,7 @@ struct CachedValue { } private: - bool IsValueInVec(const std::vector &vec_values, const TypedValue &value) const { + static bool IsValueInVec(const std::vector &vec_values, const TypedValue &value) { return std::any_of(vec_values.begin(), vec_values.end(), [&value](auto &vec_value) { const auto is_value_equal = vec_value == value; if (is_value_equal.IsNull()) return false; @@ -80,43 +79,37 @@ struct CachedValue { }; // Class tracks keys for which user can cache values which help with faster search or faster retrieval -// in the future. +// in the future. Used for IN LIST operator. class FrameChangeCollector { public: - explicit FrameChangeCollector(utils::MemoryResource *mem) : tracked_values_(mem){}; + explicit FrameChangeCollector() : tracked_values_(&memory_resource_){}; - // Add tracking key to cache later value CachedValue &AddTrackingKey(const std::string &key) { const auto &[it, _] = tracked_values_.emplace(key, tracked_values_.get_allocator().GetMemoryResource()); return it->second; } - // Is key tracked bool IsKeyTracked(const std::string &key) const { return tracked_values_.contains(key); } - // Is value for given key cached bool IsKeyValueCached(const std::string &key) const { - return tracked_values_.contains(key) && !tracked_values_.at(key).cache_.empty(); + return IsKeyTracked(key) && !tracked_values_.at(key).cache_.empty(); } - // Reset value for tracking key bool ResetTrackingValue(const std::string &key) { - if (tracked_values_.contains(key)) { - tracked_values_.erase(key); - AddTrackingKey(key); + if (!tracked_values_.contains(key)) { + return false; } - + tracked_values_.erase(key); + AddTrackingKey(key); return true; } - // Get value cached for tracking key, throws if key is not in tracked CachedValue &GetCachedValue(const std::string &key) { return tracked_values_.at(key); } - // Checks for keys tracked bool IsTrackingValues() const { return !tracked_values_.empty(); } private: - // Key is output of utils::GetFrameChangeId, value is utils::pmr::unordered_map + utils::MonotonicBufferResource memory_resource_{0}; memgraph::utils::pmr::unordered_map tracked_values_; }; } // namespace memgraph::query diff --git a/src/query/interpreter.cpp b/src/query/interpreter.cpp index 1dab6f84c..20f0e330e 100644 --- a/src/query/interpreter.cpp +++ b/src/query/interpreter.cpp @@ -3761,7 +3761,7 @@ Interpreter::PrepareResult Interpreter::Prepare(const std::string &query_string, std::visit([](auto &execution_memory) -> utils::MemoryResource * { return &execution_memory; }, query_execution->execution_memory); frame_change_collector_.reset(); - frame_change_collector_.emplace(memory_resource); + frame_change_collector_.emplace(); if (utils::Downcast(parsed_query.query)) { prepared_query = PrepareCypherQuery(std::move(parsed_query), &query_execution->summary, interpreter_context_, current_db_, memory_resource, &query_execution->notifications, username_,