Skip to content

Commit 40d078f

Browse files
Shyngys Sapraliyevmp911de
authored andcommitted
Added zRangeStoreByLex and zRangeStoreByScore for ZRANGESTORE command.
Original pull request: #2370. Closes #2345
1 parent e61d9e7 commit 40d078f

File tree

11 files changed

+405
-0
lines changed

11 files changed

+405
-0
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* @author dengliming
8181
* @author ihaohong
8282
* @author Dennis Neufeld
83+
* @author Shyngys Sapraliyev
8384
*/
8485
@SuppressWarnings({ "ConstantConditions", "deprecation" })
8586
public class DefaultStringRedisConnection implements StringRedisConnection, DecoratedRedisConnection {
@@ -2710,6 +2711,37 @@ public Set<byte[]> zRevRangeByLex(byte[] key, org.springframework.data.domain.Ra
27102711
return convertAndReturn(delegate.zRevRangeByLex(key, range, limit), Converters.identityConverter());
27112712
}
27122713

2714+
@Override
2715+
public Long zRangeStoreByLex(byte[] dstKey, byte[] srcKey,
2716+
org.springframework.data.domain.Range<byte[]> range,
2717+
org.springframework.data.redis.connection.Limit limit) {
2718+
return convertAndReturn(delegate.zRangeStoreByLex(dstKey, srcKey, range, limit),
2719+
Converters.identityConverter());
2720+
}
2721+
2722+
@Override
2723+
public Long zRangeStoreByLex(String dstKey, String srcKey,
2724+
org.springframework.data.domain.Range<String> range,
2725+
org.springframework.data.redis.connection.Limit limit) {
2726+
return convertAndReturn(delegate.zRangeStoreByLex(serialize(dstKey), serialize(srcKey), serialize(range), limit),
2727+
Converters.identityConverter());
2728+
}
2729+
2730+
@Override
2731+
public Long zRangeStoreByScore(String dstKey, String srcKey, double min, double max,
2732+
org.springframework.data.redis.connection.Limit limit) {
2733+
return convertAndReturn(delegate.zRangeStoreByScore(serialize(dstKey), serialize(srcKey), min, max, limit),
2734+
Converters.identityConverter());
2735+
}
2736+
2737+
@Override
2738+
public Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey,
2739+
org.springframework.data.domain.Range<Number> range,
2740+
org.springframework.data.redis.connection.Limit limit) {
2741+
return convertAndReturn(delegate.zRangeStoreByScore(dstKey, srcKey, range, limit),
2742+
Converters.identityConverter());
2743+
}
2744+
27132745
@Override
27142746
public Set<String> zRevRangeByLex(String key, org.springframework.data.domain.Range<String> range,
27152747
org.springframework.data.redis.connection.Limit limit) {

src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* @author dengliming
6565
* @author ihaohong
6666
* @author Dennis Neufeld
67+
* @author Shyngys Sapraliyev
6768
* @since 2.0
6869
*/
6970
@Deprecated
@@ -1835,4 +1836,23 @@ default <T> T evalSha(String scriptSha, ReturnType returnType, int numKeys, byte
18351836
default <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
18361837
return scriptingCommands().evalSha(scriptSha, returnType, numKeys, keysAndArgs);
18371838
}
1839+
1840+
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
1841+
@Override
1842+
@Deprecated
1843+
default Long zRangeStoreByLex(byte[] dstKey, byte[] srcKey,
1844+
org.springframework.data.domain.Range<byte[]> range,
1845+
org.springframework.data.redis.connection.Limit limit) {
1846+
return zSetCommands().zRangeStoreByLex(dstKey, srcKey, range, limit);
1847+
}
1848+
1849+
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
1850+
@Override
1851+
@Deprecated
1852+
default Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey,
1853+
org.springframework.data.domain.Range<Number> range,
1854+
org.springframework.data.redis.connection.Limit limit) {
1855+
return zSetCommands().zRangeStoreByScore(dstKey, srcKey, range, limit);
1856+
}
1857+
18381858
}

