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;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* StudioExecuteDTO
......@@ -40,6 +41,7 @@ import lombok.Setter;
*/
@Getter
@Setter
@Slf4j
public class StudioExecuteDTO extends AbstractStatementDTO {
// RUN_MODE
private String type;
......@@ -67,10 +69,9 @@ public class StudioExecuteDTO extends AbstractStatementDTO {
public JobConfig getJobConfig() {
Map<String, String> config = new HashMap<>();
JsonNode paras = null;
if (Asserts.isNotNullString(configJson)) {
try {
paras = mapper.readTree(configJson);
JsonNode paras = mapper.readTree(configJson);
paras.forEach((JsonNode node) -> {
if (!node.isNull()) {
config.put(node.get("key").asText(), node.get("value").asText());
......@@ -78,7 +79,7 @@ public class StudioExecuteDTO extends AbstractStatementDTO {
}
);
} catch (JsonProcessingException e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
return new JobConfig(
......
......@@ -171,9 +171,8 @@ public class DataBaseServiceImpl extends SuperServiceImpl<DataBaseMapper, DataBa
@Override
public List<String> listEnabledFlinkWith() {
List<DataBase> dataBases = listEnabledAll();
List<String> list = new ArrayList<>();
for (DataBase dataBase : dataBases) {
for (DataBase dataBase : listEnabledAll()) {
if (Asserts.isNotNullString(dataBase.getFlinkConfig())) {
list.add(dataBase.getName() + ":=" + dataBase.getFlinkConfig() + "\n;\n");
}
......
......@@ -48,11 +48,10 @@ public class FragmentVariableServiceImpl extends SuperServiceImpl<FragmentVariab
@Override
public Map<String, String> listEnabledVariables() {
List<FragmentVariable> fragmentVariables = listEnabledAll();
Map<String, String> variables = new LinkedHashMap<>();
for (FragmentVariable fragmentVariable : fragmentVariables) {
for (FragmentVariable fragmentVariable : listEnabledAll()) {
variables.put(fragmentVariable.getName(), fragmentVariable.getFragmentValue());
}
return variables;
}
}
\ No newline at end of file
}
......@@ -21,8 +21,10 @@ package com.dlink.assertion;
import com.dlink.exception.RunTimeException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
/**
* Asserts
......@@ -44,17 +46,11 @@ public class Asserts {
}
public static boolean isNullString(String str) {
return isNull(str) || "".equals(str);
return isNull(str) || str.isEmpty();
}
public static boolean isAllNullString(String... str) {
boolean isNull = true;
for (String item : str) {
if (isNotNullString(item)) {
isNull = false;
}
}
return isNull;
return Arrays.stream(str).allMatch(Asserts::isNullString);
}
public static boolean isNotNullString(String str) {
......@@ -62,33 +58,15 @@ public class Asserts {
}
public static boolean isAllNotNullString(String... str) {
boolean isNotNull = true;
for (String item : str) {
if (isNullString(item)) {
isNotNull = false;
}
}
return isNotNull;
return Arrays.stream(str).noneMatch(Asserts::isNullString);
}
public static boolean isEquals(String str1, String str2) {
if (isNull(str1) && isNull(str2)) {
return true;
} else if (isNull(str1) || isNull(str2)) {
return false;
} else {
return str1.equals(str2);
}
return Objects.equals(str1, str2);
}
public static boolean isEqualsIgnoreCase(String str1, String str2) {
if (isNull(str1) && isNull(str2)) {
return true;
} else if (isNull(str1) || isNull(str2)) {
return false;
} else {
return str1.equalsIgnoreCase(str2);
}
return (str1 == null && str2 == null) || (str1 != null && str1.equalsIgnoreCase(str2));
}
public static boolean isNullCollection(Collection<?> collection) {
......@@ -100,7 +78,7 @@ public class Asserts {
}
public static boolean isNullMap(Map<?, ?> map) {
return isNull(map) || map.size() == 0;
return isNull(map) || map.isEmpty();
}
public static boolean isNotNullMap(Map<?, ?> map) {
......
......@@ -66,4 +66,12 @@ public class JobResult {
this.startTime = startTime;
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;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
......@@ -69,6 +70,9 @@ import groovy.lang.GroovyClassLoader;
*/
public class UDFUtil {
private UDFUtil() {
}
protected static final Logger log = LoggerFactory.getLogger(UDFUtil.class);
/**
* 存放 udf md5与版本对应的k,v值
......@@ -130,7 +134,7 @@ public class UDFUtil {
}).collect(Collectors.toList());
List<String> classNameList = udfList.stream().map(UDF::getClassName).collect(Collectors.toList());
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;
}
......
......@@ -41,7 +41,7 @@ import java.util.Set;
* @author wenmo
* @since 2021/7/19 23:15
*/
public interface Driver {
public interface Driver extends AutoCloseable {
static Optional<Driver> get(DriverConfig config) {
Asserts.checkNotNull(config, "数据源配置不能为空");
......
......@@ -27,7 +27,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.text.CharSequenceUtil;
/**
* Process
......@@ -121,7 +121,7 @@ public class ProcessEntity {
if (isNullProcess()) {
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);
ConsolePool.write(message, userId);
}
......@@ -130,7 +130,7 @@ public class ProcessEntity {
if (isNullProcess()) {
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);
ConsolePool.write(message, userId);
}
......@@ -155,7 +155,7 @@ public class ProcessEntity {
if (isNullProcess()) {
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).appendError(message);
ConsolePool.write(message, userId);
......
......@@ -41,11 +41,10 @@ public class ProcessStep {
}
public ProcessStep(ProcessStatus stepStatus, LocalDateTime startTime) {
this.stepStatus = stepStatus;
this.startTime = startTime;
this(stepStatus, startTime, null, 0, null, null);
}
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) {
this.stepStatus = stepStatus;
this.startTime = startTime;
......
......@@ -32,9 +32,9 @@ import java.util.concurrent.ConcurrentHashMap;
*/
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() {
return instance;
......@@ -51,13 +51,8 @@ public class ConsolePool extends AbstractPool<StringBuilder> {
}
public static void write(String str, Integer userId) {
String user = userId.toString();
if (consoleEntityMap.containsKey(user)) {
consoleEntityMap.get(user).append(str);
} else {
StringBuilder sb = new StringBuilder("Dinky User Console:");
consoleEntityMap.put(user, sb.append(str));
}
String user = String.valueOf(userId);
consoleEntityMap.getOrDefault(user, new StringBuilder("Dinky User Console:")).append(str);
}
}
......@@ -33,9 +33,9 @@ import java.util.concurrent.ConcurrentHashMap;
*/
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() {
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