forked from VolantMQ/volantmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
342 lines (283 loc) · 7.85 KB
/
node.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
package mem
import (
"strings"
"github.com/VolantMQ/vlapi/mqttp"
"github.com/VolantMQ/vlapi/subscriber"
"github.com/VolantMQ/volantmq/systree"
"github.com/VolantMQ/volantmq/topics/types"
"github.com/VolantMQ/volantmq/types"
)
type topicSubscriber struct {
s topicsTypes.Subscriber
p *vlsubscriber.SubscriptionParams
}
type subscribers map[uintptr]*topicSubscriber
func (s *topicSubscriber) acquire() *publish {
s.s.Acquire()
pe := &publish{
s: s.s,
qos: s.p.Granted,
ops: s.p.Ops,
}
if s.p.ID > 0 {
pe.ids = []uint32{s.p.ID}
}
return pe
}
type publish struct {
s topicsTypes.Subscriber
ops mqttp.SubscriptionOptions
qos mqttp.QosType
ids []uint32
}
type publishes map[uintptr][]*publish
type node struct {
retained interface{}
subs subscribers
parent *node
children map[string]*node
getSubscribers func(uintptr, *publishes)
}
func newNode(overlap bool, parent *node) *node {
n := &node{
subs: make(subscribers),
children: make(map[string]*node),
parent: parent,
}
if overlap {
n.getSubscribers = n.overlappingSubscribers
} else {
n.getSubscribers = n.nonOverlappingSubscribers
}
return n
}
func (mT *provider) leafInsertNode(levels []string) *node {
root := mT.root
for _, level := range levels {
// Add node if it doesn't already exist
n, ok := root.children[level]
if !ok {
n = newNode(mT.allowOverlapping, root)
root.children[level] = n
} else {
}
root = n
}
return root
}
func (mT *provider) leafSearchNode(levels []string) *node {
root := mT.root
// run down and try get path matching given topic
for _, token := range levels {
n, ok := root.children[token]
if !ok {
return nil
}
root = n
}
return root
}
func (mT *provider) subscriptionInsert(filter string, sub topicsTypes.Subscriber, p *vlsubscriber.SubscriptionParams) bool {
levels := strings.Split(filter, "/")
root := mT.leafInsertNode(levels)
// Let's see if the subscriber is already on the list and just update QoS if so
// Otherwise create new entry
exists := false
if s, ok := root.subs[sub.Hash()]; !ok {
root.subs[sub.Hash()] = &topicSubscriber{
s: sub,
p: p,
}
} else {
s.p = p
exists = true
}
return exists
}
func (mT *provider) subscriptionRemove(topic string, sub topicsTypes.Subscriber) error {
levels := strings.Split(topic, "/")
var err error
root := mT.leafSearchNode(levels)
if root == nil {
return topicsTypes.ErrNotFound
}
// path matching the topic exists.
// if subscriber argument is nil remove all of subscribers
// otherwise try remove subscriber or set error if not exists
if sub == nil {
// If subscriber == nil, then it's signal to remove ALL subscribers
root.subs = make(subscribers)
} else {
id := sub.Hash()
if _, ok := root.subs[id]; ok {
delete(root.subs, id)
} else {
err = topicsTypes.ErrNotFound
}
}
// Run up and on each level and check if level has subscriptions and nested nodes
// If both are empty tell parent node to remove that token
level := len(levels)
for leafNode := root; leafNode != nil; leafNode = leafNode.parent {
// If there are no more subscribers or inner nodes or retained messages remove this node from parent
if len(leafNode.subs) == 0 && len(leafNode.children) == 0 && leafNode.retained == nil {
// if this is not root node
mT.onCleanUnsubscribe(levels[:level])
if leafNode.parent != nil {
delete(leafNode.parent.children, levels[level-1])
}
}
level--
}
return err
}
func subscriptionRecurseSearch(root *node, levels []string, publishID uintptr, p *publishes) {
if len(levels) == 0 {
// leaf level of the topic
// get all subscribers and return
root.getSubscribers(publishID, p)
if n, ok := root.children[topicsTypes.MWC]; ok {
n.getSubscribers(publishID, p)
}
} else {
if n, ok := root.children[topicsTypes.MWC]; ok && len(levels[0]) != 0 {
n.getSubscribers(publishID, p)
}
if n, ok := root.children[levels[0]]; ok {
subscriptionRecurseSearch(n, levels[1:], publishID, p)
}
if n, ok := root.children[topicsTypes.SWC]; ok {
subscriptionRecurseSearch(n, levels[1:], publishID, p)
}
}
}
func (mT *provider) subscriptionSearch(topic string, publishID uintptr, p *publishes) {
root := mT.root
levels := strings.Split(topic, "/")
level := levels[0]
if !strings.HasPrefix(level, "$") {
subscriptionRecurseSearch(root, levels, publishID, p)
} else if n, ok := root.children[level]; ok {
subscriptionRecurseSearch(n, levels[1:], publishID, p)
}
}
func (mT *provider) retainInsert(topic string, obj types.RetainObject) {
levels := strings.Split(topic, "/")
root := mT.leafInsertNode(levels)
root.retained = obj
}
func (mT *provider) retainRemove(topic string) error {
levels := strings.Split(topic, "/")
root := mT.leafSearchNode(levels)
if root == nil {
return topicsTypes.ErrNotFound
}
root.retained = nil
// Run up and on each level and check if level has subscriptions and nested nodes
// If both are empty tell parent node to remove that token
level := len(levels)
for leafNode := root; leafNode != nil; leafNode = leafNode.parent {
// If there are no more subscribers or inner nodes or retained messages remove this node from parent
if len(leafNode.subs) == 0 && len(leafNode.children) == 0 && leafNode.retained == nil {
// if this is not root node
if leafNode.parent != nil {
delete(leafNode.parent.children, levels[level-1])
}
}
level--
}
return nil
}
func retainRecurseSearch(root *node, levels []string, retained *[]*mqttp.Publish) {
if len(levels) == 0 {
// leaf level of the topic
root.getRetained(retained)
if n, ok := root.children[topicsTypes.MWC]; ok {
n.allRetained(retained)
}
} else {
switch levels[0] {
case topicsTypes.MWC:
// If '#', add all retained messages starting this node
root.allRetained(retained)
return
case topicsTypes.SWC:
// If '+', check all nodes at this level. Next levels must be matched.
for _, n := range root.children {
retainRecurseSearch(n, levels[1:], retained)
}
default:
if n, ok := root.children[levels[0]]; ok {
retainRecurseSearch(n, levels[1:], retained)
}
}
}
}
func (mT *provider) retainSearch(filter string, retained *[]*mqttp.Publish) {
levels := strings.Split(filter, "/")
level := levels[0]
if level == topicsTypes.MWC {
for t, n := range mT.root.children {
if t != "" && !strings.HasPrefix(t, "$") {
n.allRetained(retained)
}
}
} else if strings.HasPrefix(level, "$") && mT.root.children[level] != nil {
retainRecurseSearch(mT.root.children[level], levels[1:], retained)
} else {
retainRecurseSearch(mT.root, levels, retained)
}
}
func (sn *node) getRetained(retained *[]*mqttp.Publish) {
if sn.retained != nil {
var p *mqttp.Publish
switch t := sn.retained.(type) {
case *mqttp.Publish:
p = t
case systree.DynamicValue:
p = t.Retained()
}
// if publish has expiration set check if there time left to live
if _, _, expired := p.Expired(); !expired {
*retained = append(*retained, p)
} else {
// publish has expired, thus nobody should get it
sn.retained = nil
}
}
}
func (sn *node) allRetained(retained *[]*mqttp.Publish) {
sn.getRetained(retained)
for _, n := range sn.children {
n.allRetained(retained)
}
}
func (sn *node) overlappingSubscribers(publishID uintptr, p *publishes) {
for id, sub := range sn.subs {
if s, ok := (*p)[id]; ok {
if sub.p.ID > 0 {
s[0].ids = append(s[0].ids, sub.p.ID)
}
if s[0].qos < sub.p.Granted {
s[0].qos = sub.p.Granted
}
} else {
if !sub.p.Ops.NL() || id != publishID {
pe := sub.acquire()
(*p)[id] = append((*p)[id], pe)
}
}
}
}
func (sn *node) nonOverlappingSubscribers(publishID uintptr, p *publishes) {
for id, sub := range sn.subs {
if !sub.p.Ops.NL() || id != publishID {
pe := sub.acquire()
if _, ok := (*p)[id]; ok {
(*p)[id] = append((*p)[id], pe)
} else {
(*p)[id] = []*publish{pe}
}
}
}
}