Encoder/Decoder added

pull/243/head
Nikita 10 years ago
parent 394c9539a2
commit 93eb8fad27

@ -21,8 +21,10 @@ import org.redisson.client.handler.RedisCommandsQueue;
import org.redisson.client.handler.RedisData; import org.redisson.client.handler.RedisData;
import org.redisson.client.handler.RedisDecoder; import org.redisson.client.handler.RedisDecoder;
import org.redisson.client.handler.RedisEncoder; import org.redisson.client.handler.RedisEncoder;
import org.redisson.client.protocol.Codec;
import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.StringCodec;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel; import io.netty.channel.Channel;
@ -66,27 +68,38 @@ public class RedisClient {
return future; return future;
} }
public <R> Future<R> execute(RedisCommand<R> command, Object ... params) { // public <R> Future<R> execute(Codec encoder, RedisCommand<R> command, Object ... params) {
// Promise<R> promise = bootstrap.group().next().<R>newPromise();
// channel.writeAndFlush(new RedisData<R, R>(promise, encoder, command, params));
// return promise;
// }
public <T, R> Future<R> execute(Codec encoder, RedisCommand<T> command, Object ... params) {
Promise<R> promise = bootstrap.group().next().<R>newPromise(); Promise<R> promise = bootstrap.group().next().<R>newPromise();
channel.writeAndFlush(new RedisData<R>(promise, command, params)); channel.writeAndFlush(new RedisData<T, R>(promise, encoder, command, params));
return promise; return promise;
} }
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
RedisClient rc = new RedisClient("127.0.0.1", 6379); RedisClient rc = new RedisClient("127.0.0.1", 6379);
rc.connect().sync(); rc.connect().sync();
for (int i = 0; i < 10000; i++) { Future<String> res = rc.execute(new StringCodec(), RedisCommands.SET, "test", "" + Math.random());
final int j = i; res.addListener(new FutureListener<String>() {
Future<String> res = rc.execute(RedisCommands.SET, "test", "" + Math.random());
res.addListener(new FutureListener<String>() { @Override
public void operationComplete(Future<String> future) throws Exception {
@Override System.out.println("res 1: " + future.getNow());
public void operationComplete(Future<String> future) throws Exception { }
System.out.println("res: " + future.getNow() + " " + j);
} });
}); Future<String> r = rc.execute(new StringCodec(), RedisCommands.GET, "test");
} r.addListener(new FutureListener<Object>() {
// rc.execute(RedisCommands.GET, "test");
@Override
public void operationComplete(Future<Object> future) throws Exception {
System.out.println("res 2: " + future.getNow());
}
});
} }
} }

@ -0,0 +1,15 @@
package org.redisson.client;
public class RedisException extends RuntimeException {
private static final long serialVersionUID = 3389820652701696154L;
public RedisException(String message, Throwable cause) {
super(message, cause);
}
public RedisException(String message) {
super(message);
}
}

