diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java index 8fa50eae5..ab73f12df 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java @@ -1,12 +1,12 @@ /* * Copyright 2011-2017 the original author or authors. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -985,7 +985,7 @@ public class JedisConnection extends AbstractRedisConnection { } } - /* + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[]) */ @@ -1010,7 +1010,7 @@ public class JedisConnection extends AbstractRedisConnection { } } - /* + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[], java.util.concurrent.TimeUnit) */ @@ -1073,7 +1073,7 @@ public class JedisConnection extends AbstractRedisConnection { } } - /* + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[]) */ @@ -1098,7 +1098,7 @@ public class JedisConnection extends AbstractRedisConnection { } } - /* + /* * (non-Javadoc) * @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[], java.util.concurrent.TimeUnit) */ @@ -2232,7 +2232,7 @@ public class JedisConnection extends AbstractRedisConnection { if (isPipelined() || isQueueing()) { throw new UnsupportedOperationException("zAdd of multiple fields not supported " + "in pipeline or transaction"); } - Map args = zAddArgs(tuples); + Map args = JedisConverters.zAddArgsConvertor(tuples); try { return jedis.zadd(key, args); } catch (Exception ex) { @@ -3957,29 +3957,6 @@ public class JedisConnection extends AbstractRedisConnection { return args.toArray(new byte[args.size()][]); } - 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; - } - @Override protected boolean isActive(RedisNode node) { 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 3ca3774fb..ad5404e1a 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 @@ -18,6 +18,8 @@ package org.springframework.data.redis.connection.jedis; 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; @@ -72,7 +74,7 @@ import redis.clients.util.SafeEncoder; /** * Jedis type converters. - * + * * @author Jennifer Hickey * @author Christoph Strobl * @author Thomas Darimont @@ -224,7 +226,7 @@ abstract public class JedisConverters extends Converters { /** * {@link ListConverter} converting jedis {@link redis.clients.jedis.Tuple} to {@link Tuple}. - * + * * @return * @since 1.4 */ @@ -394,7 +396,7 @@ abstract public class JedisConverters extends Converters { /** * Converts a given {@link Boundary} to its binary representation suitable for {@literal ZRANGEBY*} commands, despite * {@literal ZRANGEBYLEX}. - * + * * @param boundary * @param defaultValue * @return @@ -411,7 +413,7 @@ abstract public class JedisConverters extends Converters { /** * Converts a given {@link Boundary} to its binary representation suitable for ZRANGEBYLEX command. - * + * * @param boundary * @return * @since 1.6 @@ -433,7 +435,7 @@ abstract public class JedisConverters extends Converters { *
{@link TimeUnit#MILLISECONDS}
*
{@code PX}
* - * + * * @param expiration * @return * @since 1.7 @@ -452,7 +454,7 @@ abstract public class JedisConverters extends Converters { *
{@link SetOption#SET_IF_PRESENT}
*
{@code XX}
* - * + * * @param option * @return * @since 1.7 @@ -537,7 +539,7 @@ abstract public class JedisConverters extends Converters { /** * Get a {@link Converter} capable of converting {@link GeoRadiusResponse} into {@link GeoResults}. - * + * * @param metric * @return * @since 1.8 @@ -549,7 +551,7 @@ abstract public class JedisConverters extends Converters { /** * Convert {@link Metric} into {@link GeoUnit}. - * + * * @param metric * @return * @since 1.8 @@ -563,7 +565,7 @@ abstract public class JedisConverters extends Converters { /** * Convert {@link Point} into {@link GeoCoordinate}. - * + * * @param source * @return * @since 1.8 @@ -574,7 +576,7 @@ abstract public class JedisConverters extends Converters { /** * Convert {@link GeoRadiusCommandArgs} into {@link GeoRadiusParam}. - * + * * @param source * @return * @since 1.8 @@ -686,4 +688,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; + } }