Skip to content

Commit 19b8dd7

Browse files
committed
Add incrementScore to RedisZSet
Adds a method to increment the score of an element in RedisZSet. Signed-off-by: vinsguru <vino.infy@gmail.com>
1 parent 9787a5a commit 19b8dd7

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @author Christoph Strobl
4040
* @author Mark Paluch
4141
* @author Andrey Shlykov
42+
* @author Vinoth Selvaraj
4243
*/
4344
public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
4445

@@ -455,6 +456,11 @@ public Double score(Object o) {
455456
return boundZSetOps.score(o);
456457
}
457458

459+
@Override
460+
public Double incrementScore(E e, double increment) {
461+
return boundZSetOps.incrementScore(e, increment);
462+
}
463+
458464
@Override
459465
public DataType getType() {
460466
return DataType.ZSET;

src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Mark Paluch
4242
* @author Christoph Strobl
4343
* @author Andrey Shlykov
44+
* @author Vinoth Selvaraj
4445
*/
4546
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
4647

@@ -564,6 +565,18 @@ default boolean addIfAbsent(E e) {
564565
*/
565566
Double score(Object o);
566567

568+
/**
569+
* Increments the score of the given element by the specified {@code increment}.
570+
* <p>
571+
* If the element does not exist in the sorted set, it will be added with the specified increment as its initial score.
572+
*
573+
* @param e the element whose score to increment, must not be {@literal null}.
574+
* @param increment the value by which the score should be increased.
575+
* @return the new score after incrementing
576+
* @see <a href="https://redis.io/commands/zincrby">Redis Documentation: ZINCRBY</a>
577+
*/
578+
Double incrementScore(E e, double increment);
579+
567580
/**
568581
* Returns the rank (position) of the given element in the set, in ascending order. Returns null if the element is not
569582
* contained by the set.

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Mark Paluch
5555
* @author Andrey Shlykov
5656
* @author Christoph Strobl
57+
* @author Vinoth Selvaraj
5758
*/
5859
public abstract class AbstractRedisZSetTestIntegration<T> extends AbstractRedisCollectionIntegrationTests<T> {
5960

@@ -297,6 +298,29 @@ void testScore() {
297298
assertThat(zSet.score(t3)).isEqualTo(Double.valueOf(5));
298299
}
299300

301+
@Test // DATAREDIS-3256
302+
void testIncrementScore() {
303+
RedisZSet<T> set = createZSetFor("test:zset:increment");
304+
T t1 = getT();
305+
T t2 = getT();
306+
T t3 = getT(); // new member for creation test
307+
308+
set.add(t1, 3);
309+
set.add(t2, 4);
310+
311+
// existing members
312+
set.incrementScore(t1, 5);
313+
set.incrementScore(t2, -2);
314+
315+
assertThat(set.score(t1)).isEqualTo(Double.valueOf(8));
316+
assertThat(set.score(t2)).isEqualTo(Double.valueOf(2));
317+
318+
// new member (absent before)
319+
Double newScore = set.incrementScore(t3, 7);
320+
assertThat(newScore).isEqualTo(Double.valueOf(7));
321+
assertThat(set.score(t3)).isEqualTo(Double.valueOf(7));
322+
}
323+
300324
@Test
301325
void testDefaultScore() {
302326
assertThat(zSet.getDefaultScore()).isCloseTo(1, Offset.offset(0d));

0 commit comments

Comments
 (0)