LZ4 codec added. #267

pull/282/head
Nikita 9 years ago
parent 590d097d10
commit 1bdd0a59d7

@ -140,6 +140,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.jpountz.lz4</groupId>
<artifactId>lz4</artifactId>
<version>1.3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>jackson-dataformat-msgpack</artifactId>

@ -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 <code>Codec</codec> to convert object to binary stream.
* <codec>FstCodec</codec> 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<Object> decoder = new Decoder<Object>() {
@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<Object> getMapValueDecoder() {
return getValueDecoder();
}
@Override
public Encoder getMapValueEncoder() {
return getValueEncoder();
}
@Override
public Decoder<Object> getMapKeyDecoder() {
return getValueDecoder();
}
@Override
public Encoder getMapKeyEncoder() {
return getValueEncoder();
}
@Override
public Decoder<Object> getValueDecoder() {
return decoder;
}
@Override
public Encoder getValueEncoder() {
return encoder;
}
}

@ -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() {

Loading…
Cancel
Save