@@ -29,7 +29,7 @@ public static Condition HashEqual(RedisKey key, RedisValue hashField, RedisValue
29
29
public static Condition HashExists ( RedisKey key , RedisValue hashField )
30
30
{
31
31
if ( hashField . IsNull ) throw new ArgumentNullException ( nameof ( hashField ) ) ;
32
- return new ExistsCondition ( key , hashField , true ) ;
32
+ return new ExistsCondition ( key , RedisType . Hash , hashField , true ) ;
33
33
}
34
34
35
35
/// <summary>
@@ -48,23 +48,23 @@ public static Condition HashNotEqual(RedisKey key, RedisValue hashField, RedisVa
48
48
public static Condition HashNotExists ( RedisKey key , RedisValue hashField )
49
49
{
50
50
if ( hashField . IsNull ) throw new ArgumentNullException ( nameof ( hashField ) ) ;
51
- return new ExistsCondition ( key , hashField , false ) ;
51
+ return new ExistsCondition ( key , RedisType . Hash , hashField , false ) ;
52
52
}
53
53
54
54
/// <summary>
55
55
/// Enforces that the given key must exist
56
56
/// </summary>
57
57
public static Condition KeyExists ( RedisKey key )
58
58
{
59
- return new ExistsCondition ( key , RedisValue . Null , true ) ;
59
+ return new ExistsCondition ( key , RedisType . None , RedisValue . Null , true ) ;
60
60
}
61
61
62
62
/// <summary>
63
63
/// Enforces that the given key must not exist
64
64
/// </summary>
65
65
public static Condition KeyNotExists ( RedisKey key )
66
66
{
67
- return new ExistsCondition ( key , RedisValue . Null , false ) ;
67
+ return new ExistsCondition ( key , RedisType . None , RedisValue . Null , false ) ;
68
68
}
69
69
70
70
/// <summary>
@@ -213,6 +213,22 @@ public static Condition SetLengthGreaterThan(RedisKey key, long length)
213
213
return new LengthCondition ( key , RedisType . Set , - 1 , length ) ;
214
214
}
215
215
216
+ /// <summary>
217
+ /// Enforces that the given set contains a certain member
218
+ /// </summary>
219
+ public static Condition SetContains ( RedisKey key , RedisValue member )
220
+ {
221
+ return new ExistsCondition ( key , RedisType . Set , member , true ) ;
222
+ }
223
+
224
+ /// <summary>
225
+ /// Enforces that the given set does not contain a certain member
226
+ /// </summary>
227
+ public static Condition SetNotContains ( RedisKey key , RedisValue member )
228
+ {
229
+ return new ExistsCondition ( key , RedisType . Set , member , false ) ;
230
+ }
231
+
216
232
/// <summary>
217
233
/// Enforces that the given sorted set cardinality is a certain value
218
234
/// </summary>
@@ -237,6 +253,22 @@ public static Condition SortedSetLengthGreaterThan(RedisKey key, long length)
237
253
return new LengthCondition ( key , RedisType . SortedSet , - 1 , length ) ;
238
254
}
239
255
256
+ /// <summary>
257
+ /// Enforces that the given sorted set contains a certain member
258
+ /// </summary>
259
+ public static Condition SortedSetContains ( RedisKey key , RedisValue member )
260
+ {
261
+ return new ExistsCondition ( key , RedisType . SortedSet , member , true ) ;
262
+ }
263
+
264
+ /// <summary>
265
+ /// Enforces that the given sorted set does not contain a certain member
266
+ /// </summary>
267
+ public static Condition SortedSetNotContains ( RedisKey key , RedisValue member )
268
+ {
269
+ return new ExistsCondition ( key , RedisType . SortedSet , member , false ) ;
270
+ }
271
+
240
272
internal abstract void CheckCommands ( CommandMap commandMap ) ;
241
273
242
274
internal abstract IEnumerable < Message > CreateMessages ( int db , ResultBox resultBox ) ;
@@ -298,38 +330,62 @@ internal override void WriteImpl(PhysicalConnection physical)
298
330
internal class ExistsCondition : Condition
299
331
{
300
332
private readonly bool expectedResult ;
301
- private readonly RedisValue hashField ;
333
+ private readonly RedisValue expectedValue ;
302
334
private readonly RedisKey key ;
335
+ private readonly RedisType type ;
336
+ private readonly RedisCommand cmd ;
303
337
304
338
internal override Condition MapKeys ( Func < RedisKey , RedisKey > map )
305
339
{
306
- return new ExistsCondition ( map ( key ) , hashField , expectedResult ) ;
340
+ return new ExistsCondition ( map ( key ) , type , expectedValue , expectedResult ) ;
307
341
}
308
- public ExistsCondition ( RedisKey key , RedisValue hashField , bool expectedResult )
342
+ public ExistsCondition ( RedisKey key , RedisType type , RedisValue expectedValue , bool expectedResult )
309
343
{
310
344
if ( key . IsNull ) throw new ArgumentException ( "key" ) ;
311
345
this . key = key ;
312
- this . hashField = hashField ;
346
+ this . type = type ;
347
+ this . expectedValue = expectedValue ;
313
348
this . expectedResult = expectedResult ;
349
+
350
+ if ( expectedValue . IsNull ) {
351
+ cmd = RedisCommand . EXISTS ;
352
+ }
353
+ else {
354
+ switch ( type ) {
355
+ case RedisType . Hash :
356
+ cmd = RedisCommand . HEXISTS ;
357
+ break ;
358
+
359
+ case RedisType . Set :
360
+ cmd = RedisCommand . SISMEMBER ;
361
+ break ;
362
+
363
+ case RedisType . SortedSet :
364
+ cmd = RedisCommand . ZSCORE ;
365
+ break ;
366
+
367
+ default :
368
+ throw new ArgumentException ( nameof ( type ) ) ;
369
+ }
370
+ }
314
371
}
315
372
316
373
public override string ToString ( )
317
374
{
318
- return ( hashField . IsNull ? key . ToString ( ) : ( ( string ) key ) + " > " + hashField )
375
+ return ( expectedValue . IsNull ? key . ToString ( ) : ( ( string ) key ) + " " + type + " > " + expectedValue )
319
376
+ ( expectedResult ? " exists" : " does not exists" ) ;
320
377
}
321
378
322
379
internal override void CheckCommands ( CommandMap commandMap )
323
380
{
324
- commandMap . AssertAvailable ( hashField . IsNull ? RedisCommand . EXISTS : RedisCommand . HEXISTS ) ;
381
+ commandMap . AssertAvailable ( cmd ) ;
325
382
}
326
383
327
384
internal override IEnumerable < Message > CreateMessages ( int db , ResultBox resultBox )
328
385
{
329
386
yield return Message . Create ( db , CommandFlags . None , RedisCommand . WATCH , key ) ;
330
387
331
- var cmd = hashField . IsNull ? RedisCommand . EXISTS : RedisCommand . HEXISTS ;
332
- var message = ConditionProcessor . CreateMessage ( this , db , CommandFlags . None , cmd , key , hashField ) ;
388
+ var message = ConditionProcessor . CreateMessage ( this , db , CommandFlags . None , cmd , key , expectedValue ) ;
333
389
message . SetSource ( ConditionProcessor . Default , resultBox ) ;
334
390
yield return message ;
335
391
}
@@ -340,15 +396,24 @@ internal override int GetHashSlot(ServerSelectionStrategy serverSelectionStrateg
340
396
}
341
397
internal override bool TryValidate ( RawResult result , out bool value )
342
398
{
343
- bool parsed ;
344
- if ( ResultProcessor . DemandZeroOrOneProcessor . TryGet ( result , out parsed ) )
345
- {
346
- value = parsed == expectedResult ;
347
- ConnectionMultiplexer . TraceWithoutContext ( "exists: " + parsed + "; expected: " + expectedResult + "; voting: " + value ) ;
348
- return true ;
399
+ switch ( type ) {
400
+ case RedisType . SortedSet :
401
+ var parsedValue = result . AsRedisValue ( ) ;
402
+ value = ( parsedValue . IsNull != expectedResult ) ;
403
+ ConnectionMultiplexer . TraceWithoutContext ( "exists: " + parsedValue + "; expected: " + expectedResult + "; voting: " + value ) ;
404
+ return true ;
405
+
406
+ default :
407
+ bool parsed ;
408
+ if ( ResultProcessor . DemandZeroOrOneProcessor . TryGet ( result , out parsed ) )
409
+ {
410
+ value = parsed == expectedResult ;
411
+ ConnectionMultiplexer . TraceWithoutContext ( "exists: " + parsed + "; expected: " + expectedResult + "; voting: " + value ) ;
412
+ return true ;
413
+ }
414
+ value = false ;
415
+ return false ;
349
416
}
350
- value = false ;
351
- return false ;
352
417
}
353
418
}
354
419
0 commit comments