| 1 |
// Copyright 2016-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"errors" |
| 19 |
"iter" |
| 20 |
"strings" |
| 21 |
"sync" |
| 22 |
"sync/atomic" |
| 23 |
"unicode/utf8" |
| 24 |
) |
| 25 |
|
| 26 |
// Sublist is a routing mechanism to handle subject distribution and |
| 27 |
// provides a facility to match subjects from published messages to |
| 28 |
// interested subscribers. Subscribers can have wildcard subjects to |
| 29 |
// match multiple published subjects. |
| 30 |
|
| 31 |
// Common byte variables for wildcards and token separator. |
| 32 |
const ( |
| 33 |
pwc = '*' |
| 34 |
pwcs = "*" |
| 35 |
fwc = '>' |
| 36 |
fwcs = ">" |
| 37 |
tsep = "." |
| 38 |
btsep = '.' |
| 39 |
) |
| 40 |
|
| 41 |
// Sublist related errors |
| 42 |
var ( |
| 43 |
ErrInvalidSubject = errors.New("sublist: invalid subject") |
| 44 |
ErrNotFound = errors.New("sublist: no matches found") |
| 45 |
ErrNilChan = errors.New("sublist: nil channel") |
| 46 |
ErrAlreadyRegistered = errors.New("sublist: notification already registered") |
| 47 |
) |
| 48 |
|
| 49 |
const ( |
| 50 |
// cacheMax is used to bound limit the frontend cache |
| 51 |
slCacheMax = 1024 |
| 52 |
// If we run a sweeper we will drain to this count. |
| 53 |
slCacheSweep = 256 |
| 54 |
// plistMin is our lower bounds to create a fast plist for Match. |
| 55 |
plistMin = 256 |
| 56 |
) |
| 57 |
|
| 58 |
// SublistResult is a result structure better optimized for queue subs. |
| 59 |
type SublistResult struct { |
| 60 |
psubs []*subscription |
| 61 |
qsubs [][]*subscription // don't make this a map, too expensive to iterate |
| 62 |
} |
| 63 |
|
| 64 |
// A Sublist stores and efficiently retrieves subscriptions. |
| 65 |
type Sublist struct { |
| 66 |
sync.RWMutex |
| 67 |
genid uint64 |
| 68 |
matches uint64 |
| 69 |
cacheHits uint64 |
| 70 |
inserts uint64 |
| 71 |
removes uint64 |
| 72 |
root *level |
| 73 |
cache map[string]*SublistResult |
| 74 |
ccSweep int32 |
| 75 |
notify *notifyMaps |
| 76 |
count uint32 |
| 77 |
} |
| 78 |
|
| 79 |
// notifyMaps holds maps of arrays of channels for notifications |
| 80 |
// on a change of interest. |
| 81 |
type notifyMaps struct { |
| 82 |
insert map[string][]chan<- bool |
| 83 |
remove map[string][]chan<- bool |
| 84 |
} |
| 85 |
|
| 86 |
// A node contains subscriptions and a pointer to the next level. |
| 87 |
type node struct { |
| 88 |
next *level |
| 89 |
psubs map[*subscription]struct{} |
| 90 |
qsubs map[string]map[*subscription]struct{} |
| 91 |
plist []*subscription |
| 92 |
} |
| 93 |
|
| 94 |
// A level represents a group of nodes and special pointers to |
| 95 |
// wildcard nodes. |
| 96 |
type level struct { |
| 97 |
nodes map[string]*node |
| 98 |
pwc, fwc *node |
| 99 |
} |
| 100 |
|
| 101 |
// Create a new default node. |
| 102 |
func newNode() *node { |
| 103 |
return &node{psubs: make(map[*subscription]struct{})} |
| 104 |
} |
| 105 |
|
| 106 |
// Create a new default level. |
| 107 |
func newLevel() *level { |
| 108 |
return &level{nodes: make(map[string]*node)} |
| 109 |
} |
| 110 |
|
| 111 |
// In general caching is recommended however in some extreme cases where |
| 112 |
// interest changes are high, suppressing the cache can help. |
| 113 |
// https://github.com/nats-io/nats-server/issues/941 |
| 114 |
// FIXME(dlc) - should be more dynamic at some point based on cache thrashing. |
| 115 |
|
| 116 |
// NewSublist will create a default sublist with caching enabled per the flag. |
| 117 |
func NewSublist(enableCache bool) *Sublist { |
| 118 |
if enableCache { |
| 119 |
return &Sublist{root: newLevel(), cache: make(map[string]*SublistResult)} |
| 120 |
} |
| 121 |
return &Sublist{root: newLevel()} |
| 122 |
} |
| 123 |
|
| 124 |
// NewSublistForServer will create a default sublist with caching enabled determined |
| 125 |
// by the server options. |
| 126 |
func NewSublistForServer(srv *Server) *Sublist { |
| 127 |
if srv == nil { |
| 128 |
return NewSublistNoCache() // Probably just unit tests. |
| 129 |
} |
| 130 |
if opts := srv.getOpts(); opts != nil { |
| 131 |
return NewSublist(!opts.NoSublistCache) |
| 132 |
} |
| 133 |
return NewSublistNoCache() |
| 134 |
} |
| 135 |
|
| 136 |
// NewSublistWithCache will create a default sublist with caching enabled. |
| 137 |
func NewSublistWithCache() *Sublist { |
| 138 |
return NewSublist(true) |
| 139 |
} |
| 140 |
|
| 141 |
// NewSublistNoCache will create a default sublist with caching disabled. |
| 142 |
func NewSublistNoCache() *Sublist { |
| 143 |
return NewSublist(false) |
| 144 |
} |
| 145 |
|
| 146 |
// CacheEnabled returns whether or not caching is enabled for this sublist. |
| 147 |
func (s *Sublist) CacheEnabled() bool { |
| 148 |
s.RLock() |
| 149 |
enabled := s.cache != nil |
| 150 |
s.RUnlock() |
| 151 |
return enabled |
| 152 |
} |
| 153 |
|
| 154 |
// RegisterNotification will register for notifications when interest for the given |
| 155 |
// subject changes. The subject must be a literal publish type subject. |
| 156 |
// The notification is true for when the first interest for a subject is inserted, |
| 157 |
// and false when all interest in the subject is removed. Note that this interest |
| 158 |
// needs to be exact and that wildcards will not trigger the notifications. The sublist |
| 159 |
// will not block when trying to send the notification. Its up to the caller to make |
| 160 |
// sure the channel send will not block. |
| 161 |
func (s *Sublist) RegisterNotification(subject string, notify chan<- bool) error { |
| 162 |
return s.registerNotification(subject, _EMPTY_, notify) |
| 163 |
} |
| 164 |
|
| 165 |
func (s *Sublist) RegisterQueueNotification(subject, queue string, notify chan<- bool) error { |
| 166 |
return s.registerNotification(subject, queue, notify) |
| 167 |
} |
| 168 |
|
| 169 |
func (s *Sublist) registerNotification(subject, queue string, notify chan<- bool) error { |
| 170 |
if subjectHasWildcard(subject) { |
| 171 |
return ErrInvalidSubject |
| 172 |
} |
| 173 |
if notify == nil { |
| 174 |
return ErrNilChan |
| 175 |
} |
| 176 |
|
| 177 |
var hasInterest bool |
| 178 |
r := s.Match(subject) |
| 179 |
|
| 180 |
if len(r.psubs)+len(r.qsubs) > 0 { |
| 181 |
if queue == _EMPTY_ { |
| 182 |
for _, sub := range r.psubs { |
| 183 |
if string(sub.subject) == subject { |
| 184 |
hasInterest = true |
| 185 |
break |
| 186 |
} |
| 187 |
} |
| 188 |
} else { |
| 189 |
for _, qsub := range r.qsubs { |
| 190 |
qs := qsub[0] |
| 191 |
if string(qs.subject) == subject && string(qs.queue) == queue { |
| 192 |
hasInterest = true |
| 193 |
break |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
key := keyFromSubjectAndQueue(subject, queue) |
| 200 |
var err error |
| 201 |
|
| 202 |
s.Lock() |
| 203 |
if s.notify == nil { |
| 204 |
s.notify = ¬ifyMaps{ |
| 205 |
insert: make(map[string][]chan<- bool), |
| 206 |
remove: make(map[string][]chan<- bool), |
| 207 |
} |
| 208 |
} |
| 209 |
// Check which list to add us to. |
| 210 |
if hasInterest { |
| 211 |
err = s.addRemoveNotify(key, notify) |
| 212 |
} else { |
| 213 |
err = s.addInsertNotify(key, notify) |
| 214 |
} |
| 215 |
s.Unlock() |
| 216 |
|
| 217 |
if err == nil { |
| 218 |
sendNotification(notify, hasInterest) |
| 219 |
} |
| 220 |
return err |
| 221 |
} |
| 222 |
|
| 223 |
// Lock should be held. |
| 224 |
func chkAndRemove(key string, notify chan<- bool, ms map[string][]chan<- bool) bool { |
| 225 |
chs := ms[key] |
| 226 |
for i, ch := range chs { |
| 227 |
if ch == notify { |
| 228 |
chs[i] = chs[len(chs)-1] |
| 229 |
chs = chs[:len(chs)-1] |
| 230 |
if len(chs) == 0 { |
| 231 |
delete(ms, key) |
| 232 |
} |
| 233 |
return true |
| 234 |
} |
| 235 |
} |
| 236 |
return false |
| 237 |
} |
| 238 |
|
| 239 |
func (s *Sublist) ClearNotification(subject string, notify chan<- bool) bool { |
| 240 |
return s.clearNotification(subject, _EMPTY_, notify) |
| 241 |
} |
| 242 |
|
| 243 |
func (s *Sublist) ClearQueueNotification(subject, queue string, notify chan<- bool) bool { |
| 244 |
return s.clearNotification(subject, queue, notify) |
| 245 |
} |
| 246 |
|
| 247 |
func (s *Sublist) clearNotification(subject, queue string, notify chan<- bool) bool { |
| 248 |
s.Lock() |
| 249 |
if s.notify == nil { |
| 250 |
s.Unlock() |
| 251 |
return false |
| 252 |
} |
| 253 |
key := keyFromSubjectAndQueue(subject, queue) |
| 254 |
// Check both, start with remove. |
| 255 |
didRemove := chkAndRemove(key, notify, s.notify.remove) |
| 256 |
didRemove = didRemove || chkAndRemove(key, notify, s.notify.insert) |
| 257 |
// Check if everything is gone |
| 258 |
if len(s.notify.remove)+len(s.notify.insert) == 0 { |
| 259 |
s.notify = nil |
| 260 |
} |
| 261 |
s.Unlock() |
| 262 |
return didRemove |
| 263 |
} |
| 264 |
|
| 265 |
func sendNotification(ch chan<- bool, hasInterest bool) { |
| 266 |
select { |
| 267 |
case ch <- hasInterest: |
| 268 |
default: |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
// Add a new channel for notification in insert map. |
| 273 |
// Write lock should be held. |
| 274 |
func (s *Sublist) addInsertNotify(subject string, notify chan<- bool) error { |
| 275 |
return s.addNotify(s.notify.insert, subject, notify) |
| 276 |
} |
| 277 |
|
| 278 |
// Add a new channel for notification in removal map. |
| 279 |
// Write lock should be held. |
| 280 |
func (s *Sublist) addRemoveNotify(subject string, notify chan<- bool) error { |
| 281 |
return s.addNotify(s.notify.remove, subject, notify) |
| 282 |
} |
| 283 |
|
| 284 |
// Add a new channel for notification. |
| 285 |
// Write lock should be held. |
| 286 |
func (s *Sublist) addNotify(m map[string][]chan<- bool, subject string, notify chan<- bool) error { |
| 287 |
chs := m[subject] |
| 288 |
if len(chs) > 0 { |
| 289 |
// Check to see if this chan is already registered. |
| 290 |
for _, ch := range chs { |
| 291 |
if ch == notify { |
| 292 |
return ErrAlreadyRegistered |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
m[subject] = append(chs, notify) |
| 298 |
return nil |
| 299 |
} |
| 300 |
|
| 301 |
// To generate a key from subject and queue. We just add spc. |
| 302 |
func keyFromSubjectAndQueue(subject, queue string) string { |
| 303 |
if len(queue) == 0 { |
| 304 |
return subject |
| 305 |
} |
| 306 |
var sb strings.Builder |
| 307 |
sb.WriteString(subject) |
| 308 |
sb.WriteString(" ") |
| 309 |
sb.WriteString(queue) |
| 310 |
return sb.String() |
| 311 |
} |
| 312 |
|
| 313 |
// chkForInsertNotification will check to see if we need to notify on this subject. |
| 314 |
// Write lock should be held. |
| 315 |
func (s *Sublist) chkForInsertNotification(subject, queue string) { |
| 316 |
key := keyFromSubjectAndQueue(subject, queue) |
| 317 |
|
| 318 |
// All notify subjects are also literal so just do a hash lookup here. |
| 319 |
if chs := s.notify.insert[key]; len(chs) > 0 { |
| 320 |
for _, ch := range chs { |
| 321 |
sendNotification(ch, true) |
| 322 |
} |
| 323 |
// Move from the insert map to the remove map. |
| 324 |
s.notify.remove[key] = append(s.notify.remove[key], chs...) |
| 325 |
delete(s.notify.insert, key) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// chkForRemoveNotification will check to see if we need to notify on this subject. |
| 330 |
// Write lock should be held. |
| 331 |
func (s *Sublist) chkForRemoveNotification(subject, queue string) { |
| 332 |
key := keyFromSubjectAndQueue(subject, queue) |
| 333 |
if chs := s.notify.remove[key]; len(chs) > 0 { |
| 334 |
// We need to always check that we have no interest anymore. |
| 335 |
var hasInterest bool |
| 336 |
r := s.matchNoLock(subject) |
| 337 |
|
| 338 |
if len(r.psubs)+len(r.qsubs) > 0 { |
| 339 |
if queue == _EMPTY_ { |
| 340 |
for _, sub := range r.psubs { |
| 341 |
if string(sub.subject) == subject { |
| 342 |
hasInterest = true |
| 343 |
break |
| 344 |
} |
| 345 |
} |
| 346 |
} else { |
| 347 |
for _, qsub := range r.qsubs { |
| 348 |
qs := qsub[0] |
| 349 |
if string(qs.subject) == subject && string(qs.queue) == queue { |
| 350 |
hasInterest = true |
| 351 |
break |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
if !hasInterest { |
| 357 |
for _, ch := range chs { |
| 358 |
sendNotification(ch, false) |
| 359 |
} |
| 360 |
// Move from the remove map to the insert map. |
| 361 |
s.notify.insert[key] = append(s.notify.insert[key], chs...) |
| 362 |
delete(s.notify.remove, key) |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// Insert adds a subscription into the sublist |
| 368 |
func (s *Sublist) Insert(sub *subscription) error { |
| 369 |
// copy the subject since we hold this and this might be part of a large byte slice. |
| 370 |
subject := string(sub.subject) |
| 371 |
|
| 372 |
s.Lock() |
| 373 |
|
| 374 |
var sfwc, haswc, isnew bool |
| 375 |
var n *node |
| 376 |
l := s.root |
| 377 |
|
| 378 |
for t := range strings.SplitSeq(subject, tsep) { |
| 379 |
lt := len(t) |
| 380 |
if lt == 0 || sfwc { |
| 381 |
s.Unlock() |
| 382 |
return ErrInvalidSubject |
| 383 |
} |
| 384 |
|
| 385 |
if lt > 1 { |
| 386 |
n = l.nodes[t] |
| 387 |
} else { |
| 388 |
switch t[0] { |
| 389 |
case pwc: |
| 390 |
n = l.pwc |
| 391 |
haswc = true |
| 392 |
case fwc: |
| 393 |
n = l.fwc |
| 394 |
haswc, sfwc = true, true |
| 395 |
default: |
| 396 |
n = l.nodes[t] |
| 397 |
} |
| 398 |
} |
| 399 |
if n == nil { |
| 400 |
n = newNode() |
| 401 |
if lt > 1 { |
| 402 |
l.nodes[t] = n |
| 403 |
} else { |
| 404 |
switch t[0] { |
| 405 |
case pwc: |
| 406 |
l.pwc = n |
| 407 |
case fwc: |
| 408 |
l.fwc = n |
| 409 |
default: |
| 410 |
l.nodes[t] = n |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
if n.next == nil { |
| 415 |
n.next = newLevel() |
| 416 |
} |
| 417 |
l = n.next |
| 418 |
} |
| 419 |
if sub.queue == nil { |
| 420 |
n.psubs[sub] = struct{}{} |
| 421 |
isnew = len(n.psubs) == 1 |
| 422 |
if n.plist != nil { |
| 423 |
n.plist = append(n.plist, sub) |
| 424 |
} else if len(n.psubs) > plistMin { |
| 425 |
n.plist = make([]*subscription, 0, len(n.psubs)) |
| 426 |
// Populate |
| 427 |
for psub := range n.psubs { |
| 428 |
n.plist = append(n.plist, psub) |
| 429 |
} |
| 430 |
} |
| 431 |
} else { |
| 432 |
if n.qsubs == nil { |
| 433 |
n.qsubs = make(map[string]map[*subscription]struct{}) |
| 434 |
} |
| 435 |
qname := string(sub.queue) |
| 436 |
// This is a queue subscription |
| 437 |
subs, ok := n.qsubs[qname] |
| 438 |
if !ok { |
| 439 |
subs = make(map[*subscription]struct{}) |
| 440 |
n.qsubs[qname] = subs |
| 441 |
isnew = true |
| 442 |
} |
| 443 |
subs[sub] = struct{}{} |
| 444 |
} |
| 445 |
|
| 446 |
s.count++ |
| 447 |
s.inserts++ |
| 448 |
|
| 449 |
s.addToCache(subject, sub) |
| 450 |
atomic.AddUint64(&s.genid, 1) |
| 451 |
|
| 452 |
if s.notify != nil && isnew && !haswc && len(s.notify.insert) > 0 { |
| 453 |
s.chkForInsertNotification(subject, string(sub.queue)) |
| 454 |
} |
| 455 |
s.Unlock() |
| 456 |
|
| 457 |
return nil |
| 458 |
} |
| 459 |
|
| 460 |
// Deep copy |
| 461 |
func copyResult(r *SublistResult) *SublistResult { |
| 462 |
nr := &SublistResult{} |
| 463 |
nr.psubs = append([]*subscription(nil), r.psubs...) |
| 464 |
for _, qr := range r.qsubs { |
| 465 |
nqr := append([]*subscription(nil), qr...) |
| 466 |
nr.qsubs = append(nr.qsubs, nqr) |
| 467 |
} |
| 468 |
return nr |
| 469 |
} |
| 470 |
|
| 471 |
// Adds a new sub to an existing result. |
| 472 |
func (r *SublistResult) addSubToResult(sub *subscription) *SublistResult { |
| 473 |
// Copy since others may have a reference. |
| 474 |
nr := copyResult(r) |
| 475 |
if sub.queue == nil { |
| 476 |
nr.psubs = append(nr.psubs, sub) |
| 477 |
} else { |
| 478 |
if i := findQSlot(sub.queue, nr.qsubs); i >= 0 { |
| 479 |
nr.qsubs[i] = append(nr.qsubs[i], sub) |
| 480 |
} else { |
| 481 |
nr.qsubs = append(nr.qsubs, []*subscription{sub}) |
| 482 |
} |
| 483 |
} |
| 484 |
return nr |
| 485 |
} |
| 486 |
|
| 487 |
// addToCache will add the new entry to the existing cache |
| 488 |
// entries if needed. Assumes write lock is held. |
| 489 |
// Assumes write lock is held. |
| 490 |
func (s *Sublist) addToCache(subject string, sub *subscription) { |
| 491 |
if s.cache == nil { |
| 492 |
return |
| 493 |
} |
| 494 |
// If literal we can direct match. |
| 495 |
if subjectIsLiteral(subject) { |
| 496 |
if r := s.cache[subject]; r != nil { |
| 497 |
s.cache[subject] = r.addSubToResult(sub) |
| 498 |
} |
| 499 |
return |
| 500 |
} |
| 501 |
for key, r := range s.cache { |
| 502 |
if matchLiteral(key, subject) { |
| 503 |
s.cache[key] = r.addSubToResult(sub) |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
// removeFromCache will remove the sub from any active cache entries. |
| 509 |
// Assumes write lock is held. |
| 510 |
func (s *Sublist) removeFromCache(subject string) { |
| 511 |
if s.cache == nil { |
| 512 |
return |
| 513 |
} |
| 514 |
// If literal we can direct match. |
| 515 |
if subjectIsLiteral(subject) { |
| 516 |
delete(s.cache, subject) |
| 517 |
return |
| 518 |
} |
| 519 |
// Wildcard here. |
| 520 |
for key := range s.cache { |
| 521 |
if matchLiteral(key, subject) { |
| 522 |
delete(s.cache, key) |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
// a place holder for an empty result. |
| 528 |
var emptyResult = &SublistResult{} |
| 529 |
|
| 530 |
// Match will match all entries to the literal subject. |
| 531 |
// It will return a set of results for both normal and queue subscribers. |
| 532 |
func (s *Sublist) Match(subject string) *SublistResult { |
| 533 |
return s.match(subject, true, false) |
| 534 |
} |
| 535 |
|
| 536 |
// MatchBytes will match all entries to the literal subject. |
| 537 |
// It will return a set of results for both normal and queue subscribers. |
| 538 |
func (s *Sublist) MatchBytes(subject []byte) *SublistResult { |
| 539 |
return s.match(bytesToString(subject), true, true) |
| 540 |
} |
| 541 |
|
| 542 |
// HasInterest will return whether or not there is any interest in the subject. |
| 543 |
// In cases where more detail is not required, this may be faster than Match. |
| 544 |
func (s *Sublist) HasInterest(subject string) bool { |
| 545 |
return s.hasInterest(subject, true, nil, nil) |
| 546 |
} |
| 547 |
|
| 548 |
// NumInterest will return the number of subs/qsubs interested in the subject. |
| 549 |
// In cases where more detail is not required, this may be faster than Match. |
| 550 |
func (s *Sublist) NumInterest(subject string) (np, nq int) { |
| 551 |
s.hasInterest(subject, true, &np, &nq) |
| 552 |
return |
| 553 |
} |
| 554 |
|
| 555 |
func (s *Sublist) matchNoLock(subject string) *SublistResult { |
| 556 |
return s.match(subject, false, false) |
| 557 |
} |
| 558 |
|
| 559 |
func (s *Sublist) match(subject string, doLock bool, doCopyOnCache bool) *SublistResult { |
| 560 |
atomic.AddUint64(&s.matches, 1) |
| 561 |
|
| 562 |
// Check cache first. |
| 563 |
if doLock { |
| 564 |
s.RLock() |
| 565 |
} |
| 566 |
cacheEnabled := s.cache != nil |
| 567 |
r, ok := s.cache[subject] |
| 568 |
if doLock { |
| 569 |
s.RUnlock() |
| 570 |
} |
| 571 |
if ok { |
| 572 |
atomic.AddUint64(&s.cacheHits, 1) |
| 573 |
return r |
| 574 |
} |
| 575 |
|
| 576 |
tsa := [32]string{} |
| 577 |
tokens := tsa[:0] |
| 578 |
start := 0 |
| 579 |
for i := 0; i < len(subject); i++ { |
| 580 |
if subject[i] == btsep { |
| 581 |
if i-start == 0 { |
| 582 |
return emptyResult |
| 583 |
} |
| 584 |
tokens = append(tokens, subject[start:i]) |
| 585 |
start = i + 1 |
| 586 |
} |
| 587 |
} |
| 588 |
if start >= len(subject) { |
| 589 |
return emptyResult |
| 590 |
} |
| 591 |
tokens = append(tokens, subject[start:]) |
| 592 |
|
| 593 |
// FIXME(dlc) - Make shared pool between sublist and client readLoop? |
| 594 |
result := &SublistResult{} |
| 595 |
|
| 596 |
// Get result from the main structure and place into the shared cache. |
| 597 |
// Hold the read lock to avoid race between match and store. |
| 598 |
var n int |
| 599 |
|
| 600 |
if doLock { |
| 601 |
if cacheEnabled { |
| 602 |
s.Lock() |
| 603 |
} else { |
| 604 |
s.RLock() |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
matchLevel(s.root, tokens, result) |
| 609 |
// Check for empty result. |
| 610 |
if len(result.psubs) == 0 && len(result.qsubs) == 0 { |
| 611 |
result = emptyResult |
| 612 |
} |
| 613 |
if cacheEnabled { |
| 614 |
if doCopyOnCache { |
| 615 |
subject = copyString(subject) |
| 616 |
} |
| 617 |
s.cache[subject] = result |
| 618 |
n = len(s.cache) |
| 619 |
} |
| 620 |
if doLock { |
| 621 |
if cacheEnabled { |
| 622 |
s.Unlock() |
| 623 |
} else { |
| 624 |
s.RUnlock() |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
// Reduce the cache count if we have exceeded our set maximum. |
| 629 |
if cacheEnabled && n > slCacheMax && atomic.CompareAndSwapInt32(&s.ccSweep, 0, 1) { |
| 630 |
go s.reduceCacheCount() |
| 631 |
} |
| 632 |
|
| 633 |
return result |
| 634 |
} |
| 635 |
|
| 636 |
func (s *Sublist) hasInterest(subject string, doLock bool, np, nq *int) bool { |
| 637 |
// Check cache first. |
| 638 |
if doLock { |
| 639 |
s.RLock() |
| 640 |
} |
| 641 |
var matched, ok bool |
| 642 |
if s.cache != nil { |
| 643 |
var r *SublistResult |
| 644 |
if r, ok = s.cache[subject]; ok { |
| 645 |
if np != nil && nq != nil { |
| 646 |
*np += len(r.psubs) |
| 647 |
for _, qsub := range r.qsubs { |
| 648 |
*nq += len(qsub) |
| 649 |
} |
| 650 |
} |
| 651 |
matched = len(r.psubs)+len(r.qsubs) > 0 |
| 652 |
} |
| 653 |
} |
| 654 |
if doLock { |
| 655 |
s.RUnlock() |
| 656 |
} |
| 657 |
if ok { |
| 658 |
atomic.AddUint64(&s.cacheHits, 1) |
| 659 |
return matched |
| 660 |
} |
| 661 |
|
| 662 |
tsa := [32]string{} |
| 663 |
tokens := tsa[:0] |
| 664 |
start := 0 |
| 665 |
for i := 0; i < len(subject); i++ { |
| 666 |
if subject[i] == btsep { |
| 667 |
if i-start == 0 { |
| 668 |
return false |
| 669 |
} |
| 670 |
tokens = append(tokens, subject[start:i]) |
| 671 |
start = i + 1 |
| 672 |
} |
| 673 |
} |
| 674 |
if start >= len(subject) { |
| 675 |
return false |
| 676 |
} |
| 677 |
tokens = append(tokens, subject[start:]) |
| 678 |
|
| 679 |
if doLock { |
| 680 |
s.RLock() |
| 681 |
defer s.RUnlock() |
| 682 |
} |
| 683 |
return matchLevelForAny(s.root, tokens, np, nq) |
| 684 |
} |
| 685 |
|
| 686 |
// Remove entries in the cache until we are under the maximum. |
| 687 |
// TODO(dlc) this could be smarter now that its not inline. |
| 688 |
func (s *Sublist) reduceCacheCount() { |
| 689 |
defer atomic.StoreInt32(&s.ccSweep, 0) |
| 690 |
// If we are over the cache limit randomly drop until under the limit. |
| 691 |
s.Lock() |
| 692 |
for key := range s.cache { |
| 693 |
delete(s.cache, key) |
| 694 |
if len(s.cache) <= slCacheSweep { |
| 695 |
break |
| 696 |
} |
| 697 |
} |
| 698 |
s.Unlock() |
| 699 |
} |
| 700 |
|
| 701 |
// Helper function for auto-expanding remote qsubs. |
| 702 |
func isRemoteQSub(sub *subscription) bool { |
| 703 |
return sub != nil && sub.queue != nil && sub.client != nil && (sub.client.kind == ROUTER || sub.client.kind == LEAF) |
| 704 |
} |
| 705 |
|
| 706 |
// UpdateRemoteQSub should be called when we update the weight of an existing |
| 707 |
// remote queue sub. |
| 708 |
func (s *Sublist) UpdateRemoteQSub(sub *subscription) { |
| 709 |
// We could search to make sure we find it, but probably not worth |
| 710 |
// it unless we are thrashing the cache. Just remove from our L2 and update |
| 711 |
// the genid so L1 will be flushed. |
| 712 |
s.Lock() |
| 713 |
s.removeFromCache(string(sub.subject)) |
| 714 |
atomic.AddUint64(&s.genid, 1) |
| 715 |
s.Unlock() |
| 716 |
} |
| 717 |
|
| 718 |
// This will add in a node's results to the total results. |
| 719 |
func addNodeToResults(n *node, results *SublistResult) { |
| 720 |
// Normal subscriptions |
| 721 |
if n.plist != nil { |
| 722 |
results.psubs = append(results.psubs, n.plist...) |
| 723 |
} else { |
| 724 |
for psub := range n.psubs { |
| 725 |
results.psubs = append(results.psubs, psub) |
| 726 |
} |
| 727 |
} |
| 728 |
// Queue subscriptions |
| 729 |
for qname, qr := range n.qsubs { |
| 730 |
if len(qr) == 0 { |
| 731 |
continue |
| 732 |
} |
| 733 |
// Need to find matching list in results |
| 734 |
var i int |
| 735 |
if i = findQSlot([]byte(qname), results.qsubs); i < 0 { |
| 736 |
i = len(results.qsubs) |
| 737 |
nqsub := make([]*subscription, 0, len(qr)) |
| 738 |
results.qsubs = append(results.qsubs, nqsub) |
| 739 |
} |
| 740 |
for sub := range qr { |
| 741 |
if isRemoteQSub(sub) { |
| 742 |
ns := atomic.LoadInt32(&sub.qw) |
| 743 |
// Shadow these subscriptions |
| 744 |
for n := 0; n < int(ns); n++ { |
| 745 |
results.qsubs[i] = append(results.qsubs[i], sub) |
| 746 |
} |
| 747 |
} else { |
| 748 |
results.qsubs[i] = append(results.qsubs[i], sub) |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
// We do not use a map here since we want iteration to be past when |
| 755 |
// processing publishes in L1 on client. So we need to walk sequentially |
| 756 |
// for now. Keep an eye on this in case we start getting large number of |
| 757 |
// different queue subscribers for the same subject. |
| 758 |
func findQSlot(queue []byte, qsl [][]*subscription) int { |
| 759 |
if queue == nil { |
| 760 |
return -1 |
| 761 |
} |
| 762 |
for i, qr := range qsl { |
| 763 |
if len(qr) > 0 && bytes.Equal(queue, qr[0].queue) { |
| 764 |
return i |
| 765 |
} |
| 766 |
} |
| 767 |
return -1 |
| 768 |
} |
| 769 |
|
| 770 |
// matchLevel is used to recursively descend into the trie. |
| 771 |
func matchLevel(l *level, toks []string, results *SublistResult) { |
| 772 |
var pwc, n *node |
| 773 |
for i, t := range toks { |
| 774 |
if l == nil { |
| 775 |
return |
| 776 |
} |
| 777 |
if l.fwc != nil { |
| 778 |
addNodeToResults(l.fwc, results) |
| 779 |
} |
| 780 |
if pwc = l.pwc; pwc != nil { |
| 781 |
matchLevel(pwc.next, toks[i+1:], results) |
| 782 |
} |
| 783 |
n = l.nodes[t] |
| 784 |
if n != nil { |
| 785 |
l = n.next |
| 786 |
} else { |
| 787 |
l = nil |
| 788 |
} |
| 789 |
} |
| 790 |
if n != nil { |
| 791 |
addNodeToResults(n, results) |
| 792 |
} |
| 793 |
if pwc != nil { |
| 794 |
addNodeToResults(pwc, results) |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
func matchLevelForAny(l *level, toks []string, np, nq *int) bool { |
| 799 |
var pwc, n *node |
| 800 |
for i, t := range toks { |
| 801 |
if l == nil { |
| 802 |
return false |
| 803 |
} |
| 804 |
if l.fwc != nil { |
| 805 |
if np != nil && nq != nil { |
| 806 |
*np += len(l.fwc.psubs) |
| 807 |
for _, qsub := range l.fwc.qsubs { |
| 808 |
*nq += len(qsub) |
| 809 |
} |
| 810 |
} |
| 811 |
return true |
| 812 |
} |
| 813 |
if pwc = l.pwc; pwc != nil { |
| 814 |
if match := matchLevelForAny(pwc.next, toks[i+1:], np, nq); match { |
| 815 |
return true |
| 816 |
} |
| 817 |
} |
| 818 |
n = l.nodes[t] |
| 819 |
if n != nil { |
| 820 |
l = n.next |
| 821 |
} else { |
| 822 |
l = nil |
| 823 |
} |
| 824 |
} |
| 825 |
if n != nil { |
| 826 |
if np != nil && nq != nil { |
| 827 |
*np += len(n.psubs) |
| 828 |
for _, qsub := range n.qsubs { |
| 829 |
*nq += len(qsub) |
| 830 |
} |
| 831 |
} |
| 832 |
if len(n.plist) > 0 || len(n.psubs) > 0 || len(n.qsubs) > 0 { |
| 833 |
return true |
| 834 |
} |
| 835 |
} |
| 836 |
if pwc != nil { |
| 837 |
if np != nil && nq != nil { |
| 838 |
*np += len(pwc.psubs) |
| 839 |
for _, qsub := range pwc.qsubs { |
| 840 |
*nq += len(qsub) |
| 841 |
} |
| 842 |
} |
| 843 |
return len(pwc.plist) > 0 || len(pwc.psubs) > 0 || len(pwc.qsubs) > 0 |
| 844 |
} |
| 845 |
return false |
| 846 |
} |
| 847 |
|
| 848 |
// lnt is used to track descent into levels for a removal for pruning. |
| 849 |
type lnt struct { |
| 850 |
l *level |
| 851 |
n *node |
| 852 |
t string |
| 853 |
} |
| 854 |
|
| 855 |
// Raw low level remove, can do batches with lock held outside. |
| 856 |
func (s *Sublist) remove(sub *subscription, shouldLock bool, doCacheUpdates bool) error { |
| 857 |
subject := string(sub.subject) |
| 858 |
|
| 859 |
if shouldLock { |
| 860 |
s.Lock() |
| 861 |
defer s.Unlock() |
| 862 |
} |
| 863 |
|
| 864 |
var sfwc, haswc bool |
| 865 |
var n *node |
| 866 |
l := s.root |
| 867 |
|
| 868 |
// Track levels for pruning |
| 869 |
var lnts [32]lnt |
| 870 |
levels := lnts[:0] |
| 871 |
|
| 872 |
for t := range strings.SplitSeq(subject, tsep) { |
| 873 |
lt := len(t) |
| 874 |
if lt == 0 || sfwc { |
| 875 |
return ErrInvalidSubject |
| 876 |
} |
| 877 |
if l == nil { |
| 878 |
return ErrNotFound |
| 879 |
} |
| 880 |
if lt > 1 { |
| 881 |
n = l.nodes[t] |
| 882 |
} else { |
| 883 |
switch t[0] { |
| 884 |
case pwc: |
| 885 |
n = l.pwc |
| 886 |
haswc = true |
| 887 |
case fwc: |
| 888 |
n = l.fwc |
| 889 |
haswc, sfwc = true, true |
| 890 |
default: |
| 891 |
n = l.nodes[t] |
| 892 |
} |
| 893 |
} |
| 894 |
if n != nil { |
| 895 |
levels = append(levels, lnt{l, n, t}) |
| 896 |
l = n.next |
| 897 |
} else { |
| 898 |
l = nil |
| 899 |
} |
| 900 |
} |
| 901 |
removed, last := s.removeFromNode(n, sub) |
| 902 |
if !removed { |
| 903 |
return ErrNotFound |
| 904 |
} |
| 905 |
|
| 906 |
s.count-- |
| 907 |
s.removes++ |
| 908 |
|
| 909 |
for i := len(levels) - 1; i >= 0; i-- { |
| 910 |
l, n, t := levels[i].l, levels[i].n, levels[i].t |
| 911 |
if n.isEmpty() { |
| 912 |
l.pruneNode(n, t) |
| 913 |
} |
| 914 |
} |
| 915 |
if doCacheUpdates { |
| 916 |
s.removeFromCache(subject) |
| 917 |
atomic.AddUint64(&s.genid, 1) |
| 918 |
} |
| 919 |
|
| 920 |
if s.notify != nil && last && !haswc && len(s.notify.remove) > 0 { |
| 921 |
s.chkForRemoveNotification(subject, string(sub.queue)) |
| 922 |
} |
| 923 |
|
| 924 |
return nil |
| 925 |
} |
| 926 |
|
| 927 |
// Remove will remove a subscription. |
| 928 |
func (s *Sublist) Remove(sub *subscription) error { |
| 929 |
return s.remove(sub, true, true) |
| 930 |
} |
| 931 |
|
| 932 |
// RemoveBatch will remove a list of subscriptions. |
| 933 |
func (s *Sublist) RemoveBatch(subs []*subscription) error { |
| 934 |
if len(subs) == 0 { |
| 935 |
return nil |
| 936 |
} |
| 937 |
|
| 938 |
s.Lock() |
| 939 |
defer s.Unlock() |
| 940 |
|
| 941 |
// TODO(dlc) - We could try to be smarter here for a client going away but the account |
| 942 |
// has a large number of subscriptions compared to this client. Quick and dirty testing |
| 943 |
// though said just disabling all the time best for now. |
| 944 |
|
| 945 |
// Turn off our cache if enabled. |
| 946 |
wasEnabled := s.cache != nil |
| 947 |
s.cache = nil |
| 948 |
// We will try to remove all subscriptions but will report the first that caused |
| 949 |
// an error. In other words, we don't bail out at the first error which would |
| 950 |
// possibly leave a bunch of subscriptions that could have been removed. |
| 951 |
var err error |
| 952 |
for _, sub := range subs { |
| 953 |
if lerr := s.remove(sub, false, false); lerr != nil && err == nil { |
| 954 |
err = lerr |
| 955 |
} |
| 956 |
} |
| 957 |
// Turn caching back on here. |
| 958 |
atomic.AddUint64(&s.genid, 1) |
| 959 |
if wasEnabled { |
| 960 |
s.cache = make(map[string]*SublistResult) |
| 961 |
} |
| 962 |
return err |
| 963 |
} |
| 964 |
|
| 965 |
// pruneNode is used to prune an empty node from the tree. |
| 966 |
func (l *level) pruneNode(n *node, t string) { |
| 967 |
if n == nil { |
| 968 |
return |
| 969 |
} |
| 970 |
if n == l.fwc { |
| 971 |
l.fwc = nil |
| 972 |
} else if n == l.pwc { |
| 973 |
l.pwc = nil |
| 974 |
} else { |
| 975 |
delete(l.nodes, t) |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
// isEmpty will test if the node has any entries. Used |
| 980 |
// in pruning. |
| 981 |
func (n *node) isEmpty() bool { |
| 982 |
if len(n.psubs) == 0 && len(n.qsubs) == 0 { |
| 983 |
if n.next == nil || n.next.numNodes() == 0 { |
| 984 |
return true |
| 985 |
} |
| 986 |
} |
| 987 |
return false |
| 988 |
} |
| 989 |
|
| 990 |
// Return the number of nodes for the given level. |
| 991 |
func (l *level) numNodes() int { |
| 992 |
if l == nil { |
| 993 |
return 0 |
| 994 |
} |
| 995 |
num := len(l.nodes) |
| 996 |
if l.pwc != nil { |
| 997 |
num++ |
| 998 |
} |
| 999 |
if l.fwc != nil { |
| 1000 |
num++ |
| 1001 |
} |
| 1002 |
return num |
| 1003 |
} |
| 1004 |
|
| 1005 |
// Remove the sub for the given node. |
| 1006 |
func (s *Sublist) removeFromNode(n *node, sub *subscription) (found, last bool) { |
| 1007 |
if n == nil { |
| 1008 |
return false, true |
| 1009 |
} |
| 1010 |
if sub.queue == nil { |
| 1011 |
_, found = n.psubs[sub] |
| 1012 |
delete(n.psubs, sub) |
| 1013 |
if found && n.plist != nil { |
| 1014 |
// This will brute force remove the plist to perform |
| 1015 |
// correct behavior. Will get re-populated on a call |
| 1016 |
// to Match as needed. |
| 1017 |
n.plist = nil |
| 1018 |
} |
| 1019 |
return found, len(n.psubs) == 0 |
| 1020 |
} |
| 1021 |
|
| 1022 |
// We have a queue group subscription here |
| 1023 |
qsub := n.qsubs[string(sub.queue)] |
| 1024 |
_, found = qsub[sub] |
| 1025 |
delete(qsub, sub) |
| 1026 |
if len(qsub) == 0 { |
| 1027 |
// This is the last queue subscription interest when len(qsub) == 0, not |
| 1028 |
// when n.qsubs is empty. |
| 1029 |
last = true |
| 1030 |
delete(n.qsubs, string(sub.queue)) |
| 1031 |
} |
| 1032 |
return found, last |
| 1033 |
} |
| 1034 |
|
| 1035 |
// Count returns the number of subscriptions. |
| 1036 |
func (s *Sublist) Count() uint32 { |
| 1037 |
s.RLock() |
| 1038 |
defer s.RUnlock() |
| 1039 |
return s.count |
| 1040 |
} |
| 1041 |
|
| 1042 |
// CacheCount returns the number of result sets in the cache. |
| 1043 |
func (s *Sublist) CacheCount() int { |
| 1044 |
s.RLock() |
| 1045 |
cc := len(s.cache) |
| 1046 |
s.RUnlock() |
| 1047 |
return cc |
| 1048 |
} |
| 1049 |
|
| 1050 |
// SublistStats are public stats for the sublist |
| 1051 |
type SublistStats struct { |
| 1052 |
NumSubs uint32 `json:"num_subscriptions"` |
| 1053 |
NumCache uint32 `json:"num_cache"` |
| 1054 |
NumInserts uint64 `json:"num_inserts"` |
| 1055 |
NumRemoves uint64 `json:"num_removes"` |
| 1056 |
NumMatches uint64 `json:"num_matches"` |
| 1057 |
CacheHitRate float64 `json:"cache_hit_rate"` |
| 1058 |
MaxFanout uint32 `json:"max_fanout"` |
| 1059 |
AvgFanout float64 `json:"avg_fanout"` |
| 1060 |
totFanout int |
| 1061 |
cacheCnt int |
| 1062 |
cacheHits uint64 |
| 1063 |
} |
| 1064 |
|
| 1065 |
func (s *SublistStats) add(stat *SublistStats) { |
| 1066 |
s.NumSubs += stat.NumSubs |
| 1067 |
s.NumCache += stat.NumCache |
| 1068 |
s.NumInserts += stat.NumInserts |
| 1069 |
s.NumRemoves += stat.NumRemoves |
| 1070 |
s.NumMatches += stat.NumMatches |
| 1071 |
s.cacheHits += stat.cacheHits |
| 1072 |
if s.MaxFanout < stat.MaxFanout { |
| 1073 |
s.MaxFanout = stat.MaxFanout |
| 1074 |
} |
| 1075 |
|
| 1076 |
// ignore slStats.AvgFanout, collect the values |
| 1077 |
// it's based on instead |
| 1078 |
s.totFanout += stat.totFanout |
| 1079 |
s.cacheCnt += stat.cacheCnt |
| 1080 |
if s.totFanout > 0 { |
| 1081 |
s.AvgFanout = float64(s.totFanout) / float64(s.cacheCnt) |
| 1082 |
} |
| 1083 |
if s.NumMatches > 0 { |
| 1084 |
s.CacheHitRate = float64(s.cacheHits) / float64(s.NumMatches) |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
// Stats will return a stats structure for the current state. |
| 1089 |
func (s *Sublist) Stats() *SublistStats { |
| 1090 |
st := &SublistStats{} |
| 1091 |
|
| 1092 |
s.RLock() |
| 1093 |
cache := s.cache |
| 1094 |
cc := len(s.cache) |
| 1095 |
st.NumSubs = s.count |
| 1096 |
st.NumInserts = s.inserts |
| 1097 |
st.NumRemoves = s.removes |
| 1098 |
s.RUnlock() |
| 1099 |
|
| 1100 |
st.NumCache = uint32(cc) |
| 1101 |
st.NumMatches = atomic.LoadUint64(&s.matches) |
| 1102 |
st.cacheHits = atomic.LoadUint64(&s.cacheHits) |
| 1103 |
if st.NumMatches > 0 { |
| 1104 |
st.CacheHitRate = float64(st.cacheHits) / float64(st.NumMatches) |
| 1105 |
} |
| 1106 |
|
| 1107 |
// whip through cache for fanout stats, this can be off if cache is full and doing evictions. |
| 1108 |
// If this is called frequently, which it should not be, this could hurt performance. |
| 1109 |
if cache != nil { |
| 1110 |
tot, max, clen := 0, 0, 0 |
| 1111 |
s.RLock() |
| 1112 |
for _, r := range s.cache { |
| 1113 |
clen++ |
| 1114 |
l := len(r.psubs) + len(r.qsubs) |
| 1115 |
tot += l |
| 1116 |
if l > max { |
| 1117 |
max = l |
| 1118 |
} |
| 1119 |
} |
| 1120 |
s.RUnlock() |
| 1121 |
st.totFanout = tot |
| 1122 |
st.cacheCnt = clen |
| 1123 |
st.MaxFanout = uint32(max) |
| 1124 |
if tot > 0 { |
| 1125 |
st.AvgFanout = float64(tot) / float64(clen) |
| 1126 |
} |
| 1127 |
} |
| 1128 |
return st |
| 1129 |
} |
| 1130 |
|
| 1131 |
// numLevels will return the maximum number of levels |
| 1132 |
// contained in the Sublist tree. |
| 1133 |
func (s *Sublist) numLevels() int { |
| 1134 |
return visitLevel(s.root, 0) |
| 1135 |
} |
| 1136 |
|
| 1137 |
// visitLevel is used to descend the Sublist tree structure |
| 1138 |
// recursively. |
| 1139 |
func visitLevel(l *level, depth int) int { |
| 1140 |
if l == nil || l.numNodes() == 0 { |
| 1141 |
return depth |
| 1142 |
} |
| 1143 |
|
| 1144 |
depth++ |
| 1145 |
maxDepth := depth |
| 1146 |
|
| 1147 |
for _, n := range l.nodes { |
| 1148 |
if n == nil { |
| 1149 |
continue |
| 1150 |
} |
| 1151 |
newDepth := visitLevel(n.next, depth) |
| 1152 |
if newDepth > maxDepth { |
| 1153 |
maxDepth = newDepth |
| 1154 |
} |
| 1155 |
} |
| 1156 |
if l.pwc != nil { |
| 1157 |
pwcDepth := visitLevel(l.pwc.next, depth) |
| 1158 |
if pwcDepth > maxDepth { |
| 1159 |
maxDepth = pwcDepth |
| 1160 |
} |
| 1161 |
} |
| 1162 |
if l.fwc != nil { |
| 1163 |
fwcDepth := visitLevel(l.fwc.next, depth) |
| 1164 |
if fwcDepth > maxDepth { |
| 1165 |
maxDepth = fwcDepth |
| 1166 |
} |
| 1167 |
} |
| 1168 |
return maxDepth |
| 1169 |
} |
| 1170 |
|
| 1171 |
// Determine if a subject has any wildcard tokens. |
| 1172 |
func subjectHasWildcard(subject string) bool { |
| 1173 |
// This one exits earlier then !subjectIsLiteral(subject) |
| 1174 |
for i, c := range subject { |
| 1175 |
if c == pwc || c == fwc { |
| 1176 |
if (i == 0 || subject[i-1] == btsep) && |
| 1177 |
(i+1 == len(subject) || subject[i+1] == btsep) { |
| 1178 |
return true |
| 1179 |
} |
| 1180 |
} |
| 1181 |
} |
| 1182 |
return false |
| 1183 |
} |
| 1184 |
|
| 1185 |
// Determine if the subject has any wildcards. Fast version, does not check for |
| 1186 |
// valid subject. Used in caching layer. |
| 1187 |
func subjectIsLiteral(subject string) bool { |
| 1188 |
for i, c := range subject { |
| 1189 |
if c == pwc || c == fwc { |
| 1190 |
if (i == 0 || subject[i-1] == btsep) && |
| 1191 |
(i+1 == len(subject) || subject[i+1] == btsep) { |
| 1192 |
return false |
| 1193 |
} |
| 1194 |
} |
| 1195 |
} |
| 1196 |
return true |
| 1197 |
} |
| 1198 |
|
| 1199 |
// IsValidPublishSubject returns true if a subject is valid and a literal, false otherwise |
| 1200 |
func IsValidPublishSubject(subject string) bool { |
| 1201 |
return IsValidSubject(subject) && subjectIsLiteral(subject) |
| 1202 |
} |
| 1203 |
|
| 1204 |
// IsValidSubject returns true if a subject is valid, false otherwise |
| 1205 |
func IsValidSubject(subject string) bool { |
| 1206 |
return isValidSubject(subject, false) |
| 1207 |
} |
| 1208 |
|
| 1209 |
func isValidSubject(subject string, checkRunes bool) bool { |
| 1210 |
if subject == _EMPTY_ { |
| 1211 |
return false |
| 1212 |
} |
| 1213 |
if checkRunes { |
| 1214 |
// Check if we have embedded nulls. |
| 1215 |
if bytes.IndexByte(stringToBytes(subject), 0) >= 0 { |
| 1216 |
return false |
| 1217 |
} |
| 1218 |
// Since casting to a string will always produce valid UTF-8, we need to look for replacement runes. |
| 1219 |
// This signals something is off or corrupt. |
| 1220 |
for _, r := range subject { |
| 1221 |
if r == utf8.RuneError { |
| 1222 |
return false |
| 1223 |
} |
| 1224 |
} |
| 1225 |
} |
| 1226 |
sfwc := false |
| 1227 |
for t := range strings.SplitSeq(subject, tsep) { |
| 1228 |
length := len(t) |
| 1229 |
if length == 0 || sfwc { |
| 1230 |
return false |
| 1231 |
} |
| 1232 |
if length > 1 { |
| 1233 |
if strings.ContainsAny(t, "\t\n\f\r ") { |
| 1234 |
return false |
| 1235 |
} |
| 1236 |
continue |
| 1237 |
} |
| 1238 |
switch t[0] { |
| 1239 |
case fwc: |
| 1240 |
sfwc = true |
| 1241 |
case ' ', '\t', '\n', '\r', '\f': |
| 1242 |
return false |
| 1243 |
} |
| 1244 |
} |
| 1245 |
return true |
| 1246 |
} |
| 1247 |
|
| 1248 |
// IsValidLiteralSubject returns true if a subject is valid and literal (no wildcards), false otherwise |
| 1249 |
func IsValidLiteralSubject(subject string) bool { |
| 1250 |
return isValidLiteralSubject(strings.SplitSeq(subject, tsep)) |
| 1251 |
} |
| 1252 |
|
| 1253 |
// isValidLiteralSubject returns true if the tokens are valid and literal (no wildcards), false otherwise |
| 1254 |
func isValidLiteralSubject(tokens iter.Seq[string]) bool { |
| 1255 |
for t := range tokens { |
| 1256 |
if len(t) == 0 { |
| 1257 |
return false |
| 1258 |
} |
| 1259 |
if len(t) > 1 { |
| 1260 |
continue |
| 1261 |
} |
| 1262 |
switch t[0] { |
| 1263 |
case pwc, fwc: |
| 1264 |
return false |
| 1265 |
} |
| 1266 |
} |
| 1267 |
return true |
| 1268 |
} |
| 1269 |
|
| 1270 |
// ValidateMapping returns nil error if the subject is a valid subject mapping destination subject |
| 1271 |
func ValidateMapping(src string, dest string) error { |
| 1272 |
if dest == _EMPTY_ { |
| 1273 |
return nil |
| 1274 |
} |
| 1275 |
sfwc := false |
| 1276 |
for t := range strings.SplitSeq(dest, tsep) { |
| 1277 |
length := len(t) |
| 1278 |
if length == 0 || sfwc { |
| 1279 |
return &mappingDestinationErr{t, ErrInvalidMappingDestinationSubject} |
| 1280 |
} |
| 1281 |
|
| 1282 |
// if it looks like it contains a mapping function, it should be a valid mapping function |
| 1283 |
if length > 4 && t[0] == '{' && t[1] == '{' && t[length-2] == '}' && t[length-1] == '}' { |
| 1284 |
if !partitionMappingFunctionRegEx.MatchString(t) && |
| 1285 |
!wildcardMappingFunctionRegEx.MatchString(t) && |
| 1286 |
!splitFromLeftMappingFunctionRegEx.MatchString(t) && |
| 1287 |
!splitFromRightMappingFunctionRegEx.MatchString(t) && |
| 1288 |
!sliceFromLeftMappingFunctionRegEx.MatchString(t) && |
| 1289 |
!sliceFromRightMappingFunctionRegEx.MatchString(t) && |
| 1290 |
!splitMappingFunctionRegEx.MatchString(t) && |
| 1291 |
!randomMappingFunctionRegEx.MatchString(t) { |
| 1292 |
return &mappingDestinationErr{t, ErrUnknownMappingDestinationFunction} |
| 1293 |
} else { |
| 1294 |
continue |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
if length == 1 && t[0] == fwc { |
| 1299 |
sfwc = true |
| 1300 |
} else if strings.ContainsAny(t, "\t\n\f\r ") { |
| 1301 |
return ErrInvalidMappingDestinationSubject |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
// Finally, verify that the transform can actually be created from the source and destination |
| 1306 |
_, err := NewSubjectTransform(src, dest) |
| 1307 |
return err |
| 1308 |
} |
| 1309 |
|
| 1310 |
// Will check tokens and report back if the have any partial or full wildcards. |
| 1311 |
func analyzeTokens(tokens []string) (hasPWC, hasFWC bool) { |
| 1312 |
for _, t := range tokens { |
| 1313 |
if lt := len(t); lt == 0 || lt > 1 { |
| 1314 |
continue |
| 1315 |
} |
| 1316 |
switch t[0] { |
| 1317 |
case pwc: |
| 1318 |
hasPWC = true |
| 1319 |
case fwc: |
| 1320 |
hasFWC = true |
| 1321 |
} |
| 1322 |
} |
| 1323 |
return |
| 1324 |
} |
| 1325 |
|
| 1326 |
// Check on a token basis if they could match. |
| 1327 |
func tokensCanMatch(t1, t2 string) bool { |
| 1328 |
if len(t1) == 0 || len(t2) == 0 { |
| 1329 |
return false |
| 1330 |
} |
| 1331 |
t1c, t2c := t1[0], t2[0] |
| 1332 |
if t1c == pwc || t2c == pwc || t1c == fwc || t2c == fwc { |
| 1333 |
return true |
| 1334 |
} |
| 1335 |
return t1 == t2 |
| 1336 |
} |
| 1337 |
|
| 1338 |
// SubjectsCollide will determine if two subjects could both match a single literal subject. |
| 1339 |
func SubjectsCollide(subj1, subj2 string) bool { |
| 1340 |
if subj1 == subj2 { |
| 1341 |
return true |
| 1342 |
} |
| 1343 |
tsa, tsb := [32]string{}, [32]string{} |
| 1344 |
toks1 := tokenizeSubjectIntoSlice(tsa[:0], subj1) |
| 1345 |
toks2 := tokenizeSubjectIntoSlice(tsb[:0], subj2) |
| 1346 |
pwc1, fwc1 := analyzeTokens(toks1) |
| 1347 |
pwc2, fwc2 := analyzeTokens(toks2) |
| 1348 |
// if both literal just string compare. |
| 1349 |
l1, l2 := !(pwc1 || fwc1), !(pwc2 || fwc2) |
| 1350 |
if l1 && l2 { |
| 1351 |
return subj1 == subj2 |
| 1352 |
} |
| 1353 |
// So one or both have wildcards. If one is literal than we can do subset matching. |
| 1354 |
if l1 && !l2 { |
| 1355 |
return isSubsetMatchTokenized(toks1, toks2) |
| 1356 |
} else if l2 && !l1 { |
| 1357 |
return isSubsetMatchTokenized(toks2, toks1) |
| 1358 |
} |
| 1359 |
// Both have wildcards. |
| 1360 |
// If they only have partials then the lengths must match. |
| 1361 |
if !fwc1 && !fwc2 && len(toks1) != len(toks2) { |
| 1362 |
return false |
| 1363 |
} |
| 1364 |
if lt1, lt2 := len(toks1), len(toks2); lt1 != lt2 { |
| 1365 |
// If the shorter one only has partials then these will not collide. |
| 1366 |
if lt1 < lt2 && !fwc1 || lt2 < lt1 && !fwc2 { |
| 1367 |
return false |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
stop := len(toks1) |
| 1372 |
if len(toks2) < stop { |
| 1373 |
stop = len(toks2) |
| 1374 |
} |
| 1375 |
|
| 1376 |
// We look for reasons to say no. |
| 1377 |
for i := 0; i < stop; i++ { |
| 1378 |
t1, t2 := toks1[i], toks2[i] |
| 1379 |
if !tokensCanMatch(t1, t2) { |
| 1380 |
return false |
| 1381 |
} |
| 1382 |
} |
| 1383 |
|
| 1384 |
return true |
| 1385 |
} |
| 1386 |
|
| 1387 |
// Returns number of tokens in the subject. |
| 1388 |
func numTokens(subject string) int { |
| 1389 |
var numTokens int |
| 1390 |
if len(subject) == 0 { |
| 1391 |
return 0 |
| 1392 |
} |
| 1393 |
for i := 0; i < len(subject); i++ { |
| 1394 |
if subject[i] == btsep { |
| 1395 |
numTokens++ |
| 1396 |
} |
| 1397 |
} |
| 1398 |
return numTokens + 1 |
| 1399 |
} |
| 1400 |
|
| 1401 |
// Fast way to return an indexed token. |
| 1402 |
// This is one based, so first token is TokenAt(subject, 1) |
| 1403 |
func tokenAt(subject string, index uint8) string { |
| 1404 |
ti, start := uint8(1), 0 |
| 1405 |
for i := 0; i < len(subject); i++ { |
| 1406 |
if subject[i] == btsep { |
| 1407 |
if ti == index { |
| 1408 |
return subject[start:i] |
| 1409 |
} |
| 1410 |
start = i + 1 |
| 1411 |
ti++ |
| 1412 |
} |
| 1413 |
} |
| 1414 |
if ti == index { |
| 1415 |
return subject[start:] |
| 1416 |
} |
| 1417 |
return _EMPTY_ |
| 1418 |
} |
| 1419 |
|
| 1420 |
// use similar to append. meaning, the updated slice will be returned |
| 1421 |
func tokenizeSubjectIntoSlice(tts []string, subject string) []string { |
| 1422 |
start := 0 |
| 1423 |
for i := 0; i < len(subject); i++ { |
| 1424 |
if subject[i] == btsep { |
| 1425 |
tts = append(tts, subject[start:i]) |
| 1426 |
start = i + 1 |
| 1427 |
} |
| 1428 |
} |
| 1429 |
tts = append(tts, subject[start:]) |
| 1430 |
return tts |
| 1431 |
} |
| 1432 |
|
| 1433 |
// SubjectMatchesFilter returns true if the subject matches the provided |
| 1434 |
// filter or false otherwise. |
| 1435 |
func SubjectMatchesFilter(subject, filter string) bool { |
| 1436 |
return subjectIsSubsetMatch(subject, filter) |
| 1437 |
} |
| 1438 |
|
| 1439 |
// Calls into the function isSubsetMatch() |
| 1440 |
func subjectIsSubsetMatch(subject, test string) bool { |
| 1441 |
tsa := [32]string{} |
| 1442 |
tts := tokenizeSubjectIntoSlice(tsa[:0], subject) |
| 1443 |
return isSubsetMatch(tts, test) |
| 1444 |
} |
| 1445 |
|
| 1446 |
// This will test a subject as an array of tokens against a test subject |
| 1447 |
// Calls into the function isSubsetMatchTokenized |
| 1448 |
func isSubsetMatch(tokens []string, test string) bool { |
| 1449 |
tsa := [32]string{} |
| 1450 |
tts := tokenizeSubjectIntoSlice(tsa[:0], test) |
| 1451 |
return isSubsetMatchTokenized(tokens, tts) |
| 1452 |
} |
| 1453 |
|
| 1454 |
// This will test a subject as an array of tokens against a test subject (also encoded as array of tokens) |
| 1455 |
// and determine if the tokens are matched. Both test subject and tokens |
| 1456 |
// may contain wildcards. So foo.* is a subset match of [">", "*.*", "foo.*"], |
| 1457 |
// but not of foo.bar, etc. |
| 1458 |
func isSubsetMatchTokenized(tokens, test []string) bool { |
| 1459 |
// Walk the target tokens |
| 1460 |
for i, t2 := range test { |
| 1461 |
if i >= len(tokens) { |
| 1462 |
return false |
| 1463 |
} |
| 1464 |
l := len(t2) |
| 1465 |
if l == 0 { |
| 1466 |
return false |
| 1467 |
} |
| 1468 |
if t2[0] == fwc && l == 1 { |
| 1469 |
return true |
| 1470 |
} |
| 1471 |
t1 := tokens[i] |
| 1472 |
|
| 1473 |
l = len(t1) |
| 1474 |
if l == 0 || t1[0] == fwc && l == 1 { |
| 1475 |
return false |
| 1476 |
} |
| 1477 |
|
| 1478 |
if t1[0] == pwc && len(t1) == 1 { |
| 1479 |
m := t2[0] == pwc && len(t2) == 1 |
| 1480 |
if !m { |
| 1481 |
return false |
| 1482 |
} |
| 1483 |
if i >= len(test) { |
| 1484 |
return true |
| 1485 |
} |
| 1486 |
continue |
| 1487 |
} |
| 1488 |
if t2[0] != pwc && strings.Compare(t1, t2) != 0 { |
| 1489 |
return false |
| 1490 |
} |
| 1491 |
} |
| 1492 |
return len(tokens) == len(test) |
| 1493 |
} |
| 1494 |
|
| 1495 |
// matchLiteral is used to test literal subjects, those that do not have any |
| 1496 |
// wildcards, with a target subject. This is used in the cache layer. |
| 1497 |
func matchLiteral(literal, subject string) bool { |
| 1498 |
li := 0 |
| 1499 |
ll := len(literal) |
| 1500 |
ls := len(subject) |
| 1501 |
for i := 0; i < ls; i++ { |
| 1502 |
if li >= ll { |
| 1503 |
return false |
| 1504 |
} |
| 1505 |
// This function has been optimized for speed. |
| 1506 |
// For instance, do not set b:=subject[i] here since |
| 1507 |
// we may bump `i` in this loop to avoid `continue` or |
| 1508 |
// skipping common test in a particular test. |
| 1509 |
// Run Benchmark_SublistMatchLiteral before making any change. |
| 1510 |
switch subject[i] { |
| 1511 |
case pwc: |
| 1512 |
// NOTE: This is not testing validity of a subject, instead ensures |
| 1513 |
// that wildcards are treated as such if they follow some basic rules, |
| 1514 |
// namely that they are a token on their own. |
| 1515 |
if i == 0 || subject[i-1] == btsep { |
| 1516 |
if i == ls-1 { |
| 1517 |
// There is no more token in the subject after this wildcard. |
| 1518 |
// Skip token in literal and expect to not find a separator. |
| 1519 |
for { |
| 1520 |
// End of literal, this is a match. |
| 1521 |
if li >= ll { |
| 1522 |
return true |
| 1523 |
} |
| 1524 |
// Presence of separator, this can't be a match. |
| 1525 |
if literal[li] == btsep { |
| 1526 |
return false |
| 1527 |
} |
| 1528 |
li++ |
| 1529 |
} |
| 1530 |
} else if subject[i+1] == btsep { |
| 1531 |
// There is another token in the subject after this wildcard. |
| 1532 |
// Skip token in literal and expect to get a separator. |
| 1533 |
for { |
| 1534 |
// We found the end of the literal before finding a separator, |
| 1535 |
// this can't be a match. |
| 1536 |
if li >= ll { |
| 1537 |
return false |
| 1538 |
} |
| 1539 |
if literal[li] == btsep { |
| 1540 |
break |
| 1541 |
} |
| 1542 |
li++ |
| 1543 |
} |
| 1544 |
// Bump `i` since we know there is a `.` following, we are |
| 1545 |
// safe. The common test below is going to check `.` with `.` |
| 1546 |
// which is good. A `continue` here is too costly. |
| 1547 |
i++ |
| 1548 |
} |
| 1549 |
} |
| 1550 |
case fwc: |
| 1551 |
// For `>` to be a wildcard, it means being the only or last character |
| 1552 |
// in the string preceded by a `.` |
| 1553 |
if (i == 0 || subject[i-1] == btsep) && i == ls-1 { |
| 1554 |
return true |
| 1555 |
} |
| 1556 |
} |
| 1557 |
if subject[i] != literal[li] { |
| 1558 |
return false |
| 1559 |
} |
| 1560 |
li++ |
| 1561 |
} |
| 1562 |
// Make sure we have processed all of the literal's chars.. |
| 1563 |
return li >= ll |
| 1564 |
} |
| 1565 |
|
| 1566 |
func addLocalSub(sub *subscription, subs *[]*subscription, includeLeafHubs bool) { |
| 1567 |
if sub != nil && sub.client != nil { |
| 1568 |
kind := sub.client.kind |
| 1569 |
if kind == CLIENT || kind == SYSTEM || kind == JETSTREAM || kind == ACCOUNT || |
| 1570 |
(includeLeafHubs && sub.client.isHubLeafNode() /* implied kind==LEAF */) { |
| 1571 |
*subs = append(*subs, sub) |
| 1572 |
} |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
func (s *Sublist) addNodeToSubs(n *node, subs *[]*subscription, includeLeafHubs bool) { |
| 1577 |
// Normal subscriptions |
| 1578 |
if n.plist != nil { |
| 1579 |
for _, sub := range n.plist { |
| 1580 |
addLocalSub(sub, subs, includeLeafHubs) |
| 1581 |
} |
| 1582 |
} else { |
| 1583 |
for sub := range n.psubs { |
| 1584 |
addLocalSub(sub, subs, includeLeafHubs) |
| 1585 |
} |
| 1586 |
} |
| 1587 |
// Queue subscriptions |
| 1588 |
for _, qr := range n.qsubs { |
| 1589 |
for sub := range qr { |
| 1590 |
addLocalSub(sub, subs, includeLeafHubs) |
| 1591 |
} |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
func (s *Sublist) collectLocalSubs(l *level, subs *[]*subscription, includeLeafHubs bool) { |
| 1596 |
for _, n := range l.nodes { |
| 1597 |
s.addNodeToSubs(n, subs, includeLeafHubs) |
| 1598 |
s.collectLocalSubs(n.next, subs, includeLeafHubs) |
| 1599 |
} |
| 1600 |
if l.pwc != nil { |
| 1601 |
s.addNodeToSubs(l.pwc, subs, includeLeafHubs) |
| 1602 |
s.collectLocalSubs(l.pwc.next, subs, includeLeafHubs) |
| 1603 |
} |
| 1604 |
if l.fwc != nil { |
| 1605 |
s.addNodeToSubs(l.fwc, subs, includeLeafHubs) |
| 1606 |
s.collectLocalSubs(l.fwc.next, subs, includeLeafHubs) |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
// Return all local client subscriptions. Use the supplied slice. |
| 1611 |
func (s *Sublist) localSubs(subs *[]*subscription, includeLeafHubs bool) { |
| 1612 |
s.RLock() |
| 1613 |
s.collectLocalSubs(s.root, subs, includeLeafHubs) |
| 1614 |
s.RUnlock() |
| 1615 |
} |
| 1616 |
|
| 1617 |
// All is used to collect all subscriptions. |
| 1618 |
func (s *Sublist) All(subs *[]*subscription) { |
| 1619 |
s.RLock() |
| 1620 |
s.collectAllSubs(s.root, subs) |
| 1621 |
s.RUnlock() |
| 1622 |
} |
| 1623 |
|
| 1624 |
func (s *Sublist) addAllNodeToSubs(n *node, subs *[]*subscription) { |
| 1625 |
// Normal subscriptions |
| 1626 |
if n.plist != nil { |
| 1627 |
*subs = append(*subs, n.plist...) |
| 1628 |
} else { |
| 1629 |
for sub := range n.psubs { |
| 1630 |
*subs = append(*subs, sub) |
| 1631 |
} |
| 1632 |
} |
| 1633 |
// Queue subscriptions |
| 1634 |
for _, qr := range n.qsubs { |
| 1635 |
for sub := range qr { |
| 1636 |
*subs = append(*subs, sub) |
| 1637 |
} |
| 1638 |
} |
| 1639 |
} |
| 1640 |
|
| 1641 |
func (s *Sublist) collectAllSubs(l *level, subs *[]*subscription) { |
| 1642 |
for _, n := range l.nodes { |
| 1643 |
s.addAllNodeToSubs(n, subs) |
| 1644 |
s.collectAllSubs(n.next, subs) |
| 1645 |
} |
| 1646 |
if l.pwc != nil { |
| 1647 |
s.addAllNodeToSubs(l.pwc, subs) |
| 1648 |
s.collectAllSubs(l.pwc.next, subs) |
| 1649 |
} |
| 1650 |
if l.fwc != nil { |
| 1651 |
s.addAllNodeToSubs(l.fwc, subs) |
| 1652 |
s.collectAllSubs(l.fwc.next, subs) |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
// For a given subject (which may contain wildcards), this call returns all |
| 1657 |
// subscriptions that would match that subject. For instance, suppose that |
| 1658 |
// the sublist contains: foo.bar, foo.bar.baz and foo.baz, ReverseMatch("foo.*") |
| 1659 |
// would return foo.bar and foo.baz. |
| 1660 |
// This is used in situations where the sublist is likely to contain only |
| 1661 |
// literals and one wants to get all the subjects that would have been a match |
| 1662 |
// to a subscription on `subject`. |
| 1663 |
func (s *Sublist) ReverseMatch(subject string) *SublistResult { |
| 1664 |
tsa := [32]string{} |
| 1665 |
tokens := tsa[:0] |
| 1666 |
start := 0 |
| 1667 |
for i := 0; i < len(subject); i++ { |
| 1668 |
if subject[i] == btsep { |
| 1669 |
tokens = append(tokens, subject[start:i]) |
| 1670 |
start = i + 1 |
| 1671 |
} |
| 1672 |
} |
| 1673 |
tokens = append(tokens, subject[start:]) |
| 1674 |
|
| 1675 |
result := &SublistResult{} |
| 1676 |
|
| 1677 |
s.RLock() |
| 1678 |
reverseMatchLevel(s.root, tokens, nil, result) |
| 1679 |
// Check for empty result. |
| 1680 |
if len(result.psubs) == 0 && len(result.qsubs) == 0 { |
| 1681 |
result = emptyResult |
| 1682 |
} |
| 1683 |
s.RUnlock() |
| 1684 |
|
| 1685 |
return result |
| 1686 |
} |
| 1687 |
|
| 1688 |
func reverseMatchLevel(l *level, toks []string, n *node, results *SublistResult) { |
| 1689 |
if l == nil { |
| 1690 |
return |
| 1691 |
} |
| 1692 |
for i, t := range toks { |
| 1693 |
if len(t) == 1 { |
| 1694 |
if t[0] == fwc { |
| 1695 |
getAllNodes(l, results) |
| 1696 |
return |
| 1697 |
} else if t[0] == pwc { |
| 1698 |
for _, n := range l.nodes { |
| 1699 |
reverseMatchLevel(n.next, toks[i+1:], n, results) |
| 1700 |
} |
| 1701 |
if l.pwc != nil { |
| 1702 |
reverseMatchLevel(l.pwc.next, toks[i+1:], n, results) |
| 1703 |
} |
| 1704 |
if l.fwc != nil { |
| 1705 |
getAllNodes(l, results) |
| 1706 |
} |
| 1707 |
return |
| 1708 |
} |
| 1709 |
} |
| 1710 |
// If the sub tree has a fwc at this position, match as well. |
| 1711 |
if l.fwc != nil { |
| 1712 |
getAllNodes(l, results) |
| 1713 |
return |
| 1714 |
} else if l.pwc != nil { |
| 1715 |
reverseMatchLevel(l.pwc.next, toks[i+1:], n, results) |
| 1716 |
} |
| 1717 |
n = l.nodes[t] |
| 1718 |
if n == nil { |
| 1719 |
break |
| 1720 |
} |
| 1721 |
l = n.next |
| 1722 |
} |
| 1723 |
if n != nil { |
| 1724 |
addNodeToResults(n, results) |
| 1725 |
} |
| 1726 |
} |
| 1727 |
|
| 1728 |
func getAllNodes(l *level, results *SublistResult) { |
| 1729 |
if l == nil { |
| 1730 |
return |
| 1731 |
} |
| 1732 |
if l.pwc != nil { |
| 1733 |
addNodeToResults(l.pwc, results) |
| 1734 |
} |
| 1735 |
if l.fwc != nil { |
| 1736 |
addNodeToResults(l.fwc, results) |
| 1737 |
} |
| 1738 |
for _, n := range l.nodes { |
| 1739 |
addNodeToResults(n, results) |
| 1740 |
getAllNodes(n.next, results) |
| 1741 |
} |
| 1742 |
} |