forked from facesea/banshee
/
index.go
66 lines (60 loc) · 1.46 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2015 Eleme Inc. All rights reserved.
package models
// Index is for metric indexing, it records metric name for faster metric
// indexing. And metric latest informations like timstamp, score, average are
// also indexed.
type Index struct {
// Index may be cached.
cache `json:"-"`
// Metric name
Name string `json:"name"`
// Latest stamp for the metric.
Stamp uint32 `json:"stamp"`
// Latest trending score for the metric.
Score float64 `json:"score"`
// Latest average for the metric.
Average float64 `json:"average"`
// Link between index and metric.
Link uint32 `json:"link"`
// Matched rules.
MatchedRules []*Rule `json:"matchedRules,omitempty"`
}
// WriteMetric writes metric to index.
func (idx *Index) WriteMetric(m *Metric) {
idx.Lock()
defer idx.Unlock()
idx.Name = m.Name
idx.Stamp = m.Stamp
idx.Score = m.Score
idx.Average = m.Average
}
// Copy the index.
func (idx *Index) Copy() *Index {
i := &Index{}
idx.CopyTo(i)
return i
}
// CopyTo copy the index to another.
func (idx *Index) CopyTo(i *Index) {
idx.RLock()
defer idx.RUnlock()
i.Lock()
defer i.Unlock()
i.Name = idx.Name
i.Stamp = idx.Stamp
i.Score = idx.Score
i.Average = idx.Average
i.Link = idx.Link
}
// Equal tests the equality.
func (idx *Index) Equal(i *Index) bool {
idx.RLock()
defer idx.RUnlock()
i.RLock()
defer i.RUnlock()
return (idx.Name == i.Name &&
idx.Stamp == i.Stamp &&
idx.Score == i.Score &&
idx.Average == i.Average &&
idx.Link == i.Link)
}