@@ -976,6 +976,25 @@ func (m *UnpartitionedMemoryIdx) idsByTagQuery(orgId uint32, query TagQuery) IdS
976
976
return query .Run (tags , m .defById )
977
977
}
978
978
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
+
979
998
func (m * UnpartitionedMemoryIdx ) Find (orgId uint32 , pattern string , from int64 ) ([]idx.Node , error ) {
980
999
pre := time .Now ()
981
1000
var matchedNodes []* Node
@@ -986,42 +1005,19 @@ func (m *UnpartitionedMemoryIdx) Find(orgId uint32, pattern string, from int64)
986
1005
if ! ok {
987
1006
log .Debugf ("memory-idx: orgId %d has no metrics indexed." , orgId )
988
1007
} 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
1003
1011
}
1004
1012
}
1005
1013
if orgId != idx .OrgIdPublic && idx .OrgIdPublic > 0 {
1006
1014
tree , ok = m .tree [idx .OrgIdPublic ]
1007
1015
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
1024
1019
}
1020
+ matchedNodes = append (matchedNodes , publicNodes ... )
1025
1021
}
1026
1022
}
1027
1023
log .Debugf ("memory-idx: %d nodes matching pattern %s found" , len (matchedNodes ), pattern )
0 commit comments