Skip to content

Commit 1964ed1

Browse files
committed
DATAREDIS-729 - Polishing.
Reorder methods so lexCount(…) comes after count(…). Tweak Javadoc wording. Simplify tests. Original pull request: #569.
1 parent bd1332e commit 1964ed1

21 files changed

+411
-412
lines changed

src/main/asciidoc/new-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This section briefly covers items that are new and noteworthy in the latest rele
99
* `RedisCache` now exposes `CacheStatistics`.
1010
* ACL authentication support for Redis Standalone, Redis Cluster and Master/Replica.
1111
* Password support for Redis Sentinel using Jedis.
12+
* Support for `ZREVRANGEBYLEX` and `ZLEXCOUNT` commands.
1213

1314
[[new-in-2.3.0]]
1415
== New in Spring Data Redis 2.3

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,15 +1659,6 @@ public Long zUnionStore(byte[] destKey, byte[]... sets) {
16591659
return convertAndReturn(delegate.zUnionStore(destKey, sets), identityConverter);
16601660
}
16611661

1662-
/*
1663-
* (non-Javadoc)
1664-
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
1665-
*/
1666-
@Override
1667-
public Long zLexCount(byte[] key, Range range) {
1668-
return delegate.zLexCount(key, range);
1669-
}
1670-
16711662
/*
16721663
* (non-Javadoc)
16731664
* @see org.springframework.data.redis.connection.StringRedisConnection#zLexCount(java.lang.String, org.springframework.data.redis.connection.RedisZSetCommands.Range)
@@ -2727,6 +2718,15 @@ public Long zCount(String key, double min, double max) {
27272718
return zCount(serialize(key), min, max);
27282719
}
27292720

2721+
/*
2722+
* (non-Javadoc)
2723+
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
2724+
*/
2725+
@Override
2726+
public Long zLexCount(byte[] key, Range range) {
2727+
return delegate.zLexCount(key, range);
2728+
}
2729+
27302730
/*
27312731
* (non-Javadoc)
27322732
* @see org.springframework.data.redis.connection.StringRedisConnection#zIncrBy(java.lang.String, double, java.lang.String)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,13 @@ default Long zCount(byte[] key, double min, double max) {
898898
return zSetCommands().zCount(key, min, max);
899899
}
900900

901+
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
902+
@Override
903+
@Deprecated
904+
default Long zLexCount(byte[] key, Range range) {
905+
return zSetCommands().zLexCount(key, range);
906+
}
907+
901908
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
902909
@Override
903910
@Deprecated
@@ -1094,13 +1101,6 @@ default Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offse
10941101
return zSetCommands().zRangeByScore(key, min, max, offset, count);
10951102
}
10961103

1097-
/** @deprecated in favor of {@link RedisConnection#zSetCommands()}}. */
1098-
@Override
1099-
@Deprecated
1100-
default Long zLexCount(byte[] key, Range range) {
1101-
return zSetCommands().zLexCount(key, range);
1102-
}
1103-
11041104
// HASH COMMANDS
11051105

