new commands added

pull/243/head
Nikita 10 years ago
parent b7911a8fb3
commit cbbac2c8e0

@ -81,25 +81,48 @@ public class RedisClient {
}
public static void main(String[] args) throws InterruptedException {
RedisClient rc = new RedisClient("127.0.0.1", 6379);
final RedisClient rc = new RedisClient("127.0.0.1", 6379);
rc.connect().sync();
Future<String> res = rc.execute(new StringCodec(), RedisCommands.SET, "test", "" + Math.random());
res.addListener(new FutureListener<String>() {
// for (int i = 0; i < 10000; i++) {
Future<String> res1 = rc.execute(new StringCodec(), RedisCommands.CLIENT_SETNAME, "12333");
res1.addListener(new FutureListener<String>() {
@Override
public void operationComplete(Future<String> future) throws Exception {
System.out.println("res 1: " + future.getNow());
}
@Override
public void operationComplete(Future<String> future) throws Exception {
System.out.println("res 12: " + future.getNow());
}
});
});
Future<String> r = rc.execute(new StringCodec(), RedisCommands.GET, "test");
r.addListener(new FutureListener<Object>() {
Future<String> res2 = rc.execute(new StringCodec(), RedisCommands.CLIENT_GETNAME);
res2.addListener(new FutureListener<String>() {
@Override
public void operationComplete(Future<Object> future) throws Exception {
System.out.println("res 2: " + future.getNow());
}
});
@Override
public void operationComplete(Future<String> future) throws Exception {
System.out.println("res name: " + future.getNow());
}
});
/* Future<String> res = rc.execute(new StringCodec(), RedisCommands.SET, "test", "" + Math.random());
res.addListener(new FutureListener<String>() {
@Override
public void operationComplete(Future<String> future) throws Exception {
// System.out.println("res 1: " + future.getNow());
}
});
Future<String> r = rc.execute(new StringCodec(), RedisCommands.GET, "test");
r.addListener(new FutureListener<Object>() {
@Override
public void operationComplete(Future<Object> future) throws Exception {
System.out.println("res 2: " + future.getNow());
}
});
*/// }
}
}

@ -20,6 +20,7 @@ import java.util.Arrays;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;
public class RedisEncoder extends MessageToByteEncoder<RedisData<Object, Object>> {
@ -30,22 +31,27 @@ public class RedisEncoder extends MessageToByteEncoder<RedisData<Object, Object>
@Override
protected void encode(ChannelHandlerContext ctx, RedisData<Object, Object> msg, ByteBuf out) throws Exception {
out.writeByte(ARGS_PREFIX);
out.writeBytes(toChars(1 + msg.getParams().length));
int len = 1 + msg.getParams().length;
if (msg.getCommand().getSubName() != null) {
len++;
}
out.writeBytes(toChars(len));
out.writeBytes(CRLF);
if (Arrays.binarySearch(msg.getCommand().getEncodeParamIndexes(), 0) != -1) {
writeArgument(out, msg.getCodec().encode(msg.getCommand().getName()));
writeArgument(out, msg.getCommand().getName().getBytes("UTF-8"));
if (msg.getCommand().getSubName() != null) {
writeArgument(out, msg.getCommand().getSubName().getBytes("UTF-8"));
}
int i = 1;
for (Object param : msg.getParams()) {
if (Arrays.binarySearch(msg.getCommand().getEncodeParamIndexes(), i) != -1) {
writeArgument(out, msg.getCodec().encode(param));
writeArgument(out, msg.getCodec().encode(i, param));
}
i++;
}
// String o = out.toString(CharsetUtil.UTF_8);
// System.out.println(o);
String o = out.toString(CharsetUtil.UTF_8);
System.out.println(o);
}
private void writeArgument(ByteBuf out, byte[] arg) {

@ -2,6 +2,6 @@ package org.redisson.client.protocol;
public interface Encoder {
byte[] encode(Object in);
byte[] encode(int paramIndex, Object in);
}

@ -18,20 +18,34 @@ package org.redisson.client.protocol;
public class RedisCommand<R> {
private final String name;
private final String subName;
private final int[] encodeParamIndexes;
private Decoder<R> reponseDecoder;
public RedisCommand(String name, String subName, int ... encodeParamIndexes) {
this(name, subName, null, encodeParamIndexes);
}
public RedisCommand(String name, int ... encodeParamIndexes) {
this(name, null, encodeParamIndexes);
this(name, null, null, encodeParamIndexes);
}
public RedisCommand(String name, Decoder<R> reponseDecoder, int ... encodeParamIndexes) {
this(name, null, reponseDecoder, encodeParamIndexes);
}
public RedisCommand(String name, String subName, Decoder<R> reponseDecoder, int ... encodeParamIndexes) {
super();
this.name = name;
this.subName = subName;
this.reponseDecoder = reponseDecoder;
this.encodeParamIndexes = encodeParamIndexes;
}
public String getSubName() {
return subName;
}
public String getName() {
return name;
}

@ -17,7 +17,12 @@ package org.redisson.client.protocol;
public interface RedisCommands {
RedisCommand<Object> GET = new RedisCommand<Object>("GET", 0);
RedisCommand<String> SET = new RedisCommand<String>("SET", new StringReplayDecoder(), 0, 1);
RedisCommand<String> AUTH = new RedisCommand<String>("AUTH", new StringReplayDecoder());
RedisCommand<String> SELECT = new RedisCommand<String>("SELECT", new StringReplayDecoder());
RedisCommand<String> CLIENT_SETNAME = new RedisCommand<String>("CLIENT", "SETNAME", new StringReplayDecoder(), 1);
RedisCommand<String> CLIENT_GETNAME = new RedisCommand<String>("CLIENT", "GETNAME");
RedisCommand<Object> GET = new RedisCommand<Object>("GET");
RedisCommand<String> SET = new RedisCommand<String>("SET", new StringReplayDecoder(), 1);
}

@ -8,7 +8,7 @@ import io.netty.util.CharsetUtil;
public class StringCodec implements Codec {
@Override
public byte[] encode(Object in) {
public byte[] encode(int paramIndex, Object in) {
try {
return in.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {

Loading…
Cancel
Save