Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Merge branch 'FewerAllocs'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieterbe committed Jul 17, 2017
2 parents 8d7570a + 197ae4f commit 7e9bf99
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions idx/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,46 +191,37 @@ func (m *MemoryIdx) add(def *schema.MetricDefinition) idx.Archive {
return *archive
}
}

// now walk backwards through the node path to find the first branch which exists that
// this path extends.
nodes := strings.Split(path, ".")

// if we're trying to insert foo.bar.baz.quux then we see if we can insert it under (in this order):
// - foo.bar.baz (if found, startPos is 3)
// - foo.bar (if found, startPos is 2)
// - foo (if found, startPos is 1)
nodePos := 0 // the index of the first word that is not part of the prefix
var startNode *Node

for i := len(nodes) - 1; i > 0; i-- {
branch := strings.Join(nodes[0:i], ".")
pos := strings.LastIndex(path, ".")
prevPos := len(path)
for {
branch := path[:pos]
prevNode := path[pos+1 : prevPos]
if n, ok := tree.Items[branch]; ok {
log.Debug("memory-idx: Found branch %s which metricDef %s is a descendant of", branch, path)
startNode = n
nodePos = i
log.Debug("memory-idx: adding %s as child of %s", prevNode, n.Path)
n.Children = append(n.Children, prevNode)
break
}
}

if nodePos == 0 && startNode == nil {
// need to add to the root node.
log.Debug("memory-idx: no existing branches found for %s. Adding to the root node.", path)
startNode = tree.Items[""]
}

log.Debug("memory-idx: adding %s as child of %s", nodes[nodePos], startNode.Path)
startNode.Children = append(startNode.Children, nodes[nodePos])
nodePos++

// Add missing branch nodes
for ; nodePos < len(nodes); nodePos++ {
branch := strings.Join(nodes[0:nodePos], ".")
log.Debug("memory-idx: creating branch %s with child %s", branch, nodes[nodePos])
log.Debug("memory-idx: creating branch %s with child %s", branch, prevNode)
tree.Items[branch] = &Node{
Path: branch,
Children: []string{nodes[nodePos]},
Children: []string{prevNode},
Defs: make([]string, 0),
}

prevPos = pos
pos = strings.LastIndex(branch, ".")

if pos == -1 {
// need to add to the root node.
log.Debug("memory-idx: no existing branches found for %s. Adding to the root node.", branch)
n := tree.Items[""]
n.Children = append(n.Children, branch)
break
}
}

// Add leaf node
Expand Down

0 comments on commit 7e9bf99

Please sign in to comment.