DATAREDIS-729 - Add support for ZLEXCOUNT.

Original pull request: #569.
This commit is contained in:
anshlykov
2020-10-09 17:23:11 +03:00
committed by Mark Paluch
parent 4b33a62f64
commit bd1332ec5c
21 changed files with 448 additions and 0 deletions

View File

@@ -1659,6 +1659,24 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
return convertAndReturn(delegate.zUnionStore(destKey, sets), identityConverter);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long zLexCount(byte[] key, Range range) {
return delegate.zLexCount(key, range);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#zLexCount(java.lang.String, org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long zLexCount(String key, Range range) {
return delegate.zLexCount(serialize(key), range);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#pExpire(byte[], long)

View File

@@ -55,6 +55,7 @@ import org.springframework.lang.Nullable;
* @author Christoph Strobl
* @author Mark Paluch
* @author Tugdual Grall
* @author Andrey Shlykov
* @since 2.0
*/
public interface DefaultedRedisConnection extends RedisConnection {
@@ -1093,6 +1094,13 @@ public interface DefaultedRedisConnection extends RedisConnection {
return zSetCommands().zRangeByScore(key, min, max, offset, count);
}
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
default Long zLexCount(byte[] key, Range range) {
return zSetCommands().zLexCount(key, range);
}
// HASH COMMANDS
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */

View File

@@ -46,6 +46,7 @@ import org.springframework.util.Assert;
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Andrey Shlykov
* @since 2.0
*/
public interface ReactiveZSetCommands {
@@ -1935,4 +1936,79 @@ public interface ReactiveZSetCommands {
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
*/
Flux<CommandResponse<ZRangeByLexCommand, Flux<ByteBuffer>>> zRangeByLex(Publisher<ZRangeByLexCommand> commands);
/**
* {@code ZLEXCOUNT} command parameters.
*
* @author Andrey Shlykov
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
*/
class ZLexCountCommand extends KeyCommand {
private final Range<String> range;
private ZLexCountCommand(@Nullable ByteBuffer key, Range<String> range) {
super(key);
this.range = range;
}
/**
* Creates a new {@link ZLexCountCommand} given a {@link Range} of {@link String} to retrieve elements
* count.
*
* @param range must not be {@literal null}.
* @return a new {@link ZLexCountCommand} for {@link Range}.
*/
public static ZLexCountCommand stringsWithin(Range<String> range) {
Assert.notNull(range, "Range must not be null!");
return new ZLexCountCommand(null, range);
}
/**
* Applies the {@literal key}. Constructs a new command instance with all previously configured properties.
*
* @param key must not be {@literal null}.
* @return a new {@link ZLexCountCommand} with {@literal key} applied.
*/
public ZLexCountCommand forKey(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return new ZLexCountCommand(key, range);
}
/**
* @return
*/
public Range<String> getRange() {
return range;
}
}
/**
* Count number of elements within sorted set with value within {@link Range}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
* @return
* @since 2.4
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
*/
default Mono<Long> zLexCount(ByteBuffer key, Range<String> range) {
return zLexCount(Mono.just(ZLexCountCommand.stringsWithin(range).forKey(key))).next()
.map(NumericResponse::getOutput);
}
/**
* Count number of elements within sorted set with value within {@link Range}.
*
* @param commands must not be {@literal null}.
* @return
* @since 2.4
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
*/
Flux<NumericResponse<ZLexCountCommand, Long>> zLexCount(Publisher<ZLexCountCommand> commands);
}

View File

@@ -1059,4 +1059,16 @@ public interface RedisZSetCommands {
@Nullable
Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit);
/**
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
*/
@Nullable
Long zLexCount(byte[] key, Range range);
}

View File

@@ -1541,6 +1541,19 @@ public interface StringRedisConnection extends RedisConnection {
*/
Set<String> zRevRangeByLex(String key, Range range, Limit limit);
/**
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
* @see RedisZSetCommands#zLexCount(byte[], Range)
*/
@Nullable
Long zLexCount(String key, Range range);
// -------------------------------------------------------------------------
// Methods dealing with Redis Hashes
// -------------------------------------------------------------------------

View File

@@ -795,6 +795,26 @@ class JedisClusterZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long zLexCount(byte[] key, Range range) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(range, "Range must not be null!");
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.MINUS_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.PLUS_BYTES);
try {
return connection.getCluster().zlexcount(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
private DataAccessException convertJedisAccessException(Exception ex) {
return connection.convertJedisAccessException(ex);
}

View File

@@ -942,6 +942,34 @@ class JedisZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long zLexCount(byte[] key, Range range) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(range, "Range must not be null!");
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.MINUS_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.PLUS_BYTES);
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().zlexcount(key, min, max)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().zlexcount(key, min, max)));
return null;
}
return connection.getJedis().zlexcount(key, min, max);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
private boolean isPipelined() {
return connection.isPipelined();
}

View File

