Skip to content

Add support for LMOVE and BLMOVE #2107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.6.0-SNAPSHOT</version>
<version>2.6.0-2039-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
2 changes: 1 addition & 1 deletion src/main/asciidoc/new-features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This section briefly covers items that are new and noteworthy in the latest rele
== New in Spring Data Redis 2.6

* 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.
* Support Redis 6.2 commands (`LPOP`/`RPOP` with `count`, `COPY`, `GETEX`, `GETDEL`, `ZPOPMIN`, `BZPOPMIN`, `ZPOPMAX`, `BZPOPMAX`, `ZMSCORE`, `ZDIFF`, `ZDIFFSTORE`, `ZINTER`, `ZUNION`).
* Support Redis 6.2 commands (`LPOP`/`RPOP` with `count`, `LMOVE`/`BLMOVE`, `COPY`, `GETEX`, `GETDEL`, `ZPOPMIN`, `BZPOPMIN`, `ZPOPMAX`, `BZPOPMAX`, `ZMSCORE`, `ZDIFF`, `ZDIFFSTORE`, `ZINTER`, `ZUNION`).

[[new-in-2.5.0]]
== New in Spring Data Redis 2.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,44 @@ public Long lInsert(byte[] key, Position where, byte[] pivot, byte[] value) {
return convertAndReturn(delegate.lInsert(key, where, pivot, value), Converters.identityConverter());
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#lMove(byte[], byte[], org.springframework.data.redis.connection.RedisListCommands.Direction, org.springframework.data.redis.connection.RedisListCommands.Direction)
*/
@Override
public byte[] lMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to) {
return convertAndReturn(delegate.lMove(sourceKey, destinationKey, from, to), Converters.identityConverter());
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#bLMove(byte[], byte[], org.springframework.data.redis.connection.RedisListCommands.Direction, org.springframework.data.redis.connection.RedisListCommands.Direction, double)
*/
@Override
public byte[] bLMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to, double timeout) {
return convertAndReturn(delegate.bLMove(sourceKey, destinationKey, from, to, timeout),
Converters.identityConverter());
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#lMove(java.lang.String, java.lang.String, org.springframework.data.redis.connection.RedisListCommands.Direction, org.springframework.data.redis.connection.RedisListCommands.Direction)
*/
@Override
public String lMove(String sourceKey, String destinationKey, Direction from, Direction to) {
return convertAndReturn(delegate.lMove(serialize(sourceKey), serialize(destinationKey), from, to), bytesToString);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#bLMove(java.lang.String, java.lang.String, org.springframework.data.redis.connection.RedisListCommands.Direction, org.springframework.data.redis.connection.RedisListCommands.Direction, double)
*/
@Override
public String bLMove(String sourceKey, String destinationKey, Direction from, Direction to, double timeout) {
return convertAndReturn(delegate.bLMove(serialize(sourceKey), serialize(destinationKey), from, to, timeout),
bytesToString);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#lLen(byte[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,20 @@ default Long lInsert(byte[] key, Position where, byte[] pivot, byte[] value) {
return listCommands().lInsert(key, where, pivot, value);
}

/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
@Override
@Deprecated
default byte[] lMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to) {
return listCommands().lMove(sourceKey, destinationKey, from, to);
}

/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
@Override
@Deprecated
default byte[] bLMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to, double timeout) {
return listCommands().bLMove(sourceKey, destinationKey, from, to, timeout);
}

