RScoredSortedSet implements RSortable interface #703

pull/748/head
Nikita 8 years ago
parent 89606bfe28
commit 9db81f0bb6

@ -25,10 +25,12 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.redisson.api.RFuture;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.SortOrder;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.DoubleCodec;
import org.redisson.client.codec.LongCodec;
@ -41,6 +43,12 @@ import org.redisson.client.protocol.convertor.BooleanReplayConvertor;
import org.redisson.client.protocol.decoder.ListScanResult;
import org.redisson.command.CommandAsyncExecutor;
/**
*
* @author Nikita Koksharov
*
* @param <V> value type
*/
public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RScoredSortedSet<V> {
public RedissonScoredSortedSet(CommandAsyncExecutor commandExecutor, String name) {
@ -627,5 +635,168 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.ZUNIONSTORE_INT, args.toArray());
}
@Override
public Set<V> readSort(SortOrder order) {
return get(readSortAsync(order));
}
@Override
public RFuture<Set<V>> readSortAsync(SortOrder order) {
return commandExecutor.readAsync(getName(), codec, RedisCommands.SORT_SET, getName(), order);
}
@Override
public Set<V> readSort(SortOrder order, int offset, int count) {
return get(readSortAsync(order, offset, count));
}
@Override
public RFuture<Set<V>> readSortAsync(SortOrder order, int offset, int count) {
return commandExecutor.readAsync(getName(), codec, RedisCommands.SORT_SET, getName(), "LIMIT", offset, count, order);
}
@Override
public Set<V> readSort(String byPattern, SortOrder order) {
return get(readSortAsync(byPattern, order));
}
@Override
public RFuture<Set<V>> readSortAsync(String byPattern, SortOrder order) {
return commandExecutor.readAsync(getName(), codec, RedisCommands.SORT_SET, getName(), "BY", byPattern, order);
}
@Override
public Set<V> readSort(String byPattern, SortOrder order, int offset, int count) {
return get(readSortAsync(byPattern, order, offset, count));
}
@Override
public RFuture<Set<V>> readSortAsync(String byPattern, SortOrder order, int offset, int count) {
return commandExecutor.readAsync(getName(), codec, RedisCommands.SORT_SET, getName(), "BY", byPattern, "LIMIT", offset, count, order);
}
@Override
public <T> Collection<T> readSort(String byPattern, List<String> getPatterns, SortOrder order) {
return (Collection<T>)get(readSortAsync(byPattern, getPatterns, order));
}
@Override
public <T> RFuture<Collection<T>> readSortAsync(String byPattern, List<String> getPatterns, SortOrder order) {
return readSortAsync(byPattern, getPatterns, order, -1, -1);
}
@Override
public <T> Collection<T> readSort(String byPattern, List<String> getPatterns, SortOrder order, int offset, int count) {
return (Collection<T>)get(readSortAsync(byPattern, getPatterns, order, offset, count));
}
@Override
public <T> RFuture<Collection<T>> readSortAsync(String byPattern, List<String> getPatterns, SortOrder order, int offset, int count) {
List<Object> params = new ArrayList<Object>();
params.add(getName());
if (byPattern != null) {
params.add("BY");
params.add(byPattern);
}
if (offset != -1 && count != -1) {
params.add("LIMIT");
}
if (offset != -1) {
params.add(offset);
}
if (count != -1) {
params.add(count);
}
for (String pattern : getPatterns) {
params.add("GET");
params.add(pattern);
}
params.add(order);
return commandExecutor.readAsync(getName(), codec, RedisCommands.SORT_SET, params.toArray());
}
@Override
public int sortTo(String destName, SortOrder order) {
return get(sortToAsync(destName, order));
}
@Override
public RFuture<Integer> sortToAsync(String destName, SortOrder order) {
return sortToAsync(destName, null, Collections.<String>emptyList(), order, -1, -1);
}
@Override
public int sortTo(String destName, SortOrder order, int offset, int count) {
return get(sortToAsync(destName, order, offset, count));
}
@Override
public RFuture<Integer> sortToAsync(String destName, SortOrder order, int offset, int count) {
return sortToAsync(destName, null, Collections.<String>emptyList(), order, offset, count);
}
@Override
public int sortTo(String destName, String byPattern, SortOrder order, int offset, int count) {
return get(sortToAsync(destName, byPattern, order, offset, count));
}
@Override
public int sortTo(String destName, String byPattern, SortOrder order) {
return get(sortToAsync(destName, byPattern, order));
}
@Override
public RFuture<Integer> sortToAsync(String destName, String byPattern, SortOrder order) {
return sortToAsync(destName, byPattern, Collections.<String>emptyList(), order, -1, -1);
}
@Override
public RFuture<Integer> sortToAsync(String destName, String byPattern, SortOrder order, int offset, int count) {
return sortToAsync(destName, byPattern, Collections.<String>emptyList(), order, offset, count);
}
@Override
public int sortTo(String destName, String byPattern, List<String> getPatterns, SortOrder order) {
return get(sortToAsync(destName, byPattern, getPatterns, order));
}
@Override
public RFuture<Integer> sortToAsync(String destName, String byPattern, List<String> getPatterns, SortOrder order) {
return sortToAsync(destName, byPattern, getPatterns, order, -1, -1);
}
@Override
public int sortTo(String destName, String byPattern, List<String> getPatterns, SortOrder order, int offset, int count) {
return get(sortToAsync(destName, byPattern, getPatterns, order, offset, count));
}
@Override
public RFuture<Integer> sortToAsync(String destName, String byPattern, List<String> getPatterns, SortOrder order, int offset, int count) {
List<Object> params = new ArrayList<Object>();
params.add(getName());
if (byPattern != null) {
params.add("BY");
params.add(byPattern);
}
if (offset != -1 && count != -1) {
params.add("LIMIT");
}
if (offset != -1) {
params.add(offset);
}
if (count != -1) {
params.add(count);
}
for (String pattern : getPatterns) {
params.add("GET");
params.add(pattern);
}
params.add(order);
params.add("STORE");
params.add(destName);
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SORT_TO, params.toArray());
}
}

