-
Notifications
You must be signed in to change notification settings - Fork 28
/
data_map.go
70 lines (58 loc) · 1.68 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package mmdbwriter
import "github.com/maxmind/mmdbwriter/mmdbtype"
type dataMapKey string
// Please note, if you change the order of these fields, please check
// alignment as we end up storing quite a few in memory.
type dataMapValue struct {
data mmdbtype.DataType
key dataMapKey
// Alternatively, we could use a weak map for the data map, but I
// don't see any very good options at the moment. We should revist
// if something happens with https://github.com/golang/go/issues/43615
refCount uint32
}
// 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 KeyGenerator
}
func newDataMap(keyWriter KeyGenerator) *dataMap {
return &dataMap{
data: map[dataMapKey]*dataMapValue{},
keyWriter: keyWriter,
}
}
// store stores the value in the dataMap and returns the dataMapValue for it.
// If the value is already in the dataMap, the reference count for it is
// incremented.
func (dm *dataMap) store(v mmdbtype.DataType) (*dataMapValue, error) {
key, err := dm.keyWriter.Key(v)
if err != nil {
return nil, err
}
dmv, ok := dm.data[dataMapKey(key)]
if !ok {
dmKey := dataMapKey(key)
dmv = &dataMapValue{
key: dmKey,
data: v,
}
dm.data[dmKey] = dmv
}
dmv.refCount++
return dmv, nil
}
// remove removes a reference to the value. If the reference count
// drops to zero, the value is removed from the dataMap.
func (dm *dataMap) remove(v *dataMapValue) {
// This is here mostly so that we don't have to guard against it
// elsewhere.
if v == nil {
return
}
v.refCount--
if v.refCount == 0 {
delete(dm.data, v.key)
}
}