forked from maxmind/mmdbwriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_map.go
54 lines (44 loc) · 1.08 KB
/
data_map.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
package mmdbwriter
import "github.com/maxmind/mmdbwriter/mmdbtype"
type dataMapKey string
type dataMapValue struct {
data mmdbtype.DataType
// This exists to reduce allocations by interning the key.
key dataMapKey
}
// dataMap is used to deduplicate data inserted into the tree to reduce
// memory usage using keys generated by keyWriter.
type dataMap struct {
data map[dataMapKey]dataMapValue
keyWriter *keyWriter
}
const noDataMapKey dataMapKey = ""
func newDataMap() *dataMap {
return &dataMap{
data: map[dataMapKey]dataMapValue{},
keyWriter: newKeyWriter(),
}
}
func (dm *dataMap) get(k dataMapKey) mmdbtype.DataType {
if k == noDataMapKey {
return nil
}
return dm.data[k].data
}
// store stores the value in the dataMap and returns a key for it.
func (dm *dataMap) store(v mmdbtype.DataType) (dataMapKey, error) {
key, err := dm.keyWriter.key(v)
if err != nil {
return "", err
}
dmv, ok := dm.data[dataMapKey(key)]
if !ok {
dmKey := dataMapKey(key)
dmv = dataMapValue{
key: dmKey,
data: v,
}
dm.data[dmKey] = dmv
}
return dmv.key, nil
}