lexRange, lexRangeHead and lexRangeTail methods added. #135

pull/255/head
Nikita 10 years ago
parent 803ca44348
commit 4877152190

@ -277,6 +277,42 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return commandExecutor.readAsync(getName(), RedisCommands.ZRANGE_ENTRY, getName(), startIndex, endIndex, "WITHSCORES"); return commandExecutor.readAsync(getName(), RedisCommands.ZRANGE_ENTRY, getName(), startIndex, endIndex, "WITHSCORES");
} }
@Override
public Collection<V> lexRange(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive) {
return get(lexRangeAsync(fromElement, fromInclusive, toElement, toInclusive));
}
@Override
public Collection<V> lexRangeHead(V toElement, boolean toInclusive) {
return get(lexRangeHeadAsync(toElement, toInclusive));
}
@Override
public Future<Collection<V>> lexRangeHeadAsync(V toElement, boolean toInclusive) {
String toValue = value(toElement, toInclusive);
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), "-", toValue);
}
@Override
public Collection<V> lexRangeTail(V fromElement, boolean fromInclusive) {
return get(lexRangeTailAsync(fromElement, fromInclusive));
}
@Override
public Future<Collection<V>> lexRangeTailAsync(V fromElement, boolean fromInclusive) {
String fromValue = value(fromElement, fromInclusive);
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, "+");
}
@Override
public Future<Collection<V>> lexRangeAsync(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive) {
String fromValue = value(fromElement, fromInclusive);
String toValue = value(toElement, toInclusive);
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue);
}
@Override @Override
public Integer lexCount(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive) { public Integer lexCount(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive) {
return get(lexCountAsync(fromElement, fromInclusive, toElement, toInclusive)); return get(lexCountAsync(fromElement, fromInclusive, toElement, toInclusive));
@ -284,21 +320,20 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override @Override
public Future<Integer> lexCountAsync(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive) { public Future<Integer> lexCountAsync(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive) {
String fromValue = value(fromElement, fromInclusive);
String toValue = value(toElement, toInclusive);
return commandExecutor.readAsync(getName(), RedisCommands.ZLEXCOUNT, getName(), fromValue, toValue);
}
private String value(V fromElement, boolean fromInclusive) {
String fromValue = fromElement.toString(); String fromValue = fromElement.toString();
if (fromInclusive) { if (fromInclusive) {
fromValue = "[" + fromValue; fromValue = "[" + fromValue;
} else { } else {
fromValue = "(" + fromValue; fromValue = "(" + fromValue;
} }
return fromValue;
String toValue = toElement.toString();
if (toInclusive) {
toValue = "[" + toValue;
} else {
toValue = "(" + toValue;
}
return commandExecutor.readAsync(getName(), RedisCommands.ZLEXCOUNT, getName(), fromValue, toValue);
} }
} }

