Skip to content

Commit

Permalink
Remove InstanceTypeUtils (#33278)
Browse files Browse the repository at this point in the history
* Remove InstanceTypeUtils

* Remove InstanceTypeUtils

* Remove InstanceTypeUtils
  • Loading branch information
terrymanu authored Oct 17, 2024
1 parent 4a6437e commit 59cc0a3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,35 @@

package org.apache.shardingsphere.infra.instance.metadata;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Instance type.
*/
@RequiredArgsConstructor
@Getter
public enum InstanceType {

JDBC, PROXY
JDBC('j'),

PROXY('p');

private final char code;

/**
* Value of instance type.
*
* @param code instance type code
* @return instance type
* @throws IllegalArgumentException unknown instance type code
*/
public static InstanceType valueOf(final char code) {
for (InstanceType each : values()) {
if (each.code == code) {
return each;
}
}
throw new IllegalArgumentException(String.format("Unknown instance type code: '%s'.", code));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@
* limitations under the License.
*/

package org.apache.shardingsphere.data.pipeline.core.util;
package org.apache.shardingsphere.infra.instance.metadata;

import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class InstanceTypeUtilsTest {
class InstanceTypeTest {

@Test
void assertEncodeAndDecode() {
for (InstanceType each : InstanceType.values()) {
assertThat(InstanceTypeUtils.decode(InstanceTypeUtils.encode(each)), is(each));
}
void assertValueOfValidCode() {
assertThat(InstanceType.valueOf('j'), is(InstanceType.JDBC));
assertThat(InstanceType.valueOf('p'), is(InstanceType.PROXY));
}

@Test
void assertValueOfInvalidCode() {
assertThrows(IllegalArgumentException.class, () -> InstanceType.valueOf('a'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.shardingsphere.data.pipeline.core.job.api.PipelineAPIFactory;
import org.apache.shardingsphere.data.pipeline.core.job.type.JobCodeRegistry;
import org.apache.shardingsphere.data.pipeline.core.job.type.PipelineJobType;
import org.apache.shardingsphere.data.pipeline.core.util.InstanceTypeUtils;
import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
Expand Down Expand Up @@ -57,8 +56,7 @@ private static String marshalPrefix(final PipelineJobType jobType, final Pipelin
String databaseName = instanceType == InstanceType.PROXY ? "" : contextKey.getDatabaseName();
String databaseNameHex = Hex.encodeHexString(databaseName.getBytes(StandardCharsets.UTF_8), true);
String databaseNameLengthHex = Hex.encodeHexString(Shorts.toByteArray((short) databaseNameHex.length()), true);
char encodedInstanceType = InstanceTypeUtils.encode(instanceType);
return 'j' + jobType.getCode() + PipelineJobId.CURRENT_VERSION + encodedInstanceType + databaseNameLengthHex + databaseNameHex;
return 'j' + jobType.getCode() + PipelineJobId.CURRENT_VERSION + instanceType.getCode() + databaseNameLengthHex + databaseNameHex;
}

/**
Expand Down Expand Up @@ -88,10 +86,10 @@ public static PipelineContextKey parseContextKey(final String jobId) {
verifyJobId(jobId);
String formatVersion = jobId.substring(3, 5);
Preconditions.checkArgument(PipelineJobId.CURRENT_VERSION.equals(formatVersion), "Format version doesn't match, format version: " + formatVersion);
char instanceType = jobId.charAt(5);
char instanceTypeCode = jobId.charAt(5);
short databaseNameLength = Shorts.fromByteArray(Hex.decodeHex(jobId.substring(6, 10)));
String databaseName = new String(Hex.decodeHex(jobId.substring(10, 10 + databaseNameLength)), StandardCharsets.UTF_8);
return new PipelineContextKey(databaseName, InstanceTypeUtils.decode(instanceType));
return new PipelineContextKey(databaseName, InstanceType.valueOf(instanceTypeCode));
}

/**
Expand Down

This file was deleted.

0 comments on commit 59cc0a3

Please sign in to comment.