Skip to content

Commit 41136c6

Browse files
committed
DATAREDIS-729 - Expose reverseRangeByLex methods on Template API.
Also, switch methods to default methods where applicable. Original pull request: #566.
1 parent df37dc8 commit 41136c6

File tree

8 files changed

+164
-33
lines changed

8 files changed

+164
-33
lines changed

src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
368368
* @see <a href="https://redis.io/commands/zrangebylex">Redis Documentation: ZRANGEBYLEX</a>
369369
*/
370370
@Nullable
371-
Set<V> rangeByLex(Range range);
371+
default Set<V> rangeByLex(Range range) {
372+
return rangeByLex(range, Limit.unlimited());
373+
}
372374

373375
/**
374376
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
@@ -384,6 +386,34 @@ public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
384386
@Nullable
385387
Set<V> rangeByLex(Range range, Limit limit);
386388

389+
/**
390+
* Get all elements with reverse lexicographical ordering with a value between {@link Range#getMin()} and
391+
* {@link Range#getMax()}.
392+
*
393+
* @param range must not be {@literal null}.
394+
* @return {@literal null} when used in pipeline / transaction.
395+
* @since 2.4
396+
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
397+
*/
398+
@Nullable
399+
default Set<V> reverseRangeByLex(Range range) {
400+
return reverseRangeByLex(range, Limit.unlimited());
401+
}
402+
403+
/**
404+
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
405+
* {@link Limit#getOffset()} with reverse lexicographical ordering having a value between {@link Range#getMin()} and
406+
* {@link Range#getMax()}.
407+
*
408+
* @param range must not be {@literal null}.
409+
* @param limit can be {@literal null}.
410+
* @return {@literal null} when used in pipeline / transaction.
411+
* @since 2.4
412+
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
413+
*/
414+
@Nullable
415+
Set<V> reverseRangeByLex(Range range, Limit limit);
416+
387417
/**
388418
* @return never {@literal null}.
389419
*/

src/main/java/org/springframework/data/redis/core/DefaultBoundZSetOperations.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public Long intersectAndStore(Collection<K> otherKeys, K destKey) {
104104
return ops.intersectAndStore(getKey(), otherKeys, destKey);
105105
}
106106

107-
/*
107+
/*
108108
* (non-Javadoc)
109109
* @see org.springframework.data.redis.core.BoundZSetOperations#intersectAndStore(java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate)
110110
*/
@@ -113,7 +113,7 @@ public Long intersectAndStore(Collection<K> otherKeys, K destKey, Aggregate aggr
113113
return ops.intersectAndStore(getKey(), otherKeys, destKey, aggregate);
114114
}
115115

