logic: refactored using Occurrences in PersistentStorage

This commit is contained in:
malte_langkabel
2016-12-19 14:20:54 +01:00
parent d9e6b23a8b
commit 79937b21f0
4 changed files with 124 additions and 216 deletions
-13
View File
@@ -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-----
+86 -55
View File
@@ -941,24 +941,37 @@ std::shared_ptr<TokenLocationCollection> PersistentStorage::getTokenLocationsFor
collection->addTokenLocationFile(m_sqliteStorage.getTokenLocationsForFile(storageFile.filePath));
}
for (const std::pair<StorageSourceLocation, Id>& 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<Id> locationIds;
std::unordered_map<Id, Id> 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<FilePath> PersistentStorage::getDependingFilePathsForIncludes(const std
std::set<FilePath> PersistentStorage::getDependingFilePathsForImports(const std::set<FilePath>& filePaths)
{
std::map<Id, std::set<Id>> fileIdToDependingFileIds;
std::multimap<Id, Id> importEdgeTargetIdToSourceIds;
std::vector<Id> importedTargetIds;
for (const StorageEdge& importEdge : m_sqliteStorage.getEdgesByType(Edge::typeToInt(Edge::EDGE_IMPORT)))
std::unordered_map<Id, std::set<Id>> fileIdToDependingFileIds;
{
importedTargetIds.push_back(importEdge.targetNodeId);
importEdgeTargetIdToSourceIds.emplace(importEdge.targetNodeId, importEdge.sourceNodeId);
}
std::vector<Id> importedElementIds;
std::map<Id, std::set<Id>> elementIdToImportingFileIds;
if (!importedTargetIds.size())
{
return std::set<FilePath>();
}
for (const std::pair<StorageSourceLocation, Id>& p :
m_sqliteStorage.getSourceLocationsAndElementIdsForElementIds(importedTargetIds))
{
std::pair <std::multimap<Id, Id>::const_iterator, std::multimap<Id, Id>::const_iterator> ret =
importEdgeTargetIdToSourceIds.equal_range(p.second);
for (std::multimap<Id, Id>::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<FilePath>();
}
std::unordered_map<Id, Id> importedElementIdToFileNodeId;
{
std::vector<Id> importedSourceLocationIds;
std::unordered_map<Id, Id> 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<Id> dependingFileNodeIds;
std::set<Id> working;
for (const FilePath& filePath: filePaths)
{
working.insert(getFileNodeId(filePath));
}
std::set<Id> tempWorking;
while (working.size() > 0)
{
for (Id id: working)
std::set<Id> working;
for (const FilePath& filePath: filePaths)
{
std::map<Id, std::set<Id>>::const_iterator it = fileIdToDependingFileIds.find(id);
if (it != fileIdToDependingFileIds.end())
working.insert(getFileNodeId(filePath));
}
std::set<Id> 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<FilePath> dependingFilePaths;
for (Id id: dependingFileNodeIds)
{
dependingFilePaths.insert(getFileNodePath(id));
+36 -144
View File
@@ -1,5 +1,7 @@
#include "data/SqliteStorage.h"
#include <unordered_map>
#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<StorageSourceLocation> SqliteStorage::getSourceLocationsByIds(const std::vector<Id> ids) const
{
return getAll<StorageSourceLocation>(
"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<StorageSourceLocation>(
@@ -643,17 +652,29 @@ std::shared_ptr<TokenLocationFile> SqliteStorage::getTokenLocationsForFile(const
return ret;
}
for (std::pair<StorageSourceLocation, Id> e: getAllSourceLocationsAndElementIdsForFileId(fileNodeId))
std::vector<Id> sourceLocationIds;
std::unordered_map<Id, StorageSourceLocation> sourceLocationIdToData;
for (const StorageSourceLocation& storageLocation: getAll<StorageSourceLocation>("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<TokenLocationFile> SqliteStorage::getTokenLocationsForFile(const
return ret;
}
std::vector<StorageSourceLocation> SqliteStorage::getSourceLocationsForElementId(const Id elementId) const
{
std::vector<Id> elementIds {elementId};
std::vector<StorageSourceLocation> ret;
for (std::pair<StorageSourceLocation, Id> e: getSourceLocationsAndElementIdsForElementIds(elementIds))
{
ret.push_back(e.first);
}
return ret;
}
std::vector<std::pair<StorageSourceLocation, Id>> SqliteStorage::getSourceLocationsAndElementIdsForElementIds(const std::vector<Id> elementIds) const
{
return getAllSourceLocationsAndElementIds("WHERE occurrence.element_id IN (" + utility::join(utility::toStrings(elementIds), ',') + ")");
}
std::vector<std::pair<StorageSourceLocation, Id>> 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<std::pair<StorageSourceLocation, Id>> 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<std::pair<StorageSourceLocation, Id>> 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<Id, StorageSourceLocation> locations;
std::vector<Id> 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<std::pair<StorageSourceLocation, Id>> 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<StorageOccurrence> SqliteStorage::getOccurrencesForLocationId(Id locationId) const
{
std::vector<Id> locationIds {locationId};
@@ -806,6 +693,11 @@ std::vector<StorageOccurrence> SqliteStorage::getOccurrencesForLocationIds(const
return getAll<StorageOccurrence>("WHERE source_location_id IN (" + utility::join(utility::toStrings(locationIds), ',') + ")");
}
std::vector<StorageOccurrence> SqliteStorage::getOccurrencesForElementIds(const std::vector<Id>& elementIds) const
{
return getAll<StorageOccurrence>("WHERE element_id IN (" + utility::join(utility::toStrings(elementIds), ',') + ")");
}
StorageComponentAccess SqliteStorage::getComponentAccessByNodeId(Id nodeId) const
{
return getFirst<StorageComponentAccess>("WHERE node_id == " + std::to_string(nodeId));
+2 -4
View File
@@ -105,15 +105,13 @@ public:
void setNodeDefinitionType(int definitionType, Id nodeId);
StorageSourceLocation getSourceLocationById(const Id id) const;
std::vector<StorageSourceLocation> getSourceLocationsByIds(const std::vector<Id> 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<TokenLocationFile> getTokenLocationsForFile(const FilePath& filePath) const;
std::vector<StorageSourceLocation> getSourceLocationsForElementId(const Id elementId) const;
std::vector<std::pair<StorageSourceLocation, Id>> getSourceLocationsAndElementIdsForElementIds(const std::vector<Id> elementIds) const;
std::vector<std::pair<StorageSourceLocation, Id>> getAllSourceLocationsAndElementIds(const std::string& query) const;
std::vector<std::pair<StorageSourceLocation, Id>> getAllSourceLocationsAndElementIdsForFileId(Id fileNodeId) const;
std::vector<StorageOccurrence> getOccurrencesForLocationId(Id locationId) const;
std::vector<StorageOccurrence> getOccurrencesForLocationIds(const std::vector<Id>& locationIds) const;
std::vector<StorageOccurrence> getOccurrencesForElementIds(const std::vector<Id>& elementIds) const;
StorageComponentAccess getComponentAccessByNodeId(Id memberEdgeId) const;
std::vector<StorageComponentAccess> getComponentAccessesByNodeIds(const std::vector<Id>& memberEdgeIds) const;