11061106
/** @deprecated in favor of {@link RedisConnection#hashCommands()}}. */

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

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,82 @@ default Mono<Long> zCount(ByteBuffer key, Range<Double> range) {
11211121
*/
11221122
Flux<NumericResponse<ZCountCommand, Long>> zCount(Publisher<ZCountCommand> commands);
11231123

1124+
/**
1125+
* {@code ZLEXCOUNT} command parameters.
1126+
*
1127+
* @author Andrey Shlykov
1128+
* @since 2.4
1129+
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1130+
*/
1131+
class ZLexCountCommand extends KeyCommand {
1132+
1133+
private final Range<String> range;
1134+
1135+
private ZLexCountCommand(@Nullable ByteBuffer key, Range<String> range) {
1136+
super(key);
1137+
this.range = range;
1138+
}
1139+
1140+
/**
1141+
* Creates a new {@link ZLexCountCommand} given a {@link Range} of {@link String} to retrieve elements count.
1142+
*
1143+
* @param range must not be {@literal null}.
1144+
* @return a new {@link ZLexCountCommand} for {@link Range}.
1145+
*/
1146+
public static ZLexCountCommand stringsWithin(Range<String> range) {
1147+
1148+
Assert.notNull(range, "Range must not be null!");
1149+
1150+
return new ZLexCountCommand(null, range);
1151+
}
1152+
1153+
/**
1154+
* Applies the {@literal key}. Constructs a new command instance with all previously configured properties.
1155+
*
1156+
* @param key must not be {@literal null}.
1157+
* @return a new {@link ZLexCountCommand} with {@literal key} applied.
1158+
*/
1159+
public ZLexCountCommand forKey(ByteBuffer key) {
1160+
1161+
Assert.notNull(key, "Key must not be null!");
1162+
1163+
return new ZLexCountCommand(key, range);
1164+
}
1165+
1166+
/**
1167+
* @return
1168+
*/
1169+
public Range<String> getRange() {
1170+
return range;
1171+
}
1172+
}
1173+
1174+
/**
1175+
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max} applying
1176+
* lexicographical ordering.
1177+
*
1178+
* @param key must not be {@literal null}.
1179+
* @param range must not be {@literal null}.
1180+
* @return
1181+
* @since 2.4
1182+
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1183+
*/
1184+
default Mono<Long> zLexCount(ByteBuffer key, Range<String> range) {
1185+
return zLexCount(Mono.just(ZLexCountCommand.stringsWithin(range).forKey(key))).next()
1186+
.map(NumericResponse::getOutput);
1187+
}
1188+
1189+
/**
1190+
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max} applying
1191+
* lexicographical ordering.
1192+
*
1193+
* @param commands must not be {@literal null}.
1194+
* @return
1195+
* @since 2.4
1196+
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1197+
*/
1198+
Flux<NumericResponse<ZLexCountCommand, Long>> zLexCount(Publisher<ZLexCountCommand> commands);
1199+
11241200
/**
11251201
* Get the size of sorted set with {@literal key}.
11261202
*
@@ -1937,78 +2013,4 @@ default Flux<ByteBuffer> zRevRangeByLex(ByteBuffer key, Range<String> range, Lim
19372013
*/
19382014
Flux<CommandResponse<ZRangeByLexCommand, Flux<ByteBuffer>>> zRangeByLex(Publisher<ZRangeByLexCommand> commands);
19392015

1940-
/**
1941-
* {@code ZLEXCOUNT} command parameters.
1942-
*
1943-
* @author Andrey Shlykov
1944-
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1945-
*/
1946-
class ZLexCountCommand extends KeyCommand {
1947-
1948-
private final Range<String> range;
1949-
1950-
private ZLexCountCommand(@Nullable ByteBuffer key, Range<String> range) {
1951-
super(key);
1952-
this.range = range;
1953-
}
1954-
1955-
/**
1956-
* Creates a new {@link ZLexCountCommand} given a {@link Range} of {@link String} to retrieve elements
1957-
* count.
1958-
*
1959-
* @param range must not be {@literal null}.
1960-
* @return a new {@link ZLexCountCommand} for {@link Range}.
1961-
*/
1962-
public static ZLexCountCommand stringsWithin(Range<String> range) {
1963-
1964-
Assert.notNull(range, "Range must not be null!");
1965-
1966-
return new ZLexCountCommand(null, range);
1967-
}
1968-
1969-
/**
1970-
* Applies the {@literal key}. Constructs a new command instance with all previously configured properties.
1971-
*
1972-
* @param key must not be {@literal null}.
1973-
* @return a new {@link ZLexCountCommand} with {@literal key} applied.
1974-
*/
1975-
public ZLexCountCommand forKey(ByteBuffer key) {
1976-
1977-
Assert.notNull(key, "Key must not be null!");
1978-
1979-
return new ZLexCountCommand(key, range);
1980-
}
1981-
1982-
/**
1983-
* @return
1984-
*/
1985-
public Range<String> getRange() {
1986-
return range;
1987-
}
1988-
}
1989-
1990-
/**
1991-
* Count number of elements within sorted set with value within {@link Range}.
1992-
*
1993-
* @param key must not be {@literal null}.
1994-
* @param range must not be {@literal null}.
1995-
* @return
1996-
* @since 2.4
1997-
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1998-
*/
1999-
default Mono<Long> zLexCount(ByteBuffer key, Range<String> range) {
2000-
return zLexCount(Mono.just(ZLexCountCommand.stringsWithin(range).forKey(key))).next()
2001-
.map(NumericResponse::getOutput);
2002-
}
2003-
2004-
/**
2005-
* Count number of elements within sorted set with value within {@link Range}.
2006-
*
2007-
* @param commands must not be {@literal null}.
2008-
* @return
2009-
* @since 2.4
2010-
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
2011-
*/
2012-
Flux<NumericResponse<ZLexCountCommand, Long>> zLexCount(Publisher<ZLexCountCommand> commands);
2013-
20142016
}

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,19 @@ default Long zCount(byte[] key, double min, double max) {
768768
@Nullable
769769
Long zCount(byte[] key, Range range);
770770

771+
/**
772+
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max} applying
773+
* lexicographical ordering.
774+
*
775+
* @param key must not be {@literal null}.
776+
* @param range must not be {@literal null}.
777+
* @return {@literal null} when used in pipeline / transaction.
778+
* @since 2.4
779+
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
780+
*/
781+
@Nullable
782+
Long zLexCount(byte[] key, Range range);
783+
771784
/**
772785
* Get the size of sorted set with {@code key}.
773786
*
@@ -1059,16 +1072,4 @@ default Set<byte[]> zRevRangeByLex(byte[] key, Range range) {
10591072
@Nullable
10601073
Set<byte[]> zRevRangeByLex(byte[] key, Range range, Limit limit);
10611074

1062-
/**
1063-
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max}.
1064-
*
1065-
* @param key must not be {@literal null}.
1066-
* @param range must not be {@literal null}.
1067-
* @return {@literal null} when used in pipeline / transaction.
1068-
* @since 2.4
1069-
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1070-
*/
1071-
@Nullable
1072-
Long zLexCount(byte[] key, Range range);
1073-
10741075
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,20 @@ default Long lPos(String key, String element) {
13291329
*/
13301330
Long zCount(String key, double min, double max);
13311331

1332+
/**
1333+
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max} applying
1334+
* lexicographical ordering.
1335+
*
1336+
* @param key must not be {@literal null}.
1337+
* @param range must not be {@literal null}.
1338+
* @return {@literal null} when used in pipeline / transaction.
1339+
* @since 2.4
1340+
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1341+
* @see RedisZSetCommands#zLexCount(byte[], Range)
1342+
*/
1343+
@Nullable
1344+
Long zLexCount(String key, Range range);
1345+
13321346
/**
13331347
* Get the size of sorted set with {@code key}.
13341348
*
@@ -1541,19 +1555,6 @@ default Set<String> zRevRangeByLex(String key, Range range) {
15411555
*/
15421556
Set<String> zRevRangeByLex(String key, Range range, Limit limit);
15431557

1544-
/**
1545-
* Count number of elements within sorted set with value between {@code Range#min} and {@code Range#max}.
1546-
*
1547-
* @param key must not be {@literal null}.
1548-
* @param range must not be {@literal null}.
1549-
* @return {@literal null} when used in pipeline / transaction.
1550-
* @since 2.4
1551-
* @see <a href="https://redis.io/commands/zlexcount">Redis Documentation: ZLEXCOUNT</a>
1552-
* @see RedisZSetCommands#zLexCount(byte[], Range)
1553-
*/
1554-
@Nullable
1555-
Long zLexCount(String key, Range range);
1556-
15571558
// -------------------------------------------------------------------------
15581559
// Methods dealing with Redis Hashes
15591560
// -------------------------------------------------------------------------

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ public Long zCount(byte[] key, Range range) {
257257
}
258258
}
259259

260+
/*
261+
* (non-Javadoc)
262+
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
263+
*/
264+
@Override
265+
public Long zLexCount(byte[] key, Range range) {
266+
267+
Assert.notNull(key, "Key must not be null!");
268+
Assert.notNull(range, "Range must not be null!");
269+
270+
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.MINUS_BYTES);
271+
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.PLUS_BYTES);
272+
273+
try {
274+
return connection.getCluster().zlexcount(key, min, max);
275+
} catch (Exception ex) {
276+
throw convertJedisAccessException(ex);
277+
}
278+
}
279+
260280
/*
261281
* (non-Javadoc)
262282
* @see org.springframework.data.redis.connection.RedisZSetCommands#zRemRangeByScore(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
@@ -795,26 +815,6 @@ public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset
795815
}
796816
}
797817

798-
/*
799-
* (non-Javadoc)
800-
* @see org.springframework.data.redis.connection.RedisZSetCommands#zLexCount(byte[], org.springframework.data.redis.connection.RedisZSetCommands.Range)
801-
*/
802-
@Override
803-
public Long zLexCount(byte[] key, Range range) {
804-
805-
Assert.notNull(key, "Key must not be null!");
806-
Assert.notNull(range, "Range must not be null!");
807-
808-
byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.MINUS_BYTES);
809-
byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.PLUS_BYTES);
810-
811-
try {
812-
return connection.getCluster().zlexcount(key, min, max);
813-
} catch (Exception ex) {
814-
throw convertJedisAccessException(ex);
815-
}
816-
}
817-
818818
private DataAccessException convertJedisAccessException(Exception ex) {
819819
return connection.convertJedisAccessException(ex);
820820
}

0 commit comments

Comments
 (0)