From 969dbf97178c14a37ab14faa42da3c9d62594a58 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 18 Nov 2024 23:55:03 +0800 Subject: [PATCH] Refactor ShowCreateTableMergedResult (#33716) * Refactor LogicTablesMergedResult * Refactor ShowCreateTableMergedResult * Refactor ShowCreateTableMergedResult * Refactor ShowCreateTableMergedResult --- .../dal/show/LogicTablesMergedResult.java | 21 ++++--- .../dal/show/ShowCreateTableMergedResult.java | 55 ++++++++++++------- .../dal/show/LogicTablesMergedResultTest.java | 8 +-- .../show/ShowCreateTableMergedResultTest.java | 10 ++-- .../dal/show/ShowIndexMergedResultTest.java | 6 +- .../show/ShowTableStatusMergedResultTest.java | 8 +-- 6 files changed, 60 insertions(+), 48 deletions(-) diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java index 9a41cb6d094b5..16c5babe3f30e 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java @@ -38,43 +38,42 @@ */ public class LogicTablesMergedResult extends MemoryMergedResult { - public LogicTablesMergedResult(final ShardingRule shardingRule, + public LogicTablesMergedResult(final ShardingRule rule, final SQLStatementContext sqlStatementContext, final ShardingSphereSchema schema, final List queryResults) throws SQLException { - super(shardingRule, schema, sqlStatementContext, queryResults); + super(rule, schema, sqlStatementContext, queryResults); } @Override - protected final List init(final ShardingRule shardingRule, final ShardingSphereSchema schema, + protected final List init(final ShardingRule rule, final ShardingSphereSchema schema, final SQLStatementContext sqlStatementContext, final List queryResults) throws SQLException { List result = new LinkedList<>(); Set tableNames = new HashSet<>(); for (QueryResult each : queryResults) { while (each.next()) { - createMemoryQueryResultRow(shardingRule, schema, each, tableNames).ifPresent(result::add); + createMemoryQueryResultRow(rule, schema, each, tableNames).ifPresent(result::add); } } return result; } - private Optional createMemoryQueryResultRow(final ShardingRule shardingRule, + private Optional createMemoryQueryResultRow(final ShardingRule rule, final ShardingSphereSchema schema, final QueryResult queryResult, final Set tableNames) throws SQLException { MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(queryResult); String actualTableName = memoryResultSetRow.getCell(1).toString(); - Optional shardingTable = shardingRule.findShardingTableByActualTable(actualTableName); + Optional shardingTable = rule.findShardingTableByActualTable(actualTableName); if (shardingTable.isPresent() && tableNames.add(shardingTable.get().getLogicTable())) { String logicTableName = shardingTable.get().getLogicTable(); memoryResultSetRow.setCell(1, logicTableName); - setCellValue(memoryResultSetRow, logicTableName, actualTableName, schema.getTable(logicTableName), shardingRule); + setCellValue(memoryResultSetRow, logicTableName, actualTableName, schema.getTable(logicTableName), rule); return Optional.of(memoryResultSetRow); } - if (shardingRule.getShardingTables().isEmpty() || tableNames.add(actualTableName)) { - setCellValue(memoryResultSetRow, actualTableName, actualTableName, schema.getTable(actualTableName), shardingRule); + if (rule.getShardingTables().isEmpty() || tableNames.add(actualTableName)) { + setCellValue(memoryResultSetRow, actualTableName, actualTableName, schema.getTable(actualTableName), rule); return Optional.of(memoryResultSetRow); } return Optional.empty(); } - protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow, - final String logicTableName, final String actualTableName, final ShardingSphereTable table, final ShardingRule shardingRule) { + protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName, final ShardingSphereTable table, final ShardingRule rule) { } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResult.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResult.java index e7d8250644b79..f8ab351d839b8 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResult.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResult.java @@ -44,24 +44,53 @@ */ public final class ShowCreateTableMergedResult extends LogicTablesMergedResult { - public ShowCreateTableMergedResult(final ShardingRule shardingRule, + public ShowCreateTableMergedResult(final ShardingRule rule, final SQLStatementContext sqlStatementContext, final ShardingSphereSchema schema, final List queryResults) throws SQLException { - super(shardingRule, sqlStatementContext, schema, queryResults); + super(rule, sqlStatementContext, schema, queryResults); } @Override protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName, - final ShardingSphereTable table, final ShardingRule shardingRule) { + final ShardingSphereTable table, final ShardingRule rule) { + replaceTables(memoryResultSetRow, logicTableName, actualTableName); + replaceBindingTables(memoryResultSetRow, logicTableName, actualTableName, rule); + replaceIndexes(memoryResultSetRow, actualTableName, table); + replaceConstraints(memoryResultSetRow, actualTableName, table, rule); + } + + private void replaceTables(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName) { memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replaceFirst(actualTableName, logicTableName)); - setViewCellValue(memoryResultSetRow, logicTableName, actualTableName, shardingRule); + } + + private void replaceBindingTables(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName, final ShardingRule rule) { + Optional shardingTable = rule.findShardingTable(logicTableName); + Optional bindingTableRule = rule.findBindingTableRule(logicTableName); + if (!shardingTable.isPresent() || !bindingTableRule.isPresent()) { + return; + } + Collection actualDataNodes = shardingTable.get().getActualDataNodes().stream().filter(each -> each.getTableName().equalsIgnoreCase(actualTableName)).collect(Collectors.toList()); + Map logicAndActualTablesFromBindingTables = new CaseInsensitiveMap<>(); + for (DataNode each : actualDataNodes) { + logicAndActualTablesFromBindingTables + .putAll(rule.getLogicAndActualTablesFromBindingTable(each.getDataSourceName(), logicTableName, actualTableName, bindingTableRule.get().getAllLogicTables())); + } + for (Entry entry : logicAndActualTablesFromBindingTables.entrySet()) { + memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replaceFirst(entry.getValue(), entry.getKey())); + } + } + + private void replaceIndexes(final MemoryQueryResultRow memoryResultSetRow, final String actualTableName, final ShardingSphereTable table) { for (ShardingSphereIndex each : table.getIndexValues()) { String actualIndexName = IndexMetaDataUtils.getActualIndexName(each.getName(), actualTableName); memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replace(actualIndexName, each.getName())); } + } + + private void replaceConstraints(final MemoryQueryResultRow memoryResultSetRow, final String actualTableName, final ShardingSphereTable table, final ShardingRule rule) { for (ShardingSphereConstraint each : table.getConstraintValues()) { String actualIndexName = IndexMetaDataUtils.getActualIndexName(each.getName(), actualTableName); memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replace(actualIndexName, each.getName())); - Optional shardingTable = shardingRule.findShardingTable(each.getReferencedTableName()); + Optional shardingTable = rule.findShardingTable(each.getReferencedTableName()); if (!shardingTable.isPresent()) { continue; } @@ -70,20 +99,4 @@ protected void setCellValue(final MemoryQueryResultRow memoryResultSetRow, final } } } - - private void setViewCellValue(final MemoryQueryResultRow memoryResultSetRow, final String logicTableName, final String actualTableName, final ShardingRule shardingRule) { - Optional shardingTable = shardingRule.findShardingTable(logicTableName); - Optional bindingTableRule = shardingRule.findBindingTableRule(logicTableName); - if (shardingTable.isPresent() && bindingTableRule.isPresent()) { - Collection actualDataNodes = shardingTable.get().getActualDataNodes().stream().filter(each -> each.getTableName().equalsIgnoreCase(actualTableName)).collect(Collectors.toList()); - Map logicAndActualTablesFromBindingTables = new CaseInsensitiveMap<>(); - for (DataNode each : actualDataNodes) { - logicAndActualTablesFromBindingTables - .putAll(shardingRule.getLogicAndActualTablesFromBindingTable(each.getDataSourceName(), logicTableName, actualTableName, bindingTableRule.get().getAllLogicTables())); - } - for (Entry entry : logicAndActualTablesFromBindingTables.entrySet()) { - memoryResultSetRow.setCell(2, memoryResultSetRow.getCell(2).toString().replaceFirst(entry.getValue(), entry.getKey())); - } - } - } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResultTest.java index 1cd16542e89a5..cc62ededa2a83 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResultTest.java @@ -42,13 +42,13 @@ class LogicTablesMergedResultTest { - private ShardingRule shardingRule; + private ShardingRule rule; private ShardingSphereSchema schema; @BeforeEach void setUp() { - shardingRule = createShardingRule(); + rule = createShardingRule(); schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table", new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap()); } @@ -61,12 +61,12 @@ private ShardingRule createShardingRule() { @Test void assertNextForEmptyQueryResult() throws SQLException { - assertFalse(new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); + assertFalse(new LogicTablesMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); } @Test void assertNextForActualTableNameInTableRule() throws SQLException { - assertTrue(new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult("table_0"))).next()); + assertTrue(new LogicTablesMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult("table_0"))).next()); } private QueryResult mockQueryResult(final String value) throws SQLException { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java index 37c6088b2b0a8..be196f3555cc5 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java @@ -47,13 +47,13 @@ class ShowCreateTableMergedResultTest { - private ShardingRule shardingRule; + private ShardingRule rule; private ShardingSphereSchema schema; @BeforeEach void setUp() { - shardingRule = buildShardingRule(); + rule = buildShardingRule(); schema = createSchema(); } @@ -74,17 +74,17 @@ private ShardingSphereSchema createSchema() { @Test void assertNextForEmptyQueryResult() throws SQLException { - assertFalse(new ShowCreateTableMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); + assertFalse(new ShowCreateTableMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); } @Test void assertNextForTableRulePresent() throws SQLException { - assertTrue(new ShowCreateTableMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next()); + assertTrue(new ShowCreateTableMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next()); } @Test void assertGetValueForTableRulePresent() throws SQLException { - ShowCreateTableMergedResult actual = new ShowCreateTableMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())); + ShowCreateTableMergedResult actual = new ShowCreateTableMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())); assertTrue(actual.next()); assertThat(actual.getValue(1, String.class), is("t_order")); assertThat(actual.getValue(2, String.class), is("CREATE TABLE `t_order` (\n" diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResultTest.java index 14a02347da6e6..bcad1b9e34535 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResultTest.java @@ -39,19 +39,19 @@ class ShowIndexMergedResultTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Mock private ShardingSphereSchema schema; @Test void assertNextForEmptyQueryResult() throws SQLException { - assertFalse(new ShowIndexMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); + assertFalse(new ShowIndexMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); } @Test void assertNextForTableRuleIsPresent() throws SQLException { - assertTrue(new ShowIndexMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next()); + assertTrue(new ShowIndexMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())).next()); } private QueryResult mockQueryResult() throws SQLException { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResultTest.java index 499cfe0f320e4..db870b2adfc7e 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResultTest.java @@ -50,14 +50,14 @@ class ShowTableStatusMergedResultTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Mock private ShardingSphereSchema schema; @BeforeEach void setUp() { - shardingRule = buildShardingRule(); + rule = buildShardingRule(); schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table", new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap()); } @@ -70,12 +70,12 @@ private ShardingRule buildShardingRule() { @Test void assertNextForEmptyQueryResult() throws SQLException { - assertFalse(new ShowTableStatusMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); + assertFalse(new ShowTableStatusMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.emptyList()).next()); } @Test void assertNextForTableRuleIsPresent() throws SQLException { - MergedResult mergedResult = new ShowTableStatusMergedResult(shardingRule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())); + MergedResult mergedResult = new ShowTableStatusMergedResult(rule, mock(SQLStatementContext.class), schema, Collections.singletonList(mockQueryResult())); assertTrue(mergedResult.next()); assertFalse(mergedResult.next()); }