Polishing.

Reformat code, add since and author tags.

See #2883
Original pull request: #2918
This commit is contained in:
Mark Paluch
2025-03-12 14:20:14 +01:00
parent d5555126b3
commit ef5e735f39
6 changed files with 67 additions and 44 deletions

View File

@@ -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 <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
* @since 3.5
*/
Mono<Long> exists(List<ByteBuffer> keys);

View File

@@ -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<Long> exists(List<ByteBuffer> keys) {
Assert.notNull(keys, "Key list must not be null");
Assert.notEmpty(keys, "Key list must not be empty");

View File

@@ -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<K, V> {
@@ -243,6 +244,16 @@ public interface ReactiveRedisOperations<K, V> {
*/
Mono<Boolean> 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 <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
* @since 3.5
*/
Mono<Long> countExistingKeys(Collection<K> keys);
/**
* Determine the type stored at {@code key}.
*
@@ -422,15 +433,6 @@ public interface ReactiveRedisOperations<K, V> {
*/
Mono<Duration> 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 <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
*/
Mono<Long> countExistingKeys(Collection<K> keys);
// -------------------------------------------------------------------------
// Methods dealing with Redis Lua scripts
// -------------------------------------------------------------------------

View File

@@ -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 <K> the Redis key type against which the template works (usually a String)
* @param <V> the Redis value type against which the template works
* @since 2.0
@@ -326,6 +328,14 @@ public class ReactiveRedisTemplate<K, V> implements ReactiveRedisOperations<K, V
return doCreateMono(connection -> connection.keyCommands().exists(rawKey(key)));
}
@Override
public Mono<Long> countExistingKeys(Collection<K> keys) {
Assert.notNull(keys, "Keys must not be null");
return doCreateMono(connection -> connection.keyCommands().exists(rawKeys(keys)));
}
@Override
public Mono<DataType> type(K key) {
@@ -506,14 +516,6 @@ public class ReactiveRedisTemplate<K, V> implements ReactiveRedisOperations<K, V
return doCreateMono(connection -> connection.keyCommands().move(rawKey(key), dbIndex));
}
@Override
public Mono<Long> countExistingKeys(Collection<K> 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<K, V> implements ReactiveRedisOperations<K, V
return getSerializationContext().getKeySerializationPair().getWriter().write(key);
}
private ByteBuffer[] rawKeys(Collection<K> keys) {
final ByteBuffer[] rawKeys = new ByteBuffer[keys.size()];
private List<ByteBuffer> rawKeys(Collection<K> keys) {
List<ByteBuffer> rawKeys = new ArrayList<>(keys.size());
int i = 0;
for (K key : keys) {
rawKeys[i++] = rawKey(key);
rawKeys.add(rawKey(key));
}
return rawKeys;

View File

@@ -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();
}

View File

@@ -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<K, V> {
@@ -127,6 +134,30 @@ public class ReactiveRedisTemplateIntegrationTests<K, V> {
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<K, V> 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<K, V> {
.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();
}
}