@@ -222,7 +222,10 @@ func (m *MemoryIdx) Update(point schema.MetricPoint, partition int32) (idx.Archi
222
222
if LogLevel < 2 {
223
223
log .Debug ("metricDef with id %v already in index" , point .MKey )
224
224
}
225
- existing .LastUpdate = int64 (point .Time )
225
+
226
+ if existing .LastUpdate < int64 (point .Time ) {
227
+ existing .LastUpdate = int64 (point .Time )
228
+ }
226
229
existing .Partition = partition
227
230
statUpdate .Inc ()
228
231
statUpdateDuration .Value (time .Since (pre ))
@@ -244,7 +247,9 @@ func (m *MemoryIdx) AddOrUpdate(mkey schema.MKey, data *schema.MetricData, parti
244
247
if ok {
245
248
oldPart := existing .Partition
246
249
log .Debug ("metricDef with id %s already in index." , mkey )
247
- existing .LastUpdate = data .Time
250
+ if existing .LastUpdate < int64 (data .Time ) {
251
+ existing .LastUpdate = int64 (data .Time )
252
+ }
248
253
existing .Partition = partition
249
254
statUpdate .Inc ()
250
255
statUpdateDuration .Value (time .Since (pre ))
@@ -831,8 +836,9 @@ func (m *MemoryIdx) FindByTag(orgId uint32, expressions []string, from int64) ([
831
836
m .RLock ()
832
837
defer m .RUnlock ()
833
838
839
+ // construct the output slice of idx.Node's such that there is only 1 idx.Node for each path
834
840
ids := m .idsByTagQuery (orgId , query )
835
- res := make ([] idx.Node , 0 , len ( ids ) )
841
+ byPath := make (map [ string ] * idx.Node )
836
842
for id := range ids {
837
843
def , ok := m .defById [id ]
838
844
if ! ok {
@@ -841,14 +847,25 @@ func (m *MemoryIdx) FindByTag(orgId uint32, expressions []string, from int64) ([
841
847
continue
842
848
}
843
849
844
- res = append (res , idx.Node {
845
- Path : def .NameWithTags (),
846
- Leaf : true ,
847
- HasChildren : false ,
848
- Defs : []idx.Archive {* def },
849
- })
850
+ if existing , ok := byPath [def .NameWithTags ()]; ! ok {
851
+ byPath [def .NameWithTags ()] = & idx.Node {
852
+ Path : def .NameWithTags (),
853
+ Leaf : true ,
854
+ HasChildren : false ,
855
+ Defs : []idx.Archive {* def },
856
+ }
857
+ } else {
858
+ existing .Defs = append (existing .Defs , * def )
859
+ }
860
+ }
861
+
862
+ results := make ([]idx.Node , 0 , len (byPath ))
863
+
864
+ for _ , v := range byPath {
865
+ results = append (results , * v )
850
866
}
851
- return res , nil
867
+
868
+ return results , nil
852
869
}
853
870
854
871
func (m * MemoryIdx ) idsByTagQuery (orgId uint32 , query TagQuery ) IdSet {
@@ -877,11 +894,13 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
877
894
}
878
895
log .Debug ("memory-idx: %d nodes matching pattern %s found" , len (matchedNodes ), pattern )
879
896
results := make ([]idx.Node , 0 )
880
- 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.
881
900
// if there are public (orgId OrgIdPublic) and private leaf nodes with the same series
882
901
// path, then the public metricDefs will be excluded.
883
902
for _ , n := range matchedNodes {
884
- if _ , ok := seen [n .Path ]; ! ok {
903
+ if _ , ok := byPath [n .Path ]; ! ok {
885
904
idxNode := idx.Node {
886
905
Path : n .Path ,
887
906
Leaf : n .Leaf (),
@@ -904,7 +923,7 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
904
923
}
905
924
}
906
925
results = append (results , idxNode )
907
- seen [n .Path ] = struct {}{}
926
+ byPath [n .Path ] = struct {}{}
908
927
} else {
909
928
log .Debug ("memory-idx: path %s already seen" , n .Path )
910
929
}
@@ -914,6 +933,7 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
914
933
return results , nil
915
934
}
916
935
936
+ // find returns all Nodes matching the pattern for the given orgId
917
937
func (m * MemoryIdx ) find (orgId uint32 , pattern string ) ([]* Node , error ) {
918
938
tree , ok := m .tree [orgId ]
919
939
if ! ok {
0 commit comments