Unverified Commit 0459b324 authored by Licho's avatar Licho Committed by GitHub

refactor: improve code readability. eliminate hide problem, wait for 0.7 (#1155)

* [WIP]refactor: improve code readability.

* refactor: constructor invoke constructor

* refactor: Asserts.java simple and format.

* refactor: simple code

* refactor: simple code

* chore: reformat code

* chore: remove unused import

* feat: improve variable concurrency strict

* feat: encapsulate start end time set function.

* feat: add AutoClosable to Driver interface.

* refactor: remove double bracket init, it is hide danger.
parent 30fad324
...@@ -31,6 +31,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -31,6 +31,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/** /**
* StudioExecuteDTO * StudioExecuteDTO
...@@ -40,6 +41,7 @@ import lombok.Setter; ...@@ -40,6 +41,7 @@ import lombok.Setter;
*/ */
@Getter @Getter
@Setter @Setter
@Slf4j
public class StudioExecuteDTO extends AbstractStatementDTO { public class StudioExecuteDTO extends AbstractStatementDTO {
// RUN_MODE // RUN_MODE
private String type; private String type;
...@@ -67,10 +69,9 @@ public class StudioExecuteDTO extends AbstractStatementDTO { ...@@ -67,10 +69,9 @@ public class StudioExecuteDTO extends AbstractStatementDTO {
public JobConfig getJobConfig() { public JobConfig getJobConfig() {
Map<String, String> config = new HashMap<>(); Map<String, String> config = new HashMap<>();
JsonNode paras = null;
if (Asserts.isNotNullString(configJson)) { if (Asserts.isNotNullString(configJson)) {
try { try {
paras = mapper.readTree(configJson); JsonNode paras = mapper.readTree(configJson);
paras.forEach((JsonNode node) -> { paras.forEach((JsonNode node) -> {
if (!node.isNull()) { if (!node.isNull()) {
config.put(node.get("key").asText(), node.get("value").asText()); config.put(node.get("key").asText(), node.get("value").asText());
...@@ -78,7 +79,7 @@ public class StudioExecuteDTO extends AbstractStatementDTO { ...@@ -78,7 +79,7 @@ public class StudioExecuteDTO extends AbstractStatementDTO {
} }
); );
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); log.error(e.getMessage());
} }
} }
return new JobConfig( return new JobConfig(
......
...@@ -171,9 +171,8 @@ public class DataBaseServiceImpl extends SuperServiceImpl<DataBaseMapper, DataBa ...@@ -171,9 +171,8 @@ public class DataBaseServiceImpl extends SuperServiceImpl<DataBaseMapper, DataBa
@Override @Override
public List<String> listEnabledFlinkWith() { public List<String> listEnabledFlinkWith() {
List<DataBase> dataBases = listEnabledAll();
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (DataBase dataBase : dataBases) { for (DataBase dataBase : listEnabledAll()) {
if (Asserts.isNotNullString(dataBase.getFlinkConfig())) { if (Asserts.isNotNullString(dataBase.getFlinkConfig())) {
list.add(dataBase.getName() + ":=" + dataBase.getFlinkConfig() + "\n;\n"); list.add(dataBase.getName() + ":=" + dataBase.getFlinkConfig() + "\n;\n");
} }
......
...@@ -48,11 +48,10 @@ public class FragmentVariableServiceImpl extends SuperServiceImpl<FragmentVariab ...@@ -48,11 +48,10 @@ public class FragmentVariableServiceImpl extends SuperServiceImpl<FragmentVariab
@Override @Override
public Map<String, String> listEnabledVariables() { public Map<String, String> listEnabledVariables() {
List<FragmentVariable> fragmentVariables = listEnabledAll();
Map<String, String> variables = new LinkedHashMap<>(); Map<String, String> variables = new LinkedHashMap<>();
for (FragmentVariable fragmentVariable : fragmentVariables) { for (FragmentVariable fragmentVariable : listEnabledAll()) {
variables.put(fragmentVariable.getName(), fragmentVariable.getFragmentValue()); variables.put(fragmentVariable.getName(), fragmentVariable.getFragmentValue());
} }
return variables; return variables;
} }
} }
\ No newline at end of file
...@@ -21,8 +21,10 @@ package com.dlink.assertion; ...@@ -21,8 +21,10 @@ package com.dlink.assertion;
import com.dlink.exception.RunTimeException; import com.dlink.exception.RunTimeException;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* Asserts * Asserts
...@@ -44,17 +46,11 @@ public class Asserts { ...@@ -44,17 +46,11 @@ public class Asserts {
} }
public static boolean isNullString(String str) { public static boolean isNullString(String str) {
return isNull(str) || "".equals(str); return isNull(str) || str.isEmpty();
} }
public static boolean isAllNullString(String... str) { public static boolean isAllNullString(String... str) {
boolean isNull = true; return Arrays.stream(str).allMatch(Asserts::isNullString);
for (String item : str) {
if (isNotNullString(item)) {
isNull = false;
}
}
return isNull;
} }
public static boolean isNotNullString(String str) { public static boolean isNotNullString(String str) {
...@@ -62,33 +58,15 @@ public class Asserts { ...@@ -62,33 +58,15 @@ public class Asserts {
} }
public static boolean isAllNotNullString(String... str) { public static boolean isAllNotNullString(String... str) {
boolean isNotNull = true; return Arrays.stream(str).noneMatch(Asserts::isNullString);
for (String item : str) {
if (isNullString(item)) {
isNotNull = false;
}
}
return isNotNull;
} }
public static boolean isEquals(String str1, String str2) { public static boolean isEquals(String str1, String str2) {
if (isNull(str1) && isNull(str2)) { return Objects.equals(str1, str2);
return true;
} else if (isNull(str1) || isNull(str2)) {
return false;
} else {
return str1.equals(str2);
}
} }
public static boolean isEqualsIgnoreCase(String str1, String str2) { public static boolean isEqualsIgnoreCase(String str1, String str2) {
if (isNull(str1) && isNull(str2)) { return (str1 == null && str2 == null) || (str1 != null && str1.equalsIgnoreCase(str2));
return true;
} else if (isNull(str1) || isNull(str2)) {
return false;
} else {
return str1.equalsIgnoreCase(str2);
}
} }
public static boolean isNullCollection(Collection<?> collection) { public static boolean isNullCollection(Collection<?> collection) {
...@@ -100,7 +78,7 @@ public class Asserts { ...@@ -100,7 +78,7 @@ public class Asserts {
} }
public static boolean isNullMap(Map<?, ?> map) { public static boolean isNullMap(Map<?, ?> map) {
return isNull(map) || map.size() == 0; return isNull(map) || map.isEmpty();
} }
public static boolean isNotNullMap(Map<?, ?> map) { public static boolean isNotNullMap(Map<?, ?> map) {
......
...@@ -66,4 +66,12 @@ public class JobResult { ...@@ -66,4 +66,12 @@ public class JobResult {
this.startTime = startTime; this.startTime = startTime;
this.endTime = endTime; this.endTime = endTime;
} }
public void setStartTimeNow() {
this.setStartTime(LocalDateTime.now());
}
public void setEndTimeNow() {
this.setEndTime(LocalDateTime.now());
}
} }
...@@ -53,6 +53,7 @@ import cn.hutool.core.io.FileUtil; ...@@ -53,6 +53,7 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict; import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.Opt; import cn.hutool.core.lang.Opt;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5; import cn.hutool.crypto.digest.MD5;
...@@ -69,6 +70,9 @@ import groovy.lang.GroovyClassLoader; ...@@ -69,6 +70,9 @@ import groovy.lang.GroovyClassLoader;
*/ */
public class UDFUtil { public class UDFUtil {
private UDFUtil() {
}
protected static final Logger log = LoggerFactory.getLogger(UDFUtil.class); protected static final Logger log = LoggerFactory.getLogger(UDFUtil.class);
/** /**
* 存放 udf md5与版本对应的k,v值 * 存放 udf md5与版本对应的k,v值
...@@ -130,7 +134,7 @@ public class UDFUtil { ...@@ -130,7 +134,7 @@ public class UDFUtil {
}).collect(Collectors.toList()); }).collect(Collectors.toList());
List<String> classNameList = udfList.stream().map(UDF::getClassName).collect(Collectors.toList()); List<String> classNameList = udfList.stream().map(UDF::getClassName).collect(Collectors.toList());
process.info(StringUtils.join(",", classNameList)); process.info(StringUtils.join(",", classNameList));
process.info(StrUtil.format("A total of {} UDF have been Parsed.", classNameList.size())); process.info(CharSequenceUtil.format("A total of {} UDF have been Parsed.", classNameList.size()));
return udfList; return udfList;
} }
......
...@@ -41,7 +41,7 @@ import java.util.Set; ...@@ -41,7 +41,7 @@ import java.util.Set;
* @author wenmo * @author wenmo
* @since 2021/7/19 23:15 * @since 2021/7/19 23:15
*/ */
public interface Driver { public interface Driver extends AutoCloseable {
static Optional<Driver> get(DriverConfig config) { static Optional<Driver> get(DriverConfig config) {
Asserts.checkNotNull(config, "数据源配置不能为空"); Asserts.checkNotNull(config, "数据源配置不能为空");
......
...@@ -27,7 +27,7 @@ import java.util.ArrayList; ...@@ -27,7 +27,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.text.CharSequenceUtil;
/** /**
* Process * Process
...@@ -121,7 +121,7 @@ public class ProcessEntity { ...@@ -121,7 +121,7 @@ public class ProcessEntity {
if (isNullProcess()) { if (isNullProcess()) {
return; return;
} }
String message = StrUtil.format("\n[{}] {} CONFIG: {}", type.getValue(), LocalDateTime.now(), str); String message = CharSequenceUtil.format("\n[{}] {} CONFIG: {}", type.getValue(), LocalDateTime.now(), str);
steps.get(stepIndex - 1).appendInfo(message); steps.get(stepIndex - 1).appendInfo(message);
ConsolePool.write(message, userId); ConsolePool.write(message, userId);
} }
...@@ -130,7 +130,7 @@ public class ProcessEntity { ...@@ -130,7 +130,7 @@ public class ProcessEntity {
if (isNullProcess()) { if (isNullProcess()) {
return; return;
} }
String message = StrUtil.format("\n[{}] {} INFO: {}", type.getValue(), LocalDateTime.now(), str); String message = CharSequenceUtil.format("\n[{}] {} INFO: {}", type.getValue(), LocalDateTime.now(), str);
steps.get(stepIndex - 1).appendInfo(message); steps.get(stepIndex - 1).appendInfo(message);
ConsolePool.write(message, userId); ConsolePool.write(message, userId);
} }
...@@ -155,7 +155,7 @@ public class ProcessEntity { ...@@ -155,7 +155,7 @@ public class ProcessEntity {
if (isNullProcess()) { if (isNullProcess()) {
return; return;
} }
String message = StrUtil.format("\n[{}] {} ERROR: {}", type.getValue(), LocalDateTime.now(), str); String message = CharSequenceUtil.format("\n[{}] {} ERROR: {}", type.getValue(), LocalDateTime.now(), str);
steps.get(stepIndex - 1).appendInfo(message); steps.get(stepIndex - 1).appendInfo(message);
steps.get(stepIndex - 1).appendError(message); steps.get(stepIndex - 1).appendError(message);
ConsolePool.write(message, userId); ConsolePool.write(message, userId);
......
...@@ -41,11 +41,10 @@ public class ProcessStep { ...@@ -41,11 +41,10 @@ public class ProcessStep {
} }
public ProcessStep(ProcessStatus stepStatus, LocalDateTime startTime) { public ProcessStep(ProcessStatus stepStatus, LocalDateTime startTime) {
this.stepStatus = stepStatus; this(stepStatus, startTime, null, 0, null, null);
this.startTime = startTime;
} }
public ProcessStep(int index, ProcessStatus stepStatus, LocalDateTime startTime, LocalDateTime endTime, long time, public ProcessStep(ProcessStatus stepStatus, LocalDateTime startTime, LocalDateTime endTime, long time,
StringBuilder info, StringBuilder error) { StringBuilder info, StringBuilder error) {
this.stepStatus = stepStatus; this.stepStatus = stepStatus;
this.startTime = startTime; this.startTime = startTime;
......
...@@ -32,9 +32,9 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -32,9 +32,9 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class ConsolePool extends AbstractPool<StringBuilder> { public class ConsolePool extends AbstractPool<StringBuilder> {
private static volatile Map<String, StringBuilder> consoleEntityMap = new ConcurrentHashMap<>(); private static final Map<String, StringBuilder> consoleEntityMap = new ConcurrentHashMap<>();
private static ConsolePool instance = new ConsolePool(); private static final ConsolePool instance = new ConsolePool();
public static ConsolePool getInstance() { public static ConsolePool getInstance() {
return instance; return instance;
...@@ -51,13 +51,8 @@ public class ConsolePool extends AbstractPool<StringBuilder> { ...@@ -51,13 +51,8 @@ public class ConsolePool extends AbstractPool<StringBuilder> {
} }
public static void write(String str, Integer userId) { public static void write(String str, Integer userId) {
String user = userId.toString(); String user = String.valueOf(userId);
if (consoleEntityMap.containsKey(user)) { consoleEntityMap.getOrDefault(user, new StringBuilder("Dinky User Console:")).append(str);
consoleEntityMap.get(user).append(str);
} else {
StringBuilder sb = new StringBuilder("Dinky User Console:");
consoleEntityMap.put(user, sb.append(str));
}
} }
} }
...@@ -33,9 +33,9 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -33,9 +33,9 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class ProcessPool extends AbstractPool<ProcessEntity> { public class ProcessPool extends AbstractPool<ProcessEntity> {
private static volatile Map<String, ProcessEntity> processEntityMap = new ConcurrentHashMap<>(); private static final Map<String, ProcessEntity> processEntityMap = new ConcurrentHashMap<>();
private static ProcessPool instance = new ProcessPool(); private static final ProcessPool instance = new ProcessPool();
public static ProcessPool getInstance() { public static ProcessPool getInstance() {
return instance; return instance;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment