From 42dad5f83ae3776de290ded6e3b51fb2afc3d9d5 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Wed, 7 Dec 2016 15:33:07 +0100 Subject: [PATCH] logic: Improved performance of source location retrieval for files --- src/lib/data/SqliteStorage.cpp | 73 +++++++++++++++++++++++++++++++++- src/lib/data/SqliteStorage.h | 1 + 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/lib/data/SqliteStorage.cpp b/src/lib/data/SqliteStorage.cpp index ea35fc11..2d3688fe 100644 --- a/src/lib/data/SqliteStorage.cpp +++ b/src/lib/data/SqliteStorage.cpp @@ -665,7 +665,7 @@ std::shared_ptr SqliteStorage::getTokenLocationsForFile(const return ret; } - for (std::pair e: getAllSourceLocationsAndElementIds("WHERE source_location.file_node_id == " + std::to_string(fileNodeId))) + for (std::pair e: getAllSourceLocationsAndElementIdsForFileId(fileNodeId)) { TokenLocation* loc = ret->addTokenLocation( e.first.id, @@ -746,6 +746,77 @@ std::vector> SqliteStorage::getAllSourceLoc return ret; } +std::vector> SqliteStorage::getAllSourceLocationsAndElementIdsForFileId(Id fileNodeId) const +{ + CppSQLite3Query q = m_database.execQuery(( + "SELECT " + "source_location.id, " + "source_location.file_node_id, " + "source_location.start_line, " + "source_location.start_column, " + "source_location.end_line, " + "source_location.end_column, " + "source_location.type " + "FROM source_location WHERE source_location.file_node_id == " + std::to_string(fileNodeId) + ";" + ).c_str()); + + std::map locations; + std::vector locationIds; + while (!q.eof()) + { + const Id id = q.getIntField(0, 0); + const Id fileNodeId = q.getIntField(1, 0); + const int startLine = q.getIntField(2, -1); + const int startColumn = q.getIntField(3, -1); + const int endLine = q.getIntField(4, -1); + const int endColumn = q.getIntField(5, -1); + const int type = q.getIntField(6, -1); + + if (id != 0 && fileNodeId != 0 && startLine != -1 && startColumn != -1 && endLine != -1 && endColumn != -1 && type != -1) + { + locationIds.push_back(id); + locations.emplace( + id, + StorageSourceLocation( + id, + fileNodeId, + startLine, + startColumn, + endLine, + endColumn, + type + ) + ); + } + q.nextRow(); + } + + CppSQLite3Query q2 = m_database.execQuery(( + "SELECT " + "occurrence.element_id, " + "occurrence.source_location_id " + "FROM occurrence WHERE occurrence.source_location_id IN (" + utility::join(utility::toStrings(locationIds), ',') + ");" + ).c_str()); + + std::vector> ret; + while (!q2.eof()) + { + const Id elementId = q2.getIntField(0, 0); + const Id sourceLocationId = q2.getIntField(1, 0); + + if (elementId != 0 && sourceLocationId != 0) + { + ret.push_back(std::make_pair( + locations[sourceLocationId], + elementId + )); + } + q2.nextRow(); + } + + return ret; +} + std::vector SqliteStorage::getOccurrencesForLocationId(Id locationId) const { std::vector locationIds {locationId}; diff --git a/src/lib/data/SqliteStorage.h b/src/lib/data/SqliteStorage.h index f791b72e..88d1bbb4 100644 --- a/src/lib/data/SqliteStorage.h +++ b/src/lib/data/SqliteStorage.h @@ -110,6 +110,7 @@ public: std::vector getSourceLocationsForElementId(const Id elementId) const; std::vector> getSourceLocationsAndElementIdsForElementIds(const std::vector elementIds) const; std::vector> getAllSourceLocationsAndElementIds(const std::string& query) const; + std::vector> getAllSourceLocationsAndElementIdsForFileId(Id fileNodeId) const; std::vector getOccurrencesForLocationId(Id locationId) const; std::vector getOccurrencesForLocationIds(const std::vector& locationIds) const;