@ -17,6 +17,7 @@ package org.redisson.api;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.redisson.client.protocol.ScoredEntry;
@ -26,7 +27,7 @@ import org.redisson.client.protocol.ScoredEntry;
*
* @param <V> value
*/
public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<V>, RExpirable {
public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<V>, RExpirable, RSortable<Set<V>> {
public enum Aggregate {

@ -17,6 +17,7 @@ package org.redisson.api;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
@ -27,7 +28,7 @@ import org.redisson.client.protocol.ScoredEntry;
*
* @param <V> value
*/
public interface RScoredSortedSetAsync<V> extends RExpirableAsync {
public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsync<Set<V>> {
RFuture<V> pollLastAsync();

@ -18,12 +18,178 @@ import org.junit.Assume;
import org.junit.Test;
import org.redisson.api.RFuture;
import org.redisson.api.RLexSortedSet;
import org.redisson.api.RList;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RSortedSet;
import org.redisson.api.SortOrder;
import org.redisson.client.codec.IntegerCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.ScoredEntry;
public class RedissonScoredSortedSetTest extends BaseTest {
@Test
public void testSortOrder() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", IntegerCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
Set<Integer> descSort = set.readSort(SortOrder.DESC);
assertThat(descSort).containsExactly(3, 2, 1);
Set<Integer> ascSort = set.readSort(SortOrder.ASC);
assertThat(ascSort).containsExactly(1, 2, 3);
}
@Test
public void testSortOrderLimit() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", IntegerCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
Set<Integer> descSort = set.readSort(SortOrder.DESC, 1, 2);
assertThat(descSort).containsExactly(2, 1);
Set<Integer> ascSort = set.readSort(SortOrder.ASC, 1, 2);
assertThat(ascSort).containsExactly(2, 3);
}
@Test
public void testSortOrderByPattern() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", IntegerCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
redisson.getBucket("test1", IntegerCodec.INSTANCE).set(3);
redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);
Set<Integer> descSort = set.readSort("test*", SortOrder.DESC);
assertThat(descSort).containsExactly(1, 2, 3);
Set<Integer> ascSort = set.readSort("test*", SortOrder.ASC);
assertThat(ascSort).containsExactly(3, 2, 1);
}
@Test
public void testSortOrderByPatternLimit() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", IntegerCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
redisson.getBucket("test1", IntegerCodec.INSTANCE).set(3);
redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);
Set<Integer> descSort = set.readSort("test*", SortOrder.DESC, 1, 2);
assertThat(descSort).containsExactly(2, 3);
Set<Integer> ascSort = set.readSort("test*", SortOrder.ASC, 1, 2);
assertThat(ascSort).containsExactly(2, 1);
}
@Test
public void testSortOrderByPatternGet() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", StringCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
redisson.getBucket("test1", IntegerCodec.INSTANCE).set(1);
redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
redisson.getBucket("test3", IntegerCodec.INSTANCE).set(3);
redisson.getBucket("tester1", StringCodec.INSTANCE).set("obj1");
redisson.getBucket("tester2", StringCodec.INSTANCE).set("obj2");
redisson.getBucket("tester3", StringCodec.INSTANCE).set("obj3");
Collection<String> descSort = set.readSort("test*", Arrays.asList("tester*"), SortOrder.DESC);
assertThat(descSort).containsExactly("obj3", "obj2", "obj1");
Collection<String> ascSort = set.readSort("test*", Arrays.asList("tester*"), SortOrder.ASC);
assertThat(ascSort).containsExactly("obj1", "obj2", "obj3");
}
@Test
public void testSortOrderByPatternGetLimit() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", StringCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
redisson.getBucket("test1", IntegerCodec.INSTANCE).set(1);
redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
redisson.getBucket("test3", IntegerCodec.INSTANCE).set(3);
redisson.getBucket("tester1", StringCodec.INSTANCE).set("obj1");
redisson.getBucket("tester2", StringCodec.INSTANCE).set("obj2");
redisson.getBucket("tester3", StringCodec.INSTANCE).set("obj3");
Collection<String> descSort = set.readSort("test*", Arrays.asList("tester*"), SortOrder.DESC, 1, 2);
assertThat(descSort).containsExactly("obj2", "obj1");
Collection<String> ascSort = set.readSort("test*", Arrays.asList("tester*"), SortOrder.ASC, 1, 2);
assertThat(ascSort).containsExactly("obj2", "obj3");
}
@Test
public void testSortTo() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", IntegerCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
assertThat(set.sortTo("test3", SortOrder.DESC)).isEqualTo(3);
RList<String> list2 = redisson.getList("test3", StringCodec.INSTANCE);
assertThat(list2).containsExactly("3", "2", "1");
assertThat(set.sortTo("test4", SortOrder.ASC)).isEqualTo(3);
RList<String> list3 = redisson.getList("test4", StringCodec.INSTANCE);
assertThat(list3).containsExactly("1", "2", "3");
}
@Test
public void testSortToLimit() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", IntegerCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
assertThat(set.sortTo("test3", SortOrder.DESC, 1, 2)).isEqualTo(2);
RList<String> list2 = redisson.getList("test3", StringCodec.INSTANCE);
assertThat(list2).containsExactly("2", "1");
assertThat(set.sortTo("test4", SortOrder.ASC, 1, 2)).isEqualTo(2);
RList<String> list3 = redisson.getList("test4", StringCodec.INSTANCE);
assertThat(list3).containsExactly("2", "3");
}
@Test
public void testSortToByPattern() {
RScoredSortedSet<Integer> set = redisson.getScoredSortedSet("list", IntegerCodec.INSTANCE);
set.add(10, 1);
set.add(9, 2);
set.add(8, 3);
redisson.getBucket("test1", IntegerCodec.INSTANCE).set(3);
redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);
assertThat(set.sortTo("tester3", "test*", SortOrder.DESC, 1, 2)).isEqualTo(2);
RList<String> list2 = redisson.getList("tester3", StringCodec.INSTANCE);
assertThat(list2).containsExactly("2", "3");
assertThat(set.sortTo("tester4", "test*", SortOrder.ASC, 1, 2)).isEqualTo(2);
RList<String> list3 = redisson.getList("tester4", StringCodec.INSTANCE);
assertThat(list3).containsExactly("2", "1");
}
@Test
public void testCount() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");

Loading…
Cancel
Save