From 05c8468563b626a4c10f0e9c6dc65c4df4b61ed9 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 9 Aug 2016 16:11:40 +0300 Subject: [PATCH] Avro and Smile codecs added. #571 --- pom.xml | 12 ++++++ .../org/redisson/codec/AvroJacksonCodec.java | 37 +++++++++++++++++++ .../org/redisson/codec/JsonJacksonCodec.java | 3 +- .../org/redisson/codec/SmileJacksonCodec.java | 37 +++++++++++++++++++ .../java/org/redisson/RedissonCodecTest.java | 23 +++++++++++- 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/redisson/codec/AvroJacksonCodec.java create mode 100644 src/main/java/org/redisson/codec/SmileJacksonCodec.java diff --git a/pom.xml b/pom.xml index 75227f5ef..3bfeaa23f 100644 --- a/pom.xml +++ b/pom.xml @@ -214,6 +214,18 @@ 2.6.5 provided + + com.fasterxml.jackson.dataformat + jackson-dataformat-smile + 2.6.5 + provided + + + com.fasterxml.jackson.dataformat + jackson-dataformat-avro + 2.6.5 + provided + net.openhft zero-allocation-hashing diff --git a/src/main/java/org/redisson/codec/AvroJacksonCodec.java b/src/main/java/org/redisson/codec/AvroJacksonCodec.java new file mode 100644 index 000000000..4994bbd64 --- /dev/null +++ b/src/main/java/org/redisson/codec/AvroJacksonCodec.java @@ -0,0 +1,37 @@ +/** + * Copyright 2016 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.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.avro.AvroFactory; + +/** + * Avro binary codec + * + * @author Nikita Koksharov + * + */ +public class AvroJacksonCodec extends JsonJacksonCodec { + + public AvroJacksonCodec() { + super(new ObjectMapper(new AvroFactory())); + } + + public AvroJacksonCodec(ClassLoader classLoader) { + super(createObjectMapper(classLoader, new ObjectMapper(new AvroFactory()))); + } + +} diff --git a/src/main/java/org/redisson/codec/JsonJacksonCodec.java b/src/main/java/org/redisson/codec/JsonJacksonCodec.java index 815cede52..9b5a271d3 100755 --- a/src/main/java/org/redisson/codec/JsonJacksonCodec.java +++ b/src/main/java/org/redisson/codec/JsonJacksonCodec.java @@ -124,7 +124,8 @@ public class JsonJacksonCodec implements Codec { // warm up codec try { - mapObjectMapper.readValue("1".getBytes(), Object.class); + byte[] s = mapObjectMapper.writeValueAsBytes(1); + mapObjectMapper.readValue(s, Object.class); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/src/main/java/org/redisson/codec/SmileJacksonCodec.java b/src/main/java/org/redisson/codec/SmileJacksonCodec.java new file mode 100644 index 000000000..7f8e84cd9 --- /dev/null +++ b/src/main/java/org/redisson/codec/SmileJacksonCodec.java @@ -0,0 +1,37 @@ +/** + * Copyright 2016 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.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.smile.SmileFactory; + +/** + * Smile binary codec + * + * @author Nikita Koksharov + * + */ +public class SmileJacksonCodec extends JsonJacksonCodec { + + public SmileJacksonCodec() { + super(new ObjectMapper(new SmileFactory())); + } + + public SmileJacksonCodec(ClassLoader classLoader) { + super(createObjectMapper(classLoader, new ObjectMapper(new SmileFactory()))); + } + +} diff --git a/src/test/java/org/redisson/RedissonCodecTest.java b/src/test/java/org/redisson/RedissonCodecTest.java index 8f6d2574d..bab1670ea 100644 --- a/src/test/java/org/redisson/RedissonCodecTest.java +++ b/src/test/java/org/redisson/RedissonCodecTest.java @@ -13,6 +13,7 @@ import org.redisson.codec.KryoCodec; import org.redisson.codec.LZ4Codec; import org.redisson.codec.MsgPackJacksonCodec; import org.redisson.codec.SerializationCodec; +import org.redisson.codec.SmileJacksonCodec; import org.redisson.codec.SnappyCodec; import org.redisson.config.Config; @@ -26,6 +27,8 @@ import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; public class RedissonCodecTest extends BaseTest { + private Codec avroCodec = new SmileJacksonCodec(); + private Codec smileCodec = new SmileJacksonCodec(); private Codec codec = new SerializationCodec(); private Codec kryoCodec = new KryoCodec(); private Codec jsonCodec = new JsonJacksonCodec(); @@ -54,7 +57,7 @@ public class RedissonCodecTest extends BaseTest { test(); } - + @Test public void testMsgPack() { Config config = createConfig(); @@ -63,6 +66,24 @@ public class RedissonCodecTest extends BaseTest { test(); } + + @Test + public void testSmile() { + Config config = createConfig(); + config.setCodec(smileCodec); + redisson = Redisson.create(config); + + test(); + } + + @Test + public void testAvro() { + Config config = createConfig(); + config.setCodec(avroCodec); + redisson = Redisson.create(config); + + test(); + } @Test public void testFst() {