diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java index 63a0e46895..650dae1eb7 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java @@ -39,6 +39,7 @@ * @author Christoph Strobl * @author Mark Paluch * @author Andrey Shlykov + * @author Vinoth Selvaraj */ public class DefaultRedisZSet extends AbstractRedisCollection implements RedisZSet { @@ -455,6 +456,11 @@ public Double score(Object o) { return boundZSetOps.score(o); } + @Override + public Double incrementScore(E e, double increment) { + return boundZSetOps.incrementScore(e, increment); + } + @Override public DataType getType() { return DataType.ZSET; diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java b/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java index 02d5fe98e2..837bc277ca 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java @@ -41,6 +41,7 @@ * @author Mark Paluch * @author Christoph Strobl * @author Andrey Shlykov + * @author Vinoth Selvaraj */ public interface RedisZSet extends RedisCollection, Set { @@ -564,6 +565,18 @@ default boolean addIfAbsent(E e) { */ Double score(Object o); + /** + * Increments the score of the given element by the specified {@code increment}. + *

+ * If the element does not exist in the sorted set, it will be added with the specified increment as its initial score. + * + * @param e the element whose score to increment, must not be {@literal null}. + * @param increment the value by which the score should be increased. + * @return the new score after incrementing + * @see Redis Documentation: ZINCRBY + */ + Double incrementScore(E e, double increment); + /** * Returns the rank (position) of the given element in the set, in ascending order. Returns null if the element is not * contained by the set. diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java index 541989ebd9..9e319a0273 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java @@ -54,6 +54,7 @@ * @author Mark Paluch * @author Andrey Shlykov * @author Christoph Strobl + * @author Vinoth Selvaraj */ public abstract class AbstractRedisZSetTestIntegration extends AbstractRedisCollectionIntegrationTests { @@ -297,6 +298,29 @@ void testScore() { assertThat(zSet.score(t3)).isEqualTo(Double.valueOf(5)); } + @Test // DATAREDIS-3256 + void testIncrementScore() { + RedisZSet set = createZSetFor("test:zset:increment"); + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); // new member for creation test + + set.add(t1, 3); + set.add(t2, 4); + + // existing members + set.incrementScore(t1, 5); + set.incrementScore(t2, -2); + + assertThat(set.score(t1)).isEqualTo(Double.valueOf(8)); + assertThat(set.score(t2)).isEqualTo(Double.valueOf(2)); + + // new member (absent before) + Double newScore = set.incrementScore(t3, 7); + assertThat(newScore).isEqualTo(Double.valueOf(7)); + assertThat(set.score(t3)).isEqualTo(Double.valueOf(7)); + } + @Test void testDefaultScore() { assertThat(zSet.getDefaultScore()).isCloseTo(1, Offset.offset(0d));