forked from go-chi/chi
/
tree.go
420 lines (356 loc) · 8.04 KB
/
tree.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package chi
// Radix tree implementation below is a based on the original work by
// Armon Dadgar in https://github.com/armon/go-radix/blob/master/radix.go
// (MIT licensed)
import (
"sort"
"strings"
)
type nodeTyp uint8
const (
ntStatic nodeTyp = iota // /home
ntRegexp // /:id([0-9]+) or #id^[0-9]+$
ntParam // /:user
ntCatchAll // /api/v1/*
)
// WalkFn is used when walking the tree. Takes a
// key and value, returning if iteration should
// be terminated.
type WalkFn func(path string, handler Handler) bool
// edge is used to represent an edge node
type edge struct {
label byte
node *node
}
type node struct {
typ nodeTyp
// prefix is the common prefix we ignore
prefix string
// HTTP handler on the leaf node
handler Handler
// Edges should be stored in-order for iteration,
// in groups of the node type.
edges [ntCatchAll + 1]edges
}
func (n *node) isLeaf() bool {
return n.handler != nil
}
func (n *node) addEdge(e edge) {
search := e.node.prefix
// Find any wildcard segments
p := strings.IndexAny(search, ":*")
// Determine new node type
ntyp := ntStatic
if p >= 0 {
switch search[p] {
case ':':
ntyp = ntParam
case '*':
ntyp = ntCatchAll
}
}
if p == 0 {
// Path starts with a wildcard
handler := e.node.handler
e.node.typ = ntyp
if ntyp == ntCatchAll {
p = -1
} else {
p = strings.IndexByte(search, '/')
}
if p < 0 {
p = len(search)
}
e.node.prefix = search[:p]
if p != len(search) {
// add edge for the remaining part, split the end.
e.node.handler = nil
search = search[p:]
e2 := edge{
label: search[0], // this will always start with /
node: &node{
typ: ntStatic,
prefix: search,
handler: handler,
},
}
e.node.addEdge(e2)
}
} else if p > 0 {
// Path has some wildcard
// starts with a static segment
handler := e.node.handler
e.node.typ = ntStatic
e.node.prefix = search[:p]
e.node.handler = nil
// add the wild edge node
search = search[p:]
e2 := edge{
label: search[0],
node: &node{
typ: ntyp,
prefix: search,
handler: handler,
},
}
e.node.addEdge(e2)
} else {
// Path is all static
e.node.typ = ntyp
}
n.edges[e.node.typ] = append(n.edges[e.node.typ], e)
n.edges[e.node.typ].Sort()
}
func (n *node) replaceEdge(e edge) {
num := len(n.edges[e.node.typ])
for i := 0; i < num; i++ {
if n.edges[e.node.typ][i].label == e.label {
n.edges[e.node.typ][i].node = e.node
return
}
}
panic("chi: replacing missing edge")
}
func (n *node) getEdge(label byte) *node {
for _, edges := range n.edges {
num := len(edges)
for i := 0; i < num; i++ {
if edges[i].label == label {
return edges[i].node
}
}
}
return nil
}
func (n *node) findEdge(ntyp nodeTyp, label byte) *node {
subedges := n.edges[ntyp]
num := len(subedges)
idx := sort.Search(num, func(i int) bool {
switch ntyp {
case ntStatic:
return subedges[i].label >= label
default: // wild nodes
// TODO: right now we match them all.. but regexp should
// run through regexp matcher
return true
}
})
if idx >= num {
return nil
}
if subedges[idx].node.typ == ntStatic && subedges[idx].label == label {
return subedges[idx].node
} else if subedges[idx].node.typ > ntStatic {
return subedges[idx].node
}
return nil
}
// Recursive edge traversal by checking all nodeTyp groups along the way.
// It's like searching through a three-dimensional radix trie.
func (n *node) findNode(path string, params map[string]string) *node {
nn := n
search := path
for t, edges := range nn.edges {
ntyp := nodeTyp(t)
if len(edges) == 0 {
continue
}
// search subset of edges of the index for a matching node
var label byte
if search != "" {
label = search[0]
}
xn := nn.findEdge(ntyp, label) // next node
if xn == nil {
continue
}
// Prepare next search path by trimming prefix from requested path
xsearch := search
if xn.typ > ntStatic {
p := -1
if xn.typ < ntCatchAll {
p = strings.IndexByte(xsearch, '/')
}
if p < 0 {
p = len(xsearch)
}
if xn.typ == ntCatchAll {
params["*"] = xsearch
} else {
params[xn.prefix[1:]] = xsearch[:p]
}
xsearch = xsearch[p:]
} else if strings.HasPrefix(xsearch, xn.prefix) {
xsearch = xsearch[len(xn.prefix):]
} else {
continue // no match
}
// did we find it yet?
if len(xsearch) == 0 {
if xn.isLeaf() {
return xn
}
}
// recursively find the next node..
fin := xn.findNode(xsearch, params)
if fin != nil {
// found a node, return it
return fin
} else {
// let's remove the param here if it was set
if xn.typ > ntStatic {
if xn.typ == ntCatchAll {
delete(params, "*")
} else {
delete(params, xn.prefix[1:])
}
}
}
}
return nil
}
type edges []edge
func (e edges) Len() int {
return len(e)
}
// Sort the list of edges by label
func (e edges) Less(i, j int) bool {
return e[i].label < e[j].label
}
func (e edges) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
func (e edges) Sort() {
sort.Sort(e)
}
// Tree implements a radix tree. This can be treated as a
// Dictionary abstract data type. The main advantage over
// a standard hash map is prefix-based lookups and
// ordered iteration.
type tree struct {
root *node
// Optional handler to return in case nothing was found
notFoundHandler Handler
}
func (t *tree) Insert(pattern string, handler Handler) {
var parent *node
n := t.root
search := pattern
for {
// Handle key exhaustion
if len(search) == 0 {
// Insert or update the node's leaf handler
n.handler = handler
return
}
// Look for the edge
parent = n
n = n.getEdge(search[0])
// No edge, create one
if n == nil {
e := edge{
label: search[0],
node: &node{
prefix: search,
handler: handler,
},
}
parent.addEdge(e)
return
}
if n.typ > ntStatic {
// We found a wildcard node, meaning search path starts with
// a wild prefix. Trim off the wildcard search path and continue.
p := strings.Index(search, "/")
if p < 0 {
p = len(search)
}
search = search[p:]
continue
}
// Static node fall below here.
// Determine longest prefix of the search key on match.
commonPrefix := t.longestPrefix(search, n.prefix)
if commonPrefix == len(n.prefix) {
// the common prefix is as long as the current node's prefix we're attempting to insert.
// keep the search going.
search = search[commonPrefix:]
continue
}
// Split the node
child := &node{
typ: ntStatic,
prefix: search[:commonPrefix],
}
parent.replaceEdge(edge{
label: search[0],
node: child,
})
// Restore the existing node
child.addEdge(edge{
label: n.prefix[commonPrefix],
node: n,
})
n.prefix = n.prefix[commonPrefix:]
// If the new key is a subset, add to to this node
search = search[commonPrefix:]
if len(search) == 0 {
child.handler = handler
return
}
// Create a new edge for the node
child.addEdge(edge{
label: search[0],
node: &node{
typ: ntStatic,
prefix: search,
handler: handler,
},
})
return
}
return
}
func (t *tree) Find(path string, params map[string]string) Handler {
node := t.root.findNode(path, params)
if node == nil {
return nil
}
return node.handler
}
// Walk is used to walk the tree
func (t *tree) Walk(fn WalkFn) {
t.recursiveWalk(t.root, fn)
}
// recursiveWalk is used to do a pre-order walk of a node
// recursively. Returns true if the walk should be aborted
func (t *tree) recursiveWalk(n *node, fn WalkFn) bool {
// Visit the leaf values if any
if n.handler != nil && fn(n.prefix, n.handler) {
return true
}
// Recurse on the children
for _, edges := range n.edges {
for _, e := range edges {
if t.recursiveWalk(e.node, fn) {
return true
}
}
}
return false
}
// longestPrefix finds the length of the shared prefix
// of two strings
func (t *tree) longestPrefix(k1, k2 string) int {
max := len(k1)
if l := len(k2); l < max {
max = l
}
var i int
for i = 0; i < max; i++ {
if k1[i] != k2[i] {
break
}
}
return i
}