@@ -17,19 +17,18 @@ public static bool TryParse(in RedisChannel channel, in RedisValue value, out Ke
1717 // validate that it looks reasonable
1818 var span = channel . Span ;
1919
20- const int PREFIX_LEN = KeySpaceStart . Length , MIN_LEN = PREFIX_LEN + MinSuffixBytes ;
21- Debug . Assert ( KeyEventStart . Length == PREFIX_LEN ) ; // prove these are the same, DEBUG only
22-
23- if ( span . Length >= MIN_LEN )
20+ // KeySpaceStart and KeyEventStart are the same size, see KeyEventPrefix_KeySpacePrefix_Length_Matches
21+ if ( span . Length >= KeySpacePrefix . Length + MinSuffixBytes )
2422 {
25- var prefix = span . Slice ( 0 , PREFIX_LEN ) ;
23+ // check that the prefix is valid, i.e. "__keyspace@" or "__keyevent@"
24+ var prefix = span . Slice ( 0 , KeySpacePrefix . Length ) ;
2625 var hash = prefix . Hash64 ( ) ;
2726 switch ( hash )
2827 {
29- case KeySpaceStart . Hash when KeySpaceStart . Is ( hash , prefix ) :
30- case KeyEventStart . Hash when KeyEventStart . Is ( hash , prefix ) :
28+ case KeySpacePrefix . Hash when KeySpacePrefix . Is ( hash , prefix ) :
29+ case KeyEventPrefix . Hash when KeyEventPrefix . Is ( hash , prefix ) :
3130 // check that there is *something* non-empty after the prefix, with __: as the suffix (we don't verify *what*)
32- if ( span . Slice ( PREFIX_LEN ) . IndexOf ( "__:"u8 ) > 0 )
31+ if ( span . Slice ( KeySpacePrefix . Length ) . IndexOf ( "__:"u8 ) > 0 )
3332 {
3433 notification = new KeyNotification ( in channel , in value ) ;
3534 return true ;
@@ -74,8 +73,8 @@ public int Database
7473 {
7574 // prevalidated format, so we can just skip past the prefix (except for the default value)
7675 if ( _channel . IsNull ) return - 1 ;
77- var span = _channel . Span . Slice ( 11 ) ;
78- var end = span . IndexOf ( ( byte ) '_' ) ; // expecting __:
76+ var span = _channel . Span . Slice ( KeySpacePrefix . Length ) ; // also works for KeyEventPrefix
77+ var end = span . IndexOf ( ( byte ) '_' ) ; // expecting " __:foo" - we'll just stop at the underscore
7978 if ( end <= 0 ) return - 1 ;
8079
8180 span = span . Slice ( 0 , end ) ;
@@ -207,39 +206,39 @@ public KeyNotificationType Type
207206 }
208207
209208 /// <summary>
210- /// Indicates whether this notification originated from a keyspace notification, for example <c>__keyspace@0__ :mykey</c> with payload <c>set</c>.
209+ /// Indicates whether this notification originated from a keyspace notification, for example <c>__keyspace@4__ :mykey</c> with payload <c>set</c>.
211210 /// </summary>
212211 public bool IsKeySpace
213212 {
214213 get
215214 {
216215 var span = _channel . Span ;
217- return span . Length >= KeySpaceStart . Length + MinSuffixBytes && KeySpaceStart . Is ( span . Hash64 ( ) , span . Slice ( 0 , KeySpaceStart . Length ) ) ;
216+ return span . Length >= KeySpacePrefix . Length + MinSuffixBytes && KeySpacePrefix . Is ( span . Hash64 ( ) , span . Slice ( 0 , KeySpacePrefix . Length ) ) ;
218217 }
219218 }
220219
221220 /// <summary>
222- /// Indicates whether this notification originated from a keyevent notification, for example <c>__keyevent@0__ :set</c> with payload <c>mykey</c>.
221+ /// Indicates whether this notification originated from a keyevent notification, for example <c>__keyevent@4__ :set</c> with payload <c>mykey</c>.
223222 /// </summary>
224223 public bool IsKeyEvent
225224 {
226225 get
227226 {
228227 var span = _channel . Span ;
229- return span . Length >= KeyEventStart . Length + MinSuffixBytes && KeyEventStart . Is ( span . Hash64 ( ) , span . Slice ( 0 , KeyEventStart . Length ) ) ;
228+ return span . Length >= KeyEventPrefix . Length + MinSuffixBytes && KeyEventPrefix . Is ( span . Hash64 ( ) , span . Slice ( 0 , KeyEventPrefix . Length ) ) ;
230229 }
231230 }
232231}
233232
234233internal static partial class KeyNotificationChannels
235234{
236235 [ FastHash ( "__keyspace@" ) ]
237- internal static partial class KeySpaceStart
236+ internal static partial class KeySpacePrefix
238237 {
239238 }
240239
241240 [ FastHash ( "__keyevent@" ) ]
242- internal static partial class KeyEventStart
241+ internal static partial class KeyEventPrefix
243242 {
244243 }
245244}
0 commit comments