@ -28,9 +28,9 @@ public class RedisCommandsQueue extends ChannelDuplexHandler {
public enum QueueCommands {NEXT_COMMAND} public enum QueueCommands {NEXT_COMMAND}
public static final AttributeKey<Promise<Object>> REPLAY_PROMISE = AttributeKey.valueOf("promise"); public static final AttributeKey<RedisData<Object, Object>> REPLAY_PROMISE = AttributeKey.valueOf("promise");
private final Queue<RedisData<Object>> queue = PlatformDependent.newMpscQueue(); private final Queue<RedisData<Object, Object>> queue = PlatformDependent.newMpscQueue();
@Override @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
@ -45,8 +45,8 @@ public class RedisCommandsQueue extends ChannelDuplexHandler {
@Override @Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof RedisData) { if (msg instanceof RedisData) {
RedisData<Object> data = (RedisData<Object>) msg; RedisData<Object, Object> data = (RedisData<Object, Object>) msg;
if (data.getSend().get()) { if (data.getSended().get()) {
super.write(ctx, msg, promise); super.write(ctx, msg, promise);
} else { } else {
queue.add(data); queue.add(data);
@ -58,9 +58,9 @@ public class RedisCommandsQueue extends ChannelDuplexHandler {
} }
private void sendData(ChannelHandlerContext ctx) throws Exception { private void sendData(ChannelHandlerContext ctx) throws Exception {
RedisData<Object> data = queue.peek(); RedisData<Object, Object> data = queue.peek();
if (data != null && data.getSend().compareAndSet(false, true)) { if (data != null && data.getSended().compareAndSet(false, true)) {
ctx.channel().attr(REPLAY_PROMISE).set(data.getPromise()); ctx.channel().attr(REPLAY_PROMISE).set(data);
ctx.channel().writeAndFlush(data); ctx.channel().writeAndFlush(data);
} }
} }

@ -17,24 +17,28 @@ package org.redisson.client.handler;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.redisson.client.protocol.Codec;
import org.redisson.client.protocol.Encoder;
import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommand;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
public class RedisData<R> { public class RedisData<T, R> {
Promise<R> promise; final Promise<R> promise;
RedisCommand<R> command; final RedisCommand<T> command;
Object[] params; final Object[] params;
AtomicBoolean send = new AtomicBoolean(); final Codec codec;
final AtomicBoolean sended = new AtomicBoolean();
public RedisData(Promise<R> promise, RedisCommand<R> command, Object[] params) { public RedisData(Promise<R> promise, Codec encoder, RedisCommand<T> command, Object[] params) {
this.promise = promise; this.promise = promise;
this.command = command; this.command = command;
this.params = params; this.params = params;
this.codec = encoder;
} }
public RedisCommand<R> getCommand() { public RedisCommand<T> getCommand() {
return command; return command;
} }
@ -46,8 +50,12 @@ public class RedisData<R> {
return promise; return promise;
} }
public AtomicBoolean getSend() { public AtomicBoolean getSended() {
return send; return sended;
}
public Codec getCodec() {
return codec;
} }
} }

@ -15,56 +15,91 @@
*/ */
package org.redisson.client.handler; package org.redisson.client.handler;
import java.io.IOException;
import java.util.List; import java.util.List;
import org.redisson.client.RedisException;
import org.redisson.client.handler.RedisCommandsQueue.QueueCommands; import org.redisson.client.handler.RedisCommandsQueue.QueueCommands;
import org.redisson.client.protocol.Decoder;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.ReplayingDecoder;
import io.netty.util.CharsetUtil;
public class RedisDecoder extends ReplayingDecoder<Void> { public class RedisDecoder extends ReplayingDecoder<Void> {
private static final char CR = '\r';
private static final char LF = '\n';
private static final char ZERO = '0';
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
int code = in.readByte(); RedisData<Object, Object> data = ctx.channel().attr(RedisCommandsQueue.REPLAY_PROMISE).getAndRemove();
String status = in.readBytes(in.bytesBefore((byte) '\r')).toString(CharsetUtil.UTF_8);
in.skipBytes(2);
out.add(status);
System.out.println("status: " + status);
ctx.channel().attr(RedisCommandsQueue.REPLAY_PROMISE).getAndRemove().setSuccess(status); int code = in.readByte();
if (code == '+') {
Object result = data.getCommand().getReponseDecoder().decode(in);
data.getPromise().setSuccess(result);
} else if (code == '-') {
Object result = data.getCommand().getReponseDecoder().decode(in);
data.getPromise().setFailure(new RedisException(result.toString()));
} else if (code == '$') {
Decoder<Object> decoder = data.getCommand().getReponseDecoder();
if (decoder == null) {
decoder = data.getCodec();
}
Object result = decoder.decode(readBytes(in));
data.getPromise().setSuccess(result);
} else {
throw new IllegalStateException("Can't decode replay");
}
ctx.pipeline().fireUserEventTriggered(QueueCommands.NEXT_COMMAND); ctx.pipeline().fireUserEventTriggered(QueueCommands.NEXT_COMMAND);
// switch (code) { }
// case StatusReply.MARKER: {
// String status = is.readBytes(is.bytesBefore((byte) '\r')).toString(Charsets.UTF_8); public ByteBuf readBytes(ByteBuf is) throws IOException {
// is.skipBytes(2); long l = readLong(is);
// return new StatusReply(status); if (l > Integer.MAX_VALUE) {
// } throw new IllegalArgumentException(
// case ErrorReply.MARKER: { "Java only supports arrays up to " + Integer.MAX_VALUE + " in size");
// String error = is.readBytes(is.bytesBefore((byte) '\r')).toString(Charsets.UTF_8); }
// is.skipBytes(2); int size = (int) l;
// return new ErrorReply(error); if (size == -1) {
// } return null;
// case IntegerReply.MARKER: { }
// return new IntegerReply(readLong(is)); ByteBuf buffer = is.readSlice(size);
// } int cr = is.readByte();
// case BulkReply.MARKER: { int lf = is.readByte();
// return new BulkReply(readBytes(is)); if (cr != CR || lf != LF) {
// } throw new IOException("Improper line ending: " + cr + ", " + lf);
// case MultiBulkReply.MARKER: { }
// if (reply == null) { return buffer;
// return decodeMultiBulkReply(is); }
// } else {
// return new RedisReplyDecoder(false).decodeMultiBulkReply(is); public static long readLong(ByteBuf is) throws IOException {
// } long size = 0;
// } int sign = 1;
// default: { int read = is.readByte();
// throw new IOException("Unexpected character in stream: " + code); if (read == '-') {
// } read = is.readByte();
// } sign = -1;
}
do {
if (read == CR) {
if (is.readByte() == LF) {
break;
}
}
int value = read - ZERO;
if (value >= 0 && value < 10) {
size *= 10;
size += value;
} else {
throw new IOException("Invalid character in integer");
}
read = is.readByte();
} while (true);
return size * sign;
} }
} }

