Skip to content

Commit ef5e735

Browse files
committed
Polishing.
Reformat code, add since and author tags. See #2883 Original pull request: #2918
1 parent d555512 commit ef5e735

File tree

6 files changed

+67
-44
lines changed

6 files changed

+67
-44
lines changed

src/main/java/org/springframework/data/redis/connection/ReactiveKeyCommands.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Christoph Strobl
4444
* @author Mark Paluch
45+
* @author Dahye Anne Lee
4546
* @since 2.0
4647
*/
4748
public interface ReactiveKeyCommands {
@@ -177,12 +178,14 @@ default Mono<Boolean> exists(ByteBuffer key) {
177178

178179
return exists(Mono.just(new KeyCommand(key))).next().map(BooleanResponse::getOutput);
179180
}
181+
180182
/**
181183
* Determine the number of given {@literal keys} that exist.
182184
*
183185
* @param keys must not be {@literal null} or {@literal empty}.
184186
* @return {@link Mono} emitting {@literal the number of existing keys}.
185187
* @see <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
188+
* @since 3.5
186189
*/
187190
Mono<Long> exists(List<ByteBuffer> keys);
188191

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommands.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
/**
4949
* @author Christoph Strobl
5050
* @author Mark Paluch
51+
* @author Dahye Anne Lee
5152
* @since 2.0
5253
*/
5354
class LettuceReactiveKeyCommands implements ReactiveKeyCommands {
@@ -346,6 +347,7 @@ public Mono<Long> refcount(ByteBuffer key) {
346347

347348
@Override
348349
public Mono<Long> exists(List<ByteBuffer> keys) {
350+
349351
Assert.notNull(keys, "Key list must not be null");
350352
Assert.notEmpty(keys, "Key list must not be empty");
351353

src/main/java/org/springframework/data/redis/core/ReactiveRedisOperations.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
*
5555
* @author Mark Paluch
5656
* @author Christoph Strobl
57+
* @author Dahye Anne Lee
5758
* @since 2.0
5859
*/
5960
public interface ReactiveRedisOperations<K, V> {
@@ -243,6 +244,16 @@ default Mono<Flux<? extends Message<String, V>>> listenToPatternLater(String...
243244
*/
244245
Mono<Boolean> hasKey(K key);
245246

247+
/**
248+
* Get the number of given {@code keys} that exists.
249+
*
250+
* @param keys must not be {@literal null} or {@literal empty}.
251+
* @return the number of existing keys in redis. 0 if there are no existing keys.
252+
* @see <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
253+
* @since 3.5
254+
*/
255+
Mono<Long> countExistingKeys(Collection<K> keys);
256+
246257
/**
247258
* Determine the type stored at {@code key}.
248259
*
@@ -422,15 +433,6 @@ default Flux<K> scan() {
422433
*/
423434
Mono<Duration> getExpire(K key);
424435

425-
/**
426-
* Get the number of given {@code keys} that exists.
427-
*
428-
* @param keys must not be {@literal null} or {@literal empty}.
429-
* @return the number of existing keys in redis. 0 if there are no existing keys.
430-
* @see <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
431-
*/
432-
Mono<Long> countExistingKeys(Collection<K> keys);
433-
434436
// -------------------------------------------------------------------------
435437
// Methods dealing with Redis Lua scripts
436438
// -------------------------------------------------------------------------

src/main/java/org/springframework/data/redis/core/ReactiveRedisTemplate.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.ByteBuffer;
2323
import java.time.Duration;
2424
import java.time.Instant;
25+
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.List;
@@ -69,6 +70,7 @@
6970
* @author Christoph Strobl
7071
* @author Petromir Dzhunev
7172
* @author John Blum
73+
* @author Dahye Anne Lee
7274
* @param <K> the Redis key type against which the template works (usually a String)
7375
* @param <V> the Redis value type against which the template works
7476
* @since 2.0
@@ -326,6 +328,14 @@ public Mono<Boolean> hasKey(K key) {
326328
return doCreateMono(connection -> connection.keyCommands().exists(rawKey(key)));
327329
}
328330

331+
@Override
332+
public Mono<Long> countExistingKeys(Collection<K> keys) {
333+
334+
Assert.notNull(keys, "Keys must not be null");
335+
336+
return doCreateMono(connection -> connection.keyCommands().exists(rawKeys(keys)));
337+
}
338+
329339
@Override
330340
public Mono<DataType> type(K key) {
331341

@@ -506,14 +516,6 @@ public Mono<Boolean> move(K key, int dbIndex) {
506516
return doCreateMono(connection -> connection.keyCommands().move(rawKey(key), dbIndex));
507517
}
508518

509-
@Override
510-
public Mono<Long> countExistingKeys(Collection<K> keys) {
511-
Assert.notNull(keys, "Keys must not be null");
512-
513-
ByteBuffer[] rawKeys = rawKeys(keys);
514-
return doCreateMono(connection -> connection.keyCommands().exists(Arrays.asList(rawKeys)));
515-
}
516-
517519
// -------------------------------------------------------------------------
518520
// Methods dealing with Redis Lua scripts
519521
// -------------------------------------------------------------------------
@@ -697,12 +699,12 @@ private ByteBuffer rawKey(K key) {
697699
return getSerializationContext().getKeySerializationPair().getWriter().write(key);
698700
}
699701

700-
private ByteBuffer[] rawKeys(Collection<K> keys) {
701-
final ByteBuffer[] rawKeys = new ByteBuffer[keys.size()];
702+
private List<ByteBuffer> rawKeys(Collection<K> keys) {
703+
704+
List<ByteBuffer> rawKeys = new ArrayList<>(keys.size());
702705

703-
int i = 0;
704706
for (K key : keys) {
705-
rawKeys[i++] = rawKey(key);
707+
rawKeys.add(rawKey(key));
706708
}
707709

708710
return rawKeys;

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommandsIntegrationTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
*
5151
* @author Christoph Strobl
5252
* @author Mark Paluch
53+
* @author Dahye Anne Lee
5354
*/
5455
public class LettuceReactiveKeyCommandsIntegrationTests extends LettuceReactiveCommandsTestSupport {
5556

@@ -70,8 +71,9 @@ void existsShouldReturnFalseForNonExistingKeys() {
7071
connection.keyCommands().exists(KEY_1_BBUFFER).as(StepVerifier::create).expectNext(false).verifyComplete();
7172
}
7273

73-
@ParameterizedRedisTest
74+
@ParameterizedRedisTest // GH-2883
7475
void existsKeyReturnsKeyCount() {
76+
7577
nativeCommands.set(KEY_1, "1000");
7678
nativeCommands.set(KEY_2, "2000");
7779
nativeCommands.set(KEY_3, "3000");
@@ -80,8 +82,8 @@ void existsKeyReturnsKeyCount() {
8082
.expectNext(3L).verifyComplete();
8183
}
8284

83-
@ParameterizedRedisTest
84-
void existsKeyReturnsZeroWhenKeyDoesNotExist() {
85+
@ParameterizedRedisTest // GH-2883
86+
void existsKeyReturnsZeroWhenKeysDoNotExist() {
8587
connection.keyCommands().exists(List.of(KEY_1_BBUFFER, KEY_2_BBUFFER, KEY_3_BBUFFER)).as(StepVerifier::create)
8688
.expectNext(0L).verifyComplete();
8789
}

src/test/java/org/springframework/data/redis/core/ReactiveRedisTemplateIntegrationTests.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@
2424

2525
import java.time.Duration;
2626
import java.time.Instant;
27-
import java.util.*;
27+
import java.util.Arrays;
28+
import java.util.Collection;
29+
import java.util.Collections;
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Map;
2833
import java.util.function.Function;
2934

3035
import org.junit.jupiter.api.BeforeEach;
36+
3137
import org.springframework.dao.InvalidDataAccessApiUsageException;
3238
import org.springframework.data.redis.ObjectFactory;
3339
import org.springframework.data.redis.Person;
@@ -60,6 +66,7 @@
6066
*
6167
* @author Mark Paluch
6268
* @author Christoph Strobl
69+
* @author Dahye Anne Lee
6370
*/
6471
@MethodSource("testParams")
6572
public class ReactiveRedisTemplateIntegrationTests<K, V> {
@@ -127,6 +134,30 @@ void exists() {
127134
redisTemplate.hasKey(key).as(StepVerifier::create).expectNext(true).verifyComplete();
128135
}
129136

137+
@ParameterizedRedisTest // GH-2883
138+
void countExistingKeysIfValidKeyExists() {
139+
140+
K key = keyFactory.instance();
141+
K key2 = keyFactory.instance();
142+
K key3 = keyFactory.instance();
143+
144+
ReactiveValueOperations<K, V> ops = redisTemplate.opsForValue();
145+
146+
ops.set(key, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
147+
ops.set(key2, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
148+
ops.set(key3, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
149+
150+
redisTemplate.countExistingKeys(Arrays.asList(key, key2, key3)).as(StepVerifier::create).expectNext(3L)
151+
.verifyComplete();
152+
}
153+
154+
@ParameterizedRedisTest // GH-2883
155+
void countExistingKeysIfNotValidKeyExists() {
156+
157+
K key = keyFactory.instance();
158+
redisTemplate.countExistingKeys(List.of(key)).as(StepVerifier::create).expectNext(0L).verifyComplete();
159+
}
160+
130161
@ParameterizedRedisTest // DATAREDIS-743
131162
void scan() {
132163

@@ -580,23 +611,4 @@ void listenToPatternLaterShouldReceiveChannelMessagesCorrectly() {
580611
.verify(Duration.ofSeconds(3));
581612
}
582613

583-
@ParameterizedRedisTest
584-
void countExistingKeysIfValidKeyExists() {
585-
586-
K key = keyFactory.instance();
587-
K key2 = keyFactory.instance();
588-
K key3 = keyFactory.instance();
589-
590-
redisTemplate.opsForValue().set(key, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
591-
redisTemplate.opsForValue().set(key2, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
592-
redisTemplate.opsForValue().set(key3, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
593-
594-
redisTemplate.countExistingKeys(Arrays.asList(key, key2, key3)).as(StepVerifier::create).expectNext(3L).verifyComplete();
595-
}
596-
597-
@ParameterizedRedisTest
598-
void countExistingKeysIfNotValidKeyExists() {
599-
K key = keyFactory.instance();
600-
redisTemplate.countExistingKeys(List.of(key)).as(StepVerifier::create).expectNext(0L).verifyComplete();
601-
}
602614
}

0 commit comments

Comments
 (0)