From 9df91853823246bd77c35f15829d03770879a263 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 28 Apr 2023 12:49:51 +0200 Subject: [PATCH] LocatorFileCache: Update the cache in one go Don't leave the partially updated cache when the generate() method was canceled. Before, it was possible that only m_filePaths were updated, without updating m_lastInput and m_cache, what could leave the cache in inconsistent state. Remove unneeded namespace qualifier. Some doc fixes. Amends 19918129bfe8839f1f54b26f546fc92cfeca729e Change-Id: Icef6389f45f0699d851ce412f134c93353728338 Reviewed-by: Eike Ziller --- .../coreplugin/locator/ilocatorfilter.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/plugins/coreplugin/locator/ilocatorfilter.cpp b/src/plugins/coreplugin/locator/ilocatorfilter.cpp index 4e427901385..b8e393839d3 100644 --- a/src/plugins/coreplugin/locator/ilocatorfilter.cpp +++ b/src/plugins/coreplugin/locator/ilocatorfilter.cpp @@ -1179,10 +1179,10 @@ public: LocatorFileCache::FilePathsGenerator m_generator; int m_executionId = 0; - std::optional m_filePaths; + std::optional m_filePaths; QString m_lastInput; - std::optional m_cache; + std::optional m_cache; }; // Clears all but provider @@ -1242,7 +1242,7 @@ static bool containsPathSeparator(const QString &candidate) \internal Uses the generator to update the cache if needed and returns entries for the input. - Uses the cached data when no need for re-generation. Updates the cached accordingly. + Uses the cached data when no need for re-generation. Updates the cache accordingly. */ LocatorFilterEntries LocatorFileCachePrivate::generate(const QFuture &future, const QString &input) @@ -1265,14 +1265,14 @@ LocatorFilterEntries LocatorFileCachePrivate::generate(const QFuture &futu const bool pathSeparatorAdded = !containsPathSeparator(m_lastInput) && hasPathSeparator; const bool searchInCache = m_filePaths && m_cache && containsLastInput && !pathSeparatorAdded; - if (!searchInCache && !m_filePaths) { - const FilePaths newPaths = m_generator(future); + std::optional newPaths = m_filePaths; + if (!searchInCache && !newPaths) { + newPaths = m_generator(future); if (future.isCanceled()) // Ensure we got not canceled results from generator. return {}; - m_filePaths = newPaths; } - const FilePaths &sourcePaths = searchInCache ? *m_cache : *m_filePaths; + const FilePaths &sourcePaths = searchInCache ? *m_cache : *newPaths; LocatorFileCache::MatchedEntries entries = {}; const FilePaths newCache = LocatorFileCache::processFilePaths( future, sourcePaths, hasPathSeparator, regExp, inputLink, &entries); @@ -1287,8 +1287,11 @@ LocatorFilterEntries LocatorFileCachePrivate::generate(const QFuture &futu if (future.isCanceled()) return {}; + // Update all the cache data in one go + m_filePaths = newPaths; m_lastInput = newInput; m_cache = newCache; + return std::accumulate(std::begin(entries), std::end(entries), LocatorFilterEntries()); } @@ -1400,6 +1403,7 @@ void LocatorFileCache::setFilePathsGenerator(const FilePathsGenerator &generator \note This function invalidates the cache temporarily, clearing all the cached data, and sets it to a valid state with the new generator for the passed \a filePaths. + The stored generator provider is preserved. \sa setGenerator */