Skip to content

Commit 47f2254

Browse files
committed
Polishing.
Add since tags. Consistently document scan command with try-with-resources guidance. Use try-with-resources in tests. See #2260 Original pull request: #2263.
1 parent 14a1419 commit 47f2254

12 files changed

+67
-44
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,12 @@ public interface BoundHashOperations<H, HK, HV> extends BoundKeyOperations<H> {
202202
Map<HK, HV> entries();
203203

204204
/**
205-
* Use a {@link Cursor} to iterate over entries in the hash.
205+
* Use a {@link Cursor} to iterate over entries in hash at the bound key. <br />
206+
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leaks.
206207
*
207-
* @param options
208-
* @return
208+
* @param options must not be {@literal null}.
209+
* @return the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a
210+
* try-with-resources clause).
209211
* @since 1.4
210212
*/
211213
Cursor<Map.Entry<HK, HV>> scan(ScanOptions options);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,12 @@ public interface BoundSetOperations<K, V> extends BoundKeyOperations<K> {
253253
List<V> randomMembers(long count);
254254

255255
/**
256-
* @param options
257-
* @return {@literal null} when used in pipeline / transaction.
256+
* Use a {@link Cursor} to iterate over entries in set at {@code key}. <br />
257+
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leaks.
258+
*
259+
* @param options must not be {@literal null}.
260+
* @return the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a
261+
* try-with-resources clause).
258262
* @since 1.4
259263
*/
260264
@Nullable

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,12 @@ default Set<TypedTuple<V>> unionWithScores(Collection<K> otherKeys, Aggregate ag
794794
Long unionAndStore(Collection<K> otherKeys, K destKey, Aggregate aggregate);
795795

796796
/**
797-
* Iterate over elements in zset at the bound key. <br />
798-
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leak.
797+
* Use a {@link Cursor} to iterate over entries in zset at the bound key. <br />
798+
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leaks.
799799
*
800-
* @param options
801-
* @return
800+
* @param options must not be {@literal null}.
801+
* @return the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a
802+
* try-with-resources clause).
802803
* @since 1.4
803804
*/
804805
Cursor<TypedTuple<V>> scan(ScanOptions options);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,12 @@ public interface HashOperations<H, HK, HV> {
210210

211211
/**
212212
* Use a {@link Cursor} to iterate over entries in hash at {@code key}. <br />
213-
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leak.
213+
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leaks.
214214
*
215215
* @param key must not be {@literal null}.
216-
* @param options
217-
* @return {@literal null} when used in pipeline / transaction.
216+
* @param options must not be {@literal null}.
217+
* @return the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a
218+
* try-with-resources clause).
218219
* @since 1.4
219220
*/
220221
Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,12 @@ <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSer
262262

263263
/**
264264
* Use a {@link Cursor} to iterate over keys. <br />
265-
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leak.
265+
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leaks.
266266
*
267267
* @param options must not be {@literal null}.
268-
* @return never {@literal null}.
268+
* @return the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a
269+
* try-with-resources clause).
270+
* @since 2.7
269271
* @see <a href="https://redis.io/commands/scan">Redis Documentation: SCAN</a>
270272
*/
271273
Cursor<K> scan(ScanOptions options);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,13 @@ public interface SetOperations<K, V> {
372372
List<V> randomMembers(K key, long count);
373373

374374
/**
375-
* Iterate over elements in set at {@code key}. <br />
376-
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leak.
375+
* Use a {@link Cursor} to iterate over entries set at {@code key}. <br />
376+
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leaks.
377377
*
378378
* @param key
379-
* @param options
380-
* @return
379+
* @param options must not be {@literal null}.
380+
* @return the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a
381+
* try-with-resources clause).
381382
* @since 1.4
382383
*/
383384
Cursor<V> scan(K key, ScanOptions options);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -947,12 +947,13 @@ default Long unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate
947947
Long unionAndStore(K key, Collection<K> otherKeys, K destKey, Aggregate aggregate, Weights weights);
948948

949949
/**
950-
* Iterate over elements in zset at {@code key}. <br />
951-
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leak.
950+
* Use a {@link Cursor} to iterate over entries zset at {@code key}. <br />
951+
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leaks.
952952
*
953953
* @param key
954-
* @param options
955-
* @return {@literal null} when used in pipeline / transaction.
954+
* @param options must not be {@literal null}.
955+
* @return the result cursor providing access to the scan result. Must be closed once fully processed (e.g. through a
956+
* try-with-resources clause).
956957
* @see <a href="https://redis.io/commands/zscan">Redis Documentation: ZSCAN</a>
957958
* @since 1.4
958959
*/

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,18 @@ void testHScanReadsValuesFully() throws IOException {
131131
hashOps.put(key, key1, val1);
132132
hashOps.put(key, key2, val2);
133133

134-
Cursor<Map.Entry<HK, HV>> it = hashOps.scan(key, ScanOptions.scanOptions().count(1).build());
135134

136135
long count = 0;
137-
while (it.hasNext()) {
138-
Map.Entry<HK, HV> entry = it.next();
139-
assertThat(entry.getKey()).isIn(key1, key2);
140-
assertThat(entry.getValue()).isIn(val1, val2);
141-
count++;
136+
try (Cursor<Map.Entry<HK, HV>> it = hashOps.scan(key, ScanOptions.scanOptions().count(1).build())) {
137+
138+
while (it.hasNext()) {
139+
Map.Entry<HK, HV> entry = it.next();
140+
assertThat(entry.getKey()).isIn(key1, key2);
141+
assertThat(entry.getValue()).isIn(val1, val2);
142+
count++;
143+
}
142144
}
143145

144-
it.close();
145146
assertThat(count).isEqualTo(hashOps.size(key));
146147
}
147148

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,14 @@ void testSSCanReadsValuesFully() throws IOException {
208208
V v3 = valueFactory.instance();
209209

210210
setOps.add(key, v1, v2, v3);
211-
Cursor<V> it = setOps.scan(key, ScanOptions.scanOptions().count(1).build());
212211
long count = 0;
213-
while (it.hasNext()) {
214-
assertThat(it.next()).isIn(v1, v2, v3);
215-
count++;
212+
try (Cursor<V> it = setOps.scan(key, ScanOptions.scanOptions().count(1).build())) {
213+
while (it.hasNext()) {
214+
assertThat(it.next()).isIn(v1, v2, v3);
215+
count++;
216+
}
216217
}
217-
it.close();
218+
218219
assertThat(count).isEqualTo(setOps.size(key));
219220
}
220221

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,13 @@ void testZScanShouldReadEntireValueRange() throws IOException {
497497
zSetOps.add(key, values);
498498

499499
int count = 0;
500-
Cursor<TypedTuple<V>> it = zSetOps.scan(key, ScanOptions.scanOptions().count(2).build());
501-
while (it.hasNext()) {
502-
assertThat(it.next()).isIn(tuple1, tuple2, tuple3);
503-
count++;
500+
try (Cursor<TypedTuple<V>> it = zSetOps.scan(key, ScanOptions.scanOptions().count(2).build())) {
501+
while (it.hasNext()) {
502+
assertThat(it.next()).isIn(tuple1, tuple2, tuple3);
503+
count++;
504+
}
504505
}
505506

506-
it.close();
507507
assertThat(count).isEqualTo(3);
508508
}
509509

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.core;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.assertj.core.api.Assumptions.*;
1920

2021
import java.util.Arrays;
2122
import java.util.Collection;
@@ -141,6 +142,14 @@ public void testGetExpireMillisUsingPipelining() {
141142
super.testGetExpireMillisUsingPipelining();
142143
}
143144

145+
@ParameterizedRedisTest
146+
void testScan() {
147+
148+
// Only Lettuce supports cluster-wide scanning
149+
assumeThat(redisTemplate.getConnectionFactory()).isInstanceOf(LettuceConnectionFactory.class);
150+
super.testScan();
151+
}
152+
144153
@Parameters
145154
public static Collection<Object[]> testParams() {
146155

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ void testScan() {
133133
V value1 = valueFactory.instance();
134134
assumeThat(key1 instanceof String || key1 instanceof byte[]).isTrue();
135135
redisTemplate.opsForValue().set(key1, value1);
136-
Cursor<K> cursor = redisTemplate.scan(ScanOptions.scanOptions().count(1).build());
137136
long count = 0;
138-
while (cursor.hasNext()) {
139-
assertThat(cursor.next()).isEqualTo(key1);
140-
count++;
137+
try (Cursor<K> cursor = redisTemplate.scan(ScanOptions.scanOptions().count(1).build())) {
138+
while (cursor.hasNext()) {
139+
assertThat(cursor.next()).isEqualTo(key1);
140+
count++;
141+
}
141142
}
142-
cursor.close();
143143
assertThat(count).isEqualTo(1);
144144
}
145145

0 commit comments

Comments
 (0)