/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
@Override
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,28 @@ public interface ReactiveListCommands {
* @author Christoph Strobl
*/
enum Direction {
LEFT, RIGHT

LEFT, RIGHT;

/**
* Alias for {@link Direction#LEFT}.
*
* @since 2.6
* @return
*/
public static Direction first() {
return LEFT;
}

/**
* Alias for {@link Direction#RIGHT}.
*
* @since 2.6
* @return
*/
public static Direction last() {
return RIGHT;
}
}

/**
Expand Down Expand Up @@ -635,6 +656,189 @@ default Mono<Long> lInsert(ByteBuffer key, Position position, ByteBuffer pivot,
*/
Flux<NumericResponse<LInsertCommand, Long>> lInsert(Publisher<LInsertCommand> commands);

/**
* {@code LMOVE} command parameters.
*
* @author Mark Paluch
* @since 2.6
* @see <a href="https://redis.io/commands/lmove">Redis Documentation: LMOVE</a>
*/
class LMoveCommand extends KeyCommand {

private final @Nullable ByteBuffer destinationKey;
private final @Nullable Direction from;
private final @Nullable Direction to;

public LMoveCommand(@Nullable ByteBuffer sourceKey, @Nullable ByteBuffer destinationKey, @Nullable Direction from,
@Nullable Direction to) {
super(sourceKey);
this.destinationKey = destinationKey;
this.from = from;
this.to = to;
}

/**
* Creates a new {@link LMoveCommand} given a {@link ByteBuffer sourceKey}.
*
* @param sourceKey must not be {@literal null}.
* @param sourceDirection must not be {@literal null}.
* @return a new {@link LMoveCommand} for {@link ByteBuffer value}.
*/
public static LMoveCommand from(ByteBuffer sourceKey, Direction sourceDirection) {

Assert.notNull(sourceKey, "Source key must not be null!");
Assert.notNull(sourceDirection, "Direction must not be null!");

return new LMoveCommand(sourceKey, null, sourceDirection, null);
}

/**
* Applies the {@link ByteBuffer destinationKey}. Constructs a new command instance with all previously configured
* properties.
*
* @param destinationKey must not be {@literal null}.
* @param to must not be {@literal null}.
* @return a new {@link LMoveCommand} with {@literal pivot} applied.
*/
public LMoveCommand to(ByteBuffer destinationKey, Direction destinationDirection) {

Assert.notNull(destinationKey, "Destination key must not be null!");
Assert.notNull(destinationDirection, "Direction must not be null!");

return new LMoveCommand(getKey(), destinationKey, from, destinationDirection);
}

/**
* Applies the {@link Duration timeout}. Constructs a new command instance with all previously configured
* properties.
*
* @param timeout must not be {@literal null}.
* @return a new {@link LMoveCommand} with {@literal pivot} applied.
*/
public BLMoveCommand timeout(Duration timeout) {

Assert.notNull(timeout, "Timeout must not be null!");

return new BLMoveCommand(getKey(), destinationKey, from, to, timeout);
}

@Nullable
public ByteBuffer getDestinationKey() {
return destinationKey;
}

@Nullable
public Direction getFrom() {
return from;
}

@Nullable
public Direction getTo() {
return to;
}
}

/**
* {@code BLMOVE} command parameters.
*
* @author Mark Paluch
* @since 2.6
* @see <a href="https://redis.io/commands/blmove">Redis Documentation: BLMOVE</a>
*/
class BLMoveCommand extends LMoveCommand {

private final @Nullable Duration timeout;

private BLMoveCommand(@Nullable ByteBuffer sourceKey, @Nullable ByteBuffer destinationKey, @Nullable Direction from,
@Nullable Direction to, @Nullable Duration timeout) {
super(sourceKey, destinationKey, from, to);
this.timeout = timeout;
}

@Nullable
public Duration getTimeout() {
return timeout;
}
}

/**
* Atomically returns and removes the first/last element (head/tail depending on the {@code from} argument) of the
* list stored at {@code sourceKey}, and pushes the element at the first/last element (head/tail depending on the
* {@code to} argument) of the list stored at {@code destinationKey}.
*
* @param sourceKey must not be {@literal null}.
* @param destinationKey must not be {@literal null}.
* @param from must not be {@literal null}.
* @param to must not be {@literal null}.
* @return
* @since 2.6
* @see <a href="https://redis.io/commands/lmove">Redis Documentation: LMOVE</a>
*/
default Mono<ByteBuffer> lMove(ByteBuffer sourceKey, ByteBuffer destinationKey, Direction from, Direction to) {

Assert.notNull(sourceKey, "Source key must not be null!");
Assert.notNull(destinationKey, "Destination key must not be null!");
Assert.notNull(from, "From direction must not be null!");
Assert.notNull(to, "To direction must not be null!");

return lMove(Mono.just(LMoveCommand.from(sourceKey, from).to(destinationKey, to))).map(CommandResponse::getOutput)
.next();
}

/**
* Atomically returns and removes the first/last element (head/tail depending on the {@code from} argument) of the
* list stored at {@code sourceKey}, and pushes the element at the first/last element (head/tail depending on the
* {@code to} argument) of the list stored at {@code destinationKey}.
*
* @param commands must not be {@literal null}.
* @return
* @since 2.6
* @see <a href="https://redis.io/commands/lmove">Redis Documentation: LMOVE</a>
*/
Flux<ByteBufferResponse<LMoveCommand>> lMove(Publisher<? extends LMoveCommand> commands);

/**
* Atomically returns and removes the first/last element (head/tail depending on the {@code from} argument) of the
* list stored at {@code sourceKey}, and pushes the element at the first/last element (head/tail depending on the
* {@code to} argument) of the list stored at {@code destinationKey}.
*
* @param sourceKey must not be {@literal null}.
* @param destinationKey must not be {@literal null}.
* @param from must not be {@literal null}.
* @param to must not be {@literal null}.
* @param timeout
* @return
* @since 2.6
* @see <a href="https://redis.io/commands/blmove">Redis Documentation: BLMOVE</a>
*/
default Mono<ByteBuffer> bLMove(ByteBuffer sourceKey, ByteBuffer destinationKey, Direction from, Direction to,
Duration timeout) {

Assert.notNull(sourceKey, "Source key must not be null!");
Assert.notNull(destinationKey, "Destination key must not be null!");
Assert.notNull(from, "From direction must not be null!");
Assert.notNull(to, "To direction must not be null!");
Assert.notNull(timeout, "Timeout must not be null!");
Assert.isTrue(!timeout.isNegative(), "Timeout must not be negative!");

return bLMove(Mono.just(BLMoveCommand.from(sourceKey, from).to(destinationKey, to).timeout(timeout)))
.map(CommandResponse::getOutput).next();
}

/**
* Atomically returns and removes the first/last element (head/tail depending on the {@code from} argument) of the
* list stored at {@code sourceKey}, and pushes the element at the first/last element (head/tail depending on the
* {@code to} argument) of the list stored at {@code destinationKey}.
* <p/>
* <b>Blocks connection</b> until element available or {@code timeout} reached.
*
* @param commands must not be {@literal null}.
* @return
* @since 2.6
* @see <a href="https://redis.io/commands/blmove">Redis Documentation: BLMOVE</a>
*/
Flux<ByteBufferResponse<BLMoveCommand>> bLMove(Publisher<BLMoveCommand> commands);

/**
* {@code LSET} command parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ enum Position {
BEFORE, AFTER
}

/**
* List move direction.
*
* @since 2.6
*/
enum Direction {
LEFT, RIGHT;

/**
* Alias for {@link Direction#LEFT}.
*
* @return
*/
public static Direction first() {
return LEFT;
}

/**
* Alias for {@link Direction#RIGHT}.
*
* @return
*/
public static Direction last() {
return RIGHT;
}
}

/**
* Append {@code values} to {@code key}.
*
Expand Down Expand Up @@ -169,6 +196,43 @@ default Long lPos(byte[] key, byte[] element) {
@Nullable
Long lInsert(byte[] key, Position where, byte[] pivot, byte[] value);

/**
* Atomically returns and removes the first/last element (head/tail depending on the {@code from} argument) of the
* list stored at {@code sourceKey}, and pushes the element at the first/last element (head/tail depending on the
* {@code to} argument) of the list stored at {@code destinationKey}.
*
* @param sourceKey must not be {@literal null}.
* @param destinationKey must not be {@literal null}.
* @param from must not be {@literal null}.
* @param to must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.6
* @see <a href="https://redis.io/commands/lmove">Redis Documentation: LMOVE</a>
* @see #bLMove(byte[], byte[], Direction, Direction, double)
*/
@Nullable
byte[] lMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to);

/**
* Atomically returns and removes the first/last element (head/tail depending on the {@code from} argument) of the
* list stored at {@code sourceKey}, and pushes the element at the first/last element (head/tail depending on the
* {@code to} argument) of the list stored at {@code destinationKey}.
* <p/>
* <b>Blocks connection</b> until element available or {@code timeout} reached.
*
* @param sourceKey must not be {@literal null}.
* @param destinationKey must not be {@literal null}.
* @param from must not be {@literal null}.
* @param to must not be {@literal null}.
* @param timeout
* @return {@literal null} when used in pipeline / transaction.
* @since 2.6
* @see <a href="https://redis.io/commands/blmove">Redis Documentation: BLMOVE</a>
* @see #lMove(byte[], byte[], Direction, Direction)
*/
@Nullable
byte[] bLMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to, double timeout);

/**
* Set the {@code value} list element at {@code index}.
*
Expand Down
Loading