This repository has been archived by the owner on Dec 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bson_hash.go
68 lines (60 loc) · 1.56 KB
/
bson_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
59
60
61
62
63
64
65
66
67
68
package ftdc
import (
"fmt"
"hash"
"hash/fnv"
"github.com/deciduosity/birch"
"github.com/deciduosity/birch/bsontype"
)
func metricKeyHash(doc *birch.Document) (string, int) {
checksum := fnv.New64()
seen := metricKeyHashDocument(checksum, "", doc)
return fmt.Sprintf("%x", checksum.Sum(nil)), seen
}
func metricKeyHashDocument(checksum hash.Hash, key string, doc *birch.Document) int {
iter := doc.Iterator()
seen := 0
for iter.Next() {
elem := iter.Element()
seen += metricKeyHashValue(checksum, fmt.Sprintf("%s.%s", key, elem.Key()), elem.Value())
}
return seen
}
func metricKeyHashArray(checksum hash.Hash, key string, array *birch.Array) int {
seen := 0
iter := array.Iterator()
idx := 0
for iter.Next() {
seen += metricKeyHashValue(checksum, fmt.Sprintf("%s.%d", key, idx), iter.Value())
idx++
}
return seen
}
func metricKeyHashValue(checksum hash.Hash, key string, value *birch.Value) int {
switch value.Type() {
case bsontype.Array:
return metricKeyHashArray(checksum, key, value.MutableArray())
case bsontype.EmbeddedDocument:
return metricKeyHashDocument(checksum, key, value.MutableDocument())
case bsontype.Boolean:
_, _ = checksum.Write([]byte(key))
return 1
case bsontype.Double:
_, _ = checksum.Write([]byte(key))
return 1
case bsontype.Int32:
_, _ = checksum.Write([]byte(key))
return 1
case bsontype.Int64:
_, _ = checksum.Write([]byte(key))
return 1
case bsontype.DateTime:
_, _ = checksum.Write([]byte(key))
return 1
case bsontype.Timestamp:
_, _ = checksum.Write([]byte(key))
return 2
default:
return 0
}
}