forked from StackExchange/StackExchange.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisChannel.cs
More file actions
250 lines (220 loc) · 9.81 KB
/
RedisChannel.cs
File metadata and controls
250 lines (220 loc) · 9.81 KB
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
using System;
using System.Text;
namespace StackExchange.Redis
{
/// <summary>
/// Represents a pub/sub channel name
/// </summary>
public struct RedisChannel : IEquatable<RedisChannel>
{
internal static readonly RedisChannel[] EmptyArray = new RedisChannel[0];
internal readonly byte[] Value;
internal readonly bool IsPatternBased;
internal readonly bool IsKeyspaceChannel;
/// <summary>
/// Indicates whether the channel-name is either null or a zero-length value
/// </summary>
public bool IsNullOrEmpty => Value == null || Value.Length == 0;
internal bool IsNull => Value == null;
/// <summary>
/// Create a new redis channel from a buffer, explicitly controlling the pattern mode
/// </summary>
/// <param name="value">The name of the channel to create.</param>
/// <param name="mode">The mode for name matching.</param>
public RedisChannel(byte[] value, PatternMode mode) : this(value, DeterminePatternBased(value, mode)) {}
/// <summary>
/// Create a new redis channel from a string, explicitly controlling the pattern mode
/// </summary>
/// <param name="value">The string name of the channel to create.</param>
/// <param name="mode">The mode for name matching.</param>
public RedisChannel(string value, PatternMode mode) : this(value == null ? null : Encoding.UTF8.GetBytes(value), mode) {}
private RedisChannel(byte[] value, bool isPatternBased)
{
Value = value;
IsPatternBased = isPatternBased;
IsKeyspaceChannel = value != null && Encoding.UTF8.GetString(value).ToLower().StartsWith("__key");
}
private static bool DeterminePatternBased(byte[] value, PatternMode mode)
{
switch (mode)
{
case PatternMode.Auto:
return value != null && Array.IndexOf(value, (byte)'*') >= 0;
case PatternMode.Literal: return false;
case PatternMode.Pattern: return true;
default:
throw new ArgumentOutOfRangeException(nameof(mode));
}
}
/// <summary>
/// Indicate whether two channel names are not equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator !=(RedisChannel x, RedisChannel y) => !(x == y);
/// <summary>
/// Indicate whether two channel names are not equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator !=(string x, RedisChannel y) => !(x == y);
/// <summary>
/// Indicate whether two channel names are not equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator !=(byte[] x, RedisChannel y) => !(x == y);
/// <summary>
/// Indicate whether two channel names are not equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator !=(RedisChannel x, string y) => !(x == y);
/// <summary>
/// Indicate whether two channel names are not equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator !=(RedisChannel x, byte[] y) => !(x == y);
/// <summary>
/// Indicate whether two channel names are equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator ==(RedisChannel x, RedisChannel y) =>
x.IsPatternBased == y.IsPatternBased && RedisValue.Equals(x.Value, y.Value);
/// <summary>
/// Indicate whether two channel names are equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator ==(string x, RedisChannel y) =>
RedisValue.Equals(x == null ? null : Encoding.UTF8.GetBytes(x), y.Value);
/// <summary>
/// Indicate whether two channel names are equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator ==(byte[] x, RedisChannel y) => RedisValue.Equals(x, y.Value);
/// <summary>
/// Indicate whether two channel names are equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator ==(RedisChannel x, string y) =>
RedisValue.Equals(x.Value, y == null ? null : Encoding.UTF8.GetBytes(y));
/// <summary>
/// Indicate whether two channel names are equal
/// </summary>
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
public static bool operator ==(RedisChannel x, byte[] y) => RedisValue.Equals(x.Value, y);
/// <summary>
/// See Object.Equals
/// </summary>
/// <param name="obj">The <see cref="RedisChannel"/> to compare to.</param>
public override bool Equals(object obj)
{
if (obj is RedisChannel rcObj)
{
return RedisValue.Equals(Value, (rcObj).Value);
}
if (obj is string sObj)
{
return RedisValue.Equals(Value, Encoding.UTF8.GetBytes(sObj));
}
if (obj is byte[] bObj)
{
return RedisValue.Equals(Value, bObj);
}
return false;
}
/// <summary>
/// Indicate whether two channel names are equal
/// </summary>
/// <param name="other">The <see cref="RedisChannel"/> to compare to.</param>
public bool Equals(RedisChannel other) => IsPatternBased == other.IsPatternBased && RedisValue.Equals(Value, other.Value);
/// <summary>
/// See Object.GetHashCode
/// </summary>
public override int GetHashCode() => RedisValue.GetHashCode(Value) + (IsPatternBased ? 1 : 0);
/// <summary>
/// Obtains a string representation of the channel name
/// </summary>
public override string ToString()
{
return ((string)this) ?? "(null)";
}
internal static bool AssertStarts(byte[] value, byte[] expected)
{
for (int i = 0; i < expected.Length; i++)
{
if (expected[i] != value[i]) return false;
}
return true;
}
internal void AssertNotNull()
{
if (IsNull) throw new ArgumentException("A null key is not valid in this context");
}
internal RedisChannel Clone() => (byte[])Value?.Clone();
/// <summary>
/// The matching pattern for this channel
/// </summary>
public enum PatternMode
{
/// <summary>
/// Will be treated as a pattern if it includes *
/// </summary>
Auto = 0,
/// <summary>
/// Never a pattern
/// </summary>
Literal = 1,
/// <summary>
/// Always a pattern
/// </summary>
Pattern = 2
}
/// <summary>
/// Create a channel name from a <see cref="string"/>.
/// </summary>
/// <param name="key">The string to get a channel from.</param>
public static implicit operator RedisChannel(string key)
{
if (key == null) return default(RedisChannel);
return new RedisChannel(Encoding.UTF8.GetBytes(key), PatternMode.Auto);
}
/// <summary>
/// Create a channel name from a <see cref="T:byte[]"/>.
/// </summary>
/// <param name="key">The byte array to get a channel from.</param>
public static implicit operator RedisChannel(byte[] key)
{
if (key == null) return default(RedisChannel);
return new RedisChannel(key, PatternMode.Auto);
}
/// <summary>
/// Obtain the channel name as a <see cref="T:byte[]"/>.
/// </summary>
/// <param name="key">The channel to get a byte[] from.</param>
public static implicit operator byte[] (RedisChannel key) => key.Value;
/// <summary>
/// Obtain the channel name as a <see cref="string"/>.
/// </summary>
/// <param name="key">The channel to get a string from.</param>
public static implicit operator string (RedisChannel key)
{
var arr = key.Value;
if (arr == null) return null;
try
{
return Encoding.UTF8.GetString(arr);
}
catch
{
return BitConverter.ToString(arr);
}
}
}
}