116-
/*
116+
/*
117117
* (non-Javadoc)
118118
* @see org.springframework.data.redis.core.BoundZSetOperations#intersectAndStore(java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights)
119119
*/
@@ -187,20 +187,20 @@ public Set<TypedTuple<V>> reverseRangeWithScores(long start, long end) {
187187

188188
/*
189189
* (non-Javadoc)
190-
* @see org.springframework.data.redis.core.BoundZSetOperations#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range)
190+
* @see org.springframework.data.redis.core.BoundZSetOperations#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
191191
*/
192192
@Override
193-
public Set<V> rangeByLex(Range range) {
194-
return rangeByLex(range, Limit.unlimited());
193+
public Set<V> rangeByLex(Range range, Limit limit) {
194+
return ops.rangeByLex(getKey(), range, limit);
195195
}
196196

197197
/*
198198
* (non-Javadoc)
199-
* @see org.springframework.data.redis.core.BoundZSetOperations#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
199+
* @see org.springframework.data.redis.core.BoundZSetOperations#reverseRangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
200200
*/
201201
@Override
202-
public Set<V> rangeByLex(Range range, Limit limit) {
203-
return ops.rangeByLex(getKey(), range, limit);
202+
public Set<V> reverseRangeByLex(Range range, Limit limit) {
203+
return ops.reverseRangeByLex(getKey(), range, limit);
204204
}
205205

206206
/*
@@ -311,7 +311,7 @@ public Long unionAndStore(Collection<K> otherKeys, K destKey) {
311311
return ops.unionAndStore(getKey(), otherKeys, destKey);
312312
}
313313

314-
/*
314+
/*
315315
* (non-Javadoc)
316316
* @see org.springframework.data.redis.core.BoundZSetOperations#unionAndStore(java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate)
317317
*/
@@ -320,7 +320,7 @@ public Long unionAndStore(Collection<K> otherKeys, K destKey, Aggregate aggregat
320320
return ops.unionAndStore(getKey(), otherKeys, destKey, aggregate);
321321
}
322322

323-
/*
323+
/*
324324
* (non-Javadoc)
325325
* @see org.springframework.data.redis.core.BoundZSetOperations#unionAndStore(java.util.Collection, java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Aggregate, org.springframework.data.redis.connection.RedisZSetCommands.Weights)
326326
*/

src/main/java/org/springframework/data/redis/core/DefaultZSetOperations.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,26 @@ public Set<TypedTuple<V>> reverseRangeWithScores(K key, long start, long end) {
166166

167167
/*
168168
* (non-Javadoc)
169-
* @see org.springframework.data.redis.core.ZSetOperations#rangeByLex(java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Range)
169+
* @see org.springframework.data.redis.core.ZSetOperations#rangeByLex(java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
170170
*/
171171
@Override
172-
public Set<V> rangeByLex(K key, Range range) {
173-
return rangeByLex(key, range, Limit.unlimited());
172+
public Set<V> rangeByLex(K key, Range range, Limit limit) {
173+
174+
byte[] rawKey = rawKey(key);
175+
Set<byte[]> rawValues = execute(connection -> connection.zRangeByLex(rawKey, range, limit), true);
176+
177+
return deserializeValues(rawValues);
174178
}
175179

176180
/*
177181
* (non-Javadoc)
178-
* @see org.springframework.data.redis.core.ZSetOperations#rangeByLex(java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
182+
* @see org.springframework.data.redis.core.ZSetOperations#reverseRangeByLex(java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
179183
*/
180184
@Override
181-
public Set<V> rangeByLex(K key, Range range, Limit limit) {
185+
public Set<V> reverseRangeByLex(K key, Range range, Limit limit) {
182186

183187
byte[] rawKey = rawKey(key);
184-
Set<byte[]> rawValues = execute(connection -> connection.zRangeByLex(rawKey, range, limit), true);
188+
Set<byte[]> rawValues = execute(connection -> connection.zRevRangeByLex(rawKey, range, limit), true);
185189

186190
return deserializeValues(rawValues);
187191
}

src/main/java/org/springframework/data/redis/core/ZSetOperations.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ default Long intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggreg
475475
* @see <a href="https://redis.io/commands/zrangebylex">Redis Documentation: ZRANGEBYLEX</a>
476476
*/
477477
@Nullable
478-
Set<V> rangeByLex(K key, Range range);
478+
default Set<V> rangeByLex(K key, Range range) {
479+
return rangeByLex(key, range, Limit.unlimited());
480+
}
479481

480482
/**
481483
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
@@ -492,6 +494,36 @@ default Long intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggreg
492494
@Nullable
493495
Set<V> rangeByLex(K key, Range range, Limit limit);
494496

497+
/**
498+
* Get all elements with reverse lexicographical ordering from {@literal ZSET} at {@code key} with a value between
499+
* {@link Range#getMin()} and {@link Range#getMax()}.
500+
*
501+
* @param key must not be {@literal null}.
502+
* @param range must not be {@literal null}.
503+
* @return {@literal null} when used in pipeline / transaction.
504+
* @since 2.4
505+
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
506+
*/
507+
@Nullable
508+
default Set<V> reverseRangeByLex(K key, Range range) {
509+
return reverseRangeByLex(key, range, Limit.unlimited());
510+
}
511+
512+
/**
513+
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
514+
* {@link Limit#getOffset()} with reverse lexicographical ordering from {@literal ZSET} at {@code key} with a value
515+
* between {@link Range#getMin()} and {@link Range#getMax()}.
516+
*
517+
* @param key must not be {@literal null}
518+
* @param range must not be {@literal null}.
519+
* @param limit can be {@literal null}.
520+
* @return {@literal null} when used in pipeline / transaction.
521+
* @since 2.4
522+
* @see <a href="https://redis.io/commands/zrevrangebylex">Redis Documentation: ZREVRANGEBYLEX</a>
523+
*/
524+
@Nullable
525+
Set<V> reverseRangeByLex(K key, Range range, Limit limit);
526+
495527
/**
496528
* @return never {@literal null}.
497529
*/

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,20 @@ public Set<E> reverseRange(long start, long end) {
145145

146146
/*
147147
* (non-Javadoc)
148-
* @see org.springframework.data.redis.support.collections.RedisZSet#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range)
148+
* @see org.springframework.data.redis.support.collections.RedisZSet#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
149149
*/
150150
@Override
151-
public Set<E> rangeByLex(Range range) {
152-
return boundZSetOps.rangeByLex(range);
151+
public Set<E> rangeByLex(Range range, Limit limit) {
152+
return boundZSetOps.rangeByLex(range, limit);
153153
}
154154

155155
/*
156156
* (non-Javadoc)
157-
* @see org.springframework.data.redis.support.collections.RedisZSet#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
157+
* @see org.springframework.data.redis.support.collections.RedisZSet#reverseRangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
158158
*/
159159
@Override
160-
public Set<E> rangeByLex(Range range, Limit limit) {
161-
return boundZSetOps.rangeByLex(range, limit);
160+
public Set<E> reverseRangeByLex(Range range, Limit limit) {
161+
return boundZSetOps.reverseRangeByLex(range, limit);
162162
}
163163

164164
/*

src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
6060
* @see BoundZSetOperations#rangeByLex(Range)
6161
* @since 1.7
6262
*/
63-
Set<E> rangeByLex(Range range);
63+
default Set<E> rangeByLex(Range range) {
64+
return rangeByLex(range, Limit.unlimited());
65+
}
6466

6567
/**
6668
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
@@ -70,11 +72,37 @@ public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
7072
* @param range must not be {@literal null}.
7173
* @param limit can be {@literal null}.
7274
* @return
73-
* @see BoundZSetOperations#rangeByLex(Range, Limit)
7475
* @since 1.7
76+
* @see BoundZSetOperations#rangeByLex(Range, Limit)
7577
*/
7678
Set<E> rangeByLex(Range range, Limit limit);
7779

80+
/**
81+
* Get all elements with reverse lexicographical ordering with a value between {@link Range#getMin()} and
82+
* {@link Range#getMax()}.
83+
*
84+
* @param range must not be {@literal null}.
85+
* @return
86+
* @since 2.4
87+
* @see BoundZSetOperations#reverseRangeByLex(Range)
88+
*/
89+
default Set<E> reverseRangeByLex(Range range) {
90+
return reverseRangeByLex(range, Limit.unlimited());
91+
}
92+
93+
/**
94+
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
95+
* {@link Limit#getOffset()} with reverse lexicographical ordering having a value between {@link Range#getMin()} and
96+
* {@link Range#getMax()}.
97+
*
98+
* @param range must not be {@literal null}.
99+
* @param limit can be {@literal null}.
100+
* @return
101+
* @since 2.4
102+
* @see BoundZSetOperations#reverseRangeByLex(Range, Limit)
103+
*/
104+
Set<E> reverseRangeByLex(Range range, Limit limit);
105+
78106
Set<E> rangeByScore(double min, double max);
79107

80108
Set<E> reverseRangeByScore(double min, double max);

src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,29 @@ public void testRangeByLexUnboundedWithLimit() {
247247
zSetOps.add(key, value2, 3.7);
248248
zSetOps.add(key, value3, 5.8);
249249
Set<V> tuples = zSetOps.rangeByLex(key, RedisZSetCommands.Range.unbounded(),
250-
RedisZSetCommands.Limit.limit().count(1).offset(1));
250+
RedisZSetCommands.Limit.limit().count(2).offset(1));
251251

252-
assertThat(tuples).hasSize(1).startsWith(value2);
252+
assertThat(tuples).hasSize(2).containsSequence(value2, value3);
253+
}
254+
255+
@Test // DATAREDIS-729
256+
public void testReverseRangeByLexUnboundedWithLimit() {
257+
258+
assumeThat(valueFactory).isOfAnyClassIn(DoubleObjectFactory.class, DoubleAsStringObjectFactory.class,
259+
LongAsStringObjectFactory.class, LongObjectFactory.class);
260+
261+
K key = keyFactory.instance();
262+
V value1 = valueFactory.instance();
263+
V value2 = valueFactory.instance();
264+
V value3 = valueFactory.instance();
265+
266+
zSetOps.add(key, value1, 1.9);
267+
zSetOps.add(key, value2, 3.7);
268+
zSetOps.add(key, value3, 5.8);
269+
Set<V> tuples = zSetOps.reverseRangeByLex(key, RedisZSetCommands.Range.unbounded(),
270+
RedisZSetCommands.Limit.limit().count(2).offset(1));
271+
272+
assertThat(tuples).hasSize(2).containsSequence(value2, value1);
253273
}
254274

255275
@Test // DATAREDIS-407

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,31 @@ public void testRangeByLexBoundedWithLimit() {
402402
zSet.add(t2, 2);
403403
zSet.add(t3, 3);
404404
Set<T> tuples = zSet.rangeByLex(RedisZSetCommands.Range.range().gte(t1),
405-
RedisZSetCommands.Limit.limit().count(1).offset(1));
405+
RedisZSetCommands.Limit.limit().count(2).offset(1));
406406

407-
assertThat(tuples.size()).isEqualTo(1);
408-
T tuple = tuples.iterator().next();
409-
assertThat(tuple).isEqualTo(t2);
407+
assertThat(tuples).hasSize(2).containsSequence(t2, t3);
410408
}
411409

412-
@Test
410+
@Test // DATAREDIS-729
411+
public void testReverseRangeByLexBoundedWithLimit() {
412+
413+
assumeThat(factory).isOfAnyClassIn(DoubleObjectFactory.class, DoubleAsStringObjectFactory.class,
414+
LongAsStringObjectFactory.class, LongObjectFactory.class);
415+
416+
T t1 = getT();
417+
T t2 = getT();
418+
T t3 = getT();
419+
420+
zSet.add(t1, 1);
421+
zSet.add(t2, 2);
422+
zSet.add(t3, 3);
423+
Set<T> tuples = zSet.reverseRangeByLex(RedisZSetCommands.Range.range().gte(t1),
424+
RedisZSetCommands.Limit.limit().count(2).offset(1));
425+
426+
assertThat(tuples).hasSize(2).containsSequence(t2, t1);
427+
}
428+
429+
@Test // DATAREDIS-729
413430
public void testReverseRangeByScore() {
414431

415432
T t1 = getT();

0 commit comments

Comments
 (0)