Implemented ZUNIONSTORE and ZINTERSTORE

This commit is contained in:
Jonathan Leibiusky
2010-08-07 20:28:26 -03:00
parent 84f4c45004
commit 4a7697911d
5 changed files with 168 additions and 5 deletions

View File

@@ -473,4 +473,42 @@ public class Client extends Connection {
sendCommand("ZREMRANGEBYSCORE", key, String.valueOf(start), String
.valueOf(end));
}
}
public void zunionstore(String dstkey, String... sets) {
String[] params = new String[sets.length + 2];
params[0] = dstkey;
params[1] = String.valueOf(sets.length);
System.arraycopy(sets, 0, params, 2, sets.length);
sendCommand("ZUNIONSTORE", params);
}
public void zunionstore(String dstkey, ZParams params, String... sets) {
List<String> args = new ArrayList<String>();
args.add(dstkey);
args.add(String.valueOf(sets.length));
for (String set : sets) {
args.add(set);
}
args.addAll(params.getParams());
sendCommand("ZUNIONSTORE", args.toArray(new String[args.size()]));
}
public void zinterstore(String dstkey, String... sets) {
String[] params = new String[sets.length + 2];
params[0] = dstkey;
params[1] = String.valueOf(sets.length);
System.arraycopy(sets, 0, params, 2, sets.length);
sendCommand("ZINTERSTORE", params);
}
public void zinterstore(String dstkey, ZParams params, String... sets) {
List<String> args = new ArrayList<String>();
args.add(dstkey);
args.add(String.valueOf(sets.length));
for (String set : sets) {
args.add(set);
}
args.addAll(params.getParams());
sendCommand("ZINTERSTORE", args.toArray(new String[args.size()]));
}
}

View File

@@ -582,4 +582,24 @@ public class Jedis {
client.zremrangeByScore(key, start, end);
return client.getIntegerReply();
}
public int zunionstore(String dstkey, String... sets) {
client.zunionstore(dstkey, sets);
return client.getIntegerReply();
}
public int zunionstore(String dstkey, ZParams params, String... sets) {
client.zunionstore(dstkey, params, sets);
return client.getIntegerReply();
}
public int zinterstore(String dstkey, String... sets) {
client.zinterstore(dstkey, sets);
return client.getIntegerReply();
}
public int zinterstore(String dstkey, ZParams params, String... sets) {
client.zinterstore(dstkey, params, sets);
return client.getIntegerReply();
}
}

View File

@@ -0,0 +1,33 @@
package redis.clients.jedis;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class ZParams {
public enum Aggregate {
SUM, MIN, MAX
}
private List<String> params = new ArrayList<String>();
public ZParams weights(int... weights) {
params.add("WEIGHTS");
for (int weight : weights) {
params.add(String.valueOf(weight));
}
return this;
}
public Collection<String> getParams() {
return Collections.unmodifiableCollection(params);
}
public ZParams aggregate(Aggregate aggregate) {
params.add("AGGREGATE");
params.add(aggregate.name());
return this;
}
}

View File

@@ -6,6 +6,7 @@ import java.util.Set;
import org.junit.Test;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;
public class SortedSetCommandsTest extends JedisCommandTestBase {
@Test
@@ -287,4 +288,78 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
assertEquals(expected, jedis.zrange("foo", 0, 100));
}
@Test
public void zunionstore() {
jedis.zadd("foo", 1, "a");
jedis.zadd("foo", 2, "b");
jedis.zadd("bar", 2, "a");
jedis.zadd("bar", 2, "b");
int result = jedis.zunionstore("dst", "foo", "bar");
assertEquals(2, result);
Set<Tuple> expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("b", 4));
expected.add(new Tuple("a", 3));
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
}
@Test
public void zunionstoreParams() {
jedis.zadd("foo", 1, "a");
jedis.zadd("foo", 2, "b");
jedis.zadd("bar", 2, "a");
jedis.zadd("bar", 2, "b");
ZParams params = new ZParams();
params.weights(2, 2);
params.aggregate(ZParams.Aggregate.SUM);
int result = jedis.zunionstore("dst", params, "foo", "bar");
assertEquals(2, result);
Set<Tuple> expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("b", 8));
expected.add(new Tuple("a", 6));
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
}
@Test
public void zinterstore() {
jedis.zadd("foo", 1, "a");
jedis.zadd("foo", 2, "b");
jedis.zadd("bar", 2, "a");
int result = jedis.zinterstore("dst", "foo", "bar");
assertEquals(1, result);
Set<Tuple> expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("a", 3));
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
}
@Test
public void zintertoreParams() {
jedis.zadd("foo", 1, "a");
jedis.zadd("foo", 2, "b");
jedis.zadd("bar", 2, "a");
ZParams params = new ZParams();
params.weights(2, 2);
params.aggregate(ZParams.Aggregate.SUM);
int result = jedis.zinterstore("dst", params, "foo", "bar");
assertEquals(1, result);
Set<Tuple> expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("a", 6));
assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100));
}
}