-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
101 lines (78 loc) · 1.75 KB
/
vector.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package stats
import (
"fmt"
"sync"
)
type Int64Value struct {
Tags map[string]string
Value int64
}
type Int64VectorGetter interface {
Labels() []string
Get() []*Int64Value
}
type atomicInt64Vector struct {
entityVector
}
func newAtomicInt64Vector(ls []string, lm labelMarshaler) *atomicInt64Vector {
return &atomicInt64Vector{
entityVector: entityVector{
labels: ls,
marshaler: lm,
newFunc: func(map[string]string) interface{} { return &atomicInt64{} },
},
}
}
func (v *atomicInt64Vector) Labels() []string { return v.labels }
func (v *atomicInt64Vector) buildTags(key uint64) map[string]string {
var tags = make(map[string]string, len(v.labels))
for i, val := range v.marshaler.unmarshal(key, len(v.labels)) {
tags[v.labels[i]] = val
}
return tags
}
func (v *atomicInt64Vector) Get() []*Int64Value {
var res []*Int64Value
v.entities.Range(func(k, vv interface{}) bool {
res = append(
res,
&Int64Value{
Tags: v.buildTags(k.(uint64)),
Value: vv.(*atomicInt64).Get(),
},
)
return true
})
return res
}
func (v *atomicInt64Vector) fetchValue(ls []string) *atomicInt64 {
return v.entity(ls).(*atomicInt64)
}
type entityVector struct {
newFunc func(map[string]string) interface{}
labels []string
entities sync.Map
marshaler labelMarshaler
}
func (ev *entityVector) entity(ls []string) interface{} {
if len(ls) != len(ev.labels) {
panic(
fmt.Sprintf(
"Not the correct number of labels: labels: %v, values: %v",
ev.labels,
ls,
),
)
}
k := ev.marshaler.marshal(ls)
v, ok := ev.entities.Load(k)
if ok {
return v
}
vs := make(map[string]string, len(ev.labels))
for i, k := range ev.labels {
vs[k] = ls[i]
}
v, _ = ev.entities.LoadOrStore(k, ev.newFunc(vs))
return v
}