From 79937b21f01224329354ce3d3e0bc7774db9b0c5 Mon Sep 17 00:00:00 2001 From: malte_langkabel Date: Mon, 19 Dec 2016 14:20:54 +0100 Subject: [PATCH] logic: refactored using Occurrences in PersistentStorage --- script/license.txt | 13 --- src/lib/data/PersistentStorage.cpp | 141 +++++++++++++--------- src/lib/data/SqliteStorage.cpp | 180 ++++++----------------------- src/lib/data/SqliteStorage.h | 6 +- 4 files changed, 124 insertions(+), 216 deletions(-) delete mode 100644 script/license.txt diff --git a/script/license.txt b/script/license.txt deleted file mode 100644 index 9f978e09..00000000 --- a/script/license.txt +++ /dev/null @@ -1,13 +0,0 @@ ------BEGIN LICENSE----- -Malte Langkabel -Private/Academic Single User License -Coati 0 -$9$AQAKVgXEpRqHfGMwWj/TA5YWrVmWWJrk5PmADbFYkewV7Ddvq+JV -nT+lDlImNpfRQDY4YTJDYc0B/IW5n8ccn21HqLkRvR5t8T5/GvUzRNZ -PnZfA/sE0tVBpS1IMEhUd5JIztHKXHFDdwcvIS7Z+y6vxn0knD0/VF4 -RjvvgicxikAnfF9cNxTb3IYAaUSREFTPejX6JABSai0jJhZLR4P9upK -rj6OYFerGI2/+6D2UyBJhaAc2P3zcd2ivd01IRjJNeXlGA3r5mCcl5y -SuPtreW3Yyq/uafyP1rV6uASIb+3oV6Y+jTo1kFYqEh8tjmrxOHbJLu -3+wHyJgMu5fLhJFWAKyxPjVbY5A0nvQAb0F+ROSZZ5IiKKCYIcofcxt -HrBYDDdP0rJw== ------END LICENSE----- \ No newline at end of file diff --git a/src/lib/data/PersistentStorage.cpp b/src/lib/data/PersistentStorage.cpp index 64893db5..4bf068ad 100644 --- a/src/lib/data/PersistentStorage.cpp +++ b/src/lib/data/PersistentStorage.cpp @@ -941,24 +941,37 @@ std::shared_ptr PersistentStorage::getTokenLocationsFor collection->addTokenLocationFile(m_sqliteStorage.getTokenLocationsForFile(storageFile.filePath)); } - for (const std::pair& e: m_sqliteStorage.getSourceLocationsAndElementIdsForElementIds(nonFileIds)) { - TokenLocation* loc = collection->addTokenLocation( - e.first.id, - e.second, - getFileNodePath(e.first.fileNodeId), - e.first.startLine, - e.first.startCol, - e.first.endLine, - e.first.endCol - ); - - if (loc) + std::vector locationIds; + std::unordered_map locationIdToElementIdMap; + for (const StorageOccurrence& occurrence: m_sqliteStorage.getOccurrencesForElementIds(nonFileIds)) { - loc->setType(intToLocationType(e.first.type)); + locationIds.push_back(occurrence.sourceLocationId); + locationIdToElementIdMap[occurrence.sourceLocationId] = occurrence.elementId; + } + + for (const StorageSourceLocation& sourceLocation: m_sqliteStorage.getSourceLocationsByIds(locationIds)) + { + auto it = locationIdToElementIdMap.find(sourceLocation.id); + if (it != locationIdToElementIdMap.end()) + { + TokenLocation* tokenLocation = collection->addTokenLocation( + sourceLocation.id, + it->second, + getFileNodePath(sourceLocation.fileNodeId), + sourceLocation.startLine, + sourceLocation.startCol, + sourceLocation.endLine, + sourceLocation.endCol + ); + + if (tokenLocation) + { + tokenLocation->setType(intToLocationType(sourceLocation.type)); + } + } } } - return collection; } @@ -1194,66 +1207,84 @@ std::set PersistentStorage::getDependingFilePathsForIncludes(const std std::set PersistentStorage::getDependingFilePathsForImports(const std::set& filePaths) { - std::map> fileIdToDependingFileIds; - - std::multimap importEdgeTargetIdToSourceIds; - std::vector importedTargetIds; - - for (const StorageEdge& importEdge : m_sqliteStorage.getEdgesByType(Edge::typeToInt(Edge::EDGE_IMPORT))) + std::unordered_map> fileIdToDependingFileIds; { - importedTargetIds.push_back(importEdge.targetNodeId); - importEdgeTargetIdToSourceIds.emplace(importEdge.targetNodeId, importEdge.sourceNodeId); - } + std::vector importedElementIds; + std::map> elementIdToImportingFileIds; - if (!importedTargetIds.size()) - { - return std::set(); - } - - for (const std::pair& p : - m_sqliteStorage.getSourceLocationsAndElementIdsForElementIds(importedTargetIds)) - { - std::pair ::const_iterator, std::multimap::const_iterator> ret = - importEdgeTargetIdToSourceIds.equal_range(p.second); - - for (std::multimap::const_iterator it = ret.first; it != ret.second; it++) + for (const StorageEdge& importEdge : m_sqliteStorage.getEdgesByType(Edge::typeToInt(Edge::EDGE_IMPORT))) { - fileIdToDependingFileIds[p.first.fileNodeId].insert(it->second); + importedElementIds.push_back(importEdge.targetNodeId); + elementIdToImportingFileIds[importEdge.targetNodeId].insert(importEdge.sourceNodeId); + } + + if (!importedElementIds.size()) + { + return std::set(); + } + + std::unordered_map importedElementIdToFileNodeId; + { + std::vector importedSourceLocationIds; + std::unordered_map importedSourceLocationToElementIds; + for (const StorageOccurrence& occurrence: m_sqliteStorage.getOccurrencesForElementIds(importedElementIds)) + { + importedSourceLocationIds.push_back(occurrence.sourceLocationId); + importedSourceLocationToElementIds[occurrence.sourceLocationId] = occurrence.elementId; + } + + for (const StorageSourceLocation& sourceLocation: m_sqliteStorage.getSourceLocationsByIds(importedSourceLocationIds)) + { + auto it = importedSourceLocationToElementIds.find(sourceLocation.id); + if (it != importedSourceLocationToElementIds.end()) + { + importedElementIdToFileNodeId[it->second] = sourceLocation.fileNodeId; + } + } + } + + for (const auto& it: elementIdToImportingFileIds) + { + auto importedFileIt = importedElementIdToFileNodeId.find(it.first); + if (importedFileIt != importedElementIdToFileNodeId.end()) + { + fileIdToDependingFileIds[importedFileIt->second].insert(it.second.begin(), it.second.end()); + } } } std::set dependingFileNodeIds; - - std::set working; - for (const FilePath& filePath: filePaths) { - working.insert(getFileNodeId(filePath)); - } - - std::set tempWorking; - while (working.size() > 0) - { - for (Id id: working) + std::set working; + for (const FilePath& filePath: filePaths) { - std::map>::const_iterator it = fileIdToDependingFileIds.find(id); - if (it != fileIdToDependingFileIds.end()) + working.insert(getFileNodeId(filePath)); + } + + std::set tempWorking; + while (working.size() > 0) + { + for (Id id: working) { - for (Id dependingFileNodeId: it->second) + auto it = fileIdToDependingFileIds.find(id); + if (it != fileIdToDependingFileIds.end()) { - bool inserted = dependingFileNodeIds.insert(dependingFileNodeId).second; - if (inserted) + for (Id dependingFileNodeId: it->second) { - tempWorking.insert(dependingFileNodeId); + bool inserted = dependingFileNodeIds.insert(dependingFileNodeId).second; + if (inserted) + { + tempWorking.insert(dependingFileNodeId); + } } } } + working = tempWorking; + tempWorking.clear(); } - working = tempWorking; - tempWorking.clear(); } std::set dependingFilePaths; - for (Id id: dependingFileNodeIds) { dependingFilePaths.insert(getFileNodePath(id)); diff --git a/src/lib/data/SqliteStorage.cpp b/src/lib/data/SqliteStorage.cpp index 1407e478..9f2ab5e5 100644 --- a/src/lib/data/SqliteStorage.cpp +++ b/src/lib/data/SqliteStorage.cpp @@ -1,5 +1,7 @@ #include "data/SqliteStorage.h" +#include + #include "data/graph/Node.h" #include "data/location/TokenLocation.h" #include "data/DefinitionType.h" @@ -621,6 +623,13 @@ StorageSourceLocation SqliteStorage::getSourceLocationById(const Id id) const ); } +std::vector SqliteStorage::getSourceLocationsByIds(const std::vector ids) const +{ + return getAll( + "WHERE id IN (" + utility::join(utility::toStrings(ids), ',') + ");" + ); +} + StorageSourceLocation SqliteStorage::getSourceLocationByAll(const Id fileNodeId, const uint startLine, const uint startCol, const uint endLine, const uint endCol, const int type) const { return getFirst( @@ -643,17 +652,29 @@ std::shared_ptr SqliteStorage::getTokenLocationsForFile(const return ret; } - for (std::pair e: getAllSourceLocationsAndElementIdsForFileId(fileNodeId)) + std::vector sourceLocationIds; + std::unordered_map sourceLocationIdToData; + for (const StorageSourceLocation& storageLocation: getAll("WHERE file_node_id == " + std::to_string(fileNodeId))) { - TokenLocation* loc = ret->addTokenLocation( - e.first.id, - e.second, - e.first.startLine, - e.first.startCol, - e.first.endLine, - e.first.endCol - ); - loc->setType(intToLocationType(e.first.type)); + sourceLocationIds.push_back(storageLocation.id); + sourceLocationIdToData[storageLocation.id] = storageLocation; + } + + for (const StorageOccurrence& occurrence: getOccurrencesForLocationIds(sourceLocationIds)) + { + auto it = sourceLocationIdToData.find(occurrence.sourceLocationId); + if (it != sourceLocationIdToData.end()) + { + TokenLocation* loc = ret->addTokenLocation( + it->second.id, //e.first.id, + occurrence.elementId, + it->second.startLine, + it->second.startCol, + it->second.endLine, + it->second.endCol + ); + loc->setType(intToLocationType(it->second.type)); + } } ret->isWholeCopy = true; @@ -661,140 +682,6 @@ std::shared_ptr SqliteStorage::getTokenLocationsForFile(const return ret; } -std::vector SqliteStorage::getSourceLocationsForElementId(const Id elementId) const -{ - std::vector elementIds {elementId}; - std::vector ret; - for (std::pair e: getSourceLocationsAndElementIdsForElementIds(elementIds)) - { - ret.push_back(e.first); - } - return ret; -} - -std::vector> SqliteStorage::getSourceLocationsAndElementIdsForElementIds(const std::vector elementIds) const -{ - return getAllSourceLocationsAndElementIds("WHERE occurrence.element_id IN (" + utility::join(utility::toStrings(elementIds), ',') + ")"); -} - -std::vector> SqliteStorage::getAllSourceLocationsAndElementIds(const std::string& query) const -{ - CppSQLite3Query q = executeQuery( - "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, " - "occurrence.element_id " - "FROM source_location " - "INNER JOIN occurrence ON occurrence.source_location_id = source_location.id " + query + ";" - ); - - std::vector> ret; - 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); - const Id elementId = q.getIntField(7, 0); - - if (id != 0 && fileNodeId != 0 && startLine != -1 && startColumn != -1 && endLine != -1 && endColumn != -1 && type != -1 && elementId != 0) - { - ret.push_back(std::make_pair( - StorageSourceLocation( - id, - fileNodeId, - startLine, - startColumn, - endLine, - endColumn, - type), - elementId - )); - } - q.nextRow(); - } - return ret; -} - -std::vector> SqliteStorage::getAllSourceLocationsAndElementIdsForFileId(Id fileNodeId) const -{ - CppSQLite3Query q = executeQuery( - "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) + ";" - ); - - 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 = executeQuery( - "SELECT " - "occurrence.element_id, " - "occurrence.source_location_id " - "FROM occurrence WHERE occurrence.source_location_id IN (" + utility::join(utility::toStrings(locationIds), ',') + ");" - ); - - 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}; @@ -806,6 +693,11 @@ std::vector SqliteStorage::getOccurrencesForLocationIds(const return getAll("WHERE source_location_id IN (" + utility::join(utility::toStrings(locationIds), ',') + ")"); } +std::vector SqliteStorage::getOccurrencesForElementIds(const std::vector& elementIds) const +{ + return getAll("WHERE element_id IN (" + utility::join(utility::toStrings(elementIds), ',') + ")"); +} + StorageComponentAccess SqliteStorage::getComponentAccessByNodeId(Id nodeId) const { return getFirst("WHERE node_id == " + std::to_string(nodeId)); diff --git a/src/lib/data/SqliteStorage.h b/src/lib/data/SqliteStorage.h index ca190753..625b6f54 100644 --- a/src/lib/data/SqliteStorage.h +++ b/src/lib/data/SqliteStorage.h @@ -105,15 +105,13 @@ public: void setNodeDefinitionType(int definitionType, Id nodeId); StorageSourceLocation getSourceLocationById(const Id id) const; + std::vector getSourceLocationsByIds(const std::vector ids) const; StorageSourceLocation getSourceLocationByAll(const Id fileNodeId, const uint startLine, const uint startCol, const uint endLine, const uint endCol, const int type) const; std::shared_ptr getTokenLocationsForFile(const FilePath& filePath) const; - 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; + std::vector getOccurrencesForElementIds(const std::vector& elementIds) const; StorageComponentAccess getComponentAccessByNodeId(Id memberEdgeId) const; std::vector getComponentAccessesByNodeIds(const std::vector& memberEdgeIds) const;