From 4db91778daac8aedc1f7f39e4d7dc89b6db32754 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sat, 7 Dec 2024 00:11:39 +0800 Subject: [PATCH] Refactor SingleTableSegment (#33948) * Refactor SingleTableSegment * Refactor SingleTableSegment * Refactor SingleTableSegment --- .../distsql/segment/SingleTableSegment.java | 57 +++++++++---------- .../segment/SingleTableSegmentTest.java | 53 +++++++++++++++++ 2 files changed, 79 insertions(+), 31 deletions(-) diff --git a/kernel/single/distsql/statement/src/main/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegment.java b/kernel/single/distsql/statement/src/main/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegment.java index cc6f7ada2a310..012177d5274c0 100644 --- a/kernel/single/distsql/statement/src/main/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegment.java +++ b/kernel/single/distsql/statement/src/main/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegment.java @@ -17,52 +17,29 @@ package org.apache.shardingsphere.single.distsql.segment; -import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.shardingsphere.distsql.segment.DistSQLSegment; -import org.apache.shardingsphere.infra.metadata.caseinsensitive.CaseInsensitiveIdentifier; /** * Single table segment. */ @RequiredArgsConstructor -@EqualsAndHashCode public final class SingleTableSegment implements DistSQLSegment { - private final CaseInsensitiveIdentifier storageUnitName; + @Getter + private final String storageUnitName; - private final CaseInsensitiveIdentifier schemaName; + private final String schemaName; - private final CaseInsensitiveIdentifier tableName; + @Getter + private final String tableName; public SingleTableSegment(final String storageUnitName, final String tableName) { this(storageUnitName, null, tableName); } - public SingleTableSegment(final String storageUnitName, final String schemaName, final String tableName) { - this.storageUnitName = new CaseInsensitiveIdentifier(storageUnitName); - this.schemaName = null == schemaName ? null : new CaseInsensitiveIdentifier(schemaName); - this.tableName = new CaseInsensitiveIdentifier(tableName); - } - - /** - * Get storage unit name. - * - * @return storage unit name - */ - public String getStorageUnitName() { - return storageUnitName.toString(); - } - - /** - * Get table name. - * - * @return table name - */ - public String getTableName() { - return tableName.toString(); - } - /** * Whether to contain schema. * @@ -72,8 +49,26 @@ public boolean containsSchema() { return null != schemaName; } + @Override + public boolean equals(final Object obj) { + if (!(obj instanceof SingleTableSegment)) { + return false; + } + if (null == schemaName) { + return storageUnitName.equalsIgnoreCase(((SingleTableSegment) obj).storageUnitName) && tableName.equalsIgnoreCase(((SingleTableSegment) obj).tableName) + && null == ((SingleTableSegment) obj).schemaName; + } + return storageUnitName.equalsIgnoreCase(((SingleTableSegment) obj).storageUnitName) + && schemaName.equalsIgnoreCase(((SingleTableSegment) obj).schemaName) && tableName.equalsIgnoreCase(((SingleTableSegment) obj).tableName); + } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(storageUnitName).append(schemaName).append(tableName).toHashCode(); + } + @Override public String toString() { - return null == schemaName ? String.join(".", getStorageUnitName(), getTableName()) : String.join(".", getStorageUnitName(), schemaName.toString(), getTableName()); + return null == schemaName ? String.join(".", storageUnitName, tableName) : String.join(".", storageUnitName, schemaName, tableName); } } diff --git a/kernel/single/distsql/statement/src/test/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegmentTest.java b/kernel/single/distsql/statement/src/test/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegmentTest.java index 7d9797d08dcce..b4f0ee44f692a 100644 --- a/kernel/single/distsql/statement/src/test/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegmentTest.java +++ b/kernel/single/distsql/statement/src/test/java/org/apache/shardingsphere/single/distsql/segment/SingleTableSegmentTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -36,6 +37,58 @@ void assertDoesNotContainSchema() { assertFalse(new SingleTableSegment("foo_ds", "foo_tbl").containsSchema()); } + @Test + void assertEqualsWithNotSingleTableSegment() { + assertThat(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"), not(new Object())); + } + + @Test + void assertNotEqualsWithoutSchemaAndDifferentStorageUnitName() { + assertThat(new SingleTableSegment("foo_ds", "foo_tbl"), not(new SingleTableSegment("bar_ds", "foo_tbl"))); + } + + @Test + void assertNotEqualsWithoutSchemaAndDifferentTableName() { + assertThat(new SingleTableSegment("foo_ds", "foo_tbl"), not(new SingleTableSegment("foo_ds", "bar_tbl"))); + } + + @Test + void assertNotEqualsWithSchemaAndDifferentStorageUnitName() { + assertThat(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"), not(new SingleTableSegment("bar_ds", "foo_schema", "foo_tbl"))); + } + + @Test + void assertNotEqualsWithDifferentSchema() { + assertThat(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"), not(new SingleTableSegment("foo_ds", "bar_schema", "foo_tbl"))); + } + + @Test + void assertNotEqualsWithSchemaAndDifferentTableName() { + assertThat(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"), not(new SingleTableSegment("foo_ds", "foo_schema", "bar_tbl"))); + } + + @Test + void assertNotEqualsWithMismatchedSchema() { + assertThat(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"), not(new SingleTableSegment("foo_ds", "foo_tbl"))); + assertThat(new SingleTableSegment("foo_ds", "foo_tbl"), not(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"))); + } + + @Test + void assertEqualsWithoutSchema() { + assertThat(new SingleTableSegment("foo_ds", "foo_tbl").hashCode(), is(new SingleTableSegment("foo_ds", "foo_tbl").hashCode())); + } + + @Test + void assertEqualsWithSchema() { + assertThat(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl").hashCode(), is(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl").hashCode())); + } + + @Test + void assertHashCode() { + assertThat(new SingleTableSegment("foo_ds", "foo_tbl"), is(new SingleTableSegment("foo_ds", "foo_tbl"))); + assertThat(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"), is(new SingleTableSegment("foo_ds", "foo_schema", "foo_tbl"))); + } + @Test void assertToStringWithoutSchemaName() { assertThat(new SingleTableSegment("foo_ds", "foo_tbl").toString(), is("foo_ds.foo_tbl"));