Feature - ZStdCodec added #6270

pull/6272/head
Nikita Koksharov 4 months ago
parent 560536140a
commit 0d6fb4b301

@ -243,6 +243,13 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>1.5.6-7</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>

@ -0,0 +1,113 @@
/**
* Copyright (c) 2013-2024 Nikita Koksharov
*
* 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 com.github.luben.zstd.ZstdInputStream;
import com.github.luben.zstd.ZstdOutputStream;
import io.netty.buffer.*;
import org.redisson.client.codec.BaseCodec;
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 java.io.IOException;
/**
* ZStandard codec.
* Uses inner <code>Codec</code> to convert object to binary stream.
* <code>Kryo5Codec</code> is used by default.
* <p>
* Based on <a href="https://github.com/luben/zstd-jni">https://github.com/luben/zstd-jni</a>
*
* Fully thread-safe.
*
* @see Kryo5Codec
*
* @author Nikita Koksharov
*
*/
public class ZStdCodec extends BaseCodec {
private final Codec innerCodec;
public ZStdCodec() {
this(new Kryo5Codec());
}
public ZStdCodec(Codec innerCodec) {
this.innerCodec = innerCodec;
}
public ZStdCodec(ClassLoader classLoader) {
this(new Kryo5Codec(classLoader));
}
public ZStdCodec(ClassLoader classLoader, ZStdCodec codec) throws ReflectiveOperationException {
this(copy(classLoader, codec.innerCodec));
}
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
int size = buf.readInt();
ZstdInputStream s = new ZstdInputStream(new ByteBufInputStream(buf));
byte[] bytes = new byte[size];
s.read(bytes);
s.close();
ByteBuf in = Unpooled.wrappedBuffer(bytes);
try {
return innerCodec.getValueDecoder().decode(in, state);
} finally {
in.release();
}
}
};
private final Encoder encoder = new Encoder() {
@Override
public ByteBuf encode(Object in) throws IOException {
ByteBuf encoded = innerCodec.getValueEncoder().encode(in);
ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
ZstdOutputStream o = new ZstdOutputStream(new ByteBufOutputStream(out));
byte[] bytes = new byte[encoded.readableBytes()];
encoded.readBytes(bytes);
encoded.release();
out.writeInt(bytes.length);
o.write(bytes);
o.flush();
o.close();
return out;
}
};
@Override
public Decoder<Object> getValueDecoder() {
return decoder;
}
@Override
public Encoder getValueEncoder() {
return encoder;
}
}

@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class RedissonCodecTest extends RedisDockerTest {
private Codec kryo5Codec = new Kryo5Codec();
private Codec furyCodec = new FuryCodec();
private Codec zstdCodec = new ZStdCodec();
private Codec smileCodec = new SmileJacksonCodec();
private Codec codec = new SerializationCodec();
private Codec jsonCodec = new JsonJacksonCodec();
@ -72,6 +73,15 @@ public class RedissonCodecTest extends RedisDockerTest {
test(redisson);
}
@Test
public void testZstd() {
Config config = createConfig();
config.setCodec(zstdCodec);
RedissonClient redisson = Redisson.create(config);
test(redisson);
}
@Test
public void testFury() {
Config config = createConfig();

Loading…
Cancel
Save