Skip to content

Commit

Permalink
Expose codec/uri methods on RedisClient #108
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Aug 2, 2015
1 parent 8090a14 commit 0c37825
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 47 deletions.
165 changes: 118 additions & 47 deletions src/main/java/com/lambdaworks/redis/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -112,8 +112,8 @@ public RedisConnectionPool<RedisCommands<String, String>> 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 <K> Key type.
* @param <V> Value type.
* @param <K> Key type
* @param <V> Value type
* @return a new {@link RedisConnectionPool} instance
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -181,8 +181,8 @@ public RedisConnectionPool<RedisAsyncCommands<String, String>> asyncPool(int max
* @param codec the codec to use
* @param maxIdle max idle connections in pool
* @param maxActive max active connections in pool
* @param <K> Key type.
* @param <V> Value type.
* @param <K> Key type
* @param <V> Value type
* @return a new {@link RedisConnectionPool} instance
*/
public <K, V> RedisConnectionPool<RedisAsyncCommands<K, V>> asyncPool(final RedisCodec<K, V> codec, int maxIdle,
Expand Down Expand Up @@ -212,49 +212,54 @@ public Class<? extends RedisAsyncCommands<K, V>> 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<String, String> 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 <K> Key type.
* @param <V> Value type.
* @param <K> Key type
* @param <V> Value type
* @return A new stateful Redis connection.
*/
@SuppressWarnings("unchecked")
public <K, V> StatefulRedisConnection<K, V> connect(RedisCodec<K, V> 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<String, String> connect(RedisURI redisURI) {
checkValidRedisURI(redisURI);
return connect(newStringStringCodec(), redisURI);
}

@SuppressWarnings({ "rawtypes" })
private <K, V> StatefulRedisConnection<K, V> connect(RedisCodec<K, V> 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 <K> Key type
* @param <V> Value type
* @return A new connection.
*/
public <K, V> StatefulRedisConnection<K, V> connect(RedisCodec<K, V> 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()}
Expand All @@ -265,12 +270,12 @@ public RedisAsyncCommands<String, String> 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 <K> Key type.
* @param <V> Value type.
* @param <K> Key type
* @param <V> Value type
* @return A new connection.
* @deprecated Use {@code connect(codec).async()}
*/
Expand All @@ -284,7 +289,7 @@ public <K, V> RedisAsyncCommands<K, V> connectAsync(RedisCodec<K, V> 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()}
*/
Expand All @@ -294,10 +299,27 @@ public RedisAsyncCommands<String, String> 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 <K, V> RedisAsyncCommands<K, V> connectAsync(RedisCodec<K, V> codec, RedisURI redisURI) {
checkValidRedisURI(redisURI);
return connect(codec, redisURI).async();
}

private <K, V> StatefulRedisConnection<K, V> connectStandalone(RedisCodec<K, V> codec, RedisURI redisURI) {
checkArgument(codec != null, "RedisCodec must not be null");
checkArgument(redisURI != null, "RedisURI must not be null");

Queue<RedisCommand<K, V, ?>> queue = new ArrayDeque<>();

CommandHandler<K, V> handler = new CommandHandler<K, V>(clientOptions, queue);
CommandHandler<K, V> handler = new CommandHandler<>(clientOptions, queue);

StatefulRedisConnectionImpl<K, V> connection = newStatefulRedisConnection(handler, codec);
connectStateful(handler, connection, redisURI);
Expand Down Expand Up @@ -332,7 +354,7 @@ private <K, V> void connectStateful(CommandHandler<K, V> 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
*/
Expand All @@ -341,9 +363,10 @@ public StatefulRedisPubSubConnection<String, String> 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<String, String> connectPubSub(RedisURI redisURI) {
Expand All @@ -352,25 +375,36 @@ public StatefulRedisPubSubConnection<String, String> 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 <K> Key type.
* @param <V> Value type.
* @param <K> Key type
* @param <V> Value type
* @return A new stateful pub/sub connection
*/
public <K, V> StatefulRedisPubSubConnection<K, V> connectPubSub(RedisCodec<K, V> codec) {
checkForRedisURI();
return connectPubSub(codec, redisURI);
}

private <K, V> StatefulRedisPubSubConnection<K, V> connectPubSub(RedisCodec<K, V> 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 <K> Key type
* @param <V> Value type
* @return A new connection.
*/
public <K, V> StatefulRedisPubSubConnection<K, V> connectPubSub(RedisCodec<K, V> codec, RedisURI redisURI) {

checkArgument(codec != null, "RedisCodec must not be null");
checkArgument(redisURI != null, "RedisURI must not be null");
Queue<RedisCommand<K, V, ?>> queue = new ArrayDeque<>();

PubSubCommandHandler<K, V> handler = new PubSubCommandHandler<K, V>(clientOptions, queue, codec);
PubSubCommandHandler<K, V> handler = new PubSubCommandHandler<>(clientOptions, queue, codec);
StatefulRedisPubSubConnectionImpl<K, V> connection = newStatefulRedisPubSubConnection(handler, codec);

connectStateful(handler, connection, redisURI);
Expand All @@ -379,7 +413,7 @@ private <K, V> StatefulRedisPubSubConnection<K, V> connectPubSub(RedisCodec<K, V
}

/**
* Connect 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 values as UTF-8 strings.
*
* @return A new stateful Redis Sentinel connection
*/
Expand All @@ -388,31 +422,47 @@ public StatefulRedisSentinelConnection<String, String> 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 <K> Key type.
* @param <V> Value type.
* @param <K> Key type
* @param <V> Value type
* @return A new stateful Redis Sentinel connection
*/
public <K, V> StatefulRedisSentinelConnection<K, V> connectSentinel(RedisCodec<K, V> 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<String, String> connectSentinel(RedisURI redisURI) {
return connectSentinelImpl(newStringStringCodec(), 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 <K> Key type
* @param <V> Value type
* @return A new connection.
*/
public <K, V> StatefulRedisSentinelConnection<K, V> connectSentinel(RedisCodec<K, V> 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()}
Expand All @@ -423,11 +473,12 @@ public RedisSentinelAsyncCommands<String, String> 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 <K> Key type.
* @param <V> Value type.
* @param <K> Key type
* @param <V> Value type
* @return a new connection.
* @deprecated Use {@code connectSentinel(codec).async()}
*/
Expand All @@ -439,10 +490,10 @@ public <K, V> RedisSentinelAsyncCommands<K, V> connectSentinelAsync(RedisCodec<K
}

/**
* Creates an asynchronous connection to Redis Sentinel. You must supply a valid RedisURI containing a redis host or one or
* more sentinels.
* Creates an asynchronous connection to Redis Sentinel using the supplied {@link RedisURI} that treats keys and values as
* UTF-8 strings. You must supply a valid RedisURI containing a redis host or 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.
* @deprecated Use {@code connectSentinel(redisURI).async()}
*/
Expand All @@ -451,13 +502,33 @@ public RedisSentinelAsyncCommands<String, String> 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 <K> Key type
* @param <V> Value type
* @return A new connection.
* @deprecated Use {@code connectSentinel(codec, redisURI).async()}
*/
@Deprecated
public <K, V> RedisSentinelAsyncCommands<K, V> connectSentinelAsync(RedisCodec<K, V> codec, RedisURI redisURI) {
return connectSentinelImpl(codec, redisURI).async();
}

private <K, V> StatefulRedisSentinelConnection<K, V> connectSentinelImpl(RedisCodec<K, V> codec, RedisURI redisURI) {
checkArgument(codec != null, "RedisCodec must not be null");
checkArgument(redisURI != null, "RedisURI must not be null");

Queue<RedisCommand<K, V, ?>> queue = new ArrayDeque<RedisCommand<K, V, ?>>();

ConnectionBuilder connectionBuilder = ConnectionBuilder.connectionBuilder();
connectionBuilder.clientOptions(ClientOptions.copyOf(getOptions()));

final CommandHandler<K, V> commandHandler = new CommandHandler<K, V>(clientOptions, queue);
final CommandHandler<K, V> commandHandler = new CommandHandler<>(clientOptions, queue);

StatefulRedisSentinelConnectionImpl<K, V> connection = newStatefulRedisSentinelConnection(commandHandler, codec);

Expand Down
Loading

0 comments on commit 0c37825

Please sign in to comment.