From 0c37825bd85ed5c482b475899d01eefa98566d0e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Sun, 2 Aug 2015 21:40:16 +0200 Subject: [PATCH] Expose codec/uri methods on RedisClient #108 --- .../com/lambdaworks/redis/RedisClient.java | 165 +++++++++++++----- .../lambdaworks/redis/RedisClientTest.java | 164 +++++++++++++++++ 2 files changed, 282 insertions(+), 47 deletions(-) create mode 100644 src/test/java/com/lambdaworks/redis/RedisClientTest.java diff --git a/src/main/java/com/lambdaworks/redis/RedisClient.java b/src/main/java/com/lambdaworks/redis/RedisClient.java index dbf5cbb341..2b858020cc 100644 --- a/src/main/java/com/lambdaworks/redis/RedisClient.java +++ b/src/main/java/com/lambdaworks/redis/RedisClient.java @@ -43,7 +43,7 @@ public class RedisClient extends AbstractRedisClient { private final RedisURI redisURI; /** - * Creates a uri-less RedisClient. You can connect to different redis servers but you must supply a {@link RedisURI} on + * Creates a uri-less RedisClient. You can connect to different Redis servers but you must supply a {@link RedisURI} on * connecting. Methods without having a {@link RedisURI} will fail with a {@link java.lang.IllegalStateException}. */ public RedisClient() { @@ -112,8 +112,8 @@ public RedisConnectionPool> pool(int maxIdle, int * @param codec the codec to use * @param maxIdle max idle connections in pool * @param maxActive max active connections in pool - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return a new {@link RedisConnectionPool} instance */ @SuppressWarnings("unchecked") @@ -181,8 +181,8 @@ public RedisConnectionPool> asyncPool(int max * @param codec the codec to use * @param maxIdle max idle connections in pool * @param maxActive max active connections in pool - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return a new {@link RedisConnectionPool} instance */ public RedisConnectionPool> asyncPool(final RedisCodec codec, int maxIdle, @@ -212,49 +212,54 @@ public Class> getComponentType() { } /** - * Connect to a Redis server that treats keys and values as UTF-8 strings. + * Open a connection to a Redis server that treats keys and values as UTF-8 strings. * * @return A new stateful Redis connection. */ - @SuppressWarnings({ "unchecked" }) public StatefulRedisConnection connect() { return connect(newStringStringCodec()); } /** - * Connect to a Redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys and values. + * Open a connection to a Redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys and values. * * @param codec Use this codec to encode/decode keys and values, must note be {@literal null} - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return A new stateful Redis connection. */ - @SuppressWarnings("unchecked") public StatefulRedisConnection connect(RedisCodec codec) { checkForRedisURI(); - checkArgument(codec != null, "RedisCodec must not be null"); return connect(codec, this.redisURI); } /** - * Connect to a Redis server with the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. + * Open a connection to a Redis server with the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. * - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} * @return A new connection. */ - @SuppressWarnings({ "unchecked" }) public StatefulRedisConnection connect(RedisURI redisURI) { checkValidRedisURI(redisURI); return connect(newStringStringCodec(), redisURI); } - @SuppressWarnings({ "rawtypes" }) - private StatefulRedisConnection connect(RedisCodec codec, RedisURI redisURI) { + /** + * Open a connection to a Redis server with the supplied {@link RedisURI} and the supplied {@link RedisCodec codec} to + * encode/decode keys. + * + * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type + * @return A new connection. + */ + public StatefulRedisConnection connect(RedisCodec codec, RedisURI redisURI) { return connectStandalone(codec, redisURI); } /** - * Open a new asynchronous connection to the redis server that treats keys and values as UTF-8 strings. + * Open a new asynchronous connection to the Redis server that treats keys and values as UTF-8 strings. * * @return A new connection. * @deprecated Use {@code connect().async()} @@ -265,12 +270,12 @@ public RedisAsyncCommands connectAsync() { } /** - * Open a new asynchronous connection to the redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys + * Open a new asynchronous connection to the Redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys * and values. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return A new connection. * @deprecated Use {@code connect(codec).async()} */ @@ -284,7 +289,7 @@ public RedisAsyncCommands connectAsync(RedisCodec codec) { /** * Open a new asynchronous connection to the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. * - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} * @return A new connection. * @deprecated Use {@code connect(redisURI).async()} */ @@ -294,10 +299,27 @@ public RedisAsyncCommands connectAsync(RedisURI redisURI) { return connect(newStringStringCodec(), redisURI).async(); } + /** + * Open a new asynchronous connection to the supplied {@link RedisURI} and the supplied {@link RedisCodec codec} to + * encode/decode keys. + * + * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @return A new connection. * @deprecated Use {@code connect(redisURI).async()} + */ + @Deprecated + public RedisAsyncCommands connectAsync(RedisCodec codec, RedisURI redisURI) { + checkValidRedisURI(redisURI); + return connect(codec, redisURI).async(); + } + private StatefulRedisConnection connectStandalone(RedisCodec codec, RedisURI redisURI) { + checkArgument(codec != null, "RedisCodec must not be null"); + checkArgument(redisURI != null, "RedisURI must not be null"); + Queue> queue = new ArrayDeque<>(); - CommandHandler handler = new CommandHandler(clientOptions, queue); + CommandHandler handler = new CommandHandler<>(clientOptions, queue); StatefulRedisConnectionImpl connection = newStatefulRedisConnection(handler, codec); connectStateful(handler, connection, redisURI); @@ -332,7 +354,7 @@ private void connectStateful(CommandHandler handler, StatefulRedisC } /** - * Open a new pub/sub connection to the redis server that treats keys and values as UTF-8 strings. + * Open a new pub/sub connection to the Redis server that treats keys and values as UTF-8 strings. * * @return A new stateful pub/sub connection */ @@ -341,9 +363,10 @@ public StatefulRedisPubSubConnection connectPubSub() { } /** - * Open a new pub/sub connection to the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. + * Open a new pub/sub connection to the Redis server using the supplied {@link RedisURI} that treats keys and values as + * UTF-8 strings. * - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} * @return A new stateful pub/sub connection */ public StatefulRedisPubSubConnection connectPubSub(RedisURI redisURI) { @@ -352,12 +375,12 @@ public StatefulRedisPubSubConnection connectPubSub(RedisURI redi } /** - * Open a new pub/sub connection to the redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys and - * values. + * Open a new pub/sub connection to the Redis server using the supplied {@link RedisURI} and use the supplied + * {@link RedisCodec codec} to encode/decode keys and values. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return A new stateful pub/sub connection */ public StatefulRedisPubSubConnection connectPubSub(RedisCodec codec) { @@ -365,12 +388,23 @@ public StatefulRedisPubSubConnection connectPubSub(RedisCodec return connectPubSub(codec, redisURI); } - private StatefulRedisPubSubConnection connectPubSub(RedisCodec codec, RedisURI redisURI) { + /** + * Open a new pub/sub connection to the Redis server using the supplied {@link RedisURI} and use the supplied + * {@link RedisCodec codec} to encode/decode keys and values. + * + * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} + * @param redisURI the redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type + * @return A new connection. + */ + public StatefulRedisPubSubConnection connectPubSub(RedisCodec codec, RedisURI redisURI) { checkArgument(codec != null, "RedisCodec must not be null"); + checkArgument(redisURI != null, "RedisURI must not be null"); Queue> queue = new ArrayDeque<>(); - PubSubCommandHandler handler = new PubSubCommandHandler(clientOptions, queue, codec); + PubSubCommandHandler handler = new PubSubCommandHandler<>(clientOptions, queue, codec); StatefulRedisPubSubConnectionImpl connection = newStatefulRedisPubSubConnection(handler, codec); connectStateful(handler, connection, redisURI); @@ -379,7 +413,7 @@ private StatefulRedisPubSubConnection connectPubSub(RedisCodec connectSentinel() { } /** - * CConnect to a Redis Sentinel. You must supply a valid RedisURI containing one or more sentinels. + * Open a connection to a Redis Sentinel that treats keys and use the supplied {@link RedisCodec codec} to encode/decode + * keys and values. The client {@link RedisURI} must contain one or more sentinels. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return A new stateful Redis Sentinel connection */ public StatefulRedisSentinelConnection connectSentinel(RedisCodec codec) { checkForRedisURI(); - checkArgument(codec != null, "RedisCodec must not be null"); return connectSentinelImpl(codec, redisURI); } /** - * Connect to a Redis Sentinel. You must supply a valid RedisURI containing a redis host or one or more sentinels. + * Open a connection to a Redis Sentinel using the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. + * The client {@link RedisURI} must contain one or more sentinels. * - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} * @return A new connection. */ public StatefulRedisSentinelConnection connectSentinel(RedisURI redisURI) { @@ -412,7 +447,22 @@ public StatefulRedisSentinelConnection connectSentinel(RedisURI } /** - * Creates an asynchronous connection to Redis Sentinel. You must supply a valid RedisURI containing one or more sentinels. + * Open a connection to a Redis Sentinel using the supplied {@link RedisURI} and use the supplied {@link RedisCodec codec} + * to encode/decode keys and values. The client {@link RedisURI} must contain one or more sentinels. + * + * @param codec the Redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type + * @return A new connection. + */ + public StatefulRedisSentinelConnection connectSentinel(RedisCodec codec, RedisURI redisURI) { + return connectSentinelImpl(codec, redisURI); + } + + /** + * Creates an asynchronous connection to Redis Sentinel that treats keys and values as UTF-8 strings. You must supply a + * valid RedisURI containing one or more sentinels. * * @return a new connection. * @deprecated Use {@code connectSentinel().async()} @@ -423,11 +473,12 @@ public RedisSentinelAsyncCommands connectSentinelAsync() { } /** - * Creates an asynchronous connection to Redis Sentinel. You must supply a valid RedisURI containing one or more sentinels. + * Creates an asynchronous connection to Redis Sentinela nd use the supplied {@link RedisCodec codec} to encode/decode keys + * and values. You must supply a valid RedisURI containing one or more sentinels. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return a new connection. * @deprecated Use {@code connectSentinel(codec).async()} */ @@ -439,10 +490,10 @@ public RedisSentinelAsyncCommands connectSentinelAsync(RedisCodec connectSentinelAsync(RedisURI return connectSentinelImpl(newStringStringCodec(), redisURI).async(); } + /** + * Creates an asynchronous connection to Redis Sentinel using the supplied {@link RedisURI} and use the supplied + * {@link RedisCodec codec} to encode/decode keys and values. You must supply a valid RedisURI containing a redis host or + * one or more sentinels. + * + * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type + * @return A new connection. + * @deprecated Use {@code connectSentinel(codec, redisURI).async()} + */ + @Deprecated + public RedisSentinelAsyncCommands connectSentinelAsync(RedisCodec codec, RedisURI redisURI) { + return connectSentinelImpl(codec, redisURI).async(); + } + private StatefulRedisSentinelConnection connectSentinelImpl(RedisCodec codec, RedisURI redisURI) { + checkArgument(codec != null, "RedisCodec must not be null"); + checkArgument(redisURI != null, "RedisURI must not be null"); + Queue> queue = new ArrayDeque>(); ConnectionBuilder connectionBuilder = ConnectionBuilder.connectionBuilder(); connectionBuilder.clientOptions(ClientOptions.copyOf(getOptions())); - final CommandHandler commandHandler = new CommandHandler(clientOptions, queue); + final CommandHandler commandHandler = new CommandHandler<>(clientOptions, queue); StatefulRedisSentinelConnectionImpl connection = newStatefulRedisSentinelConnection(commandHandler, codec); diff --git a/src/test/java/com/lambdaworks/redis/RedisClientTest.java b/src/test/java/com/lambdaworks/redis/RedisClientTest.java new file mode 100644 index 0000000000..1cf73b295a --- /dev/null +++ b/src/test/java/com/lambdaworks/redis/RedisClientTest.java @@ -0,0 +1,164 @@ +package com.lambdaworks.redis; + +import static com.lambdaworks.redis.RedisURI.Builder.redis; + +import org.junit.Test; + +import com.lambdaworks.redis.codec.Utf8StringCodec; + +/** + * @author Mark Paluch + */ +public class RedisClientTest extends AbstractRedisClientTest { + + /* + * Pool/Sync + */ + @Test + public void poolClientUri() throws Exception { + client.pool().close(); + } + + @Test + public void poolClientUriConfig() throws Exception { + client.pool(1, 1).close(); + } + + @Test + public void poolCodecClientUriConfig() throws Exception { + client.pool(new Utf8StringCodec(), 1, 1).close(); + } + + /* + * Pool/Async + */ + @Test + public void asyncPoolClientUri() throws Exception { + client.asyncPool().close(); + } + + @Test + public void asyncPoolClientUriConfig() throws Exception { + client.asyncPool(1, 1).close(); + } + + @Test + public void asyncPoolCodecClientUriConfig() throws Exception { + client.asyncPool(new Utf8StringCodec(), 1, 1).close(); + } + + /* + * Standalone/Stateful + */ + @Test + public void connectClienturi() throws Exception { + client.connect().close(); + } + + @Test + public void connectCodecClientUri() throws Exception { + client.connect(new Utf8StringCodec()).close(); + } + + @Test + public void connectOwnUri() throws Exception { + client.connect(redis(host, port).build()).close(); + } + + @Test + public void connectCodecOwnUri() throws Exception { + client.connect(new Utf8StringCodec(), redis(host, port).build()).close(); + } + + /* + * Deprecated: Standalone/Async + */ + @Test + public void connectAsyncClientUri() throws Exception { + client.connectAsync().close(); + } + + @Test + public void connectAsyncCodecClientUri() throws Exception { + client.connectAsync(new Utf8StringCodec()).close(); + } + + @Test + public void connectAsyncOwnUri() throws Exception { + client.connectAsync(redis(host, port).build()).close(); + } + + @Test + public void connectAsyncCodecOwnUri() throws Exception { + client.connectAsync(new Utf8StringCodec(), redis(host, port).build()).close(); + } + + /* + * Standalone/PubSub Stateful + */ + @Test + public void connectPubSubClientUri() throws Exception { + client.connectPubSub().close(); + } + + @Test + public void connectPubSubCodecClientUri() throws Exception { + client.connectPubSub(new Utf8StringCodec()).close(); + } + + @Test + public void connectPubSubOwnUri() throws Exception { + client.connectPubSub(redis(host, port).build()).close(); + } + + @Test + public void connectPubSubCodecOwnUri() throws Exception { + client.connectPubSub(new Utf8StringCodec(), redis(host, port).build()).close(); + } + + /* + * Sentinel Stateful + */ + @Test + public void connectSentinelClientUri() throws Exception { + client.connectSentinel().close(); + } + + @Test + public void connectSentinelCodecClientUri() throws Exception { + client.connectSentinel(new Utf8StringCodec()).close(); + } + + @Test + public void connectSentinelOwnUri() throws Exception { + client.connectSentinel(redis(host, port).build()).close(); + } + + @Test + public void connectSentinelCodecOwnUri() throws Exception { + client.connectSentinel(new Utf8StringCodec(), redis(host, port).build()).close(); + } + + /* + * Deprecated: Sentinel/Async + */ + @Test + public void connectSentinelAsyncClientUri() throws Exception { + client.connectSentinelAsync().close(); + } + + @Test + public void connectSentinelAsyncCodecClientUri() throws Exception { + client.connectSentinelAsync(new Utf8StringCodec()).close(); + } + + @Test + public void connectSentineAsynclOwnUri() throws Exception { + client.connectSentinelAsync(redis(host, port).build()).close(); + } + + @Test + public void connectSentinelAsyncCodecOwnUri() throws Exception { + client.connectSentinelAsync(new Utf8StringCodec(), redis(host, port).build()).close(); + } +} \ No newline at end of file