Skip to content

Commit 0cfd473

Browse files
committed
DATAREDIS-529 - Polishing.
Accept varargs byte arrays instead of Collection of byte arrays. Bypass own routing for exists single-slot execution in Redis Cluster. Replace parallel stream execution of exists(…) with sequential ClusterCommandExecutor dispatch. Reorder methods in ClusterConnectionTests by name. Adapt Javadoc. Original pull request: spring-projects#281.
1 parent c843269 commit 0cfd473

File tree

12 files changed

+2590
-2610
lines changed

12 files changed

+2590
-2610
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,15 @@ public Boolean exists(byte[] key) {
300300
*/
301301
@Override
302302
public Long exists(String... keys) {
303-
return convertAndReturn(delegate.exists(Arrays.asList(serializeMulti(keys))), identityConverter);
303+
return convertAndReturn(delegate.exists(serializeMulti(keys)), identityConverter);
304304
}
305305

306306
/*
307307
* (non-Javadoc)
308-
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(java.util.Collection)
308+
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(byte[][])
309309
*/
310310
@Override
311-
public Long exists(Collection<byte[]> keys) {
311+
public Long exists(byte[]... keys) {
312312
return convertAndReturn(delegate.exists(keys), identityConverter);
313313
}
314314

src/main/java/org/springframework/data/redis/connection/DefaultedRedisConnection.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import java.util.Collection;
1918
import java.util.List;
2019
import java.util.Map;
2120
import java.util.Map.Entry;
@@ -58,7 +57,7 @@ default Boolean exists(byte[] key) {
5857
/** @deprecated in favor of {@link RedisConnection#keyCommands()}. */
5958
@Override
6059
@Deprecated
61-
default Long exists(Collection<byte[]> keys) {
60+
default Long exists(byte[]... keys) {
6261
return keyCommands().exists(keys);
6362
}
6463

src/main/java/org/springframework/data/redis/connection/RedisKeyCommands.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import java.util.Collection;
19-
import java.util.Collections;
2018
import java.util.List;
2119
import java.util.Set;
2220
import java.util.concurrent.TimeUnit;
@@ -46,20 +44,21 @@ public interface RedisKeyCommands {
4644
default Boolean exists(byte[] key) {
4745

4846
Assert.notNull(key, "Key must not be null!");
49-
Long count = exists(Collections.singleton(key));
47+
Long count = exists(new byte[][] { key });
5048
return count != null ? count > 0 : null;
5149
}
5250

5351
/**
54-
* Count how many of the given {@code keys} exists. Providing the very same {@code key} more than once also counts
52+
* Count how many of the given {@code keys} exist. Providing the very same {@code key} more than once also counts
5553
* multiple times.
5654
*
5755
* @param keys must not be {@literal null}.
58-
* @return the number of keys existing among the ones specified as arguments.
56+
* @return the number of keys existing among the ones specified as arguments. {@literal null} when used in pipeline /
57+
* transaction.
5958
* @since 2.1
6059
*/
6160
@Nullable
62-
Long exists(Collection<byte[]> keys);
61+
Long exists(byte[]... keys);
6362

6463
/**
6564
* Delete given {@code keys}.

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ interface StringTuple extends Tuple {
9292
Boolean exists(String key);
9393

9494
/**
95-
* Count how many of the if given {@code keys} exists.
95+
* Count how many of the given {@code keys} exist.
9696
*
9797
* @param keys must not be {@literal null}.
9898
* @return
9999
* @see <a href="http://redis.io/commands/exists">Redis Documentation: EXISTS</a>
100-
* @see RedisKeyCommands#exists(java.util.Collection)
100+
* @see RedisKeyCommands#exists(byte[][])
101101
* @since 2.1
102102
*/
103103
@Nullable

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterKeyCommands.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.redis.connection.jedis;
1717

18+
import redis.clients.jedis.BinaryJedis;
19+
1820
import java.util.ArrayList;
1921
import java.util.Arrays;
2022
import java.util.Collection;
@@ -62,7 +64,8 @@ class JedisClusterKeyCommands implements RedisKeyCommands {
6264
@Override
6365
public Long del(byte[]... keys) {
6466

65-
Assert.noNullElements(keys, "Keys must not be null or contain null key!");
67+
Assert.notNull(keys, "Keys must not be null!");
68+
Assert.noNullElements(keys, "Keys must not contain null elements!");
6669

6770
if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) {
6871
try {
@@ -473,26 +476,26 @@ public Long sort(byte[] key, SortParameters params, byte[] storeKey) {
473476

474477
/*
475478
* (non-Javadoc)
476-
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(java.util.Collection)
479+
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(byte[][])
477480
*/
478481
@Nullable
479482
@Override
480-
public Long exists(Collection<byte[]> keys) {
483+
public Long exists(byte[]... keys) {
481484

482485
Assert.notNull(keys, "Keys must not be null!");
486+
Assert.noNullElements(keys, "Keys must not contain null elements!");
483487

484-
try {
485-
486-
return keys.stream() //
487-
.parallel()
488-
.map(key -> connection.getClusterCommandExecutor()
489-
.executeCommandOnSingleNode((JedisClusterCommandCallback<Boolean>) client -> client.exists(key),
490-
connection.getTopologyProvider().getTopology().getKeyServingMasterNode(key))
491-
.getValue())
492-
.mapToLong(val -> ObjectUtils.nullSafeEquals(val, Boolean.TRUE) ? 1L : 0L).sum();
493-
} catch (Exception ex) {
494-
throw convertJedisAccessException(ex);
488+
if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) {
489+
try {
490+
return connection.getCluster().exists(keys);
491+
} catch (Exception ex) {
492+
throw convertJedisAccessException(ex);
493+
}
495494
}
495+
496+
return connection.getClusterCommandExecutor()
497+
.executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<Boolean>) BinaryJedis::exists, Arrays.asList(keys))
498+
.resultsAsList().stream().mapToLong(val -> ObjectUtils.nullSafeEquals(val, Boolean.TRUE) ? 1 : 0).sum();
496499
}
497500

498501
private DataAccessException convertJedisAccessException(Exception ex) {

src/main/java/org/springframework/data/redis/connection/jedis/JedisKeyCommands.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import redis.clients.jedis.ScanParams;
2121
import redis.clients.jedis.SortingParams;
2222

23-
import java.util.Collection;
2423
import java.util.List;
2524
import java.util.Set;
2625
import java.util.concurrent.TimeUnit;
@@ -73,26 +72,27 @@ public Boolean exists(byte[] key) {
7372

7473
/*
7574
* (non-Javadoc)
76-
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(java.util.Collection)
75+
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(byte[][])
7776
*/
7877
@Nullable
7978
@Override
80-
public Long exists(Collection<byte[]> keys) {
79+
public Long exists(byte[]... keys) {
8180

8281
Assert.notNull(keys, "Keys must not be null!");
82+
Assert.noNullElements(keys, "Keys must not contain null elements!");
8383

8484
try {
8585
if (isPipelined()) {
8686
pipeline(
87-
connection.newJedisResult(connection.getRequiredPipeline().exists(keys.toArray(new byte[keys.size()][]))));
87+
connection.newJedisResult(connection.getRequiredPipeline().exists(keys)));
8888
return null;
8989
}
9090
if (isQueueing()) {
9191
transaction(connection
92-
.newJedisResult(connection.getRequiredTransaction().exists(keys.toArray(new byte[keys.size()][]))));
92+
.newJedisResult(connection.getRequiredTransaction().exists(keys)));
9393
return null;
9494
}
95-
return connection.getJedis().exists(keys.toArray(new byte[keys.size()][]));
95+
return connection.getJedis().exists(keys);
9696
} catch (Exception ex) {
9797
throw connection.convertJedisAccessException(ex);
9898
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceKeyCommands.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import lombok.NonNull;
2424
import lombok.RequiredArgsConstructor;
2525

26-
import java.util.Collection;
2726
import java.util.List;
2827
import java.util.Set;
2928
import java.util.concurrent.TimeUnit;
@@ -80,24 +79,25 @@ public Boolean exists(byte[] key) {
8079

8180
/*
8281
* (non-Javadoc)
83-
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(java.util.Collection)
82+
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(byte[][])
8483
*/
8584
@Nullable
8685
@Override
87-
public Long exists(Collection<byte[]> keys) {
86+
public Long exists(byte[]... keys) {
8887

8988
Assert.notNull(keys, "Keys must not be null!");
89+
Assert.noNullElements(keys, "Keys must not contain null elements!");
9090

9191
try {
9292
if (isPipelined()) {
93-
pipeline(connection.newLettuceResult(getAsyncConnection().exists(keys.toArray(new byte[keys.size()][]))));
93+
pipeline(connection.newLettuceResult(getAsyncConnection().exists(keys)));
9494
return null;
9595
}
9696
if (isQueueing()) {
97-
transaction(connection.newLettuceTxResult(getAsyncConnection().exists(keys.toArray(new byte[keys.size()][]))));
97+
transaction(connection.newLettuceTxResult(getAsyncConnection().exists(keys)));
9898
return null;
9999
}
100-
return getConnection().exists(keys.toArray(new byte[keys.size()][]));
100+
return getConnection().exists(keys);
101101
} catch (Exception ex) {
102102
throw convertLettuceAccessException(ex);
103103
}

src/main/java/org/springframework/data/redis/core/RedisOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSer
163163
Boolean hasKey(K key);
164164

165165
/**
166-
* Count the number of {@code keys} that exists.
166+
* Count the number of {@code keys} that exist.
167167
*
168168
* @param keys must not be {@literal null}.
169169
* @return The number of keys existing among the ones specified as arguments. Keys mentioned multiple times and

src/main/java/org/springframework/data/redis/core/RedisTemplate.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.io.Closeable;
1919
import java.lang.reflect.Proxy;
2020
import java.util.ArrayList;
21-
import java.util.Arrays;
2221
import java.util.Collection;
2322
import java.util.Collections;
2423
import java.util.Date;
@@ -750,7 +749,7 @@ public Long countExistingKeys(Collection<K> keys) {
750749
Assert.notNull(keys, "Keys must not be null!");
751750

752751
byte[][] rawKeys = rawKeys(keys);
753-
return execute(connection -> connection.exists(Arrays.asList(rawKeys)), true);
752+
return execute(connection -> connection.exists(rawKeys), true);
754753
}
755754

756755
/*
@@ -1327,7 +1326,7 @@ public void setEnableTransactionSupport(boolean enableTransactionSupport) {
13271326
* Set the {@link ClassLoader} to be used for the default {@link JdkSerializationRedisSerializer} in case no other
13281327
* {@link RedisSerializer} is explicitly set as the default one.
13291328
*
1330-
* @param resourceLoader can be {@literal null}.
1329+
* @param classLoader can be {@literal null}.
13311330
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader
13321331
* @since 1.8
13331332
*/

0 commit comments

Comments
 (0)