Skip to content

Commit

Permalink
Refactor DataNode.format() (#32955)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Sep 22, 2024
1 parent 14a3f1e commit c5dc97c
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware;
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecutor;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableNodesStatement;
Expand Down Expand Up @@ -49,12 +48,12 @@ public Collection<String> getColumnNames(final ShowShardingTableNodesStatement s
public Collection<LocalDataQueryResultRow> getRows(final ShowShardingTableNodesStatement sqlStatement, final ContextManager contextManager) {
String tableName = sqlStatement.getTableName();
return null == tableName
? rule.getShardingTables().entrySet().stream().map(entry -> new LocalDataQueryResultRow(entry.getKey(), getTableNodes(entry.getValue()))).collect(Collectors.toList())
: Collections.singleton(new LocalDataQueryResultRow(tableName, getTableNodes(rule.getShardingTable(tableName))));
? rule.getShardingTables().entrySet().stream().map(entry -> new LocalDataQueryResultRow(entry.getKey(), getTableNodes(sqlStatement, entry.getValue()))).collect(Collectors.toList())
: Collections.singleton(new LocalDataQueryResultRow(tableName, getTableNodes(sqlStatement, rule.getShardingTable(tableName))));
}

private String getTableNodes(final ShardingTable shardingTable) {
return shardingTable.getActualDataNodes().stream().map(DataNode::format).collect(Collectors.joining(", "));
private String getTableNodes(final ShowShardingTableNodesStatement sqlStatement, final ShardingTable shardingTable) {
return shardingTable.getActualDataNodes().stream().map(each -> each.format(sqlStatement.getDatabaseType())).collect(Collectors.joining(", "));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ private boolean isActualDataNodesIncludedDataSourceInstance(final String actualD
return isValidDataNode(actualDataNodes, 3);
}

/**
* Format data node as string.
*
* @return formatted data node
*/
public String format() {
return dataSourceName + DELIMITER + tableName;
}

/**
* Format data node as string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.shardingsphere.infra.exception.kernel.metadata.datanode;

import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;

import java.util.Collection;
Expand All @@ -29,7 +28,7 @@ public final class UnsupportedActualDataNodeStructureException extends DataNodeD

private static final long serialVersionUID = -8921823916974492519L;

public UnsupportedActualDataNodeStructureException(final DataNode dataNode, final Collection<String> jdbcUrlPrefixes) {
super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 1, "Can not support 3-tier structure for actual data node '%s' with JDBC '%s'.", dataNode.format(), jdbcUrlPrefixes);
public UnsupportedActualDataNodeStructureException(final String dataSourceName, final String tableName, final Collection<String> jdbcUrlPrefixes) {
super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 1, "Can not support 3-tier structure for actual data node '%s.%s' with JDBC '%s'.", dataSourceName, tableName, jdbcUrlPrefixes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ private static void checkDataSourceTypeIncludeInstanceAndSetDatabaseTableMap(fin
final String tableName) {
for (DataNode dataNode : dataNodes.getDataNodes(tableName)) {
ShardingSpherePreconditions.checkState(notSupportThreeTierStructureStorageTypes.isEmpty() || !dataNode.getDataSourceName().contains("."),
() -> new UnsupportedActualDataNodeStructureException(dataNode, notSupportThreeTierStructureStorageTypes.iterator().next().getJdbcUrlPrefixes()));
() -> new UnsupportedActualDataNodeStructureException(
dataNode.getDataSourceName(), dataNode.getTableName(), notSupportThreeTierStructureStorageTypes.iterator().next().getJdbcUrlPrefixes()));
if (dataNode.getDataSourceName().contains(".")) {
String database = dataNode.getDataSourceName().split("\\.")[1];
GlobalDataSourceRegistry.getInstance().getCachedDatabaseTables().put(dataNode.getTableName(), database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ void assertEmptyTableDataNode() {
assertThrows(InvalidDataNodeFormatException.class, () -> new DataNode("ds_0."));
}

@Test
void assertFormat() {
String expected = "ds_0.tbl_0";
DataNode dataNode = new DataNode(expected);
assertThat(dataNode.format(), is(expected));
}

@Test
void assertNewValidDataNodeIncludeInstance() {
DataNode dataNode = new DataNode("ds_0.db_0.tbl_0");
Expand All @@ -103,11 +96,4 @@ void assertHashCodeIncludeInstance() {
void assertToStringIncludeInstance() {
assertThat(new DataNode("ds_0.db_0.tbl_0").toString(), is("DataNode(dataSourceName=ds_0.db_0, tableName=tbl_0, schemaName=null)"));
}

@Test
void assertFormatIncludeInstance() {
String expected = "ds_0.db_0.tbl_0";
DataNode dataNode = new DataNode(expected);
assertThat(dataNode.format(), is(expected));
}
}

0 comments on commit c5dc97c

Please sign in to comment.