Skip to content

Commit 042ec74

Browse files
authored
[refactor] cache compiled regex pattern (#13208)
* [refactor] cache compiled regex pattern * [refactor] cache compiled regex pattern * [refactor] cache compiled regex pattern
1 parent f233023 commit 042ec74

5 files changed

Lines changed: 11 additions & 13 deletions

File tree

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/RegexUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
public class RegexUtils {
2828

29-
private static final String LINUX_USERNAME_PATTERN = "^[a-zA-Z0-9_].{0,30}";
29+
private static final Pattern LINUX_USERNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_].{0,30}");
3030

3131
private RegexUtils() {
3232
}
@@ -37,8 +37,7 @@ private RegexUtils() {
3737
* @return boolean
3838
*/
3939
public static boolean isValidLinuxUserName(String str) {
40-
Pattern pattern = Pattern.compile(LINUX_USERNAME_PATTERN);
41-
return pattern.matcher(str).matches();
40+
return LINUX_USERNAME_PATTERN.matcher(str).matches();
4241
}
4342

4443
public static String escapeNRT(String str) {

dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/TaskConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.time.Duration;
2121
import java.util.Set;
22+
import java.util.regex.Pattern;
2223

2324
import com.google.common.collect.Sets;
2425

@@ -473,7 +474,7 @@ private TaskConstants() {
473474
public static final int LOG_LINES = 500;
474475
public static final String NAMESPACE_NAME = "name";
475476
public static final String CLUSTER = "cluster";
476-
public static final String COMMAND_SPLIT_REGEX = "[^\\s\"'`]+|\"([^\"]+)\"|'([^']+)'|`([^`]+)`";
477+
public static final Pattern COMMAND_SPLIT_REGEX = Pattern.compile("[^\\s\"'`]+|\"([^\"]+)\"|'([^']+)'|`([^`]+)`");
477478

478479
/**
479480
* conda config used by jupyter task plugin

dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/k8s/impl/K8sTaskExecutor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import java.util.concurrent.CountDownLatch;
5656
import java.util.concurrent.TimeUnit;
5757
import java.util.regex.Matcher;
58-
import java.util.regex.Pattern;
5958

6059
import org.slf4j.Logger;
6160

@@ -116,7 +115,7 @@ public Job buildK8sJob(K8sTaskMainParameters k8STaskMainParameters) {
116115
List<String> commands = new ArrayList<>();
117116

118117
if (commandString != null) {
119-
Matcher commandMatcher = Pattern.compile(COMMAND_SPLIT_REGEX).matcher(commandString.trim());
118+
Matcher commandMatcher = COMMAND_SPLIT_REGEX.matcher(commandString.trim());
120119
while (commandMatcher.find()) {
121120
commands.add(commandMatcher.group());
122121
}

dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/ParameterUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public class ParameterUtils {
4747

4848
private static final Logger logger = LoggerFactory.getLogger(ParameterUtils.class);
4949

50-
private static final String DATE_PARSE_PATTERN = "\\$\\[([^\\$\\]]+)]";
50+
private static final Pattern DATE_PARSE_PATTERN = Pattern.compile("\\$\\[([^\\$\\]]+)]");
5151

52-
private static final String DATE_START_PATTERN = "^[0-9]";
52+
private static final Pattern DATE_START_PATTERN = Pattern.compile("^[0-9]");
5353

5454
private static final char PARAM_REPLACE_CHAR = '?';
5555

@@ -253,15 +253,14 @@ private static String dateTemplateParse(String templateStr, Date date) {
253253
if (templateStr == null) {
254254
return null;
255255
}
256-
Pattern pattern = Pattern.compile(DATE_PARSE_PATTERN);
257256

258257
StringBuffer newValue = new StringBuffer(templateStr.length());
259258

260-
Matcher matcher = pattern.matcher(templateStr);
259+
Matcher matcher = DATE_PARSE_PATTERN.matcher(templateStr);
261260

262261
while (matcher.find()) {
263262
String key = matcher.group(1);
264-
if (Pattern.matches(DATE_START_PATTERN, key)) {
263+
if (DATE_START_PATTERN.matcher(key).matches()) {
265264
continue;
266265
}
267266
String value = TimePlaceholderUtils.getPlaceHolderTime(key, date);

dolphinscheduler-task-plugin/dolphinscheduler-task-openmldb/src/main/java/org/apache/dolphinscheduler/plugin/task/openmldb/OpenmldbTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class OpenmldbTask extends PythonTask {
4949
*/
5050
private static final String OPENMLDB_PYTHON = "python3";
5151
private static final Pattern PYTHON_PATH_PATTERN = Pattern.compile("/bin/python[\\d.]*$");
52+
public static final Pattern SQL_PATTERN = Pattern.compile("\\S");
5253

5354
/**
5455
* constructor
@@ -127,9 +128,8 @@ private String buildPythonScriptsFromSql(String rawSqlScript) {
127128

128129
// split sql to list
129130
// skip the sql only has space characters
130-
Pattern pattern = Pattern.compile("\\S");
131131
for (String sql : rawSqlScript.split(";")) {
132-
if (pattern.matcher(sql).find()) {
132+
if (SQL_PATTERN.matcher(sql).find()) {
133133
sql = sql.replaceAll("\\n", "\\\\n");
134134
builder.append("con.execute(\"").append(sql).append("\")\n");
135135
}

0 commit comments

Comments
 (0)