forked from maxmind/mmdbwriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
186 lines (164 loc) · 4.47 KB
/
node.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package mmdbwriter
import (
"net"
"github.com/maxmind/mmdbwriter/mmdbtype"
"github.com/pkg/errors"
)
type recordType byte
const (
recordTypeEmpty recordType = iota
recordTypeData
recordTypeNode
recordTypeAlias
recordTypeFixedNode
recordTypeReserved
)
type record struct {
node *node
valueKey dataMapKey
recordType recordType
}
// each node contains two records.
type node struct {
children [2]record
nodeNum int
}
type insertRecord struct {
ip net.IP
prefixLen int
recordType recordType
inserter func(value mmdbtype.DataType) (mmdbtype.DataType, error)
insertedNode *node
dataMap *dataMap
}
func (n *node) insert(iRec insertRecord, currentDepth int) error {
newDepth := currentDepth + 1
// Check if we are inside the network already
if newDepth > iRec.prefixLen {
// Data already exists for the network so insert into all the children.
// We will prune duplicate nodes when we finalize.
err := n.children[0].insert(iRec, newDepth)
if err != nil {
return err
}
return n.children[1].insert(iRec, newDepth)
}
// We haven't reached the network yet.
pos := bitAt(iRec.ip, currentDepth)
r := &n.children[pos]
return r.insert(iRec, newDepth)
}
func (r *record) insert(
iRec insertRecord,
newDepth int,
) error {
switch r.recordType {
case recordTypeNode, recordTypeFixedNode:
case recordTypeEmpty, recordTypeData:
// When we add record merging support, it should go here.
if newDepth >= iRec.prefixLen {
r.node = iRec.insertedNode
r.recordType = iRec.recordType
if iRec.recordType == recordTypeData {
existingValue := iRec.dataMap.get(r.valueKey)
value, err := iRec.inserter(existingValue)
if err != nil {
return err
}
if value == nil {
r.recordType = recordTypeEmpty
} else {
key, err := iRec.dataMap.store(value)
if err != nil {
return err
}
r.valueKey = key
}
} else {
r.valueKey = noDataMapKey
}
return nil
}
// We are splitting this record so we create two duplicate child
// records.
r.node = &node{children: [2]record{*r, *r}}
r.valueKey = noDataMapKey
r.recordType = recordTypeNode
case recordTypeReserved:
if iRec.prefixLen >= newDepth {
return errors.Errorf(
"attempt to insert %s/%d, which is in a reserved network",
iRec.ip,
iRec.prefixLen,
)
}
// If we are inserting a network that contains a reserved network,
// we silently remove the reserved network.
return nil
case recordTypeAlias:
if iRec.prefixLen < newDepth {
// Do nothing. We are inserting a network that contains an aliased
// network. We silently ignore.
return nil
}
// attempting to insert _into_ an aliased network
return errors.Errorf(
"attempt to insert %s/%d, which is in an aliased network",
iRec.ip,
iRec.prefixLen,
)
default:
return errors.Errorf("inserting into record type %d not implemented!", r.recordType)
}
return r.node.insert(iRec, newDepth)
}
func (n *node) get(
ip net.IP,
depth int,
) (int, record) {
r := n.children[bitAt(ip, depth)]
depth++
switch r.recordType {
case recordTypeNode, recordTypeAlias, recordTypeFixedNode:
return r.node.get(ip, depth)
default:
return depth, r
}
}
// finalize prunes unnecessary nodes (e.g., where the two records are the same) and
// sets the node number for the node. It returns a record pointer that is nil if
// the node is not mergeable or the value of the merged record if it can be merged.
// The second return value is the current node count, including the subtree.
func (n *node) finalize(currentNum int) (*record, int) {
n.nodeNum = currentNum
currentNum++
for i := 0; i < 2; i++ {
switch n.children[i].recordType {
case recordTypeFixedNode:
// We don't consider merging for fixed nodes
_, currentNum = n.children[i].node.finalize(currentNum)
case recordTypeNode:
record, newCurrentNum := n.children[i].node.finalize(currentNum)
if record == nil {
// nothing to merge. Use current number from child.
currentNum = newCurrentNum
} else {
n.children[i] = *record
}
default:
}
}
if n.children[0].recordType == n.children[1].recordType &&
(n.children[0].recordType == recordTypeEmpty ||
(n.children[0].recordType == recordTypeData &&
n.children[0].valueKey == n.children[1].valueKey)) {
return &record{
recordType: n.children[0].recordType,
valueKey: n.children[0].valueKey,
}, currentNum
}
return nil, currentNum
}
func bitAt(ip net.IP, depth int) byte {
return (ip[depth/8] >> uint(7 - (depth % 8))) & 1
}