@ -22,14 +22,14 @@ import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
public class RedisEncoder extends MessageToByteEncoder<RedisData<Object>> { public class RedisEncoder extends MessageToByteEncoder<RedisData<Object, Object>> {
final char ARGS_PREFIX = '*'; final char ARGS_PREFIX = '*';
final char BYTES_PREFIX = '$'; final char BYTES_PREFIX = '$';
final byte[] CRLF = "\r\n".getBytes(); final byte[] CRLF = "\r\n".getBytes();
@Override @Override
protected void encode(ChannelHandlerContext ctx, RedisData<Object> msg, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, RedisData<Object, Object> msg, ByteBuf out) throws Exception {
out.writeByte(ARGS_PREFIX); out.writeByte(ARGS_PREFIX);
out.writeBytes(toChars(1 + msg.getParams().length)); out.writeBytes(toChars(1 + msg.getParams().length));
out.writeBytes(CRLF); out.writeBytes(CRLF);
@ -39,8 +39,8 @@ public class RedisEncoder extends MessageToByteEncoder<RedisData<Object>> {
writeArgument(out, param.toString().getBytes("UTF-8")); writeArgument(out, param.toString().getBytes("UTF-8"));
} }
String o = out.toString(CharsetUtil.UTF_8); // String o = out.toString(CharsetUtil.UTF_8);
System.out.println(o); // System.out.println(o);
} }
private void writeArgument(ByteBuf out, byte[] arg) { private void writeArgument(ByteBuf out, byte[] arg) {

@ -0,0 +1,5 @@
package org.redisson.client.protocol;
public interface Codec extends Encoder, Decoder<Object> {
}

@ -15,6 +15,10 @@
*/ */
package org.redisson.client.protocol; package org.redisson.client.protocol;
public class ObjectDecoder implements ResponseDecoder<Object> { import io.netty.buffer.ByteBuf;
public interface Decoder<R> {
R decode(ByteBuf buf);
} }

@ -0,0 +1,7 @@
package org.redisson.client.protocol;
public interface Encoder {
Object encode(Object in);
}

@ -17,10 +17,14 @@ package org.redisson.client.protocol;
public class RedisCommand<R> { public class RedisCommand<R> {
private String name; private final String name;
private ResponseDecoder<R> reponseDecoder; private Decoder<R> reponseDecoder;
public RedisCommand(String name, ResponseDecoder<R> reponseDecoder) { public RedisCommand(String name) {
this(name, null);
}
public RedisCommand(String name, Decoder<R> reponseDecoder) {
super(); super();
this.name = name; this.name = name;
this.reponseDecoder = reponseDecoder; this.reponseDecoder = reponseDecoder;
@ -30,7 +34,7 @@ public class RedisCommand<R> {
return name; return name;
} }
public ResponseDecoder<R> getReponseDecoder() { public Decoder<R> getReponseDecoder() {
return reponseDecoder; return reponseDecoder;
} }

@ -17,7 +17,7 @@ package org.redisson.client.protocol;
public interface RedisCommands { public interface RedisCommands {
RedisCommand<Object> GET = new RedisCommand<Object>("GET", new ObjectDecoder()); RedisCommand<Object> GET = new RedisCommand<Object>("GET");
RedisCommand<String> SET = new RedisCommand<String>("SET", new StringDecoder()); RedisCommand<String> SET = new RedisCommand<String>("SET", new StringReplayDecoder());
} }

@ -0,0 +1,18 @@
package org.redisson.client.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
public class StringCodec implements Codec {
@Override
public Object encode(Object in) {
return in.toString();
}
@Override
public Object decode(ByteBuf buf) {
return buf.toString(CharsetUtil.UTF_8);
}
}

@ -1,20 +0,0 @@
/**
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson.client.protocol;
public class StringDecoder implements ResponseDecoder<String> {
}

@ -15,6 +15,16 @@
*/ */
package org.redisson.client.protocol; package org.redisson.client.protocol;
public interface ResponseDecoder<R> { import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
public class StringReplayDecoder implements Decoder<String> {
@Override
public String decode(ByteBuf buf) {
String status = buf.readBytes(buf.bytesBefore((byte) '\r')).toString(CharsetUtil.UTF_8);
buf.skipBytes(2);
return status;
}
} }
Loading…
Cancel
Save