diff --git a/RedisStudyTest/RedisSortedSetStudyTest.cs b/RedisStudyTest/RedisSortedSetStudyTest.cs index 1924be1..397d61c 100644 --- a/RedisStudyTest/RedisSortedSetStudyTest.cs +++ b/RedisStudyTest/RedisSortedSetStudyTest.cs @@ -1310,7 +1310,6 @@ namespace RedisStudyTest Assert.Equal(4, combineNum); var destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); - Assert.Equal(4, destinationMembers.Length); Assert.Equal(2, destinationMembers.FirstOrDefault(m=>m.Element=="first").Score); Assert.Equal(2, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); @@ -1321,7 +1320,6 @@ namespace RedisStudyTest Assert.Equal(4, combineNum); destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); - Assert.Equal(4, destinationMembers.Length); Assert.Equal(1, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); Assert.Equal(2, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); Assert.Equal(3, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); @@ -1389,19 +1387,250 @@ namespace RedisStudyTest [Fact] public void SortedSetCombineAndStore_Aggregate_Test() { + //准备 + RedisKey destinationKey = "RedisStudy:SortedSet:destination"; + RedisKey firstKey = "RedisStudy:SortedSet:first"; + RedisKey secondKey = "RedisStudy:SortedSet:second"; + + //删除旧数据 + redisDatabase.KeyDelete(destinationKey); + redisDatabase.KeyDelete(firstKey); + redisDatabase.KeyDelete(secondKey); + + SortedSetEntry[] firstEntries = new SortedSetEntry[] + { + new SortedSetEntry("first",1), + new SortedSetEntry("second",2), + new SortedSetEntry("third",3), + new SortedSetEntry("fourth",4), + new SortedSetEntry("sixth",6), + new SortedSetEntry("eighth",8), + }; + SortedSetEntry[] secondEntries = new SortedSetEntry[] + { + new SortedSetEntry("first",1.5), + new SortedSetEntry("second",2.5), + new SortedSetEntry("third",3.5), + new SortedSetEntry("fifth",5), + new SortedSetEntry("seventh",7), + new SortedSetEntry("ninth",9), + }; + + redisSortedSetStudy.SortedSetAdd(firstKey, firstEntries); + redisSortedSetStudy.SortedSetAdd(secondKey, secondEntries); + + //Aggregate.Sum 求和 + var combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Sum, CommandFlags.None); + Assert.Equal(9, combineNum); + + var destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(2.5, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(4.5, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(6.5, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score); + Assert.Equal(5, destinationMembers.FirstOrDefault(m => m.Element == "fifth").Score); + Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "sixth").Score); + Assert.Equal(7, destinationMembers.FirstOrDefault(m => m.Element == "seventh").Score); + Assert.Equal(8, destinationMembers.FirstOrDefault(m => m.Element == "eighth").Score); + Assert.Equal(9, destinationMembers.FirstOrDefault(m => m.Element == "ninth").Score); + + //Aggregate.Max 最大值 + combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Max, CommandFlags.None); + Assert.Equal(9, combineNum); + + destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(1.5, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(2.5, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(3.5, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score); + Assert.Equal(5, destinationMembers.FirstOrDefault(m => m.Element == "fifth").Score); + Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "sixth").Score); + Assert.Equal(7, destinationMembers.FirstOrDefault(m => m.Element == "seventh").Score); + Assert.Equal(8, destinationMembers.FirstOrDefault(m => m.Element == "eighth").Score); + Assert.Equal(9, destinationMembers.FirstOrDefault(m => m.Element == "ninth").Score); + + //Aggregate.Min 最小值 + combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Min, CommandFlags.None); + Assert.Equal(9, combineNum); + + destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(1, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(2, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(3, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score); + Assert.Equal(5, destinationMembers.FirstOrDefault(m => m.Element == "fifth").Score); + Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "sixth").Score); + Assert.Equal(7, destinationMembers.FirstOrDefault(m => m.Element == "seventh").Score); + Assert.Equal(8, destinationMembers.FirstOrDefault(m => m.Element == "eighth").Score); + Assert.Equal(9, destinationMembers.FirstOrDefault(m => m.Element == "ninth").Score); + + //清理 + redisDatabase.KeyDelete(destinationKey); + redisDatabase.KeyDelete(firstKey); + redisDatabase.KeyDelete(secondKey); } [Fact] public void SortedSetCombineAndStoreTest() { + //准备 + RedisKey destinationKey = "RedisStudy:SortedSet:destination"; + RedisKey firstKey = "RedisStudy:SortedSet:first"; + RedisKey secondKey = "RedisStudy:SortedSet:second"; + //删除旧数据 + redisDatabase.KeyDelete(destinationKey); + redisDatabase.KeyDelete(firstKey); + redisDatabase.KeyDelete(secondKey); + + SortedSetEntry[] firstEntries = new SortedSetEntry[] + { + new SortedSetEntry("first",1), + new SortedSetEntry("second",2), + new SortedSetEntry("third",3), + new SortedSetEntry("fourth",4), + new SortedSetEntry("sixth",6), + new SortedSetEntry("eighth",8), + }; + + SortedSetEntry[] secondEntries = new SortedSetEntry[] + { + new SortedSetEntry("first",1.5), + new SortedSetEntry("second",2.5), + new SortedSetEntry("third",3.5), + new SortedSetEntry("fifth",5), + new SortedSetEntry("seventh",7), + new SortedSetEntry("ninth",9), + }; + + redisSortedSetStudy.SortedSetAdd(firstKey, firstEntries); + redisSortedSetStudy.SortedSetAdd(secondKey, secondEntries); + + //1 + var combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Sum, CommandFlags.None); + Assert.Equal(9, combineNum); + + var destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(2.5, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(4.5, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(6.5, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score); + Assert.Equal(5, destinationMembers.FirstOrDefault(m => m.Element == "fifth").Score); + Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "sixth").Score); + Assert.Equal(7, destinationMembers.FirstOrDefault(m => m.Element == "seventh").Score); + Assert.Equal(8, destinationMembers.FirstOrDefault(m => m.Element == "eighth").Score); + Assert.Equal(9, destinationMembers.FirstOrDefault(m => m.Element == "ninth").Score); + + //2 + combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Max, CommandFlags.None); + Assert.Equal(9, combineNum); + + destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(1.5, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(2.5, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(3.5, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score); + Assert.Equal(5, destinationMembers.FirstOrDefault(m => m.Element == "fifth").Score); + Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "sixth").Score); + Assert.Equal(7, destinationMembers.FirstOrDefault(m => m.Element == "seventh").Score); + Assert.Equal(8, destinationMembers.FirstOrDefault(m => m.Element == "eighth").Score); + Assert.Equal(9, destinationMembers.FirstOrDefault(m => m.Element == "ninth").Score); + + //3 + combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Intersect, destinationKey, firstKey, secondKey, Aggregate.Min, CommandFlags.None); + Assert.Equal(3, combineNum); + + destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(1, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(2, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(3, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + + //清理 + redisDatabase.KeyDelete(destinationKey); + redisDatabase.KeyDelete(firstKey); + redisDatabase.KeyDelete(secondKey); } [Fact] public void SortedSetCombineAndStore_OverLoad_Test() { + //准备 + RedisKey destinationKey = "RedisStudy:SortedSet:destination"; + RedisKey firstKey = "RedisStudy:SortedSet:first"; + RedisKey secondKey = "RedisStudy:SortedSet:second"; + + //删除旧数据 + redisDatabase.KeyDelete(destinationKey); + redisDatabase.KeyDelete(firstKey); + redisDatabase.KeyDelete(secondKey); + + SortedSetEntry[] firstEntries = new SortedSetEntry[] + { + new SortedSetEntry("first",1), + new SortedSetEntry("second",2), + new SortedSetEntry("third",3), + new SortedSetEntry("fourth",4), + new SortedSetEntry("sixth",6), + new SortedSetEntry("eighth",8), + }; + SortedSetEntry[] secondEntries = new SortedSetEntry[] + { + new SortedSetEntry("first",1.5), + new SortedSetEntry("second",2.5), + new SortedSetEntry("third",3.5), + new SortedSetEntry("fifth",5), + new SortedSetEntry("seventh",7), + new SortedSetEntry("ninth",9), + }; + + redisSortedSetStudy.SortedSetAdd(firstKey, firstEntries); + redisSortedSetStudy.SortedSetAdd(secondKey, secondEntries); + + //1 + var combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, new RedisKey[] { firstKey, secondKey }, null, Aggregate.Sum, CommandFlags.None); + Assert.Equal(9, combineNum); + + var destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(2.5, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(4.5, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(6.5, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score); + Assert.Equal(5, destinationMembers.FirstOrDefault(m => m.Element == "fifth").Score); + Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "sixth").Score); + Assert.Equal(7, destinationMembers.FirstOrDefault(m => m.Element == "seventh").Score); + Assert.Equal(8, destinationMembers.FirstOrDefault(m => m.Element == "eighth").Score); + Assert.Equal(9, destinationMembers.FirstOrDefault(m => m.Element == "ninth").Score); + + //2 + combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, new RedisKey[] { firstKey, secondKey }, null, Aggregate.Max, CommandFlags.None); + Assert.Equal(9, combineNum); + + destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(1.5, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(2.5, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(3.5, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score); + Assert.Equal(5, destinationMembers.FirstOrDefault(m => m.Element == "fifth").Score); + Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "sixth").Score); + Assert.Equal(7, destinationMembers.FirstOrDefault(m => m.Element == "seventh").Score); + Assert.Equal(8, destinationMembers.FirstOrDefault(m => m.Element == "eighth").Score); + Assert.Equal(9, destinationMembers.FirstOrDefault(m => m.Element == "ninth").Score); + + //3 + combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Intersect, destinationKey, new RedisKey[] { firstKey, secondKey }, null, Aggregate.Min, CommandFlags.None); + Assert.Equal(3, combineNum); + + destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey); + Assert.Equal(1, destinationMembers.FirstOrDefault(m => m.Element == "first").Score); + Assert.Equal(2, destinationMembers.FirstOrDefault(m => m.Element == "second").Score); + Assert.Equal(3, destinationMembers.FirstOrDefault(m => m.Element == "third").Score); + + //清理 + redisDatabase.KeyDelete(destinationKey); + redisDatabase.KeyDelete(firstKey); + redisDatabase.KeyDelete(secondKey); } #endregion