-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtopic.go
312 lines (263 loc) · 7.54 KB
/
topic.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
/*
* Copyright 2021 Saffat Technologies, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package unitdb
import (
"errors"
"fmt"
"strings"
)
// Various constant on Topic.
const (
TopicInvalid = uint8(iota)
TopicStatic
TopicWildcard
TopicWildcardSymbol = "*"
TopicMultiWildcardSymbol = "..."
TopicKeySeparator = '/'
TopicSeparator = '.' // The separator character.
TopicMaxLength = 65535
TopicMaxDepth = 100 // Maximum depth for topic using a separator
)
// topicOption represents a key/value pair option.
type topicOption struct {
key string
value string
}
// topic represents a parsed topic.
type (
topic struct {
topic string // Gets or sets the topic string.
parts []string
topicOptions string
options []topicOption // Gets or sets the options.
topicType uint8
}
TopicFilter struct {
subscriptionTopic *topic
updates chan []*PubMessage
}
)
func (t *TopicFilter) Updates() <-chan []*PubMessage {
return t.updates
}
// splitFunc various split function to split topic using delimeter.
type splitFunc struct{}
func (splitFunc) splitTopic(c rune) bool {
return c == TopicSeparator
}
func (splitFunc) splitKey(c rune) bool {
return c == TopicKeySeparator
}
func (splitFunc) options(c rune) bool {
return c == '?'
}
func (splitFunc) splitOptions(c rune) bool {
return c == '&'
}
func (splitFunc) splitOpsKeyValue(c rune) bool {
return c == '='
}
// getOption retrieves a Uint option.
func (t *topic) getOption(name string) (string, bool) {
for i := 0; i < len(t.options); i++ {
if t.options[i].key == name {
return t.options[i].value, true
}
}
return "", false
}
// parseOptions parse the options from the topic.
func (t *topic) parseOptions(text string) (ok bool) {
//Parse Options
var fn splitFunc
ops := strings.FieldsFunc(text, fn.splitOptions)
if ops != nil || len(ops) >= 1 {
for _, o := range ops {
op := strings.FieldsFunc(o, fn.splitOpsKeyValue)
if op == nil || len(op) < 2 {
continue
}
t.options = append(t.options, topicOption{
key: op[0],
value: op[1],
})
}
}
return true
}
// parse attempts to parse the topic from the underlying slice.
func (t *topic) parse(text string) (ok bool) {
// start := time.Now()
// defer logger.Debug().Str("context", "topic.parseStaticTopic").Dur("duration", time.Since(start)).Msg("")
var fn splitFunc
parts := strings.FieldsFunc(text, fn.splitKey)
if parts == nil || len(parts) < 1 {
t.topicType = TopicInvalid
return
}
t.topic = parts[0]
if len(parts) > 1 {
t.topic = parts[1]
}
parts = strings.FieldsFunc(t.topic, fn.options)
l := len(parts)
if parts == nil || l < 1 {
t.topicType = TopicInvalid
return
}
if l > 1 {
t.topicOptions = parts[1]
}
t.topic = parts[0]
ok = t.parseOptions(t.topicOptions)
if !ok {
t.topicType = TopicInvalid
return false
}
var multiWildcard bool
if strings.HasSuffix(t.topic, TopicMultiWildcardSymbol) {
t.topic = strings.TrimRight(t.topic, TopicMultiWildcardSymbol)
multiWildcard = true
}
t.parts = strings.FieldsFunc(t.topic, fn.splitTopic)
if multiWildcard {
t.parts = append(t.parts, TopicMultiWildcardSymbol)
}
return true
}
func (t *topic) validate(fn ...func(t *topic) error) error {
for _, f := range fn {
if err := f(t); err != nil {
return err
}
}
return nil
}
func validateWildcards(t *topic) error {
if strings.Contains(t.topic, TopicMultiWildcardSymbol) || strings.Contains(t.topic, TopicWildcardSymbol) {
return errors.New("publish: cannot publish to a topic that contains topic wildcard (* or ...)")
}
return nil
}
func validateMinLength(t *topic) error {
if len(t.topic) == 0 {
return errors.New("topic must contain at least one character")
}
return nil
}
func validateMaxLenth(t *topic) error {
if len(t.topic) > TopicMaxLength {
return fmt.Errorf("the length of topic %d is longer than the max topic length allowed %d", len(t.topic), TopicMaxLength)
}
return nil
}
func validateMaxDepth(t *topic) error {
if len(t.parts) > TopicMaxDepth {
return fmt.Errorf("the depath of topic %d is longer than the max topic length allowed %d", len(t.parts), TopicMaxDepth)
}
return nil
}
func validateMultiWildcard(t *topic) error {
if strings.Contains(t.topic, TopicMultiWildcardSymbol) &&
!(strings.HasSuffix(t.topic, TopicMultiWildcardSymbol)) {
return errors.New("the topic multiwild ... can only be present at the end of a topic")
}
return nil
}
func validateTopicParts(t *topic) error {
for _, part := range t.parts {
if strings.Contains(part, TopicWildcardSymbol) && len(part) > 1 {
return errors.New("topic part contains a wildcard but is more than one character long")
}
}
return nil
}
func (t *TopicFilter) filter(notice *Notice) error {
messages := make([]*PubMessage, 0)
for _, pubMsg := range notice.messages {
pubTopic := new(topic)
// parse the topic.
if ok := pubTopic.parse(pubMsg.Topic); !ok {
return fmt.Errorf("filter: unable to parse topic")
}
if err := pubTopic.validate(validateMinLength,
validateMaxLenth,
validateMaxDepth,
validateMultiWildcard,
validateTopicParts); err != nil {
return err
}
if t.subscriptionTopic.matches(pubTopic) {
messages = append(messages, notice.messages...)
}
}
if len(messages) > 0 {
t.updates <- messages
}
return nil
}
func (t *topic) matches(pubTopic *topic) bool {
// If the topic is just multi wildcard then return matches to true.
if t.topic == TopicMultiWildcardSymbol ||
pubTopic.topic == TopicWildcardSymbol {
return true
}
// If the topics are an exact match then matches to true.
if t.topic == pubTopic.topic {
return true
}
// no match yet so we need to check each part
for i := 0; i < len(t.parts); i++ {
lhsPart := t.parts[i]
// If we've reached a multi wildcard in the lhs topic,
// we have a match.
// (this is the rule finance matches finance or finance...)
if lhsPart == TopicMultiWildcardSymbol {
return true
}
isLhsWildcard := lhsPart == TopicWildcardSymbol
// If we've reached a wildcard match but the matchee does
// not have anything at this part level then it's not a match.
// (this is the rule 'finance does not match finance.*'
if isLhsWildcard && len(pubTopic.parts) <= i {
return false
}
// if lhs is not a wildcard we need to check whether the
// two parts match each other.
if !isLhsWildcard {
rhsPart := pubTopic.parts[i]
// If the lhs part is not wildcard then we need an exact match
if lhsPart != rhsPart {
return false
}
}
// If we're at the last part of the lhs topic but there are
// more patrs in the in the publicationTopic then the publicationTopic
// is too specific to be a match.
if i+1 == len(t.parts) &&
len(pubTopic.parts) > len(t.parts) {
return false
}
if i+1 == len(pubTopic.parts) &&
len(pubTopic.parts) < len(t.parts) {
return false
}
// If we're here the current part matches so check the next
}
// If we exit out of the loop without a return then we have a full match which would
// have been caught by the original exact match check at the top anyway.
return true
}