From 6d6d67470b2287600e884a371e2570393bef4005 Mon Sep 17 00:00:00 2001 From: malte_langkabel Date: Fri, 8 Jan 2016 12:20:27 +0100 Subject: [PATCH] data: performance optimization * added index for node names. * removed some unused methods of the SqliteStorage. --- src/lib/data/SqliteStorage.cpp | 82 +++++----------------------------- src/lib/data/SqliteStorage.h | 7 +-- src/lib/data/Storage.cpp | 2 +- 3 files changed, 13 insertions(+), 78 deletions(-) diff --git a/src/lib/data/SqliteStorage.cpp b/src/lib/data/SqliteStorage.cpp index bb24a7a7..1762951b 100644 --- a/src/lib/data/SqliteStorage.cpp +++ b/src/lib/data/SqliteStorage.cpp @@ -180,6 +180,13 @@ void SqliteStorage::removeElement(Id id) ).c_str()); } +void SqliteStorage::removeElements(const std::vector& ids) +{ + m_database.execDML(( + "DELETE FROM element WHERE id IN (" + utility::join(utility::toStrings(ids), ',') + ");" + ).c_str()); +} + void SqliteStorage::removeElementsWithLocationInFiles(const std::vector& fileIds) { CppSQLite3Query q = m_database.execQuery(( @@ -205,27 +212,6 @@ void SqliteStorage::removeElementsWithLocationInFiles(const std::vector& fil ).c_str()); } -void SqliteStorage::removeFile(Id id) -{ - if (isFile(id)) - { - m_database.execDML(( - "DELETE FROM element WHERE id == " + std::to_string(id) + ";" - ).c_str()); - } - else - { - LOG_WARNING("Removing file from DB failed since there is no file element with id " + std::to_string(id)); - } -} - -void SqliteStorage::removeFiles(const std::vector& fileIds) -{ - m_database.execDML(( - "DELETE FROM element WHERE id IN (" + utility::join(utility::toStrings(fileIds), ',') + ");" - ).c_str()); -} - void SqliteStorage::removeErrorsInFiles(const std::vector& filePaths) { m_database.execDML(( @@ -466,19 +452,6 @@ void SqliteStorage::setNodeDefined(bool defined, Id nodeId) ).c_str()); } -StorageSourceLocation SqliteStorage::getSourceLocationByData(Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, bool isScope) const -{ - return getFirstSourceLocation(( - "SELECT * FROM source_location WHERE element_id == " + std::to_string(elementId) - + " AND file_node_id == " + std::to_string(fileNodeId) - + " AND start_line == " + std::to_string(startLine) - + " AND start_column == " + std::to_string(startCol) - + " AND end_line == " + std::to_string(endLine) - + " AND end_column == " + std::to_string(endCol) - + " AND is_scope == " + std::to_string(isScope) + ";" - ).c_str()); -} - StorageSourceLocation SqliteStorage::getSourceLocationById(const Id id) const { return getFirstSourceLocation( @@ -486,13 +459,6 @@ StorageSourceLocation SqliteStorage::getSourceLocationById(const Id id) const ); } -std::vector SqliteStorage::getAllSourceLocations() const -{ - return getAllSourceLocations( - "SELECT * FROM source_location;" - ); -} - std::shared_ptr SqliteStorage::getTokenLocationsForFile(const FilePath& filePath) const { std::shared_ptr ret = std::make_shared(filePath); @@ -722,6 +688,10 @@ void SqliteStorage::setupTables() "defined INTEGER NOT NULL, " "PRIMARY KEY(id), " "FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE);" + ); + + m_database.execDML( + "CREATE INDEX IF NOT EXISTS node_serializedName_index ON node(serializedName);" ); m_database.execDML( @@ -886,36 +856,6 @@ StorageSourceLocation SqliteStorage::getFirstSourceLocation(const std::string& q return StorageSourceLocation(0, 0, 0, -1, -1, -1, -1, -1); } -std::vector SqliteStorage::getAllSourceLocations(const std::string& query) const -{ - std::vector sourceLocations; - - CppSQLite3Query q = m_database.execQuery(query.c_str()); - - while (!q.eof()) - { - const Id id = q.getIntField(0, 0); - const Id elementId = q.getIntField(1, 0); - const Id fileNodeId = q.getIntField(2, 0); - const int startLineNumber = q.getIntField(3, -1); - const int startColNumber = q.getIntField(4, -1); - const int endLineNumber = q.getIntField(5, -1); - const int endColNumber = q.getIntField(6, -1); - const int isScope = q.getIntField(7, -1); - - if (id != 0 && elementId != 0 && fileNodeId != 0 && startLineNumber != -1 && startColNumber != -1 && endLineNumber != -1 && endColNumber != -1 && isScope != -1) - { - sourceLocations.push_back(StorageSourceLocation( - id, elementId, fileNodeId, startLineNumber, startColNumber, endLineNumber, endColNumber, isScope - )); - } - - q.nextRow(); - } - - return sourceLocations; -} - std::vector SqliteStorage::getAllEdges(const std::string& query) const { CppSQLite3Query q = m_database.execQuery(( diff --git a/src/lib/data/SqliteStorage.h b/src/lib/data/SqliteStorage.h index b5e3f07f..394f38dd 100644 --- a/src/lib/data/SqliteStorage.h +++ b/src/lib/data/SqliteStorage.h @@ -44,11 +44,9 @@ public: Id addError(const std::string& message, const std::string& filePath, uint lineNumber, uint columnNumber); void removeElement(Id id); + void removeElements(const std::vector& ids); void removeElementsWithLocationInFiles(const std::vector& fileIds); - void removeFile(Id id); - void removeFiles(const std::vector& fileIds); - void removeCommentLocationsInFiles(const std::vector& filePaths); void removeErrorsInFiles(const std::vector& filePaths); StorageNode getFirstNode() const; @@ -84,9 +82,7 @@ public: void setNodeType(int type, Id nodeId); void setNodeDefined(bool defined, Id nodeId); - StorageSourceLocation getSourceLocationByData(Id elementId, Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, bool isScope) const; StorageSourceLocation getSourceLocationById(const Id id) const; - std::vector getAllSourceLocations() const; std::shared_ptr getTokenLocationsForFile(const FilePath& filePath) const; std::vector getTokenLocationsForElementId(const Id elementId) const; @@ -115,7 +111,6 @@ private: StorageFile getFirstFile(const std::string& query) const; std::vector getAllFiles(const std::string& query) const; StorageSourceLocation getFirstSourceLocation(const std::string& query) const; - std::vector getAllSourceLocations(const std::string& query) const; std::vector getAllEdges(const std::string& query) const; std::vector getAllNodes(const std::string& query) const; diff --git a/src/lib/data/Storage.cpp b/src/lib/data/Storage.cpp index 4fdf1cd6..a27bbd0c 100644 --- a/src/lib/data/Storage.cpp +++ b/src/lib/data/Storage.cpp @@ -94,7 +94,7 @@ void Storage::clearFileElements(const std::vector& filePaths) if (fileNodeIds.size()) { m_sqliteStorage.removeElementsWithLocationInFiles(fileNodeIds); - m_sqliteStorage.removeFiles(fileNodeIds); + m_sqliteStorage.removeElements(fileNodeIds); m_sqliteStorage.removeErrorsInFiles(filePaths); }