Added commands operating on sorted sets
This commit is contained in:
@@ -7,13 +7,12 @@ Jedis was conceived to be EASY to use and FULLY COMPATIBLE with the latest versi
|
||||
Jedis is a WORK IN PROGRESS.
|
||||
|
||||
## What's still missing?
|
||||
- Commands operating on sorted sets
|
||||
- Sorting
|
||||
- Transactions
|
||||
- Publish/Subscribe
|
||||
- Persistence control commands
|
||||
- Remote server control commands
|
||||
- The AUTH, SORT, BLPOP and BRPOP commands
|
||||
- The AUTH, SORT, BLPOP, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands
|
||||
|
||||
But stay close because things are going fast and all this will be implemented soon!
|
||||
|
||||
@@ -24,6 +23,7 @@ But stay close because things are going fast and all this will be implemented so
|
||||
- Commands operating on hashes
|
||||
- Commands operating on lists (not SORT, BLPOP, BRPOP)
|
||||
- Commands operating on sets
|
||||
- Commands operating on sorted sets (not SORT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE)
|
||||
|
||||
## How do I use it?
|
||||
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>0.0.6</version>
|
||||
<version>0.0.7</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
||||
@@ -337,4 +337,82 @@ public class Jedis extends Client {
|
||||
public String srandmember(String key) throws JedisException {
|
||||
return sendCommand("SRANDMEMBER", key).getBulkReply();
|
||||
}
|
||||
|
||||
public int zadd(String key, double score, String member)
|
||||
throws JedisException {
|
||||
return sendCommand("ZADD", key, String.valueOf(score), member)
|
||||
.getIntegerReply();
|
||||
}
|
||||
|
||||
public Set<String> zrange(String key, int start, int end)
|
||||
throws JedisException {
|
||||
List<String> members = sendCommand("ZRANGE", key,
|
||||
String.valueOf(start), String.valueOf(end)).getMultiBulkReply();
|
||||
return new LinkedHashSet<String>(members);
|
||||
}
|
||||
|
||||
public int zrem(String key, String member) throws JedisException {
|
||||
return sendCommand("ZREM", key, member).getIntegerReply();
|
||||
}
|
||||
|
||||
public double zincrby(String key, double score, String member)
|
||||
throws JedisException {
|
||||
String newscore = sendCommand("ZINCRBY", key, String.valueOf(score),
|
||||
member).getBulkReply();
|
||||
return Double.valueOf(newscore);
|
||||
}
|
||||
|
||||
public int zrank(String key, String member) throws JedisException {
|
||||
return sendCommand("ZRANK", key, member).getIntegerReply();
|
||||
}
|
||||
|
||||
public int zrevrank(String key, String member) throws JedisException {
|
||||
return sendCommand("ZREVRANK", key, member).getIntegerReply();
|
||||
}
|
||||
|
||||
public Set<String> zrevrange(String key, int start, int end)
|
||||
throws JedisException {
|
||||
List<String> members = sendCommand("ZREVRANGE", key,
|
||||
String.valueOf(start), String.valueOf(end)).getMultiBulkReply();
|
||||
return new LinkedHashSet<String>(members);
|
||||
}
|
||||
|
||||
public Set<Tuple> zrangeWithScores(String key, int start, int end)
|
||||
throws JedisException {
|
||||
List<String> membersWithScores = sendCommand("ZRANGE", key,
|
||||
String.valueOf(start), String.valueOf(end), "WITHSCORES")
|
||||
.getMultiBulkReply();
|
||||
Set<Tuple> set = new LinkedHashSet<Tuple>();
|
||||
Iterator<String> iterator = membersWithScores.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
set
|
||||
.add(new Tuple(iterator.next(), Double.valueOf(iterator
|
||||
.next())));
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
public Set<Tuple> zrevrangeWithScores(String key, int start, int end)
|
||||
throws JedisException {
|
||||
List<String> membersWithScores = sendCommand("ZREVRANGE", key,
|
||||
String.valueOf(start), String.valueOf(end), "WITHSCORES")
|
||||
.getMultiBulkReply();
|
||||
Set<Tuple> set = new LinkedHashSet<Tuple>();
|
||||
Iterator<String> iterator = membersWithScores.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
set
|
||||
.add(new Tuple(iterator.next(), Double.valueOf(iterator
|
||||
.next())));
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
public int zcard(String key) throws JedisException {
|
||||
return sendCommand("ZCARD", key).getIntegerReply();
|
||||
}
|
||||
|
||||
public double zscore(String key, String member) throws JedisException {
|
||||
String score = sendCommand("ZSCORE", key, member).getBulkReply();
|
||||
return Double.valueOf(score);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisException;
|
||||
import redis.clients.jedis.Tuple;
|
||||
|
||||
public class SortedSetCommandsTest extends Assert {
|
||||
private Jedis jedis;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
jedis = new Jedis("localhost");
|
||||
jedis.connect();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
jedis.flushDB();
|
||||
jedis.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zadd() throws JedisException {
|
||||
int status = jedis.zadd("foo", 1d, "a");
|
||||
assertEquals(1, status);
|
||||
|
||||
status = jedis.zadd("foo", 10d, "b");
|
||||
assertEquals(1, status);
|
||||
|
||||
status = jedis.zadd("foo", 0.1d, "c");
|
||||
assertEquals(1, status);
|
||||
|
||||
status = jedis.zadd("foo", 2d, "a");
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zrange() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 10d, "b");
|
||||
jedis.zadd("foo", 0.1d, "c");
|
||||
jedis.zadd("foo", 2d, "a");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("c");
|
||||
expected.add("a");
|
||||
|
||||
Set<String> range = jedis.zrange("foo", 0, 1);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected.add("b");
|
||||
range = jedis.zrange("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zrevrange() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 10d, "b");
|
||||
jedis.zadd("foo", 0.1d, "c");
|
||||
jedis.zadd("foo", 2d, "a");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
expected.add("a");
|
||||
|
||||
Set<String> range = jedis.zrevrange("foo", 0, 1);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected.add("c");
|
||||
range = jedis.zrevrange("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zrem() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 2d, "b");
|
||||
|
||||
int status = jedis.zrem("foo", "a");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(1, status);
|
||||
assertEquals(expected, jedis.zrange("foo", 0, 100));
|
||||
|
||||
status = jedis.zrem("foo", "bar");
|
||||
|
||||
assertEquals(0, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zincrby() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 2d, "b");
|
||||
|
||||
double score = jedis.zincrby("foo", 2d, "a");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
|
||||
assertEquals(3d, score);
|
||||
assertEquals(expected, jedis.zrange("foo", 0, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zrank() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 2d, "b");
|
||||
|
||||
int rank = jedis.zrank("foo", "a");
|
||||
assertEquals(0, rank);
|
||||
|
||||
rank = jedis.zrank("foo", "b");
|
||||
assertEquals(1, rank);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zrevrank() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 2d, "b");
|
||||
|
||||
int rank = jedis.zrevrank("foo", "a");
|
||||
assertEquals(1, rank);
|
||||
|
||||
rank = jedis.zrevrank("foo", "b");
|
||||
assertEquals(0, rank);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zrangeWithScores() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 10d, "b");
|
||||
jedis.zadd("foo", 0.1d, "c");
|
||||
jedis.zadd("foo", 2d, "a");
|
||||
|
||||
Set<Tuple> expected = new LinkedHashSet<Tuple>();
|
||||
expected.add(new Tuple("c", 0.1d));
|
||||
expected.add(new Tuple("a", 2d));
|
||||
|
||||
Set<Tuple> range = jedis.zrangeWithScores("foo", 0, 1);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected.add(new Tuple("b", 10d));
|
||||
range = jedis.zrangeWithScores("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zrevrangeWithScores() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 10d, "b");
|
||||
jedis.zadd("foo", 0.1d, "c");
|
||||
jedis.zadd("foo", 2d, "a");
|
||||
|
||||
Set<Tuple> expected = new LinkedHashSet<Tuple>();
|
||||
expected.add(new Tuple("b", 10d));
|
||||
expected.add(new Tuple("a", 2d));
|
||||
|
||||
Set<Tuple> range = jedis.zrevrangeWithScores("foo", 0, 1);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected.add(new Tuple("c", 0.1d));
|
||||
range = jedis.zrevrangeWithScores("foo", 0, 100);
|
||||
assertEquals(expected, range);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zcard() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 10d, "b");
|
||||
jedis.zadd("foo", 0.1d, "c");
|
||||
jedis.zadd("foo", 2d, "a");
|
||||
|
||||
int size = jedis.zcard("foo");
|
||||
assertEquals(3, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zscore() throws JedisException {
|
||||
jedis.zadd("foo", 1d, "a");
|
||||
jedis.zadd("foo", 10d, "b");
|
||||
jedis.zadd("foo", 0.1d, "c");
|
||||
jedis.zadd("foo", 2d, "a");
|
||||
|
||||
double score = jedis.zscore("foo", "b");
|
||||
assertEquals(10d, score);
|
||||
|
||||
score = jedis.zscore("foo", "c");
|
||||
assertEquals(0.1d, score);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user