Implemented BLPOP

This commit is contained in:
Jonathan Leibiusky
2010-08-03 00:32:13 -03:00
parent 3dd7ca6f2a
commit ef5518e581
8 changed files with 63 additions and 9 deletions

View File

@@ -11,7 +11,7 @@ Jedis is a WORK IN PROGRESS.
- Publish/Subscribe - Publish/Subscribe
- Persistence control commands - Persistence control commands
- Remote server control commands - Remote server control commands
- The AUTH, SORT, BLPOP, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands - The AUTH, SORT, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands
But stay close because things are going fast and all this will be implemented soon! But stay close because things are going fast and all this will be implemented soon!
@@ -20,7 +20,7 @@ But stay close because things are going fast and all this will be implemented so
- Commands operating on all the kind of values - Commands operating on all the kind of values
- 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, BRPOP)
- Commands operating on sets - Commands operating on sets
- Commands operating on sorted sets (not SORT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE) - Commands operating on sorted sets (not SORT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE)
- Transactions - Transactions

View File

@@ -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.8</version> <version>0.0.9</version>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@@ -395,4 +395,8 @@ public class Client extends Connection {
args.addAll(sortingParameters.getParams()); args.addAll(sortingParameters.getParams());
sendCommand("SORT", args.toArray(new String[args.size()])); sendCommand("SORT", args.toArray(new String[args.size()]));
} }
public void blpop(String[] args) throws JedisException {
sendCommand("BLPOP", args);
}
} }

View File

@@ -2,6 +2,7 @@ package redis.clients.jedis;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@@ -428,7 +429,9 @@ public class Jedis {
Set<Tuple> set = new LinkedHashSet<Tuple>(); Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator(); Iterator<String> iterator = membersWithScores.iterator();
while (iterator.hasNext()) { 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; return set;
} }
@@ -440,7 +443,9 @@ public class Jedis {
Set<Tuple> set = new LinkedHashSet<Tuple>(); Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator(); Iterator<String> iterator = membersWithScores.iterator();
while (iterator.hasNext()) { 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; return set;
} }
@@ -503,4 +508,15 @@ public class Jedis {
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
} public List<String> blpop(int timeout, String... keys)
throws JedisException {
List<String> args = new ArrayList<String>();
for (String arg : keys) {
args.add(arg);
}
args.add(String.valueOf(timeout));
client.blpop(args.toArray(new String[args.size()]));
return client.getMultiBulkReply();
}
}

View File

@@ -129,8 +129,11 @@ public class Protocol {
} }
private Object processMultiBulkReply(InputStream is) throws JedisException { private Object processMultiBulkReply(InputStream is) throws JedisException {
List<Object> ret = new ArrayList<Object>();
int num = Integer.parseInt(readLine(is)); int num = Integer.parseInt(readLine(is));
if (num == -1) {
return null;
}
List<Object> ret = new ArrayList<Object>();
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
ret.add(process(is)); ret.add(process(is));
} }

View File

@@ -102,6 +102,15 @@ public class ProtocolTest extends Assert {
expected2.add(sub); expected2.add(sub);
assertEquals(expected2, response2); assertEquals(expected2, response2);
}
@SuppressWarnings("unchecked")
@Test
public void nullMultiBulkReply() throws JedisException {
InputStream is = new ByteArrayInputStream("*-1\r\n".getBytes());
Protocol protocol = new Protocol();
List<String> response = (List<String>) (List<?>) protocol
.getMultiBulkReply(is);
assertNull(response);
} }
} }

View File

@@ -220,4 +220,26 @@ public class ListCommandsTest extends Assert {
assertEquals(srcExpected, jedis.lrange("foo", 0, 1000)); assertEquals(srcExpected, jedis.lrange("foo", 0, 1000));
assertEquals(dstExpected, jedis.lrange("dst", 0, 1000)); assertEquals(dstExpected, jedis.lrange("dst", 0, 1000));
} }
}
@Test
public void blpop() throws JedisException {
List<String> result = jedis.blpop(2, "foo");
assertNull(result);
new Thread(new Runnable() {
public void run() {
try {
Jedis j = new Jedis("localhost");
j.connect();
List<String> result = j.blpop(0, "foo");
assertNotNull(result);
assertEquals(1, result.size());
assertEquals("bar", result.get(0));
} catch (Exception ex) {
fail(ex.getMessage());
}
}
});
jedis.lpush("foo", "bar");
}
}

View File

@@ -90,7 +90,7 @@ public class TransactionCommandsTest extends Assert {
t.set("mykey", val); t.set("mykey", val);
List<Object> resp = t.exec(); List<Object> resp = t.exec();
assertEquals(new ArrayList<Object>(), resp); assertEquals(null, resp);
} }
@Test @Test