Skip to content

Commit

Permalink
Refactor JobCodeRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Nov 9, 2023
1 parent 16a75b8 commit dca28b3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
@Slf4j
public final class JobCodeRegistry {

private static final Map<String, String> JOB_CODE_AND_TYPE_MAP = new HashMap<>();
private static final Map<String, JobType> JOB_CODE_AND_TYPE_MAP = new HashMap<>();

static {
for (JobType each : ShardingSphereServiceLoader.getServiceInstances(JobType.class)) {
Preconditions.checkArgument(2 == each.getCode().length(), "Job type code length is not 2.");
JOB_CODE_AND_TYPE_MAP.put(each.getCode(), each.getType());
JOB_CODE_AND_TYPE_MAP.put(each.getCode(), each);
}
}

Expand All @@ -48,9 +48,8 @@ public final class JobCodeRegistry {
* @param jobTypeCode job type code
* @return job type
*/
public static String getJobType(final String jobTypeCode) {
String result = JOB_CODE_AND_TYPE_MAP.get(jobTypeCode);
Preconditions.checkNotNull(result, "Can not get job type by `%s`.", jobTypeCode);
return result;
public static JobType getJobType(final String jobTypeCode) {
Preconditions.checkArgument(JOB_CODE_AND_TYPE_MAP.containsKey(jobTypeCode), "Can not get job type by `%s`.", jobTypeCode);
return JOB_CODE_AND_TYPE_MAP.get(jobTypeCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.shardingsphere.data.pipeline.common.job.type.JobType;
import org.apache.shardingsphere.data.pipeline.common.util.InstanceTypeUtils;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -63,8 +62,7 @@ public static String marshalJobIdCommonPrefix(final PipelineJobId pipelineJobId)
*/
public static JobType parseJobType(final String jobId) {
verifyJobId(jobId);
String jobTypeCode = jobId.substring(1, 3);
return TypedSPILoader.getService(JobType.class, JobCodeRegistry.getJobType(jobTypeCode));
return JobCodeRegistry.getJobType(jobId.substring(1, 3));
}

private static void verifyJobId(final String jobId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
* limitations under the License.
*/

package org.apache.shardingsphere.test.it.data.pipeline.spi.job;
package org.apache.shardingsphere.data.pipeline.common.job.type;

import org.apache.shardingsphere.data.pipeline.common.job.type.JobCodeRegistry;
import org.apache.shardingsphere.data.pipeline.scenario.consistencycheck.ConsistencyCheckJobType;
import org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJobType;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -29,7 +26,6 @@ class JobCodeRegistryTest {

@Test
void assertGetJobType() {
assertThat(JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE), is("MIGRATION"));
assertThat(JobCodeRegistry.getJobType(ConsistencyCheckJobType.TYPE_CODE), is("CONSISTENCY_CHECK"));
assertThat(JobCodeRegistry.getJobType("00").getType(), is("FIXTURE"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.shardingsphere.data.pipeline.common.job.type.FixtureJobType
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,6 @@ protected String getJobClassName() {

@Override
public JobType getJobType() {
return TypedSPILoader.getService(JobType.class, JobCodeRegistry.getJobType(ConsistencyCheckJobType.TYPE_CODE));
return JobCodeRegistry.getJobType(ConsistencyCheckJobType.TYPE_CODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public void refreshTableMetadata(final String jobId, final String databaseName)

@Override
public JobType getJobType() {
return TypedSPILoader.getService(JobType.class, JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE));
return JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE);
}

@Override
Expand Down

0 comments on commit dca28b3

Please sign in to comment.