/
hash.go
58 lines (52 loc) · 1.08 KB
/
hash.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
51
52
53
54
55
56
57
58
package qjson
import (
"bytes"
"hash/fnv"
"sort"
"strconv"
"strings"
)
func (n *Node) Hash() uint64 {
if n.hashId == 0 {
return n.hash()
}
return n.hashId
}
func (n *Node) Rehash() uint64 {
return n.hash()
}
func (n *Node) hash() uint64 {
if n == nil {
return 0
}
switch n.Type {
case Null:
n.hashId = 0
case String, Bool, Integer, Float:
n.hashId = bytesHash(stringToBytes(n.Value))
case Object:
list := getStrSlice()
defer putStrSlice(list)
for _, item := range n.ObjectValues {
list.Str = append(list.Str, strconv.FormatUint(item.Key.hash(), 10)+":"+
strconv.FormatUint(item.Value.hash(), 10))
}
sort.Strings(list.Str)
n.hashId = bytesHash(stringToBytes(strings.Join(list.Str, ",")))
case Array:
buf := bytesPool.Get().(*bytes.Buffer)
buf.Reset()
defer bytesPool.Put(buf)
for _, item := range n.ArrayValues {
buf.Write(stringToBytes(strconv.FormatUint(item.hash(), 10)))
buf.WriteByte(',')
}
n.hashId = bytesHash(buf.Bytes())
}
return n.hashId
}
func bytesHash(bs []byte) uint64 {
h := fnv.New64()
h.Write(bs)
return h.Sum64()
}