Added commands to operate on sets
This commit is contained in:
@@ -7,7 +7,6 @@ Jedis was conceived to be EASY to use and FULLY COMPATIBLE with the latest versi
|
|||||||
Jedis is a WORK IN PROGRESS.
|
Jedis is a WORK IN PROGRESS.
|
||||||
|
|
||||||
## What's still missing?
|
## What's still missing?
|
||||||
- Commands operating on sets
|
|
||||||
- Commands operating on sorted sets
|
- Commands operating on sorted sets
|
||||||
- Sorting
|
- Sorting
|
||||||
- Transactions
|
- Transactions
|
||||||
@@ -24,6 +23,7 @@ But stay close because things are going fast and all this will be implemented so
|
|||||||
- Commands operating on string values
|
- Commands operating on string values
|
||||||
- Commands operating on hashes
|
- Commands operating on hashes
|
||||||
- Commands operating on lists (not SORT, BLPOP, BRPOP)
|
- Commands operating on lists (not SORT, BLPOP, BRPOP)
|
||||||
|
- Commands operating on sets
|
||||||
|
|
||||||
## How do I use it?
|
## How do I use it?
|
||||||
|
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>redis.clients</groupId>
|
<groupId>redis.clients</groupId>
|
||||||
<artifactId>jedis</artifactId>
|
<artifactId>jedis</artifactId>
|
||||||
<version>0.0.5</version>
|
<version>0.0.6</version>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|||||||
@@ -276,4 +276,65 @@ public class Jedis extends Client {
|
|||||||
List<String> members = sendCommand("SMEMBERS", key).getMultiBulkReply();
|
List<String> members = sendCommand("SMEMBERS", key).getMultiBulkReply();
|
||||||
return new LinkedHashSet<String>(members);
|
return new LinkedHashSet<String>(members);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int srem(String key, String member) throws JedisException {
|
||||||
|
return sendCommand("SREM", key, member).getIntegerReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String spop(String key) throws JedisException {
|
||||||
|
return sendCommand("SPOP", key).getBulkReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int smove(String srckey, String dstkey, String member)
|
||||||
|
throws JedisException {
|
||||||
|
return sendCommand("SMOVE", srckey, dstkey, member).getIntegerReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int scard(String key) throws JedisException {
|
||||||
|
return sendCommand("SCARD", key).getIntegerReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int sismember(String key, String member) throws JedisException {
|
||||||
|
return sendCommand("SISMEMBER", key, member).getIntegerReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> sinter(String... keys) throws JedisException {
|
||||||
|
List<String> members = sendCommand("SINTER", keys).getMultiBulkReply();
|
||||||
|
return new LinkedHashSet<String>(members);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int sinterstore(String dstkey, String... keys) throws JedisException {
|
||||||
|
String[] params = new String[keys.length + 1];
|
||||||
|
params[0] = dstkey;
|
||||||
|
System.arraycopy(keys, 0, params, 1, keys.length);
|
||||||
|
return sendCommand("SINTERSTORE", params).getIntegerReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> sunion(String... keys) throws JedisException {
|
||||||
|
List<String> members = sendCommand("SUNION", keys).getMultiBulkReply();
|
||||||
|
return new LinkedHashSet<String>(members);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int sunionstore(String dstkey, String... keys) throws JedisException {
|
||||||
|
String[] params = new String[keys.length + 1];
|
||||||
|
params[0] = dstkey;
|
||||||
|
System.arraycopy(keys, 0, params, 1, keys.length);
|
||||||
|
return sendCommand("SUNIONSTORE", params).getIntegerReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> sdiff(String... keys) throws JedisException {
|
||||||
|
List<String> members = sendCommand("SDIFF", keys).getMultiBulkReply();
|
||||||
|
return new LinkedHashSet<String>(members);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int sdiffstore(String dstkey, String... keys) throws JedisException {
|
||||||
|
String[] params = new String[keys.length + 1];
|
||||||
|
params[0] = dstkey;
|
||||||
|
System.arraycopy(keys, 0, params, 1, keys.length);
|
||||||
|
return sendCommand("SDIFFSTORE", params).getIntegerReply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String srandmember(String key) throws JedisException {
|
||||||
|
return sendCommand("SRANDMEMBER", key).getBulkReply();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,4 +50,208 @@ public class SetCommandsTest extends Assert {
|
|||||||
assertEquals(expected, members);
|
assertEquals(expected, members);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Test
|
||||||
|
public void srem() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
int status = jedis.srem("foo", "a");
|
||||||
|
|
||||||
|
Set<String> expected = new LinkedHashSet<String>();
|
||||||
|
expected.add("b");
|
||||||
|
|
||||||
|
assertEquals(1, status);
|
||||||
|
assertEquals(expected, jedis.smembers("foo"));
|
||||||
|
|
||||||
|
status = jedis.srem("foo", "bar");
|
||||||
|
|
||||||
|
assertEquals(0, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void spop() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
String member = jedis.spop("foo");
|
||||||
|
|
||||||
|
assertTrue("a".equals(member) || "b".equals(member));
|
||||||
|
assertEquals(1, jedis.smembers("foo").size());
|
||||||
|
|
||||||
|
member = jedis.spop("bar");
|
||||||
|
assertNull(member);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void smove() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
jedis.sadd("bar", "c");
|
||||||
|
|
||||||
|
int status = jedis.smove("foo", "bar", "a");
|
||||||
|
|
||||||
|
Set<String> expectedSrc = new LinkedHashSet<String>();
|
||||||
|
expectedSrc.add("b");
|
||||||
|
|
||||||
|
Set<String> expectedDst = new LinkedHashSet<String>();
|
||||||
|
expectedDst.add("c");
|
||||||
|
expectedDst.add("a");
|
||||||
|
|
||||||
|
assertEquals(status, 1);
|
||||||
|
assertEquals(expectedSrc, jedis.smembers("foo"));
|
||||||
|
assertEquals(expectedDst, jedis.smembers("bar"));
|
||||||
|
|
||||||
|
status = jedis.smove("foo", "bar", "a");
|
||||||
|
assertEquals(status, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void scard() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
int card = jedis.scard("foo");
|
||||||
|
|
||||||
|
assertEquals(2, card);
|
||||||
|
|
||||||
|
card = jedis.scard("bar");
|
||||||
|
assertEquals(0, card);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sismember() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
int status = jedis.sismember("foo", "a");
|
||||||
|
assertEquals(1, status);
|
||||||
|
|
||||||
|
status = jedis.sismember("foo", "c");
|
||||||
|
assertEquals(0, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sinter() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
jedis.sadd("bar", "b");
|
||||||
|
jedis.sadd("bar", "c");
|
||||||
|
|
||||||
|
Set<String> expected = new LinkedHashSet<String>();
|
||||||
|
expected.add("b");
|
||||||
|
|
||||||
|
Set<String> intersection = jedis.sinter("foo", "bar");
|
||||||
|
assertEquals(expected, intersection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sinterstore() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
jedis.sadd("bar", "b");
|
||||||
|
jedis.sadd("bar", "c");
|
||||||
|
|
||||||
|
Set<String> expected = new LinkedHashSet<String>();
|
||||||
|
expected.add("b");
|
||||||
|
|
||||||
|
int status = jedis.sinterstore("car", "foo", "bar");
|
||||||
|
assertEquals(1, status);
|
||||||
|
|
||||||
|
assertEquals(expected, jedis.smembers("car"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sunion() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
jedis.sadd("bar", "b");
|
||||||
|
jedis.sadd("bar", "c");
|
||||||
|
|
||||||
|
Set<String> expected = new LinkedHashSet<String>();
|
||||||
|
expected.add("a");
|
||||||
|
expected.add("b");
|
||||||
|
expected.add("c");
|
||||||
|
|
||||||
|
Set<String> union = jedis.sunion("foo", "bar");
|
||||||
|
assertEquals(expected, union);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sunionstore() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
jedis.sadd("bar", "b");
|
||||||
|
jedis.sadd("bar", "c");
|
||||||
|
|
||||||
|
Set<String> expected = new LinkedHashSet<String>();
|
||||||
|
expected.add("a");
|
||||||
|
expected.add("b");
|
||||||
|
expected.add("c");
|
||||||
|
|
||||||
|
int status = jedis.sunionstore("car", "foo", "bar");
|
||||||
|
assertEquals(3, status);
|
||||||
|
|
||||||
|
assertEquals(expected, jedis.smembers("car"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sdiff() throws JedisException {
|
||||||
|
jedis.sadd("foo", "x");
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
jedis.sadd("foo", "c");
|
||||||
|
|
||||||
|
jedis.sadd("bar", "c");
|
||||||
|
|
||||||
|
jedis.sadd("car", "a");
|
||||||
|
jedis.sadd("car", "d");
|
||||||
|
|
||||||
|
Set<String> expected = new LinkedHashSet<String>();
|
||||||
|
expected.add("x");
|
||||||
|
expected.add("b");
|
||||||
|
|
||||||
|
Set<String> diff = jedis.sdiff("foo", "bar", "car");
|
||||||
|
assertEquals(expected, diff);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sdiffstore() throws JedisException {
|
||||||
|
jedis.sadd("foo", "x");
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
jedis.sadd("foo", "c");
|
||||||
|
|
||||||
|
jedis.sadd("bar", "c");
|
||||||
|
|
||||||
|
jedis.sadd("car", "a");
|
||||||
|
jedis.sadd("car", "d");
|
||||||
|
|
||||||
|
Set<String> expected = new LinkedHashSet<String>();
|
||||||
|
expected.add("d");
|
||||||
|
expected.add("a");
|
||||||
|
|
||||||
|
int status = jedis.sdiffstore("tar", "foo", "bar", "car");
|
||||||
|
assertEquals(2, status);
|
||||||
|
assertEquals(expected, jedis.smembers("car"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void srandmember() throws JedisException {
|
||||||
|
jedis.sadd("foo", "a");
|
||||||
|
jedis.sadd("foo", "b");
|
||||||
|
|
||||||
|
String member = jedis.srandmember("foo");
|
||||||
|
|
||||||
|
assertTrue("a".equals(member) || "b".equals(member));
|
||||||
|
assertEquals(2, jedis.smembers("foo").size());
|
||||||
|
|
||||||
|
member = jedis.srandmember("bar");
|
||||||
|
assertNull(member);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user