Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SingleTableSegment #33948

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading