Added more sorting options

This commit is contained in:
Jonathan Leibiusky
2010-07-25 17:51:11 -03:00
parent d48ab8def2
commit 3dd7ca6f2a
3 changed files with 82 additions and 18 deletions

View File

@@ -340,8 +340,8 @@ public class Client extends Connection {
} }
public void zrevrange(String key, int start, int end) throws JedisException { public void zrevrange(String key, int start, int end) throws JedisException {
sendCommand("ZREVRANGE", key, String.valueOf(start), sendCommand("ZREVRANGE", key, String.valueOf(start), String
String.valueOf(end)); .valueOf(end));
} }
public void zrangeWithScores(String key, int start, int end) public void zrangeWithScores(String key, int start, int end)
@@ -352,8 +352,8 @@ public class Client extends Connection {
public void zrevrangeWithScores(String key, int start, int end) public void zrevrangeWithScores(String key, int start, int end)
throws JedisException { throws JedisException {
sendCommand("ZREVRANGE", key, String.valueOf(start), sendCommand("ZREVRANGE", key, String.valueOf(start), String
String.valueOf(end), "WITHSCORES"); .valueOf(end), "WITHSCORES");
} }
public void zcard(String key) throws JedisException { public void zcard(String key) throws JedisException {
@@ -390,6 +390,9 @@ public class Client extends Connection {
public void sort(String key, SortingParams sortingParameters) public void sort(String key, SortingParams sortingParameters)
throws JedisException { throws JedisException {
sendCommand("SORT", sortingParameters.getParams()); List<String> args = new ArrayList<String>();
args.add(key);
args.addAll(sortingParameters.getParams());
sendCommand("SORT", args.toArray(new String[args.size()]));
} }
} }

View File

@@ -1,20 +1,42 @@
package redis.clients.jedis; package redis.clients.jedis;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
public class SortingParams { public class SortingParams {
private String pattern = null;
private List<String> params = new ArrayList<String>(); private List<String> params = new ArrayList<String>();
public SortingParams by(String pattern) { public SortingParams by(String pattern) {
this.pattern = pattern; params.add("BY");
params.add("BY"); params.add(pattern);
params.add(pattern); return this;
return this;
} }
public String[] getParams() { public Collection<String> getParams() {
return params.toArray(new String[params.size()]); return Collections.unmodifiableCollection(params);
} }
}
public SortingParams desc() {
params.add("DESC");
return this;
}
public SortingParams asc() {
params.add("ASC");
return this;
}
public SortingParams limit(int start, int count) {
params.add("LIMIT");
params.add(String.valueOf(start));
params.add(String.valueOf(count));
return this;
}
public SortingParams alpha() {
params.add("ALPHA");
return this;
}
}

View File

@@ -20,6 +20,7 @@ public class SortingCommandsTest extends Assert {
public void setUp() throws Exception { public void setUp() throws Exception {
jedis = new Jedis("localhost"); jedis = new Jedis("localhost");
jedis.connect(); jedis.connect();
jedis.flushDB();
} }
@After @After
@@ -45,20 +46,58 @@ public class SortingCommandsTest extends Assert {
} }
@Test @Test
public void sortBy() throws JedisException { public void sortDesc() throws JedisException {
jedis.lpush("foo", "3"); jedis.lpush("foo", "3");
jedis.lpush("foo", "2"); jedis.lpush("foo", "2");
jedis.lpush("foo", "1"); jedis.lpush("foo", "1");
SortingParams sp = new SortingParams(); SortingParams sp = new SortingParams();
sp.by("bar_*"); sp.desc();
List<String> result = jedis.sort("foo", sp); List<String> result = jedis.sort("foo", sp);
List<String> expected = new ArrayList<String>(); List<String> expected = new ArrayList<String>();
expected.add("foo_1"); expected.add("3");
expected.add("foo_2"); expected.add("2");
expected.add("foo_3"); expected.add("1");
assertEquals(expected, result);
}
@Test
public void sortLimit() throws JedisException {
for (int n = 10; n > 0; n--) {
jedis.lpush("foo", String.valueOf(n));
}
SortingParams sp = new SortingParams();
sp.limit(0, 3);
List<String> result = jedis.sort("foo", sp);
List<String> expected = new ArrayList<String>();
expected.add("1");
expected.add("2");
expected.add("3");
assertEquals(expected, result);
}
@Test
public void sortAlpha() throws JedisException {
jedis.lpush("foo", "1");
jedis.lpush("foo", "2");
jedis.lpush("foo", "10");
SortingParams sp = new SortingParams();
sp.alpha();
List<String> result = jedis.sort("foo", sp);
List<String> expected = new ArrayList<String>();
expected.add("1");
expected.add("10");
expected.add("2");
assertEquals(expected, result); assertEquals(expected, result);
} }