Codec's encoder/decoder instances caching

pull/255/head
Nikita 10 years ago
parent 40af48f4ae
commit fbef4ee89a

@ -25,14 +25,16 @@ public class LongCodec extends StringCodec {
public static final LongCodec INSTANCE = new LongCodec(); public static final LongCodec INSTANCE = new LongCodec();
public final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) {
return Long.valueOf(buf.toString(CharsetUtil.UTF_8));
}
};
@Override @Override
public Decoder<Object> getValueDecoder() { public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() { return decoder;
@Override
public Object decode(ByteBuf buf, State state) {
return Long.valueOf(buf.toString(CharsetUtil.UTF_8));
}
};
} }
} }

@ -28,24 +28,28 @@ public class StringCodec implements Codec {
public static final StringCodec INSTANCE = new StringCodec(); public static final StringCodec INSTANCE = new StringCodec();
private final Encoder encoder = new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
return in.toString().getBytes("UTF-8");
}
};
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) {
return buf.toString(CharsetUtil.UTF_8);
}
};
@Override @Override
public Decoder<Object> getValueDecoder() { public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() { return decoder;
@Override
public Object decode(ByteBuf buf, State state) {
return buf.toString(CharsetUtil.UTF_8);
}
};
} }
@Override @Override
public Encoder getValueEncoder() { public Encoder getValueEncoder() {
return new Encoder() { return encoder;
@Override
public byte[] encode(Object in) throws IOException {
return in.toString().getBytes("UTF-8");
}
};
} }
@Override @Override

@ -44,7 +44,21 @@ import io.netty.buffer.ByteBufInputStream;
*/ */
public class JsonJacksonCodec implements Codec { public class JsonJacksonCodec implements Codec {
private ObjectMapper mapObjectMapper = new ObjectMapper(); private final ObjectMapper mapObjectMapper = new ObjectMapper();
private final Encoder encoder = new Encoder() {
@Override
public byte[] encode(Object in) throws IOException {
return mapObjectMapper.writeValueAsBytes(in);
}
};
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
return mapObjectMapper.readValue(new ByteBufInputStream(buf), Object.class);
}
};
public JsonJacksonCodec() { public JsonJacksonCodec() {
init(mapObjectMapper); init(mapObjectMapper);
@ -94,24 +108,12 @@ public class JsonJacksonCodec implements Codec {
@Override @Override
public Decoder<Object> getMapValueDecoder() { public Decoder<Object> getMapValueDecoder() {
return new Decoder<Object>() { return decoder;
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
return mapObjectMapper.readValue(new ByteBufInputStream(buf), Object.class);
}
};
} }
@Override @Override
public Encoder getMapValueEncoder() { public Encoder getMapValueEncoder() {
return new Encoder() { return encoder;
@Override
public byte[] encode(Object in) throws IOException {
return mapObjectMapper.writeValueAsBytes(in);
}
};
} }
@Override @Override

@ -93,6 +93,51 @@ public class KryoCodec implements Codec {
private final KryoPool kryoPool; private final KryoPool kryoPool;
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) 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);
}
}
}
};
private final Encoder encoder = 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);
}
}
}
};
public KryoCodec() { public KryoCodec() {
this(new KryoPoolImpl(Collections.<Class<?>>emptyList())); this(new KryoPoolImpl(Collections.<Class<?>>emptyList()));
} }
@ -128,54 +173,12 @@ public class KryoCodec implements Codec {
@Override @Override
public Decoder<Object> getValueDecoder() { public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() { return decoder;
@Override
public Object decode(ByteBuf buf, State state) 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 @Override
public Encoder getValueEncoder() { public Encoder getValueEncoder() {
return new Encoder() { return 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);
}
}
}
};
} }
} }

@ -35,6 +35,32 @@ import io.netty.buffer.ByteBufInputStream;
*/ */
public class SerializationCodec implements Codec { public class SerializationCodec implements Codec {
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) 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);
}
}
};
private final Encoder encoder = new Encoder() {
@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();
}
};
@Override @Override
public Decoder<Object> getMapValueDecoder() { public Decoder<Object> getMapValueDecoder() {
return getValueDecoder(); return getValueDecoder();
@ -57,34 +83,12 @@ public class SerializationCodec implements Codec {
@Override @Override
public Decoder<Object> getValueDecoder() { public Decoder<Object> getValueDecoder() {
return new Decoder<Object>() { return decoder;
@Override
public Object decode(ByteBuf buf, State state) 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 @Override
public Encoder getValueEncoder() { public Encoder getValueEncoder() {
return new Encoder() { return encoder;
@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();
}
};
} }
} }

Loading…
Cancel
Save