Skip to content

Commit

Permalink
[Optimization-2579][core] Optimizate JobManager (#2580)
Browse files Browse the repository at this point in the history
* [Optimization-2579][core] Optimizate JobManager

* optimize return

---------

Co-authored-by: wenmo <[email protected]>
  • Loading branch information
aiwenmo and aiwenmo authored Nov 29, 2023
1 parent e32555e commit e38c814
Show file tree
Hide file tree
Showing 14 changed files with 809 additions and 437 deletions.
12 changes: 12 additions & 0 deletions dinky-common/src/main/java/org/dinky/utils/SqlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ public static String replaceAllParam(String sql, Map<String, String> values) {
}
return sql;
}

public static String addLineNumber(String input) {
String[] lines = input.split("\n");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < lines.length; i++) {
sb.append(String.format("%-4d", i + 1));
sb.append(" ");
sb.append(lines[i]);
sb.append("\n");
}
return sb.toString();
}
}
10 changes: 10 additions & 0 deletions dinky-common/src/main/java/org/dinky/utils/URLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.dinky.utils;

import org.dinky.assertion.Asserts;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -93,4 +95,12 @@ public static URL[] getURLs(Set<File> files) {
public static String toString(URL[] urls) {
return Arrays.stream(urls).map(URL::toString).collect(Collectors.joining(","));
}

public static String formatAddress(String webURL) {
if (Asserts.isNotNullString(webURL)) {
return webURL.replaceAll("http://", "");
} else {
return "";
}
}
}
37 changes: 37 additions & 0 deletions dinky-core/src/main/java/org/dinky/executor/ExecutorContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.executor;

public class ExecutorContext {

private static final ThreadLocal<Executor> EXECUTOR = new ThreadLocal<>();

public static void setExecutor(Executor executor) {
EXECUTOR.set(executor);
}

public static Executor getExecutor() {
return EXECUTOR.get();
}

public static void clear() {
EXECUTOR.remove();
}
}
9 changes: 8 additions & 1 deletion dinky-core/src/main/java/org/dinky/explainer/Explainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.dinky.job.JobManager;
import org.dinky.job.JobParam;
import org.dinky.job.StatementParam;
import org.dinky.job.builder.JobUDFBuilder;
import org.dinky.parser.SqlType;
import org.dinky.trans.Operations;
import org.dinky.trans.parse.AddJarSqlParseStrategy;
Expand Down Expand Up @@ -99,7 +100,13 @@ public static Explainer build(Executor executor, boolean useStatementSet, String
public Explainer initialize(JobManager jobManager, JobConfig config, String statement) {
DinkyClassLoaderUtil.initClassLoader(config);
String[] statements = SqlUtil.getStatements(SqlUtil.removeNote(statement), sqlSeparator);
jobManager.initUDF(parseUDFFromStatements(statements));
List<UDF> udfs = parseUDFFromStatements(statements);
jobManager.setJobParam(new JobParam(udfs));
try {
JobUDFBuilder.build(jobManager).run();
} catch (Exception e) {
e.printStackTrace();
}
return this;
}

Expand Down
50 changes: 50 additions & 0 deletions dinky-core/src/main/java/org/dinky/job/JobBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.job;

import org.dinky.executor.Executor;
import org.dinky.gateway.enums.GatewayType;

public abstract class JobBuilder {

protected JobManager jobManager;
protected JobConfig config;
protected JobParam jobParam;
protected GatewayType runMode;
protected Executor executor;
protected boolean useStatementSet;
protected boolean useGateway;
protected String sqlSeparator;
protected Job job;

public JobBuilder(JobManager jobManager) {
this.jobManager = jobManager;
this.config = jobManager.getConfig();
this.jobParam = jobManager.getJobParam();
this.runMode = jobManager.getRunMode();
this.executor = jobManager.getExecutor();
this.useStatementSet = jobManager.isUseStatementSet();
this.useGateway = jobManager.isUseGateway();
this.sqlSeparator = jobManager.getSqlSeparator();
this.job = JobContextHolder.getJob();
}

public abstract void run() throws Exception;
}
Loading

0 comments on commit e38c814

Please sign in to comment.