DATAREDIS-729 - Add support for ZREVRANGEBYLEX command.

Original pull request: #566.
This commit is contained in:
anshlykov
2020-10-02 20:58:44 +03:00
committed by Mark Paluch
parent 0f54fcf796
commit da81275c4d
8 changed files with 289 additions and 6 deletions

View File

@@ -3639,6 +3639,24 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
return convertAndReturn(delegate.zRangeByLex(serialize(key), range, limit), byteSetToStringSet);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByLex(java.lang.String, org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit) {
return convertAndReturn(delegate.zRevRangeByLex(key, range, limit), identityConverter);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#zRevRangeByLex(java.lang.String, org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<String> zRevRangeByLex(String key, Range range, Limit limit) {
return convertAndReturn(delegate.zRevRangeByLex(serialize(key), range, limit), byteSetToStringSet);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisServerCommands#migrate(byte[], org.springframework.data.redis.connection.RedisNode, int, org.springframework.data.redis.connection.RedisServerCommands.MigrateOption)

View File

@@ -953,6 +953,13 @@ public interface DefaultedRedisConnection extends RedisConnection {
return zSetCommands().zRangeByLex(key, range, limit);
}
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated
default Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit) {
return zSetCommands().zRevRangeByLex(key, range, limit);
}
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
@Override
@Deprecated

View File

