Skip to content

Commit f614899

Browse files
committed
Avoid recompiling pattern in Connection URI parsers
Applies to DB2, MySQL, PostgreSQL and MSSQL. The cost of compiling regular expression (in particular complex ones) can be avoided. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent c376b57 commit f614899

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

vertx-db2-client/src/main/java/io/vertx/db2client/impl/DB2ConnectionUriParser.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package io.vertx.db2client.impl;
1717

18-
import static java.lang.Integer.parseInt;
19-
import static java.lang.String.format;
18+
import io.vertx.core.json.JsonObject;
2019

2120
import java.io.UnsupportedEncodingException;
2221
import java.net.URLDecoder;
@@ -25,7 +24,8 @@
2524
import java.util.regex.Matcher;
2625
import java.util.regex.Pattern;
2726

28-
import io.vertx.core.json.JsonObject;
27+
import static java.lang.Integer.parseInt;
28+
import static java.lang.String.format;
2929

3030
/**
3131
* This is a parser for parsing connection URIs of DB2.
@@ -43,9 +43,9 @@ public class DB2ConnectionUriParser {
4343
private static final String DATABASE_REGEX = "(/(?<database>[a-zA-Z0-9\\-._~%!*]+))?"; // database name
4444
private static final String ATTRIBUTES_REGEX = "(\\?(?<attributes>.*))?"; // attributes
4545

46-
private static final String FULL_URI_REGEX = "^" // regex start
47-
+ SCHEME_DESIGNATOR_REGEX + USER_INFO_REGEX + NET_LOCATION_REGEX + PORT_REGEX + DATABASE_REGEX + ATTRIBUTES_REGEX
48-
+ "$"; // regex end
46+
private static final Pattern FULL_URI_REGEX = Pattern.compile("^" // regex start
47+
+ SCHEME_DESIGNATOR_REGEX + USER_INFO_REGEX + NET_LOCATION_REGEX + PORT_REGEX + DATABASE_REGEX + ATTRIBUTES_REGEX
48+
+ "$"); // regex end
4949

5050
public static JsonObject parse(String connectionUri) {
5151
// if we get any exception during the parsing, then we throw an IllegalArgumentException.
@@ -60,8 +60,7 @@ public static JsonObject parse(String connectionUri) {
6060

6161
// execute the parsing process and store options in the configuration
6262
private static void doParse(String connectionUri, JsonObject configuration) {
63-
Pattern pattern = Pattern.compile(FULL_URI_REGEX);
64-
Matcher matcher = pattern.matcher(connectionUri);
63+
Matcher matcher = FULL_URI_REGEX.matcher(connectionUri);
6564

6665
if (matcher.matches()) {
6766
// parse the user and password

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/MSSQLConnectionUriParser.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -35,14 +35,14 @@ public class MSSQLConnectionUriParser {
3535
private static final String DATABASE_REGEX = "(/(?<database>[a-zA-Z0-9\\-._~%!*]+))?"; // database name
3636
private static final String ATTRIBUTES_REGEX = "(\\?(?<attributes>.*))?"; // attributes
3737

38-
private static final String FULL_URI_REGEX = "^" // regex start
38+
private static final Pattern FULL_URI_REGEX = Pattern.compile("^" // regex start
3939
+ SCHEME_DESIGNATOR_REGEX
4040
+ USER_INFO_REGEX
4141
+ NET_LOCATION_REGEX
4242
+ PORT_REGEX
4343
+ DATABASE_REGEX
4444
+ ATTRIBUTES_REGEX
45-
+ "$"; // regex end
45+
+ "$"); // regex end
4646

4747
public static JsonObject parse(String connectionUri) {
4848
// if we get any exception during the parsing, then we throw an IllegalArgumentException.
@@ -57,8 +57,7 @@ public static JsonObject parse(String connectionUri) {
5757

5858
// execute the parsing process and store options in the configuration
5959
private static void doParse(String connectionUri, JsonObject configuration) {
60-
Pattern pattern = Pattern.compile(FULL_URI_REGEX);
61-
Matcher matcher = pattern.matcher(connectionUri);
60+
Matcher matcher = FULL_URI_REGEX.matcher(connectionUri);
6261

6362
if (matcher.matches()) {
6463
// parse the user and password

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLConnectionUriParser.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -35,14 +35,14 @@ public class MySQLConnectionUriParser {
3535
private static final String SCHEMA_REGEX = "(/(?<schema>[a-zA-Z0-9\\-._~%!*]+))?"; // schema name
3636
private static final String ATTRIBUTES_REGEX = "(\\?(?<attributes>.*))?"; // attributes
3737

38-
private static final String FULL_URI_REGEX = "^" // regex start
38+
private static final Pattern FULL_URI_REGEX = Pattern.compile("^" // regex start
3939
+ SCHEME_DESIGNATOR_REGEX
4040
+ USER_INFO_REGEX
4141
+ NET_LOCATION_REGEX
4242
+ PORT_REGEX
4343
+ SCHEMA_REGEX
4444
+ ATTRIBUTES_REGEX
45-
+ "$"; // regex end
45+
+ "$"); // regex end
4646

4747
public static JsonObject parse(String connectionUri) {
4848
// if we get any exception during the parsing, then we throw an IllegalArgumentException.
@@ -57,8 +57,7 @@ public static JsonObject parse(String connectionUri) {
5757

5858
// execute the parsing process and store options in the configuration
5959
private static void doParse(String connectionUri, JsonObject configuration) {
60-
Pattern pattern = Pattern.compile(FULL_URI_REGEX);
61-
Matcher matcher = pattern.matcher(connectionUri);
60+
Matcher matcher = FULL_URI_REGEX.matcher(connectionUri);
6261

6362
if (matcher.matches()) {
6463
// parse the user and password

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionUriParser.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
package io.vertx.pgclient.impl;
1818

19-
import io.vertx.pgclient.SslMode;
2019
import io.vertx.core.json.JsonObject;
20+
import io.vertx.pgclient.SslMode;
2121

2222
import java.io.UnsupportedEncodingException;
2323
import java.net.URLDecoder;
@@ -43,14 +43,14 @@ public class PgConnectionUriParser {
4343
private static final String DATABASE_REGEX = "(/(?<database>[a-zA-Z0-9\\-._~%!*]+))?"; // database name
4444
private static final String PARAMS_REGEX = "(\\?(?<params>.*))?"; // parameters
4545

46-
private static final String FULL_URI_REGEX = "^" // regex start
46+
private static final Pattern FULL_URI_REGEX = Pattern.compile("^" // regex start
4747
+ SCHEME_DESIGNATOR_REGEX
4848
+ USER_INFO_REGEX
4949
+ NET_LOCATION_REGEX
5050
+ PORT_REGEX
5151
+ DATABASE_REGEX
5252
+ PARAMS_REGEX
53-
+ "$"; // regex end
53+
+ "$"); // regex end
5454

5555
public static JsonObject parse(String connectionUri) {
5656
// if we get any exception during the parsing, then we throw an IllegalArgumentException.
@@ -65,8 +65,7 @@ public static JsonObject parse(String connectionUri) {
6565

6666
// execute the parsing process and store options in the configuration
6767
private static void doParse(String connectionUri, JsonObject configuration) {
68-
Pattern pattern = Pattern.compile(FULL_URI_REGEX);
69-
Matcher matcher = pattern.matcher(connectionUri);
68+
Matcher matcher = FULL_URI_REGEX.matcher(connectionUri);
7069

7170
if (matcher.matches()) {
7271
// parse the user and password

0 commit comments

Comments
 (0)