fingerprinting.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  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.
  13. package model
  14. import (
  15. "fmt"
  16. "strconv"
  17. )
  18. // Fingerprint provides a hash-capable representation of a Metric.
  19. // For our purposes, FNV-1A 64-bit is used.
  20. type Fingerprint uint64
  21. // FingerprintFromString transforms a string representation into a Fingerprint.
  22. func FingerprintFromString(s string) (Fingerprint, error) {
  23. num, err := strconv.ParseUint(s, 16, 64)
  24. return Fingerprint(num), err
  25. }
  26. // ParseFingerprint parses the input string into a fingerprint.
  27. func ParseFingerprint(s string) (Fingerprint, error) {
  28. num, err := strconv.ParseUint(s, 16, 64)
  29. if err != nil {
  30. return 0, err
  31. }
  32. return Fingerprint(num), nil
  33. }
  34. func (f Fingerprint) String() string {
  35. return fmt.Sprintf("%016x", uint64(f))
  36. }
  37. // Fingerprints represents a collection of Fingerprint subject to a given
  38. // natural sorting scheme. It implements sort.Interface.
  39. type Fingerprints []Fingerprint
  40. // Len implements sort.Interface.
  41. func (f Fingerprints) Len() int {
  42. return len(f)
  43. }
  44. // Less implements sort.Interface.
  45. func (f Fingerprints) Less(i, j int) bool {
  46. return f[i] < f[j]
  47. }
  48. // Swap implements sort.Interface.
  49. func (f Fingerprints) Swap(i, j int) {
  50. f[i], f[j] = f[j], f[i]
  51. }
  52. // FingerprintSet is a set of Fingerprints.
  53. type FingerprintSet map[Fingerprint]struct{}
  54. // Equal returns true if both sets contain the same elements (and not more).
  55. func (s FingerprintSet) Equal(o FingerprintSet) bool {
  56. if len(s) != len(o) {
  57. return false
  58. }
  59. for k := range s {
  60. if _, ok := o[k]; !ok {
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. // Intersection returns the elements contained in both sets.
  67. func (s FingerprintSet) Intersection(o FingerprintSet) FingerprintSet {
  68. myLength, otherLength := len(s), len(o)
  69. if myLength == 0 || otherLength == 0 {
  70. return FingerprintSet{}
  71. }
  72. subSet := s
  73. superSet := o
  74. if otherLength < myLength {
  75. subSet = o
  76. superSet = s
  77. }
  78. out := FingerprintSet{}
  79. for k := range subSet {
  80. if _, ok := superSet[k]; ok {
  81. out[k] = struct{}{}
  82. }
  83. }
  84. return out
  85. }