Small refactoring to make command definitions even smaller
This commit is contained in:
@@ -7,19 +7,24 @@ import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class Client {
|
||||
protected String host;
|
||||
protected int port = Protocol.DEFAULT_PORT;
|
||||
protected Socket socket;
|
||||
protected boolean connected = false;
|
||||
protected Protocol protocol = new Protocol();
|
||||
protected OutputStream outputStream;
|
||||
protected InputStream inputStream;
|
||||
private String host;
|
||||
private int port = Protocol.DEFAULT_PORT;
|
||||
private Socket socket;
|
||||
private boolean connected = false;
|
||||
private Protocol protocol = new Protocol();
|
||||
private OutputStream outputStream;
|
||||
private InputStream inputStream;
|
||||
|
||||
public Client(String host) {
|
||||
super();
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
protected Client sendCommand(String name, String... args) {
|
||||
protocol.sendCommand(outputStream, name, args);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Client(String host, int port) {
|
||||
super();
|
||||
this.host = host;
|
||||
@@ -69,4 +74,12 @@ public class Client {
|
||||
return connected;
|
||||
}
|
||||
|
||||
protected String getSingleLineReply() {
|
||||
return protocol.getSingleLineReply(inputStream);
|
||||
}
|
||||
|
||||
public String getBulkReply() {
|
||||
return protocol.getBulkReply(inputStream);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +1,21 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Jedis extends Client {
|
||||
public Jedis(String host) {
|
||||
super(host);
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
// TODO: I want to be able to do the followin
|
||||
// return protocol.runCommand(os,
|
||||
// "PING").getSingleLineReply(inputStream);
|
||||
// or even maybe
|
||||
// return protocol.runCommand("PING").getSingleLineReply();
|
||||
|
||||
String command = protocol.buildCommand("PING");
|
||||
try {
|
||||
outputStream.write(command.getBytes());
|
||||
return protocol.getSingleLineReply(inputStream);
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure what to do here
|
||||
return null;
|
||||
}
|
||||
return sendCommand("PING").getSingleLineReply();
|
||||
}
|
||||
|
||||
public String set(String key, String value) {
|
||||
String command = protocol.buildCommand("SET", key, value);
|
||||
try {
|
||||
outputStream.write(command.getBytes());
|
||||
return protocol.getSingleLineReply(inputStream);
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure what to do here
|
||||
return null;
|
||||
}
|
||||
return sendCommand("SET", key, value).getSingleLineReply();
|
||||
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
String command = protocol.buildCommand("GET", key);
|
||||
try {
|
||||
outputStream.write(command.getBytes());
|
||||
return protocol.getBulkReply(inputStream);
|
||||
} catch (IOException e) {
|
||||
// TODO Not sure what to do here
|
||||
return null;
|
||||
}
|
||||
return sendCommand("GET", key).getBulkReply();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package redis.clients.jedis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,7 +19,7 @@ public class Protocol {
|
||||
public static final byte PLUS_BYTE = PLUS.getBytes()[0];
|
||||
public static final byte COLON_BYTE = COLON.getBytes()[0];
|
||||
|
||||
public String buildCommand(String name, String... args) {
|
||||
public void sendCommand(OutputStream os, String name, String... args) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(ASTERISK).append(args.length + 1).append(
|
||||
COMMAND_DELIMITER);
|
||||
@@ -28,7 +29,11 @@ public class Protocol {
|
||||
builder.append(DOLLAR).append(arg.length()).append(
|
||||
COMMAND_DELIMITER).append(arg).append(COMMAND_DELIMITER);
|
||||
}
|
||||
return builder.toString();
|
||||
try {
|
||||
os.write(builder.toString().getBytes());
|
||||
} catch (IOException e) {
|
||||
// TODO don't know what to do here!
|
||||
}
|
||||
}
|
||||
|
||||
public String getBulkReply(InputStream is) {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -13,13 +17,24 @@ import redis.clients.jedis.Protocol;
|
||||
|
||||
public class ProtocolTest extends Assert {
|
||||
@Test
|
||||
public void buildACommand() {
|
||||
Protocol protocol = new Protocol();
|
||||
String command = protocol.buildCommand("GET", "SOMEKEY");
|
||||
public void buildACommand() throws IOException {
|
||||
PipedInputStream pis = new PipedInputStream();
|
||||
BufferedInputStream bis = new BufferedInputStream(pis);
|
||||
PipedOutputStream pos = new PipedOutputStream(pis);
|
||||
|
||||
Protocol protocol = new Protocol();
|
||||
protocol.sendCommand(pos, "GET", "SOMEKEY");
|
||||
|
||||
pos.close();
|
||||
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
|
||||
|
||||
assertEquals(expectedCommand, command);
|
||||
int b;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((b = bis.read()) != -1) {
|
||||
sb.append((char) b);
|
||||
}
|
||||
|
||||
assertEquals(expectedCommand, sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user