From 2c2615ad3898dffea05b712a74ce8c71f7f4ec5f Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 19 Sep 2019 12:45:27 +0200 Subject: [PATCH] Polishing #1122 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rename wrapper type to ComposedRedisCodec. Introduce RedisCodec.of(…) factory method. Add tests. Original pull request: #1123. --- .../core/codec/ComposedRedisCodec.java | 59 +++++++++++++++ .../io/lettuce/core/codec/RedisCodec.java | 16 +++++ .../lettuce/core/codec/RedisCodecAdapter.java | 71 ------------------- .../core/CustomCodecIntegrationTests.java | 38 ++++++++-- 4 files changed, 106 insertions(+), 78 deletions(-) create mode 100644 src/main/java/io/lettuce/core/codec/ComposedRedisCodec.java delete mode 100644 src/main/java/io/lettuce/core/codec/RedisCodecAdapter.java diff --git a/src/main/java/io/lettuce/core/codec/ComposedRedisCodec.java b/src/main/java/io/lettuce/core/codec/ComposedRedisCodec.java new file mode 100644 index 0000000000..a9c61b0549 --- /dev/null +++ b/src/main/java/io/lettuce/core/codec/ComposedRedisCodec.java @@ -0,0 +1,59 @@ +/* + * Copyright 2019 the original author or authors. + * + * 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 io.lettuce.core.codec; + +import java.nio.ByteBuffer; + +import io.lettuce.core.internal.LettuceAssert; + +/** + * A {@link ComposedRedisCodec} combines two {@link RedisCodec cdecs} to encode/decode key and value to the command output. + * + * @author Dimitris Mandalidis + * @since 5.2 + */ +class ComposedRedisCodec implements RedisCodec { + + private final RedisCodec keyCodec; + private final RedisCodec valueCodec; + + ComposedRedisCodec(RedisCodec keyCodec, RedisCodec valueCodec) { + LettuceAssert.notNull(keyCodec, "Key codec must not be null"); + LettuceAssert.notNull(valueCodec, "Value codec must not be null"); + this.keyCodec = keyCodec; + this.valueCodec = valueCodec; + } + + @Override + public K decodeKey(ByteBuffer bytes) { + return keyCodec.decodeKey(bytes); + } + + @Override + public V decodeValue(ByteBuffer bytes) { + return valueCodec.decodeValue(bytes); + } + + @Override + public ByteBuffer encodeKey(K key) { + return keyCodec.encodeKey(key); + } + + @Override + public ByteBuffer encodeValue(V value) { + return valueCodec.encodeValue(value); + } +} diff --git a/src/main/java/io/lettuce/core/codec/RedisCodec.java b/src/main/java/io/lettuce/core/codec/RedisCodec.java index d432b3eecd..46eca2193b 100644 --- a/src/main/java/io/lettuce/core/codec/RedisCodec.java +++ b/src/main/java/io/lettuce/core/codec/RedisCodec.java @@ -27,8 +27,24 @@ * * @author Will Glozer * @author Mark Paluch + * @author Dimitris Mandalidis */ public interface RedisCodec { + + /** + * Returns a composite {@link RedisCodec} that uses {@code keyCodec} for keys and {@code valueCodec} for values. + * + * @param the type of the key + * @param the type of the value + * @param keyCodec the codec to encode/decode the keys. + * @param valueCodec the codec to encode/decode the values. + * @return a composite {@link RedisCodec}. + * @since 5.2 + */ + static RedisCodec of(RedisCodec keyCodec, RedisCodec valueCodec) { + return new ComposedRedisCodec<>(keyCodec, valueCodec); + } + /** * Decode the key output by redis. * diff --git a/src/main/java/io/lettuce/core/codec/RedisCodecAdapter.java b/src/main/java/io/lettuce/core/codec/RedisCodecAdapter.java deleted file mode 100644 index ea523b1d69..0000000000 --- a/src/main/java/io/lettuce/core/codec/RedisCodecAdapter.java +++ /dev/null @@ -1,71 +0,0 @@ -package io.lettuce.core.codec; - -import java.nio.ByteBuffer; - -/* - * Copyright 2011-2019 the original author or authors. - * - * 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. - */ -/** - * A {@link RedisCodecAdapter} combines different codecs to encode/decode key and value - * to the command output. - * - * @author Dimitris Mandalidis - */ -public final class RedisCodecAdapter { - - private RedisCodecAdapter() {} - - /** - * Returns new {@link RedisCodec} - * @param the type of the key - * @param the type of the value - * @param keyCodec the codec to encode/decode the keys - * @param valueCodec the codec to encode/decode the values - * @return - */ - public static RedisCodec of(RedisCodec keyCodec, RedisCodec valueCodec) { - return new RedisCodecWrapper(keyCodec, valueCodec); - } - - private static class RedisCodecWrapper implements RedisCodec { - private final RedisCodec keyCodec; - private final RedisCodec valueCodec; - - RedisCodecWrapper(RedisCodec keyCodec, RedisCodec valueCodec) { - this.keyCodec = keyCodec; - this.valueCodec = valueCodec; - } - - @Override - public K decodeKey(ByteBuffer bytes) { - return keyCodec.decodeKey(bytes); - } - - @Override - public V decodeValue(ByteBuffer bytes) { - return valueCodec.decodeValue(bytes); - } - - @Override - public ByteBuffer encodeKey(K key) { - return keyCodec.encodeKey(key); - } - - @Override - public ByteBuffer encodeValue(V value) { - return valueCodec.encodeValue(value); - } - } -} diff --git a/src/test/java/io/lettuce/core/CustomCodecIntegrationTests.java b/src/test/java/io/lettuce/core/CustomCodecIntegrationTests.java index f9f445a1bb..4cbd80e7f7 100644 --- a/src/test/java/io/lettuce/core/CustomCodecIntegrationTests.java +++ b/src/test/java/io/lettuce/core/CustomCodecIntegrationTests.java @@ -20,6 +20,7 @@ import java.io.*; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.List; import javax.crypto.Cipher; @@ -95,8 +96,10 @@ void testJavaSerializerReactive() { @Test void testDeflateCompressedJavaSerializer() { - RedisCommands connection = client.connect( - CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.DEFLATE)).sync(); + RedisCommands connection = client + .connect( + CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.DEFLATE)) + .sync(); List list = list("one", "two"); connection.set(key, list); assertThat(connection.get(key)).isEqualTo(list); @@ -106,8 +109,9 @@ void testDeflateCompressedJavaSerializer() { @Test void testGzipompressedJavaSerializer() { - RedisCommands connection = client.connect( - CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.GZIP)).sync(); + RedisCommands connection = client + .connect(CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.GZIP)) + .sync(); List list = list("one", "two"); connection.set(key, list); assertThat(connection.get(key)).isEqualTo(list); @@ -118,8 +122,8 @@ void testGzipompressedJavaSerializer() { @Test void testEncryptedCodec() { - RedisCommands connection = client.connect( - CipherCodec.forValues(StringCodec.UTF8, encrypt, decrypt)).sync(); + RedisCommands connection = client.connect(CipherCodec.forValues(StringCodec.UTF8, encrypt, decrypt)) + .sync(); connection.set(key, "foobar"); assertThat(connection.get(key)).isEqualTo("foobar"); @@ -163,9 +167,25 @@ void testByteBufferCodec() { connection.getStatefulConnection().close(); } + @Test + void testComposedCodec() { + + RedisCodec composed = RedisCodec.of(StringCodec.ASCII, new SerializedObjectCodec()); + RedisCommands connection = client.connect(composed).sync(); + + connection.set(key, new Person()); + + List keys = connection.keys(key); + assertThat(keys).hasSize(1); + + assertThat(connection.get(key)).isInstanceOf(Person.class); + + connection.getStatefulConnection().close(); + } + class SerializedObjectCodec implements RedisCodec { - private Charset charset = Charset.forName("UTF-8"); + private Charset charset = StandardCharsets.UTF_8; @Override public String decodeKey(ByteBuffer bytes) { @@ -201,4 +221,8 @@ public ByteBuffer encodeValue(Object value) { } } } + + static class Person implements Serializable { + + } }