data: Improved graph retrieval to use fixed number of requests

This commit is contained in:
Eberhard Graether
2016-03-30 12:26:19 +02:00
parent a662424e2b
commit cec370cfff
2 changed files with 115 additions and 95 deletions
+111 -89
View File
@@ -343,31 +343,32 @@ std::shared_ptr<Graph> Storage::getGraphForActiveTokenIds(const std::vector<Id>&
std::shared_ptr<Graph> g = std::make_shared<Graph>(); std::shared_ptr<Graph> g = std::make_shared<Graph>();
Graph* graph = g.get(); Graph* graph = g.get();
std::vector<Id> nodeIds;
std::vector<Id> edgeIds;
bool addAggregations = false;
if (tokenIds.size() == 1 && !activeOnly) if (tokenIds.size() == 1 && !activeOnly)
{ {
const Id elementId = tokenIds[0]; const Id elementId = tokenIds[0];
if (m_sqliteStorage.isNode(elementId)) if (m_sqliteStorage.isNode(elementId))
{ {
const StorageNode node = m_sqliteStorage.getNodeById(elementId); nodeIds.push_back(elementId);
addNodeAndAllChildrenToGraph(getLastVisibleParentNodeId(node.id), graph); std::vector<StorageEdge> edges = m_sqliteStorage.getEdgesBySourceOrTargetId(elementId);
for (const StorageEdge& edge : edges)
std::vector<StorageEdge> edges = m_sqliteStorage.getEdgesBySourceOrTargetId(node.id);
for (size_t i = 0; i < edges.size(); i++)
{ {
if (Edge::intToType(edges[i].type) != Edge::EDGE_MEMBER) if (Edge::intToType(edge.type) != Edge::EDGE_MEMBER)
{ {
addEdgeAndAllChildrenToGraph(edges[i].id, graph); edgeIds.push_back(edge.id);
} }
} }
addAggregationEdgesToGraph(elementId, graph); addAggregations = true;
} }
else if (m_sqliteStorage.isEdge(elementId)) else if (m_sqliteStorage.isEdge(elementId))
{ {
addEdgeAndAllChildrenToGraph(elementId, graph); edgeIds.push_back(elementId);
} }
} }
else if (tokenIds.size() >= 1) else if (tokenIds.size() >= 1)
@@ -378,15 +379,22 @@ std::shared_ptr<Graph> Storage::getGraphForActiveTokenIds(const std::vector<Id>&
if (m_sqliteStorage.isNode(elementId)) if (m_sqliteStorage.isNode(elementId))
{ {
addNodeAndAllChildrenToGraph(getLastVisibleParentNodeId(elementId), graph); nodeIds.push_back(elementId);
} }
else else
{ {
addEdgeAndAllChildrenToGraph(elementId, graph); edgeIds.push_back(elementId);
} }
} }
} }
addNodesAndEdgesToGraph(nodeIds, edgeIds, graph);
if (addAggregations)
{
addAggregationEdgesToGraph(tokenIds[0], graph);
}
addComponentAccessToGraph(graph); addComponentAccessToGraph(graph);
return g; return g;
@@ -759,47 +767,94 @@ std::vector<Id> Storage::getAllChildNodeIds(const Id nodeId) const
return childNodeIds; return childNodeIds;
} }
void Storage::addEdgeAndAllChildrenToGraph(const Id edgeId, Graph* graph) const void Storage::addNodesToGraph(const std::vector<Id>& nodeIds, Graph* graph) const
{ {
StorageEdge storageEdge = m_sqliteStorage.getEdgeById(edgeId); if (nodeIds.size() == 0)
Node* sourceNode = graph->getNodeById(storageEdge.sourceNodeId);
Node* targetNode = graph->getNodeById(storageEdge.targetNodeId);
if (!sourceNode)
{ {
addNodeAndAllChildrenToGraph(getLastVisibleParentNodeId(storageEdge.sourceNodeId), graph); return;
sourceNode = graph->getNodeById(storageEdge.sourceNodeId);
} }
if (!targetNode) std::vector<StorageNode> storageNodes = m_sqliteStorage.getNodesByIds(nodeIds);
{
addNodeAndAllChildrenToGraph(getLastVisibleParentNodeId(storageEdge.targetNodeId), graph);
targetNode = graph->getNodeById(storageEdge.targetNodeId);
}
graph->createEdge(edgeId, Edge::intToType(storageEdge.type), sourceNode, targetNode); for (const StorageNode& storageNode : storageNodes)
{
NameHierarchy nameHierarchy = NameHierarchy::deserialize(storageNode.serializedName);
Node::NodeType type = Node::intToType(storageNode.type);
Node* node = graph->createNode(
storageNode.id,
type,
nameHierarchy,
intToDefinitionType(storageNode.definitionType) != DEFINITION_NONE
);
if (type == Node::NODE_FUNCTION || type == Node::NODE_METHOD)
{
std::string signatureString = nameHierarchy.getRawNameWithSignature();
if (signatureString.size() > 0) // this should always be the case since functions and methods must have sigs.
{
node->addComponentSignature(
std::make_shared<TokenComponentSignature>(signatureString)
);
}
}
}
} }
Node* Storage::addNodeAndAllChildrenToGraph(const Id nodeId, Graph* graph) const void Storage::addEdgesToGraph(const std::vector<Id>& edgeIds, Graph* graph) const
{ {
Node* node = graph->getNodeById(nodeId); if (edgeIds.size() == 0)
if (node)
{ {
return node; return;
} }
std::vector<Id> nodeIdsToAdd; std::vector<StorageEdge> storageEdges = m_sqliteStorage.getEdgesByIds(edgeIds);
std::vector<Id> edgeIdsToAdd; for (const StorageEdge& storageEdge : storageEdges)
{
Node* sourceNode = graph->getNodeById(storageEdge.sourceNodeId);
Node* targetNode = graph->getNodeById(storageEdge.targetNodeId);
nodeIdsToAdd.push_back(nodeId); if (sourceNode && targetNode)
{
graph->createEdge(storageEdge.id, Edge::intToType(storageEdge.type), sourceNode, targetNode);
}
else
{
LOG_ERROR("Can't add edge because nodes are not present");
}
}
}
m_hierarchyCache.addAllChildIdsForNodeId(nodeId, &nodeIdsToAdd, &edgeIdsToAdd); void Storage::addNodesAndEdgesToGraph(const std::vector<Id>& nodeIds, const std::vector<Id>& edgeIds, Graph* graph) const
{
std::set<Id> parentNodeIds;
addNodesToGraph(nodeIdsToAdd, graph); for (Id nodeId : nodeIds)
addEdgesToGraph(edgeIdsToAdd, graph); {
parentNodeIds.insert(getLastVisibleParentNodeId(nodeId));
}
return graph->getNodeById(nodeId); if (edgeIds.size() > 0)
{
std::vector<StorageEdge> storageEdges = m_sqliteStorage.getEdgesByIds(edgeIds);
for (const StorageEdge& storageEdge : storageEdges)
{
parentNodeIds.insert(getLastVisibleParentNodeId(storageEdge.sourceNodeId));
parentNodeIds.insert(getLastVisibleParentNodeId(storageEdge.targetNodeId));
}
}
std::vector<Id> allNodeIds;
std::vector<Id> allEdgeIds = edgeIds;
for (Id parentNodeId : parentNodeIds)
{
allNodeIds.push_back(parentNodeId);
m_hierarchyCache.addAllChildIdsForNodeId(parentNodeId, &allNodeIds, &allEdgeIds);
}
addNodesToGraph(allNodeIds, graph);
addEdgesToGraph(allEdgeIds, graph);
} }
void Storage::addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const void Storage::addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const
@@ -813,6 +868,10 @@ void Storage::addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const
// build aggregation edges: // build aggregation edges:
// get all children of the active node // get all children of the active node
std::vector<Id> childNodeIds = getAllChildNodeIds(nodeId); std::vector<Id> childNodeIds = getAllChildNodeIds(nodeId);
if (childNodeIds.size() == 0)
{
return;
}
// get all edges of the children // get all edges of the children
std::map<Id, std::vector<EdgeInfo>> connectedNodeIds; std::map<Id, std::vector<EdgeInfo>> connectedNodeIds;
@@ -849,9 +908,20 @@ void Storage::addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const
} }
} }
// add hierarchies for these parents and create aggregation edges between parents and active node // add hierarchies of these parents
Node* sourceNode = graph->getNodeById(nodeId); std::vector<Id> nodeIdsToAdd;
for (const std::pair<Id, std::vector<EdgeInfo>> p : connectedParentNodeIds)
{
const Id aggregationTargetNodeId = p.first;
if (!graph->getNodeById(aggregationTargetNodeId))
{
nodeIdsToAdd.push_back(aggregationTargetNodeId);
}
}
addNodesAndEdgesToGraph(nodeIdsToAdd, std::vector<Id>(), graph);
// create aggregation edges between parents and active node
Node* sourceNode = graph->getNodeById(nodeId);
for (const std::pair<Id, std::vector<EdgeInfo>> p : connectedParentNodeIds) for (const std::pair<Id, std::vector<EdgeInfo>> p : connectedParentNodeIds)
{ {
const Id aggregationTargetNodeId = p.first; const Id aggregationTargetNodeId = p.first;
@@ -859,7 +929,7 @@ void Storage::addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const
Node* targetNode = graph->getNodeById(aggregationTargetNodeId); Node* targetNode = graph->getNodeById(aggregationTargetNodeId);
if (!targetNode) if (!targetNode)
{ {
targetNode = addNodeAndAllChildrenToGraph(aggregationTargetNodeId, graph); LOG_ERROR("Aggregation target node not present.");
} }
std::shared_ptr<TokenComponentAggregation> componentAggregation = std::make_shared<TokenComponentAggregation>(); std::shared_ptr<TokenComponentAggregation> componentAggregation = std::make_shared<TokenComponentAggregation>();
@@ -879,54 +949,6 @@ void Storage::addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const
} }
} }
void Storage::addNodesToGraph(const std::vector<Id> nodeIds, Graph* graph) const
{
std::vector<StorageNode> storageNodes = m_sqliteStorage.getNodesByIds(nodeIds);
for (const StorageNode& storageNode : storageNodes)
{
NameHierarchy nameHierarchy = NameHierarchy::deserialize(storageNode.serializedName);
Node::NodeType type = Node::intToType(storageNode.type);
Node* node = graph->createNode(
storageNode.id,
type,
nameHierarchy,
intToDefinitionType(storageNode.definitionType) != DEFINITION_NONE
);
if (type == Node::NODE_FUNCTION || type == Node::NODE_METHOD)
{
std::string signatureString = nameHierarchy.getRawNameWithSignature();
if (signatureString.size() > 0) // this should always be the case since functions and methods must have sigs.
{
node->addComponentSignature(
std::make_shared<TokenComponentSignature>(signatureString)
);
}
}
}
}
void Storage::addEdgesToGraph(const std::vector<Id> edgeIds, Graph* graph) const
{
std::vector<StorageEdge> storageEdges = m_sqliteStorage.getEdgesByIds(edgeIds);
for (const StorageEdge& storageEdge : storageEdges)
{
Node* sourceNode = graph->getNodeById(storageEdge.sourceNodeId);
Node* targetNode = graph->getNodeById(storageEdge.targetNodeId);
if (sourceNode && targetNode)
{
graph->createEdge(storageEdge.id, Edge::intToType(storageEdge.type), sourceNode, targetNode);
}
else
{
LOG_ERROR("Can't add edge because nodes are not present");
}
}
}
void Storage::addComponentAccessToGraph(Graph* graph) const void Storage::addComponentAccessToGraph(Graph* graph) const
{ {
std::vector<Id> memberEdgeIds; std::vector<Id> memberEdgeIds;
+4 -6
View File
@@ -93,13 +93,11 @@ private:
Id getLastVisibleParentNodeId(const Id nodeId) const; Id getLastVisibleParentNodeId(const Id nodeId) const;
std::vector<Id> getAllChildNodeIds(const Id nodeId) const; std::vector<Id> getAllChildNodeIds(const Id nodeId) const;
void addEdgeAndAllChildrenToGraph(const Id edgeId, Graph* graph) const; void addNodesToGraph(const std::vector<Id>& nodeIds, Graph* graph) const;
Node* addNodeAndAllChildrenToGraph(const Id nodeId, Graph* graph) const; void addEdgesToGraph(const std::vector<Id>& edgeIds, Graph* graph) const;
void addNodesAndEdgesToGraph(const std::vector<Id>& nodeIds, const std::vector<Id>& edgeIds, Graph* graph) const;
void addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const; void addAggregationEdgesToGraph(const Id nodeId, Graph* graph) const;
void addNodesToGraph(const std::vector<Id> nodeIds, Graph* graph) const;
void addEdgesToGraph(const std::vector<Id> edgeIds, Graph* graph) const;
void addComponentAccessToGraph(Graph* graph) const; void addComponentAccessToGraph(Graph* graph) const;
void buildSearchIndex(); void buildSearchIndex();