-
Notifications
You must be signed in to change notification settings - Fork 816
/
Copy pathsignature.go
50 lines (42 loc) · 1.12 KB
/
signature.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
package cortexpb
import (
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
)
// Inline and byte-free variant of hash/fnv's fnv64a.
// Ref: https://github.com/prometheus/common/blob/main/model/fnv.go
func LabelsToFingerprint(lset labels.Labels) model.Fingerprint {
if len(lset) == 0 {
return model.Fingerprint(hashNew())
}
sum := hashNew()
for _, l := range lset {
sum = hashAdd(sum, string(l.Name))
sum = hashAddByte(sum, model.SeparatorByte)
sum = hashAdd(sum, string(l.Value))
sum = hashAddByte(sum, model.SeparatorByte)
}
return model.Fingerprint(sum)
}
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
// hashNew initializes a new fnv64a hash value.
func hashNew() uint64 {
return offset64
}
// hashAdd adds a string to a fnv64a hash value, returning the updated hash.
func hashAdd(h uint64, s string) uint64 {
for i := 0; i < len(s); i++ {
h ^= uint64(s[i])
h *= prime64
}
return h
}
// hashAddByte adds a byte to a fnv64a hash value, returning the updated hash.
func hashAddByte(h uint64, b byte) uint64 {
h ^= uint64(b)
h *= prime64
return h
}