RScoredSortedSet.intersection methods implemented. #595

pull/653/head
Nikita 9 years ago
parent 7fd29a2b49
commit dd02c3f55a

@ -31,6 +31,7 @@ import org.redisson.api.RFuture;
import org.redisson.api.RScoredSortedSet;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.DoubleCodec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.ScoredCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommand.ValueType;
@ -488,4 +489,58 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZCOUNT, getName(), startValue, endValue);
}
@Override
public int intersection(String... names) {
return get(intersectionAsync(names));
}
public RFuture<Integer> intersectionAsync(String... names) {
return intersectionAsync(Aggregate.SUM, names);
}
@Override
public int intersection(Aggregate aggregate, String... names) {
return get(intersectionAsync(aggregate, names));
}
public RFuture<Integer> intersectionAsync(Aggregate aggregate, String... names) {
List<Object> args = new ArrayList<Object>(names.length + 4);
args.add(getName());
args.add(names.length);
args.addAll(Arrays.asList(names));
args.add("AGGREGATE");
args.add(aggregate.name());
return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.ZINTERSTORE_INT, args.toArray());
}
@Override
public int intersection(Map<String, Double> nameWithWeight) {
return get(intersectionAsync(nameWithWeight));
}
public RFuture<Integer> intersectionAsync(Map<String, Double> nameWithWeight) {
return intersectionAsync(Aggregate.SUM, nameWithWeight);
}
@Override
public int intersection(Aggregate aggregate, Map<String, Double> nameWithWeight) {
return get(intersectionAsync(aggregate, nameWithWeight));
}
public RFuture<Integer> intersectionAsync(Aggregate aggregate, Map<String, Double> nameWithWeight) {
List<Object> args = new ArrayList<Object>(nameWithWeight.size()*2 + 5);
args.add(getName());
args.add(nameWithWeight.size());
args.addAll(nameWithWeight.keySet());
args.add("WEIGHTS");
List<String> weights = new ArrayList<String>();
for (Double weight : nameWithWeight.values()) {
weights.add(BigDecimal.valueOf(weight).toPlainString());
}
args.addAll(weights);
args.add("AGGREGATE");
args.add(aggregate.name());
return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.ZINTERSTORE_INT, args.toArray());
}
}

@ -18,10 +18,17 @@ package org.redisson.api;
import java.util.Collection;
import java.util.Map;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<V>, RExpirable {
public enum Aggregate {
SUM, MAX, MIN
}
V pollFirst();
V pollLast();
@ -131,5 +138,13 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
* @return
*/
Collection<V> readAll();
int intersection(String... names);
int intersection(Aggregate aggregate, String... names);
int intersection(Map<String, Double> nameWithWeight);
int intersection(Aggregate aggregate, Map<String, Double> nameWithWeight);
}

@ -18,6 +18,7 @@ package org.redisson.api;
import java.util.Collection;
import java.util.Map;
import org.redisson.api.RScoredSortedSet.Aggregate;
import org.redisson.client.protocol.ScoredEntry;
public interface RScoredSortedSetAsync<V> extends RExpirableAsync {
@ -111,5 +112,13 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync {
* @return
*/
RFuture<Collection<V>> readAllAsync();
RFuture<Integer> intersectionAsync(String... names);
RFuture<Integer> intersectionAsync(Aggregate aggregate, String... names);
RFuture<Integer> intersectionAsync(Map<String, Double> nameWithWeight);
RFuture<Integer> intersectionAsync(Aggregate aggregate, Map<String, Double> nameWithWeight);
}

@ -86,6 +86,7 @@ public interface RedisCommands {
RedisStrictCommand<Void> ASKING = new RedisStrictCommand<Void>("ASKING", new VoidReplayConvertor());
RedisStrictCommand<Void> READONLY = new RedisStrictCommand<Void>("READONLY", new VoidReplayConvertor());
RedisStrictCommand<Integer> ZINTERSTORE_INT = new RedisStrictCommand<Integer>("ZINTERSTORE", new IntegerReplayConvertor());
RedisCommand<Boolean> ZADD_BOOL = new RedisCommand<Boolean>("ZADD", new BooleanAmountReplayConvertor(), 3);
RedisCommand<Boolean> ZADD_NX_BOOL = new RedisCommand<Boolean>("ZADD", new BooleanAmountReplayConvertor(), 4);
RedisCommand<Boolean> ZADD_BOOL_RAW = new RedisCommand<Boolean>("ZADD", new BooleanAmountReplayConvertor());

@ -754,4 +754,45 @@ public class RedissonScoredSortedSetTest extends BaseTest {
Assert.assertTrue(new Double(112.3).compareTo(res2) == 0);
}
@Test
public void testIntersection() {
RScoredSortedSet<String> set1 = redisson.getScoredSortedSet("simple1");
set1.add(1, "one");
set1.add(2, "two");
RScoredSortedSet<String> set2 = redisson.getScoredSortedSet("simple2");
set2.add(1, "one");
set2.add(2, "two");
set2.add(3, "three");
RScoredSortedSet<String> out = redisson.getScoredSortedSet("out");
assertThat(out.intersection(set1.getName(), set2.getName())).isEqualTo(2);
assertThat(out.readAll()).containsOnly("one", "two");
assertThat(out.getScore("one")).isEqualTo(2);
assertThat(out.getScore("two")).isEqualTo(4);
}
@Test
public void testIntersectionWithWeight() {
RScoredSortedSet<String> set1 = redisson.getScoredSortedSet("simple1");
set1.add(1, "one");
set1.add(2, "two");
RScoredSortedSet<String> set2 = redisson.getScoredSortedSet("simple2");
set2.add(1, "one");
set2.add(2, "two");
set2.add(3, "three");
RScoredSortedSet<String> out = redisson.getScoredSortedSet("out");
Map<String, Double> nameWithWeight = new HashMap<>();
nameWithWeight.put(set1.getName(), 2D);
nameWithWeight.put(set2.getName(), 3D);
assertThat(out.intersection(nameWithWeight)).isEqualTo(2);
assertThat(out.readAll()).containsOnly("one", "two");
assertThat(out.getScore("one")).isEqualTo(5);
assertThat(out.getScore("two")).isEqualTo(10);
}
}

Loading…
Cancel
Save