diff --git a/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java b/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java index 737b735d6..3566610c4 100644 --- a/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java @@ -18,7 +18,7 @@ package org.springframework.data.redis.core; import java.util.Collection; import java.util.Set; -import org.springframework.data.redis.connection.RedisZSetCommands; +import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate; import org.springframework.data.redis.connection.RedisZSetCommands.Limit; import org.springframework.data.redis.connection.RedisZSetCommands.Range; import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; @@ -31,7 +31,7 @@ import org.springframework.lang.Nullable; * @author Costin Leau * @author Christoph Strobl * @author Mark Paluch - * @author wongoo + * @author Wongoo */ public interface BoundZSetOperations extends BoundKeyOperations { @@ -269,9 +269,10 @@ public interface BoundZSetOperations extends BoundKeyOperations { * @param otherKeys must not be {@literal null}. * @param destKey must not be {@literal null}. * @param aggregate must not be {@literal null}. + * @since 2.1 * @see Redis Documentation: ZUNIONSTORE */ - void unionAndStore(Collection otherKeys, K destKey, RedisZSetCommands.Aggregate aggregate); + void unionAndStore(Collection otherKeys, K destKey, Aggregate aggregate); /** * Union sorted sets at the bound key and {@code otherKeys} and store result in destination {@code destKey}. @@ -280,9 +281,10 @@ public interface BoundZSetOperations extends BoundKeyOperations { * @param weights must not be {@literal null}. * @param destKey must not be {@literal null}. * @param aggregate must not be {@literal null}. + * @since 2.1 * @see Redis Documentation: ZUNIONSTORE */ - void unionAndStore(Collection otherKeys, int[] weights, K destKey, RedisZSetCommands.Aggregate aggregate); + void unionAndStore(Collection otherKeys, int[] weights, K destKey, Aggregate aggregate); /** * Intersect sorted sets at the bound key and {@code otherKey} and store result in destination {@code destKey}. diff --git a/src/main/java/org/springframework/data/redis/core/ZSetOperations.java b/src/main/java/org/springframework/data/redis/core/ZSetOperations.java index 22ea93d58..100c9eadc 100644 --- a/src/main/java/org/springframework/data/redis/core/ZSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ZSetOperations.java @@ -18,7 +18,7 @@ package org.springframework.data.redis.core; import java.util.Collection; import java.util.Set; -import org.springframework.data.redis.connection.RedisZSetCommands; +import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate; import org.springframework.data.redis.connection.RedisZSetCommands.Limit; import org.springframework.data.redis.connection.RedisZSetCommands.Range; import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; @@ -373,10 +373,11 @@ public interface ZSetOperations { * @param destKey must not be {@literal null}. * @param aggregate must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction. + * @since 2.1 * @see Redis Documentation: ZUNIONSTORE */ @Nullable - Long unionAndStore(K key, Collection otherKeys, K destKey, RedisZSetCommands.Aggregate aggregate); + Long unionAndStore(K key, Collection otherKeys, K destKey, Aggregate aggregate); /** * Union sorted sets at {@code key} and {@code otherKeys} and store result in destination {@code destKey}. @@ -387,10 +388,11 @@ public interface ZSetOperations { * @param destKey must not be {@literal null}. * @param aggregate must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction. + * @since 2.1 * @see Redis Documentation: ZUNIONSTORE */ @Nullable - Long unionAndStore(K key, Collection otherKeys, int[] weights, K destKey, RedisZSetCommands.Aggregate aggregate); + Long unionAndStore(K key, Collection otherKeys, int[] weights, K destKey, Aggregate aggregate); /** * Intersect sorted sets at {@code key} and {@code otherKey} and store result in destination {@code destKey}. diff --git a/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java index 28a4cf96e..5ac8b2006 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java @@ -15,20 +15,16 @@ */ package org.springframework.data.redis.core; -import static org.hamcrest.core.AnyOf.*; -import static org.hamcrest.core.IsEqual.*; -import static org.hamcrest.core.IsInstanceOf.*; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.junit.Assume.*; import static org.springframework.data.redis.matcher.RedisTestMatchers.*; import java.io.IOException; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; -import java.util.List; import java.util.Set; import org.junit.After; @@ -39,7 +35,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; - import org.springframework.data.redis.ConnectionFactoryTracker; import org.springframework.data.redis.DoubleAsStringObjectFactory; import org.springframework.data.redis.DoubleObjectFactory; @@ -370,58 +365,39 @@ public class DefaultZSetOperationsTests { assertThat(count, equalTo(3)); } - @Test //DATAREDIS-746 + @Test // DATAREDIS-746 public void testZsetUnionWithAggregate() { + K key1 = keyFactory.instance(); K key2 = keyFactory.instance(); V value1 = valueFactory.instance(); V value2 = valueFactory.instance(); - V value3 = valueFactory.instance(); - V value4 = valueFactory.instance(); - Set> values1 = new HashSet<>(); - values1.add(new DefaultTypedTuple<>(value1, 1.0)); - values1.add(new DefaultTypedTuple<>(value2, 2.0)); - zSetOps.add(key1, values1); + zSetOps.add(key1, value1, 1.0); + zSetOps.add(key1, value2, 2.0); + zSetOps.add(key2, value2, 3.0); - Set> values2 = new HashSet<>(); - values2.add(new DefaultTypedTuple<>(value3, 3.0)); - zSetOps.add(key2, values2); - // union values - assertThat(zSetOps.count(key1, 0, 99), equalTo(2L)); - List otherKeys = Arrays.asList(key2); - zSetOps.unionAndStore(key1, otherKeys, key1, RedisZSetCommands.Aggregate.MIN); - assertThat(zSetOps.count(key1, 0, 99), equalTo(3L)); - assertThat(zSetOps.score(key1, value3), equalTo(3.0)); + zSetOps.unionAndStore(key1, Collections.singletonList(key2), key1, RedisZSetCommands.Aggregate.MIN); - // aggregate in MAX - K key3 = keyFactory.instance(); - Set> values3 = new HashSet<>(); - values3.add(new DefaultTypedTuple<>(value4, 4.0)); - values3.add(new DefaultTypedTuple<>(value2, 20.0)); - zSetOps.add(key3, values3); - List otherKeys3 = Arrays.asList(key3); - zSetOps.unionAndStore(key1, otherKeys3, key1, RedisZSetCommands.Aggregate.MAX); - assertThat(zSetOps.count(key1, 0, 99), equalTo(4L)); - assertThat(zSetOps.score(key1, value4), equalTo(4.0)); - assertThat(zSetOps.score(key1, value2), equalTo(20.0)); + assertThat(zSetOps.score(key1, value2), closeTo(2.0, 0.1)); + } - // aggregate in MIN with weight - K key4 = keyFactory.instance(); - Set> values1_1 = new HashSet<>(); - values1_1.add(new DefaultTypedTuple<>(value1, 4.0)); - zSetOps.add(key4, values1_1); - List otherKeys4 = Arrays.asList(key4); - int weight[] = {2, 1}; - zSetOps.unionAndStore(key1, otherKeys4, weight, key1, RedisZSetCommands.Aggregate.MIN); - assertThat(zSetOps.count(key1, 0, 99), equalTo(4L)); - assertThat(zSetOps.score(key1, value1), equalTo(2.0)); + @Test // DATAREDIS-746 + public void testZsetUnionWithAggregateWeights() { - int weight2[] = {5, 1}; - zSetOps.unionAndStore(key1, otherKeys4, weight2, key1, RedisZSetCommands.Aggregate.MIN); - assertThat(zSetOps.score(key1, value1), equalTo(4.0)); + K key1 = keyFactory.instance(); + K key2 = keyFactory.instance(); + V value1 = valueFactory.instance(); + zSetOps.add(key1, value1, 4.0); + zSetOps.add(key2, value1, 3.0); + + int weight[] = { 1, 2 }; + + zSetOps.unionAndStore(key1, Collections.singletonList(key2), weight, key1, RedisZSetCommands.Aggregate.MAX); + + assertThat(zSetOps.score(key1, value1), closeTo(6.0, 0.1)); } }