Skip to content

Commit 5747c7d

Browse files
committed
Polishing.
Add leftPop/rightPop methods to BoundListOperations. Tweak Javadoc and add since tags. Extend tests. Closes #1987
1 parent 3b146cc commit 5747c7d

11 files changed

+111
-12
lines changed

src/main/asciidoc/new-features.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This section briefly covers items that are new and noteworthy in the latest rele
77
== New in Spring Data Redis 2.6
88

99
* Support for `SubscriptionListener` when using `MessageListener` for subscription confirmation callbacks. `ReactiveRedisMessageListenerContainer` and `ReactiveRedisOperations` provide `receiveLater(…)` and `listenToLater(…)` methods to await until Redis acknowledges the subscription.
10+
* Support Redis 6.2 commands (`LPOP`/`RPOP` with `count`).
1011

1112
[[new-in-2.5.0]]
1213
== New in Spring Data Redis 2.5

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ public PopCommand from(ByteBuffer key) {
922922
*
923923
* @param count
924924
* @return a new {@link LSetCommand} with {@literal value} applied.
925+
* @since 2.6
925926
*/
926927
public PopCommand count(long count) {
927928
return new PopCommand(getKey(), count, direction);
@@ -954,12 +955,13 @@ default Mono<ByteBuffer> lPop(ByteBuffer key) {
954955
}
955956

956957
/**
957-
* Removes and returns first element in list stored at {@literal key}.
958+
* Removes and returns first {@code count} elements in list stored at {@literal key}.
958959
*
959960
* @param key must not be {@literal null}.
960961
* @param count
961962
* @return
962963
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
964+
* @since 2.6
963965
*/
964966
default Flux<ByteBuffer> lPop(ByteBuffer key, long count) {
965967

@@ -983,12 +985,13 @@ default Mono<ByteBuffer> rPop(ByteBuffer key) {
983985
}
984986

985987
/**
986-
* Removes and returns last element in list stored at {@literal key}.
988+
* Removes and returns last {@code count} elements in list stored at {@literal key}.
987989
*
988990
* @param key must not be {@literal null}.
989991
* @param count
990992
* @return
991993
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
994+
* @since 2.6
992995
*/
993996
default Flux<ByteBuffer> rPop(ByteBuffer key, long count) {
994997

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,13 @@ default Long lPos(byte[] key, byte[] element) {
202202
byte[] lPop(byte[] key);
203203

204204
/**
205-
* Removes and returns first element in list stored at {@code key}.
205+
* Removes and returns first {@code} elements in list stored at {@code key}.
206206
*
207207
* @param key must not be {@literal null}.
208208
* @param count
209209
* @return {@literal null} when key does not exist or used in pipeline / transaction.
210210
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
211+
* @since 2.6
211212
*/
212213
@Nullable
213214
List<byte[]> lPop(byte[] key, long count);
@@ -223,12 +224,13 @@ default Long lPos(byte[] key, byte[] element) {
223224
byte[] rPop(byte[] key);
224225

225226
/**
226-
* Removes and returns last element in list stored at {@code key}.
227+
* Removes and returns last {@code} elements in list stored at {@code key}.
227228
*
228229
* @param key must not be {@literal null}.
229230
* @param count
230231
* @return {@literal null} when key does not exist or used in pipeline / transaction.
231232
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
233+
* @since 2.6
232234
*/
233235
@Nullable
234236
List<byte[]> rPop(byte[] key, long count);

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -843,13 +843,14 @@ default Long lPos(String key, String element) {
843843
String lPop(String key);
844844

845845
/**
846-
* Removes and returns first element in list stored at {@code key}.
846+
* Removes and returns first {@code} elements in list stored at {@code key}.
847847
*
848848
* @param key must not be {@literal null}.
849849
* @param count
850850
* @return
851851
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
852852
* @see RedisListCommands#lPop(byte[], long)
853+
* @since 2.6
853854
*/
854855
List<String> lPop(String key, long count);
855856

@@ -864,13 +865,14 @@ default Long lPos(String key, String element) {
864865
String rPop(String key);
865866

866867
/**
867-
* Removes and returns last element in list stored at {@code key}.
868+
* Removes and returns last {@code} elements in list stored at {@code key}.
868869
*
869870
* @param key must not be {@literal null}.
870871
* @param count
871872
* @return
872873
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
873874
* @see RedisListCommands#rPop(byte[], long)
875+
* @since 2.6
874876
*/
875877
List<String> rPop(String key, long count);
876878

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

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ public Flux<ByteBufferResponse<PopCommand>> pop(Publisher<PopCommand> commands)
277277
*/
278278
@Override
279279
public Flux<CommandResponse<PopCommand, Flux<ByteBuffer>>> popList(Publisher<PopCommand> commands) {
280+
280281
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
281282

282283
Assert.notNull(command.getKey(), "Key must not be null!");

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

+24
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ public interface BoundListOperations<K, V> extends BoundKeyOperations<K> {
200200
@Nullable
201201
V leftPop();
202202

203+
/**
204+
* Removes and returns first {@code} elements in list stored at {@code key}.
205+
*
206+
* @param key must not be {@literal null}.
207+
* @param count
208+
* @return can be {@literal null}.
209+
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
210+
* @since 2.6
211+
*/
212+
@Nullable
213+
List<V> leftPop(long count);
214+
203215
/**
204216
* Removes and returns first element from lists stored at the bound key . <br>
205217
* <b>Blocks connection</b> until element available or {@code timeout} reached.
@@ -240,6 +252,18 @@ default V leftPop(Duration timeout) {
240252
@Nullable
241253
V rightPop();
242254

255+
/**
256+
* Removes and returns last {@code} elements in list stored at {@code key}.
257+
*
258+
* @param key must not be {@literal null}.
259+
* @param count
260+
* @return can be {@literal null}.
261+
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
262+
* @since 2.6
263+
*/
264+
@Nullable
265+
List<V> rightPop(long count);
266+
243267
/**
244268
* Removes and returns last element from lists stored at the bound key. <br>
245269
* <b>Blocks connection</b> until element available or {@code timeout} reached.

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

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.concurrent.TimeUnit;
2020

2121
import org.springframework.data.redis.connection.DataType;
22+
import org.springframework.lang.Nullable;
2223

2324
/**
2425
* Default implementation for {@link BoundListOperations}.
@@ -82,6 +83,15 @@ public V leftPop() {
8283
return ops.leftPop(getKey());
8384
}
8485

86+
/*
87+
* (non-Javadoc)
88+
* @see org.springframework.data.redis.core.BoundListOperations#leftPop(long)
89+
*/
90+
@Override
91+
public List<V> leftPop(long count) {
92+
return ops.leftPop(getKey(), count);
93+
}
94+
8595
/*
8696
* (non-Javadoc)
8797
* @see org.springframework.data.redis.core.BoundListOperations#leftPop(long, java.util.concurrent.TimeUnit)
@@ -163,6 +173,15 @@ public V rightPop() {
163173
return ops.rightPop(getKey());
164174
}
165175

176+
/*
177+
* (non-Javadoc)
178+
* @see org.springframework.data.redis.core.BoundListOperations#rightPop(long)
179+
*/
180+
@Override
181+
public List<V> rightPop(long count) {
182+
return ops.rightPop(getKey(), count);
183+
}
184+
166185
/*
167186
* (non-Javadoc)
168187
* @see org.springframework.data.redis.core.BoundListOperations#rightPop(long, java.util.concurrent.TimeUnit)

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,13 @@ public interface ListOperations<K, V> {
249249
V leftPop(K key);
250250

251251
/**
252-
* Removes and returns first element in list stored at {@code key}.
252+
* Removes and returns first {@code} elements in list stored at {@code key}.
253253
*
254254
* @param key must not be {@literal null}.
255255
* @param count
256256
* @return can be {@literal null}.
257257
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
258+
* @since 2.6
258259
*/
259260
@Nullable
260261
List<V> leftPop(K key, long count);
@@ -303,12 +304,13 @@ default V leftPop(K key, Duration timeout) {
303304
V rightPop(K key);
304305

305306
/**
306-
* Removes and returns last element in list stored at {@code key}.
307+
* Removes and returns last {@code} elements in list stored at {@code key}.
307308
*
308309
* @param key must not be {@literal null}.
309310
* @param count
310311
* @return can be {@literal null}.
311312
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
313+
* @since 2.6
312314
*/
313315
@Nullable
314316
List<V> rightPop(K key, long count);

src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.springframework.data.redis.serializer.RedisSerializer;
8181
import org.springframework.data.redis.test.condition.EnabledOnCommand;
8282
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver;
83+
import org.springframework.data.redis.test.condition.EnabledOnRedisVersion;
8384
import org.springframework.data.redis.test.condition.LongRunningTest;
8485
import org.springframework.data.redis.test.condition.RedisDriver;
8586
import org.springframework.data.redis.test.util.HexStringUtils;
@@ -1286,6 +1287,16 @@ void testLPop() {
12861287
verifyResults(Arrays.asList(new Object[] { 1L, 2L, "hello" }));
12871288
}
12881289

1290+
@Test // GH-1987
1291+
@EnabledOnRedisVersion("6.2")
1292+
void testLPopWithCount() {
1293+
actual.add(connection.rPush("PopList", "hello"));
1294+
actual.add(connection.rPush("PopList", "world"));
1295+
actual.add(connection.rPush("PopList", "42"));
1296+
actual.add(connection.lPop("PopList", 2));
1297+
verifyResults(Arrays.asList(new Object[] { 1L, 2L, 3L, Arrays.asList("hello", "world") }));
1298+
}
1299+
12891300
@Test
12901301
void testLRem() {
12911302
actual.add(connection.rPush("PopList", "hello"));
@@ -1335,6 +1346,16 @@ void testRPop() {
13351346
verifyResults(Arrays.asList(new Object[] { 1L, 2L, "world" }));
13361347
}
13371348

1349+
@Test // GH-1987
1350+
@EnabledOnRedisVersion("6.2")
1351+
void testRPopWithCount() {
1352+
actual.add(connection.rPush("PopList", "hello"));
1353+
actual.add(connection.rPush("PopList", "world"));
1354+
actual.add(connection.rPush("PopList", "42"));
1355+
actual.add(connection.rPop("PopList", 2));
1356+
verifyResults(Arrays.asList(new Object[] { 1L, 2L, 3L, Arrays.asList("42", "world") }));
1357+
}
1358+
13381359
@Test
13391360
void testRPopLPush() {
13401361
actual.add(connection.rPush("PopList", "hello"));

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,14 @@ public void testLPop() {
670670
verifyResults(Collections.singletonList(bar));
671671
}
672672

673-
@Test
673+
@Test // GH-1987
674674
public void testLPopCountBytes() {
675675
doReturn(Collections.singletonList(barBytes)).when(nativeConnection).lPop(fooBytes, 2);
676676
actual.add(connection.lPop(fooBytes, 2));
677677
verifyResults(Collections.singletonList(bytesList));
678678
}
679679

680-
@Test
680+
@Test // GH-1987
681681
public void testLPopCount() {
682682
doReturn(Collections.singletonList(barBytes)).when(nativeConnection).lPop(fooBytes, 2);
683683
actual.add(connection.lPop(foo, 2));
@@ -852,14 +852,14 @@ public void testRPop() {
852852
verifyResults(Collections.singletonList(bar));
853853
}
854854

855-
@Test
855+
@Test // GH-1987
856856
public void testRPopCountBytes() {
857857
doReturn(Collections.singletonList(barBytes)).when(nativeConnection).rPop(fooBytes, 2);
858858
actual.add(connection.rPop(fooBytes, 2));
859859
verifyResults(Collections.singletonList(bytesList));
860860
}
861861

862-
@Test
862+
@Test // GH-1987
863863
public void testRPopCount() {
864864
doReturn(Collections.singletonList(barBytes)).when(nativeConnection).rPop(fooBytes, 2);
865865
actual.add(connection.rPop(foo, 2));

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTxTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,18 @@ public void testLPop() {
509509
super.testLPop();
510510
}
511511

512+
@Test // GH-1987
513+
public void testLPopCountBytes() {
514+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
515+
super.testLPopCountBytes();
516+
}
517+
518+
@Test // GH-1987
519+
public void testLPopCount() {
520+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
521+
super.testLPopCount();
522+
}
523+
512524
@Test
513525
public void testLPushBytes() {
514526
doReturn(Collections.singletonList(8L)).when(nativeConnection).exec();
@@ -653,6 +665,18 @@ public void testRPop() {
653665
super.testRPop();
654666
}
655667

668+
@Test // GH-1987
669+
public void testRPopCountBytes() {
670+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
671+
super.testRPopCountBytes();
672+
}
673+
674+
@Test // GH-1987
675+
public void testRPopCount() {
676+
doReturn(Collections.singletonList(Collections.singletonList(barBytes))).when(nativeConnection).exec();
677+
super.testRPopCount();
678+
}
679+
656680
@Test
657681
public void testRPopLPushBytes() {
658682
doReturn(Arrays.asList(new Object[] { barBytes })).when(nativeConnection).exec();

0 commit comments

Comments
 (0)