@@ -45,6 +45,7 @@ import org.springframework.util.ObjectUtils;
* @author Christoph Strobl
* @author Mark Paluch
* @author Michele Mancioppi
* @author Andrey Shlykov
* @since 2.0
*/
class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
@@ -484,6 +485,24 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveZSetCommands#zLexCount(org.reactivestreams.Publisher)
*/
@Override
public Flux<NumericResponse<ZLexCountCommand, Long>> zLexCount(Publisher<ZLexCountCommand> commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getRange(), "Range must not be null!");
Mono<Long> result = cmd.zlexcount(command.getKey(), RangeConverter.toRange(command.getRange()));
return result.map(value -> new NumericResponse<>(command, value));
}));
}
private static ZStoreArgs zStoreArgs(@Nullable Aggregate aggregate, @Nullable List<Double> weights) {
ZStoreArgs args = new ZStoreArgs();

View File

@@ -927,6 +927,31 @@ class LettuceZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long zLexCount(byte[] key, Range range) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(range, "Range must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newLettuceResult(getAsyncConnection().zlexcount(key, LettuceConverters.toRange(range))));
return null;
}
if (isQueueing()) {
transaction(connection.newLettuceResult(getAsyncConnection().zlexcount(key, LettuceConverters.toRange(range))));
return null;
}
return getConnection().zlexcount(key, LettuceConverters.toRange(range, true));
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
private boolean isPipelined() {
return connection.isPipelined();
}

View File

@@ -33,6 +33,7 @@ import org.springframework.lang.Nullable;
* @author Christoph Strobl
* @author Mark Paluch
* @author Wongoo (望哥)
* @author Andrey Shlykov
*/
public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
@@ -414,6 +415,17 @@ public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
@Nullable
Set<V> reverseRangeByLex(Range range, Limit limit);
/**
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max}.
*
* @param range must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
*/
@Nullable
Long lexCount(Range range);
/**
* @return never {@literal null}.
*/

View File

@@ -33,6 +33,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
* @author Christoph Strobl
* @author Mark Paluch
* @author Wongoo (望哥)
* @author Andrey Shlykov
*/
class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundZSetOperations<K, V> {
@@ -329,6 +330,15 @@ class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> impl
return ops.unionAndStore(getKey(), otherKeys, destKey, aggregate, weights);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.BoundZSetOperations#lexCount(org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long lexCount(Range range) {
return ops.lexCount(getKey(), range);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.BoundKeyOperations#getType()

View File

@@ -43,6 +43,7 @@ import org.springframework.util.Assert;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Andrey Shlykov
* @since 2.0
*/
class DefaultReactiveZSetOperations<K, V> implements ReactiveZSetOperations<K, V> {
@@ -548,6 +549,20 @@ class DefaultReactiveZSetOperations<K, V> implements ReactiveZSetOperations<K, V
return template.createMono(connection -> connection.keyCommands().del(rawKey(key))).map(l -> l != 0);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveZSetOperations#lexCount(java.lang.Object, org.springframework.data.domain.Range)
*/
@Override
public Mono<Long> lexCount(K key, Range<String> range) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(range, "Range must not be null!");
return createMono(connection -> connection.zLexCount(rawKey(key), range));
}
private <T> Mono<T> createMono(Function<ReactiveZSetCommands, Publisher<T>> function) {
Assert.notNull(function, "Function must not be null!");

View File

@@ -34,6 +34,7 @@ import org.springframework.data.redis.connection.RedisZSetCommands.Weights;
* @author David Liu
* @author Mark Paluch
* @author Wongoo (望哥)
* @author Andrey Shlykov
*/
class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZSetOperations<K, V> {
@@ -466,4 +467,11 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
return execute(connection -> connection.zRangeByScore(rawKey, min, max, offset, count), true);
}
@Override
public Long lexCount(K key, Range range) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.zLexCount(rawKey, range), true);
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Andrey Shlykov
* @see <a href="https://redis.io/commands#zset">Redis Documentation: Sorted Set Commands</a>
* @since 2.0
*/
@@ -461,4 +462,15 @@ public interface ReactiveZSetOperations<K, V> {
* @param key must not be {@literal null}.
*/
Mono<Boolean> delete(K key);
/**
* Count number of elements within sorted set with a value between
* {@link Range#getLowerBound()} and {@link Range#getUpperBound()}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}
* @return
* @see <a href="https://redis.io/commands/ZLEXCOUNT">Redis Documentation: ZLEXCOUNT</a>
*/
Mono<Long> lexCount(K key, Range<String> range);
}

View File

@@ -33,6 +33,7 @@ import org.springframework.lang.Nullable;
* @author Mark Paluch
* @author Rosty Kerei
* @author Wongoo (望哥)
* @author Andrey Shlykov
*/
public interface ZSetOperations<K, V> {
@@ -524,6 +525,18 @@ public interface ZSetOperations<K, V> {
@Nullable
Set<V> reverseRangeByLex(K key, Range range, Limit limit);
/**
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
*/
@Nullable
Long lexCount(K key, Range range);
/**
* @return never {@literal null}.
*/

View File

@@ -37,6 +37,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
* @author Andrey Shlykov
*/
public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
@@ -392,6 +393,15 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
return boundZSetOps.reverseRank(o);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisZSet#lexCount(org.springframework.data.redis.connection.RedisZSetCommands.Range)
*/
@Override
public Long lexCount(Range range) {
return boundZSetOps.lexCount(range);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisZSet#score(java.lang.Object)

View File

@@ -36,6 +36,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
* @author Costin Leau
* @author Mark Paluch
* @author Christoph Strobl
* @author Andrey Shlykov
*/
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
@@ -167,6 +168,16 @@ public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
*/
Double getDefaultScore();
/**
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max}.
*
* @param range must not be {@literal null}.
* @return
* @since 2.4
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
*/
Long lexCount(Range range);
/**
* Returns the first (lowest) element currently in this sorted set.
*