Use joinResults to accumulate nodes in Map.Render()

joinResults grew out of a special case of Map.Render(), so now we
merge the two lines back together and have just one way to accumulate
results and remap adjacencies.
This commit is contained in:
Bryan Boreham
2018-04-09 12:52:29 +00:00
parent e3539a8d92
commit dd087cff01

View File

@@ -99,38 +99,19 @@ func MakeMap(f MapFunc, r Renderer) Renderer {
// using a map function
func (m Map) Render(rpt report.Report) Nodes {
var (
input = m.Renderer.Render(rpt)
output = report.Nodes{}
mapped = map[string]report.IDList{} // input node ID -> output node IDs
adjacencies = map[string]report.IDList{} // output node ID -> input node Adjacencies
input = m.Renderer.Render(rpt)
output = newJoinResults(nil)
)
// Rewrite all the nodes according to the map function
for _, inRenderable := range input.Nodes {
outRenderable := m.MapFunc(inRenderable)
if outRenderable.ID != "" {
if existing, ok := output[outRenderable.ID]; ok {
outRenderable = outRenderable.Merge(existing)
}
output[outRenderable.ID] = outRenderable
mapped[inRenderable.ID] = mapped[inRenderable.ID].Add(outRenderable.ID)
adjacencies[outRenderable.ID] = adjacencies[outRenderable.ID].Merge(inRenderable.Adjacency)
output.add(inRenderable.ID, outRenderable)
}
}
// Rewrite Adjacency for new node IDs.
for outNodeID, inAdjacency := range adjacencies {
outAdjacency := report.MakeIDList()
for _, inAdjacent := range inAdjacency {
outAdjacency = outAdjacency.Merge(mapped[inAdjacent])
}
outNode := output[outNodeID]
outNode.Adjacency = outAdjacency
output[outNodeID] = outNode
}
return Nodes{Nodes: output}
return output.result(input)
}
func propagateLatest(key string, from, to report.Node) report.Node {
@@ -185,6 +166,15 @@ func (ret *joinResults) mapChild(from, to string) {
}
}
// Add m into the results as a top-level node, mapped from original ID
func (ret *joinResults) add(from string, m report.Node) {
if existing, ok := ret.nodes[m.ID]; ok {
m = m.Merge(existing)
}
ret.nodes[m.ID] = m
ret.mapChild(from, m.ID)
}
// Add m as a child of the node at id, creating a new result node in
// the specified topology if not already there.
func (ret *joinResults) addUnmappedChild(m report.Node, id string, topology string) {