Skip to content

Commit f21a87f

Browse files
DATAREDIS-270 - Add support for 'CLIENT GETNAME'.
Support for getting client connection name has been added for 'jedis', 'jredis' and 'srp' drivers. Using 'jredis' will throw 'UnsupportedOperationException'. Original Pull Request: #51
1 parent 7a7fbf3 commit f21a87f

File tree

13 files changed

+189
-1
lines changed

13 files changed

+189
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,15 @@ public Long time() {
21912191
return this.delegate.time();
21922192
}
21932193

2194+
/*
2195+
* (non-Javadoc)
2196+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2197+
*/
2198+
@Override
2199+
public String getClientName() {
2200+
return this.delegate.getClientName();
2201+
}
2202+
21942203
/**
21952204
* Specifies if pipelined and tx results should be deserialized to Strings. If false, results of
21962205
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the underlying connection

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,14 @@ public enum ShutdownOption {
161161
* @since 1.1
162162
*/
163163
Long time();
164+
165+
/**
166+
* Returns the name of the current connection.
167+
*
168+
* @see http://redis.io/commands/client-getname
169+
* @return
170+
* @since 1.3
171+
*/
172+
String getClientName();
173+
164174
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,18 @@ public Long time() {
27832783
return Converters.toTimeMillis(serverTimeInformation.get(0), serverTimeInformation.get(1));
27842784
}
27852785

2786+
/*
2787+
* (non-Javadoc)
2788+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2789+
*/
2790+
@Override
2791+
public String getClientName() {
2792+
if (isPipelined() || isQueueing()) {
2793+
throw new UnsupportedOperationException();
2794+
}
2795+
return jedis.clientGetname();
2796+
}
2797+
27862798
/**
27872799
* Specifies if pipelined results should be converted to the expected data type. If false, results of
27882800
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Jedis driver

src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,4 +1185,13 @@ public <T> T evalSha(String scriptSha1, ReturnType returnType, int numKeys, byte
11851185
public Long time() {
11861186
throw new UnsupportedOperationException("The 'TIME' command is not supported by the JRedis driver.");
11871187
}
1188+
1189+
/*
1190+
* (non-Javadoc)
1191+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
1192+
*/
1193+
@Override
1194+
public String getClientName() {
1195+
throw new UnsupportedOperationException("The 'CLIENT GETNAME' command is not supported by the JRedis driver.");
1196+
}
11881197
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,6 +2903,28 @@ public Long time() {
29032903
return Converters.toTimeMillis(new String(result.get(0)), new String(result.get(1)));
29042904
}
29052905

