Commit 7c80891a authored by Nick Craver's avatar Nick Craver

Cleanup: Condition

parent 9f326234
......@@ -3,7 +3,6 @@
namespace StackExchange.Redis
{
/// <summary>
/// Describes a pre-condition used in a redis transaction
/// </summary>
......@@ -11,21 +10,26 @@ public abstract class Condition
{
internal abstract Condition MapKeys(Func<RedisKey,RedisKey> map);
private Condition() { }
private Condition() { }
/// <summary>
/// Enforces that the given hash-field must have the specified value
/// </summary>
/// Enforces that the given hash-field must have the specified value.
/// </summary>
/// <param name="key">The key of the hash to check.</param>
/// <param name="hashField">The field in the hash to check.</param>
/// <param name="value">The value that the hash field must match.</param>
public static Condition HashEqual(RedisKey key, RedisValue hashField, RedisValue value)
{
if (hashField.IsNull) throw new ArgumentNullException(nameof(hashField));
if (value.IsNull) return HashNotExists(key, hashField);
return new EqualsCondition(key, hashField, true, value);
}
}
/// <summary>
/// Enforces that the given hash-field must exist
/// </summary>
/// Enforces that the given hash-field must exist.
/// </summary>
/// <param name="key">The key of the hash to check.</param>
/// <param name="hashField">The field in the hash to check.</param>
public static Condition HashExists(RedisKey key, RedisValue hashField)
{
if (hashField.IsNull) throw new ArgumentNullException(nameof(hashField));
......@@ -33,8 +37,11 @@ public static Condition HashExists(RedisKey key, RedisValue hashField)
}
/// <summary>
/// Enforces that the given hash-field must not have the specified value
/// </summary>
/// Enforces that the given hash-field must not have the specified value.
/// </summary>
/// <param name="key">The key of the hash to check.</param>
/// <param name="hashField">The field in the hash to check.</param>
/// <param name="value">The value that the hash field must not match.</param>
public static Condition HashNotEqual(RedisKey key, RedisValue hashField, RedisValue value)
{
if (hashField.IsNull) throw new ArgumentNullException(nameof(hashField));
......@@ -43,8 +50,10 @@ public static Condition HashNotEqual(RedisKey key, RedisValue hashField, RedisVa
}
/// <summary>
/// Enforces that the given hash-field must not exist
/// </summary>
/// Enforces that the given hash-field must not exist.
/// </summary>
/// <param name="key">The key of the hash to check.</param>
/// <param name="hashField">The field in the hash that must not exist.</param>
public static Condition HashNotExists(RedisKey key, RedisValue hashField)
{
if (hashField.IsNull) throw new ArgumentNullException(nameof(hashField));
......@@ -52,56 +61,52 @@ public static Condition HashNotExists(RedisKey key, RedisValue hashField)
}
/// <summary>
/// Enforces that the given key must exist
/// </summary>
public static Condition KeyExists(RedisKey key)
{
return new ExistsCondition(key, RedisType.None, RedisValue.Null, true);
}
/// Enforces that the given key must exist.
/// </summary>
/// <param name="key">The key that must exist.</param>
public static Condition KeyExists(RedisKey key) => new ExistsCondition(key, RedisType.None, RedisValue.Null, true);
/// <summary>
/// Enforces that the given key must not exist
/// </summary>
public static Condition KeyNotExists(RedisKey key)
{
return new ExistsCondition(key, RedisType.None, RedisValue.Null, false);
}
/// </summary>
/// <param name="key">The key that must not exist.</param>
public static Condition KeyNotExists(RedisKey key) => new ExistsCondition(key, RedisType.None, RedisValue.Null, false);
/// <summary>
/// Enforces that the given list index must have the specified value
/// </summary>
public static Condition ListIndexEqual(RedisKey key, long index, RedisValue value)
{
return new ListCondition(key, index, true, value);
}
/// </summary>
/// <param name="key">The key of the list to check.</param>
/// <param name="index">The position in the list to check.</param>
/// <param name="value">The value of the list position that must match.</param>
public static Condition ListIndexEqual(RedisKey key, long index, RedisValue value) => new ListCondition(key, index, true, value);
/// <summary>
/// Enforces that the given list index must exist
/// </summary>
public static Condition ListIndexExists(RedisKey key, long index)
{
return new ListCondition(key, index, true, null);
}
/// </summary>
/// <param name="key">The key of the list to check.</param>
/// <param name="index">The position in the list that must exist.</param>
public static Condition ListIndexExists(RedisKey key, long index) => new ListCondition(key, index, true, null);
/// <summary>
/// Enforces that the given list index must not have the specified value
/// </summary>
public static Condition ListIndexNotEqual(RedisKey key, long index, RedisValue value)
{
return new ListCondition(key, index, false, value);
}
/// </summary>
/// <param name="key">The key of the list to check.</param>
/// <param name="index">The position in the list to check.</param>
/// <param name="value">The value of the list position must not match.</param>
public static Condition ListIndexNotEqual(RedisKey key, long index, RedisValue value) => new ListCondition(key, index, false, value);
/// <summary>
/// Enforces that the given list index must not exist
/// </summary>
public static Condition ListIndexNotExists(RedisKey key, long index)
{
return new ListCondition(key, index, false, null);
}
/// </summary>
/// <param name="key">The key of the list to check.</param>
/// <param name="index">The position in the list that must not exist.</param>
public static Condition ListIndexNotExists(RedisKey key, long index) => new ListCondition(key, index, false, null);
/// <summary>
/// Enforces that the given key must have the specified value
/// </summary>
/// </summary>
/// <param name="key">The key to check.</param>
/// <param name="value">The value that must match.</param>
public static Condition StringEqual(RedisKey key, RedisValue value)
{
if (value.IsNull) return KeyNotExists(key);
......@@ -110,7 +115,9 @@ public static Condition StringEqual(RedisKey key, RedisValue value)
/// <summary>
/// Enforces that the given key must not have the specified value
/// </summary>
/// </summary>
/// <param name="key">The key to check.</param>
/// <param name="value">The value that must not match.</param>
public static Condition StringNotEqual(RedisKey key, RedisValue value)
{
if (value.IsNull) return KeyExists(key);
......@@ -119,156 +126,137 @@ public static Condition StringNotEqual(RedisKey key, RedisValue value)
/// <summary>
/// Enforces that the given hash length is a certain value
/// </summary>
public static Condition HashLengthEqual(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.Hash, 0, length);
}
/// </summary>
/// <param name="key">The key of the hash to check.</param>
/// <param name="length">The length the hash must have.</param>
public static Condition HashLengthEqual(RedisKey key, long length) => new LengthCondition(key, RedisType.Hash, 0, length);
/// <summary>
/// Enforces that the given hash length is less than a certain value
/// </summary>
public static Condition HashLengthLessThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.Hash, 1, length);
}
/// </summary>
/// <param name="key">The key of the hash to check.</param>
/// <param name="length">The length the hash must be less than.</param>
public static Condition HashLengthLessThan(RedisKey key, long length) => new LengthCondition(key, RedisType.Hash, 1, length);
/// <summary>
/// Enforces that the given hash length is greater than a certain value
/// </summary>
public static Condition HashLengthGreaterThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.Hash, -1, length);
}
/// </summary>
/// <param name="key">The key of the hash to check.</param>
/// <param name="length">The length the hash must be greater than.</param>
public static Condition HashLengthGreaterThan(RedisKey key, long length) => new LengthCondition(key, RedisType.Hash, -1, length);
/// <summary>
/// Enforces that the given string length is a certain value
/// </summary>
public static Condition StringLengthEqual(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.String, 0, length);
}
/// </summary>
/// <param name="key">The key of the string to check.</param>
/// <param name="length">The length the string must be equal to.</param>
public static Condition StringLengthEqual(RedisKey key, long length) => new LengthCondition(key, RedisType.String, 0, length);
/// <summary>
/// Enforces that the given string length is less than a certain value
/// </summary>
public static Condition StringLengthLessThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.String, 1, length);
}
/// </summary>
/// <param name="key">The key of the string to check.</param>
/// <param name="length">The length the string must be less than.</param>
public static Condition StringLengthLessThan(RedisKey key, long length) => new LengthCondition(key, RedisType.String, 1, length);
/// <summary>
/// Enforces that the given string length is greater than a certain value
/// </summary>
public static Condition StringLengthGreaterThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.String, -1, length);
}
/// </summary>
/// <param name="key">The key of the string to check.</param>
/// <param name="length">The length the string must be greater than.</param>
public static Condition StringLengthGreaterThan(RedisKey key, long length) => new LengthCondition(key, RedisType.String, -1, length);
/// <summary>
/// Enforces that the given list length is a certain value
/// </summary>
public static Condition ListLengthEqual(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.List, 0, length);
}
/// </summary>
/// <param name="key">The key of the list to check.</param>
/// <param name="length">The length the list must be equal to.</param>
public static Condition ListLengthEqual(RedisKey key, long length) => new LengthCondition(key, RedisType.List, 0, length);
/// <summary>
/// Enforces that the given list length is less than a certain value
/// </summary>
public static Condition ListLengthLessThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.List, 1, length);
}
/// </summary>
/// <param name="key">The key of the list to check.</param>
/// <param name="length">The length the list must be less than.</param>
public static Condition ListLengthLessThan(RedisKey key, long length) => new LengthCondition(key, RedisType.List, 1, length);
/// <summary>
/// Enforces that the given list length is greater than a certain value
/// </summary>
public static Condition ListLengthGreaterThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.List, -1, length);
}
/// </summary>
/// <param name="key">The key of the list to check.</param>
/// <param name="length">The length the list must be greater than.</param>
public static Condition ListLengthGreaterThan(RedisKey key, long length) => new LengthCondition(key, RedisType.List, -1, length);
/// <summary>
/// Enforces that the given set cardinality is a certain value
/// </summary>
public static Condition SetLengthEqual(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.Set, 0, length);
}
/// </summary>
/// <param name="key">The key of the set to check.</param>
/// <param name="length">The length the set must be equal to.</param>
public static Condition SetLengthEqual(RedisKey key, long length) => new LengthCondition(key, RedisType.Set, 0, length);
/// <summary>
/// Enforces that the given set cardinality is less than a certain value
/// </summary>
public static Condition SetLengthLessThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.Set, 1, length);
}
/// </summary>
/// <param name="key">The key of the set to check.</param>
/// <param name="length">The length the set must be less than.</param>
public static Condition SetLengthLessThan(RedisKey key, long length) => new LengthCondition(key, RedisType.Set, 1, length);
/// <summary>
/// Enforces that the given set cardinality is greater than a certain value
/// </summary>
public static Condition SetLengthGreaterThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.Set, -1, length);
}
/// </summary>
/// <param name="key">The key of the set to check.</param>
/// <param name="length">The length the set must be greater than.</param>
public static Condition SetLengthGreaterThan(RedisKey key, long length) => new LengthCondition(key, RedisType.Set, -1, length);
/// <summary>
/// Enforces that the given set contains a certain member
/// </summary>
public static Condition SetContains(RedisKey key, RedisValue member)
{
return new ExistsCondition(key, RedisType.Set, member, true);
}
/// </summary>
/// <param name="key">The key of the set to check.</param>
/// <param name="member">The member the set must contain.</param>
public static Condition SetContains(RedisKey key, RedisValue member) => new ExistsCondition(key, RedisType.Set, member, true);
/// <summary>
/// Enforces that the given set does not contain a certain member
/// </summary>
public static Condition SetNotContains(RedisKey key, RedisValue member)
{
return new ExistsCondition(key, RedisType.Set, member, false);
}
/// </summary>
/// <param name="key">The key of the set to check.</param>
/// <param name="member">The member the set must not contain.</param>
public static Condition SetNotContains(RedisKey key, RedisValue member) => new ExistsCondition(key, RedisType.Set, member, false);
/// <summary>
/// Enforces that the given sorted set cardinality is a certain value
/// </summary>
public static Condition SortedSetLengthEqual(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.SortedSet, 0, length);
}
/// </summary>
/// <param name="key">The key of the sorted set to check.</param>
/// <param name="length">The length the sorted set must be equal to.</param>
public static Condition SortedSetLengthEqual(RedisKey key, long length) => new LengthCondition(key, RedisType.SortedSet, 0, length);
/// <summary>
/// Enforces that the given sorted set cardinality is less than a certain value
/// </summary>
public static Condition SortedSetLengthLessThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.SortedSet, 1, length);
}
/// </summary>
/// <param name="key">The key of the sorted set to check.</param>
/// <param name="length">The length the sorted set must be less than.</param>
public static Condition SortedSetLengthLessThan(RedisKey key, long length) => new LengthCondition(key, RedisType.SortedSet, 1, length);
/// <summary>
/// Enforces that the given sorted set cardinality is greater than a certain value
/// </summary>
public static Condition SortedSetLengthGreaterThan(RedisKey key, long length)
{
return new LengthCondition(key, RedisType.SortedSet, -1, length);
}
/// </summary>
/// <param name="key">The key of the sorted set to check.</param>
/// <param name="length">The length the sorted set must be greater than.</param>
public static Condition SortedSetLengthGreaterThan(RedisKey key, long length) => new LengthCondition(key, RedisType.SortedSet, -1, length);
/// <summary>
/// Enforces that the given sorted set contains a certain member
/// </summary>
public static Condition SortedSetContains(RedisKey key, RedisValue member)
{
return new ExistsCondition(key, RedisType.SortedSet, member, true);
}
/// </summary>
/// <param name="key">The key of the sorted set to check.</param>
/// <param name="member">The member the sorted set must contain.</param>
public static Condition SortedSetContains(RedisKey key, RedisValue member) => new ExistsCondition(key, RedisType.SortedSet, member, true);
/// <summary>
/// Enforces that the given sorted set does not contain a certain member
/// </summary>
public static Condition SortedSetNotContains(RedisKey key, RedisValue member)
{
return new ExistsCondition(key, RedisType.SortedSet, member, false);
}
/// </summary>
/// <param name="key">The key of the sorted set to check.</param>
/// <param name="member">The member the sorted set must not contain.</param>
public static Condition SortedSetNotContains(RedisKey key, RedisValue member) => new ExistsCondition(key, RedisType.SortedSet, member, false);
internal abstract void CheckCommands(CommandMap commandMap);
internal abstract IEnumerable<Message> CreateMessages(int db, ResultBox resultBox);
......@@ -289,8 +277,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
{
var msg = message as ConditionMessage;
var condition = msg?.Condition;
bool final;
if (condition != null && condition.TryValidate(result, out final))
if (condition != null && condition.TryValidate(result, out bool final))
{
SetResult(message, final);
return true;
......@@ -339,6 +326,7 @@ internal override Condition MapKeys(Func<RedisKey,RedisKey> map)
{
return new ExistsCondition(map(key), type, expectedValue, expectedResult);
}
public ExistsCondition(RedisKey key, RedisType type, RedisValue expectedValue, bool expectedResult)
{
if (key.IsNull) throw new ArgumentException("key");
......@@ -376,10 +364,7 @@ public override string ToString()
+ (expectedResult ? " exists" : " does not exists");
}
internal override void CheckCommands(CommandMap commandMap)
{
commandMap.AssertAvailable(cmd);
}
internal override void CheckCommands(CommandMap commandMap) => commandMap.AssertAvailable(cmd);
internal override IEnumerable<Message> CreateMessages(int db, ResultBox resultBox)
{
......@@ -390,13 +375,12 @@ internal override IEnumerable<Message> CreateMessages(int db, ResultBox resultBo
yield return message;
}
internal override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
{
return serverSelectionStrategy.HashSlot(key);
}
internal override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy) => serverSelectionStrategy.HashSlot(key);
internal override bool TryValidate(RawResult result, out bool value)
{
switch (type) {
switch (type)
{
case RedisType.SortedSet:
var parsedValue = result.AsRedisValue();
value = (parsedValue.IsNull != expectedResult);
......@@ -419,11 +403,11 @@ internal override bool TryValidate(RawResult result, out bool value)
internal class EqualsCondition : Condition
{
internal override Condition MapKeys(Func<RedisKey,RedisKey> map)
{
return new EqualsCondition(map(key), hashField, expectedEqual, expectedValue);
}
private readonly bool expectedEqual;
private readonly RedisValue hashField, expectedValue;
private readonly RedisKey key;
......@@ -462,6 +446,7 @@ internal override int GetHashSlot(ServerSelectionStrategy serverSelectionStrateg
{
return serverSelectionStrategy.HashSlot(key);
}
internal override bool TryValidate(RawResult result, out bool value)
{
switch (result.Type)
......@@ -486,6 +471,7 @@ internal override Condition MapKeys(Func<RedisKey,RedisKey> map)
{
return new ListCondition(map(key), index, expectedResult, expectedValue);
}
private readonly bool expectedResult;
private readonly long index;
private readonly RedisValue? expectedValue;
......@@ -519,10 +505,8 @@ internal sealed override IEnumerable<Message> CreateMessages(int db, ResultBox r
yield return message;
}
internal override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
{
return serverSelectionStrategy.HashSlot(key);
}
internal override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy) => serverSelectionStrategy.HashSlot(key);
internal override bool TryValidate(RawResult result, out bool value)
{
switch (result.Type)
......@@ -555,6 +539,7 @@ internal override Condition MapKeys(Func<RedisKey,RedisKey> map)
{
return new LengthCondition(map(key), type, compareToResult, expectedLength);
}
private readonly int compareToResult;
private readonly long expectedLength;
private readonly RedisKey key;
......@@ -622,6 +607,7 @@ internal override int GetHashSlot(ServerSelectionStrategy serverSelectionStrateg
{
return serverSelectionStrategy.HashSlot(key);
}
internal override bool TryValidate(RawResult result, out bool value)
{
switch (result.Type)
......@@ -639,7 +625,6 @@ internal override bool TryValidate(RawResult result, out bool value)
return false;
}
}
}
/// <summary>
......@@ -664,19 +649,14 @@ internal ConditionResult(Condition condition)
/// </summary>
public bool WasSatisfied => wasSatisfied;
internal IEnumerable<Message> CreateMessages(int db)
{
return Condition.CreateMessages(db, resultBox);
}
internal IEnumerable<Message> CreateMessages(int db) => Condition.CreateMessages(db, resultBox);
internal ResultBox<bool> GetBox() { return resultBox; }
internal bool UnwrapBox()
{
if (resultBox != null)
{
Exception ex;
bool val;
ResultBox<bool>.UnwrapAndRecycle(resultBox, false, out val, out ex);
ResultBox<bool>.UnwrapAndRecycle(resultBox, false, out bool val, out Exception ex);
resultBox = null;
wasSatisfied = ex == null && val;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment