forked from gaissmai/bart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliteDumper.go
254 lines (203 loc) · 5.69 KB
/
liteDumper.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) 2025 Karl Gaissmaier
// SPDX-License-Identifier: MIT
package bart
import (
"fmt"
"io"
"strings"
"github.com/metacubex/bart/internal/art"
)
// ##################################################
// useful during development, debugging and testing
// ##################################################
// dumpString is just a wrapper for dump.
func (l *Lite) dumpString() string {
w := new(strings.Builder)
l.dump(w)
return w.String()
}
// dump the table structure and all the nodes to w.
func (l *Lite) dump(w io.Writer) {
if l == nil {
return
}
if !l.root4.isEmpty() {
stats := l.root4.nodeStatsRec()
fmt.Fprintln(w)
fmt.Fprintf(w, "### IPv4: nodes(%d), pfxs(%d), leaves(%d), fringes(%d),",
stats.nodes, stats.pfxs, stats.leaves, stats.fringes)
l.root4.dumpRec(w, stridePath{}, 0, true)
}
if !l.root6.isEmpty() {
stats := l.root6.nodeStatsRec()
fmt.Fprintln(w)
fmt.Fprintf(w, "### IPv6: nodes(%d), pfxs(%d), leaves(%d), fringes(%d),",
stats.nodes, stats.pfxs, stats.leaves, stats.fringes)
l.root6.dumpRec(w, stridePath{}, 0, false)
}
}
// dumpRec, rec-descent the trie.
func (n *liteNode) dumpRec(w io.Writer, path stridePath, depth int, is4 bool) {
// dump this node
n.dump(w, path, depth, is4)
// the node may have childs, rec-descent down
for i, addr := range n.children.All() {
octet := byte(addr)
path[depth&15] = octet
if child, ok := n.children.Items[i].(*liteNode); ok {
child.dumpRec(w, path, depth+1, is4)
}
}
}
// dump the node to w.
func (n *liteNode) dump(w io.Writer, path stridePath, depth int, is4 bool) {
bits := depth * strideLen
indent := strings.Repeat(".", depth)
// node type with depth and octet path and bits.
fmt.Fprintf(w, "\n%s[%s] depth: %d path: [%s] / %d\n",
indent, n.hasType(), depth, ipStridePath(path, depth, is4), bits)
if nPfxCount := n.prefixes.Size(); nPfxCount != 0 {
// no heap allocs
allIndices := n.prefixes.All()
// print the baseIndices for this node.
fmt.Fprintf(w, "%sindexs(#%d): %v\n", indent, nPfxCount, allIndices)
// print the prefixes for this node
fmt.Fprintf(w, "%sprefxs(#%d):", indent, nPfxCount)
for _, idx := range allIndices {
octet, pfxLen := art.IdxToPfx(idx)
fmt.Fprintf(w, " %s/%d", octetFmt(octet, is4), pfxLen)
}
fmt.Fprintln(w)
/* Lite has no values
// print the values for this node
fmt.Fprintf(w, "%svalues(#%d):", indent, nPfxCount)
for _, val := range n.prefixes.Items {
fmt.Fprintf(w, " %v", val)
}
fmt.Fprintln(w)
*/
}
if n.children.Len() != 0 {
nodeAddrs := make([]uint, 0, maxItems)
leafAddrs := make([]uint, 0, maxItems)
fringeAddrs := make([]uint, 0, maxItems)
// the node has recursive child nodes or path-compressed leaves
for i, addr := range n.children.All() {
switch kid := n.children.Items[i].(type) {
case *liteNode:
nodeAddrs = append(nodeAddrs, addr)
continue
case *liteLeaf:
if kid.fringe {
fringeAddrs = append(fringeAddrs, addr)
} else {
leafAddrs = append(leafAddrs, addr)
}
default:
panic("logic error, wrong node type")
}
}
if nodeCount := len(nodeAddrs); nodeCount > 0 {
// print the childs for this node
fmt.Fprintf(w, "%schilds(#%d):", indent, nodeCount)
for _, addr := range nodeAddrs {
octet := byte(addr)
fmt.Fprintf(w, " %s", octetFmt(octet, is4))
}
fmt.Fprintln(w)
}
if leafCount := len(leafAddrs); leafCount > 0 {
// print the pathcomp prefixes for this node
fmt.Fprintf(w, "%sleaves(#%d):", indent, leafCount)
for _, addr := range leafAddrs {
octet := byte(addr)
k := n.children.MustGet(addr)
pc := k.(*liteLeaf)
fmt.Fprintf(w, " %s:{%s}", octetFmt(octet, is4), pc.prefix)
}
fmt.Fprintln(w)
}
if fringeCount := len(fringeAddrs); fringeCount > 0 {
// print the pathcomp prefixes for this node
fmt.Fprintf(w, "%sfringe(#%d):", indent, fringeCount)
for _, addr := range fringeAddrs {
octet := byte(addr)
k := n.children.MustGet(addr)
pc := k.(*liteLeaf)
fmt.Fprintf(w, " %s:{%s}", octetFmt(octet, is4), pc.prefix)
}
fmt.Fprintln(w)
}
}
}
// hasType returns the nodeType.
func (n *liteNode) hasType() nodeType {
s := n.nodeStats()
switch {
case s.pfxs == 0 && s.childs == 0:
return nullNode
case s.nodes == 0:
return leafNode
case (s.pfxs > 0 || s.leaves > 0 || s.fringes > 0) && s.nodes > 0:
return fullNode
case (s.pfxs == 0 && s.leaves == 0 && s.fringes == 0) && s.nodes > 0:
return intermediateNode
default:
panic(fmt.Sprintf("UNREACHABLE: pfx: %d, chld: %d, node: %d, leaf: %d, fringe: %d",
s.pfxs, s.childs, s.nodes, s.leaves, s.fringes))
}
}
// node statistics for this single node
func (n *liteNode) nodeStats() stats {
var s stats
s.pfxs = n.prefixes.Size()
s.childs = n.children.Len()
for i := range n.children.All() {
switch kid := n.children.Items[i].(type) {
case *liteNode:
s.nodes++
case *liteLeaf:
if kid.fringe {
s.fringes++
} else {
s.leaves++
}
default:
panic("logic error, wrong node type")
}
}
return s
}
// nodeStatsRec, calculate the number of pfxs, nodes and leaves under n, rec-descent.
func (n *liteNode) nodeStatsRec() stats {
var s stats
if n == nil || n.isEmpty() {
return s
}
s.pfxs = n.prefixes.Size()
s.childs = n.children.Len()
s.nodes = 1 // this node
s.leaves = 0
s.fringes = 0
for _, kidAny := range n.children.Items {
switch kid := kidAny.(type) {
case *liteNode:
// rec-descent
rs := kid.nodeStatsRec()
s.pfxs += rs.pfxs
s.childs += rs.childs
s.nodes += rs.nodes
s.leaves += rs.leaves
s.fringes += rs.fringes
case *liteLeaf:
if kid.fringe {
s.fringes++
} else {
s.leaves++
}
default:
panic("logic error, wrong node type")
}
}
return s
}