diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterZSetCommands.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterZSetCommands.java index 731deaddb..22a96d29d 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterZSetCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterZSetCommands.java @@ -19,6 +19,7 @@ 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; @@ -33,6 +34,7 @@ import org.springframework.util.Assert; /** * @author Christoph Strobl + * @author Clement Ong * @since 2.0 */ class JedisClusterZSetCommands implements RedisZSetCommands { @@ -64,8 +66,13 @@ class JedisClusterZSetCommands implements RedisZSetCommands { @Override public Long zAdd(byte[] key, Set tuples) { - // TODO: need to move the tuple conversion form jedisconnection. - throw new UnsupportedOperationException(); + Map args = JedisConverters.zAddArgsConvertor(tuples); + + try { + return connection.getCluster().zadd(key, args); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } } /* diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java index 61d88285a..17d91ba91 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java @@ -28,6 +28,8 @@ import redis.clients.util.SafeEncoder; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -658,4 +660,33 @@ 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 zAddArgsConvertor(Set tuples) { + + Map args = new LinkedHashMap<>(tuples.size(), 1); + Set 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; + } } diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisZSetCommands.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisZSetCommands.java index 165b9fa2b..5b6078ddc 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisZSetCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisZSetCommands.java @@ -19,8 +19,6 @@ import redis.clients.jedis.ScanParams; import redis.clients.jedis.ScanResult; import redis.clients.jedis.ZParams; -import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; @@ -79,7 +77,7 @@ class JedisZSetCommands implements RedisZSetCommands { throw new UnsupportedOperationException("zAdd of multiple fields not supported " + "in pipeline or transaction"); } - Map args = zAddArgs(tuples); + Map args = JedisConverters.zAddArgsConvertor(tuples); try { return connection.getJedis().zadd(key, args); } catch (Exception ex) { @@ -808,29 +806,6 @@ class JedisZSetCommands implements RedisZSetCommands { } } - private Map zAddArgs(Set tuples) { - - Map args = new LinkedHashMap<>(tuples.size(), 1); - Set 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; - } - private boolean isPipelined() { return connection.isPipelined(); }