Skip to content

Commit

Permalink
DATAREDIS-106 - Support for open interval for scores for sorted sets.
Browse files Browse the repository at this point in the history
We now support open zset's interval queriesfor JedisConnection, JredisConnection, LettuceConnection, SrpConnection and DefaultStringRedisConnection.

Original pull request: #97.
  • Loading branch information
Thomas Darimont committed Oct 27, 2014
1 parent 6514122 commit 1ef7902
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 1 deletion.
Expand Up @@ -2393,4 +2393,44 @@ public RedisSentinelConnection getSentinelConnection() {
return delegate.getSentinelConnection();
}

@Override
public Set<byte[]> zRangeByScore(String key, String min, String max) {
Set<byte[]> results = delegate.zRangeByScore(serialize(key), min, max);
if (isFutureConversion()) {
addResultConverter(identityConverter);
}
return results;
}

@Override
public Set<byte[]> zRangeByScore(String key, String min, String max, long offset, long count) {
Set<byte[]> results = delegate.zRangeByScore(serialize(key), min, max, offset, count);
if (isFutureConversion()) {
addResultConverter(identityConverter);
}
return results;
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {

Set<byte[]> results = delegate.zRangeByScore(key, min, max);
if (isFutureConversion()) {
addResultConverter(identityConverter);
}

return results;
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {

Set<byte[]> results = delegate.zRangeByScore(key, min, max, offset, count);
if (isFutureConversion()) {
addResultConverter(identityConverter);
}

return results;
}

}
Expand Up @@ -25,6 +25,8 @@
*
* @author Costin Leau
* @author Christoph Strobl
* @author Thomas Darimont
* @author David Liu
*/
public interface RedisZSetCommands {

Expand Down Expand Up @@ -346,4 +348,29 @@ public interface Tuple extends Comparable<Double> {
* @return
*/
Cursor<Tuple> zScan(byte[] key, ScanOptions options);

/**
* Get elements where score is between {@code min} and {@code max} from sorted set.
*
* @since 1.5
* @see http://redis.io/commands/zrangebyscore
* @param key
* @param begin
* @param end
* @return
*/
Set<byte[]> zRangeByScore(byte[] key, String min, String max);

/**
* Get elements in range from {@code begin} to {@code end} where score is between {@code min} and {@code max} from
* sorted set.
*
* @since 1.5
* @see http://redis.io/commands/zrangebyscore
* @param key
* @param begin
* @param end
* @return
*/
Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count);
}
Expand Up @@ -33,6 +33,9 @@
*
* @author Costin Leau
* @author Christoph Strobl
* @author Thomas Darimont
* @author David Liu
*
* @see RedisCallback
* @see RedisSerializer
* @see StringRedisTemplate
Expand Down Expand Up @@ -345,4 +348,24 @@ public interface StringTuple extends Tuple {
* @return
*/
Cursor<StringTuple> zScan(String key, ScanOptions options);

/**
* @since 1.5
* @param key
* @param min
* @param max
* @return
*/
Set<byte[]> zRangeByScore(String key, String min, String max);

/**
* @since 1.5
* @param key
* @param min
* @param max
* @param offset
* @param count
* @return
*/
Set<byte[]> zRangeByScore(String key, String min, String max, long offset, long count);
}
Expand Up @@ -3163,4 +3163,42 @@ protected JedisSentinelConnection getSentinelConnection(RedisNode sentinel) {
protected Jedis getJedis(RedisNode node) {
return new Jedis(node.getHost(), node.getPort());
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {

try {
String keyStr = new String(key, "UTF-8");
if (isPipelined()) {
pipeline(new JedisResult(pipeline.zrangeByScore(keyStr, min, max)));
return null;
}
if (isQueueing()) {
transaction(new JedisResult(transaction.zrangeByScore(keyStr, min, max)));
return null;
}
return JedisConverters.stringSetToByteSet().convert(jedis.zrangeByScore(keyStr, min, max));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {

try {
String keyStr = new String(key, "UTF-8");
if (isPipelined()) {
pipeline(new JedisResult(pipeline.zrangeByScore(keyStr, min, max, (int) offset, (int) count)));
return null;
}
if (isQueueing()) {
transaction(new JedisResult(transaction.zrangeByScore(keyStr, min, max, (int) offset, (int) count)));
return null;
}
return JedisConverters.stringSetToByteSet().convert(jedis.zrangeByScore(keyStr, min, max, (int) offset, (int) count));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
}
Expand Up @@ -1282,5 +1282,14 @@ public Cursor<byte[]> sScan(byte[] key, ScanOptions options) {
public Cursor<Entry<byte[], byte[]>> hScan(byte[] key, ScanOptions options) {
throw new UnsupportedOperationException("'HSCAN' command is not uspported for jredis");
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {
throw new UnsupportedOperationException("'zRangeByScore' command is not uspported for jredis");
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {
throw new UnsupportedOperationException("'zRangeByScore' command is not uspported for jredis");
}
}
Expand Up @@ -3616,4 +3616,44 @@ public CommandOutput getTypeHint(CommandType type, CommandOutput defaultType) {
}
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {

try {
if (isPipelined()) {
pipeline(new LettuceResult(getAsyncConnection().zrangebyscore(key, min, max),
LettuceConverters.bytesListToBytesSet()));
return null;
}
if (isQueueing()) {
transaction(new LettuceTxResult(getConnection().zrangebyscore(key, min, max),
LettuceConverters.bytesListToBytesSet()));
return null;
}
return LettuceConverters.toBytesSet(getConnection().zrangebyscore(key, min, max));
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {

try {
if (isPipelined()) {
pipeline(new LettuceResult(getAsyncConnection().zrangebyscore(key, min, max, offset, count),
LettuceConverters.bytesListToBytesSet()));
return null;
}
if (isQueueing()) {
transaction(new LettuceTxResult(getConnection().zrangebyscore(key, min, max, offset, count),
LettuceConverters.bytesListToBytesSet()));
return null;
}
return LettuceConverters.toBytesSet(getConnection().zrangebyscore(key, min, max, offset, count));
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}

}
Expand Up @@ -2472,4 +2472,35 @@ private Object[] sortParams(SortParameters params, byte[] sortKey) {
return arrays.toArray();
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {

try {
String keyStr = new String(key, "UTF-8");
if (isPipelined()) {
pipeline(new SrpResult(pipeline.zrangebyscore(keyStr, min, max, null, EMPTY_PARAMS_ARRAY),
SrpConverters.repliesToBytesSet()));
return null;
}
return SrpConverters.toBytesSet(client.zrangebyscore(keyStr, min, max, null, EMPTY_PARAMS_ARRAY).data());
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {

try {
String keyStr = new String(key, "UTF-8");
Object[] limit = limitParams(offset, count);
if (isPipelined()) {
pipeline(new SrpResult(pipeline.zrangebyscore(keyStr, min, max, null, limit), SrpConverters.repliesToBytesSet()));
return null;
}
return SrpConverters.toBytesSet(client.zrangebyscore(keyStr, min, max, null, limit).data());
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}
}
}
Expand Up @@ -29,6 +29,8 @@
*
* @author Costin Leau
* @author Christoph Strobl
* @author Thomas Darimont
* @author David Liu
*/
class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZSetOperations<K, V> {

Expand Down Expand Up @@ -392,4 +394,32 @@ public TypedTuple<V> convert(Tuple source) {
}
});
}

public Set<byte[]> rangeByScore(K key, final String min, final String max) {

final byte[] rawKey = rawKey(key);

Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {

public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.zRangeByScore(rawKey, min, max);
}
}, true);

return rawValues;
}

public Set<byte[]> rangeByScore(K key, final String min, final String max, final long offset, final long count) {

final byte[] rawKey = rawKey(key);

Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {

public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.zRangeByScore(rawKey, min, max, offset, count);
}
}, true);

return rawValues;
}
}
Expand Up @@ -35,6 +35,7 @@

/**
* @author Christoph Strobl
* @author Thomas Darimont
* @author David Liu
*/
public class RedisConnectionUnitTests {
Expand Down Expand Up @@ -790,5 +791,15 @@ protected RedisSentinelConnection getSentinelConnection(RedisNode sentinel) {
public <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
return delegate.evalSha(scriptSha, returnType, numKeys, keysAndArgs);
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {
return delegate.zRangeByScore(key, min, max);
}

@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {
return delegate.zRangeByScore(key, min, max, offset, count);
}
}
}
Expand Up @@ -61,6 +61,7 @@
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @author David Liu
*/
@RunWith(RelaxedJUnit4ClassRunner.class)
@ContextConfiguration
Expand Down Expand Up @@ -400,4 +401,19 @@ public void shouldReturnSentinelCommandsWhenWhenActiveSentinelFound() {
.sentinel("127.0.0.1", 26379).sentinel("127.0.0.1", 26380));
assertThat(connection.getSentinelConnection(), notNullValue());
}

/**
* @see DATAREDIS-106
*/
@Test
public void zRangeByScoreTest() {

connection.zAdd("myzset", 1, "one");
connection.zAdd("myzset", 2, "two");
connection.zAdd("myzset", 3, "three");

Set<byte[]> zRangeByScore = connection.zRangeByScore("myzset", "(1", "2");

assertEquals("two", new String(zRangeByScore.iterator().next()));
}
}
Expand Up @@ -22,6 +22,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.hamcrest.core.AllOf;
Expand Down Expand Up @@ -52,6 +53,7 @@
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @author David Liu
*/
@RunWith(RelaxedJUnit4ClassRunner.class)
@ContextConfiguration
Expand Down Expand Up @@ -311,5 +313,20 @@ public void testEvalShaArrayBytes() {
assertEquals(Arrays.asList(new Object[] { "key1", "arg1" }),
Arrays.asList(new Object[] { new String(scriptResults.get(0)), new String(scriptResults.get(1)) }));
}

/**
* @see DATAREDIS-106
*/
@Test
public void zRangeByScoreTest() {

connection.zAdd("myzset", 1, "one");
connection.zAdd("myzset", 2, "two");
connection.zAdd("myzset", 3, "three");

Set<byte[]> zRangeByScore = connection.zRangeByScore("myzset", "(1", "2");

assertEquals("two", new String(zRangeByScore.iterator().next()));
}

}

0 comments on commit 1ef7902

Please sign in to comment.