Partial support for sorting

This commit is contained in:
Jonathan Leibiusky
2010-07-25 10:36:13 -03:00
parent 4d078d747e
commit d48ab8def2
5 changed files with 126 additions and 12 deletions

15
pom.xml
View File

@@ -12,4 +12,17 @@
<scope>compile</scope>
</dependency>
</dependencies>
</project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -340,8 +340,8 @@ public class Client extends Connection {
}
public void zrevrange(String key, int start, int end) throws JedisException {
sendCommand("ZREVRANGE", key, String.valueOf(start), String
.valueOf(end));
sendCommand("ZREVRANGE", key, String.valueOf(start),
String.valueOf(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)
throws JedisException {
sendCommand("ZREVRANGE", key, String.valueOf(start), String
.valueOf(end), "WITHSCORES");
sendCommand("ZREVRANGE", key, String.valueOf(start),
String.valueOf(end), "WITHSCORES");
}
public void zcard(String key) throws JedisException {
@@ -383,4 +383,13 @@ public class Client extends Connection {
public void unwatch() throws JedisException {
sendCommand("UNWATCH");
}
}
public void sort(String key) throws JedisException {
sendCommand("SORT", key);
}
public void sort(String key, SortingParams sortingParameters)
throws JedisException {
sendCommand("SORT", sortingParameters.getParams());
}
}

View File

@@ -428,9 +428,7 @@ public class Jedis {
Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator();
while (iterator.hasNext()) {
set
.add(new Tuple(iterator.next(), Double.valueOf(iterator
.next())));
set.add(new Tuple(iterator.next(), Double.valueOf(iterator.next())));
}
return set;
}
@@ -442,9 +440,7 @@ public class Jedis {
Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator();
while (iterator.hasNext()) {
set
.add(new Tuple(iterator.next(), Double.valueOf(iterator
.next())));
set.add(new Tuple(iterator.next(), Double.valueOf(iterator.next())));
}
return set;
}
@@ -496,4 +492,15 @@ public class Jedis {
return client.getStatusCodeReply();
}
public List<String> sort(String key) throws JedisException {
client.sort(key);
return client.getMultiBulkReply();
}
public List<String> sort(String key, SortingParams sortingParameters)
throws JedisException {
client.sort(key, sortingParameters);
return client.getMultiBulkReply();
}
}

View File

@@ -0,0 +1,20 @@
package redis.clients.jedis;
import java.util.ArrayList;
import java.util.List;
public class SortingParams {
private String pattern = null;
private List<String> params = new ArrayList<String>();
public SortingParams by(String pattern) {
this.pattern = pattern;
params.add("BY");
params.add(pattern);
return this;
}
public String[] getParams() {
return params.toArray(new String[params.size()]);
}
}

View File

@@ -0,0 +1,65 @@
package redis.clients.jedis.tests.commands;
import java.util.ArrayList;
import java.util.List;
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.SortingParams;
public class SortingCommandsTest 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 sort() throws JedisException {
jedis.lpush("foo", "3");
jedis.lpush("foo", "2");
jedis.lpush("foo", "1");
List<String> result = jedis.sort("foo");
List<String> expected = new ArrayList<String>();
expected.add("1");
expected.add("2");
expected.add("3");
assertEquals(expected, result);
}
@Test
public void sortBy() throws JedisException {
jedis.lpush("foo", "3");
jedis.lpush("foo", "2");
jedis.lpush("foo", "1");
SortingParams sp = new SortingParams();
sp.by("bar_*");
List<String> result = jedis.sort("foo", sp);
List<String> expected = new ArrayList<String>();
expected.add("foo_1");
expected.add("foo_2");
expected.add("foo_3");
assertEquals(expected, result);
}
}