Skip to content

LPOP and RPOP with count option #1987

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 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,15 @@ public byte[] lPop(byte[] key) {
return convertAndReturn(delegate.lPop(key), Converters.identityConverter());
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#lPop(byte[], long)
*/
@Override
public List<byte[]> lPop(byte[] key, long count) {
return convertAndReturn(delegate.lPop(key, count), Converters.identityConverter());
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#lPos(byte[], byte[], java.lang.Integer, java.lang.Integer)
Expand Down Expand Up @@ -887,6 +896,15 @@ public byte[] rPop(byte[] key) {
return convertAndReturn(delegate.rPop(key), Converters.identityConverter());
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#rPop(byte[], long)
*/
@Override
public List<byte[]> rPop(byte[] key, long count) {
return convertAndReturn(delegate.rPop(key, count), Converters.identityConverter());
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#rPopLPush(byte[], byte[])
Expand Down Expand Up @@ -2160,6 +2178,15 @@ public String lPop(String key) {
return convertAndReturn(delegate.lPop(serialize(key)), bytesToString);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#lPop(java.lang.String, long)
*/
@Override
public List<String> lPop(String key, long count) {
return convertAndReturn(delegate.lPop(serialize(key), count), byteListToStringList);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#lPos(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)
Expand Down Expand Up @@ -2313,6 +2340,15 @@ public String rPop(String key) {
return convertAndReturn(delegate.rPop(serialize(key)), bytesToString);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#rPop(java.lang.String, long)
*/
@Override
public List<String> rPop(String key, long count) {
return convertAndReturn(delegate.rPop(serialize(key), count), byteListToStringList);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#rPopLPush(java.lang.String, java.lang.String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* @author Mark Paluch
* @author Tugdual Grall
* @author Andrey Shlykov
* @author dengliming
* @since 2.0
*/
public interface DefaultedRedisConnection extends RedisConnection {
Expand Down Expand Up @@ -712,13 +713,27 @@ default byte[] lPop(byte[] key) {
return listCommands().lPop(key);
}

/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
@Override
@Deprecated
default List<byte[]> lPop(byte[] key, long count) {
return listCommands().lPop(key, count);
}

/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
@Override
@Deprecated
default byte[] rPop(byte[] key) {
return listCommands().rPop(key);
}

/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
@Override
@Deprecated
default List<byte[]> rPop(byte[] key, long count) {
return listCommands().rPop(key, count);
}

/** @deprecated in favor of {@link RedisConnection#listCommands()}}. */
@Override
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Christoph Strobl
* @author Mark Paluch
* @author dengliming
* @since 2.0
*/
public interface ReactiveListCommands {
Expand Down Expand Up @@ -874,12 +875,14 @@ default Mono<Long> lRem(ByteBuffer key, Long count, ByteBuffer value) {
*/
class PopCommand extends KeyCommand {

private final long count;

private final Direction direction;

private PopCommand(@Nullable ByteBuffer key, Direction direction) {
private PopCommand(@Nullable ByteBuffer key, long count, Direction direction) {

super(key);

this.count = count;
this.direction = direction;
}

Expand All @@ -889,7 +892,7 @@ private PopCommand(@Nullable ByteBuffer key, Direction direction) {
* @return a new {@link PopCommand} for right push ({@literal RPOP}).
*/
public static PopCommand right() {
return new PopCommand(null, Direction.RIGHT);
return new PopCommand(null, 0, Direction.RIGHT);
}

/**
Expand All @@ -898,7 +901,7 @@ public static PopCommand right() {
* @return a new {@link PopCommand} for right push ({@literal LPOP}).
*/
public static PopCommand left() {
return new PopCommand(null, Direction.LEFT);
return new PopCommand(null, 0, Direction.LEFT);
}

/**
Expand All @@ -911,7 +914,17 @@ public PopCommand from(ByteBuffer key) {

Assert.notNull(key, "Key must not be null!");

return new PopCommand(key, direction);
return new PopCommand(key, count, direction);
}

/**
* Applies the {@literal key}. Constructs a new command instance with all previously configured properties.
*
* @param count
* @return a new {@link LSetCommand} with {@literal value} applied.
*/
public PopCommand count(long count) {
return new PopCommand(getKey(), count, direction);
}

/**
Expand All @@ -920,6 +933,10 @@ public PopCommand from(ByteBuffer key) {
public Direction getDirection() {
return direction;
}

public long getCount() {
return count;
}
}

/**
Expand All @@ -936,6 +953,21 @@ default Mono<ByteBuffer> lPop(ByteBuffer key) {
return pop(Mono.just(PopCommand.left().from(key))).next().map(ByteBufferResponse::getOutput);
}

/**
* Removes and returns first element in list stored at {@literal key}.
*
* @param key must not be {@literal null}.
* @param count
* @return
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
*/
default Flux<ByteBuffer> lPop(ByteBuffer key, long count) {

Assert.notNull(key, "Key must not be null!");

return popList(Mono.just(PopCommand.left().from(key).count(count))).flatMap(CommandResponse::getOutput);
}

/**
* Removes and returns last element in list stored at {@literal key}.
*
Expand All @@ -950,6 +982,21 @@ default Mono<ByteBuffer> rPop(ByteBuffer key) {
return pop(Mono.just(PopCommand.right().from(key))).next().map(ByteBufferResponse::getOutput);
}

/**
* Removes and returns last element in list stored at {@literal key}.
*
* @param key must not be {@literal null}.
* @param count
* @return
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
*/
default Flux<ByteBuffer> rPop(ByteBuffer key, long count) {

Assert.notNull(key, "Key must not be null!");

return popList(Mono.just(PopCommand.right().from(key).count(count))).flatMap(CommandResponse::getOutput);
}

/**
* Removes and returns last element in list stored at {@link KeyCommand#getKey()}
*
Expand All @@ -960,6 +1007,16 @@ default Mono<ByteBuffer> rPop(ByteBuffer key) {
*/
Flux<ByteBufferResponse<PopCommand>> pop(Publisher<PopCommand> commands);

/**
* Removes and returns last element in list stored at {@link KeyCommand#getKey()}
*
* @param commands must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
*/
Flux<CommandResponse<PopCommand, Flux<ByteBuffer>>> popList(Publisher<PopCommand> commands);

/**
* @author Christoph Strobl
* @see <a href="https://redis.io/commands/blpop">Redis Documentation: BLPOP</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
* @author dengliming
*/
public interface RedisListCommands {

Expand Down Expand Up @@ -200,6 +201,17 @@ default Long lPos(byte[] key, byte[] element) {
@Nullable
byte[] lPop(byte[] key);

/**
* Removes and returns first element in list stored at {@code key}.
*
* @param key must not be {@literal null}.
* @param count
* @return {@literal null} when key does not exist or used in pipeline / transaction.
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
*/
@Nullable
List<byte[]> lPop(byte[] key, long count);

/**
* Removes and returns last element in list stored at {@code key}.
*
Expand All @@ -210,6 +222,17 @@ default Long lPos(byte[] key, byte[] element) {
@Nullable
byte[] rPop(byte[] key);

/**
* Removes and returns last element in list stored at {@code key}.
*
* @param key must not be {@literal null}.
* @param count
* @return {@literal null} when key does not exist or used in pipeline / transaction.
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
*/
@Nullable
List<byte[]> rPop(byte[] key, long count);

/**
* Removes and returns first element from lists stored at {@code keys}. <br>
* <b>Blocks connection</b> until element available or {@code timeout} reached.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,17 @@ default Long lPos(String key, String element) {
*/
String lPop(String key);

/**
* Removes and returns first element in list stored at {@code key}.
*
* @param key must not be {@literal null}.
* @param count
* @return
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
* @see RedisListCommands#lPop(byte[], long)
*/
List<String> lPop(String key, long count);

/**
* Removes and returns last element in list stored at {@code key}.
*
Expand All @@ -852,6 +863,17 @@ default Long lPos(String key, String element) {
*/
String rPop(String key);

/**
* Removes and returns last element in list stored at {@code key}.
*
* @param key must not be {@literal null}.
* @param count
* @return
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>
* @see RedisListCommands#rPop(byte[], long)
*/
List<String> rPop(String key, long count);

/**
* Removes and returns first element from lists stored at {@code keys} (see: {@link #lPop(byte[])}). <br>
* <b>Blocks connection</b> until element available or {@code timeout} reached.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Jot Zhao
* @author dengliming
* @since 2.0
*/
class JedisClusterListCommands implements RedisListCommands {
Expand Down Expand Up @@ -269,6 +270,22 @@ public byte[] lPop(byte[] key) {
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#lPop(byte[], long)
*/
@Override
public List<byte[]> lPop(byte[] key, long count) {

Assert.notNull(key, "Key must not be null!");

try {
return connection.getCluster().lpop(key, (int) count);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#rPop(byte[])
Expand All @@ -285,6 +302,22 @@ public byte[] rPop(byte[] key) {
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#rPop(byte[], long)
*/
@Override
public List<byte[]> rPop(byte[] key, long count) {

Assert.notNull(key, "Key must not be null!");

try {
return connection.getCluster().rpop(key, (int) count);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisListCommands#bLPop(int, byte[][])
Expand Down
Loading