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

treeFromPath: simplify and better comments #1236

Merged
merged 2 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions idx/memory/find_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ func (c *FindCache) InvalidateFor(orgId uint32, path string) {
}

// convert our path to a tree so that we can call `find(tree, pattern)`
// for each pattern in the cache.
// for each pattern in the cache and purge it if it matches the path or a subtree of it.
// we can't simply prune all cache keys that equal path or a subtree of it, because
// what's cached are search patterns which may contain wildcards and other expressions
tree := treeFromPath(path)

for _, k := range cache.Keys() {
Expand Down Expand Up @@ -181,29 +183,23 @@ func (p *PartitionedMemoryIdx) PurgeFindCache() {
}
}

// treeFromPath creates a index tree from a series path.
// treeFromPath creates an index tree from a series path.
// The tree will have a single leaf node and nodes for
// each branch.
func treeFromPath(path string) *Tree {
tree := &Tree{
tree := Tree{
Items: map[string]*Node{
"": {
Path: "",
Children: make([]string, 0),
Defs: make([]schema.MKey, 0),
},
"": {},
},
}
pos := strings.Index(path, ".")
prevPos := 0
var parentBranch string
prevPos := -1
for {
branch := path[:pos]
// add as child of parent branch
thisNode := branch[prevPos+1:]
if prevPos == 0 {
thisNode = branch[prevPos:]
}
tree.Items[path[:prevPos]].Children = []string{thisNode}

tree.Items[parentBranch].Children = []string{thisNode}

// create this branch/leaf
tree.Items[branch] = &Node{
Expand All @@ -220,7 +216,8 @@ func treeFromPath(path string) *Tree {
} else {
pos = pos + nextPos + 1
}
parentBranch = branch
}

return tree
return &tree
}
11 changes: 11 additions & 0 deletions idx/memory/find_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ func TestFindCache(t *testing.T) {
})

}

func BenchmarkTreeFromPath(b *testing.B) {
numPaths := 1000
paths := getSeriesNames(10, numPaths, "benchmark")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := i % numPaths
treeFromPath(paths[p])
}
}