Skip to content

Commit

Permalink
Polishing #1122
Browse files Browse the repository at this point in the history
Rename wrapper type to ComposedRedisCodec. Introduce RedisCodec.of(…) factory method.

Add tests.

Original pull request: #1123.
  • Loading branch information
mp911de committed Sep 19, 2019
1 parent 10bbfd9 commit 2c2615a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 78 deletions.
59 changes: 59 additions & 0 deletions src/main/java/io/lettuce/core/codec/ComposedRedisCodec.java
Original file line number Diff line number Diff line change
@@ -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<K, V> implements RedisCodec<K, V> {

private final RedisCodec<K, ?> keyCodec;
private final RedisCodec<?, V> valueCodec;

ComposedRedisCodec(RedisCodec<K, ?> keyCodec, RedisCodec<?, V> 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);
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/lettuce/core/codec/RedisCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,24 @@
*
* @author Will Glozer
* @author Mark Paluch
* @author Dimitris Mandalidis
*/
public interface RedisCodec<K, V> {

/**
* Returns a composite {@link RedisCodec} that uses {@code keyCodec} for keys and {@code valueCodec} for values.
*
* @param <K> the type of the key
* @param <V> 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 <K, V> RedisCodec<K, V> of(RedisCodec<K, ?> keyCodec, RedisCodec<?, V> valueCodec) {
return new ComposedRedisCodec<>(keyCodec, valueCodec);
}

/**
* Decode the key output by redis.
*
Expand Down
71 changes: 0 additions & 71 deletions src/main/java/io/lettuce/core/codec/RedisCodecAdapter.java

This file was deleted.

38 changes: 31 additions & 7 deletions src/test/java/io/lettuce/core/CustomCodecIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,8 +96,10 @@ void testJavaSerializerReactive() {

@Test
void testDeflateCompressedJavaSerializer() {
RedisCommands<String, Object> connection = client.connect(
CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.DEFLATE)).sync();
RedisCommands<String, Object> connection = client
.connect(
CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.DEFLATE))
.sync();
List<String> list = list("one", "two");
connection.set(key, list);
assertThat(connection.get(key)).isEqualTo(list);
Expand All @@ -106,8 +109,9 @@ void testDeflateCompressedJavaSerializer() {

@Test
void testGzipompressedJavaSerializer() {
RedisCommands<String, Object> connection = client.connect(
CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.GZIP)).sync();
RedisCommands<String, Object> connection = client
.connect(CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.GZIP))
.sync();
List<String> list = list("one", "two");
connection.set(key, list);
assertThat(connection.get(key)).isEqualTo(list);
Expand All @@ -118,8 +122,8 @@ void testGzipompressedJavaSerializer() {
@Test
void testEncryptedCodec() {

RedisCommands<String, String> connection = client.connect(
CipherCodec.forValues(StringCodec.UTF8, encrypt, decrypt)).sync();
RedisCommands<String, String> connection = client.connect(CipherCodec.forValues(StringCodec.UTF8, encrypt, decrypt))
.sync();

connection.set(key, "foobar");
assertThat(connection.get(key)).isEqualTo("foobar");
Expand Down Expand Up @@ -163,9 +167,25 @@ void testByteBufferCodec() {
connection.getStatefulConnection().close();
}

@Test
void testComposedCodec() {

RedisCodec<String, Object> composed = RedisCodec.of(StringCodec.ASCII, new SerializedObjectCodec());
RedisCommands<String, Object> connection = client.connect(composed).sync();

connection.set(key, new Person());

List<String> keys = connection.keys(key);
assertThat(keys).hasSize(1);

assertThat(connection.get(key)).isInstanceOf(Person.class);

connection.getStatefulConnection().close();
}

class SerializedObjectCodec implements RedisCodec<String, Object> {

private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;

@Override
public String decodeKey(ByteBuffer bytes) {
Expand Down Expand Up @@ -201,4 +221,8 @@ public ByteBuffer encodeValue(Object value) {
}
}
}

static class Person implements Serializable {

}
}

0 comments on commit 2c2615a

Please sign in to comment.