Skip to content

Commit

Permalink
Refactor SingleTableSegment (#33948)
Browse files Browse the repository at this point in the history
* Refactor SingleTableSegment

* Refactor SingleTableSegment

* Refactor SingleTableSegment
  • Loading branch information
terrymanu authored Dec 6, 2024
1 parent eab8cd6 commit 4db9177
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
Expand Down

0 comments on commit 4db9177

Please sign in to comment.