-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.go
280 lines (216 loc) · 5.4 KB
/
index.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
// Copyright © 2016-2018 Eduard Sesigin. All rights reserved. Contacts: <[email protected]>
package bxog
// Index
import (
//"fmt"
"net/http"
"strings"
)
// Router using the index selects the route
type index struct {
tree *node
index map[typeHash]route
listShifts [DELIMITER_IN_LIST]int
listRoutes []*route
coreRoute *route
findMethod func(*http.Request) *route
}
func newIndex() *index {
x := &index{
index: make(map[typeHash]route),
listRoutes: []*route{&route{}}, // first route - dummy
}
x.findMethod = x.findTree
return x
}
func (x *index) getNode(arr map[string]*route) *node {
out := newNode()
childs := make(map[typeHash]map[string]*route)
for url, r := range arr {
url = strings.Trim(url, DELIMITER_STRING)
if url == "" {
out.route = r
out.flag = true
return out
}
arrStr := strings.Split(url, DELIMITER_STRING)
if len(arrStr) > 1 {
key := arrStr[0]
salt := x.genSalt(r.method)
hash := x.genUint(key, salt)
if key[:1] == ":" {
hash = DELIMITER_UINT
}
if _, ok := childs[hash]; !ok {
childs[hash] = make(map[string]*route)
}
arrStr = arrStr[1:]
url = strings.Join(arrStr, DELIMITER_STRING)
childs[hash][url] = r
} else if len(arrStr) == 1 {
key := arrStr[0]
salt := x.genSalt(r.method)
hash := x.genUint(key, salt)
if key[:1] == ":" {
hash = DELIMITER_UINT
}
if _, ok := childs[hash]; !ok {
childs[hash] = make(map[string]*route)
}
url = ""
childs[hash][url] = r
}
}
for hash, v := range childs {
n := x.getNode(v)
out.child[hash] = n
}
return out
}
/*
When compiling the required data for two methods of routing search
(the second is left as an alternative)
*/
func (x *index) compile(routes []*route) {
if len(routes) > MAX_ROUTES {
panic("Too many routs, change the constant MAX_ROUTES in the configuration file.")
}
mapRoutes := make(map[string]*route)
var core *route
for _, r := range routes {
x.index[x.genUint(r.id, 0)] = *r
if r.url == "/" {
core = r
} else {
mapRoutes[r.url] = r
}
}
x.tree = x.getNode(mapRoutes)
x.tree.route = core
x.fillCore()
x.fillNode(x.tree, 0)
}
func (x *index) fillCore() {
x.coreRoute, x.tree.route = x.tree.route, x.coreRoute
}
func (x *index) fillNode(n *node, shiftLeft int) int {
if shiftLeft > DELIMITER_IN_LIST-HTTP_PATTERN_COUNT {
panic("Too many routs, change the constant MAX_ROUTES in the configuration file.")
}
shiftRigth := shiftLeft + 1 // zero - delim
if n.route != nil {
x.listShifts[shiftRigth] = -(int(len(x.listRoutes)))
shiftRigth++
x.listRoutes = append(x.listRoutes, n.route)
return shiftRigth
} else if countChild := len(n.child); countChild != 0 {
shiftCur := shiftRigth
shiftRigth += countChild * 2
if _, ok := n.child[DELIMITER_UINT]; ok {
x.listShifts[shiftCur] = DELIMITER_IN_LIST
shiftCur++
x.listShifts[shiftCur] = shiftRigth
shiftRigth = x.fillNode(n.child[DELIMITER_UINT], shiftRigth)
shiftCur++
} else {
for k, n2 := range n.child {
x.listShifts[shiftCur] = int(k)
shiftCur++
x.listShifts[shiftCur] = shiftRigth
shiftRigth = x.fillNode(n2, shiftRigth)
shiftCur++
}
}
return shiftRigth
}
return 0
}
func (x *index) findShift(req *http.Request) *route {
cHashes := [HTTP_SECTION_COUNT]typeHash{}
level := x.genUintSlice(req.URL.Path, x.genSalt(req.Method), &cHashes)
if level == 0 && cHashes[0] == SLASH_HASH { //
return x.coreRoute
} else if level > 1 && cHashes[level-1] == SLASH_HASH { // 140
return nil
}
curShift := 0
for curLevel := 0; curLevel <= level; curLevel++ {
shft := x.find2X(curLevel, curShift+1, cHashes[curLevel])
switch {
case shft < 0:
shft = -shft
return x.listRoutes[shft]
case shft > 0:
curShift = shft
default:
return nil
}
}
return nil
}
func (x *index) find2X(curLevel int, curShift int, curHash typeHash) int {
hash := int(curHash)
for {
csh := x.listShifts[curShift]
if csh == hash {
return x.listShifts[curShift+1]
} else if csh == DELIMITER_IN_LIST {
return x.listShifts[curShift+1]
} else if csh < 0 {
return csh
} else if csh == 0 {
return 0
}
curShift++
}
}
func (x *index) findTree(req *http.Request) *route {
cHashes := [HTTP_SECTION_COUNT]typeHash{}
level := x.genUintSlice(req.URL.Path, x.genSalt(req.Method), &cHashes)
if level == 0 && x.coreRoute != nil {
return x.coreRoute
}
return x.findX(level, 0, x.tree, &cHashes)
}
func (x *index) findX(ln int, level int, tree2 *node, cHashes *[HTTP_SECTION_COUNT]typeHash) *route {
if ln == level {
return tree2.route
} else if z1, ok := tree2.child[cHashes[level]]; ok {
return x.findX(ln, level+1, z1, cHashes)
} else if z2, ok := tree2.child[DELIMITER_UINT]; ok {
return x.findX(ln, level+1, z2, cHashes)
}
return nil
}
func (x *index) genUintSlice(s string, salt typeHash, cHashes *[HTTP_SECTION_COUNT]typeHash) int {
c := DELIMITER_BYTE
na := 0
length := len(s)
if length == 1 {
cHashes[0] = SLASH_HASH
return 0
}
var total typeHash = salt
for i := 1; i < length; i++ {
if s[i] == c {
cHashes[na] = total
total = salt
na++
continue
}
total = total<<5 + typeHash(s[i])
}
cHashes[na] = total
na++
return na
}
func (x *index) genUint(s string, total typeHash) typeHash {
length := len(s)
for i := 0; i < length; i++ {
total = total<<5 + typeHash(s[i])
}
return total
}
func (x *index) genSalt(s string) typeHash {
return typeHash(s[0] + s[1])
}