From ed339020852bbc4208cd01f648524fdd5b6e2405 Mon Sep 17 00:00:00 2001 From: Nikita Date: Thu, 16 Jul 2015 16:30:53 +0300 Subject: [PATCH] convertMulti method added --- .../client/handler/CommandDecoder.java | 20 +++++++-------- .../convertor/BooleanReplayConvertor.java | 2 +- .../client/protocol/convertor/Convertor.java | 2 ++ .../protocol/convertor/EmptyConvertor.java | 2 +- .../convertor/LongReplayConvertor.java | 2 +- .../protocol/convertor/SingleConvertor.java | 25 +++++++++++++++++++ .../convertor/VoidReplayConvertor.java | 2 +- 7 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/redisson/client/protocol/convertor/SingleConvertor.java diff --git a/src/main/java/org/redisson/client/handler/CommandDecoder.java b/src/main/java/org/redisson/client/handler/CommandDecoder.java index 1e36fea86..42e9c5b1e 100644 --- a/src/main/java/org/redisson/client/handler/CommandDecoder.java +++ b/src/main/java/org/redisson/client/handler/CommandDecoder.java @@ -91,7 +91,7 @@ public class CommandDecoder extends ReplayingDecoder { String result = in.readBytes(in.bytesBefore((byte) '\r')).toString(CharsetUtil.UTF_8); in.skipBytes(2); - handleResult(data, parts, result); + handleResult(data, parts, result, false); } else if (code == '-') { String error = in.readBytes(in.bytesBefore((byte) '\r')).toString(CharsetUtil.UTF_8); in.skipBytes(2); @@ -111,14 +111,14 @@ public class CommandDecoder extends ReplayingDecoder { String status = in.readBytes(in.bytesBefore((byte) '\r')).toString(CharsetUtil.UTF_8); in.skipBytes(2); Object result = Long.valueOf(status); - handleResult(data, parts, result); + handleResult(data, parts, result, false); } else if (code == '$') { ByteBuf buf = readBytes(in); Object result = null; if (buf != null) { result = decoder(data, parts, currentDecoder).decode(buf); } - handleResult(data, parts, result); + handleResult(data, parts, result, false); } else if (code == '*') { long size = readLong(in); List respParts = new ArrayList(); @@ -160,11 +160,7 @@ public class CommandDecoder extends ReplayingDecoder { } } - if (parts != null) { - parts.add(result); - } else { - data.getPromise().setSuccess(result); - } + handleResult(data, parts, result, true); } else { RedisPubSubConnection pubSubConnection = (RedisPubSubConnection)channel.attr(RedisPubSubConnection.CONNECTION).get(); if (result instanceof PubSubMessage) { @@ -175,9 +171,13 @@ public class CommandDecoder extends ReplayingDecoder { } } - private void handleResult(CommandData data, List parts, Object result) { + private void handleResult(CommandData data, List parts, Object result, boolean multiResult) { if (data != null) { - result = data.getCommand().getConvertor().convert(result); + if (multiResult) { + result = data.getCommand().getConvertor().convertMulti(result); + } else { + result = data.getCommand().getConvertor().convert(result); + } } if (parts != null) { parts.add(result); diff --git a/src/main/java/org/redisson/client/protocol/convertor/BooleanReplayConvertor.java b/src/main/java/org/redisson/client/protocol/convertor/BooleanReplayConvertor.java index 9635e3a4e..a26673910 100644 --- a/src/main/java/org/redisson/client/protocol/convertor/BooleanReplayConvertor.java +++ b/src/main/java/org/redisson/client/protocol/convertor/BooleanReplayConvertor.java @@ -15,7 +15,7 @@ */ package org.redisson.client.protocol.convertor; -public class BooleanReplayConvertor implements Convertor { +public class BooleanReplayConvertor extends SingleConvertor { @Override public Boolean convert(Object obj) { diff --git a/src/main/java/org/redisson/client/protocol/convertor/Convertor.java b/src/main/java/org/redisson/client/protocol/convertor/Convertor.java index d7de9ecfd..dd1c72d88 100644 --- a/src/main/java/org/redisson/client/protocol/convertor/Convertor.java +++ b/src/main/java/org/redisson/client/protocol/convertor/Convertor.java @@ -17,6 +17,8 @@ package org.redisson.client.protocol.convertor; public interface Convertor { + Object convertMulti(Object obj); + R convert(Object obj); } diff --git a/src/main/java/org/redisson/client/protocol/convertor/EmptyConvertor.java b/src/main/java/org/redisson/client/protocol/convertor/EmptyConvertor.java index 5af21fcc5..8ebfba99b 100644 --- a/src/main/java/org/redisson/client/protocol/convertor/EmptyConvertor.java +++ b/src/main/java/org/redisson/client/protocol/convertor/EmptyConvertor.java @@ -15,7 +15,7 @@ */ package org.redisson.client.protocol.convertor; -public class EmptyConvertor implements Convertor { +public class EmptyConvertor extends SingleConvertor { @Override public R convert(Object obj) { diff --git a/src/main/java/org/redisson/client/protocol/convertor/LongReplayConvertor.java b/src/main/java/org/redisson/client/protocol/convertor/LongReplayConvertor.java index a10d18242..da7f45606 100644 --- a/src/main/java/org/redisson/client/protocol/convertor/LongReplayConvertor.java +++ b/src/main/java/org/redisson/client/protocol/convertor/LongReplayConvertor.java @@ -15,7 +15,7 @@ */ package org.redisson.client.protocol.convertor; -public class LongReplayConvertor implements Convertor { +public class LongReplayConvertor extends SingleConvertor { @Override public Long convert(Object obj) { diff --git a/src/main/java/org/redisson/client/protocol/convertor/SingleConvertor.java b/src/main/java/org/redisson/client/protocol/convertor/SingleConvertor.java new file mode 100644 index 000000000..c62aec746 --- /dev/null +++ b/src/main/java/org/redisson/client/protocol/convertor/SingleConvertor.java @@ -0,0 +1,25 @@ +/** + * 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.convertor; + +public abstract class SingleConvertor implements Convertor { + + @Override + public Object convertMulti(Object obj) { + return obj; + } + +} diff --git a/src/main/java/org/redisson/client/protocol/convertor/VoidReplayConvertor.java b/src/main/java/org/redisson/client/protocol/convertor/VoidReplayConvertor.java index b63cfcf8b..0dc45fb9a 100644 --- a/src/main/java/org/redisson/client/protocol/convertor/VoidReplayConvertor.java +++ b/src/main/java/org/redisson/client/protocol/convertor/VoidReplayConvertor.java @@ -15,7 +15,7 @@ */ package org.redisson.client.protocol.convertor; -public class VoidReplayConvertor implements Convertor { +public class VoidReplayConvertor extends SingleConvertor { @Override public Void convert(Object obj) {