Polishing.

Remove ZAddArgs.empty() method to avoid confusion with none() method. Use NONE constant only internally. Adapt calling methods.

Reformat code.

See #1794
Original pull request: #1988.
This commit is contained in:
Mark Paluch
2021-03-03 10:10:22 +01:00
parent 4d77392c9d
commit 7cbeca1f48
6 changed files with 79 additions and 67 deletions

View File

@@ -2684,6 +2684,15 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
return touch(serializeMulti(keys));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, double, java.lang.String)
*/
@Override
public Boolean zAdd(String key, double score, String value) {
return zAdd(serialize(key), score, serialize(value));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, double, java.lang.String, org.springframework.data.redis.connection.RedisZSetCommands.ZAddArgs)
@@ -2693,6 +2702,15 @@ public class DefaultStringRedisConnection implements StringRedisConnection, Deco
return zAdd(serialize(key), score, serialize(value), args);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, java.util.Set)
*/
@Override
public Long zAdd(String key, Set<StringTuple> tuples) {
return zAdd(serialize(key), stringTupleToTuple.convert(tuples));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, java.util.Set, org.springframework.data.redis.connection.RedisZSetCommands.ZAddArgs)

View File

@@ -17,7 +17,7 @@ package org.springframework.data.redis.connection;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.DoubleUnaryOperator;
@@ -400,13 +400,13 @@ public interface RedisZSetCommands {
/**
* {@code ZADD} specific arguments. <br />
* Looking of the {@code INCR} flag? Use the {@code ZINCRBY} operation instead.
*
* @since 2.3
*
* @since 2.5
* @see <a href="https://redis.io/commands/zadd">Redis Documentation: ZADD</a>
*/
class ZAddArgs {
private static final ZAddArgs NONE = new ZAddArgs(Collections.emptySet());
private static final ZAddArgs NONE = new ZAddArgs(EnumSet.noneOf(Flag.class));
private final Set<Flag> flags;
@@ -414,18 +414,11 @@ public interface RedisZSetCommands {
this.flags = flags;
}
/**
* @return immutable {@link ZAddArgs}.
*/
public static ZAddArgs none() {
return NONE;
}
/**
* @return new instance of {@link ZAddArgs} without any flags set.
*/
public static ZAddArgs empty() {
return new ZAddArgs(new LinkedHashSet<>(5));
return new ZAddArgs(EnumSet.noneOf(Flag.class));
}
/**
@@ -573,7 +566,7 @@ public interface RedisZSetCommands {
*/
@Nullable
default Boolean zAdd(byte[] key, double score, byte[] value) {
return zAdd(key, score, value, ZAddArgs.none());
return zAdd(key, score, value, ZAddArgs.NONE);
}
/**
@@ -601,7 +594,7 @@ public interface RedisZSetCommands {
*/
@Nullable
default Long zAdd(byte[] key, Set<Tuple> tuples) {
return zAdd(key, tuples, ZAddArgs.none());
return zAdd(key, tuples, ZAddArgs.NONE);
}
/**

View File

@@ -1102,9 +1102,7 @@ public interface StringRedisConnection extends RedisConnection {
* @see <a href="https://redis.io/commands/zadd">Redis Documentation: ZADD</a>
* @see RedisZSetCommands#zAdd(byte[], double, byte[])
*/
default Boolean zAdd(String key, double score, String value) {
return zAdd(key, score, value, ZAddArgs.none());
}
Boolean zAdd(String key, double score, String value);
/**
* Add the {@code value} to a sorted set at {@code key}, or update its {@code score} depending on the given
@@ -1130,9 +1128,7 @@ public interface StringRedisConnection extends RedisConnection {
* @see <a href="https://redis.io/commands/zadd">Redis Documentation: ZADD</a>
* @see RedisZSetCommands#zAdd(byte[], Set)
*/
default Long zAdd(String key, Set<StringTuple> tuples) {
return zAdd(key, tuples, ZAddArgs.none());
}
Long zAdd(String key, Set<StringTuple> tuples);
/**
* Add {@code tuples} to a sorted set at {@code key}, or update its {@code score} depending on the given

View File

@@ -98,14 +98,15 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
}
if (command.isUpsert()) {
args = ZAddArgs.Builder.nx();
args = args == null ? ZAddArgs.Builder.nx() : args.nx();
} else {
args = ZAddArgs.Builder.xx();
args = args == null ? ZAddArgs.Builder.xx() : args.xx();
}
if(command.isGt()) {
if (command.isGt()) {
args = args == null ? ZAddArgs.Builder.gt() : args.gt();
}
if(command.isLt()) {
if (command.isLt()) {
args = args == null ? ZAddArgs.Builder.lt() : args.lt();
}
}
@@ -168,7 +169,8 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
Assert.notNull(command.getValue(), "Value must not be null!");
Mono<Long> result = ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)
? cmd.zrank(command.getKey(), command.getValue()) : cmd.zrevrank(command.getKey(), command.getValue());
? cmd.zrank(command.getKey(), command.getValue())
: cmd.zrevrank(command.getKey(), command.getValue());
return result.map(value -> new NumericResponse<>(command, value));
}));
@@ -194,24 +196,21 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
if (ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)) {
if (command.isWithScores()) {
result = cmd
.zrangeWithScores(command.getKey(), start, stop).map(sc -> new DefaultTuple(getBytes(sc), sc.getScore()));
result = cmd.zrangeWithScores(command.getKey(), start, stop)
.map(sc -> new DefaultTuple(getBytes(sc), sc.getScore()));
} else {
result = cmd
.zrange(command.getKey(), start, stop)
result = cmd.zrange(command.getKey(), start, stop)
.map(value -> new DefaultTuple(ByteUtils.getBytes(value), Double.NaN));
}
} else {
if (command.isWithScores()) {
result = cmd
.zrevrangeWithScores(command.getKey(), start, stop)
result = cmd.zrevrangeWithScores(command.getKey(), start, stop)
.map(sc -> new DefaultTuple(getBytes(sc), sc.getScore()));
} else {
result = cmd
.zrevrange(command.getKey(), start, stop)
result = cmd.zrevrange(command.getKey(), start, stop)
.map(value -> new DefaultTuple(ByteUtils.getBytes(value), Double.NaN));
}
}
@@ -399,8 +398,7 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands {
LettuceConverters.getLowerBoundIndex(command.getRange()), //
LettuceConverters.getUpperBoundIndex(command.getRange()));
return result
.map(value -> new NumericResponse<>(command, value));
return result.map(value -> new NumericResponse<>(command, value));
}));
}

View File

@@ -59,7 +59,8 @@ class LettuceZSetCommands implements RedisZSetCommands {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(value, "Value must not be null!");
return connection.invoke().from(RedisSortedSetAsyncCommands::zadd, key, LettuceZSetCommands.toZAddArgs(args), score, value)
return connection.invoke()
.from(RedisSortedSetAsyncCommands::zadd, key, LettuceZSetCommands.toZAddArgs(args), score, value)
.get(LettuceConverters.longToBoolean());
}
@@ -165,15 +166,15 @@ class LettuceZSetCommands implements RedisZSetCommands {
Assert.notNull(range, "Range for ZRANGEBYSCOREWITHSCORES must not be null!");
Assert.notNull(limit, "Limit must not be null!");
if (limit.isUnlimited()) {
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
}
if (limit.isUnlimited()) {
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
}
return connection.invoke()
.fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
.toSet(LettuceConverters::toTuple);
return connection
.invoke().fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
.toSet(LettuceConverters::toTuple);
}
@@ -214,15 +215,15 @@ class LettuceZSetCommands implements RedisZSetCommands {
Assert.notNull(range, "Range for ZREVRANGEBYSCORE must not be null!");
Assert.notNull(limit, "Limit must not be null!");
if (limit.isUnlimited()) {
if (limit.isUnlimited()) {
return connection.invoke()
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key, LettuceConverters.<Number> toRange(range))
.toSet();
}
return connection.invoke()
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key, LettuceConverters.<Number> toRange(range))
.toSet();
}
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key,
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit)).toSet();
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key,
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit)).toSet();
}
@@ -237,15 +238,15 @@ class LettuceZSetCommands implements RedisZSetCommands {
Assert.notNull(range, "Range for ZREVRANGEBYSCOREWITHSCORES must not be null!");
Assert.notNull(limit, "Limit must not be null!");
if (limit.isUnlimited()) {
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
}
if (limit.isUnlimited()) {
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
}
return connection.invoke()
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
.toSet(LettuceConverters::toTuple);
return connection.invoke()
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
.toSet(LettuceConverters::toTuple);
}
@@ -580,23 +581,23 @@ class LettuceZSetCommands implements RedisZSetCommands {
io.lettuce.core.ZAddArgs target = new io.lettuce.core.ZAddArgs();
if(!source.isEmpty()) {
if (!source.isEmpty()) {
return target;
}
if(source.contains(Flag.XX)) {
if (source.contains(Flag.XX)) {
target.xx();
}
if(source.contains(Flag.NX)) {
if (source.contains(Flag.NX)) {
target.nx();
}
if(source.contains(Flag.GT)) {
if (source.contains(Flag.GT)) {
target.gt();
}
if(source.contains(Flag.LT)) {
if (source.contains(Flag.LT)) {
target.lt();
}
if(source.contains(Flag.CH)) {
if (source.contains(Flag.CH)) {
target.ch();
}
return target;

View File

@@ -50,7 +50,10 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
*/
@Override
public Boolean add(K key, V value, double score) {
return add(key, value, score, ZAddArgs.none());
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.zAdd(rawKey, score, rawValue), true);
}
/*
@@ -83,7 +86,10 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
*/
@Override
public Long add(K key, Set<TypedTuple<V>> tuples) {
return add(key, tuples, ZAddArgs.none());
byte[] rawKey = rawKey(key);
Set<Tuple> rawValues = rawTupleValues(tuples);
return execute(connection -> connection.zAdd(rawKey, rawValues), true);
}
/*