Skip to content

Commit f281fac

Browse files
committed
Redis Cluster: extract parseHostAndPort
1 parent 6390f09 commit f281fac

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

driver/src/main/java/jdbc/client/helpers/query/parser/QueryParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ private static class NodeHintLine extends CommentLine implements HintLine {
244244
if (!accepts(tokens)) throw new AssertionError(String.format("Incorrect node hint tokens: %s.", tokens));
245245
HostAndPort hostAndPort = null;
246246
try {
247-
// TODO (cluster): improve parsing (URL too)
248-
hostAndPort = HostAndPort.from(tokens.get(3));
247+
hostAndPort = parseHostAndPort(tokens.get(3));
249248
} catch (Exception ignored) {
250249
}
251250
this.hostAndPort = hostAndPort;

driver/src/main/java/jdbc/client/impl/RedisJedisURIBase.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import redis.clients.jedis.HostAndPort;
77
import redis.clients.jedis.HostAndPortMapper;
88
import redis.clients.jedis.JedisClientConfig;
9-
import redis.clients.jedis.Protocol;
109
import redis.clients.jedis.exceptions.JedisConnectionException;
1110

1211
import javax.net.ssl.SSLContext;
@@ -202,19 +201,10 @@ protected boolean getBoolean(Map<String, String> parameters, Properties info, St
202201

203202

204203
private void setHostAndPortMapping(Properties info) {
205-
Map<HostAndPort, HostAndPort> mapping = getMap(info, HOST_AND_PORT_MAPPING, this::parseHostAndPort, this::parseHostAndPort);
204+
Map<HostAndPort, HostAndPort> mapping = getMap(info, HOST_AND_PORT_MAPPING, Utils::parseHostAndPort, Utils::parseHostAndPort);
206205
this.hostAndPortMapper = mapping == null ? null : new CompleteHostAndPortMapper(mapping);
207206
}
208207

209-
// TODO (cluster): improvements + tests
210-
@NotNull
211-
private HostAndPort parseHostAndPort(@NotNull String hostAndPortStr) {
212-
HostAndPort hostAndPort = HostAndPort.from(hostAndPortStr);
213-
if ("localhost".equalsIgnoreCase(hostAndPort.getHost()))
214-
return new HostAndPort(Protocol.DEFAULT_HOST, hostAndPort.getPort());
215-
return hostAndPort;
216-
}
217-
218208

219209
@Override
220210
public String getUser() {

driver/src/main/java/jdbc/utils/Utils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jetbrains.annotations.Contract;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
7+
import redis.clients.jedis.HostAndPort;
78
import redis.clients.jedis.Protocol;
89
import redis.clients.jedis.Protocol.Keyword;
910
import redis.clients.jedis.args.Rawable;
@@ -106,6 +107,17 @@ private static <T> T getObject(Map<?, ?> map, String name, T defaultValue, Funct
106107
}
107108

108109

110+
public static @NotNull HostAndPort parseHostAndPort(@NotNull String hostAndPortStr) {
111+
HostAndPort hostAndPort = HostAndPort.from(hostAndPortStr);
112+
String adjustedHost = hostAndPort.getHost().trim();
113+
if ("localhost".equalsIgnoreCase(adjustedHost)) {
114+
adjustedHost = Protocol.DEFAULT_HOST;
115+
}
116+
if (adjustedHost.equals(hostAndPort.getHost())) return hostAndPort;
117+
return new HostAndPort(adjustedHost, hostAndPort.getPort());
118+
}
119+
120+
109121
public static int parseSqlDbIndex(@Nullable String db) throws SQLException {
110122
if (db == null) throw new SQLException("Database should be specified.");
111123
try {

0 commit comments

Comments
 (0)