File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed
main/java/org/springframework/data/redis/support/collections
test/java/org/springframework/data/redis/support/collections Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 3939 * @author Christoph Strobl
4040 * @author Mark Paluch
4141 * @author Andrey Shlykov
42+ * @author Vinoth Selvaraj
4243 */
4344public 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 ;
Original file line number Diff line number Diff line change 4141 * @author Mark Paluch
4242 * @author Christoph Strobl
4343 * @author Andrey Shlykov
44+ * @author Vinoth Selvaraj
4445 */
4546public 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.
Original file line number Diff line number Diff line change 5454 * @author Mark Paluch
5555 * @author Andrey Shlykov
5656 * @author Christoph Strobl
57+ * @author Vinoth Selvaraj
5758 */
5859public 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 ));
You can’t perform that action at this time.
0 commit comments