diff options
author | Marco Bubke <marco.bubke@qt.io> | 2019-08-06 13:12:03 +0200 |
---|---|---|
committer | Marco Bubke <marco.bubke@qt.io> | 2019-08-27 11:51:28 +0000 |
commit | f9fb4508d4b73b59b7a7a3c74c009cdccb24d21c (patch) | |
tree | e4ebcfda4cd6a0021ff7730187310a460057db8e | |
parent | 912b4763e00437d32759ba948e9bc678ff53b6be (diff) | |
download | qt-creator-f9fb4508d4b73b59b7a7a3c74c009cdccb24d21c.tar.gz |
Clang: Optimize file path cache
We now fetch all directories and sources from the database at file
path cache creation.
Change-Id: I92510b49a234128f4c82b840611db82ead3f1a54
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
23 files changed, 290 insertions, 205 deletions
diff --git a/src/libs/clangsupport/filepathcache.h b/src/libs/clangsupport/filepathcache.h index 4845416d22..1771f5b6c3 100644 --- a/src/libs/clangsupport/filepathcache.h +++ b/src/libs/clangsupport/filepathcache.h @@ -29,6 +29,7 @@ #include "filepath.h" #include "filepathexceptions.h" #include "filepathid.h" +#include "filepathstoragesources.h" #include "filepathview.h" #include "stringcache.h" @@ -39,84 +40,36 @@ namespace ClangBackEnd { template <typename FilePathStorage> class CLANGSUPPORT_GCCEXPORT FilePathCache { - class FileNameView - { - public: - friend bool operator==(const FileNameView &first, const FileNameView &second) - { - return first.directoryId == second.directoryId - && first.fileName == second.fileName; - } - - static - int compare(FileNameView first, FileNameView second) noexcept - { - int directoryDifference = first.directoryId - second.directoryId; - - if (directoryDifference) - return directoryDifference; - - return Utils::compare(first.fileName, second.fileName); - } - - public: - Utils::SmallStringView fileName; - int directoryId; - }; - - class FileNameEntry - { - public: - FileNameEntry(Utils::SmallStringView fileName, int directoryId) - : fileName(fileName), - directoryId(directoryId) - {} - - FileNameEntry(FileNameView view) - : fileName(view.fileName), - directoryId(view.directoryId) - {} - - friend bool operator==(const FileNameEntry &first, const FileNameEntry &second) - { - return first.directoryId == second.directoryId - && first.fileName == second.fileName; - } - - operator FileNameView() const - { - return {fileName, directoryId}; - } - - operator Utils::SmallString() && - { - return std::move(fileName); - } - - public: - Utils::SmallString fileName; - int directoryId; - }; - using DirectoryPathCache = StringCache<Utils::PathString, Utils::SmallStringView, int, SharedMutex, decltype(&Utils::reverseCompare), - Utils::reverseCompare>; + Utils::reverseCompare, + Sources::Directory>; using FileNameCache = StringCache<FileNameEntry, FileNameView, int, SharedMutex, decltype(&FileNameView::compare), - FileNameView::compare>; + FileNameView::compare, + Sources::Source>; + + FilePathCache(const FilePathCache &) = default; + FilePathCache &operator=(const FilePathCache &) = default; + public: FilePathCache(FilePathStorage &filePathStorage) : m_filePathStorage(filePathStorage) - {} + { + m_directoryPathCache.populate(filePathStorage.fetchAllDirectories()); + m_fileNameCache.populate(filePathStorage.fetchAllSources()); + } + + FilePathCache(FilePathCache &&) = default; + FilePathCache &operator=(FilePathCache &&) = default; - FilePathCache(const FilePathCache &) = delete; - FilePathCache &operator=(const FilePathCache &) = delete; + FilePathCache clone() { return *this; } FilePathId filePathId(FilePathView filePath) const { diff --git a/src/libs/clangsupport/filepathcaching.cpp b/src/libs/clangsupport/filepathcaching.cpp index 372ed86bd3..4464ea8201 100644 --- a/src/libs/clangsupport/filepathcaching.cpp +++ b/src/libs/clangsupport/filepathcaching.cpp @@ -52,4 +52,29 @@ DirectoryPathId FilePathCaching::directoryPathId(FilePathId filePathId) const return m_cache.directoryPathId(filePathId); } +FilePathId CopyableFilePathCaching::filePathId(FilePathView filePath) const +{ + return m_cache.filePathId(filePath); +} + +FilePath CopyableFilePathCaching::filePath(FilePathId filePathId) const +{ + return m_cache.filePath(filePathId); +} + +DirectoryPathId CopyableFilePathCaching::directoryPathId(Utils::SmallStringView directoryPath) const +{ + return m_cache.directoryPathId(directoryPath); +} + +Utils::PathString CopyableFilePathCaching::directoryPath(DirectoryPathId directoryPathId) const +{ + return m_cache.directoryPath(directoryPathId); +} + +DirectoryPathId CopyableFilePathCaching::directoryPathId(FilePathId filePathId) const +{ + return m_cache.directoryPathId(filePathId); +} + } // namespace ClangBackEnd diff --git a/src/libs/clangsupport/filepathcaching.h b/src/libs/clangsupport/filepathcaching.h index f69b940b15..1ca6522752 100644 --- a/src/libs/clangsupport/filepathcaching.h +++ b/src/libs/clangsupport/filepathcaching.h @@ -40,9 +40,11 @@ namespace ClangBackEnd { class CLANGSUPPORT_EXPORT FilePathCaching final : public FilePathCachingInterface { + friend class CopyableFilePathCaching; using Factory = FilePathStorageSqliteStatementFactory<Sqlite::Database>; using Storage = FilePathStorage<Factory>; using Cache = FilePathCache<Storage>; + public: FilePathCaching(Sqlite::Database &database) : m_factory(database) @@ -60,4 +62,25 @@ private: Cache m_cache{m_storage}; }; +class CLANGSUPPORT_EXPORT CopyableFilePathCaching final : public FilePathCachingInterface +{ + using Factory = FilePathStorageSqliteStatementFactory<Sqlite::Database>; + using Storage = FilePathStorage<Factory>; + using Cache = FilePathCache<Storage>; + +public: + CopyableFilePathCaching(FilePathCaching &cache) + : m_cache(cache.m_cache.clone()) + {} + + FilePathId filePathId(FilePathView filePath) const override; + FilePath filePath(FilePathId filePathId) const override; + DirectoryPathId directoryPathId(Utils::SmallStringView directoryPath) const override; + Utils::PathString directoryPath(DirectoryPathId directoryPathId) const override; + DirectoryPathId directoryPathId(FilePathId filePathId) const override; + +private: + Cache m_cache; +}; + } // namespace ClangBackEnd diff --git a/src/libs/clangsupport/filepathstorage.h b/src/libs/clangsupport/filepathstorage.h index 8aba2ef6d2..8434f50659 100644 --- a/src/libs/clangsupport/filepathstorage.h +++ b/src/libs/clangsupport/filepathstorage.h @@ -212,7 +212,7 @@ public: ReadStatement &statement = m_statementFactory.selectAllSources; - auto sources = statement.template values<Sources::Source, 2>(8192); + auto sources = statement.template values<Sources::Source, 3>(8192); transaction.commit(); diff --git a/src/libs/clangsupport/filepathstoragesources.h b/src/libs/clangsupport/filepathstoragesources.h index e76cb3b2cf..d81c23f99b 100644 --- a/src/libs/clangsupport/filepathstoragesources.h +++ b/src/libs/clangsupport/filepathstoragesources.h @@ -33,41 +33,106 @@ #include <unordered_map> namespace ClangBackEnd { -namespace Sources { -class Directory + +template<typename StringType, typename StringViewType, typename IndexType> +class StringCacheEntry { public: - Directory(int directoryId, Utils::PathString &&directoryPath) - : directoryId(directoryId), directoryPath(std::move(directoryPath)) + StringCacheEntry(StringViewType string, IndexType id) + : string(string) + , id(id) {} - friend - bool operator==(const Directory &first, const Directory &second) + operator StringViewType() const { return string; } + +public: + StringType string; + IndexType id; +}; + +class FileNameView +{ +public: + friend bool operator==(const FileNameView &first, const FileNameView &second) + { + return first.directoryId == second.directoryId && first.fileName == second.fileName; + } + + static int compare(FileNameView first, FileNameView second) noexcept { - return first.directoryId == second.directoryId && first.directoryPath == second.directoryPath; + int directoryDifference = first.directoryId - second.directoryId; + + if (directoryDifference) + return directoryDifference; + + return Utils::compare(first.fileName, second.fileName); } public: + Utils::SmallStringView fileName; int directoryId; - Utils::PathString directoryPath; }; -class Source +class FileNameEntry { public: - Source(int sourceId, Utils::PathString &&sourceName) - : sourceId(sourceId), sourceName(std::move(sourceName)) + FileNameEntry(Utils::SmallStringView fileName, int directoryId) + : fileName(fileName) + , directoryId(directoryId) + {} + + FileNameEntry(FileNameView view) + : fileName(view.fileName) + , directoryId(view.directoryId) {} - friend - bool operator==(const Source &first, const Source &second) + friend bool operator==(const FileNameEntry &first, const FileNameEntry &second) { - return first.sourceId == second.sourceId && first.sourceName == second.sourceName; + return first.directoryId == second.directoryId && first.fileName == second.fileName; } + friend bool operator!=(const FileNameEntry &first, const FileNameEntry &second) + { + return !(first == second); + } + + operator FileNameView() const { return {fileName, directoryId}; } + + operator Utils::SmallString() && { return std::move(fileName); } + public: - int sourceId; - Utils::PathString sourceName; + Utils::SmallString fileName; + int directoryId; +}; + +namespace Sources { +class Directory : public StringCacheEntry<Utils::PathString, Utils::SmallStringView, int> +{ + using Base = StringCacheEntry<Utils::PathString, Utils::SmallStringView, int>; + +public: + using Base::Base; + + friend bool operator==(const Directory &first, const Directory &second) + { + return first.id == second.id && first.string == second.string; + } +}; + +class Source : public StringCacheEntry<FileNameEntry, FileNameView, int> +{ + using Base = StringCacheEntry<FileNameEntry, FileNameView, int>; + +public: + using Base::Base; + Source(Utils::SmallStringView sourceName, int directoryId, int sourceId) + : Base{{sourceName, directoryId}, sourceId} + {} + + friend bool operator==(const Source &first, const Source &second) + { + return first.id == second.id && first.string == second.string; + } }; class SourceNameAndDirectoryId diff --git a/src/libs/clangsupport/filepathstoragesqlitestatementfactory.h b/src/libs/clangsupport/filepathstoragesqlitestatementfactory.h index 27296a1df6..f0d261dd49 100644 --- a/src/libs/clangsupport/filepathstoragesqlitestatementfactory.h +++ b/src/libs/clangsupport/filepathstoragesqlitestatementfactory.h @@ -50,13 +50,8 @@ public: database }; ReadStatement selectDirectoryPathFromDirectoriesByDirectoryId{ - "SELECT directoryPath FROM directories WHERE directoryId = ?", - database - }; - ReadStatement selectAllDirectories{ - "SELECT directoryId, directoryPath FROM directories", - database - }; + "SELECT directoryPath FROM directories WHERE directoryId = ?", database}; + ReadStatement selectAllDirectories{"SELECT directoryPath, directoryId FROM directories", database}; WriteStatement insertIntoDirectories{ "INSERT INTO directories(directoryPath) VALUES (?)", database @@ -75,10 +70,7 @@ public: "INSERT INTO sources(directoryId, sourceName) VALUES (?,?)", database }; - ReadStatement selectAllSources{ - "SELECT sourceId, sourceName FROM sources", - database - }; + ReadStatement selectAllSources{"SELECT sourceName, directoryId, sourceId FROM sources", database}; }; } // namespace ClangBackEnd diff --git a/src/libs/clangsupport/stringcache.h b/src/libs/clangsupport/stringcache.h index 9192b46aa5..5d416a46c4 100644 --- a/src/libs/clangsupport/stringcache.h +++ b/src/libs/clangsupport/stringcache.h @@ -25,9 +25,11 @@ #pragma once +#include "filepathstoragesources.h" #include "stringcachealgorithms.h" #include "stringcachefwd.h" +#include <utils/algorithm.h> #include <utils/optional.h> #include <utils/smallstringfwd.h> @@ -90,42 +92,17 @@ private: QReadWriteLock m_mutex; }; -template <typename StringType, - typename StringViewType, - typename IndexType> -class StringCacheEntry -{ -public: - StringCacheEntry(StringType &&string, IndexType id) - : string(std::move(string)), - id(id) - {} - - operator StringViewType() const - { - return string; - } - - StringType string; - IndexType id; -}; - -template <typename StringType, - typename StringViewType, - typename IndexType> -using StringCacheEntries = std::vector<StringCacheEntry<StringType, StringViewType, IndexType>>; - -template <typename StringType, - typename StringViewType, - typename IndexType, - typename Mutex, - typename Compare, - Compare compare = Utils::compare> +template<typename StringType, + typename StringViewType, + typename IndexType, + typename Mutex, + typename Compare, + Compare compare = Utils::compare, + typename CacheEntry = StringCacheEntry<StringType, StringViewType, IndexType>> class StringCache { public: - using CacheEntry = StringCacheEntry<StringType, StringViewType, IndexType>; - using CacheEntries = StringCacheEntries<StringType, StringViewType, IndexType>; + using CacheEntries = std::vector<CacheEntry>; using const_iterator = typename CacheEntries::const_iterator; using Found = ClangBackEnd::Found<const_iterator>; @@ -135,8 +112,19 @@ public: m_indices.reserve(reserveSize); } - StringCache(const StringCache &) = delete; - StringCache &operator=(const StringCache &) = delete; + StringCache(const StringCache &other) + : m_strings(other.m_strings) + , m_indices(other.m_indices) + {} + StringCache &operator=(const StringCache &other) + { + if (*this != other) { + m_strings = other.m_strings; + m_indices = other.m_indices; + } + + return *this; + } void populate(CacheEntries &&entries) { @@ -154,14 +142,25 @@ public: }); m_strings = std::move(entries); - m_indices.resize(m_strings.size()); + + int max_id = 0; + + auto found = std::max_element(m_strings.begin(), + m_strings.end(), + [](const auto &first, const auto &second) { + return first.id < second.id; + }); + + if (found != m_strings.end()) + max_id = found->id + 1; + + m_indices.resize(max_id, -1); auto begin = m_strings.cbegin(); for (auto current = begin; current != m_strings.end(); ++current) - m_indices.at(current->id) = std::distance(begin, current); + m_indices[current->id] = std::distance(begin, current); } - IndexType stringId(StringViewType stringView) { std::shared_lock<Mutex> sharedLock(m_mutex); @@ -176,7 +175,7 @@ public: found = find(stringView); if (!found.wasFound) { IndexType index = insertString(found.iterator, stringView, IndexType(m_indices.size())); - found.iterator = m_strings.begin() + index;; + found.iterator = m_strings.begin() + index; } return found.iterator->id; @@ -300,7 +299,7 @@ private: StringViewType stringView, IndexType id) { - auto inserted = m_strings.emplace(beforeIterator, StringType(stringView), id); + auto inserted = m_strings.emplace(beforeIterator, stringView, id); auto newIndex = IndexType(std::distance(m_strings.begin(), inserted)); diff --git a/src/libs/clangsupport/stringcachefwd.h b/src/libs/clangsupport/stringcachefwd.h index adc00ff1f5..2059b503b6 100644 --- a/src/libs/clangsupport/stringcachefwd.h +++ b/src/libs/clangsupport/stringcachefwd.h @@ -31,12 +31,7 @@ namespace ClangBackEnd { class NonLockingMutex; -template <typename StringType, - typename StringViewType, - typename IndexType, - typename Mutex, - typename Compare, - Compare compare> +template<typename StringType, typename StringViewType, typename IndexType, typename Mutex, typename Compare, Compare compare, typename CacheEntry> class StringCache; } // namespace ClangBackEnd diff --git a/src/tools/clangpchmanagerbackend/clangpchmanagerbackendmain.cpp b/src/tools/clangpchmanagerbackend/clangpchmanagerbackendmain.cpp index f98e87eaf0..1dd3f9acbe 100644 --- a/src/tools/clangpchmanagerbackend/clangpchmanagerbackendmain.cpp +++ b/src/tools/clangpchmanagerbackend/clangpchmanagerbackendmain.cpp @@ -139,13 +139,13 @@ public: using Processor = ClangBackEnd::PchCreator; PchCreatorManager(const ClangBackEnd::GeneratedFiles &generatedFiles, ClangBackEnd::Environment &environment, - Sqlite::Database &database, + ClangBackEnd::FilePathCaching &filePathCache, PchManagerServer &pchManagerServer, ClangBackEnd::ClangPathWatcherInterface &pathWatcher, ClangBackEnd::BuildDependenciesStorageInterface &buildDependenciesStorage) : ProcessorManager(generatedFiles) , m_environment(environment) - , m_database(database) + , m_filePathCache(filePathCache) , m_pchManagerServer(pchManagerServer) , m_pathWatcher(pathWatcher) , m_buildDependenciesStorage(buildDependenciesStorage) @@ -155,7 +155,7 @@ protected: std::unique_ptr<ClangBackEnd::PchCreator> createProcessor() const override { return std::make_unique<PchCreator>(m_environment, - m_database, + m_filePathCache, *m_pchManagerServer.client(), m_pathWatcher, m_buildDependenciesStorage); @@ -163,7 +163,7 @@ protected: private: ClangBackEnd::Environment &m_environment; - Sqlite::Database &m_database; + ClangBackEnd::FilePathCaching &m_filePathCache; ClangBackEnd::PchManagerServer &m_pchManagerServer; ClangBackEnd::ClangPathWatcherInterface &m_pathWatcher; ClangBackEnd::BuildDependenciesStorageInterface &m_buildDependenciesStorage; @@ -194,7 +194,7 @@ struct Data // because we have a cycle dependency generatedFiles}; PchCreatorManager pchCreatorManager{generatedFiles, environment, - database, + filePathCache, clangPchManagerServer, includeWatcher, buildDependencyStorage}; diff --git a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp index 9d0f338b87..f9e94fc14f 100644 --- a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp +++ b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp @@ -211,7 +211,7 @@ void PchCreator::doInMainThreadAfterFinished() } } -const FilePathCaching &PchCreator::filePathCache() +const FilePathCachingInterface &PchCreator::filePathCache() { return m_filePathCache; } diff --git a/src/tools/clangpchmanagerbackend/source/pchcreator.h b/src/tools/clangpchmanagerbackend/source/pchcreator.h index bc217d59e7..af8bf8bfdd 100644 --- a/src/tools/clangpchmanagerbackend/source/pchcreator.h +++ b/src/tools/clangpchmanagerbackend/source/pchcreator.h @@ -54,14 +54,16 @@ class PchCreator final : public PchCreatorInterface { public: PchCreator(Environment &environment, - Sqlite::Database &database, + FilePathCaching &filePathCache, PchManagerClientInterface &pchManagerClient, ClangPathWatcherInterface &clangPathwatcher, BuildDependenciesStorageInterface &buildDependenciesStorage) - : m_filePathCache(database), m_environment(environment), - m_pchManagerClient(pchManagerClient), - m_clangPathwatcher(clangPathwatcher), - m_buildDependenciesStorage(buildDependenciesStorage) {} + : m_filePathCache(filePathCache) + , m_environment(environment) + , m_pchManagerClient(pchManagerClient) + , m_clangPathwatcher(clangPathwatcher) + , m_buildDependenciesStorage(buildDependenciesStorage) + {} void generatePch(PchTask &&pchTask) override; const ProjectPartPch &projectPartPch() override; @@ -71,7 +73,7 @@ public: void clear() override; void doInMainThreadAfterFinished() override; - const FilePathCaching &filePathCache(); + const FilePathCachingInterface &filePathCache(); Utils::SmallString generatePchIncludeFileContent(const FilePathIds &includeIds) const; bool generatePch(NativeFilePathView path, Utils::SmallStringView content); @@ -93,7 +95,7 @@ private: mutable std::mt19937_64 randomNumberGenator{std::random_device{}()}; ClangTool m_clangTool; ProjectPartPch m_projectPartPch; - FilePathCaching m_filePathCache; + CopyableFilePathCaching m_filePathCache; FilePathIds m_watchedSystemIncludes; FilePathIds m_watchedProjectIncludes; FilePathIds m_watchedUserIncludes; diff --git a/src/tools/clangrefactoringbackend/source/symbolindexing.h b/src/tools/clangrefactoringbackend/source/symbolindexing.h index 936a34af00..e680a45be0 100644 --- a/src/tools/clangrefactoringbackend/source/symbolindexing.h +++ b/src/tools/clangrefactoringbackend/source/symbolindexing.h @@ -63,19 +63,19 @@ class SymbolsCollectorManager final : public ClangBackEnd::ProcessorManager<Symb public: using Processor = SymbolsCollector; SymbolsCollectorManager(const ClangBackEnd::GeneratedFiles &generatedFiles, - Sqlite::Database &database) - : ProcessorManager(generatedFiles), - m_database(database) + FilePathCaching &filePathCache) + : ProcessorManager(generatedFiles) + , m_filePathCache(filePathCache) {} protected: std::unique_ptr<SymbolsCollector> createProcessor() const { - return std::make_unique<SymbolsCollector>(m_database); + return std::make_unique<SymbolsCollector>(m_filePathCache); } private: - Sqlite::Database &m_database; + FilePathCaching &m_filePathCache; }; class SymbolIndexing final : public SymbolIndexingInterface @@ -84,7 +84,7 @@ public: using BuildDependenciesStorage = ClangBackEnd::BuildDependenciesStorage<Sqlite::Database>; using SymbolStorage = ClangBackEnd::SymbolStorage<Sqlite::Database>; SymbolIndexing(Sqlite::Database &database, - FilePathCachingInterface &filePathCache, + FilePathCaching &filePathCache, const GeneratedFiles &generatedFiles, ProgressCounter::SetProgressCallback &&setProgressCallback, const Environment &environment) @@ -93,7 +93,7 @@ public: , m_precompiledHeaderStorage(database) , m_projectPartsStorage(database) , m_symbolStorage(database) - , m_collectorManger(generatedFiles, database) + , m_collectorManger(generatedFiles, filePathCache) , m_progressCounter(std::move(setProgressCallback)) , m_indexer(m_indexerQueue, m_symbolStorage, diff --git a/src/tools/clangrefactoringbackend/source/symbolscollector.cpp b/src/tools/clangrefactoringbackend/source/symbolscollector.cpp index ac93ae360d..a233f6d8fe 100644 --- a/src/tools/clangrefactoringbackend/source/symbolscollector.cpp +++ b/src/tools/clangrefactoringbackend/source/symbolscollector.cpp @@ -31,8 +31,8 @@ namespace ClangBackEnd { -SymbolsCollector::SymbolsCollector(Sqlite::Database &database) - : m_filePathCache(database) +SymbolsCollector::SymbolsCollector(FilePathCaching &filePathCache) + : m_filePathCache(filePathCache) , m_indexDataConsumer(std::make_shared<IndexDataConsumer>(m_symbolEntries, m_sourceLocationEntries, m_filePathCache, diff --git a/src/tools/clangrefactoringbackend/source/symbolscollector.h b/src/tools/clangrefactoringbackend/source/symbolscollector.h index 1ad0c9e29c..824fe057fd 100644 --- a/src/tools/clangrefactoringbackend/source/symbolscollector.h +++ b/src/tools/clangrefactoringbackend/source/symbolscollector.h @@ -42,7 +42,7 @@ namespace ClangBackEnd { class SymbolsCollector final : public SymbolsCollectorInterface { public: - SymbolsCollector(Sqlite::Database &database); + SymbolsCollector(FilePathCaching &filePathCache); void addFiles(const FilePathIds &filePathIds, const Utils::SmallStringVector &arguments); @@ -65,7 +65,7 @@ public: bool isClean() const { return m_clangTool.isClean(); } private: - FilePathCaching m_filePathCache; + CopyableFilePathCaching m_filePathCache; ClangTool m_clangTool; SymbolEntries m_symbolEntries; SourceLocationEntries m_sourceLocationEntries; diff --git a/tests/unit/unittest/filepathcache-test.cpp b/tests/unit/unittest/filepathcache-test.cpp index 478e5f0215..8ec44064e4 100644 --- a/tests/unit/unittest/filepathcache-test.cpp +++ b/tests/unit/unittest/filepathcache-test.cpp @@ -57,12 +57,22 @@ protected: ON_CALL(mockStorage, fetchDirectoryPath(5)) .WillByDefault(Return(Utils::PathString("/path/to"))); ON_CALL(mockStorage, fetchSourceNameAndDirectoryId(42)) - .WillByDefault(Return(SourceNameAndDirectoryId("file.cpp", 5))); + .WillByDefault(Return(SourceNameAndDirectoryId("file.cpp", 5))); + ON_CALL(mockStorageFilled, fetchAllSources()) + .WillByDefault(Return(std::vector<ClangBackEnd::Sources::Source>({ + {"file.cpp", 6, 72}, + {"file2.cpp", 5, 63}, + {"file.cpp", 5, 42}, + }))); + ON_CALL(mockStorageFilled, fetchAllDirectories()) + .WillByDefault(Return( + std::vector<ClangBackEnd::Sources::Directory>({{"/path2/to", 6}, {"/path/to", 5}}))); } protected: NiceMock<MockFilePathStorage> mockStorage; Cache cache{mockStorage}; + NiceMock<MockFilePathStorage> mockStorageFilled; }; TEST_F(FilePathCache, FilePathIdWithOutAnyEntryCallDirectoryId) @@ -316,4 +326,48 @@ TEST_F(FilePathCache, FetchDirectoryPathIdAfterFetchingFilePathByFilePathId) ASSERT_THAT(directoryId, Eq(5)); } + +TEST_F(FilePathCache, FetchAllDirectoriesAndSourcesAtCreation) +{ + EXPECT_CALL(mockStorage, fetchAllDirectories()); + EXPECT_CALL(mockStorage, fetchAllSources()); + + Cache cache{mockStorage}; +} + +TEST_F(FilePathCache, GetFileIdInFilledCache) +{ + Cache cacheFilled{mockStorageFilled}; + + auto id = cacheFilled.filePathId("/path2/to/file.cpp"); + + ASSERT_THAT(id, Eq(72)); +} + +TEST_F(FilePathCache, GetDirectoryIdInFilledCache) +{ + Cache cacheFilled{mockStorageFilled}; + + auto id = cacheFilled.directoryPathId(42); + + ASSERT_THAT(id, Eq(5)); +} + +TEST_F(FilePathCache, GetDirectoryPathInFilledCache) +{ + Cache cacheFilled{mockStorageFilled}; + + auto path = cacheFilled.directoryPath(5); + + ASSERT_THAT(path, Eq("/path/to")); +} + +TEST_F(FilePathCache, GetFilePathInFilledCache) +{ + Cache cacheFilled{mockStorageFilled}; + + auto path = cacheFilled.filePath(42); + + ASSERT_THAT(path, Eq("/path/to/file.cpp")); +} } // namespace diff --git a/tests/unit/unittest/filepathstorage-test.cpp b/tests/unit/unittest/filepathstorage-test.cpp index fb60e32512..309b5d0c70 100644 --- a/tests/unit/unittest/filepathstorage-test.cpp +++ b/tests/unit/unittest/filepathstorage-test.cpp @@ -56,9 +56,8 @@ protected: .WillByDefault(Return(Utils::optional<int>(5))); ON_CALL(mockDatabase, lastInsertedRowId()) .WillByDefault(Return(12)); - ON_CALL(selectAllDirectories, - valuesReturnStdVectorDirectory(_)) - .WillByDefault(Return(std::vector<Directory>{{1, "/path/to"}, {2, "/other/path"}})); + ON_CALL(selectAllDirectories, valuesReturnStdVectorDirectory(_)) + .WillByDefault(Return(std::vector<Directory>{{"/path/to", 1}, {"/other/path", 2}})); ON_CALL(selectSourceIdFromSourcesByDirectoryIdAndSourceName, valueReturnInt32(_, _)) .WillByDefault(Return(Utils::optional<int>())); @@ -68,9 +67,8 @@ protected: ON_CALL(selectSourceIdFromSourcesByDirectoryIdAndSourceName, valueReturnInt32(5, Utils::SmallStringView("file.h"))) .WillByDefault(Return(Utils::optional<int>(42))); - ON_CALL(selectAllSources, - valuesReturnStdVectorSource(_)) - .WillByDefault(Return(std::vector<Source>{{1, "file.h"}, {4, "file.cpp"}})); + ON_CALL(selectAllSources, valuesReturnStdVectorSource(_)) + .WillByDefault(Return(std::vector<Source>{{"file.h", 1, 1}, {"file.cpp", 2, 4}})); ON_CALL(selectDirectoryPathFromDirectoriesByDirectoryId, valueReturnPathString(5)) .WillByDefault(Return(Utils::optional<Utils::PathString>("/path/to"))); @@ -407,16 +405,14 @@ TEST_F(FilePathStorage, SelectAllDirectories) { auto directories = storage.fetchAllDirectories(); - ASSERT_THAT(directories, - ElementsAre(Directory{1, "/path/to"}, Directory{2, "/other/path"})); + ASSERT_THAT(directories, ElementsAre(Directory{"/path/to", 1}, Directory{"/other/path", 2})); } TEST_F(FilePathStorage, SelectAllSources) { auto sources = storage.fetchAllSources(); - ASSERT_THAT(sources, - ElementsAre(Source{1, "file.h"}, Source{4, "file.cpp"})); + ASSERT_THAT(sources, ElementsAre(Source{"file.h", 1, 1}, Source{"file.cpp", 2, 4})); } TEST_F(FilePathStorage, CallSelectAllDirectories) diff --git a/tests/unit/unittest/filepathstoragesqlitestatementfactory-test.cpp b/tests/unit/unittest/filepathstoragesqlitestatementfactory-test.cpp index 40af2d99c3..42c1d0541a 100644 --- a/tests/unit/unittest/filepathstoragesqlitestatementfactory-test.cpp +++ b/tests/unit/unittest/filepathstoragesqlitestatementfactory-test.cpp @@ -73,12 +73,6 @@ TEST_F(FilePathStorageSqliteStatementFactory, SelectSourceNameAndDirectoryIdBySo Eq("SELECT sourceName, directoryId FROM sources WHERE sourceId = ?")); } -TEST_F(FilePathStorageSqliteStatementFactory, SelectAllDirectories) -{ - ASSERT_THAT(factory.selectAllDirectories.sqlStatement, - Eq("SELECT directoryId, directoryPath FROM directories")); -} - TEST_F(FilePathStorageSqliteStatementFactory, InsertIntoDirectories) { ASSERT_THAT(factory.insertIntoDirectories.sqlStatement, @@ -91,10 +85,4 @@ TEST_F(FilePathStorageSqliteStatementFactory, InsertIntoSources) Eq("INSERT INTO sources(directoryId, sourceName) VALUES (?,?)")); } -TEST_F(FilePathStorageSqliteStatementFactory, SelectAllSources) -{ - ASSERT_THAT(factory.selectAllSources.sqlStatement, - Eq("SELECT sourceId, sourceName FROM sources")); -} - } diff --git a/tests/unit/unittest/mockfilepathstorage.h b/tests/unit/unittest/mockfilepathstorage.h index 7056ee2480..4adb965e86 100644 --- a/tests/unit/unittest/mockfilepathstorage.h +++ b/tests/unit/unittest/mockfilepathstorage.h @@ -40,5 +40,7 @@ public: Utils::PathString (int directoryId)); MOCK_METHOD1(fetchSourceNameAndDirectoryId, ClangBackEnd::Sources::SourceNameAndDirectoryId (int sourceId)); + MOCK_METHOD0(fetchAllDirectories, std::vector<ClangBackEnd::Sources::Directory>()); + MOCK_METHOD0(fetchAllSources, std::vector<ClangBackEnd::Sources::Source>()); }; diff --git a/tests/unit/unittest/mocksqlitereadstatement.cpp b/tests/unit/unittest/mocksqlitereadstatement.cpp index eb68dcc9c9..9a949e4b0a 100644 --- a/tests/unit/unittest/mocksqlitereadstatement.cpp +++ b/tests/unit/unittest/mocksqlitereadstatement.cpp @@ -103,8 +103,8 @@ std::vector<Sources::Directory> MockSqliteReadStatement::values<Sources::Directo return valuesReturnStdVectorDirectory(reserveSize); } -template <> -std::vector<Sources::Source> MockSqliteReadStatement::values<Sources::Source, 2>(std::size_t reserveSize) +template<> +std::vector<Sources::Source> MockSqliteReadStatement::values<Sources::Source, 3>(std::size_t reserveSize) { return valuesReturnStdVectorSource(reserveSize); } diff --git a/tests/unit/unittest/mocksqlitereadstatement.h b/tests/unit/unittest/mocksqlitereadstatement.h index 5386c13907..8ebcb6e13b 100644 --- a/tests/unit/unittest/mocksqlitereadstatement.h +++ b/tests/unit/unittest/mocksqlitereadstatement.h @@ -223,8 +223,8 @@ FilePathIds MockSqliteReadStatement::values<ClangBackEnd::FilePathId>(std::size_ template <> std::vector<Sources::Directory> MockSqliteReadStatement::values<Sources::Directory, 2>(std::size_t reserveSize); -template <> -std::vector<Sources::Source> MockSqliteReadStatement::values<Sources::Source, 2>(std::size_t reserveSize); +template<> +std::vector<Sources::Source> MockSqliteReadStatement::values<Sources::Source, 3>(std::size_t reserveSize); template <> Utils::optional<int> diff --git a/tests/unit/unittest/pchcreator-test.cpp b/tests/unit/unittest/pchcreator-test.cpp index c5f7f7394d..e97f60b9f5 100644 --- a/tests/unit/unittest/pchcreator-test.cpp +++ b/tests/unit/unittest/pchcreator-test.cpp @@ -114,8 +114,9 @@ protected: NiceMock<MockPchManagerClient> mockPchManagerClient; NiceMock<MockClangPathWatcher> mockClangPathWatcher; NiceMock<MockBuildDependenciesStorage> mockBuildDependenciesStorage; + ClangBackEnd::FilePathCaching filePathCache{database}; ClangBackEnd::PchCreator creator{environment, - database, + filePathCache, mockPchManagerClient, mockClangPathWatcher, mockBuildDependenciesStorage}; diff --git a/tests/unit/unittest/stringcache-test.cpp b/tests/unit/unittest/stringcache-test.cpp index 102d851042..f8c7114e5e 100644 --- a/tests/unit/unittest/stringcache-test.cpp +++ b/tests/unit/unittest/stringcache-test.cpp @@ -194,9 +194,9 @@ TEST_F(StringCache, PopulateWithEmptyVector) TEST_F(StringCache, IsNotEmptyAfterPopulateWithSomeEntries) { CacheEntries entries{{filePath1.clone(), 0}, - {filePath2.clone(), 1}, + {filePath2.clone(), 3}, {filePath3.clone(), 2}, - {filePath4.clone(), 3}}; + {filePath4.clone(), 5}}; cache.uncheckedPopulate(std::move(entries)); @@ -207,11 +207,11 @@ TEST_F(StringCache, GetEntryAfterPopulateWithSomeEntries) { CacheEntries entries{{filePath1.clone(), 0}, {filePath2.clone(), 1}, - {filePath3.clone(), 2}, + {filePath3.clone(), 7}, {filePath4.clone(), 3}}; cache.uncheckedPopulate(std::move(entries)); - auto string = cache.string(2); + auto string = cache.string(7); ASSERT_THAT(string, filePath3); } @@ -226,16 +226,6 @@ TEST_F(StringCache, EntriesHaveUniqueIds) ASSERT_THROW(cache.populate(std::move(entries)), StringCacheException); } -TEST_F(StringCache, IdsAreHigherLowerEntriesSize) -{ - CacheEntries entries{{filePath1.clone(), 0}, - {filePath2.clone(), 1}, - {filePath3.clone(), 4}, - {filePath4.clone(), 3}}; - - ASSERT_THROW(cache.populate(std::move(entries)), std::out_of_range); -} - TEST_F(StringCache, MultipleEntries) { CacheEntries entries{{filePath1.clone(), 0}, diff --git a/tests/unit/unittest/symbolscollector-test.cpp b/tests/unit/unittest/symbolscollector-test.cpp index fca9ec88d0..cdefde2a31 100644 --- a/tests/unit/unittest/symbolscollector-test.cpp +++ b/tests/unit/unittest/symbolscollector-test.cpp @@ -177,7 +177,7 @@ protected: Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory}; ClangBackEnd::RefactoringDatabaseInitializer<Sqlite::Database> initializer{database}; FilePathCaching filePathCache{database}; - ClangBackEnd::SymbolsCollector collector{database}; + ClangBackEnd::SymbolsCollector collector{filePathCache}; }; TEST_F(SymbolsCollector, CollectSymbolName) |