From ef5e735f3977a6e4fe553283bcdefad5b1b91736 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 12 Mar 2025 14:20:14 +0100 Subject: [PATCH] Polishing. Reformat code, add since and author tags. See #2883 Original pull request: #2918 --- .../redis/connection/ReactiveKeyCommands.java | 3 ++ .../lettuce/LettuceReactiveKeyCommands.java | 2 + .../redis/core/ReactiveRedisOperations.java | 20 +++---- .../redis/core/ReactiveRedisTemplate.java | 26 +++++----- ...ceReactiveKeyCommandsIntegrationTests.java | 8 +-- ...ReactiveRedisTemplateIntegrationTests.java | 52 ++++++++++++------- 6 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/ReactiveKeyCommands.java b/src/main/java/org/springframework/data/redis/connection/ReactiveKeyCommands.java index 249b23d41..3354cf9af 100644 --- a/src/main/java/org/springframework/data/redis/connection/ReactiveKeyCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/ReactiveKeyCommands.java @@ -42,6 +42,7 @@ import org.springframework.util.Assert; * * @author Christoph Strobl * @author Mark Paluch + * @author Dahye Anne Lee * @since 2.0 */ public interface ReactiveKeyCommands { @@ -177,12 +178,14 @@ public interface ReactiveKeyCommands { return exists(Mono.just(new KeyCommand(key))).next().map(BooleanResponse::getOutput); } + /** * Determine the number of given {@literal keys} that exist. * * @param keys must not be {@literal null} or {@literal empty}. * @return {@link Mono} emitting {@literal the number of existing keys}. * @see Redis Documentation: EXISTS + * @since 3.5 */ Mono exists(List keys); diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommands.java index 5dde44bc6..f4b7b8b91 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommands.java @@ -48,6 +48,7 @@ import org.springframework.util.ObjectUtils; /** * @author Christoph Strobl * @author Mark Paluch + * @author Dahye Anne Lee * @since 2.0 */ class LettuceReactiveKeyCommands implements ReactiveKeyCommands { @@ -346,6 +347,7 @@ class LettuceReactiveKeyCommands implements ReactiveKeyCommands { @Override public Mono exists(List keys) { + Assert.notNull(keys, "Key list must not be null"); Assert.notEmpty(keys, "Key list must not be empty"); diff --git a/src/main/java/org/springframework/data/redis/core/ReactiveRedisOperations.java b/src/main/java/org/springframework/data/redis/core/ReactiveRedisOperations.java index 81c910ca5..686277f0d 100644 --- a/src/main/java/org/springframework/data/redis/core/ReactiveRedisOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ReactiveRedisOperations.java @@ -54,6 +54,7 @@ import org.springframework.util.Assert; * * @author Mark Paluch * @author Christoph Strobl + * @author Dahye Anne Lee * @since 2.0 */ public interface ReactiveRedisOperations { @@ -243,6 +244,16 @@ public interface ReactiveRedisOperations { */ Mono hasKey(K key); + /** + * Get the number of given {@code keys} that exists. + * + * @param keys must not be {@literal null} or {@literal empty}. + * @return the number of existing keys in redis. 0 if there are no existing keys. + * @see Redis Documentation: EXISTS + * @since 3.5 + */ + Mono countExistingKeys(Collection keys); + /** * Determine the type stored at {@code key}. * @@ -422,15 +433,6 @@ public interface ReactiveRedisOperations { */ Mono getExpire(K key); - /** - * Get the number of given {@code keys} that exists. - * - * @param keys must not be {@literal null} or {@literal empty}. - * @return the number of existing keys in redis. 0 if there are no existing keys. - * @see Redis Documentation: EXISTS - */ - Mono countExistingKeys(Collection keys); - // ------------------------------------------------------------------------- // Methods dealing with Redis Lua scripts // ------------------------------------------------------------------------- diff --git a/src/main/java/org/springframework/data/redis/core/ReactiveRedisTemplate.java b/src/main/java/org/springframework/data/redis/core/ReactiveRedisTemplate.java index b6238bc3e..3741aa751 100644 --- a/src/main/java/org/springframework/data/redis/core/ReactiveRedisTemplate.java +++ b/src/main/java/org/springframework/data/redis/core/ReactiveRedisTemplate.java @@ -22,6 +22,7 @@ import java.lang.reflect.Proxy; import java.nio.ByteBuffer; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -69,6 +70,7 @@ import org.springframework.util.ClassUtils; * @author Christoph Strobl * @author Petromir Dzhunev * @author John Blum + * @author Dahye Anne Lee * @param the Redis key type against which the template works (usually a String) * @param the Redis value type against which the template works * @since 2.0 @@ -326,6 +328,14 @@ public class ReactiveRedisTemplate implements ReactiveRedisOperations connection.keyCommands().exists(rawKey(key))); } + @Override + public Mono countExistingKeys(Collection keys) { + + Assert.notNull(keys, "Keys must not be null"); + + return doCreateMono(connection -> connection.keyCommands().exists(rawKeys(keys))); + } + @Override public Mono type(K key) { @@ -506,14 +516,6 @@ public class ReactiveRedisTemplate implements ReactiveRedisOperations connection.keyCommands().move(rawKey(key), dbIndex)); } - @Override - public Mono countExistingKeys(Collection keys) { - Assert.notNull(keys, "Keys must not be null"); - - ByteBuffer[] rawKeys = rawKeys(keys); - return doCreateMono(connection -> connection.keyCommands().exists(Arrays.asList(rawKeys))); - } - // ------------------------------------------------------------------------- // Methods dealing with Redis Lua scripts // ------------------------------------------------------------------------- @@ -697,12 +699,12 @@ public class ReactiveRedisTemplate implements ReactiveRedisOperations keys) { - final ByteBuffer[] rawKeys = new ByteBuffer[keys.size()]; + private List rawKeys(Collection keys) { + + List rawKeys = new ArrayList<>(keys.size()); - int i = 0; for (K key : keys) { - rawKeys[i++] = rawKey(key); + rawKeys.add(rawKey(key)); } return rawKeys; diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommandsIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommandsIntegrationTests.java index d2bbc633d..5dd4f5684 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommandsIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommandsIntegrationTests.java @@ -50,6 +50,7 @@ import org.springframework.data.redis.test.extension.parametrized.ParameterizedR * * @author Christoph Strobl * @author Mark Paluch + * @author Dahye Anne Lee */ public class LettuceReactiveKeyCommandsIntegrationTests extends LettuceReactiveCommandsTestSupport { @@ -70,8 +71,9 @@ public class LettuceReactiveKeyCommandsIntegrationTests extends LettuceReactiveC connection.keyCommands().exists(KEY_1_BBUFFER).as(StepVerifier::create).expectNext(false).verifyComplete(); } - @ParameterizedRedisTest + @ParameterizedRedisTest // GH-2883 void existsKeyReturnsKeyCount() { + nativeCommands.set(KEY_1, "1000"); nativeCommands.set(KEY_2, "2000"); nativeCommands.set(KEY_3, "3000"); @@ -80,8 +82,8 @@ public class LettuceReactiveKeyCommandsIntegrationTests extends LettuceReactiveC .expectNext(3L).verifyComplete(); } - @ParameterizedRedisTest - void existsKeyReturnsZeroWhenKeyDoesNotExist() { + @ParameterizedRedisTest // GH-2883 + void existsKeyReturnsZeroWhenKeysDoNotExist() { connection.keyCommands().exists(List.of(KEY_1_BBUFFER, KEY_2_BBUFFER, KEY_3_BBUFFER)).as(StepVerifier::create) .expectNext(0L).verifyComplete(); } diff --git a/src/test/java/org/springframework/data/redis/core/ReactiveRedisTemplateIntegrationTests.java b/src/test/java/org/springframework/data/redis/core/ReactiveRedisTemplateIntegrationTests.java index 40830cd46..f887c1ba2 100644 --- a/src/test/java/org/springframework/data/redis/core/ReactiveRedisTemplateIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/core/ReactiveRedisTemplateIntegrationTests.java @@ -24,10 +24,16 @@ import reactor.test.StepVerifier; import java.time.Duration; import java.time.Instant; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.function.Function; import org.junit.jupiter.api.BeforeEach; + import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.redis.ObjectFactory; import org.springframework.data.redis.Person; @@ -60,6 +66,7 @@ import org.springframework.data.redis.test.extension.parametrized.ParameterizedR * * @author Mark Paluch * @author Christoph Strobl + * @author Dahye Anne Lee */ @MethodSource("testParams") public class ReactiveRedisTemplateIntegrationTests { @@ -127,6 +134,30 @@ public class ReactiveRedisTemplateIntegrationTests { redisTemplate.hasKey(key).as(StepVerifier::create).expectNext(true).verifyComplete(); } + @ParameterizedRedisTest // GH-2883 + void countExistingKeysIfValidKeyExists() { + + K key = keyFactory.instance(); + K key2 = keyFactory.instance(); + K key3 = keyFactory.instance(); + + ReactiveValueOperations ops = redisTemplate.opsForValue(); + + ops.set(key, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete(); + ops.set(key2, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete(); + ops.set(key3, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete(); + + redisTemplate.countExistingKeys(Arrays.asList(key, key2, key3)).as(StepVerifier::create).expectNext(3L) + .verifyComplete(); + } + + @ParameterizedRedisTest // GH-2883 + void countExistingKeysIfNotValidKeyExists() { + + K key = keyFactory.instance(); + redisTemplate.countExistingKeys(List.of(key)).as(StepVerifier::create).expectNext(0L).verifyComplete(); + } + @ParameterizedRedisTest // DATAREDIS-743 void scan() { @@ -580,23 +611,4 @@ public class ReactiveRedisTemplateIntegrationTests { .verify(Duration.ofSeconds(3)); } - @ParameterizedRedisTest - void countExistingKeysIfValidKeyExists() { - - K key = keyFactory.instance(); - K key2 = keyFactory.instance(); - K key3 = keyFactory.instance(); - - redisTemplate.opsForValue().set(key, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete(); - redisTemplate.opsForValue().set(key2, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete(); - redisTemplate.opsForValue().set(key3, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete(); - - redisTemplate.countExistingKeys(Arrays.asList(key, key2, key3)).as(StepVerifier::create).expectNext(3L).verifyComplete(); - } - - @ParameterizedRedisTest - void countExistingKeysIfNotValidKeyExists() { - K key = keyFactory.instance(); - redisTemplate.countExistingKeys(List.of(key)).as(StepVerifier::create).expectNext(0L).verifyComplete(); - } }