Implemented all the sorting parameters

This commit is contained in:
Jonathan Leibiusky
2010-08-03 01:20:01 -03:00
parent 350865c868
commit dea8c12d1e
5 changed files with 85 additions and 4 deletions

View File

@@ -7,22 +7,22 @@ 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?
- Sorting
- Publish/Subscribe
- Persistence control commands
- Remote server control commands
- The AUTH, SORT, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands
- The AUTH, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands
But stay close because things are going fast and all this will be implemented soon!
## Ok.. so what's already done?
- Sorting
- Connection handling (not AUTH)
- Commands operating on all the kind of values
- Commands operating on string values
- Commands operating on hashes
- Commands operating on lists (not SORT, BRPOP)
- Commands operating on lists (not BRPOP)
- Commands operating on sets
- Commands operating on sorted sets (not SORT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE)
- Commands operating on sorted sets (not ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE)
- Transactions
## How do I use it?

View File

@@ -399,4 +399,18 @@ public class Client extends Connection {
public void blpop(String[] args) throws JedisException {
sendCommand("BLPOP", args);
}
public void sort(String key, SortingParams sortingParameters, String dstkey)
throws JedisException {
List<String> args = new ArrayList<String>();
args.add(key);
args.addAll(sortingParameters.getParams());
args.add("STORE");
args.add(dstkey);
sendCommand("SORT", args.toArray(new String[args.size()]));
}
public void sort(String key, String dstkey) throws JedisException {
sendCommand("SORT", key, "STORE", dstkey);
}
}

View File

@@ -519,4 +519,15 @@ public class Jedis {
client.blpop(args.toArray(new String[args.size()]));
return client.getMultiBulkReply();
}
public int sort(String key, SortingParams sortingParameters, String dstkey)
throws JedisException {
client.sort(key, sortingParameters, dstkey);
return client.getIntegerReply();
}
public int sort(String key, String dstkey) throws JedisException {
client.sort(key, dstkey);
return client.getIntegerReply();
}
}

View File

@@ -39,4 +39,12 @@ public class SortingParams {
params.add("ALPHA");
return this;
}
public SortingParams get(String... patterns) {
for (String pattern : patterns) {
params.add("GET");
params.add(pattern);
}
return this;
}
}

View File

@@ -124,4 +124,52 @@ public class SortingCommandsTest extends Assert {
assertEquals(expected, result);
}
@Test
public void sortGet() throws JedisException {
jedis.lpush("foo", "1");
jedis.lpush("foo", "2");
jedis.lpush("foo", "10");
jedis.set("bar1", "bar1");
jedis.set("bar2", "bar2");
jedis.set("bar10", "bar10");
jedis.set("car1", "car1");
jedis.set("car2", "car2");
jedis.set("car10", "car10");
SortingParams sp = new SortingParams();
sp.get("car*", "bar*");
List<String> result = jedis.sort("foo", sp);
List<String> expected = new ArrayList<String>();
expected.add("car1");
expected.add("bar1");
expected.add("car2");
expected.add("bar2");
expected.add("car10");
expected.add("bar10");
assertEquals(expected, result);
}
@Test
public void sortStore() throws JedisException {
jedis.lpush("foo", "1");
jedis.lpush("foo", "2");
jedis.lpush("foo", "10");
int result = jedis.sort("foo", "result");
List<String> expected = new ArrayList<String>();
expected.add("1");
expected.add("2");
expected.add("10");
assertEquals(3, result);
assertEquals(expected, jedis.lrange("result", 0, 1000));
}
}