AddParent() function to optimise the case where there is only one parent

Avoids creating and discarding a StringSet just to carry the
parameters, and makes the code more readable too.
This commit is contained in:
Bryan Boreham
2018-07-16 20:22:05 +00:00
parent 0d2409c72c
commit 5edc85822b
7 changed files with 14 additions and 18 deletions

View File

@@ -187,7 +187,7 @@ func (r Reporter) Tag(rpt report.Report) (report.Report, error) {
serviceID := report.MakeECSServiceNodeID(cluster, serviceName)
parentsSets = parentsSets.AddString(report.ECSService, serviceID)
// in addition, make service parent of task
rpt.ECSTask.Nodes[taskID] = rpt.ECSTask.Nodes[taskID].WithParents(report.MakeSets().Add(report.ECSService, report.MakeStringSet(serviceID)))
rpt.ECSTask.Nodes[taskID] = rpt.ECSTask.Nodes[taskID].WithParent(report.ECSService, serviceID)
}
for _, containerID := range info.ContainerIDs {
if containerNode, ok := rpt.Container.Nodes[containerID]; ok {

View File

@@ -407,9 +407,7 @@ func (c *container) getBaseNode() report.Node {
ContainerCommand: c.getSanitizedCommand(),
ImageID: c.Image(),
ContainerHostname: c.Hostname(),
}).WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.Image()))),
)
}).WithParent(report.ContainerImage, report.MakeContainerImageNodeID(c.Image()))
result = result.AddPrefixPropertyList(LabelPrefix, c.container.Config.Labels)
if !c.noEnvironmentVariables {
result = result.AddPrefixPropertyList(EnvPrefix, c.env())

View File

@@ -74,7 +74,7 @@ func (t *Tagger) Tag(r report.Report) (report.Report, error) {
})
r.SwarmService.AddNode(node)
r.Container.Nodes[containerID] = container.WithParents(container.Parents.Add(report.SwarmService, report.MakeStringSet(nodeID)))
r.Container.Nodes[containerID] = container.WithParent(report.SwarmService, nodeID)
}
return r, nil
@@ -116,17 +116,13 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
}
node = node.WithLatest(ContainerID, mtime.Now(), c.ID())
node = node.WithParents(report.MakeSets().
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID(c.ID()))),
)
node = node.WithParent(report.Container, report.MakeContainerNodeID(c.ID()))
// If we can work out the image name, add a parent tag for it
image, ok := t.registry.GetContainerImage(c.Image())
if ok && len(image.RepoTags) > 0 {
imageName := ImageNameWithoutTag(image.RepoTags[0])
node = node.WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(imageName))),
)
node = node.WithParent(report.ContainerImage, report.MakeContainerImageNodeID(imageName))
}
topology.ReplaceNode(node)

View File

@@ -26,14 +26,13 @@ func (Tagger) Name() string { return "Host" }
func (t Tagger) Tag(r report.Report) (report.Report, error) {
var (
metadata = map[string]string{report.HostNodeID: t.hostNodeID}
parents = report.MakeSets().Add(report.Host, report.MakeStringSet(t.hostNodeID))
)
// Explicitly don't tag Endpoints, Addresses and Overlay nodes - These topologies include pseudo nodes,
// and as such do their own host tagging.
for _, topology := range []report.Topology{r.Process, r.Container, r.ContainerImage, r.Host, r.Pod} {
for _, node := range topology.Nodes {
topology.ReplaceNode(node.WithLatests(metadata).WithParents(parents))
topology.ReplaceNode(node.WithLatests(metadata).WithParent(report.Host, t.hostNodeID))
}
}
return r, nil

View File

@@ -249,10 +249,7 @@ func (r *Reporter) Tag(rpt report.Report) (report.Report, error) {
n = n.WithLatest(report.DoesNotMakeConnections, mtime.Now(), "")
}
rpt.Container.Nodes[id] = n.WithParents(report.MakeSets().Add(
report.Pod,
report.MakeStringSet(report.MakePodNodeID(uid)),
))
rpt.Container.Nodes[id] = n.WithParent(report.Pod, report.MakePodNodeID(uid))
}
return rpt, nil
}

View File

@@ -330,7 +330,7 @@ func (w *Weave) addCurrentPeerInfo(latests map[string]string, node report.Node)
latests[WeavePluginDriver] = w.statusCache.Plugin.DriverName
}
node = node.AddPrefixMulticolumnTable(WeaveConnectionsMulticolumnTablePrefix, getConnectionsTable(w.statusCache.Router))
node = node.WithParents(report.MakeSets().Add(report.Host, report.MakeStringSet(w.hostID)))
node = node.WithParent(report.Host, w.hostID)
return latests, node
}

View File

@@ -152,6 +152,12 @@ func (n Node) WithLatestControl(control string, ts time.Time, data NodeControlDa
return n
}
// WithParent returns a fresh copy of n, with one parent added
func (n Node) WithParent(key, parent string) Node {
n.Parents = n.Parents.AddString(key, parent)
return n
}
// WithParents returns a fresh copy of n, with sets merged in.
func (n Node) WithParents(parents Sets) Node {
n.Parents = n.Parents.Merge(parents)