From cbbac2c8e07399e6c2d1e369e1f181cd1e07ca24 Mon Sep 17 00:00:00 2001 From: Nikita Date: Sun, 5 Jul 2015 20:19:32 +0300 Subject: [PATCH] new commands added --- .../java/org/redisson/client/RedisClient.java | 53 +++++++++++++------ .../redisson/client/handler/RedisEncoder.java | 18 ++++--- .../org/redisson/client/protocol/Encoder.java | 2 +- .../client/protocol/RedisCommand.java | 16 +++++- .../client/protocol/RedisCommands.java | 9 +++- .../redisson/client/protocol/StringCodec.java | 2 +- 6 files changed, 74 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/redisson/client/RedisClient.java b/src/main/java/org/redisson/client/RedisClient.java index 5f226e77b..c9624497f 100644 --- a/src/main/java/org/redisson/client/RedisClient.java +++ b/src/main/java/org/redisson/client/RedisClient.java @@ -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 res = rc.execute(new StringCodec(), RedisCommands.SET, "test", "" + Math.random()); - res.addListener(new FutureListener() { +// for (int i = 0; i < 10000; i++) { + Future res1 = rc.execute(new StringCodec(), RedisCommands.CLIENT_SETNAME, "12333"); + res1.addListener(new FutureListener() { - @Override - public void operationComplete(Future future) throws Exception { - System.out.println("res 1: " + future.getNow()); - } + @Override + public void operationComplete(Future future) throws Exception { + System.out.println("res 12: " + future.getNow()); + } - }); + }); - Future r = rc.execute(new StringCodec(), RedisCommands.GET, "test"); - r.addListener(new FutureListener() { + Future res2 = rc.execute(new StringCodec(), RedisCommands.CLIENT_GETNAME); + res2.addListener(new FutureListener() { - @Override - public void operationComplete(Future future) throws Exception { - System.out.println("res 2: " + future.getNow()); - } - }); + @Override + public void operationComplete(Future future) throws Exception { + System.out.println("res name: " + future.getNow()); + } + + }); + + +/* Future res = rc.execute(new StringCodec(), RedisCommands.SET, "test", "" + Math.random()); + res.addListener(new FutureListener() { + + @Override + public void operationComplete(Future future) throws Exception { +// System.out.println("res 1: " + future.getNow()); + } + + }); + + Future r = rc.execute(new StringCodec(), RedisCommands.GET, "test"); + r.addListener(new FutureListener() { + + @Override + public void operationComplete(Future future) throws Exception { + System.out.println("res 2: " + future.getNow()); + } + }); +*/// } } } diff --git a/src/main/java/org/redisson/client/handler/RedisEncoder.java b/src/main/java/org/redisson/client/handler/RedisEncoder.java index 6fc599c0e..bb4c6e155 100644 --- a/src/main/java/org/redisson/client/handler/RedisEncoder.java +++ b/src/main/java/org/redisson/client/handler/RedisEncoder.java @@ -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> { @@ -30,22 +31,27 @@ public class RedisEncoder extends MessageToByteEncoder @Override protected void encode(ChannelHandlerContext ctx, RedisData 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) { diff --git a/src/main/java/org/redisson/client/protocol/Encoder.java b/src/main/java/org/redisson/client/protocol/Encoder.java index e7536688d..de3bd3206 100644 --- a/src/main/java/org/redisson/client/protocol/Encoder.java +++ b/src/main/java/org/redisson/client/protocol/Encoder.java @@ -2,6 +2,6 @@ package org.redisson.client.protocol; public interface Encoder { - byte[] encode(Object in); + byte[] encode(int paramIndex, Object in); } diff --git a/src/main/java/org/redisson/client/protocol/RedisCommand.java b/src/main/java/org/redisson/client/protocol/RedisCommand.java index 39e8e67ef..4ac5c5a5e 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommand.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommand.java @@ -18,20 +18,34 @@ package org.redisson.client.protocol; public class RedisCommand { private final String name; + private final String subName; private final int[] encodeParamIndexes; private Decoder 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 reponseDecoder, int ... encodeParamIndexes) { + this(name, null, reponseDecoder, encodeParamIndexes); + } + + public RedisCommand(String name, String subName, Decoder 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; } diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index 12936cbf3..eeb379a2f 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -17,7 +17,12 @@ package org.redisson.client.protocol; public interface RedisCommands { - RedisCommand GET = new RedisCommand("GET", 0); - RedisCommand SET = new RedisCommand("SET", new StringReplayDecoder(), 0, 1); + RedisCommand AUTH = new RedisCommand("AUTH", new StringReplayDecoder()); + RedisCommand SELECT = new RedisCommand("SELECT", new StringReplayDecoder()); + RedisCommand CLIENT_SETNAME = new RedisCommand("CLIENT", "SETNAME", new StringReplayDecoder(), 1); + RedisCommand CLIENT_GETNAME = new RedisCommand("CLIENT", "GETNAME"); + + RedisCommand GET = new RedisCommand("GET"); + RedisCommand SET = new RedisCommand("SET", new StringReplayDecoder(), 1); } diff --git a/src/main/java/org/redisson/client/protocol/StringCodec.java b/src/main/java/org/redisson/client/protocol/StringCodec.java index 7d967ad02..4db8fa9c6 100644 --- a/src/main/java/org/redisson/client/protocol/StringCodec.java +++ b/src/main/java/org/redisson/client/protocol/StringCodec.java @@ -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) {