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

Commit 2435f03

Browse files
committed
symmetrize Find() and FindByTag; comments
1 parent 6d8b4be commit 2435f03

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

idx/memory/memory.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,9 @@ func (m *MemoryIdx) FindByTag(orgId uint32, expressions []string, from int64) ([
836836
m.RLock()
837837
defer m.RUnlock()
838838

839+
// construct the output slice of idx.Node's such that there is only 1 idx.Node for each path
839840
ids := m.idsByTagQuery(orgId, query)
840-
nodesByPath := make(map[string]idx.Node)
841+
byPath := make(map[string]idx.Node)
841842
for id := range ids {
842843
def, ok := m.defById[id]
843844
if !ok {
@@ -846,8 +847,8 @@ func (m *MemoryIdx) FindByTag(orgId uint32, expressions []string, from int64) ([
846847
continue
847848
}
848849

849-
if existing, ok := nodesByPath[def.NameWithTags()]; !ok {
850-
nodesByPath[def.NameWithTags()] = idx.Node{
850+
if existing, ok := byPath[def.NameWithTags()]; !ok {
851+
byPath[def.NameWithTags()] = idx.Node{
851852
Path: def.NameWithTags(),
852853
Leaf: true,
853854
HasChildren: false,
@@ -858,13 +859,13 @@ func (m *MemoryIdx) FindByTag(orgId uint32, expressions []string, from int64) ([
858859
}
859860
}
860861

861-
res := make([]idx.Node, 0, len(nodesByPath))
862+
results := make([]idx.Node, 0, len(byPath))
862863

863-
for _, v := range nodesByPath {
864-
res = append(res, v)
864+
for _, v := range byPath {
865+
results = append(results, v)
865866
}
866867

867-
return res, nil
868+
return results, nil
868869
}
869870

870871
func (m *MemoryIdx) idsByTagQuery(orgId uint32, query TagQuery) IdSet {
@@ -893,11 +894,13 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
893894
}
894895
log.Debug("memory-idx: %d nodes matching pattern %s found", len(matchedNodes), pattern)
895896
results := make([]idx.Node, 0)
896-
seen := make(map[string]struct{})
897+
byPath := make(map[string]struct{})
898+
// construct the output slice of idx.Node's such that there is only 1 idx.Node
899+
// for each path, and it holds all defs that the Node refers too.
897900
// if there are public (orgId OrgIdPublic) and private leaf nodes with the same series
898901
// path, then the public metricDefs will be excluded.
899902
for _, n := range matchedNodes {
900-
if _, ok := seen[n.Path]; !ok {
903+
if _, ok := byPath[n.Path]; !ok {
901904
idxNode := idx.Node{
902905
Path: n.Path,
903906
Leaf: n.Leaf(),
@@ -920,7 +923,7 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
920923
}
921924
}
922925
results = append(results, idxNode)
923-
seen[n.Path] = struct{}{}
926+
byPath[n.Path] = struct{}{}
924927
} else {
925928
log.Debug("memory-idx: path %s already seen", n.Path)
926929
}
@@ -930,6 +933,7 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
930933
return results, nil
931934
}
932935

936+
// find returns all Nodes matching the pattern for the given orgId
933937
func (m *MemoryIdx) find(orgId uint32, pattern string) ([]*Node, error) {
934938
tree, ok := m.tree[orgId]
935939
if !ok {

0 commit comments

Comments
 (0)