github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/ingester/client/fnv.go (about) 1 // Modified from github.com/prometheus/common/model/fnv.go 2 // Copyright 2015 The Prometheus Authors 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package client 16 17 // Inline and byte-free variant of hash/fnv's fnv64a. 18 19 const ( 20 offset64 = 14695981039346656037 21 prime64 = 1099511628211 22 offset32 = 2166136261 23 prime32 = 16777619 24 ) 25 26 // hashNew initializes a new fnv64a hash value. 27 func hashNew() uint64 { 28 return offset64 29 } 30 31 // hashAdd adds a string to a fnv64a hash value, returning the updated hash. 32 // Note this is the same algorithm as Go stdlib `sum64a.Write()` 33 func hashAdd(h uint64, s string) uint64 { 34 for i := 0; i < len(s); i++ { 35 h ^= uint64(s[i]) 36 h *= prime64 37 } 38 return h 39 } 40 41 // hashAdd adds a string to a fnv64a hash value, returning the updated hash. 42 func hashAddString(h uint64, s string) uint64 { 43 for i := 0; i < len(s); i++ { 44 h ^= uint64(s[i]) 45 h *= prime64 46 } 47 return h 48 } 49 50 // hashAddByte adds a byte to a fnv64a hash value, returning the updated hash. 51 func hashAddByte(h uint64, b byte) uint64 { 52 h ^= uint64(b) 53 h *= prime64 54 return h 55 } 56 57 // HashNew32 initializies a new fnv32 hash value. 58 func HashNew32() uint32 { 59 return offset32 60 } 61 62 // HashAdd32 adds a string to a fnv32 hash value, returning the updated hash. 63 // Note this is the same algorithm as Go stdlib `sum32.Write()` 64 func HashAdd32(h uint32, s string) uint32 { 65 for i := 0; i < len(s); i++ { 66 h *= prime32 67 h ^= uint32(s[i]) 68 } 69 return h 70 } 71 72 // HashAddByte32 adds a byte to a fnv32 hash value, returning the updated hash. 73 func HashAddByte32(h uint32, b byte) uint32 { 74 h *= prime32 75 h ^= uint32(b) 76 return h 77 } 78 79 // HashNew32a initializies a new fnv32a hash value. 80 func HashNew32a() uint32 { 81 return offset32 82 } 83 84 // HashAdd32a adds a string to a fnv32a hash value, returning the updated hash. 85 // Note this is the same algorithm as Go stdlib `sum32.Write()` 86 func HashAdd32a(h uint32, s string) uint32 { 87 for i := 0; i < len(s); i++ { 88 h ^= uint32(s[i]) 89 h *= prime32 90 } 91 return h 92 } 93 94 // HashAddByte32a adds a byte to a fnv32a hash value, returning the updated hash. 95 func HashAddByte32a(h uint32, b byte) uint32 { 96 h ^= uint32(b) 97 h *= prime32 98 return h 99 }