11package pkg
22
33import (
4- "fmt "
4+ "errors "
55 "sort"
66 "strings"
77 "sync"
88)
99
1010var (
11- ErrInvalidPattern = fmt . Errorf ("invalid pattern" )
12- ErrInvalidSplatPattern = fmt . Errorf ( "splat patterns must end with * " )
13- ErrItemAlreadyExist = fmt . Errorf ( "item already exist " )
11+ ErrInvalidPattern = errors . New ("invalid pattern" )
12+ ErrItemAlreadyExist = errors . New ( "item already exist " )
13+ ErrInvalidSplatPattern = errors . New ( "splat patterns must end with * " )
1414)
1515
1616// WildcardStore utility to persist and search items using wildcards. Used for channels, topics and events
1717//
1818// IMPORTANT: Items should only persist during system startup.
1919type WildcardStore [T any ] struct {
2020 mutex sync.Mutex
21+ all map [string ]T // all elements, by key
2122 exactly map [string ]T // exactly match (Ex. /room:lobby)
2223 wildcard []* wildcardEntry [T ] // wildcard match (Ex. /room:*)
2324}
@@ -28,8 +29,11 @@ type wildcardEntry[T any] struct {
2829}
2930
3031// Match returns the exactly value corresponding to the first occurrence of the keyPattern that matches the given key
31- func (s * WildcardStore [T ]) MatchExactly (key string ) (out T ) {
32- if item , exist := s .exactly [key ]; exist {
32+ func (s * WildcardStore [T ]) Get (key string ) (out T ) {
33+ if s .all == nil {
34+ return
35+ }
36+ if item , exist := s .all [key ]; exist {
3337 out = item
3438 return
3539 }
@@ -39,9 +43,11 @@ func (s *WildcardStore[T]) MatchExactly(key string) (out T) {
3943
4044// Match returns the value corresponding to the first occurrence of the keyPattern that matches the given key
4145func (s * WildcardStore [T ]) Match (key string ) (out T ) {
42- if item , exist := s .exactly [key ]; exist {
43- out = item
44- return
46+ if s .exactly != nil {
47+ if item , exist := s .exactly [key ]; exist {
48+ out = item
49+ return
50+ }
4551 }
4652
4753 for _ , entry := range s .wildcard {
@@ -60,8 +66,10 @@ func (s *WildcardStore[T]) Match(key string) (out T) {
6066// MatchAll returns all existing values that match the given key
6167func (s * WildcardStore [T ]) MatchAll (key string ) []T {
6268 var items []T
63- if item , exist := s .exactly [key ]; exist {
64- items = append (items , item )
69+ if s .exactly != nil {
70+ if item , exist := s .exactly [key ]; exist {
71+ items = append (items , item )
72+ }
6573 }
6674
6775 for _ , entry := range s .wildcard {
@@ -78,6 +86,18 @@ func (s *WildcardStore[T]) MatchAll(key string) []T {
7886
7987func (s * WildcardStore [T ]) Insert (keyPattern string , value T ) error {
8088
89+ if s .exactly == nil {
90+ s .exactly = map [string ]T {}
91+ }
92+
93+ if s .all == nil {
94+ s .all = map [string ]T {}
95+ }
96+
97+ if _ , exist := s .all [keyPattern ]; exist {
98+ return ErrItemAlreadyExist
99+ }
100+
81101 keyPattern = strings .TrimSpace (keyPattern )
82102
83103 if keyPattern == "" {
@@ -107,17 +127,11 @@ func (s *WildcardStore[T]) Insert(keyPattern string, value T) error {
107127 })
108128
109129 s .wildcard = wildcard
130+ s .all [keyPattern ] = value
110131 return nil
111132 }
112133
113- if s .exactly == nil {
114- s .exactly = map [string ]T {}
115- }
116-
117- if _ , exist := s .exactly [keyPattern ]; exist {
118- return ErrItemAlreadyExist
119- }
120-
134+ s .all [keyPattern ] = value
121135 s .exactly [keyPattern ] = value
122136
123137 return nil
0 commit comments