Skip to content

Commit

Permalink
Command encoder handlers now are singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Apr 15, 2016
1 parent abca09e commit 4f8663f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/redisson/client/RedisClient.java
Expand Up @@ -19,7 +19,7 @@


import org.redisson.client.handler.CommandDecoder; import org.redisson.client.handler.CommandDecoder;
import org.redisson.client.handler.CommandEncoder; import org.redisson.client.handler.CommandEncoder;
import org.redisson.client.handler.CommandsListEncoder; import org.redisson.client.handler.CommandBatchEncoder;
import org.redisson.client.handler.CommandsQueue; import org.redisson.client.handler.CommandsQueue;
import org.redisson.client.handler.ConnectionWatchdog; import org.redisson.client.handler.ConnectionWatchdog;


Expand Down Expand Up @@ -62,8 +62,8 @@ public RedisClient(EventLoopGroup group, Class<? extends SocketChannel> socketCh
@Override @Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addFirst(new ConnectionWatchdog(bootstrap, channels), ch.pipeline().addFirst(new ConnectionWatchdog(bootstrap, channels),
new CommandEncoder(), CommandEncoder.INSTANCE,
new CommandsListEncoder(), CommandBatchEncoder.INSTANCE,
new CommandsQueue(), new CommandsQueue(),
new CommandDecoder()); new CommandDecoder());
} }
Expand Down
Expand Up @@ -20,19 +20,24 @@


import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;


/** /**
* *
* @author Nikita Koksharov * @author Nikita Koksharov
* *
*/ */
public class CommandsListEncoder extends MessageToByteEncoder<CommandsData> { @Sharable
public class CommandBatchEncoder extends MessageToByteEncoder<CommandsData> {


public static final CommandBatchEncoder INSTANCE = new CommandBatchEncoder();

@Override @Override
protected void encode(ChannelHandlerContext ctx, CommandsData msg, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, CommandsData msg, ByteBuf out) throws Exception {
CommandEncoder encoder = ctx.pipeline().get(CommandEncoder.class);
for (CommandData<?, ?> commandData : msg.getCommands()) { for (CommandData<?, ?> commandData : msg.getCommands()) {
ctx.pipeline().get(CommandEncoder.class).encode(ctx, (CommandData<Object, Object>)commandData, out); encoder.encode(ctx, commandData, out);
} }
} }


Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/redisson/client/handler/CommandEncoder.java
Expand Up @@ -17,17 +17,17 @@


import java.util.List; import java.util.List;


import org.redisson.client.codec.ByteArrayCodec;
import org.redisson.client.codec.StringCodec; import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.CommandData; import org.redisson.client.protocol.CommandData;
import org.redisson.client.protocol.Encoder;
import org.redisson.client.protocol.DefaultParamsEncoder; import org.redisson.client.protocol.DefaultParamsEncoder;
import org.redisson.client.protocol.Encoder;
import org.redisson.client.protocol.RedisCommand.ValueType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.redisson.client.protocol.RedisCommand.ValueType;


import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;


Expand All @@ -39,8 +39,11 @@
* @author Nikita Koksharov * @author Nikita Koksharov
* *
*/ */
public class CommandEncoder extends MessageToByteEncoder<CommandData<Object, Object>> { @Sharable
public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {


public static final CommandEncoder INSTANCE = new CommandEncoder();

private final Logger log = LoggerFactory.getLogger(getClass()); private final Logger log = LoggerFactory.getLogger(getClass());


private final Encoder paramsEncoder = new DefaultParamsEncoder(); private final Encoder paramsEncoder = new DefaultParamsEncoder();
Expand All @@ -50,7 +53,7 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<Object, Obj
private static final byte[] CRLF = "\r\n".getBytes(); private static final byte[] CRLF = "\r\n".getBytes();


@Override @Override
protected void encode(ChannelHandlerContext ctx, CommandData<Object, Object> msg, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, CommandData<?, ?> msg, ByteBuf out) throws Exception {
out.writeByte(ARGS_PREFIX); out.writeByte(ARGS_PREFIX);
int len = 1 + msg.getParams().length; int len = 1 + msg.getParams().length;
if (msg.getCommand().getSubName() != null) { if (msg.getCommand().getSubName() != null) {
Expand Down Expand Up @@ -91,7 +94,7 @@ protected void encode(ChannelHandlerContext ctx, CommandData<Object, Object> msg
} }
} }


private Encoder selectEncoder(CommandData<Object, Object> msg, int param) { private Encoder selectEncoder(CommandData<?, ?> msg, int param) {
int typeIndex = 0; int typeIndex = 0;
List<ValueType> inParamType = msg.getCommand().getInParamType(); List<ValueType> inParamType = msg.getCommand().getInParamType();
if (inParamType.size() > 1) { if (inParamType.size() > 1) {
Expand Down

0 comments on commit 4f8663f

Please sign in to comment.