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

Commit a19bf85

Browse files
committed
deduplicate code
1 parent 34161c9 commit a19bf85

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

idx/memory/memory.go

+26-30
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,25 @@ func (m *UnpartitionedMemoryIdx) idsByTagQuery(orgId uint32, query TagQuery) IdS
976976
return query.Run(tags, m.defById)
977977
}
978978

979+
func (m *UnpartitionedMemoryIdx) findMaybeCached(tree *Tree, orgId uint32, pattern string) ([]*Node, error) {
980+
981+
if m.findCache == nil {
982+
return find(tree, pattern)
983+
}
984+
985+
matchedNodes, ok := m.findCache.Get(orgId, pattern)
986+
if ok {
987+
return matchedNodes, nil
988+
}
989+
990+
matchedNodes, err := find(tree, pattern)
991+
if err != nil {
992+
return nil, err
993+
}
994+
m.findCache.Add(orgId, pattern, matchedNodes)
995+
return matchedNodes, nil
996+
}
997+
979998
func (m *UnpartitionedMemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node, error) {
980999
pre := time.Now()
9811000
var matchedNodes []*Node
@@ -986,42 +1005,19 @@ func (m *UnpartitionedMemoryIdx) Find(orgId uint32, pattern string, from int64)
9861005
if !ok {
9871006
log.Debugf("memory-idx: orgId %d has no metrics indexed.", orgId)
9881007
} else {
989-
if m.findCache != nil {
990-
matchedNodes, ok = m.findCache.Get(orgId, pattern)
991-
if !ok {
992-
matchedNodes, err = find(tree, pattern)
993-
if err != nil {
994-
return nil, err
995-
}
996-
m.findCache.Add(orgId, pattern, matchedNodes)
997-
}
998-
} else {
999-
matchedNodes, err = find(tree, pattern)
1000-
if err != nil {
1001-
return nil, err
1002-
}
1008+
matchedNodes, err = m.findMaybeCached(tree, orgId, pattern)
1009+
if err != nil {
1010+
return nil, err
10031011
}
10041012
}
10051013
if orgId != idx.OrgIdPublic && idx.OrgIdPublic > 0 {
10061014
tree, ok = m.tree[idx.OrgIdPublic]
10071015
if ok {
1008-
if m.findCache != nil {
1009-
publicNodes, ok := m.findCache.Get(idx.OrgIdPublic, pattern)
1010-
if !ok {
1011-
publicNodes, err = find(tree, pattern)
1012-
if err != nil {
1013-
return nil, err
1014-
}
1015-
m.findCache.Add(idx.OrgIdPublic, pattern, publicNodes)
1016-
}
1017-
matchedNodes = append(matchedNodes, publicNodes...)
1018-
} else {
1019-
publicNodes, err := find(tree, pattern)
1020-
if err != nil {
1021-
return nil, err
1022-
}
1023-
matchedNodes = append(matchedNodes, publicNodes...)
1016+
publicNodes, err := m.findMaybeCached(tree, idx.OrgIdPublic, pattern)
1017+
if err != nil {
1018+
return nil, err
10241019
}
1020+
matchedNodes = append(matchedNodes, publicNodes...)
10251021
}
10261022
}
10271023
log.Debugf("memory-idx: %d nodes matching pattern %s found", len(matchedNodes), pattern)

0 commit comments

Comments
 (0)