-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
75 lines (61 loc) · 1.4 KB
/
hash.go
File metadata and controls
75 lines (61 loc) · 1.4 KB
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
//
// Copyright (C) 2023 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/kshard/symbol
//
package atom
import (
"errors"
"fmt"
"hash"
"hash/fnv"
"unsafe"
)
const maxLoop = 10
// The hash function is collision resistent double hashing
// https://en.wikipedia.org/wiki/Double_hashing
type Hash struct {
hash hash.Hash64
getter Getter
}
// Creates new instance of hash function
func NewHash(getter Getter) Hash {
return Hash{
getter: getter,
hash: fnv.New64a(),
}
}
// Hashes string returning either value or error
func (h Hash) String(s string) (uint32, error) {
// This is copied from runtime. It relies on the string
// header being a prefix of the slice header!
b := *(*[]byte)(unsafe.Pointer(&s))
h.hash.Reset()
h.hash.Write(b)
hash := h.hash.Sum64()
lo := uint32(hash)
hi := uint32(hash >> 32)
for attempt := 0; attempt < maxLoop; attempt++ {
val, err := h.getter.Get(lo)
if err != nil && isNotFound(err) {
return lo, nil
}
if err != nil {
return 0, err
}
if val == "" || val == s {
return lo, nil
}
lo = ((lo << 16) | (lo >> 16)) ^ hi
}
return 0, fmt.Errorf("hash collision of value: %s", s)
}
func isNotFound(err error) bool {
var e interface{ NotFound() string }
if ok := errors.As(err, &e); !ok {
return false
}
return e.NotFound() != ""
}