@@ -39,6 +39,7 @@ import org.springframework.util.ObjectUtils;
* @author Thomas Darimont
* @author David Liu
* @author Mark Paluch
* @author Andrey Shlykov
*/
public interface RedisZSetCommands {
@@ -1017,4 +1018,45 @@ public interface RedisZSetCommands {
@Nullable
Set<byte[]> zRangeByLex(byte[] key, Range range, Limit limit);
/**
* Get all the elements in the sorted set at {@literal key} in reversed lexicographical ordering.
*
* @param key must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
*/
@Nullable
default Set<byte[]> zRevRangeByLex(byte[] key) {
return zRevRangeByLex(key, Range.unbounded());
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in reversed lexicographical ordering.
*
* @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/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
*/
@Nullable
default Set<byte[]> zRevRangeByLex(byte[] key, Range range) {
return zRevRangeByLex(key, range, Limit.unlimited());
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in reversed lexicographical ordering. Result is
* limited via {@link Limit}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
* @param limit must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.4
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
*/
@Nullable
Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit);
}

View File

@@ -63,6 +63,7 @@ import org.springframework.util.CollectionUtils;
* @author Ninad Divadkar
* @author Tugdual Grall
* @author Dengliming
* @author Andrey Shlykov
* @see RedisCallback
* @see RedisSerializer
* @see StringRedisTemplate
@@ -1499,6 +1500,47 @@ public interface StringRedisConnection extends RedisConnection {
*/
Set<String> zRangeByLex(String key, Range range, Limit limit);
/**
* Get all the elements in the sorted set at {@literal key} in reversed lexicographical ordering.
*
* @param key must not be {@literal null}.
* @return
* @since 2.4
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
* @see RedisZSetCommands#zRevRangeByLex(byte[])
*/
default Set<String> zRevRangeByLex(String key) {
return zRevRangeByLex(key, Range.unbounded());
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in reversed lexicographical ordering.
*
* @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/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
* @see RedisZSetCommands#zRevRangeByLex(byte[], Range)
*/
default Set<String> zRevRangeByLex(String key, Range range) {
return zRevRangeByLex(key, range, Limit.unlimited());
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in reversed lexicographical ordering. Result is
* limited via {@link Limit}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
* @param range can be {@literal null}.
* @return
* @since 2.4
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
* @see RedisZSetCommands#zRevRangeByLex(byte[], Range, Limit)
*/
Set<String> zRevRangeByLex(String key, Range range, Limit limit);
// -------------------------------------------------------------------------
// Methods dealing with Redis Hashes
// -------------------------------------------------------------------------

View File

@@ -35,6 +35,7 @@ import org.springframework.util.Assert;
* @author Christoph Strobl
* @author Mark Paluch
* @author Clement Ong
* @author Andrey Shlykov
* @since 2.0
*/
class JedisClusterZSetCommands implements RedisZSetCommands {
@@ -324,6 +325,30 @@ class JedisClusterZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByLex(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(range, "Range cannot be null for ZREVRANGEBYLEX.");
Assert.notNull(limit, "Limit must not be null!");
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.MINUS_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.PLUS_BYTES);
try {
if (limit.isUnlimited()) {
return connection.getCluster().zrevrangeByLex(key, min, max);
}
return connection.getCluster().zrevrangeByLex(key, min, max, limit.getOffset(), limit.getCount());
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRangeWithScores(byte[], long, long)

View File

@@ -20,6 +20,7 @@ import redis.clients.jedis.ScanResult;
import redis.clients.jedis.ZParams;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.data.redis.connection.RedisZSetCommands;
@@ -27,7 +28,6 @@ import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.KeyBoundCursor;
import org.springframework.data.redis.core.ScanIteration;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -897,6 +897,50 @@ class JedisZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByLex(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(range, "Range for ZREVRANGEBYLEX must not be null!");
Assert.notNull(limit, "Limit must not be null! Use Limit.unlimited() instead.");
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.MINUS_BYTES);
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.PLUS_BYTES);
try {
if (isPipelined()) {
if (!limit.isUnlimited()) {
pipeline(connection.newJedisResult(
connection.getRequiredPipeline().zrevrangeByLex(key, max, min, limit.getOffset(), limit.getCount())));
} else {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().zrevrangeByLex(key, max, min)));
}
return null;
}
if (isQueueing()) {
if (!limit.isUnlimited()) {
transaction(connection.newJedisResult(
connection.getRequiredTransaction().zrevrangeByLex(key, max, min, limit.getOffset(), limit.getCount())));
} else {
transaction(connection.newJedisResult(connection.getRequiredTransaction().zrevrangeByLex(key, max, min)));
}
return null;
}
if (!limit.isUnlimited()) {
return new LinkedHashSet<>(connection.getJedis().zrevrangeByLex(key, max, min, limit.getOffset(), limit.getCount()));
}
return new LinkedHashSet<>(connection.getJedis().zrevrangeByLex(key, max, min));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
private boolean isPipelined() {
return connection.isPipelined();
}

View File

@@ -37,6 +37,7 @@ import org.springframework.util.Assert;
/**
* @author Christoph Strobl
* @author Mark Paluch
* @author Andrey Shlykov
* @since 2.0
*/
class LettuceZSetCommands implements RedisZSetCommands {
@@ -877,6 +878,55 @@ class LettuceZSetCommands implements RedisZSetCommands {
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRevRangeByLex(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(range, "Range for ZREVRANGEBYLEX must not be null!");
Assert.notNull(limit, "Limit must not be null!");
try {
if (isPipelined()) {
if (limit.isUnlimited()) {
pipeline(
connection.newLettuceResult(getAsyncConnection().zrevrangebylex(key, LettuceConverters.toRange(range, true)),
LettuceConverters.bytesListToBytesSet()));
} else {
pipeline(
connection.newLettuceResult(getAsyncConnection().zrevrangebylex(key, LettuceConverters.toRange(range, true),
LettuceConverters.toLimit(limit)), LettuceConverters.bytesListToBytesSet()));
}
return null;
}
if (isQueueing()) {
if (limit.isUnlimited()) {
transaction(
connection.newLettuceResult(getAsyncConnection().zrevrangebylex(key, LettuceConverters.toRange(range, true)),
LettuceConverters.bytesListToBytesSet()));
} else {
transaction(
connection.newLettuceResult(getAsyncConnection().zrevrangebylex(key, LettuceConverters.toRange(range, true),
LettuceConverters.toLimit(limit)), LettuceConverters.bytesListToBytesSet()));
}
return null;
}
if (limit.isUnlimited()) {
return LettuceConverters.bytesListToBytesSet()
.convert(getConnection().zrevrangebylex(key, LettuceConverters.toRange(range, true)));
}
return LettuceConverters.bytesListToBytesSet().convert(
getConnection().zrevrangebylex(key, LettuceConverters.toRange(range, true), LettuceConverters.toLimit(limit)));
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
private boolean isPipelined() {
return connection.isPipelined();
}