-
Notifications
You must be signed in to change notification settings - Fork 1
/
paths.go
260 lines (217 loc) · 5.34 KB
/
paths.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
package nuts
/*
Path Prefix Scans
The prefix scanning methods `SeekPathConflict` and `SeekPathMatch` facilitate maintenance and access to buckets of paths
supporting variable elements with exclusive matches. Paths are `/` delimited, must begin with a `/`, and elements
beginning with `:` or `*` are variable.
Examples:
/
/blogs/
/blogs/:blog_id
Variable Paths
Path elements beginning with a `:` match any single element. Path elements beginning with `*` match any remaining
suffix, and therefore must be the last element.
Examples:
Path: /blogs/:blog_id
Match: /blogs/someblog
Path: /blogs/:blog_id/comments/:comment_id/*suffix
Match: /blogs/42/comments/100/edit
Exclusive Matches
Using `SeekPathConflict` before putting new paths to ensure the bucket remains conflict-free guarantees that
`SeekPathMatch` will never match more than one path.
Examples:
Conflicts: /blogs/:blog_id, /blogs/golang
Match: /blogs/golang
Conflicts: /blogs/*, /blogs/:blog_id/comments
Match: /blogs/42/comments
*/
import (
"bytes"
"github.com/boltdb/bolt"
)
// SeekPathMatch seeks an entry which matches `path`, or returns `nil, nil` when no match is found.
// Returned key may be `path`, or a matching dynamic path.
// Matches are exclusive if the set of keys are conflict free (see SeekPathConflict).
func SeekPathMatch(c *bolt.Cursor, path []byte) ([]byte, []byte) {
// Validation
if len(path) == 0 {
return nil, nil
}
if path[0] != '/' {
return nil, nil
}
// Exact match fast-path
if k, v := c.Seek(path); bytes.Equal(k, path) {
return k, v
}
// Prefix scan
prefixBuf := bytes.NewBuffer(make([]byte, 0, len(path)))
for {
// Match slash
prefixBuf.WriteByte('/')
prefix := prefixBuf.Bytes()
k, v := c.Seek(prefix)
if !bytes.HasPrefix(k, prefix) {
return nil, nil
}
// Advance past '/'
path = path[1:]
// Exact match required for trailing slash.
if len(path) == 0 {
if len(k) == len(prefix) {
return k, v
}
return nil, nil
}
// Advance cursor past exact match to first prefix match.
if len(k) == len(prefix) {
k, v = c.Next()
if !bytes.HasPrefix(k, prefix) {
return nil, nil
}
}
// Find end of element.
i := bytes.IndexByte(path, '/')
last := i < 0
switch k[len(prefix)] {
case '*':
return k, v
case ':':
// Append variable path element to prefix
ki := bytes.IndexByte(k[len(prefix):], '/')
if ki < 0 {
prefixBuf.Write(k[len(prefix):])
} else {
prefixBuf.Write(k[len(prefix) : len(prefix)+ki])
}
if last {
// Exact match required for last element.
prefix = prefixBuf.Bytes()
if k, v = c.Seek(prefix); bytes.Equal(k, prefix) {
return k, v
}
return nil, nil
}
default:
// Append path component to prefix.
if last {
prefixBuf.Write(path)
} else {
prefixBuf.Write(path[:i])
}
prefix = prefixBuf.Bytes()
k, v = c.Seek(prefix)
if last {
// Exact match required for last element.
if bytes.Equal(k, prefix) {
return k, v
}
return nil, nil
}
// Prefix match required for other elements.
if !bytes.HasPrefix(k, prefix) {
return nil, nil
}
}
// Advance past element.
path = path[i:]
}
}
// SeekPathConflict seeks an entry which conflicts with `path`, and returns the first encountered or `nil, nil` if none
// is found.
func SeekPathConflict(c *bolt.Cursor, path []byte) ([]byte, []byte) {
// Validation
if len(path) == 0 {
return nil, nil
}
if path[0] != '/' {
return nil, nil
}
// Fast-path for exact and prefix match.
if k, v := c.Seek(path); bytes.Equal(k, path) {
return k, v
} else if bytes.HasPrefix(k, path) {
// Any prefixed k is good enough when path ends in '/'.
if path[len(path)-1] == '/' {
return nil, nil
}
// If k's last element is longer it could be a conflict.
if k[len(path)] == '/' {
return nil, nil
}
}
// Prefix scan.
i := 0
for {
i++
// Match slash.
prefix := path[:i]
k, v := c.Seek(prefix)
if !bytes.HasPrefix(k, prefix) {
return nil, nil
}
// Exact match is a conflict for trailing slash.
if i == len(path) {
if len(k) == len(path) {
return k, v
}
return nil, nil
}
// Advance cursor past exact match to first prefix match.
if len(k) == len(prefix) {
k, v = c.Next()
if !bytes.HasPrefix(k, prefix) {
return nil, nil
}
}
// Find end of element.
offset := bytes.IndexByte(path[i:], '/')
last := offset < 0
if last {
i = len(path)
} else {
i += offset
}
switch k[len(prefix)] {
case '*':
return k, v
case ':':
// Find end of element.
kPrefix := k
offset := bytes.IndexByte(k[len(prefix):], '/')
if offset > 0 {
kPrefix = k[:len(prefix)+offset]
}
// Exact match required through variable element.
prefix = path[:i]
if !bytes.Equal(prefix, kPrefix) {
return k, v
}
if last {
// Exact match is a conflict for the last element.
if k, v = c.Seek(prefix); bytes.Equal(k, prefix) {
return k, v
}
return nil, nil
}
default:
// Static (non-variable) element required.
next := path[len(prefix)]
if next == ':' || next == '*' {
return k, v
}
prefix = path[:i]
k, v = c.Seek(prefix)
if last {
// Exact match is a conflict for the last element.
if bytes.Equal(k, prefix) {
return k, v
}
return nil, nil
}
if !bytes.HasPrefix(k, prefix) {
return nil, nil
}
}
}
}