From 1bdd0a59d7aa94ee35f280abf4904260e5c7aca5 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 13 Nov 2015 13:56:06 +0300 Subject: [PATCH] LZ4 codec added. #267 --- pom.xml | 6 + .../java/org/redisson/codec/LZ4Codec.java | 110 ++++++++++++++++++ .../java/org/redisson/RedissonCodecTest.java | 11 ++ 3 files changed, 127 insertions(+) create mode 100644 src/main/java/org/redisson/codec/LZ4Codec.java diff --git a/pom.xml b/pom.xml index b17614976..513155e8b 100644 --- a/pom.xml +++ b/pom.xml @@ -140,6 +140,12 @@ test + + net.jpountz.lz4 + lz4 + 1.3.0 + provided + org.msgpack jackson-dataformat-msgpack diff --git a/src/main/java/org/redisson/codec/LZ4Codec.java b/src/main/java/org/redisson/codec/LZ4Codec.java new file mode 100644 index 000000000..01afffa60 --- /dev/null +++ b/src/main/java/org/redisson/codec/LZ4Codec.java @@ -0,0 +1,110 @@ +/** + * 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.io.IOException; + +import org.redisson.client.codec.Codec; +import org.redisson.client.handler.State; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.Encoder; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import net.jpountz.lz4.LZ4Compressor; +import net.jpountz.lz4.LZ4Factory; +import net.jpountz.lz4.LZ4SafeDecompressor; + +/** + * LZ4 compression codec. + * Uses inner Codec to convert object to binary stream. + * FstCodec used by default. + * + * https://github.com/jpountz/lz4-java + * + * @see org.redisson.codec.FstCodec + * + * @author Nikita Koksharov + * + */ +public class LZ4Codec implements Codec { + + private final LZ4Factory factory = LZ4Factory.fastestInstance(); + + private final Codec innerCodec; + + public LZ4Codec() { + this(new FstCodec()); + } + + public LZ4Codec(Codec innerCodec) { + this.innerCodec = innerCodec; + } + + private final Decoder decoder = new Decoder() { + @Override + public Object decode(ByteBuf buf, State state) throws IOException { + byte[] bytes = new byte[buf.readableBytes()]; + buf.readBytes(bytes); + + LZ4SafeDecompressor decompressor = factory.safeDecompressor(); + bytes = decompressor.decompress(bytes, bytes.length*3); + ByteBuf bf = Unpooled.wrappedBuffer(bytes); + return innerCodec.getValueDecoder().decode(bf, state); + } + }; + + private final Encoder encoder = new Encoder() { + + @Override + public byte[] encode(Object in) throws IOException { + LZ4Compressor compressor = factory.fastCompressor(); + byte[] bytes = innerCodec.getValueEncoder().encode(in); + return compressor.compress(bytes); + } + }; + + @Override + public Decoder getMapValueDecoder() { + return getValueDecoder(); + } + + @Override + public Encoder getMapValueEncoder() { + return getValueEncoder(); + } + + @Override + public Decoder getMapKeyDecoder() { + return getValueDecoder(); + } + + @Override + public Encoder getMapKeyEncoder() { + return getValueEncoder(); + } + + @Override + public Decoder getValueDecoder() { + return decoder; + } + + @Override + public Encoder getValueEncoder() { + return encoder; + } + +} diff --git a/src/test/java/org/redisson/RedissonCodecTest.java b/src/test/java/org/redisson/RedissonCodecTest.java index 3b4903a4d..5bc89be98 100644 --- a/src/test/java/org/redisson/RedissonCodecTest.java +++ b/src/test/java/org/redisson/RedissonCodecTest.java @@ -12,6 +12,7 @@ import org.redisson.codec.CborJacksonCodec; import org.redisson.codec.FstCodec; import org.redisson.codec.JsonJacksonCodec; import org.redisson.codec.KryoCodec; +import org.redisson.codec.LZ4Codec; import org.redisson.codec.MsgPackJacksonCodec; import org.redisson.codec.SerializationCodec; import org.redisson.codec.SnappyCodec; @@ -25,6 +26,16 @@ public class RedissonCodecTest extends BaseTest { private Codec fstCodec = new FstCodec(); private Codec snappyCodec = new SnappyCodec(); private Codec msgPackCodec = new MsgPackJacksonCodec(); + private Codec lz4Codec = new LZ4Codec(); + + @Test + public void testLZ4() { + Config config = createConfig(); + config.setCodec(lz4Codec); + redisson = Redisson.create(config); + + test(); + } @Test public void testJdk() {