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

Commit

Permalink
deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieterbe committed Apr 15, 2019
1 parent 34161c9 commit 2574dea
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions idx/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,22 @@ func (m *UnpartitionedMemoryIdx) idsByTagQuery(orgId uint32, query TagQuery) IdS
return query.Run(tags, m.defById)
}

func (m *UnpartitionedMemoryIdx) findMaybeCached(nodes []*Node, tree *Tree, orgId uint32, pattern string) ([]*Node, error) {
if m.findCache == nil {
return find(tree, pattern)
}
matchedNodes, ok := m.findCache.Get(orgId, pattern)
if !ok {
matchedNodes, err := find(tree, pattern)
if err != nil {
return nil, err
}
m.findCache.Add(orgId, pattern, matchedNodes)
}
nodes = append(nodes, matchedNodes...)
return nodes, nil
}

func (m *UnpartitionedMemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node, error) {
pre := time.Now()
var matchedNodes []*Node
Expand All @@ -986,41 +1002,17 @@ func (m *UnpartitionedMemoryIdx) Find(orgId uint32, pattern string, from int64)
if !ok {
log.Debugf("memory-idx: orgId %d has no metrics indexed.", orgId)
} else {
if m.findCache != nil {
matchedNodes, ok = m.findCache.Get(orgId, pattern)
if !ok {
matchedNodes, err = find(tree, pattern)
if err != nil {
return nil, err
}
m.findCache.Add(orgId, pattern, matchedNodes)
}
} else {
matchedNodes, err = find(tree, pattern)
if err != nil {
return nil, err
}
matchedNodes, err = m.findMaybeCached(matchedNodes, tree, orgId, pattern)
if err != nil {
return nil, err
}
}
if orgId != idx.OrgIdPublic && idx.OrgIdPublic > 0 {
tree, ok = m.tree[idx.OrgIdPublic]
if ok {
if m.findCache != nil {
publicNodes, ok := m.findCache.Get(idx.OrgIdPublic, pattern)
if !ok {
publicNodes, err = find(tree, pattern)
if err != nil {
return nil, err
}
m.findCache.Add(idx.OrgIdPublic, pattern, publicNodes)
}
matchedNodes = append(matchedNodes, publicNodes...)
} else {
publicNodes, err := find(tree, pattern)
if err != nil {
return nil, err
}
matchedNodes = append(matchedNodes, publicNodes...)
matchedNodes, err = m.findMaybeCached(matchedNodes, tree, idx.OrgIdPublic, pattern)
if err != nil {
return nil, err
}
}
}
Expand Down

0 comments on commit 2574dea

Please sign in to comment.