-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter.go
More file actions
189 lines (164 loc) · 5.44 KB
/
iter.go
File metadata and controls
189 lines (164 loc) · 5.44 KB
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
// Copyright 2022 Sylvain Müller. All rights reserved.
// Mount of this source code is governed by a Apache-2.0 license that can be found
// at https://github.com/fox-toolkit/fox/blob/master/LICENSE.txt.
package fox
import (
"iter"
"strings"
)
const stackSizeThreshold = 25
type stack struct {
edges []*node
}
// Iter provide a set of range iterators for traversing registered methods and routes. Iter capture a point-in-time
// snapshot of the routing tree. Therefore, all iterators returned by Iter will not observe subsequent write on the
// router or on the transaction from which the Iter is created.
type Iter struct {
tree *iTree
patterns *node
names *node
methods map[string]uint
maxDepth int // tree or txn maxDepth
}
// Methods returns a range iterator over all HTTP methods registered in the routing tree. The iterator reflect a snapshot
// of the routing tree at the time [Iter] is created. This function is safe for concurrent use by multiple goroutine and
// while mutation on routes are ongoing.
func (it Iter) Methods() iter.Seq[string] {
return func(yield func(string) bool) {
for k := range it.methods {
if !yield(k) {
return
}
}
}
}
// Routes returns a range iterator over all registered routes in the routing tree that exactly match the provided route
// pattern. The iterator reflect a snapshot of the routing tree at the time [Iter] is created. This function is safe for
// concurrent use by multiple goroutine and while mutation on routes are ongoing.
func (it Iter) Routes(pattern string) iter.Seq[*Route] {
return func(yield func(*Route) bool) {
matched := it.patterns.searchPattern(pattern)
if matched == nil || !matched.isLeaf() {
return
}
for _, route := range matched.routes {
if route.pattern.str == pattern {
if !yield(route) {
return
}
}
}
}
}
// NamePrefix returns a range iterator over all routes in the routing tree that match a given name prefix. The iterator
// reflect a snapshot of the routing tree at the time [Iter] is created. This function is safe for concurrent use by
// multiple goroutine and while mutation on routes are ongoing.
func (it Iter) NamePrefix(prefix string) iter.Seq[*Route] {
return func(yield func(*Route) bool) {
var stacks []stack
if it.maxDepth < stackSizeThreshold {
stacks = make([]stack, 0, stackSizeThreshold) // stack allocation
} else {
stacks = make([]stack, 0, it.maxDepth) // heap allocation
}
matched := it.names.searchName(prefix)
if matched == nil {
return
}
stacks = append(stacks, stack{
edges: []*node{matched},
})
for len(stacks) > 0 {
n := len(stacks)
last := stacks[n-1]
elem := last.edges[0]
if len(last.edges) > 1 {
stacks[n-1].edges = last.edges[1:]
} else {
stacks = stacks[:n-1]
}
if len(elem.statics) > 0 {
stacks = append(stacks, stack{edges: elem.statics})
}
if elem.isLeaf() {
if !yield(elem.routes[0]) {
return
}
}
}
}
}
// PatternPrefix returns a range iterator over all routes in the routing tree that match a given pattern prefix.
// The iterator reflect a snapshot of the routing tree at the time [Iter] is created. This function is safe for
// concurrent use by multiple goroutine and while mutation on routes are ongoing.
// Note: Partial parameter syntax (e.g., /users/{name:) is not supported and will not match any routes.
func (it Iter) PatternPrefix(prefix string) iter.Seq[*Route] {
return func(yield func(*Route) bool) {
var stacks []stack
if it.maxDepth < stackSizeThreshold {
stacks = make([]stack, 0, stackSizeThreshold) // stack allocation
} else {
stacks = make([]stack, 0, it.maxDepth) // heap allocation
}
matched := it.patterns.searchPattern(prefix)
if matched == nil {
return
}
stacks = append(stacks, stack{
edges: []*node{matched},
})
for len(stacks) > 0 {
n := len(stacks)
last := stacks[n-1]
elem := last.edges[0]
if len(last.edges) > 1 {
stacks[n-1].edges = last.edges[1:]
} else {
stacks = stacks[:n-1]
}
if len(elem.statics) > 0 {
stacks = append(stacks, stack{edges: elem.statics})
}
if len(elem.params) > 0 {
stacks = append(stacks, stack{edges: elem.params})
}
if len(elem.wildcards) > 0 {
stacks = append(stacks, stack{edges: elem.wildcards})
}
if elem.isLeaf() {
for _, route := range elem.routes {
if len(route.params) > 0 && !strings.HasPrefix(route.pattern.str, prefix) {
continue
}
if !yield(route) {
return
}
}
}
}
}
}
// All returns a range iterator over all routes registered in the routing tree. The iterator reflect a snapshot
// of the routing tree at the time [Iter] is created. This function is safe for concurrent use by multiple goroutine
// and while mutation on routes are ongoing. See also [Iter.PatternPrefix] as an alternative.
func (it Iter) All() iter.Seq[*Route] {
return func(yield func(*Route) bool) {
for route := range it.PatternPrefix("") {
if !yield(route) {
return
}
}
}
}
// Names returns a range iterator over all routes registered in the routing tree with a name. The iterator reflect a snapshot
// of the routing tree at the time [Iter] is created. This function is safe for concurrent use by multiple goroutine
// and while mutation on routes are ongoing. See also [Iter.NamePrefix] as an alternative.
func (it Iter) Names() iter.Seq[*Route] {
return func(yield func(*Route) bool) {
for route := range it.NamePrefix("") {
if !yield(route) {
return
}
}
}
}