Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Andrey Shlykov
* @author Vinoth Selvaraj
*/
public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Andrey Shlykov
* @author Vinoth Selvaraj
*/
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {

Expand Down Expand Up @@ -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}.
* <p>
* 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 <a href="https://redis.io/commands/zincrby">Redis Documentation: ZINCRBY</a>
*/
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Mark Paluch
* @author Andrey Shlykov
* @author Christoph Strobl
* @author Vinoth Selvaraj
*/
public abstract class AbstractRedisZSetTestIntegration<T> extends AbstractRedisCollectionIntegrationTests<T> {

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

@Test // DATAREDIS-3256
void testIncrementScore() {
RedisZSet<T> 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));
Expand Down