1 // Copyright 2013 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
21 // Fingerprint provides a hash-capable representation of a Metric.
22 // For our purposes, FNV-1A 64-bit is used.
23 type Fingerprint uint64
25 // FingerprintFromString transforms a string representation into a Fingerprint.
26 func FingerprintFromString(s string) (Fingerprint, error) {
27 num, err := strconv.ParseUint(s, 16, 64)
28 return Fingerprint(num), err
31 // ParseFingerprint parses the input string into a fingerprint.
32 func ParseFingerprint(s string) (Fingerprint, error) {
33 num, err := strconv.ParseUint(s, 16, 64)
37 return Fingerprint(num), nil
40 func (f Fingerprint) String() string {
41 return fmt.Sprintf("%016x", uint64(f))
44 // Fingerprints represents a collection of Fingerprint subject to a given
45 // natural sorting scheme. It implements sort.Interface.
46 type Fingerprints []Fingerprint
48 // Len implements sort.Interface.
49 func (f Fingerprints) Len() int {
53 // Less implements sort.Interface.
54 func (f Fingerprints) Less(i, j int) bool {
58 // Swap implements sort.Interface.
59 func (f Fingerprints) Swap(i, j int) {
60 f[i], f[j] = f[j], f[i]
63 // FingerprintSet is a set of Fingerprints.
64 type FingerprintSet map[Fingerprint]struct{}
66 // Equal returns true if both sets contain the same elements (and not more).
67 func (s FingerprintSet) Equal(o FingerprintSet) bool {
73 if _, ok := o[k]; !ok {
81 // Intersection returns the elements contained in both sets.
82 func (s FingerprintSet) Intersection(o FingerprintSet) FingerprintSet {
83 myLength, otherLength := len(s), len(o)
84 if myLength == 0 || otherLength == 0 {
85 return FingerprintSet{}
91 if otherLength < myLength {
96 out := FingerprintSet{}
98 for k := range subSet {
99 if _, ok := superSet[k]; ok {