From 3e92fb70cee8419cfb2fa425ac8e24a849108c10 Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 15 Jul 2015 15:53:20 +0300 Subject: [PATCH] decoders reimplemented --- .../org/redisson/codec/JsonJacksonCodec.java | 25 ---- .../java/org/redisson/codec/KryoCodec.java | 124 +++++++++--------- .../org/redisson/codec/RedissonCodec.java | 43 ------ .../redisson/codec/SerializationCodec.java | 99 +++++++------- .../java/org/redisson/codec/StringCodec.java | 65 --------- 5 files changed, 108 insertions(+), 248 deletions(-) delete mode 100644 src/main/java/org/redisson/codec/RedissonCodec.java delete mode 100644 src/main/java/org/redisson/codec/StringCodec.java diff --git a/src/main/java/org/redisson/codec/JsonJacksonCodec.java b/src/main/java/org/redisson/codec/JsonJacksonCodec.java index e916418ab..5ea50bae5 100755 --- a/src/main/java/org/redisson/codec/JsonJacksonCodec.java +++ b/src/main/java/org/redisson/codec/JsonJacksonCodec.java @@ -43,16 +43,9 @@ import io.netty.buffer.ByteBufInputStream; */ public class JsonJacksonCodec implements Codec { - private final ObjectMapper objectMapper = new ObjectMapper(); private ObjectMapper mapObjectMapper = new ObjectMapper(); public JsonJacksonCodec() { - init(objectMapper); - TypeResolverBuilder typer = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL); - typer.init(JsonTypeInfo.Id.CLASS, null); - typer.inclusion(JsonTypeInfo.As.PROPERTY); - objectMapper.setDefaultTyping(typer); - init(mapObjectMapper); // type info inclusion TypeResolverBuilder mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) { @@ -136,30 +129,12 @@ public class JsonJacksonCodec implements Codec { @Override public Decoder getValueDecoder() { -// return new Decoder() { -// -// @Override -// public Object decode(ByteBuf buf) throws IOException { -// if (buf == null) { -// return null; -// } -// -// return objectMapper.readValue(new ByteBufInputStream(buf), Object.class); -// } -// }; return getMapValueDecoder(); } @Override public Encoder getValueEncoder() { return getMapValueEncoder(); -// return new Encoder() { -// -// @Override -// public byte[] encode(int paramIndex, Object in) throws IOException { -// return objectMapper.writeValueAsBytes(in); -// } -// }; } } diff --git a/src/main/java/org/redisson/codec/KryoCodec.java b/src/main/java/org/redisson/codec/KryoCodec.java index 5dbeeabf4..633db6599 100755 --- a/src/main/java/org/redisson/codec/KryoCodec.java +++ b/src/main/java/org/redisson/codec/KryoCodec.java @@ -15,20 +15,25 @@ */ package org.redisson.codec; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; +import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import org.redisson.client.protocol.Codec; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; -public class KryoCodec implements RedissonCodec { +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufInputStream; + +public class KryoCodec implements Codec { public interface KryoPool { @@ -100,79 +105,76 @@ public class KryoCodec implements RedissonCodec { this.kryoPool = kryoPool; } - private Object decode(ByteBuffer bytes) { - Kryo kryo = null; - try { - kryo = kryoPool.get(); - return kryo.readClassAndObject(new Input(new ByteArrayInputStream(bytes.array(), bytes - .arrayOffset() + bytes.position(), bytes.limit()))); - } catch (Exception e) { - if (e instanceof RuntimeException) { - throw (RuntimeException) e; - } - throw new RedissonKryoCodecException(e); - } finally { - if (kryo != null) { - kryoPool.yield(kryo); - } - } - } - - @Override - public byte[] encodeValue(Object value) { - Kryo kryo = null; - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Output output = new Output(baos); - kryo = kryoPool.get(); - kryo.writeClassAndObject(output, value); - output.close(); - return baos.toByteArray(); - } catch (Exception e) { - if (e instanceof RuntimeException) { - throw (RuntimeException) e; - } - throw new RedissonKryoCodecException(e); - } finally { - if (kryo != null) { - kryoPool.yield(kryo); - } - } - } - @Override - public byte[] encodeKey(Object key) { - return key.toString().getBytes(Charset.forName("ASCII")); + public Decoder getMapValueDecoder() { + return getValueDecoder(); } @Override - public Object decodeKey(ByteBuffer bytes) { - return new String(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit(), Charset.forName("ASCII")); + public Encoder getMapValueEncoder() { + return getValueEncoder(); } @Override - public Object decodeValue(ByteBuffer bytes) { - return decode(bytes); + public Decoder getMapKeyDecoder() { + return getValueDecoder(); } @Override - public byte[] encodeMapValue(Object value) { - return encodeValue(value); + public Encoder getMapKeyEncoder() { + return getValueEncoder(); } @Override - public byte[] encodeMapKey(Object key) { - return encodeKey(key); - } - - @Override - public Object decodeMapValue(ByteBuffer bytes) { - return decodeValue(bytes); + public Decoder getValueDecoder() { + return new Decoder() { + + @Override + public Object decode(ByteBuf buf) throws IOException { + Kryo kryo = null; + try { + kryo = kryoPool.get(); + return kryo.readClassAndObject(new Input(new ByteBufInputStream(buf))); + } catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new RedissonKryoCodecException(e); + } finally { + if (kryo != null) { + kryoPool.yield(kryo); + } + } + } + }; } @Override - public Object decodeMapKey(ByteBuffer bytes) { - return decodeKey(bytes); + public Encoder getValueEncoder() { + return new Encoder() { + + @Override + public byte[] encode(Object in) throws IOException { + Kryo kryo = null; + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Output output = new Output(baos); + kryo = kryoPool.get(); + kryo.writeClassAndObject(output, in); + output.close(); + return baos.toByteArray(); + } catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new RedissonKryoCodecException(e); + } finally { + if (kryo != null) { + kryoPool.yield(kryo); + } + } + } + }; } } diff --git a/src/main/java/org/redisson/codec/RedissonCodec.java b/src/main/java/org/redisson/codec/RedissonCodec.java deleted file mode 100644 index ee0e5433a..000000000 --- a/src/main/java/org/redisson/codec/RedissonCodec.java +++ /dev/null @@ -1,43 +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.codec; - -import java.nio.ByteBuffer; - -/** - * - * @author Nikita Koksharov - * - */ -public interface RedissonCodec { - - Object decodeKey(ByteBuffer bytes); - - Object decodeValue(ByteBuffer bytes); - - byte[] encodeKey(Object key); - - byte[] encodeValue(Object value); - - byte[] encodeMapValue(Object value); - - byte[] encodeMapKey(Object key); - - Object decodeMapValue(ByteBuffer bytes); - - Object decodeMapKey(ByteBuffer bytes); - -} diff --git a/src/main/java/org/redisson/codec/SerializationCodec.java b/src/main/java/org/redisson/codec/SerializationCodec.java index 1aeac3901..34965f5dd 100644 --- a/src/main/java/org/redisson/codec/SerializationCodec.java +++ b/src/main/java/org/redisson/codec/SerializationCodec.java @@ -15,84 +15,75 @@ */ package org.redisson.codec; -import java.io.*; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import org.redisson.client.protocol.Codec; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufInputStream; /** * * @author Nikita Koksharov * */ -public class SerializationCodec implements RedissonCodec { +public class SerializationCodec implements Codec { @Override - public Object decodeKey(ByteBuffer bytes) { - return new String(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit(), Charset.forName("ASCII")); + public Decoder getMapValueDecoder() { + return getValueDecoder(); } @Override - public Object decodeValue(ByteBuffer bytes) { - return decode(bytes); - } - - private Object decode(ByteBuffer bytes) { - try { - ByteArrayInputStream in = new ByteArrayInputStream(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit()); - ObjectInputStream inputStream = new ObjectInputStream(in); - return inputStream.readObject(); - } catch (Exception e) { - throw new IllegalStateException(e); - } + public Encoder getMapValueEncoder() { + return getValueEncoder(); } @Override - public byte[] encodeKey(Object key) { - return key.toString().getBytes(Charset.forName("ASCII")); + public Decoder getMapKeyDecoder() { + return getValueDecoder(); } @Override - public byte[] encodeValue(Object value) { - try { - ByteArrayOutputStream result = new ByteArrayOutputStream(); - ObjectOutputStream outputStream = new ObjectOutputStream(result); - outputStream.writeObject(value); - outputStream.close(); - return result.toByteArray(); - } catch (Exception e) { - throw new IllegalStateException(e); - } + public Encoder getMapKeyEncoder() { + return getValueEncoder(); } @Override - public byte[] encodeMapValue(Object value) { - return encodeValue(value); + public Decoder getValueDecoder() { + return new Decoder() { + @Override + public Object decode(ByteBuf buf) throws IOException { + try { + ObjectInputStream inputStream = new ObjectInputStream(new ByteBufInputStream(buf)); + return inputStream.readObject(); + } catch (IOException e) { + throw e; + } catch (Exception e) { + throw new IOException(e); + } + } + }; } @Override - public byte[] encodeMapKey(Object key) { - return encodeValue(key); - } - - @Override - public Object decodeMapValue(ByteBuffer bytes) { - return decodeValue(bytes); - } - - @Override - public Object decodeMapKey(ByteBuffer bytes) { - return decodeValue(bytes); - } + public Encoder getValueEncoder() { + return new Encoder() { - protected String decodeAscii(ByteBuffer bytes) { - if (bytes == null) { - return null; - } - char[] chars = new char[bytes.remaining()]; - for (int i = 0; i < chars.length; i++) { - chars[i] = (char) bytes.get(); - } - return new String(chars); + @Override + public byte[] encode(Object in) throws IOException { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + ObjectOutputStream outputStream = new ObjectOutputStream(result); + outputStream.writeObject(in); + outputStream.close(); + return result.toByteArray(); + } + }; } } diff --git a/src/main/java/org/redisson/codec/StringCodec.java b/src/main/java/org/redisson/codec/StringCodec.java deleted file mode 100644 index 3797b577c..000000000 --- a/src/main/java/org/redisson/codec/StringCodec.java +++ /dev/null @@ -1,65 +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.codec; - -import java.nio.ByteBuffer; -import java.nio.charset.Charset; - -public class StringCodec implements RedissonCodec { - - private Charset charset = Charset.forName("UTF-8"); - - @Override - public Object decodeKey(ByteBuffer bytes) { - return decodeValue(bytes); - } - - @Override - public Object decodeValue(ByteBuffer bytes) { - return new String(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit(), charset); - } - - @Override - public byte[] encodeKey(Object key) { - return encodeValue((String)key); - } - - @Override - public byte[] encodeValue(Object value) { - return ((String)value).getBytes(charset); - } - - @Override - public byte[] encodeMapValue(Object value) { - return encodeValue((String)value); - } - - @Override - public byte[] encodeMapKey(Object key) { - return encodeValue((String)key); - } - - @Override - public Object decodeMapValue(ByteBuffer bytes) { - return decodeValue(bytes); - } - - @Override - public Object decodeMapKey(ByteBuffer bytes) { - return decodeValue(bytes); - } - -}