DATAREDIS-674 - Polishing.
Rename JedisConverters.zAddArgsConvertor(…) to toTupleMap(…) and reorder method to group with other tuple conversion methods. Add author tags. Create tests for zadd with tuple using Redis Cluster. Enable pipelining and transactions for zadd using Jedis. Adopt tests. Original pull request: #263.
This commit is contained in:
@@ -19,7 +19,6 @@ import redis.clients.jedis.ScanParams;
|
||||
import redis.clients.jedis.ZParams;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
@@ -66,10 +65,8 @@ class JedisClusterZSetCommands implements RedisZSetCommands {
|
||||
@Override
|
||||
public Long zAdd(byte[] key, Set<Tuple> tuples) {
|
||||
|
||||
Map<byte[], Double> args = JedisConverters.zAddArgsConvertor(tuples);
|
||||
|
||||
try {
|
||||
return connection.getCluster().zadd(key, args);
|
||||
return connection.getCluster().zadd(key, JedisConverters.toTupleMap(tuples));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
|
||||
@@ -238,6 +238,38 @@ abstract public class JedisConverters extends Converters {
|
||||
return TUPLE_SET_TO_TUPLE_SET.convert(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a {@link Set} of {@link Tuple} by {@code value} to its {@code score}.
|
||||
*
|
||||
* @param tuples must not be {@literal null}.
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static Map<byte[], Double> toTupleMap(Set<Tuple> tuples) {
|
||||
|
||||
Assert.notNull(tuples, "Tuple set must not be null!");
|
||||
|
||||
Map<byte[], Double> args = new LinkedHashMap<>(tuples.size(), 1);
|
||||
Set<Double> scores = new HashSet<>(tuples.size(), 1);
|
||||
|
||||
boolean isAtLeastJedis24 = JedisVersionUtil.atLeastJedis24();
|
||||
|
||||
for (Tuple tuple : tuples) {
|
||||
|
||||
if (!isAtLeastJedis24) {
|
||||
if (scores.contains(tuple.getScore())) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Bulk add of multiple elements with the same score is not supported. Add the elements individually.");
|
||||
}
|
||||
scores.add(tuple.getScore());
|
||||
}
|
||||
|
||||
args.put(tuple.getValue(), tuple.getScore());
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
public static byte[] toBytes(Integer source) {
|
||||
return String.valueOf(source).getBytes();
|
||||
}
|
||||
@@ -660,33 +692,4 @@ abstract public class JedisConverters extends Converters {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert tuples to map of bytes and double.
|
||||
* Bytes represents the value of the element and double is for the score.
|
||||
* @param tuples
|
||||
* @return
|
||||
*/
|
||||
public static Map<byte[], Double> zAddArgsConvertor(Set<Tuple> tuples) {
|
||||
|
||||
Map<byte[], Double> args = new LinkedHashMap<>(tuples.size(), 1);
|
||||
Set<Double> scores = new HashSet<>(tuples.size(), 1);
|
||||
|
||||
boolean isAtLeastJedis24 = JedisVersionUtil.atLeastJedis24();
|
||||
|
||||
for (Tuple tuple : tuples) {
|
||||
|
||||
if (!isAtLeastJedis24) {
|
||||
if (scores.contains(tuple.getScore())) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Bulk add of multiple elements with the same score is not supported. Add the elements individually.");
|
||||
}
|
||||
scores.add(tuple.getScore());
|
||||
}
|
||||
|
||||
args.put(tuple.getValue(), tuple.getScore());
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ import redis.clients.jedis.ScanParams;
|
||||
import redis.clients.jedis.ScanResult;
|
||||
import redis.clients.jedis.ZParams;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.redis.connection.RedisZSetCommands;
|
||||
@@ -32,6 +31,7 @@ import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Clement Ong
|
||||
* @since 2.0
|
||||
*/
|
||||
class JedisZSetCommands implements RedisZSetCommands {
|
||||
@@ -73,13 +73,17 @@ class JedisZSetCommands implements RedisZSetCommands {
|
||||
@Override
|
||||
public Long zAdd(byte[] key, Set<Tuple> tuples) {
|
||||
|
||||
if (isPipelined() || isQueueing()) {
|
||||
throw new UnsupportedOperationException("zAdd of multiple fields not supported " + "in pipeline or transaction");
|
||||
}
|
||||
|
||||
Map<byte[], Double> args = JedisConverters.zAddArgsConvertor(tuples);
|
||||
try {
|
||||
return connection.getJedis().zadd(key, args);
|
||||
if (isPipelined()) {
|
||||
pipeline(connection.newJedisResult(connection.getPipeline().zadd(key, JedisConverters.toTupleMap(tuples))));
|
||||
return null;
|
||||
}
|
||||
if (isQueueing()) {
|
||||
transaction(
|
||||
connection.newJedisResult(connection.getTransaction().zadd(key, JedisConverters.toTupleMap(tuples))));
|
||||
return null;
|
||||
}
|
||||
return connection.getJedis().zadd(key, JedisConverters.toTupleMap(tuples));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user