forked from chenjiandongx/mandodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
319 lines (262 loc) · 6.61 KB
/
label.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package mandodb
import (
"bytes"
"regexp"
"regexp/syntax"
"sort"
"strconv"
"strings"
"sync"
"github.com/cespare/xxhash"
)
var labelBufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 0, 1024)
},
}
func unmarshalLabelName(s string) (string, string) {
pair := strings.SplitN(s, separator, 2)
if len(pair) != 2 {
return "", ""
}
return pair[0], pair[1]
}
// Label 代表一个标签组合
type Label struct {
Name string
Value string
}
func (l Label) MarshalName() string {
return joinSeparator(l.Name, l.Value)
}
type labelValueSet struct {
mut sync.Mutex
values map[string]map[string]struct{}
}
func newLabelValueSet() *labelValueSet {
return &labelValueSet{
values: map[string]map[string]struct{}{},
}
}
func (lvs *labelValueSet) Set(label, value string) {
lvs.mut.Lock()
defer lvs.mut.Unlock()
if _, ok := lvs.values[label]; !ok {
lvs.values[label] = make(map[string]struct{})
}
lvs.values[label][value] = struct{}{}
}
func (lvs *labelValueSet) Get(label string) []string {
lvs.mut.Lock()
defer lvs.mut.Unlock()
ret := make([]string, 0)
vs, ok := lvs.values[label]
if !ok {
return ret
}
for k := range vs {
ret = append(ret, k)
}
return ret
}
// fastRegexMatcher 是一种优化的正则匹配器 算法来自 Prometheus
type fastRegexMatcher struct {
re *regexp.Regexp
prefix string
suffix string
contains string
}
func newFastRegexMatcher(v string) (*fastRegexMatcher, error) {
re, err := regexp.Compile("^(?:" + v + ")$")
if err != nil {
return nil, err
}
parsed, err := syntax.Parse(v, syntax.Perl)
if err != nil {
return nil, err
}
m := &fastRegexMatcher{
re: re,
}
if parsed.Op == syntax.OpConcat {
m.prefix, m.suffix, m.contains = optimizeConcatRegex(parsed)
}
return m, nil
}
// optimizeConcatRegex returns literal prefix/suffix text that can be safely
// checked against the label value before running the regexp matcher.
func optimizeConcatRegex(r *syntax.Regexp) (prefix, suffix, contains string) {
sub := r.Sub
// We can safely remove begin and end text matchers respectively
// at the beginning and end of the regexp.
if len(sub) > 0 && sub[0].Op == syntax.OpBeginText {
sub = sub[1:]
}
if len(sub) > 0 && sub[len(sub)-1].Op == syntax.OpEndText {
sub = sub[:len(sub)-1]
}
if len(sub) == 0 {
return
}
// Given Prometheus regex matchers are always anchored to the begin/end
// of the text, if the first/last operations are literals, we can safely
// treat them as prefix/suffix.
if sub[0].Op == syntax.OpLiteral && (sub[0].Flags&syntax.FoldCase) == 0 {
prefix = string(sub[0].Rune)
}
if last := len(sub) - 1; sub[last].Op == syntax.OpLiteral && (sub[last].Flags&syntax.FoldCase) == 0 {
suffix = string(sub[last].Rune)
}
// If contains any literal which is not a prefix/suffix, we keep the
// 1st one. We do not keep the whole list of literals to simplify the
// fast path.
for i := 1; i < len(sub)-1; i++ {
if sub[i].Op == syntax.OpLiteral && (sub[i].Flags&syntax.FoldCase) == 0 {
contains = string(sub[i].Rune)
break
}
}
return
}
func (m *fastRegexMatcher) MatchString(s string) bool {
if m.prefix != "" && !strings.HasPrefix(s, m.prefix) {
return false
}
if m.suffix != "" && !strings.HasSuffix(s, m.suffix) {
return false
}
if m.contains != "" && !strings.Contains(s, m.contains) {
return false
}
return m.re.MatchString(s)
}
// Match 主要用于匹配 Labels 组合 支持正则匹配
func (lvs *labelValueSet) Match(matcher LabelMatcher) []string {
ret := make([]string, 0)
if matcher.IsRegx {
pattern, err := newFastRegexMatcher(matcher.Value)
if err != nil {
return []string{matcher.Value}
}
for _, v := range lvs.Get(matcher.Name) {
if pattern.MatchString(v) {
ret = append(ret, v)
}
}
return ret
}
return []string{matcher.Value}
}
// LabelSet 表示 Label 组合
type LabelSet []Label
// filter 过滤空 kv 和重复数据
func (ls LabelSet) filter() LabelSet {
mark := make(map[string]struct{})
var size int
for _, v := range ls {
_, ok := mark[v.Name]
if v.Name != "" && v.Value != "" && !ok {
ls[size] = v // 复用原来的 slice
size++
}
mark[v.Name] = struct{}{}
}
return ls[:size]
}
// Map 将 Label 列表转换成 map
func (ls LabelSet) Map() map[string]string {
m := make(map[string]string)
for _, label := range ls {
m[label.Name] = label.Value
}
return m
}
func (ls LabelSet) Len() int { return len(ls) }
func (ls LabelSet) Less(i, j int) bool { return ls[i].Name < ls[j].Name }
func (ls LabelSet) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] }
// AddMetricName 将指标名称也当成一个 label 处理 在存储的时候并不做特性的区分
// 每个指标的最后一个 label 就是 metricName
func (ls LabelSet) AddMetricName(metric string) LabelSet {
labels := ls.filter()
labels = append(labels, Label{
Name: metricName,
Value: metric,
})
return labels
}
func (ls LabelSet) Sorted() {
sort.Sort(ls)
}
// Hash 哈希计算 LabelSet 唯一标识符
func (ls LabelSet) Hash() uint64 {
b := labelBufPool.Get().([]byte)
const sep = '\xff'
for _, v := range ls {
b = append(b, v.Name...)
b = append(b, sep)
b = append(b, v.Value...)
b = append(b, sep)
}
h := xxhash.Sum64(b)
b = b[:0]
labelBufPool.Put(b) // 复用 buffer
return h
}
// Has 判断 label 是否存在
func (ls LabelSet) Has(name string) bool {
for _, label := range ls {
if label.Name == name {
return true
}
}
return false
}
// String 用户格式化输出
func (ls LabelSet) String() string {
var b bytes.Buffer
b.WriteByte('{')
for i, l := range ls {
if i > 0 {
b.WriteByte(',')
b.WriteByte(' ')
}
b.WriteString(l.Name)
b.WriteByte('=')
b.WriteString(strconv.Quote(l.Value))
}
b.WriteByte('}')
return b.String()
}
// LabelMatcher Label 匹配器 支持正则
type LabelMatcher struct {
Name string
Value string
IsRegx bool
}
// LabelMatcherSet 表示 LabelMatcher 组合
type LabelMatcherSet []LabelMatcher
// AddMetricName 将指标名称也当成一个 label 处理 在存储的时候并不做特性的区分
// 每个指标的最后一个 label 就是 metricName
func (lms LabelMatcherSet) AddMetricName(metric string) LabelMatcherSet {
labels := lms.filter()
newl := LabelMatcher{
Name: metricName,
Value: metric,
}
labels = append(labels, newl)
return labels
}
// filter 过滤空 kv 和重复数据
func (lms LabelMatcherSet) filter() LabelMatcherSet {
mark := make(map[string]struct{})
var size int
for _, v := range lms {
_, ok := mark[v.Name]
if v.Name != "" && v.Value != "" && !ok {
lms[size] = v // 复用原来的 slice
size++
}
mark[v.Name] = struct{}{}
}
return lms[:size]
}