Module: ZZQ::Routing::TopicMatch

Defined in:
lib/zzq/routing/topic_match.rb

Overview

Tiny topic-filter matcher (MQTT ยง4.7). โ€˜+` matches exactly one topic level; `#` must be the last segment and matches zero or more remaining levels. Shared-subscription prefixes (`$share/<group>/<filter>`) are stripped before matching.

Superseded by a proper trie in M7; this linear matcher is fine for MVP broker traffic.

Class Method Summary collapse

Class Method Details

.match?(filter, topic) ⇒ Boolean

Parameters:

  • filter (String)

    topic filter with โ€˜+` / `#` wildcards.

  • topic (String)

    concrete topic name.

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zzq/routing/topic_match.rb', line 19

def match?(filter, topic)
  filter = strip_share(filter)
  return false if topic.start_with?("$") && filter.start_with?("+", "#")

  f_parts = filter.split("/", -1)
  t_parts = topic.split("/", -1)

  i = 0
  while i < f_parts.length
    f = f_parts[i]
    if f == "#"
      # Must be last; matches zero or more remaining levels.
      return i == f_parts.length - 1
    end
    return false if i >= t_parts.length
    return false unless f == "+" || f == t_parts[i]
    i += 1
  end
  i == t_parts.length
end

.strip_share(filter) ⇒ Object



41
42
43
44
45
46
# File 'lib/zzq/routing/topic_match.rb', line 41

def strip_share(filter)
  return filter unless filter.start_with?("$share/")
  # $share/<group>/<filter> โ€” drop first two segments.
  parts = filter.split("/", 3)
  parts.length == 3 ? parts[2] : filter
end