2906+
/*
2907+
* (non-Javadoc)
2908+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2909+
*/
2910+
@Override
2911+
public String getClientName() {
2912+
2913+
try {
2914+
if (isPipelined()) {
2915+
pipeline(new LettuceResult(getAsyncConnection().clientGetname(), LettuceConverters.bytesToString()));
2916+
return null;
2917+
}
2918+
if (isQueueing()) {
2919+
transaction(new LettuceTxResult(getConnection().clientGetname(), LettuceConverters.bytesToString()));
2920+
return null;
2921+
}
2922+
return LettuceConverters.toString(getConnection().clientGetname());
2923+
} catch (Exception ex) {
2924+
throw convertLettuceAccessException(ex);
2925+
}
2926+
}
2927+
29062928
/**
29072929
* Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of
29082930
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Lettuce driver

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.connection.lettuce;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.Date;
2021
import java.util.LinkedHashSet;
2122
import java.util.List;
@@ -47,6 +48,7 @@ abstract public class LettuceConverters extends Converters {
4748

4849
private static final Converter<Date, Long> DATE_TO_LONG;
4950
private static final Converter<List<byte[]>, Set<byte[]>> BYTES_LIST_TO_BYTES_SET;
51+
private static final Converter<byte[], String> BYTES_TO_STRING;
5052
private static final Converter<Set<byte[]>, List<byte[]>> BYTES_SET_TO_BYTES_LIST;
5153
private static final Converter<KeyValue<byte[], byte[]>, List<byte[]>> KEY_VALUE_TO_BYTES_LIST;
5254
private static final Converter<List<ScoredValue<byte[]>>, Set<Tuple>> SCORED_VALUES_TO_TUPLE_SET;
@@ -65,6 +67,17 @@ public Set<byte[]> convert(List<byte[]> results) {
6567
return results != null ? new LinkedHashSet<byte[]>(results) : null;
6668
}
6769

70+
};
71+
BYTES_TO_STRING = new Converter<byte[], String>() {
72+
73+
@Override
74+
public String convert(byte[] source) {
75+
if (source == null || Arrays.equals(source, new byte[0])) {
76+
return null;
77+
}
78+
return new String(source);
79+
}
80+
6881
};
6982
BYTES_SET_TO_BYTES_LIST = new Converter<Set<byte[]>, List<byte[]>>() {
7083
public List<byte[]> convert(Set<byte[]> results) {
@@ -110,6 +123,10 @@ public static Converter<List<byte[]>, Set<byte[]>> bytesListToBytesSet() {
110123
return BYTES_LIST_TO_BYTES_SET;
111124
}
112125

126+
public static Converter<byte[], String> bytesToString() {
127+
return BYTES_TO_STRING;
128+
}
129+
113130
public static Converter<KeyValue<byte[], byte[]>, List<byte[]>> keyValueToBytesList() {
114131
return KEY_VALUE_TO_BYTES_LIST;
115132
}
@@ -158,6 +175,10 @@ public static DataAccessException toDataAccessException(Exception ex) {
158175
return EXCEPTION_CONVERTER.convert(ex);
159176
}
160177

178+
public static String toString(byte[] source) {
179+
return BYTES_TO_STRING.convert(source);
180+
}
181+
161182
public static ScriptOutputType toScriptOutputType(ReturnType returnType) {
162183
switch (returnType) {
163184
case BOOLEAN:

src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,24 @@ public Long time() {
22222222
return SrpConverters.toTimeAsLong(reply.data());
22232223
}
22242224

2225+
/*
2226+
* (non-Javadoc)
2227+
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
2228+
*/
2229+
@Override
2230+
public String getClientName() {
2231+
2232+
try {
2233+
if (isPipelined()) {
2234+
pipeline(new SrpGenericResult(pipeline.client_getname(), SrpConverters.replyToString()));
2235+
return null;
2236+
}
2237+
return SrpConverters.toString(client.client_getname());
2238+
} catch (Exception ex) {
2239+
throw convertSrpAccessException(ex);
2240+
}
2241+
}
2242+
22252243
private List<Object> closeTransaction() {
22262244
List<Object> results = Collections.emptyList();
22272245
if (txTracker != null) {

src/main/java/org/springframework/data/redis/connection/srp/SrpConverters.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
3636
import org.springframework.data.redis.connection.convert.Converters;
3737
import org.springframework.util.Assert;
38-
import org.springframework.util.NumberUtils;
3938

4039
import redis.client.RedisException;
4140
import redis.reply.IntegerReply;
@@ -61,6 +60,7 @@ abstract public class SrpConverters extends Converters {
6160
private static final Converter<Reply[], Map<byte[], byte[]>> REPLIES_TO_BYTES_MAP;
6261
private static final Converter<Reply[], List<Boolean>> REPLIES_TO_BOOLEAN_LIST;
6362
private static final Converter<Reply[], List<String>> REPLIES_TO_STRING_LIST;
63+
private static final Converter<Reply, String> REPLY_TO_STRING;
6464
private static final Converter<byte[], Properties> BYTES_TO_PROPERTIES;
6565
private static final Converter<byte[], String> BYTES_TO_STRING;
6666
private static final Converter<byte[], Double> BYTES_TO_DOUBLE;
@@ -171,6 +171,16 @@ public List<String> convert(Reply[] source) {
171171
return results;
172172
}
173173
};
174+
REPLY_TO_STRING = new Converter<Reply, String>() {
175+
176+
@Override
177+
public String convert(Reply source) {
178+
if (source == null) {
179+
return null;
180+
}
181+
return SrpConverters.toString((byte[]) source.data());
182+
}
183+
};
174184
}
175185

176186
public static Converter<Reply[], List<byte[]>> repliesToBytesList() {
@@ -201,6 +211,10 @@ public static Converter<byte[], String> bytesToString() {
201211
return BYTES_TO_STRING;
202212
}
203213

214+
public static Converter<Reply, String> replyToString() {
215+
return REPLY_TO_STRING;
216+
}
217+
204218
public static Converter<Reply[], List<Boolean>> repliesToBooleanList() {
205219
return REPLIES_TO_BOOLEAN_LIST;
206220
}
@@ -237,6 +251,10 @@ public static Map<byte[], byte[]> toBytesMap(Reply[] source) {
237251
return REPLIES_TO_BYTES_MAP.convert(source);
238252
}
239253

254+
public static String toString(Reply source) {
255+
return REPLY_TO_STRING.convert(source);
256+
}
257+
240258
public static String toString(byte[] source) {
241259
return BYTES_TO_STRING.convert(source);
242260
}
@@ -293,4 +311,5 @@ public static byte[] toBytes(Position source) {
293311
public static List<String> toStringList(String source) {
294312
return Collections.singletonList(source);
295313
}
314+
296315
}

src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,16 @@ public void testShutdownInDelegatedCorrectlyToNativeConnection() {
17101710
verify(nativeConnection, times(1)).shutdown(eq(ShutdownOption.NOSAVE));
17111711
}
17121712

1713+
/**
1714+
* @see DATAREDIS-270
1715+
*/
1716+
@Test
1717+
public void testGetClientNameIsDelegatedCorrectlyToNativeConnection() {
1718+
1719+
actual.add(connection.getClientName());
1720+
verify(nativeConnection, times(1)).getClientName();
1721+
}
1722+
17131723
protected List<Object> getResults() {
17141724
return actual;
17151725
}

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTestSuite.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public void shutdownSaveShouldBeSentCorrectlyUsingLuaScript() {
9090
assertThat(captor.getValue(), equalTo("return redis.call('SHUTDOWN','SAVE')".getBytes()));
9191
}
9292

93+
/**
94+
* @see DATAREDIS-270
95+
*/
96+
@Test
97+
public void getClientNameShouldSendRequestCorrectly() {
98+
99+
connection.getClientName();
100+
verifyNativeConnectionInvocation().clientGetname();
101+
}
102+
93103
}
94104

95105
public static class JedisConnectionPipelineUnitTests extends JedisConnectionUnitTests {
@@ -118,6 +128,15 @@ public void shutdownSaveShouldBeSentCorrectlyUsingLuaScript() {
118128
super.shutdownSaveShouldBeSentCorrectlyUsingLuaScript();
119129
}
120130

131+
/**
132+
* @see DATAREDIS-270
133+
*/
134+
@Override
135+
@Test(expected = UnsupportedOperationException.class)
136+
public void getClientNameShouldSendRequestCorrectly() {
137+
super.getClientNameShouldSendRequestCorrectly();
138+
}
139+
121140
}
122141

123142
/**

src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionUnitTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ public void shutdownNosaveShouldThrowUnsupportedOperationException() {
5656
public void shutdownWithNullShouldThrowUnsupportedOperationException() {
5757
connection.shutdown(null);
5858
}
59+
60+
/**
61+
* @see DATAREDIS-270
62+
*/
63+
@Test(expected = UnsupportedOperationException.class)
64+
public void getClientNameShouldSendRequestCorrectly() {
65+
connection.getClientName();
66+
}
67+
5968
}

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTestSuite.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public void shutdownWithSaveOptionIsCalledCorrectly() {
8282
verifyNativeConnectionInvocation().shutdown(true);
8383
}
8484