@ -56,6 +56,7 @@ public interface RedisCommands {
RedisStrictCommand<Double> ZSCORE = new RedisStrictCommand<Double>("ZSCORE", new DoubleReplayConvertor()); RedisStrictCommand<Double> ZSCORE = new RedisStrictCommand<Double>("ZSCORE", new DoubleReplayConvertor());
RedisStrictCommand<Integer> ZRANK = new RedisStrictCommand<Integer>("ZRANK", new IntegerReplayConvertor()); RedisStrictCommand<Integer> ZRANK = new RedisStrictCommand<Integer>("ZRANK", new IntegerReplayConvertor());
RedisCommand<List<Object>> ZRANGE = new RedisCommand<List<Object>>("ZRANGE", new ObjectListReplayDecoder<Object>()); RedisCommand<List<Object>> ZRANGE = new RedisCommand<List<Object>>("ZRANGE", new ObjectListReplayDecoder<Object>());
RedisCommand<List<Object>> ZRANGEBYLEX = new RedisCommand<List<Object>>("ZRANGEBYLEX", new ObjectListReplayDecoder<Object>());
RedisCommand<List<ScoredEntry<Object>>> ZRANGE_ENTRY = new RedisCommand<List<ScoredEntry<Object>>>("ZRANGE", new ScoredSortedSetReplayDecoder<Object>()); RedisCommand<List<ScoredEntry<Object>>> ZRANGE_ENTRY = new RedisCommand<List<ScoredEntry<Object>>>("ZRANGE", new ScoredSortedSetReplayDecoder<Object>());
RedisCommand<ListScanResult<Object>> ZSCAN = new RedisCommand<ListScanResult<Object>>("ZSCAN", new NestedMultiDecoder(new ObjectListReplayDecoder<Object>(), new ScoredSortedSetScanReplayDecoder()), ValueType.OBJECT); RedisCommand<ListScanResult<Object>> ZSCAN = new RedisCommand<ListScanResult<Object>>("ZSCAN", new NestedMultiDecoder(new ObjectListReplayDecoder<Object>(), new ScoredSortedSetScanReplayDecoder()), ValueType.OBJECT);
RedisStrictCommand<Double> ZINCRBY = new RedisStrictCommand<Double>("ZINCRBY", new DoubleReplayConvertor()); RedisStrictCommand<Double> ZINCRBY = new RedisStrictCommand<Double>("ZINCRBY", new DoubleReplayConvertor());

@ -21,6 +21,12 @@ import org.redisson.client.protocol.ScoredEntry;
public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<V>, RExpirable { public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<V>, RExpirable {
Collection<V> lexRangeTail(V fromElement, boolean fromInclusive);
Collection<V> lexRangeHead(V toElement, boolean toInclusive);
Collection<V> lexRange(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive);
Integer lexCount(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive); Integer lexCount(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive);
Integer rank(V o); Integer rank(V o);

@ -23,6 +23,12 @@ import io.netty.util.concurrent.Future;
public interface RScoredSortedSetAsync<V> extends RExpirableAsync { public interface RScoredSortedSetAsync<V> extends RExpirableAsync {
Future<Collection<V>> lexRangeTailAsync(V fromElement, boolean fromInclusive);
Future<Collection<V>> lexRangeHeadAsync(V toElement, boolean toInclusive);
Future<Collection<V>> lexRangeAsync(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive);
Future<Integer> lexCountAsync(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive); Future<Integer> lexCountAsync(V fromElement, boolean fromInclusive, V toElement, boolean toInclusive);
Future<Integer> rankAsync(V o); Future<Integer> rankAsync(V o);

@ -21,6 +21,52 @@ import io.netty.util.concurrent.Future;
public class RedissonScoredSortedSetTest extends BaseTest { public class RedissonScoredSortedSetTest extends BaseTest {
@Test
public void testLexRangeTail() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(0, "a");
set.add(0, "b");
set.add(0, "c");
set.add(0, "d");
set.add(0, "e");
set.add(0, "f");
set.add(0, "g");
MatcherAssert.assertThat(set.lexRangeTail("c", false), Matchers.contains("d", "e", "f", "g"));
MatcherAssert.assertThat(set.lexRangeTail("c", true), Matchers.contains("c", "d", "e", "f", "g"));
}
@Test
public void testLexRangeHead() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(0, "a");
set.add(0, "b");
set.add(0, "c");
set.add(0, "d");
set.add(0, "e");
set.add(0, "f");
set.add(0, "g");
MatcherAssert.assertThat(set.lexRangeHead("c", false), Matchers.contains("a", "b"));
MatcherAssert.assertThat(set.lexRangeHead("c", true), Matchers.contains("a", "b", "c"));
}
@Test
public void testLexRange() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(0, "a");
set.add(0, "b");
set.add(0, "c");
set.add(0, "d");
set.add(0, "e");
set.add(0, "f");
set.add(0, "g");
MatcherAssert.assertThat(set.lexRange("aaa", true, "g", false), Matchers.contains("b", "c", "d", "e", "f"));
}
@Test @Test
public void testRank() { public void testRank() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple"); RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");

Loading…
Cancel
Save