Skip to content

Commit

Permalink
Make RedisAsyncCommands.select() and auth() async #1118
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Oct 27, 2019
1 parent 606a6c0 commit 426fb0c
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 52 deletions.
28 changes: 14 additions & 14 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ public RedisFuture<String> asking() {
}

@Override
public String auth(String password) {
public RedisFuture<String> auth(CharSequence password) {

LettuceAssert.notNull(password, "Password must not be null");
AsyncCommand<K, V, String> cmd = authAsync(password.toCharArray());
return LettuceFutures.awaitOrCancel(cmd, connection.getTimeout().toNanos(), TimeUnit.NANOSECONDS);
return dispatch(commandBuilder.auth(password));
}

public AsyncCommand<K, V, String> authAsync(char[] password) {
public RedisFuture<String> auth(char[] password) {

LettuceAssert.notNull(password, "Password must not be null");
return dispatch(commandBuilder.auth(password));
}

Expand Down Expand Up @@ -1194,12 +1195,7 @@ public RedisFuture<Long> sdiffstore(K destination, K... keys) {
return dispatch(commandBuilder.sdiffstore(destination, keys));
}

public String select(int db) {
AsyncCommand<K, V, String> cmd = selectAsync(db);
return LettuceFutures.awaitOrCancel(cmd, connection.getTimeout().toNanos(), TimeUnit.NANOSECONDS);
}

protected AsyncCommand<K, V, String> selectAsync(int db) {
public RedisFuture<String> select(int db) {
return dispatch(commandBuilder.select(db));
}

Expand Down Expand Up @@ -1403,7 +1399,8 @@ public RedisFuture<StreamScanCursor> sscan(ValueStreamingChannel<V> channel, K k
}

@Override
public RedisFuture<StreamScanCursor> sscan(ValueStreamingChannel<V> channel, K key, ScanCursor scanCursor, ScanArgs scanArgs) {
public RedisFuture<StreamScanCursor> sscan(ValueStreamingChannel<V> channel, K key, ScanCursor scanCursor,
ScanArgs scanArgs) {
return dispatch(commandBuilder.sscanStreaming(channel, key, scanCursor, scanArgs));
}

Expand Down Expand Up @@ -1848,7 +1845,8 @@ public RedisFuture<Long> zrangebyscore(ValueStreamingChannel<V> channel, K key,
}

@Override
public RedisFuture<Long> zrangebyscore(ValueStreamingChannel<V> channel, K key, Range<? extends Number> range, Limit limit) {
public RedisFuture<Long> zrangebyscore(ValueStreamingChannel<V> channel, K key, Range<? extends Number> range,
Limit limit) {
return dispatch(commandBuilder.zrangebyscore(channel, key, range, limit));
}

Expand Down Expand Up @@ -2065,12 +2063,14 @@ public RedisFuture<List<ScoredValue<V>>> zrevrangebyscoreWithScores(K key, Range
}

@Override
public RedisFuture<List<ScoredValue<V>>> zrevrangebyscoreWithScores(K key, double max, double min, long offset, long count) {
public RedisFuture<List<ScoredValue<V>>> zrevrangebyscoreWithScores(K key, double max, double min, long offset,
long count) {
return dispatch(commandBuilder.zrevrangebyscoreWithScores(key, max, min, offset, count));
}

@Override
public RedisFuture<List<ScoredValue<V>>> zrevrangebyscoreWithScores(K key, String max, String min, long offset, long count) {
public RedisFuture<List<ScoredValue<V>>> zrevrangebyscoreWithScores(K key, String max, String min, long offset,
long count) {
return dispatch(commandBuilder.zrevrangebyscoreWithScores(key, max, min, offset, count));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Mono<String> asking() {
}

@Override
public Mono<String> auth(String password) {
public Mono<String> auth(CharSequence password) {
return createMono(() -> commandBuilder.auth(password));
}

Expand Down
43 changes: 24 additions & 19 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.lettuce.core.XReadArgs.StreamOffset;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.codec.Utf8StringCodec;
import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.output.*;
import io.lettuce.core.protocol.*;
Expand Down Expand Up @@ -63,11 +62,15 @@ Command<K, V, String> asking() {
return createCommand(ASKING, new StatusOutput<>(codec), args);
}

Command<K, V, String> auth(String password) {
Command<K, V, String> auth(CharSequence password) {
LettuceAssert.notNull(password, "Password " + MUST_NOT_BE_NULL);
LettuceAssert.notEmpty(password, "Password " + MUST_NOT_BE_EMPTY);

return auth(password.toCharArray());
char[] chars = new char[password.length()];
for (int i = 0; i < password.length(); i++) {
chars[i] = password.charAt(i);
}
return auth(chars);
}

Command<K, V, String> auth(char[] password) {
Expand Down Expand Up @@ -676,10 +679,8 @@ Command<K, V, Long> geoadd(K key, Object[] lngLatMember) {
LettuceAssert.notNull(lngLatMember, "LngLatMember " + MUST_NOT_BE_NULL);
LettuceAssert.notEmpty(lngLatMember, "LngLatMember " + MUST_NOT_BE_EMPTY);
LettuceAssert.noNullElements(lngLatMember, "LngLatMember " + MUST_NOT_CONTAIN_NULL_ELEMENTS);
LettuceAssert
.isTrue(lngLatMember.length % 3 == 0,
"LngLatMember.length must be a multiple of 3 and contain a "
+ "sequence of longitude1, latitude1, member1, longitude2, latitude2, member2, ... longitudeN, latitudeN, memberN");
LettuceAssert.isTrue(lngLatMember.length % 3 == 0, "LngLatMember.length must be a multiple of 3 and contain a "
+ "sequence of longitude1, latitude1, member1, longitude2, latitude2, member2, ... longitudeN, latitudeN, memberN");

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

Expand Down Expand Up @@ -753,8 +754,9 @@ Command<K, V, List<GeoWithin<V>>> georadius(CommandType commandType, K key, doub
CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key).add(longitude).add(latitude).add(distance).add(unit);
geoArgs.build(args);

return createCommand(commandType, new GeoWithinListOutput<>(codec, geoArgs.isWithDistance(), geoArgs.isWithHash(),
geoArgs.isWithCoordinates()), args);
return createCommand(commandType,
new GeoWithinListOutput<>(codec, geoArgs.isWithDistance(), geoArgs.isWithHash(), geoArgs.isWithCoordinates()),
args);
}

Command<K, V, Long> georadius(K key, double longitude, double latitude, double distance, String unit,
Expand Down Expand Up @@ -794,8 +796,9 @@ Command<K, V, List<GeoWithin<V>>> georadiusbymember(CommandType commandType, K k
CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key).addValue(member).add(distance).add(unit);
geoArgs.build(args);

return createCommand(commandType, new GeoWithinListOutput<>(codec, geoArgs.isWithDistance(), geoArgs.isWithHash(),
geoArgs.isWithCoordinates()), args);
return createCommand(commandType,
new GeoWithinListOutput<>(codec, geoArgs.isWithDistance(), geoArgs.isWithHash(), geoArgs.isWithCoordinates()),
args);
}

Command<K, V, Long> georadiusbymember(K key, V member, double distance, String unit,
Expand Down Expand Up @@ -2664,11 +2667,13 @@ Command<K, V, Long> zrangebyscore(ValueStreamingChannel<V> channel, K key, Strin
return createCommand(ZRANGEBYSCORE, new ValueStreamingOutput<>(codec, channel), args);
}

Command<K, V, Long> zrangebyscore(ValueStreamingChannel<V> channel, K key, double min, double max, long offset, long count) {
Command<K, V, Long> zrangebyscore(ValueStreamingChannel<V> channel, K key, double min, double max, long offset,
long count) {
return zrangebyscore(channel, key, string(min), string(max), offset, count);
}

Command<K, V, Long> zrangebyscore(ValueStreamingChannel<V> channel, K key, String min, String max, long offset, long count) {
Command<K, V, Long> zrangebyscore(ValueStreamingChannel<V> channel, K key, String min, String max, long offset,
long count) {
notNullKey(key);
notNullMinMax(min, max);
LettuceAssert.notNull(channel, "ScoredValueStreamingChannel " + MUST_NOT_BE_NULL);
Expand Down Expand Up @@ -3013,8 +3018,8 @@ Command<K, V, Long> zrevrangebyscoreWithScores(ScoredValueStreamingChannel<V> ch
return createCommand(ZREVRANGEBYSCORE, new ScoredValueStreamingOutput<>(codec, channel), args);
}

Command<K, V, Long> zrevrangebyscoreWithScores(ScoredValueStreamingChannel<V> channel, K key,
Range<? extends Number> range, Limit limit) {
Command<K, V, Long> zrevrangebyscoreWithScores(ScoredValueStreamingChannel<V> channel, K key, Range<? extends Number> range,
Limit limit) {
notNullKey(key);
notNullRange(range);
notNullLimit(limit);
Expand Down Expand Up @@ -3196,8 +3201,8 @@ private static String max(Range<? extends Number> range) {

Boundary<? extends Number> upper = range.getUpper();

if (upper.getValue() == null || upper.getValue() instanceof Double
&& upper.getValue().doubleValue() == Double.POSITIVE_INFINITY) {
if (upper.getValue() == null
|| upper.getValue() instanceof Double && upper.getValue().doubleValue() == Double.POSITIVE_INFINITY) {
return "+inf";
}

Expand All @@ -3212,8 +3217,8 @@ private static String min(Range<? extends Number> range) {

Boundary<? extends Number> lower = range.getLower();

if (lower.getValue() == null || lower.getValue() instanceof Double
&& lower.getValue().doubleValue() == Double.NEGATIVE_INFINITY) {
if (lower.getValue() == null
|| lower.getValue() instanceof Double && lower.getValue().doubleValue() == Double.NEGATIVE_INFINITY) {
return "-inf";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public void activated() {
super.activated();
// do not block in here, since the channel flow will be interrupted.
if (password != null) {
async.authAsync(password);
async.auth(password);
}

if (db != 0) {
async.selectAsync(db);
async.select(db);
}

if (clientName != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public interface RedisAsyncCommands<K, V> extends BaseRedisAsyncCommands<K, V>,
* @param password the password
* @return String simple-string-reply
*/
String auth(String password);
RedisFuture<String> auth(CharSequence password);

/**
* Change the selected database for the current connection.
*
* @param db the database number
* @return String simple-string-reply
*/
String select(int db);
RedisFuture<String> select(int db);

/**
* Swap two Redis databases, so that immediately all the clients connected to a given DB will see the data of the other DB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface RedisReactiveCommands<K, V> extends BaseRedisReactiveCommands<K
* @param password the password
* @return String simple-string-reply
*/
Mono<String> auth(String password);
Mono<String> auth(CharSequence password);

/**
* Change the selected database for the current connection.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/api/sync/RedisCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface RedisCommands<K, V> extends BaseRedisCommands<K, V>, RedisClust
* @param password the password
* @return String simple-string-reply
*/
String auth(String password);
String auth(CharSequence password);

/**
* Change the selected database for the current Commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void activated() {
super.activated();
// do not block in here, since the channel flow will be interrupted.
if (password != null) {
async.authAsync(password);
async.auth(password);
}

if (clientName != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface RedisClusterAsyncCommands<K, V> extends BaseRedisAsyncCommands<
* @param password the password
* @return String simple-string-reply
*/
String auth(String password);
RedisFuture<String> auth(CharSequence password);

/**
* Generate a new config epoch, incrementing the current epoch, assign the new epoch to this node, WITHOUT any consensus and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface RedisClusterReactiveCommands<K, V> extends BaseRedisReactiveCom
* @param password the password
* @return String simple-string-reply
*/
Mono<String> auth(String password);
Mono<String> auth(CharSequence password);

/**
* Generate a new config epoch, incrementing the current epoch, assign the new epoch to this node, WITHOUT any consensus and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface RedisClusterCommands<K, V> extends BaseRedisCommands<K, V>, Red
* @param password the password
* @return String simple-string-reply
*/
String auth(String password);
String auth(CharSequence password);

/**
* Generate a new config epoch, incrementing the current epoch, assign the new epoch to this node, WITHOUT any consensus and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.test.Delay;
import io.lettuce.test.LettuceExtension;
import io.lettuce.test.Wait;
import io.lettuce.test.WithPassword;
import io.lettuce.test.*;

/**
* @author Will Glozer
Expand Down Expand Up @@ -135,11 +132,10 @@ void getSetReconnect() {
}

@Test
@SuppressWarnings("unchecked")
void authInvalidPassword() {
RedisAsyncCommands<String, String> async = client.connect().async();
try {
async.auth("invalid");
Futures.await(async.auth("invalid"));
fail("Authenticated with invalid password");
} catch (RedisException e) {
assertThat(e.getMessage()).isEqualTo("ERR Client sent AUTH, but no password is set");
Expand All @@ -151,11 +147,10 @@ void authInvalidPassword() {
}

@Test
@SuppressWarnings("unchecked")
void selectInvalid() {
RedisAsyncCommands<String, String> async = client.connect().async();
try {
async.select(1024);
Futures.await(async.select(1024));
fail("Selected invalid db index");
} catch (RedisException e) {
assertThat(e.getMessage()).startsWith("ERR");
Expand Down

0 comments on commit 426fb0c

Please sign in to comment.