Skip to content

Commit 8f88b78

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-661 - Refactor RedisServerCommands.getConfig(…) output to Properties.
Use Properties instead of List of Strings arranged as sequence of key-value pairs to improve value lookup. Properties allows direct value lookup whereas the List previously required List scanning for an index and another index access to retrieve the actual value. Original Pull Request: #255
1 parent 50551fd commit 8f88b78

21 files changed

+166
-201
lines changed

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* Copyright 2011-2016 the original author or authors.
3-
*
2+
* Copyright 2011-2017 the original author or authors.
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,17 +15,8 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import java.util.ArrayList;
19-
import java.util.Collection;
20-
import java.util.HashMap;
21-
import java.util.LinkedHashMap;
22-
import java.util.LinkedList;
23-
import java.util.List;
24-
import java.util.Map;
18+
import java.util.*;
2519
import java.util.Map.Entry;
26-
import java.util.Properties;
27-
import java.util.Queue;
28-
import java.util.Set;
2920
import java.util.concurrent.TimeUnit;
3021

3122
import org.apache.commons.logging.Log;
@@ -52,7 +43,7 @@
5243

5344
/**
5445
* Default implementation of {@link StringRedisConnection}.
55-
*
46+
*
5647
* @author Costin Leau
5748
* @author Jennifer Hickey
5849
* @author Christoph Strobl
@@ -122,7 +113,7 @@ public List<Object> convert(List<Object> execResults) {
122113
/**
123114
* Constructs a new <code>DefaultStringRedisConnection</code> instance. Uses {@link StringRedisSerializer} as
124115
* underlying serializer.
125-
*
116+
*
126117
* @param connection Redis connection
127118
*/
128119
public DefaultStringRedisConnection(RedisConnection connection) {
@@ -131,7 +122,7 @@ public DefaultStringRedisConnection(RedisConnection connection) {
131122

132123
/**
133124
* Constructs a new <code>DefaultStringRedisConnection</code> instance.
134-
*
125+
*
135126
* @param connection Redis connection
136127
* @param serializer String serializer
137128
*/
@@ -308,8 +299,8 @@ public Boolean getBit(byte[] key, long offset) {
308299
return result;
309300
}
310301

311-
public List<String> getConfig(String pattern) {
312-
List<String> results = delegate.getConfig(pattern);
302+
public Properties getConfig(String pattern) {
303+
Properties results = delegate.getConfig(pattern);
313304
if (isFutureConversion()) {
314305
addResultConverter(identityConverter);
315306
}
@@ -939,7 +930,7 @@ public Long sUnionStore(byte[] destKey, byte[]... keys) {
939930
return result;
940931
}
941932

942-
/*
933+
/*
943934
* (non-Javadoc)
944935
* @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[])
945936
*/
@@ -953,7 +944,7 @@ public Long ttl(byte[] key) {
953944
return result;
954945
}
955946

956-
/*
947+
/*
957948
* (non-Javadoc)
958949
* @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[], java.util.concurrent.TimeUnit)
959950
*/
@@ -1351,7 +1342,7 @@ public Boolean pExpireAt(byte[] key, long unixTimeInMillis) {
13511342
return result;
13521343
}
13531344

1354-
/*
1345+
/*
13551346
* (non-Javadoc)
13561347
* @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[])
13571348
*/
@@ -1366,7 +1357,7 @@ public Long pTtl(byte[] key) {
13661357
return result;
13671358
}
13681359

1369-
/*
1360+
/*
13701361
* (non-Javadoc)
13711362
* @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[], java.util.concurrent.TimeUnit)
13721363
*/
@@ -2103,7 +2094,7 @@ public Long sUnionStore(String destKey, String... keys) {
21032094
return result;
21042095
}
21052096

2106-
/*
2097+
/*
21072098
* (non-Javadoc)
21082099
* @see org.springframework.data.redis.connection.StringRedisConnection#ttl(java.lang.String)
21092100
*/
@@ -2112,7 +2103,7 @@ public Long ttl(String key) {
21122103
return ttl(serialize(key));
21132104
}
21142105

2115-
/*
2106+
/*
21162107
* (non-Javadoc)
21172108
* @see org.springframework.data.redis.connection.StringRedisConnection#ttl(java.lang.String, java.util.concurrent.TimeUnit)
21182109
*/
@@ -2743,7 +2734,7 @@ public Boolean pExpireAt(String key, long unixTimeInMillis) {
27432734
return pExpireAt(serialize(key), unixTimeInMillis);
27442735
}
27452736

2746-
/*
2737+
/*
27472738
* (non-Javadoc)
27482739
* @see org.springframework.data.redis.connection.StringRedisConnection#pTtl(java.lang.String)
27492740
*/
@@ -2752,7 +2743,7 @@ public Long pTtl(String key) {
27522743
return pTtl(serialize(key));
27532744
}
27542745

2755-
/*
2746+
/*
27562747
* (non-Javadoc)
27572748
* @see org.springframework.data.redis.connection.StringRedisConnection#pTtl(java.lang.String, java.util.concurrent.TimeUnit)
27582749
*/
@@ -2835,7 +2826,7 @@ public Cursor<byte[]> scan(ScanOptions options) {
28352826
}
28362827

28372828
/*
2838-
*
2829+
*
28392830
*/
28402831
@Override
28412832
public Cursor<Tuple> zScan(byte[] key, ScanOptions options) {
@@ -2863,7 +2854,7 @@ public Cursor<Entry<byte[], byte[]>> hScan(byte[] key, ScanOptions options) {
28632854
/**
28642855
* Specifies if pipelined and tx results should be deserialized to Strings. If false, results of
28652856
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the underlying connection
2866-
*
2857+
*
28672858
* @param deserializePipelineAndTxResults Whether or not to deserialize pipeline and tx results
28682859
*/
28692860
public void setDeserializePipelineAndTxResults(boolean deserializePipelineAndTxResults) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ default void shutdown(RedisClusterNode node) {
100100
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
101101
@Override
102102
@Deprecated
103-
default List<String> getConfig(RedisClusterNode node, String pattern) {
103+
default Properties getConfig(RedisClusterNode node, String pattern) {
104104
return serverCommands().getConfig(node, pattern);
105105
}
106106

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ default void shutdown(ShutdownOption option) {
11071107
/** @deprecated in favor of {@link RedisConnection#serverCommands()}. */
11081108
@Override
11091109
@Deprecated
1110-
default List<String> getConfig(String pattern) {
1110+
default Properties getConfig(String pattern) {
11111111
return serverCommands().getConfig(pattern);
11121112
}
11131113

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public interface RedisClusterServerCommands extends RedisServerCommands {
9797
* @return
9898
* @see RedisServerCommands#getConfig(String)
9999
*/
100-
List<String> getConfig(RedisClusterNode node, String pattern);
100+
Properties getConfig(RedisClusterNode node, String pattern);
101101

102102
/**
103103
* @param node must not be {@literal null}.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ default void bgWriteAof() {
148148
* @return
149149
* @see <a href="http://redis.io/commands/config-get">Redis Documentation: CONFIG GET</a>
150150
*/
151-
List<String> getConfig(String pattern);
151+
Properties getConfig(String pattern);
152152

153153
/**
154154
* Set server configuration for {@code param} to {@code value}.

src/main/java/org/springframework/data/redis/connection/convert/Converters.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,16 +15,9 @@
1515
*/
1616
package org.springframework.data.redis.connection.convert;
1717

18-
import java.util.ArrayList;
19-
import java.util.Arrays;
20-
import java.util.Collection;
21-
import java.util.Collections;
22-
import java.util.LinkedHashMap;
23-
import java.util.LinkedHashSet;
24-
import java.util.List;
25-
import java.util.Map;
26-
import java.util.Properties;
27-
import java.util.Set;
18+
import lombok.RequiredArgsConstructor;
19+
20+
import java.util.*;
2821
import java.util.concurrent.TimeUnit;
2922

3023
import org.springframework.core.convert.converter.Converter;
@@ -50,8 +43,6 @@
5043
import org.springframework.util.ObjectUtils;
5144
import org.springframework.util.StringUtils;
5245

53-
import lombok.RequiredArgsConstructor;
54-
5546
/**
5647
* Common type converters
5748
*
@@ -70,6 +61,7 @@ abstract public class Converters {
7061
private static final Converter<String, DataType> STRING_TO_DATA_TYPE = new StringToDataTypeConverter();
7162
private static final Converter<Map<?, ?>, Properties> MAP_TO_PROPERTIES = MapToPropertiesConverter.INSTANCE;
7263
private static final Converter<String, RedisClusterNode> STRING_TO_CLUSTER_NODE_CONVERTER;
64+
private static final Converter<List<String>, Properties> STRING_LIST_TO_PROPERTIES_CONVERTER;
7365
private static final Map<String, Flag> flagLookupMap;
7466

7567
static {
@@ -172,6 +164,21 @@ private SlotRange parseSlotRange(String[] args) {
172164
}
173165

174166
};
167+
168+
STRING_LIST_TO_PROPERTIES_CONVERTER = input -> {
169+
170+
Assert.notNull(input, "Input list must not be null!");
171+
Assert.isTrue(input.size() % 2 == 0, "Input list must contain an even number of entries!");
172+
173+
Properties properties = new Properties();
174+
175+
for (int i = 0; i < input.size(); i += 2) {
176+
177+
properties.setProperty(input.get(i), input.get(i + 1));
178+
}
179+
180+
return properties;
181+
};
175182
}
176183

177184
public static Boolean stringToBoolean(String s) {
@@ -224,7 +231,7 @@ protected static RedisClusterNode toClusterNode(String clusterNodesLine) {
224231
/**
225232
* Converts lines from the result of {@code CLUSTER NODES} into {@link RedisClusterNode}s.
226233
*
227-
* @param clusterNodes
234+
* @param lines
228235
* @return
229236
* @since 1.7
230237
*/
@@ -302,7 +309,7 @@ public static long secondsToTimeUnit(long seconds, TimeUnit targetUnit) {
302309

303310
/**
304311
* Creates a new {@link Converter} to convert from seconds to the given {@link TimeUnit}.
305-
*
312+
*
306313
* @param timeUnit muist not be {@literal null}.
307314
* @return
308315
* @since 1.8
@@ -378,6 +385,29 @@ public static Converter<Double, Distance> distanceConverterForMetric(Metric metr
378385
return DistanceConverterFactory.INSTANCE.forMetric(metric);
379386
}
380387

388+
/**
389+
* Converts array outputs with key-value sequences (such as produced by {@code CONFIG GET}) from a {@link List} to
390+
* {@link Properties}.
391+
*
392+
* @param input must not be {@literal null}.
393+
* @return the mapped result.
394+
* @since 2.0
395+
*/
396+
public static Properties toProperties(List<String> input) {
397+
return STRING_LIST_TO_PROPERTIES_CONVERTER.convert(input);
398+
}
399+
400+
/**
401+
* Returns a converter to convert array outputs with key-value sequences (such as produced by {@code CONFIG GET}) from
402+
* a {@link List} to {@link Properties}.
403+
*
404+
* @return the converter.
405+
* @since 2.0
406+
*/
407+
public static Converter<List<String>, Properties> listToPropertiesConverter() {
408+
return STRING_LIST_TO_PROPERTIES_CONVERTER;
409+
}
410+
381411
/**
382412
* @author Christoph Strobl
383413
* @since 1.8
@@ -436,5 +466,4 @@ public GeoResults<GeoLocation<V>> convert(GeoResults<GeoLocation<byte[]>> source
436466
return new GeoResults<GeoLocation<V>>(values, source.getAverageDistance().getMetric());
437467
}
438468
}
439-
440469
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void shutdown(ShutdownOption option) {
300300
* @see org.springframework.data.redis.connection.RedisServerCommands#getConfig(java.lang.String)
301301
*/
302302
@Override
303-
public List<String> getConfig(final String pattern) {
303+
public Properties getConfig(final String pattern) {
304304

305305
List<NodeResult<List<String>>> mapResult = connection.getClusterCommandExecutor()
306306
.executeCommandOnAllNodes((JedisClusterCommandCallback<List<String>>) client -> client.configGet(pattern))
@@ -316,18 +316,19 @@ public List<String> getConfig(final String pattern) {
316316
}
317317
}
318318

319-
return result;
319+
return Converters.toProperties(result);
320320
}
321321

322322
/*
323323
* (non-Javadoc)
324324
* @see org.springframework.data.redis.connection.RedisClusterServerCommands#getConfig(org.springframework.data.redis.connection.RedisClusterNode, java.lang.String)
325325
*/
326326
@Override
327-
public List<String> getConfig(RedisClusterNode node, final String pattern) {
327+
public Properties getConfig(RedisClusterNode node, final String pattern) {
328328

329329
return connection.getClusterCommandExecutor().executeCommandOnSingleNode(
330-
(JedisClusterCommandCallback<List<String>>) client -> client.configGet(pattern), node).getValue();
330+
(JedisClusterCommandCallback<Properties>) client -> Converters.toProperties(client.configGet(pattern)), node)
331+
.getValue();
331332
}
332333

333334
/*

0 commit comments

Comments
 (0)