Skip to content

Commit

Permalink
refactor: simplify joinResults.add*
Browse files Browse the repository at this point in the history
It turns out that our usage of joinResults.add* only ever ends
creating minimal nodes, from just an id and topology. Hence
joinResults.add* can be invoked with simply an id and topology instead
of a generic node creation function.
  • Loading branch information
rade committed Dec 28, 2017
1 parent 521ce66 commit aa5be8d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
9 changes: 5 additions & 4 deletions render/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c connectionJoin) Render(rpt report.Report) Nodes {
// Nodes without a hostid may be pseudo nodes - if so, pass through to result
if _, ok := m.Latest.Lookup(report.HostNodeID); !ok {
if id, ok := externalNodeID(m, addr, local); ok {
ret.addChild(m, id, newPseudoNode)
ret.addChild(m, id, Pseudo)
continue
}
}
Expand All @@ -93,9 +93,10 @@ func (c connectionJoin) Render(rpt report.Report) Nodes {
if !found {
id, found = ipNodes[report.MakeScopedEndpointNodeID(scope, addr, port)]
}
if found && id != "" { // not one we blanked out earlier
// We are guaranteed to find the id, so no need to pass a node constructor.
ret.addChild(m, id, nil)
if found && id != "" { // not one we blanked out earlier We
// are guaranteed to find the id, so really this should
// never end up creating a node.
ret.addChild(m, id, report.Container)
}
}
return ret.result(endpoints)
Expand Down
12 changes: 4 additions & 8 deletions render/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ var HostRenderer = MakeReduce(
endpoints2Hosts{},
)

func newHostNode(id string) report.Node {
return report.MakeNode(id).WithTopology(report.Host)
}

// nodes2Hosts maps any Nodes to host Nodes.
//
// If this function is given a node without a hostname
Expand All @@ -45,9 +41,9 @@ func nodes2Hosts(nodes Nodes) Nodes {
// hosts, and hence mapping these adjacencies to host
// adjacencies would produce edges that aren't present
// in reality.
ret.addUnmappedChild(n, id, newHostNode)
ret.addUnmappedChild(n, id, report.Host)
} else {
ret.addChild(n, id, newHostNode)
ret.addChild(n, id, report.Host)
}
}
}
Expand All @@ -69,10 +65,10 @@ func (e endpoints2Hosts) Render(rpt report.Report) Nodes {
// Nodes without a hostid are treated as pseudo nodes
if hostNodeID, ok := n.Latest.Lookup(report.HostNodeID); !ok {
if id, ok := pseudoNodeID(n, local); ok {
ret.addChild(n, id, newPseudoNode)
ret.addChild(n, id, Pseudo)
}
} else {
ret.addChild(n, hostNodeID, newHostNode)
ret.addChild(n, hostNodeID, report.Host)
}
}
return ret.result(endpoints)
Expand Down
4 changes: 0 additions & 4 deletions render/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ func NewDerivedPseudoNode(id string, node report.Node) report.Node {
return output
}

func newPseudoNode(id string) report.Node {
return report.MakeNode(id).WithTopology(Pseudo)
}

func pseudoNodeID(n report.Node, local report.Networks) (string, bool) {
_, addr, _, ok := report.ParseEndpointNodeID(n.ID)
if !ok {
Expand Down
14 changes: 3 additions & 11 deletions render/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ var ProcessNameRenderer = CustomRenderer{RenderFunc: processes2Names, Renderer:
type endpoints2Processes struct {
}

func newProcessNode(id string) report.Node {
return report.MakeNode(id).WithTopology(report.Process)
}

func (e endpoints2Processes) Render(rpt report.Report) Nodes {
if len(rpt.Process.Nodes) == 0 {
return Nodes{}
Expand All @@ -93,7 +89,7 @@ func (e endpoints2Processes) Render(rpt report.Report) Nodes {
// Nodes without a hostid are treated as pseudo nodes
if hostNodeID, ok := n.Latest.Lookup(report.HostNodeID); !ok {
if id, ok := pseudoNodeID(n, local); ok {
ret.addChild(n, id, newPseudoNode)
ret.addChild(n, id, Pseudo)
}
} else {
pid, ok := n.Latest.Lookup(process.PID)
Expand All @@ -106,7 +102,7 @@ func (e endpoints2Processes) Render(rpt report.Report) Nodes {

hostID, _ := report.ParseHostNodeID(hostNodeID)
id := report.MakeProcessNodeID(hostID, pid)
ret.addChild(n, id, newProcessNode)
ret.addChild(n, id, report.Process)
}
}
return ret.result(endpoints)
Expand Down Expand Up @@ -141,10 +137,6 @@ func hasMoreThanOneConnection(n report.Node, endpoints report.Nodes) bool {

var processNameTopology = MakeGroupNodeTopology(report.Process, process.Name)

func newProcessNameNode(id string) report.Node {
return report.MakeNode(id).WithTopology(processNameTopology)
}

// processes2Names maps process Nodes to Nodes for each process name.
func processes2Names(processes Nodes) Nodes {
ret := newJoinResults(nil)
Expand All @@ -153,7 +145,7 @@ func processes2Names(processes Nodes) Nodes {
if n.Topology == Pseudo {
ret.passThrough(n)
} else if name, ok := n.Latest.Lookup(process.Name); ok {
ret.addChildAndChildren(n, name, newProcessNameNode)
ret.addChildAndChildren(n, name, processNameTopology)
}
}
return ret.result(processes)
Expand Down
21 changes: 11 additions & 10 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ func (ret *joinResults) mapChild(from, to string) {
}
}

// Add m as a child of the node at id, creating a new result node if
// not already there.
func (ret *joinResults) addUnmappedChild(m report.Node, id string, create func(string) report.Node) {
// 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) {
result, exists := ret.nodes[id]
if !exists {
result = create(id)
result = report.MakeNode(id).WithTopology(topology)
}
result.Children = result.Children.Add(m)
if m.Topology != report.Endpoint { // optimisation: we never look at endpoint counts
Expand All @@ -198,16 +198,17 @@ func (ret *joinResults) addUnmappedChild(m report.Node, id string, create func(s
ret.nodes[id] = result
}

// Add m as a child of the node at id, creating a new result node if
// not already there, and updating the mapping from old ID to new ID.
func (ret *joinResults) addChild(m report.Node, id string, create func(string) report.Node) {
ret.addUnmappedChild(m, id, create)
// Add m as a child of the node at id, creating a new result node in
// the specified topology if not already there, and updating the
// mapping from old ID to new ID.
func (ret *joinResults) addChild(m report.Node, id string, topology string) {
ret.addUnmappedChild(m, id, topology)
ret.mapChild(m.ID, id)
}

// Like addChild, but also add m's children.
func (ret *joinResults) addChildAndChildren(m report.Node, id string, create func(string) report.Node) {
ret.addUnmappedChild(m, id, create)
func (ret *joinResults) addChildAndChildren(m report.Node, id string, topology string) {
ret.addUnmappedChild(m, id, topology)
result := ret.nodes[id]
result.Children = result.Children.Merge(m.Children)
ret.nodes[id] = result
Expand Down

0 comments on commit aa5be8d

Please sign in to comment.