85+
/**
86+
* @see DATAREDIS-270
87+
*/
88+
@Test
89+
public void getClientNameShouldSendRequestCorrectly() {
90+
91+
connection.getClientName();
92+
verifyNativeConnectionInvocation().clientGetname();
93+
}
94+
8595
}
8696

8797
public static class LettucePipelineConnectionUnitTests extends LettuceConnectionUnitTests {

src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionUnitTestSuite.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public void shutdownWithNosaveIsCalledCorrectly() {
7777
verifyNativeConnectionInvocation().shutdown("NOSAVE".getBytes(Charsets.UTF_8), null);
7878
}
7979

80+
/**
81+
* @see DATAREDIS-270
82+
*/
83+
@Test
84+
public void getClientNameShouldSendRequestCorrectly() {
85+
86+
connection.getClientName();
87+
verifyNativeConnectionInvocation().client_getname();
88+
}
89+
8090
}
8191

8292
public static class SrpConnectionPiplineUnitTests extends AbstractConnectionUnitTestBase<Pipeline> {
@@ -123,6 +133,16 @@ public void shutdownWithNosaveIsCalledCorrectly() {
123133
verifyNativeConnectionInvocation().shutdown("NOSAVE".getBytes(Charsets.UTF_8), null);
124134
}
125135

136+
/**
137+
* @see DATAREDIS-270
138+
*/
139+
@Test
140+
public void getClientNameShouldSendRequestCorrectly() {
141+
142+
connection.getClientName();
143+
verifyNativeConnectionInvocation().client_getname();
144+
}
145+
126146
}
127147

128148
}

0 commit comments

Comments
 (0)