Skip to content

Add WITHSCORE option to ZRANK and ZREVRANK commands #3183

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -51,6 +51,7 @@
import org.springframework.data.redis.connection.stream.StringRecord;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.DefaultTuple;
import org.springframework.data.redis.connection.zset.RankAndScore;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.ConvertingCursor;
Expand Down Expand Up @@ -81,6 +82,7 @@
* @author ihaohong
* @author Dennis Neufeld
* @author Shyngys Sapraliyev
* @author Seongil Kim
*/
@NullUnmarked
@SuppressWarnings({ "ConstantConditions", "deprecation" })
Expand Down Expand Up @@ -1181,6 +1183,11 @@ public Long zRank(byte[] key, byte[] value) {
return convertAndReturn(delegate.zRank(key, value), Converters.identityConverter());
}

@Override
public RankAndScore zRankWithScore(byte[] key, byte[] value) {
return convertAndReturn(delegate.zRankWithScore(key, value), Converters.identityConverter());
}

@Override
public Long zRem(byte[] key, byte[]... values) {
return convertAndReturn(delegate.zRem(key, values), Converters.identityConverter());
Expand Down Expand Up @@ -1221,6 +1228,11 @@ public Long zRevRank(byte[] key, byte[] value) {
return convertAndReturn(delegate.zRevRank(key, value), Converters.identityConverter());
}

@Override
public RankAndScore zRevRankWithScore(byte[] key, byte[] value) {
return convertAndReturn(delegate.zRevRankWithScore(key, value), Converters.identityConverter());
}

@Override
public Double zScore(byte[] key, byte[] value) {
return convertAndReturn(delegate.zScore(key, value), Converters.identityConverter());
Expand Down Expand Up @@ -2139,6 +2151,11 @@ public Long zRank(String key, String value) {
return zRank(serialize(key), serialize(value));
}

@Override
public RankAndScore zRankWithScore(String key, String value) {
return zRankWithScore(serialize(key), serialize(value));
}

@Override
public Long zRem(String key, String... values) {
return zRem(serialize(key), serializeMulti(values));
Expand Down Expand Up @@ -2195,6 +2212,11 @@ public Long zRevRank(String key, String value) {
return zRevRank(serialize(key), serialize(value));
}

@Override
public RankAndScore zRevRankWithScore(String key, String value) {
return zRevRankWithScore(serialize(key), serialize(value));
}

@Override
public Double zScore(String key, String value) {
return zScore(serialize(key), serialize(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.RankAndScore;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.Cursor;
Expand All @@ -67,6 +68,7 @@
* @author Dennis Neufeld
* @author Shyngys Sapraliyev
* @author Tihomir Mateev
* @author Seongil Kim
* @since 2.0
*/
@Deprecated
Expand Down Expand Up @@ -1242,6 +1244,13 @@ default Long zRank(byte[] key, byte[] value) {
return zSetCommands().zRank(key, value);
}

/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
default RankAndScore zRankWithScore(byte[] key, byte[] value) {
return zSetCommands().zRankWithScore(key, value);
}

/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
Expand Down Expand Up @@ -1291,6 +1300,13 @@ default Long zRevRank(byte[] key, byte[] value) {
return zSetCommands().zRevRank(key, value);
}

/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
default RankAndScore zRevRankWithScore(byte[] key, byte[] value) {
return zSetCommands().zRevRankWithScore(key, value);
}

/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.springframework.data.redis.connection.ReactiveRedisConnection.*;

import org.springframework.data.redis.connection.zset.RankAndScore;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -51,6 +52,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Andrey Shlykov
* @author Seongil Kim
* @since 2.0
*/
public interface ReactiveZSetCommands {
Expand Down Expand Up @@ -737,7 +739,24 @@ default Mono<Long> zRank(ByteBuffer key, ByteBuffer value) {
}

/**
* Determine the index of element with {@literal value} in a sorted set when scored high to low.
* Determine the index and score of element with {@literal value} in a sorted set.
*
* @param key must not be {@literal null}.
* @param value must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/zrank">Redis Documentation: ZRANK</a>
*/
default Mono<RankAndScore> zRankWithScore(ByteBuffer key, ByteBuffer value) {

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

return zRankWithScore(Mono.just(ZRankCommand.indexOf(value).storedWithin(key))).next()
.map(CommandResponse::getOutput);
}

/**
* Determine the index and score of element with {@literal value} in a sorted set when scored high to low.
*
* @param key must not be {@literal null}.
* @param value must not be {@literal null}.
Expand All @@ -753,6 +772,23 @@ default Mono<Long> zRevRank(ByteBuffer key, ByteBuffer value) {
.map(NumericResponse::getOutput);
}

/**
* Determine the index and score of element with {@literal value} in a sorted set when scored high to low.
*
* @param key must not be {@literal null}.
* @param value must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/zrevrank">Redis Documentation: ZREVRANK</a>
*/
default Mono<RankAndScore> zRevRankWithScore(ByteBuffer key, ByteBuffer value) {

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

return zRankWithScore(Mono.just(ZRankCommand.reverseIndexOf(value).storedWithin(key))).next()
.map(CommandResponse::getOutput);
}

/**
* Determine the index of element with {@literal value} in a sorted set when scored by
* {@link ZRankCommand#getDirection()}.
Expand All @@ -764,6 +800,17 @@ default Mono<Long> zRevRank(ByteBuffer key, ByteBuffer value) {
*/
Flux<NumericResponse<ZRankCommand, Long>> zRank(Publisher<ZRankCommand> commands);

/**
* Determine the index and score of element with {@literal value} in a sorted set when scored by
* {@link ZRankCommand#getDirection()}.
*
* @param commands must not be {@literal null}.
* @return
* @see <a href="https://redis.io/commands/zrank">Redis Documentation: ZRANK</a>
* @see <a href="https://redis.io/commands/zrevrank">Redis Documentation: ZREVRANK</a>
*/
Flux<CommandResponse<ZRankCommand, RankAndScore>> zRankWithScore(Publisher<ZRankCommand> commands);

/**
* {@code ZRANGE}/{@literal ZREVRANGE} command parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.RankAndScore;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.Cursor;
Expand All @@ -42,6 +43,7 @@
* @author Mark Paluch
* @author Andrey Shlykov
* @author Shyngys Sapraliyev
* @author Seongil Kim
* @see RedisCommands
*/
@NullUnmarked
Expand Down Expand Up @@ -501,6 +503,17 @@ default Long zAdd(byte @NonNull [] key, @NonNull Set<@NonNull Tuple> tuples) {
*/
Long zRank(byte @NonNull [] key, byte @NonNull [] value);

/**
* Determine the index and score of element with {@code value} in a sorted set.
*
* @param key must not be {@literal null}.
* @param value the value. Must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="https://redis.io/commands/zrank">Redis Documentation: ZRANK</a>
*/
@Nullable
RankAndScore zRankWithScore(byte[] key, byte[] value);

/**
* Determine the index of element with {@code value} in a sorted set when scored high to low.
*
Expand All @@ -511,6 +524,17 @@ default Long zAdd(byte @NonNull [] key, @NonNull Set<@NonNull Tuple> tuples) {
*/
Long zRevRank(byte @NonNull [] key, byte @NonNull [] value);

/**
* Determine the index and score of element with {@code value} in a sorted set when scored high to low.
*
* @param key must not be {@literal null}.
* @param value the value.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="https://redis.io/commands/zrevrank">Redis Documentation: ZREVRANK</a>
*/
@Nullable
RankAndScore zRevRankWithScore(byte[] key, byte[] value);

/**
* Get elements between {@code start} and {@code end} from sorted set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.connection.stream.StringRecord;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.RankAndScore;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.Cursor;
Expand Down Expand Up @@ -73,6 +74,7 @@
* @author Andrey Shlykov
* @author ihaohong
* @author Shyngys Sapraliyev
* @author Seongil Kim
* @see RedisCallback
* @see RedisSerializer
* @see StringRedisTemplate
Expand Down Expand Up @@ -1391,6 +1393,17 @@ String bLMove(@NonNull String sourceKey, @NonNull String destinationKey, @NonNul
*/
Long zRank(@NonNull String key, String value);

/**
* Determine the index and score of element with {@code value} in a sorted set.
*
* @param key must not be {@literal null}.
* @param value the value.
* @return
* @see <a href="https://redis.io/commands/zrank">Redis Documentation: ZRANK</a>
* @see RedisZSetCommands#zRankWithScore(byte[], byte[])
*/
RankAndScore zRankWithScore(String key, String value);

/**
* Determine the index of element with {@code value} in a sorted set when scored high to low.
*
Expand All @@ -1402,6 +1415,17 @@ String bLMove(@NonNull String sourceKey, @NonNull String destinationKey, @NonNul
*/
Long zRevRank(@NonNull String key, String value);

/**
* Determine the index and score of element with {@code value} in a sorted set when scored high to low.
*
* @param key must not be {@literal null}.
* @param value the value.
* @return
* @see <a href="https://redis.io/commands/zrevrank">Redis Documentation: ZREVRANK</a>
* @see RedisZSetCommands#zRevRankWithScore(byte[], byte[])
*/
RankAndScore zRevRankWithScore(String key, String value);

/**
* Get elements between {@code start} and {@code end} from sorted set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.redis.connection.jedis;

import org.springframework.data.redis.connection.zset.RankAndScore;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.params.ZParams;
Expand Down Expand Up @@ -58,6 +59,7 @@
* @author Jens Deppe
* @author Shyngys Sapraliyev
* @author John Blum
* @author Seongil Kim
* @since 2.0
*/
@NullUnmarked
Expand Down Expand Up @@ -192,6 +194,20 @@ public Long zRank(byte @NonNull [] key, byte @NonNull [] value) {
}
}

@Override
public RankAndScore zRankWithScore(byte[] key, byte[] value) {

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

try {
KeyValue<Long, Double> rankWithScore = connection.getCluster().zrankWithScore(key, value);
return new RankAndScore(rankWithScore.getKey(), rankWithScore.getValue());
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

@Override
public Long zRevRank(byte @NonNull [] key, byte @NonNull [] value) {

Expand All @@ -205,6 +221,20 @@ public Long zRevRank(byte @NonNull [] key, byte @NonNull [] value) {
}
}

@Override
public RankAndScore zRevRankWithScore(byte[] key, byte[] value) {

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

try {
KeyValue<Long, Double> rankWithScore = connection.getCluster().zrevrankWithScore(key, value);
return new RankAndScore(rankWithScore.getKey(), rankWithScore.getValue());
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

@Override
public Set<byte @NonNull []> zRange(byte @NonNull [] key, long start, long end) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.redis.connection.jedis;

import org.springframework.data.redis.connection.zset.RankAndScore;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Protocol;
Expand All @@ -30,6 +31,7 @@
import redis.clients.jedis.params.SortingParams;
import redis.clients.jedis.params.ZAddParams;
import redis.clients.jedis.resps.GeoRadiusResponse;
import redis.clients.jedis.util.KeyValue;
import redis.clients.jedis.util.SafeEncoder;

import java.nio.ByteBuffer;
Expand Down Expand Up @@ -104,6 +106,7 @@
* @author Guy Korland
* @author dengliming
* @author John Blum
* @author Seongil Kim
*/
@SuppressWarnings("ConstantConditions")
abstract class JedisConverters extends Converters {
Expand Down Expand Up @@ -152,6 +155,15 @@ static List<Tuple> toTupleList(List<redis.clients.jedis.resps.Tuple> source) {
return tuplesToTuples().convert(source);
}

static RankAndScore toRankAndScore(KeyValue<Long, Double> source) {

Assert.notNull(source, "KeyValue must not be null");
Assert.notNull(source.getKey(), "Key must not be null");
Assert.notNull(source.getValue(), "Value must not be null");

return new RankAndScore(source.getKey(), source.getValue());
}

/**
* Map a {@link Set} of {@link Tuple} by {@code value} to its {@code score}.
*
Expand Down
Loading