[refactor] cache compiled regex pattern (#13208)

* [refactor] cache compiled regex pattern

* [refactor] cache compiled regex pattern

* [refactor] cache compiled regex pattern
This commit is contained in:
youzipi 2022-12-18 11:24:11 +08:00 committed by GitHub
parent f233023b74
commit 042ec74a24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 13 deletions

View File

@ -26,7 +26,7 @@ import java.util.regex.Pattern;
*/
public class RegexUtils {
private static final String LINUX_USERNAME_PATTERN = "^[a-zA-Z0-9_].{0,30}";
private static final Pattern LINUX_USERNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_].{0,30}");
private RegexUtils() {
}
@ -37,8 +37,7 @@ public class RegexUtils {
* @return boolean
*/
public static boolean isValidLinuxUserName(String str) {
Pattern pattern = Pattern.compile(LINUX_USERNAME_PATTERN);
return pattern.matcher(str).matches();
return LINUX_USERNAME_PATTERN.matcher(str).matches();
}
public static String escapeNRT(String str) {

View File

@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.plugin.task.api;
import java.time.Duration;
import java.util.Set;
import java.util.regex.Pattern;
import com.google.common.collect.Sets;
@ -473,7 +474,7 @@ public class TaskConstants {
public static final int LOG_LINES = 500;
public static final String NAMESPACE_NAME = "name";
public static final String CLUSTER = "cluster";
public static final String COMMAND_SPLIT_REGEX = "[^\\s\"'`]+|\"([^\"]+)\"|'([^']+)'|`([^`]+)`";
public static final Pattern COMMAND_SPLIT_REGEX = Pattern.compile("[^\\s\"'`]+|\"([^\"]+)\"|'([^']+)'|`([^`]+)`");
/**
* conda config used by jupyter task plugin

View File

@ -55,7 +55,6 @@ import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
@ -116,7 +115,7 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
List<String> commands = new ArrayList<>();
if (commandString != null) {
Matcher commandMatcher = Pattern.compile(COMMAND_SPLIT_REGEX).matcher(commandString.trim());
Matcher commandMatcher = COMMAND_SPLIT_REGEX.matcher(commandString.trim());
while (commandMatcher.find()) {
commands.add(commandMatcher.group());
}

View File

@ -47,9 +47,9 @@ public class ParameterUtils {
private static final Logger logger = LoggerFactory.getLogger(ParameterUtils.class);
private static final String DATE_PARSE_PATTERN = "\\$\\[([^\\$\\]]+)]";
private static final Pattern DATE_PARSE_PATTERN = Pattern.compile("\\$\\[([^\\$\\]]+)]");
private static final String DATE_START_PATTERN = "^[0-9]";
private static final Pattern DATE_START_PATTERN = Pattern.compile("^[0-9]");
private static final char PARAM_REPLACE_CHAR = '?';
@ -253,15 +253,14 @@ public class ParameterUtils {
if (templateStr == null) {
return null;
}
Pattern pattern = Pattern.compile(DATE_PARSE_PATTERN);
StringBuffer newValue = new StringBuffer(templateStr.length());
Matcher matcher = pattern.matcher(templateStr);
Matcher matcher = DATE_PARSE_PATTERN.matcher(templateStr);
while (matcher.find()) {
String key = matcher.group(1);
if (Pattern.matches(DATE_START_PATTERN, key)) {
if (DATE_START_PATTERN.matcher(key).matches()) {
continue;
}
String value = TimePlaceholderUtils.getPlaceHolderTime(key, date);

View File

@ -49,6 +49,7 @@ public class OpenmldbTask extends PythonTask {
*/
private static final String OPENMLDB_PYTHON = "python3";
private static final Pattern PYTHON_PATH_PATTERN = Pattern.compile("/bin/python[\\d.]*$");
public static final Pattern SQL_PATTERN = Pattern.compile("\\S");
/**
* constructor
@ -127,9 +128,8 @@ public class OpenmldbTask extends PythonTask {
// split sql to list
// skip the sql only has space characters
Pattern pattern = Pattern.compile("\\S");
for (String sql : rawSqlScript.split(";")) {
if (pattern.matcher(sql).find()) {
if (SQL_PATTERN.matcher(sql).find()) {
sql = sql.replaceAll("\\n", "\\\\n");
builder.append("con.execute(\"").append(sql).append("\")\n");
}