DATAREDIS-407 - Add zRangeByLex to ZSetOperations.

We now offer methods for ZRANGEBYLEX on ZSetOperations, BoundZSetOperations as well as via RedisZSet.

Original Pull Request: #166
This commit is contained in:
Mark Paluch
2016-02-05 12:44:15 +01:00
committed by Christoph Strobl
parent 008836066f
commit 4131f501a6
9 changed files with 287 additions and 1 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.data.redis.core;
import java.util.Collection;
import java.util.Set;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
/**
@@ -26,6 +27,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
*/
public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
@@ -51,6 +53,10 @@ public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
Set<TypedTuple<V>> reverseRangeByScoreWithScores(double min, double max);
Set<V> rangeByLex(RedisZSetCommands.Range range);
Set<V> rangeByLex(RedisZSetCommands.Range range, RedisZSetCommands.Limit limit);
void removeRange(long start, long end);
void removeRangeByScore(double min, double max);

View File

@@ -20,6 +20,7 @@ import java.util.Collection;
import java.util.Set;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
/**
@@ -27,6 +28,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
*/
class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundZSetOperations<K, V> {
@@ -36,7 +38,7 @@ class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> impl
* Constructs a new <code>DefaultBoundZSetOperations</code> instance.
*
* @param key
* @param oeprations
* @param operations
*/
public DefaultBoundZSetOperations(K key, RedisOperations<K, V> operations) {
super(key, operations);
@@ -95,6 +97,16 @@ class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> impl
return ops.reverseRangeWithScores(getKey(), start, end);
}
@Override
public Set<V> rangeByLex(RedisZSetCommands.Range range) {
return ops.rangeByLex(getKey(), range);
}
@Override
public Set<V> rangeByLex(RedisZSetCommands.Range range, RedisZSetCommands.Limit limit) {
return ops.rangeByLex(getKey(), range, limit);
}
public Long rank(Object o) {
return ops.rank(getKey(), o);
}

View File

@@ -22,6 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
/**
@@ -31,6 +32,7 @@ import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
* @author Christoph Strobl
* @author Thomas Darimont
* @author David Liu
* @author Mark Paluch
*/
class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZSetOperations<K, V> {
@@ -141,6 +143,36 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
return deserializeTupleValues(rawValues);
}
@Override
public Set<V> rangeByLex(K key, final RedisZSetCommands.Range range) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.zRangeByLex(rawKey, range);
}
}, true);
return deserializeValues(rawValues);
}
@Override
public Set<V> rangeByLex(K key, final RedisZSetCommands.Range range, final RedisZSetCommands.Limit limit) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.zRangeByLex(rawKey, range, limit);
}
}, true);
return deserializeValues(rawValues);
}
public Set<V> rangeByScore(K key, final double min, final double max) {
final byte[] rawKey = rawKey(key);

View File

@@ -19,11 +19,14 @@ package org.springframework.data.redis.core;
import java.util.Collection;
import java.util.Set;
import org.springframework.data.redis.connection.RedisZSetCommands;
/**
* Redis ZSet/sorted set specific operations.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
*/
public interface ZSetOperations<K, V> {
@@ -52,6 +55,10 @@ public interface ZSetOperations<K, V> {
Set<TypedTuple<V>> reverseRangeWithScores(K key, long start, long end);
Set<V> rangeByLex(K key, RedisZSetCommands.Range range);
Set<V> rangeByLex(K key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit);
Set<V> rangeByScore(K key, double min, double max);
Set<V> rangeByScore(K key, double min, double max, long offset, long count);

View File

@@ -22,6 +22,7 @@ import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.ConvertingCursor;
import org.springframework.data.redis.core.Cursor;
@@ -35,6 +36,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
*/
public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
@@ -114,6 +116,16 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
return boundZSetOps.reverseRange(start, end);
}
@Override
public Set<E> rangeByLex(RedisZSetCommands.Range range) {
return boundZSetOps.rangeByLex(range);
}
@Override
public Set<E> rangeByLex(RedisZSetCommands.Range range, RedisZSetCommands.Limit limit) {
return boundZSetOps.rangeByLex(range, limit);
}
public Set<E> rangeByScore(double min, double max) {
return boundZSetOps.rangeByScore(min, max);
}

View File

@@ -22,6 +22,7 @@ import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
/**
@@ -31,6 +32,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
* Since using a {@link Comparator} does not apply, a ZSet implements the {@link SortedSet} methods where applicable.
*
* @author Costin Leau
* @author Mark Paluch
*/
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
@@ -46,6 +48,10 @@ public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
Set<E> reverseRange(long start, long end);
Set<E> rangeByLex(RedisZSetCommands.Range range);
Set<E> rangeByLex(RedisZSetCommands.Range range, RedisZSetCommands.Limit limit);
Set<E> rangeByScore(double min, double max);
Set<E> reverseRangeByScore(double min, double max);