Commit bed3c2bc authored by Marc Gravell's avatar Marc Gravell

Merge branch 'seniorquico-zadd-nx-xx'

parents f72956ec ec289038
...@@ -638,16 +638,16 @@ public void SortAndStore() ...@@ -638,16 +638,16 @@ public void SortAndStore()
[Test] [Test]
public void SortedSetAdd_1() public void SortedSetAdd_1()
{ {
wrapper.SortedSetAdd("key", "member", 1.23, CommandFlags.HighPriority); wrapper.SortedSetAdd("key", "member", 1.23, When.Exists, CommandFlags.HighPriority);
mock.Verify(_ => _.SortedSetAdd("prefix:key", "member", 1.23, CommandFlags.HighPriority)); mock.Verify(_ => _.SortedSetAdd("prefix:key", "member", 1.23, When.Exists, CommandFlags.HighPriority));
} }
[Test] [Test]
public void SortedSetAdd_2() public void SortedSetAdd_2()
{ {
SortedSetEntry[] values = new SortedSetEntry[0]; SortedSetEntry[] values = new SortedSetEntry[0];
wrapper.SortedSetAdd("key", values, CommandFlags.HighPriority); wrapper.SortedSetAdd("key", values, When.Exists, CommandFlags.HighPriority);
mock.Verify(_ => _.SortedSetAdd("prefix:key", values, CommandFlags.HighPriority)); mock.Verify(_ => _.SortedSetAdd("prefix:key", values, When.Exists, CommandFlags.HighPriority));
} }
[Test] [Test]
......
...@@ -13,14 +13,16 @@ public void Execute() ...@@ -13,14 +13,16 @@ public void Execute()
var db = conn.GetDatabase(0); var db = conn.GetDatabase(0);
RedisKey key = Me(); RedisKey key = Me();
db.KeyDelete(key, CommandFlags.FireAndForget); db.KeyDelete(key, CommandFlags.FireAndForget);
db.SortedSetAdd(key, "c", 3, CommandFlags.FireAndForget); db.SortedSetAdd(key, "c", 3, When.Always, CommandFlags.FireAndForget);
db.SortedSetAdd(key, db.SortedSetAdd(key,
new[] { new[] {
new SortedSetEntry("a", 1), new SortedSetEntry("a", 1),
new SortedSetEntry("b", 2), new SortedSetEntry("b", 2),
new SortedSetEntry("d", 4), new SortedSetEntry("d", 4),
new SortedSetEntry("e", 5) new SortedSetEntry("e", 5)
}, CommandFlags.FireAndForget); },
When.Always,
CommandFlags.FireAndForget);
var pairs = db.SortedSetRangeByScoreWithScores( var pairs = db.SortedSetRangeByScoreWithScores(
key, order: Order.Descending, take: 3); key, order: Order.Descending, take: 3);
Assert.AreEqual(3, pairs.Length); Assert.AreEqual(3, pairs.Length);
......
...@@ -601,16 +601,16 @@ public void SortAsync() ...@@ -601,16 +601,16 @@ public void SortAsync()
[Test] [Test]
public void SortedSetAddAsync_1() public void SortedSetAddAsync_1()
{ {
wrapper.SortedSetAddAsync("key", "member", 1.23, CommandFlags.HighPriority); wrapper.SortedSetAddAsync("key", "member", 1.23, When.Exists, CommandFlags.HighPriority);
mock.Verify(_ => _.SortedSetAddAsync("prefix:key", "member", 1.23, CommandFlags.HighPriority)); mock.Verify(_ => _.SortedSetAddAsync("prefix:key", "member", 1.23, When.Exists, CommandFlags.HighPriority));
} }
[Test] [Test]
public void SortedSetAddAsync_2() public void SortedSetAddAsync_2()
{ {
SortedSetEntry[] values = new SortedSetEntry[0]; SortedSetEntry[] values = new SortedSetEntry[0];
wrapper.SortedSetAddAsync("key", values, CommandFlags.HighPriority); wrapper.SortedSetAddAsync("key", values, When.Exists, CommandFlags.HighPriority);
mock.Verify(_ => _.SortedSetAddAsync("prefix:key", values, CommandFlags.HighPriority)); mock.Verify(_ => _.SortedSetAddAsync("prefix:key", values, When.Exists, CommandFlags.HighPriority));
} }
[Test] [Test]
......
...@@ -638,14 +638,28 @@ public interface IDatabase : IRedis, IDatabaseAsync ...@@ -638,14 +638,28 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// </summary> /// </summary>
/// <returns>True if the value was added, False if it already existed (the score is still updated)</returns> /// <returns>True if the value was added, False if it already existed (the score is still updated)</returns>
/// <remarks>http://redis.io/commands/zadd</remarks> /// <remarks>http://redis.io/commands/zadd</remarks>
bool SortedSetAdd(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None); bool SortedSetAdd(RedisKey key, RedisValue member, double score, CommandFlags flags);
/// <summary>
/// Adds the specified member with the specified score to the sorted set stored at key. If the specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
/// </summary>
/// <returns>True if the value was added, False if it already existed (the score is still updated)</returns>
/// <remarks>http://redis.io/commands/zadd</remarks>
bool SortedSetAdd(RedisKey key, RedisValue member, double score, When when = When.Always, CommandFlags flags = CommandFlags.None);
/// <summary>
/// Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
/// </summary>
/// <returns>The number of elements added to the sorted sets, not including elements already existing for which the score was updated.</returns>
/// <remarks>http://redis.io/commands/zadd</remarks>
long SortedSetAdd(RedisKey key, SortedSetEntry[] values, CommandFlags flags);
/// <summary> /// <summary>
/// Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering. /// Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
/// </summary> /// </summary>
/// <returns>The number of elements added to the sorted sets, not including elements already existing for which the score was updated.</returns> /// <returns>The number of elements added to the sorted sets, not including elements already existing for which the score was updated.</returns>
/// <remarks>http://redis.io/commands/zadd</remarks> /// <remarks>http://redis.io/commands/zadd</remarks>
long SortedSetAdd(RedisKey key, SortedSetEntry[] values, CommandFlags flags = CommandFlags.None); long SortedSetAdd(RedisKey key, SortedSetEntry[] values, When when = When.Always, CommandFlags flags = CommandFlags.None);
/// <summary> /// <summary>
/// Computes a set operation over two sorted sets, and stores the result in destination, optionally performing /// Computes a set operation over two sorted sets, and stores the result in destination, optionally performing
......
...@@ -596,14 +596,28 @@ public interface IDatabaseAsync : IRedisAsync ...@@ -596,14 +596,28 @@ public interface IDatabaseAsync : IRedisAsync
/// </summary> /// </summary>
/// <returns>True if the value was added, False if it already existed (the score is still updated)</returns> /// <returns>True if the value was added, False if it already existed (the score is still updated)</returns>
/// <remarks>http://redis.io/commands/zadd</remarks> /// <remarks>http://redis.io/commands/zadd</remarks>
Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None); Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags);
/// <summary>
/// Adds the specified member with the specified score to the sorted set stored at key. If the specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
/// </summary>
/// <returns>True if the value was added, False if it already existed (the score is still updated)</returns>
/// <remarks>http://redis.io/commands/zadd</remarks>
Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, When when = When.Always, CommandFlags flags = CommandFlags.None);
/// <summary>
/// Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
/// </summary>
/// <returns>The number of elements added to the sorted sets, not including elements already existing for which the score was updated.</returns>
/// <remarks>http://redis.io/commands/zadd</remarks>
Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, CommandFlags flags);
/// <summary> /// <summary>
/// Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering. /// Adds all the specified members with the specified scores to the sorted set stored at key. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
/// </summary> /// </summary>
/// <returns>The number of elements added to the sorted sets, not including elements already existing for which the score was updated.</returns> /// <returns>The number of elements added to the sorted sets, not including elements already existing for which the score was updated.</returns>
/// <remarks>http://redis.io/commands/zadd</remarks> /// <remarks>http://redis.io/commands/zadd</remarks>
Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, CommandFlags flags = CommandFlags.None); Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, When when = When.Always, CommandFlags flags = CommandFlags.None);
/// <summary> /// <summary>
/// Computes a set operation over two sorted sets, and stores the result in destination, optionally performing /// Computes a set operation over two sorted sets, and stores the result in destination, optionally performing
......
...@@ -415,16 +415,23 @@ public RedisValue[] Sort(RedisKey key, long skip = 0, long take = -1, Order orde ...@@ -415,16 +415,23 @@ public RedisValue[] Sort(RedisKey key, long skip = 0, long take = -1, Order orde
{ {
return Inner.Sort(ToInner(key), skip, take, order, sortType, SortByToInner(by), SortGetToInner(get), flags); return Inner.Sort(ToInner(key), skip, take, order, sortType, SortByToInner(by), SortGetToInner(get), flags);
} }
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, CommandFlags flags)
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, CommandFlags flags = CommandFlags.None)
{ {
return Inner.SortedSetAdd(ToInner(key), values, flags); return Inner.SortedSetAdd(ToInner(key), values, flags);
} }
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
return Inner.SortedSetAdd(ToInner(key), values, when, flags);
}
public bool SortedSetAdd(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None) public bool SortedSetAdd(RedisKey key, RedisValue member, double score, CommandFlags flags)
{ {
return Inner.SortedSetAdd(ToInner(key), member, score, flags); return Inner.SortedSetAdd(ToInner(key), member, score, flags);
} }
public bool SortedSetAdd(RedisKey key, RedisValue member, double score, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
return Inner.SortedSetAdd(ToInner(key), member, score, when, flags);
}
public long SortedSetCombineAndStore(SetOperation operation, RedisKey destination, RedisKey[] keys, double[] weights = null, Aggregate aggregate = Aggregate.Sum, CommandFlags flags = CommandFlags.None) public long SortedSetCombineAndStore(SetOperation operation, RedisKey destination, RedisKey[] keys, double[] weights = null, Aggregate aggregate = Aggregate.Sum, CommandFlags flags = CommandFlags.None)
{ {
......
...@@ -416,15 +416,23 @@ public Task<RedisValue[]> SortAsync(RedisKey key, long skip = 0, long take = -1, ...@@ -416,15 +416,23 @@ public Task<RedisValue[]> SortAsync(RedisKey key, long skip = 0, long take = -1,
return Inner.SortAsync(ToInner(key), skip, take, order, sortType, SortByToInner(by), SortGetToInner(get), flags); return Inner.SortAsync(ToInner(key), skip, take, order, sortType, SortByToInner(by), SortGetToInner(get), flags);
} }
public Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, CommandFlags flags = CommandFlags.None) public Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, CommandFlags flags)
{ {
return Inner.SortedSetAddAsync(ToInner(key), values, flags); return Inner.SortedSetAddAsync(ToInner(key), values, flags);
} }
public Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
return Inner.SortedSetAddAsync(ToInner(key), values, when, flags);
}
public Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None) public Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags)
{ {
return Inner.SortedSetAddAsync(ToInner(key), member, score, flags); return Inner.SortedSetAddAsync(ToInner(key), member, score, flags);
} }
public Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
return Inner.SortedSetAddAsync(ToInner(key), member, score, when, flags);
}
public Task<long> SortedSetCombineAndStoreAsync(SetOperation operation, RedisKey destination, RedisKey[] keys, double[] weights = null, Aggregate aggregate = Aggregate.Sum, CommandFlags flags = CommandFlags.None) public Task<long> SortedSetCombineAndStoreAsync(SetOperation operation, RedisKey destination, RedisKey[] keys, double[] weights = null, Aggregate aggregate = Aggregate.Sum, CommandFlags flags = CommandFlags.None)
{ {
......
...@@ -1145,28 +1145,45 @@ public Task<RedisValue[]> SortAsync(RedisKey key, long skip = 0, long take = -1, ...@@ -1145,28 +1145,45 @@ public Task<RedisValue[]> SortAsync(RedisKey key, long skip = 0, long take = -1,
var msg = GetSortedSetAddMessage(default(RedisKey), key, skip, take, order, sortType, by, get, flags); var msg = GetSortedSetAddMessage(default(RedisKey), key, skip, take, order, sortType, by, get, flags);
return ExecuteAsync(msg, ResultProcessor.RedisValueArray); return ExecuteAsync(msg, ResultProcessor.RedisValueArray);
} }
public bool SortedSetAdd(RedisKey key, RedisValue member, double score, CommandFlags flags)
public bool SortedSetAdd(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None)
{ {
var msg = Message.Create(Database, flags, RedisCommand.ZADD, key, score, member); var msg = GetSortedSetAddMessage(key, member, score, When.Always, flags);
return ExecuteSync(msg, ResultProcessor.Boolean); return ExecuteSync(msg, ResultProcessor.Boolean);
} }
public bool SortedSetAdd(RedisKey key, RedisValue member, double score, When when = When.Always, CommandFlags flags = CommandFlags.None)
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, CommandFlags flags = CommandFlags.None)
{ {
var msg = GetSortedSetAddMessage(key, values, flags); var msg = GetSortedSetAddMessage(key, member, score, when, flags);
return ExecuteSync(msg, ResultProcessor.Boolean);
}
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, CommandFlags flags)
{
var msg = GetSortedSetAddMessage(key, values, When.Always, flags);
return ExecuteSync(msg, ResultProcessor.Int64);
}
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
var msg = GetSortedSetAddMessage(key, values, when, flags);
return ExecuteSync(msg, ResultProcessor.Int64); return ExecuteSync(msg, ResultProcessor.Int64);
} }
public Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags = CommandFlags.None) public Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, CommandFlags flags)
{ {
var msg = Message.Create(Database, flags, RedisCommand.ZADD, key, score, member); var msg = GetSortedSetAddMessage(key, member, score, When.Always, flags);
return ExecuteAsync(msg, ResultProcessor.Boolean); return ExecuteAsync(msg, ResultProcessor.Boolean);
} }
public Task<bool> SortedSetAddAsync(RedisKey key, RedisValue member, double score, When when = When.Always, CommandFlags flags = CommandFlags.None)
public Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, CommandFlags flags = CommandFlags.None) {
var msg = GetSortedSetAddMessage(key, member, score, when, flags);
return ExecuteAsync(msg, ResultProcessor.Boolean);
}
public Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, CommandFlags flags)
{
var msg = GetSortedSetAddMessage(key, values, When.Always, flags);
return ExecuteAsync(msg, ResultProcessor.Int64);
}
public Task<long> SortedSetAddAsync(RedisKey key, SortedSetEntry[] values, When when = When.Always, CommandFlags flags = CommandFlags.None)
{ {
var msg = GetSortedSetAddMessage(key, values, flags); var msg = GetSortedSetAddMessage(key, values, when, flags);
return ExecuteAsync(msg, ResultProcessor.Int64); return ExecuteAsync(msg, ResultProcessor.Int64);
} }
...@@ -1741,22 +1758,45 @@ Message GetRestoreMessage(RedisKey key, byte[] value, TimeSpan? expiry, CommandF ...@@ -1741,22 +1758,45 @@ Message GetRestoreMessage(RedisKey key, byte[] value, TimeSpan? expiry, CommandF
return Message.Create(Database, flags, RedisCommand.RESTORE, key, pttl, value); return Message.Create(Database, flags, RedisCommand.RESTORE, key, pttl, value);
} }
private Message GetSortedSetAddMessage(RedisKey key, SortedSetEntry[] values, CommandFlags flags) private Message GetSortedSetAddMessage(RedisKey key, RedisValue member, double score, When when, CommandFlags flags)
{ {
WhenAlwaysOrExistsOrNotExists(when);
switch (when)
{
case When.Always: return Message.Create(Database, flags, RedisCommand.ZADD, key, score, member);
case When.NotExists: return Message.Create(Database, flags, RedisCommand.ZADD, key, RedisLiterals.NX, score, member);
case When.Exists: return Message.Create(Database, flags, RedisCommand.ZADD, key, RedisLiterals.XX, score, member);
default: throw new ArgumentOutOfRangeException(nameof(when));
}
}
private Message GetSortedSetAddMessage(RedisKey key, SortedSetEntry[] values, When when, CommandFlags flags)
{
WhenAlwaysOrExistsOrNotExists(when);
if (values == null) throw new ArgumentNullException(nameof(values)); if (values == null) throw new ArgumentNullException(nameof(values));
switch (values.Length) switch (values.Length)
{ {
case 0: return null; case 0: return null;
case 1: case 1:
return Message.Create(Database, flags, RedisCommand.ZADD, key, return GetSortedSetAddMessage(key, values[0].element, values[0].score, when, flags);
values[0].score, values[0].element);
case 2:
return Message.Create(Database, flags, RedisCommand.ZADD, key,
values[0].score, values[0].element,
values[1].score, values[1].element);
default: default:
var arr = new RedisValue[values.Length * 2]; RedisValue[] arr;
int index = 0; int index = 0;
switch (when)
{
case When.Always:
arr = new RedisValue[values.Length * 2];
break;
case When.NotExists:
arr = new RedisValue[values.Length * 2 + 1];
arr[index++] = RedisLiterals.NX;
break;
case When.Exists:
arr = new RedisValue[values.Length * 2 + 1];
arr[index++] = RedisLiterals.XX;
break;
default: throw new ArgumentOutOfRangeException(nameof(when));
}
for (int i = 0; i < values.Length; i++) for (int i = 0; i < values.Length; i++)
{ {
arr[index++] = values[i].score; arr[index++] = values[i].score;
......
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