Skip to content

Commit

Permalink
DATAREDIS-729 - Expose reverseRangeByLex methods on Template API.
Browse files Browse the repository at this point in the history
Also, switch methods to default methods where applicable.

Original pull request: #566.
  • Loading branch information
mp911de committed Oct 13, 2020
1 parent df37dc8 commit 41136c6
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
* @see <a href="https://redis.io/commands/zrangebylex">Redis Documentation: ZRANGEBYLEX</a>
*/
@Nullable
Set<V> rangeByLex(Range range);
default Set<V> rangeByLex(Range range) {
return rangeByLex(range, Limit.unlimited());
}

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

/**
* Get all elements with reverse lexicographical ordering with a value between {@link Range#getMin()} and
* {@link Range#getMax()}.
*
* @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<V> reverseRangeByLex(Range range) {
return reverseRangeByLex(range, Limit.unlimited());
}

/**
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
* {@link Limit#getOffset()} with reverse lexicographical ordering having a value between {@link Range#getMin()} and
* {@link Range#getMax()}.
*
* @param range must not be {@literal null}.
* @param limit can 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<V> reverseRangeByLex(Range range, Limit limit);

/**
* @return never {@literal null}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Long intersectAndStore(Collection<K> otherKeys, K destKey) {
return ops.intersectAndStore(getKey(), otherKeys, destKey);
}

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

/*
/*
* (non-Javadoc)
* @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)
*/
Expand Down Expand Up @@ -187,20 +187,20 @@ public Set<TypedTuple<V>> reverseRangeWithScores(long start, long end) {

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.BoundZSetOperations#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range)
* @see org.springframework.data.redis.core.BoundZSetOperations#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<V> rangeByLex(Range range) {
return rangeByLex(range, Limit.unlimited());
public Set<V> rangeByLex(Range range, Limit limit) {
return ops.rangeByLex(getKey(), range, limit);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.BoundZSetOperations#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
* @see org.springframework.data.redis.core.BoundZSetOperations#reverseRangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<V> rangeByLex(Range range, Limit limit) {
return ops.rangeByLex(getKey(), range, limit);
public Set<V> reverseRangeByLex(Range range, Limit limit) {
return ops.reverseRangeByLex(getKey(), range, limit);
}

/*
Expand Down Expand Up @@ -311,7 +311,7 @@ public Long unionAndStore(Collection<K> otherKeys, K destKey) {
return ops.unionAndStore(getKey(), otherKeys, destKey);
}

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

/*
/*
* (non-Javadoc)
* @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)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,26 @@ public Set<TypedTuple<V>> reverseRangeWithScores(K key, long start, long end) {

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ZSetOperations#rangeByLex(java.lang.Object, org.springframework.data.redis.connection.RedisZSetCommands.Range)
* @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)
*/
@Override
public Set<V> rangeByLex(K key, Range range) {
return rangeByLex(key, range, Limit.unlimited());
public Set<V> rangeByLex(K key, Range range, Limit limit) {

byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(connection -> connection.zRangeByLex(rawKey, range, limit), true);

return deserializeValues(rawValues);
}

/*
* (non-Javadoc)
* @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)
* @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)
*/
@Override
public Set<V> rangeByLex(K key, Range range, Limit limit) {
public Set<V> reverseRangeByLex(K key, Range range, Limit limit) {

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

return deserializeValues(rawValues);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,9 @@ default Long intersectAndStore(K key, Collection<K> otherKeys, K destKey, Aggreg
* @see <a href="https://redis.io/commands/zrangebylex">Redis Documentation: ZRANGEBYLEX</a>
*/
@Nullable
Set<V> rangeByLex(K key, Range range);
default Set<V> rangeByLex(K key, Range range) {
return rangeByLex(key, range, Limit.unlimited());
}

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

/**
* Get all elements with reverse lexicographical ordering from {@literal ZSET} at {@code key} with a value between
* {@link Range#getMin()} and {@link Range#getMax()}.
*
* @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<V> reverseRangeByLex(K key, Range range) {
return reverseRangeByLex(key, range, Limit.unlimited());
}

/**
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
* {@link Limit#getOffset()} with reverse lexicographical ordering from {@literal ZSET} at {@code key} with a value
* between {@link Range#getMin()} and {@link Range#getMax()}.
*
* @param key must not be {@literal null}
* @param range must not be {@literal null}.
* @param limit can 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<V> reverseRangeByLex(K key, Range range, Limit limit);

/**
* @return never {@literal null}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ public Set<E> reverseRange(long start, long end) {

/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisZSet#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range)
* @see org.springframework.data.redis.support.collections.RedisZSet#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<E> rangeByLex(Range range) {
return boundZSetOps.rangeByLex(range);
public Set<E> rangeByLex(Range range, Limit limit) {
return boundZSetOps.rangeByLex(range, limit);
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.support.collections.RedisZSet#rangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
* @see org.springframework.data.redis.support.collections.RedisZSet#reverseRangeByLex(org.springframework.data.redis.connection.RedisZSetCommands.Range, org.springframework.data.redis.connection.RedisZSetCommands.Limit)
*/
@Override
public Set<E> rangeByLex(Range range, Limit limit) {
return boundZSetOps.rangeByLex(range, limit);
public Set<E> reverseRangeByLex(Range range, Limit limit) {
return boundZSetOps.reverseRangeByLex(range, limit);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
* @see BoundZSetOperations#rangeByLex(Range)
* @since 1.7
*/
Set<E> rangeByLex(Range range);
default Set<E> rangeByLex(Range range) {
return rangeByLex(range, Limit.unlimited());
}

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

/**
* Get all elements with reverse lexicographical ordering with a value between {@link Range#getMin()} and
* {@link Range#getMax()}.
*
* @param range must not be {@literal null}.
* @return
* @since 2.4
* @see BoundZSetOperations#reverseRangeByLex(Range)
*/
default Set<E> reverseRangeByLex(Range range) {
return reverseRangeByLex(range, Limit.unlimited());
}

/**
* Get all elements {@literal n} elements, where {@literal n = } {@link Limit#getCount()}, starting at
* {@link Limit#getOffset()} with reverse lexicographical ordering having a value between {@link Range#getMin()} and
* {@link Range#getMax()}.
*
* @param range must not be {@literal null}.
* @param limit can be {@literal null}.
* @return
* @since 2.4
* @see BoundZSetOperations#reverseRangeByLex(Range, Limit)
*/
Set<E> reverseRangeByLex(Range range, Limit limit);

Set<E> rangeByScore(double min, double max);

Set<E> reverseRangeByScore(double min, double max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,29 @@ public void testRangeByLexUnboundedWithLimit() {
zSetOps.add(key, value2, 3.7);
zSetOps.add(key, value3, 5.8);
Set<V> tuples = zSetOps.rangeByLex(key, RedisZSetCommands.Range.unbounded(),
RedisZSetCommands.Limit.limit().count(1).offset(1));
RedisZSetCommands.Limit.limit().count(2).offset(1));

assertThat(tuples).hasSize(1).startsWith(value2);
assertThat(tuples).hasSize(2).containsSequence(value2, value3);
}

@Test // DATAREDIS-729
public void testReverseRangeByLexUnboundedWithLimit() {

assumeThat(valueFactory).isOfAnyClassIn(DoubleObjectFactory.class, DoubleAsStringObjectFactory.class,
LongAsStringObjectFactory.class, LongObjectFactory.class);

K key = keyFactory.instance();
V value1 = valueFactory.instance();
V value2 = valueFactory.instance();
V value3 = valueFactory.instance();

zSetOps.add(key, value1, 1.9);
zSetOps.add(key, value2, 3.7);
zSetOps.add(key, value3, 5.8);
Set<V> tuples = zSetOps.reverseRangeByLex(key, RedisZSetCommands.Range.unbounded(),
RedisZSetCommands.Limit.limit().count(2).offset(1));

assertThat(tuples).hasSize(2).containsSequence(value2, value1);
}

@Test // DATAREDIS-407
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,31 @@ public void testRangeByLexBoundedWithLimit() {
zSet.add(t2, 2);
zSet.add(t3, 3);
Set<T> tuples = zSet.rangeByLex(RedisZSetCommands.Range.range().gte(t1),
RedisZSetCommands.Limit.limit().count(1).offset(1));
RedisZSetCommands.Limit.limit().count(2).offset(1));

assertThat(tuples.size()).isEqualTo(1);
T tuple = tuples.iterator().next();
assertThat(tuple).isEqualTo(t2);
assertThat(tuples).hasSize(2).containsSequence(t2, t3);
}

@Test
@Test // DATAREDIS-729
public void testReverseRangeByLexBoundedWithLimit() {

assumeThat(factory).isOfAnyClassIn(DoubleObjectFactory.class, DoubleAsStringObjectFactory.class,
LongAsStringObjectFactory.class, LongObjectFactory.class);

T t1 = getT();
T t2 = getT();
T t3 = getT();

zSet.add(t1, 1);
zSet.add(t2, 2);
zSet.add(t3, 3);
Set<T> tuples = zSet.reverseRangeByLex(RedisZSetCommands.Range.range().gte(t1),
RedisZSetCommands.Limit.limit().count(2).offset(1));

assertThat(tuples).hasSize(2).containsSequence(t2, t1);
}

@Test // DATAREDIS-729
public void testReverseRangeByScore() {

T t1 = getT();
Expand Down

0 comments on commit 41136c6

Please sign in to comment.