src/main/java/org/springframework/data/redis/connection/RedisZSetCommands.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author David Liu
3939
* @author Mark Paluch
4040
* @author Andrey Shlykov
41+
* @author Shyngys Sapraliyev
4142
*/
4243
public interface RedisZSetCommands {
4344

@@ -1367,4 +1368,103 @@ default Set<byte[]> zRevRangeByLex(byte[] key, org.springframework.data.domain.R
13671368
Set<byte[]> zRevRangeByLex(byte[] key, org.springframework.data.domain.Range<byte[]> range,
13681369
org.springframework.data.redis.connection.Limit limit);
13691370

1371+
/**
1372+
* This command is like ZRANGE , but stores the result in the {@literal dstKey} destination key.
1373+
*
1374+
* @param dstKey must not be {@literal null}.
1375+
* @param srcKey must not be {@literal null}.
1376+
* @param range must not be {@literal null}.
1377+
* @return {@literal null} when used in pipeline / transaction.
1378+
* @since 3.0
1379+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
1380+
*/
1381+
@Nullable
1382+
default Long zRangeStoreByLex(byte[] dstKey, byte[] srcKey,
1383+
org.springframework.data.domain.Range<byte[]> range) {
1384+
return zRangeStoreByLex(dstKey, srcKey, range, org.springframework.data.redis.connection.Limit.unlimited());
1385+
}
1386+
1387+
/**
1388+
* This command is like ZRANGE , but stores the result in the {@literal dstKey} destination key.
1389+
*
1390+
* @param dstKey must not be {@literal null}.
1391+
* @param srcKey must not be {@literal null}.
1392+
* @param range must not be {@literal null}.
1393+
* @param limit must not be {@literal null}.
1394+
* @return {@literal null} when used in pipeline / transaction.
1395+
* @since 3.0
1396+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
1397+
*/
1398+
@Nullable
1399+
Long zRangeStoreByLex(byte[] dstKey, byte[] srcKey,
1400+
org.springframework.data.domain.Range<byte[]> range,
1401+
org.springframework.data.redis.connection.Limit limit);
1402+
1403+
/**
1404+
* This command is like ZRANGE, but stores the result in the {@literal dstKey} destination key.
1405+
*
1406+
* @param dstKey must not be {@literal null}.
1407+
* @param srcKey must not be {@literal null}.
1408+
* @param min minimal inclusive score
1409+
* @param max maximal inclusive score
1410+
* @return {@literal null} when used in pipeline / transaction.
1411+
* @since 3.0
1412+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
1413+
*/
1414+
@Nullable
1415+
default Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey, double min, double max) {
1416+
return zRangeStoreByScore(dstKey, srcKey, org.springframework.data.domain.Range.closed(min, max));
1417+
}
1418+
1419+
/**
1420+
* This command is like ZRANGE, but stores the result in the {@literal dstKey} destination key.
1421+
*
1422+
* @param dstKey must not be {@literal null}.
1423+
* @param srcKey must not be {@literal null}.
1424+
* @param min minimal inclusive score
1425+
* @param max maximal inclusive score
1426+
* @param limit must not be {@literal null}.
1427+
* @return {@literal null} when used in pipeline / transaction.
1428+
* @since 3.0
1429+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
1430+
*/
1431+
@Nullable
1432+
default Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey, double min, double max,
1433+
org.springframework.data.redis.connection.Limit limit) {
1434+
return zRangeStoreByScore(dstKey, srcKey, org.springframework.data.domain.Range.closed(min, max), limit);
1435+
}
1436+
1437+
/**
1438+
* This command is like ZRANGE, but stores the result in the {@literal dstKey} destination key.
1439+
*
1440+
* @param dstKey must not be {@literal null}.
1441+
* @param srcKey must not be {@literal null}.
1442+
* @param range must not be {@literal null}.
1443+
* @return {@literal null} when used in pipeline / transaction.
1444+
* @since 3.0
1445+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
1446+
*/
1447+
@Nullable
1448+
default Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey,
1449+
org.springframework.data.domain.Range<Number> range) {
1450+
return zRangeStoreByScore(dstKey, srcKey, range,
1451+
org.springframework.data.redis.connection.Limit.unlimited());
1452+
}
1453+
1454+
/**
1455+
* This command is like ZRANGE, but stores the result in the {@literal dstKey} destination key.
1456+
*
1457+
* @param dstKey must not be {@literal null}.
1458+
* @param srcKey must not be {@literal null}.
1459+
* @param range must not be {@literal null}.
1460+
* @param limit must not be {@literal null}.
1461+
* @return {@literal null} when used in pipeline / transaction.
1462+
* @since 3.0
1463+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
1464+
*/
1465+
@Nullable
1466+
Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey,
1467+
org.springframework.data.domain.Range<Number> range,
1468+
org.springframework.data.redis.connection.Limit limit);
1469+
13701470
}

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
* @author Dengliming
7171
* @author Andrey Shlykov
7272
* @author ihaohong
73+
* @author Shyngys Sapraliyev
7374
*
7475
* @see RedisCallback
7576
* @see RedisSerializer
@@ -1987,6 +1988,71 @@ default Set<String> zRevRangeByLex(String key, org.springframework.data.domain.R
19871988
Set<String> zRevRangeByLex(String key, org.springframework.data.domain.Range<String> range,
19881989
org.springframework.data.redis.connection.Limit limit);
19891990

1991+
/**
1992+
* This command is like ZRANGE , but stores the result in the {@literal dstKey} destination key.
1993+
*
1994+
* @param dstKey must not be {@literal null}.
1995+
* @param srcKey must not be {@literal null}.
1996+
* @param range must not be {@literal null}.
1997+
* @return {@literal null} when used in pipeline / transaction.
1998+
* @since 3.0
1999+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
2000+
*/
2001+
@Nullable
2002+
default Long zRangeStoreByLex(String dstKey, String srcKey,
2003+
org.springframework.data.domain.Range<String> range) {
2004+
return zRangeStoreByLex(dstKey, srcKey, range, org.springframework.data.redis.connection.Limit.unlimited());
2005+
}
2006+
2007+
/**
2008+
* This command is like ZRANGE , but stores the result in the {@literal dstKey} destination key.
2009+
*
2010+
* @param dstKey must not be {@literal null}.
2011+
* @param srcKey must not be {@literal null}.
2012+
* @param range must not be {@literal null}.
2013+
* @param limit must not be {@literal null}.
2014+
* @return {@literal null} when used in pipeline / transaction.
2015+
* @since 3.0
2016+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
2017+
*/
2018+
@Nullable
2019+
Long zRangeStoreByLex(String dstKey, String srcKey,
2020+
org.springframework.data.domain.Range<String> range,
2021+
org.springframework.data.redis.connection.Limit limit);
2022+
2023+
2024+
/**
2025+
* This command is like ZRANGE, but stores the result in the {@literal dstKey} destination key.
2026+
*
2027+
* @param dstKey must not be {@literal null}.
2028+
* @param srcKey must not be {@literal null}.
2029+
* @param min minimal inclusive score
2030+
* @param max maximal inclusive score
2031+
* @return {@literal null} when used in pipeline / transaction.
2032+
* @since 3.0
2033+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
2034+
*/
2035+
@Nullable
2036+
default Long zRangeStoreByScore(String dstKey, String srcKey, double min, double max) {
2037+
return zRangeStoreByScore(dstKey, srcKey, min, max, org.springframework.data.redis.connection.Limit.unlimited());
2038+
}
2039+
2040+
/**
2041+
* This command is like ZRANGE, but stores the result in the {@literal dstKey} destination key.
2042+
*
2043+
* @param dstKey must not be {@literal null}.
2044+
* @param srcKey must not be {@literal null}.
2045+
* @param min minimal inclusive score
2046+
* @param max maximal inclusive score
2047+
* @param limit must not be {@literal null}.
2048+
* @return {@literal null} when used in pipeline / transaction.
2049+
* @since 3.0
2050+
* @see <a href="https://redis.io/commands/zrangestore">Redis Documentation: ZRANGESTORE</a>
2051+
*/
2052+
@Nullable
2053+
Long zRangeStoreByScore(String dstKey, String srcKey, double min, double max,
2054+
org.springframework.data.redis.connection.Limit limit);
2055+
19902056
// -------------------------------------------------------------------------
19912057
// Methods dealing with Redis Hashes
19922058
// -------------------------------------------------------------------------

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterZSetCommands.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package org.springframework.data.redis.connection.jedis;
1717

18+
import redis.clients.jedis.Protocol;
1819
import redis.clients.jedis.params.ScanParams;
1920
import redis.clients.jedis.params.ZParams;
21+
import redis.clients.jedis.params.ZRangeParams;
2022
import redis.clients.jedis.resps.ScanResult;
2123

2224
import java.util.ArrayList;
@@ -49,6 +51,7 @@
4951
* @author Clement Ong
5052
* @author Andrey Shlykov
5153
* @author Jens Deppe
54+
* @author Shyngys Sapraliyev
5255
* @since 2.0
5356
*/
5457
class JedisClusterZSetCommands implements RedisZSetCommands {
@@ -493,6 +496,52 @@ public Set<byte[]> zRevRangeByLex(byte[] key, org.springframework.data.domain.Ra
493496
}
494497
}
495498

499+
@Override
500+
public Long zRangeStoreByLex(byte[] dstKey, byte[] srcKey,
501+
org.springframework.data.domain.Range<byte[]> range,
502+
org.springframework.data.redis.connection.Limit limit) {
503+
Assert.notNull(dstKey, "Destination key must not be null");
504+
Assert.notNull(srcKey, "Source key must not be null");
505+
Assert.notNull(range, "Range for ZRANGESTORE BYLEX must not be null");
506+
Assert.notNull(limit, "Limit must not be null. Use Limit.unlimited() instead.");
507+
508+
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getLowerBound(), JedisConverters.MINUS_BYTES);
509+
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getUpperBound(), JedisConverters.PLUS_BYTES);
510+
511+
ZRangeParams zRangeParams = new ZRangeParams(Protocol.Keyword.BYLEX, min, max)
512+
.limit(limit.getOffset(), limit.getCount());
513+
514+
try {
515+
return connection.getCluster().zrangestore(dstKey, srcKey, zRangeParams);
516+
} catch (Exception ex) {
517+
throw convertJedisAccessException(ex);
518+
}
519+
}
520+
521+
@Override
522+
public Long zRangeStoreByScore(byte[] dstKey, byte[] srcKey,
523+
org.springframework.data.domain.Range<Number> range,
524+
org.springframework.data.redis.connection.Limit limit) {
525+
Assert.notNull(dstKey, "Destination key must not be null");
526+
Assert.notNull(srcKey, "Source key must not be null");
527+
Assert.notNull(range, "Range for ZRANGESTORE BYSCORE must not be null");
528+
Assert.notNull(limit, "Limit must not be null. Use Limit.unlimited() instead.");
529+
530+
byte[] min = JedisConverters
531+
.boundaryToBytesForZRange(range.getLowerBound(), JedisConverters.NEGATIVE_INFINITY_BYTES);
532+
byte[] max = JedisConverters
533+
.boundaryToBytesForZRange(range.getUpperBound(), JedisConverters.POSITIVE_INFINITY_BYTES);
534+
535+
ZRangeParams zRangeParams = new ZRangeParams(Protocol.Keyword.BYSCORE, min, max)
536+
.limit(limit.getOffset(), limit.getCount());
537+
538+
try {
539+
return connection.getCluster().zrangestore(dstKey, srcKey, zRangeParams);
540+
} catch (Exception ex) {
541+
throw convertJedisAccessException(ex);
542+
}
543+
}
544+
496545
@Override
497546
public Set<Tuple> zRangeWithScores(byte[] key, long start, long end) {
498547

0 commit comments

Comments
 (0)