diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java index 3904d1c4620c1..b55f9355d2a93 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java @@ -121,25 +121,25 @@ private TimestampServiceRule createTimeServiceRule() { private ShardingSphereDatabase createDatabase(final ShardingRule shardingRule, final TimestampServiceRule timestampServiceRule) { ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME); - schema.getTables().put("t_warehouse", new ShardingSphereTable("t_warehouse", Arrays.asList( + schema.putTable(new ShardingSphereTable("t_warehouse", Arrays.asList( new ShardingSphereColumn("id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("warehouse_name", Types.VARCHAR, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - schema.getTables().put("t_order", new ShardingSphereTable("t_order", Arrays.asList( + schema.putTable(new ShardingSphereTable("t_order", Arrays.asList( new ShardingSphereColumn("warehouse_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - schema.getTables().put("t_order_item", new ShardingSphereTable("t_order_item", Arrays.asList( + schema.putTable(new ShardingSphereTable("t_order_item", Arrays.asList( new ShardingSphereColumn("warehouse_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("order_broadcast_table_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - schema.getTables().put("t_non_sharding_table", new ShardingSphereTable("t_non_sharding_table", Collections.singleton( + schema.putTable(new ShardingSphereTable("t_non_sharding_table", Collections.singleton( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - schema.getTables().put("t_non_cacheable_database_sharding", new ShardingSphereTable("t_non_cacheable_database_sharding", Collections.singleton( + schema.putTable(new ShardingSphereTable("t_non_cacheable_database_sharding", Collections.singleton( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - schema.getTables().put("t_non_cacheable_table_sharding", new ShardingSphereTable("t_non_cacheable_table_sharding", Collections.singleton( + schema.putTable(new ShardingSphereTable("t_non_cacheable_table_sharding", Collections.singleton( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); return new ShardingSphereDatabase(DATABASE_NAME, TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"), 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 cc62ededa2a83..45d5419459c5a 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 @@ -49,8 +49,8 @@ class LogicTablesMergedResultTest { @BeforeEach void setUp() { rule = createShardingRule(); - schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table", - new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap()); + schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, + Collections.singleton(new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyList()); } private ShardingRule createShardingRule() { 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 adb24b5230a18..8ca0eeff75452 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 @@ -32,9 +32,9 @@ import org.junit.jupiter.api.Test; import java.sql.SQLException; +import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.util.LinkedList; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -64,11 +64,10 @@ private ShardingRule buildShardingRule() { } private ShardingSphereSchema createSchema() { - Map tables = new HashMap<>(2, 1F); - tables.put("foo_tbl", - new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.singleton(new ShardingSphereConstraint("foo_tbl_foreign_key", "bar_tbl")))); - tables.put("bar_tbl", new ShardingSphereTable("bar_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - return new ShardingSphereSchema("foo_db", tables, Collections.emptyMap()); + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.singleton(new ShardingSphereConstraint("foo_tbl_foreign_key", "bar_tbl")))); + tables.add(new ShardingSphereTable("bar_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + return new ShardingSphereSchema("foo_db", tables, Collections.emptyList()); } @Test 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 db870b2adfc7e..30077c7ba615f 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 @@ -58,8 +58,8 @@ class ShowTableStatusMergedResultTest { @BeforeEach void setUp() { rule = buildShardingRule(); - schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table", - new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap()); + schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, + Collections.singleton(new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyList()); } private ShardingRule buildShardingRule() { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java index 05e69694440c2..8e41af1c91cdf 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java @@ -513,7 +513,7 @@ private ShardingSphereDatabase createDatabase() { ShardingSphereColumn column2 = new ShardingSphereColumn("col2", 0, false, false, false, true, false, false); ShardingSphereColumn column3 = new ShardingSphereColumn("col3", 0, false, false, false, true, false, false); ShardingSphereTable table = new ShardingSphereTable("tbl", Arrays.asList(column1, column2, column3), Collections.emptyList(), Collections.emptyList()); - ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()); return new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), mock(ResourceMetaData.class), mock(RuleMetaData.class), Collections.singletonMap(DefaultDatabase.LOGIC_NAME, schema)); } @@ -523,7 +523,7 @@ private ShardingSphereDatabase createSQLServerDatabase() { ShardingSphereColumn column2 = new ShardingSphereColumn("col2", 0, false, false, false, true, false, false); ShardingSphereColumn column3 = new ShardingSphereColumn("col3", 0, false, false, false, true, false, false); ShardingSphereTable table = new ShardingSphereTable("tbl", Arrays.asList(column1, column2, column3), Collections.emptyList(), Collections.emptyList()); - ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()); return new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "SQLServer"), mock(ResourceMetaData.class), mock(RuleMetaData.class), Collections.singletonMap("dbo", schema)); } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java index f9ed7a3a2c5c3..b596bde1628c3 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java @@ -187,7 +187,7 @@ private ShardingSphereDatabase createDatabase() { ShardingSphereColumn column2 = new ShardingSphereColumn("col2", 0, false, false, false, true, false, false); ShardingSphereColumn column3 = new ShardingSphereColumn("col3", 0, false, false, false, true, false, false); ShardingSphereTable table = new ShardingSphereTable("tbl", Arrays.asList(column1, column2, column3), Collections.emptyList(), Collections.emptyList()); - ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()); return new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), mock(ResourceMetaData.class), mock(RuleMetaData.class), Collections.singletonMap(DefaultDatabase.LOGIC_NAME, schema)); } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java index cd3e5add266b7..15cd2e9ec334b 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java @@ -206,7 +206,7 @@ private ShardingSphereDatabase createDatabase() { ShardingSphereColumn column1 = new ShardingSphereColumn("col1", 0, false, false, true, true, false, false); ShardingSphereColumn column2 = new ShardingSphereColumn("col2", 0, false, false, false, true, false, false); ShardingSphereTable table = new ShardingSphereTable("tbl", Arrays.asList(column1, column2), Collections.emptyList(), Collections.emptyList()); - ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()); return new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), mock(ResourceMetaData.class), mock(RuleMetaData.class), Collections.singletonMap(DefaultDatabase.LOGIC_NAME, schema)); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java index 2699dc955547e..9b760c3a7b6b7 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java @@ -48,8 +48,9 @@ import java.sql.Types; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; -import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -101,27 +102,27 @@ private static ShardingSphereMetaData createShardingSphereMetaData(final Shardin } private static Map buildSchemas() { - Map tables = new HashMap<>(3, 1F); - tables.put("t_order", new ShardingSphereTable("t_order", Arrays.asList(new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("t_order", Arrays.asList(new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("product_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_order_item", new ShardingSphereTable("t_order_item", Arrays.asList(new ShardingSphereColumn("item_id", Types.INTEGER, true, false, false, true, false, false), + tables.add(new ShardingSphereTable("t_order_item", Arrays.asList(new ShardingSphereColumn("item_id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("order_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("product_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("c_date", Types.TIMESTAMP, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_other", new ShardingSphereTable("t_other", Collections.singletonList( + tables.add(new ShardingSphereTable("t_other", Collections.singletonList( new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_category", new ShardingSphereTable("t_category", Collections.singleton(new ShardingSphereColumn("id", Types.INTEGER, true, false, false, true, false, false)), + tables.add(new ShardingSphereTable("t_category", Collections.singleton(new ShardingSphereColumn("id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_product", new ShardingSphereTable("t_product", Collections.singleton(new ShardingSphereColumn("product_id", Types.INTEGER, true, false, false, true, false, false)), + tables.add(new ShardingSphereTable("t_product", Collections.singleton(new ShardingSphereColumn("product_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_user", new ShardingSphereTable("t_user", Collections.singleton(new ShardingSphereColumn("user_id", Types.INTEGER, true, false, false, true, false, false)), + tables.add(new ShardingSphereTable("t_user", Collections.singleton(new ShardingSphereColumn("user_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_hint_test", new ShardingSphereTable("t_hint_test", Collections.singleton(new ShardingSphereColumn("user_id", Types.INTEGER, true, false, false, true, false, false)), + tables.add(new ShardingSphereTable("t_hint_test", Collections.singleton(new ShardingSphereColumn("user_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - return Collections.singletonMap(DefaultDatabase.LOGIC_NAME, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap())); + return Collections.singletonMap(DefaultDatabase.LOGIC_NAME, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList())); } } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/insert/keygen/engine/GeneratedKeyContextEngineTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/insert/keygen/engine/GeneratedKeyContextEngineTest.java index ede9e8f3e9d21..4ecee6d7705bc 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/insert/keygen/engine/GeneratedKeyContextEngineTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/insert/keygen/engine/GeneratedKeyContextEngineTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.infra.binder.context.segment.insert.keygen.engine; import com.cedarsoftware.util.CaseInsensitiveMap; -import com.google.common.collect.ImmutableMap; import org.apache.shardingsphere.infra.binder.context.segment.insert.keygen.GeneratedKeyContext; import org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertValueContext; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; @@ -45,6 +44,7 @@ import org.junit.jupiter.api.Test; import java.sql.Types; +import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -66,7 +66,7 @@ void setUp() { "tbl", Collections.singletonList(new ShardingSphereColumn("id", Types.INTEGER, true, true, false, true, false, false)), Collections.emptyList(), Collections.emptyList()); ShardingSphereTable table2 = new ShardingSphereTable( "tbl2", Collections.singletonList(new ShardingSphereColumn("ID", Types.INTEGER, true, true, false, true, false, false)), Collections.emptyList(), Collections.emptyList()); - schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, ImmutableMap.of(table.getName(), table, table2.getName(), table2), Collections.emptyMap()); + schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Arrays.asList(table, table2), Collections.emptyList()); } @Test diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java index 4ab7dabc18de5..d9106ed54d24a 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java @@ -37,8 +37,6 @@ import java.util.HashSet; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -129,8 +127,7 @@ void assertFindTableNameWhenColumnSegmentOwnerAbsentAndSchemaMetaDataContainsCol SimpleTableSegment tableSegment2 = createTableSegment("TABLE_2", "TBL_2"); ShardingSphereTable table = new ShardingSphereTable("TABLE_1", Collections.singletonList(new ShardingSphereColumn("COL", 0, false, false, true, true, false, false)), Collections.emptyList(), Collections.emptyList()); - ShardingSphereSchema schema = - new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Stream.of(table).collect(Collectors.toMap(ShardingSphereTable::getName, value -> value)), Collections.emptyMap()); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()); ColumnSegment columnSegment = createColumnSegment(null, "COL"); Map actual = new TablesContext(Arrays.asList(tableSegment1, tableSegment2), databaseType, DefaultDatabase.LOGIC_NAME).findTableNames(Collections.singletonList(columnSegment), schema); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java index f0e93e12550b9..c030d214bd6e2 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java @@ -123,21 +123,14 @@ private static Map convertToSchemaMap(final Map result = new ConcurrentHashMap<>(schemaMetaDataMap.size(), 1F); for (Entry entry : schemaMetaDataMap.entrySet()) { - Map tables = convertToTableMap(entry.getValue().getTables()); - result.put(entry.getKey().toLowerCase(), new ShardingSphereSchema(entry.getKey(), tables, new LinkedHashMap<>())); + result.put(entry.getKey().toLowerCase(), new ShardingSphereSchema(entry.getKey(), convertToTables(entry.getValue().getTables()), new LinkedList<>())); } return result; } - private static Map convertToTableMap(final Collection tableMetaDataList) { - Map result = new LinkedHashMap<>(tableMetaDataList.size(), 1F); - for (TableMetaData each : tableMetaDataList) { - Collection columns = convertToColumns(each.getColumns()); - Collection indexes = convertToIndexes(each.getIndexes()); - Collection constraints = convertToConstraints(each.getConstraints()); - result.put(each.getName(), new ShardingSphereTable(each.getName(), columns, indexes, constraints, each.getType())); - } - return result; + private static Collection convertToTables(final Collection tableMetaDataList) { + return tableMetaDataList.stream().map(each -> new ShardingSphereTable( + each.getName(), convertToColumns(each.getColumns()), convertToIndexes(each.getIndexes()), convertToConstraints(each.getConstraints()), each.getType())).collect(Collectors.toList()); } private static Collection convertToColumns(final Collection columnMetaDataList) { diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java index e779cefdce9b2..9d0972466c2e8 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java @@ -36,6 +36,7 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Properties; @@ -77,13 +78,13 @@ private static Collection getSystemSchemas(final String originalDatabase private static ShardingSphereSchema createSchema(final String schemaName, final Collection schemaStreams, final YamlTableSwapper swapper, final boolean isSystemSchemaMetadataEnabled) { - Map tables = new LinkedHashMap<>(schemaStreams.size(), 1F); + Collection tables = new LinkedList<>(); for (InputStream each : schemaStreams) { YamlShardingSphereTable metaData = new Yaml().loadAs(each, YamlShardingSphereTable.class); if (isSystemSchemaMetadataEnabled || KernelSupportedSystemTables.isSupportedSystemTable(schemaName, metaData.getName())) { - tables.put(metaData.getName(), swapper.swapToObject(metaData)); + tables.add(swapper.swapToObject(metaData)); } } - return new ShardingSphereSchema(schemaName, tables, Collections.emptyMap()); + return new ShardingSphereSchema(schemaName, tables, Collections.emptyList()); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManager.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManager.java index a5be33de72f76..f15e0e6e7b000 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManager.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManager.java @@ -22,7 +22,9 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; +import java.util.Collection; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; @@ -49,18 +51,18 @@ public static Map getToBeAddedTablesBySchemas(fina } private static ShardingSphereSchema getToBeAddedTablesBySchema(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) { - return new ShardingSphereSchema(currentSchema.getName(), getToBeAddedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>()); + return new ShardingSphereSchema(currentSchema.getName(), getToBeAddedTables(reloadSchema, currentSchema), new LinkedList<>()); } /** * Get to be added tables. * - * @param reloadTables reload tables - * @param currentTables current tables + * @param reloadSchema reload schema + * @param currentSchema current schema * @return to be added tables */ - public static Map getToBeAddedTables(final Map reloadTables, final Map currentTables) { - return reloadTables.entrySet().stream().filter(entry -> !entry.getValue().equals(currentTables.get(entry.getKey()))).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + public static Collection getToBeAddedTables(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) { + return reloadSchema.getAllTables().stream().filter(each -> !each.equals(currentSchema.getTable(each.getName().toLowerCase()))).collect(Collectors.toList()); } /** @@ -78,18 +80,18 @@ public static Map getToBeDroppedTablesBySchemas(fi } private static ShardingSphereSchema getToBeDroppedTablesBySchema(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) { - return new ShardingSphereSchema(currentSchema.getName(), getToBeDroppedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>()); + return new ShardingSphereSchema(currentSchema.getName(), getToBeDroppedTables(reloadSchema, currentSchema), new LinkedList<>()); } /** * Get to be drop tables. * - * @param reloadTables reload tables - * @param currentTables current tables + * @param reloadSchema reload schema + * @param currentSchema current schema * @return to be dropped table */ - public static Map getToBeDroppedTables(final Map reloadTables, final Map currentTables) { - return currentTables.entrySet().stream().filter(entry -> !reloadTables.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + public static Collection getToBeDroppedTables(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) { + return currentSchema.getAllTables().stream().filter(each -> !reloadSchema.containsTable(each.getName().toLowerCase())).collect(Collectors.toList()); } /** diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchema.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchema.java index 94d5b3fd1fc74..5966c8080598d 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchema.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchema.java @@ -23,15 +23,14 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; /** * ShardingSphere schema. */ -@Getter public final class ShardingSphereSchema { + @Getter private final String name; private final Map tables; @@ -45,12 +44,12 @@ public ShardingSphereSchema(final String name) { views = new ConcurrentHashMap<>(); } - public ShardingSphereSchema(final String name, final Map tables, final Map views) { + public ShardingSphereSchema(final String name, final Collection tables, final Collection views) { this.name = name; this.tables = new ConcurrentHashMap<>(tables.size(), 1F); this.views = new ConcurrentHashMap<>(views.size(), 1F); - tables.forEach((key, value) -> this.tables.put(key.toLowerCase(), value)); - views.forEach((key, value) -> this.views.put(key.toLowerCase(), value)); + tables.forEach(each -> this.tables.put(each.getName().toLowerCase(), each)); + views.forEach(each -> this.views.put(each.getName().toLowerCase(), each)); } /** @@ -63,23 +62,32 @@ public Collection getAllTableNames() { } /** - * Get table. + * Get all tables. + * + * @return all tables + */ + public Collection getAllTables() { + return tables.values(); + } + + /** + * Judge whether contains table. * * @param tableName table name - * @return table + * @return contains table or not */ - public ShardingSphereTable getTable(final String tableName) { - return tables.get(tableName.toLowerCase()); + public boolean containsTable(final String tableName) { + return tables.containsKey(tableName.toLowerCase()); } /** - * Get view. + * Get table. * - * @param viewName view name - * @return view + * @param tableName table name + * @return table */ - public ShardingSphereView getView(final String viewName) { - return views.get(viewName.toLowerCase()); + public ShardingSphereTable getTable(final String tableName) { + return tables.get(tableName.toLowerCase()); } /** @@ -92,51 +100,59 @@ public void putTable(final ShardingSphereTable table) { } /** - * Add tables. + * Remove table. * - * @param tables tables + * @param tableName table name */ - public void putTables(final Map tables) { - for (Entry entry : tables.entrySet()) { - putTable(entry.getValue()); - } + public void removeTable(final String tableName) { + tables.remove(tableName.toLowerCase()); } /** - * Add view. + * Get all views. * - * @param view view + * @return all views */ - public void putView(final ShardingSphereView view) { - views.put(view.getName().toLowerCase(), view); + public Collection getAllViews() { + return views.values(); } /** - * Remove table. + * Judge whether contains view. * - * @param tableName table name + * @param viewName view name + * @return contains view or not */ - public void removeTable(final String tableName) { - tables.remove(tableName.toLowerCase()); + public boolean containsView(final String viewName) { + return views.containsKey(viewName.toLowerCase()); } /** - * Remove view. + * Get view. * * @param viewName view name + * @return view */ - public void removeView(final String viewName) { - views.remove(viewName.toLowerCase()); + public ShardingSphereView getView(final String viewName) { + return views.get(viewName.toLowerCase()); } /** - * Judge whether contains table. + * Add view. * - * @param tableName table name - * @return contains table or not + * @param view view */ - public boolean containsTable(final String tableName) { - return tables.containsKey(tableName.toLowerCase()); + public void putView(final ShardingSphereView view) { + views.put(view.getName().toLowerCase(), view); + } + + /** + * Remove view. + * + * @param viewName view name + */ + public void removeView(final String viewName) { + views.remove(viewName.toLowerCase()); } /** @@ -150,16 +166,6 @@ public boolean containsIndex(final String tableName, final String indexName) { return containsTable(tableName) && getTable(tableName).containsIndex(indexName); } - /** - * Judge whether contains view. - * - * @param viewName view name - * @return contains view or not - */ - public boolean containsView(final String viewName) { - return views.containsKey(viewName.toLowerCase()); - } - /** * Get all column names. * diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilder.java index 9408541901004..81d928444b451 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilder.java @@ -94,10 +94,9 @@ private void initShardingTableStatisticsTable(final ShardingSphereSchemaData sch } private void initTables(final ShardingSphereSchema schema, final Collection tables, final ShardingSphereSchemaData schemaData) { - for (Entry entry : schema.getTables().entrySet()) { - if (tables.contains(entry.getValue().getName())) { - ShardingSphereTableData tableData = new ShardingSphereTableData(entry.getValue().getName()); - schemaData.putTable(entry.getKey(), tableData); + for (ShardingSphereTable each : schema.getAllTables()) { + if (tables.contains(each.getName().toLowerCase())) { + schemaData.putTable(each.getName().toLowerCase(), new ShardingSphereTableData(each.getName())); } } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java index d58aee597ed0c..d924c4c56956d 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java @@ -60,11 +60,11 @@ public Optional collect(final String databaseName, fina private Collection collectForSchema(final Long oid, final Long relNamespace, final ShardingSphereSchema schema, final ShardingSphereTable table) { Collection result = new LinkedList<>(); - for (Entry entry : schema.getTables().entrySet()) { + for (ShardingSphereTable each : schema.getAllTables()) { Map columnValues = new CaseInsensitiveMap<>(4, 1F); columnValues.put("oid", oid); columnValues.put("relnamespace", relNamespace); - columnValues.put("relname", entry.getKey()); + columnValues.put("relname", each.getName()); columnValues.put("relkind", "r"); result.add(new ShardingSphereRowData(ShardingSphereTableDataCollectorUtils.createRowValue(columnValues, table))); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java index 8fe0dbfa8f255..a5d31b1c576b4 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java @@ -24,7 +24,6 @@ import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute; @@ -75,14 +74,14 @@ void setUp() { void assertLoadWithExistedTableName() throws SQLException { Collection tableNames = Collections.singletonList("data_node_routed_table1"); when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames, material)); - assertFalse(GenericSchemaBuilder.build(tableNames, material).get(DefaultDatabase.LOGIC_NAME).getTables().isEmpty()); + assertFalse(GenericSchemaBuilder.build(tableNames, material).get(DefaultDatabase.LOGIC_NAME).getAllTables().isEmpty()); } @Test void assertLoadWithNotExistedTableName() throws SQLException { Collection tableNames = Collections.singletonList("invalid_table"); when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames, material)); - assertTrue(GenericSchemaBuilder.build(tableNames, material).get(DefaultDatabase.LOGIC_NAME).getTables().isEmpty()); + assertTrue(GenericSchemaBuilder.build(tableNames, material).get(DefaultDatabase.LOGIC_NAME).getAllTables().isEmpty()); } @Test @@ -91,7 +90,7 @@ void assertLoadAllTables() throws SQLException { when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames, material)); Map actual = GenericSchemaBuilder.build(tableNames, material); assertThat(actual.size(), is(1)); - assertTables(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, actual.values().iterator().next().getTables(), Collections.emptyMap()).getTables()); + assertTables(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, actual.values().iterator().next().getAllTables(), Collections.emptyList())); } private Map createSchemaMetaDataMap(final Collection tableNames, final GenericSchemaBuilderMaterial material) { @@ -103,9 +102,9 @@ private Map createSchemaMetaDataMap(final Collection actual) { - assertThat(actual.size(), is(2)); - assertTrue(actual.get("data_node_routed_table1").getAllColumns().isEmpty()); - assertTrue(actual.get("data_node_routed_table2").getAllColumns().isEmpty()); + private void assertTables(final ShardingSphereSchema actual) { + assertThat(actual.getAllTables().size(), is(2)); + assertTrue(actual.getTable("data_node_routed_table1").getAllColumns().isEmpty()); + assertTrue(actual.getTable("data_node_routed_table2").getAllColumns().isEmpty()); } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderTest.java index d02be5381f396..1092be90a81a0 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderTest.java @@ -39,19 +39,19 @@ void assertBuildForMySQL() { Map actualInformationSchema = SystemSchemaBuilder.build("information_schema", databaseType, configProps); assertThat(actualInformationSchema.size(), is(1)); assertTrue(actualInformationSchema.containsKey("information_schema")); - assertThat(actualInformationSchema.get("information_schema").getTables().size(), is(95)); + assertThat(actualInformationSchema.get("information_schema").getAllTables().size(), is(95)); Map actualMySQLSchema = SystemSchemaBuilder.build("mysql", databaseType, configProps); assertThat(actualMySQLSchema.size(), is(1)); assertTrue(actualMySQLSchema.containsKey("mysql")); - assertThat(actualMySQLSchema.get("mysql").getTables().size(), is(40)); + assertThat(actualMySQLSchema.get("mysql").getAllTables().size(), is(40)); Map actualPerformanceSchema = SystemSchemaBuilder.build("performance_schema", databaseType, configProps); assertThat(actualPerformanceSchema.size(), is(1)); assertTrue(actualPerformanceSchema.containsKey("performance_schema")); - assertThat(actualPerformanceSchema.get("performance_schema").getTables().size(), is(114)); + assertThat(actualPerformanceSchema.get("performance_schema").getAllTables().size(), is(114)); Map actualSysSchema = SystemSchemaBuilder.build("sys", databaseType, configProps); assertThat(actualSysSchema.size(), is(1)); assertTrue(actualSysSchema.containsKey("sys")); - assertThat(actualSysSchema.get("sys").getTables().size(), is(53)); + assertThat(actualSysSchema.get("sys").getAllTables().size(), is(53)); } @Test @@ -62,9 +62,9 @@ void assertBuildForPostgreSQL() { assertTrue(actual.containsKey("information_schema")); assertTrue(actual.containsKey("pg_catalog")); assertTrue(actual.containsKey("shardingsphere")); - assertThat(actual.get("information_schema").getTables().size(), is(69)); - assertThat(actual.get("pg_catalog").getTables().size(), is(134)); - assertThat(actual.get("shardingsphere").getTables().size(), is(2)); + assertThat(actual.get("information_schema").getAllTables().size(), is(69)); + assertThat(actual.get("pg_catalog").getAllTables().size(), is(134)); + assertThat(actual.get("shardingsphere").getAllTables().size(), is(2)); } @Test @@ -74,8 +74,8 @@ void assertBuildForOpenGaussSQL() { assertThat(actual.size(), is(16)); assertTrue(actual.containsKey("pg_catalog")); assertTrue(actual.containsKey("shardingsphere")); - assertThat(actual.get("information_schema").getTables().size(), is(66)); - assertThat(actual.get("pg_catalog").getTables().size(), is(240)); - assertThat(actual.get("shardingsphere").getTables().size(), is(2)); + assertThat(actual.get("information_schema").getAllTables().size(), is(66)); + assertThat(actual.get("pg_catalog").getAllTables().size(), is(240)); + assertThat(actual.get("shardingsphere").getAllTables().size(), is(2)); } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java index 23330df3cdc87..6d61b344d9f9e 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.junit.jupiter.api.Test; +import java.util.Collection; import java.util.Collections; import java.util.Map; @@ -34,42 +35,45 @@ class GenericSchemaManagerTest { @Test void assertGetToBeAddedTablesBySchemas() { - Map reloadSchemas = Collections.singletonMap("foo_schema", - new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("foo_table", new ShardingSphereTable("foo_table", - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap())); - Map currentSchemas = Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyMap(), Collections.emptyMap())); + Map reloadSchemas = Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, + Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyList())); + Map currentSchemas = Collections.singletonMap( + "foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyList(), Collections.emptyList())); Map actual = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadSchemas, currentSchemas); assertThat(actual.size(), is(1)); - assertThat(actual.get("foo_schema").getTables().size(), is(1)); - assertTrue(actual.get("foo_schema").getTables().containsKey("foo_table")); + assertThat(actual.get("foo_schema").getAllTables().size(), is(1)); + assertTrue(actual.get("foo_schema").containsTable("foo_tbl")); } @Test void assertGetToBeDroppedTablesBySchemas() { - Map currentSchemas = Collections.singletonMap("foo_schema", - new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("foo_table", new ShardingSphereTable("foo_table", - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap())); - Map reloadSchemas = Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyMap(), Collections.emptyMap())); + Map currentSchemas = Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, + Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyList())); + Map reloadSchemas = Collections.singletonMap("foo_schema", + new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyList(), Collections.emptyList())); Map actual = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadSchemas, currentSchemas); assertThat(actual.size(), is(1)); - assertThat(actual.get("foo_schema").getTables().size(), is(1)); - assertTrue(actual.get("foo_schema").getTables().containsKey("foo_table")); + assertThat(actual.get("foo_schema").getAllTables().size(), is(1)); + assertTrue(actual.get("foo_schema").containsTable("foo_tbl")); } @Test void assertGetToBeAddedTables() { - Map actual = GenericSchemaManager.getToBeAddedTables(Collections.singletonMap("foo_table", - new ShardingSphereTable("", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE)), Collections.emptyMap()); + ShardingSphereSchema reloadSchema = new ShardingSphereSchema("foo_schema", + Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE)), Collections.emptyList()); + Collection actual = GenericSchemaManager.getToBeAddedTables(reloadSchema, new ShardingSphereSchema("foo_schema", Collections.emptyList(), Collections.emptyList())); assertThat(actual.size(), is(1)); - assertTrue(actual.containsKey("foo_table")); + assertThat(actual.iterator().next().getName(), is("foo_tbl")); } @Test void assertGetToBeDroppedTables() { - Map actual = GenericSchemaManager.getToBeDroppedTables(Collections.emptyMap(), Collections.singletonMap("foo_table", - new ShardingSphereTable("", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE))); + ShardingSphereSchema reloadSchema = new ShardingSphereSchema("foo_schema", Collections.emptyList(), Collections.emptyList()); + ShardingSphereSchema currentSchema = new ShardingSphereSchema("foo_schema", + Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE)), Collections.emptyList()); + Collection actual = GenericSchemaManager.getToBeDroppedTables(reloadSchema, currentSchema); assertThat(actual.size(), is(1)); - assertTrue(actual.containsKey("foo_table")); + assertThat(actual.iterator().next().getName(), is("foo_tbl")); } @Test diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchemaTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchemaTest.java index 9af047fc1800c..84ce48d1f8e19 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchemaTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereSchemaTest.java @@ -34,27 +34,30 @@ class ShardingSphereSchemaTest { @Test void assertGetAllTableNames() { - assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", mock(ShardingSphereTable.class)), Collections.emptyMap()).getAllTableNames(), - is(new HashSet<>(Collections.singleton("tbl")))); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("tbl"); + assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).getAllTableNames(), is(new HashSet<>(Collections.singleton("tbl")))); } @Test void assertGetTable() { ShardingSphereTable table = mock(ShardingSphereTable.class); - assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()).getTable("tbl"), is(table)); + when(table.getName()).thenReturn("tbl"); + assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).getTable("tbl"), is(table)); } @Test void assertGetView() { + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("tbl"); ShardingSphereView view = mock(ShardingSphereView.class); - assertThat( - new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", mock(ShardingSphereTable.class)), Collections.singletonMap("tbl_view", view)).getView("tbl_view"), - is(view)); + when(view.getName()).thenReturn("tbl_view"); + assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.singleton(view)).getView("tbl_view"), is(view)); } @Test void assertPutTable() { - ShardingSphereSchema actual = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyMap(), Collections.emptyMap()); + ShardingSphereSchema actual = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyList(), Collections.emptyList()); ShardingSphereTable table = mock(ShardingSphereTable.class); when(table.getName()).thenReturn("tbl"); actual.putTable(table); @@ -63,28 +66,32 @@ void assertPutTable() { @Test void assertRemoveTable() { - ShardingSphereSchema actual = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", mock(ShardingSphereTable.class)), Collections.emptyMap()); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("tbl"); + ShardingSphereSchema actual = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()); actual.removeTable("tbl"); assertNull(actual.getTable("tbl")); } @Test void assertContainsTable() { - assertTrue(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", mock(ShardingSphereTable.class)), Collections.emptyMap()).containsTable("tbl")); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("tbl"); + assertTrue(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).containsTable("tbl")); } @Test void assertGetAllColumnNamesWhenContainsKey() { ShardingSphereTable table = new ShardingSphereTable("tbl", Collections.singletonList( new ShardingSphereColumn("col", 0, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList()); - assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()).getAllColumnNames("tbl"), is(Collections.singletonList("col"))); + assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).getAllColumnNames("tbl"), is(Collections.singletonList("col"))); } @Test void assertGetAllColumnNamesWhenNotContainsKey() { ShardingSphereTable table = new ShardingSphereTable("tbl", Collections.singletonList( new ShardingSphereColumn("col", 0, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList()); - assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl1", table), Collections.emptyMap()).getAllColumnNames("tbl2"), + assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).getAllColumnNames("tbl2"), is(Collections.emptyList())); } @@ -92,14 +99,14 @@ void assertGetAllColumnNamesWhenNotContainsKey() { void assertContainsIndex() { ShardingSphereTable table = new ShardingSphereTable( "tbl", Collections.emptyList(), Collections.singletonList(new ShardingSphereIndex("col_idx", Collections.emptyList(), false)), Collections.emptyList()); - assertTrue(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()).containsIndex("tbl", "col_idx")); + assertTrue(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).containsIndex("tbl", "col_idx")); } @Test void assertGetVisibleColumnNamesWhenContainsKey() { ShardingSphereTable table = new ShardingSphereTable("tbl", Collections.singletonList( new ShardingSphereColumn("col", 0, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList()); - assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()).getVisibleColumnNames("tbl"), + assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).getVisibleColumnNames("tbl"), is(Collections.singletonList("col"))); } @@ -107,6 +114,6 @@ void assertGetVisibleColumnNamesWhenContainsKey() { void assertGetVisibleColumnNamesWhenNotContainsKey() { ShardingSphereTable table = new ShardingSphereTable("tbl", Collections.singletonList( new ShardingSphereColumn("col", 0, false, false, false, false, true, false)), Collections.emptyList(), Collections.emptyList()); - assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap()).getVisibleColumnNames("tbl"), is(Collections.emptyList())); + assertThat(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()).getVisibleColumnNames("tbl"), is(Collections.emptyList())); } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/IndexMetaDataUtilsTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/IndexMetaDataUtilsTest.java index d75cdbacf0b95..ad0675381d61d 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/IndexMetaDataUtilsTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/IndexMetaDataUtilsTest.java @@ -92,8 +92,8 @@ void assertGetTableNames() { private ShardingSphereDatabase buildDatabase() { ShardingSphereTable table = new ShardingSphereTable( TABLE_NAME, Collections.emptyList(), Collections.singleton(new ShardingSphereIndex(INDEX_NAME, Collections.emptyList(), false)), Collections.emptyList()); - Map tables = Collections.singletonMap(TABLE_NAME, table); - Map schemas = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap())); + Map schemas = Collections.singletonMap( + DefaultDatabase.LOGIC_NAME, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList())); return new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ResourceMetaData.class), mock(RuleMetaData.class), schemas); } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilderTest.java index 5057cb28ebf6c..713b92c1be82f 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilderTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilderTest.java @@ -59,14 +59,9 @@ private Map mockDatabaseMap() { private Map mockSchemaMap() { ShardingSphereSchema schema = mock(ShardingSphereSchema.class); - Map tableMap = mockTableMap(); - when(schema.getTables()).thenReturn(tableMap); - return Collections.singletonMap("pg_catalog", schema); - } - - private Map mockTableMap() { ShardingSphereTable table = mock(ShardingSphereTable.class); when(table.getName()).thenReturn("pg_class"); - return Collections.singletonMap("pg_class", table); + when(schema.getAllTables()).thenReturn(Collections.singleton(table)); + return Collections.singletonMap("pg_catalog", schema); } } diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutor.java index cf7d81b121b6e..e44cd6c515169 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutor.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutor.java @@ -67,7 +67,7 @@ private LocalDataQueryResultRow getRow(final ShardingSphereTable table, final Sh } private Collection getTables(final String schemaName, final ShowLogicalTablesStatement sqlStatement) { - Collection tables = database.getSchema(schemaName).getTables().values(); + Collection tables = database.getSchema(schemaName).getAllTables(); Collection filteredTables = filterByLike(tables, sqlStatement); return filteredTables.stream().sorted(Comparator.comparing(ShardingSphereTable::getName)).collect(Collectors.toList()); } diff --git a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutorTest.java b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutorTest.java index fa95d1ec71a3e..39e49a4d2e428 100644 --- a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutorTest.java +++ b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowLogicalTablesExecutorTest.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.distsql.handler.executor.rql.resource; -import org.apache.groovy.util.Maps; import org.apache.shardingsphere.distsql.statement.rql.resource.ShowLogicalTablesStatement; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; @@ -35,9 +34,9 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; -import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -59,8 +58,8 @@ void setUp() { when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(database.getSchema("foo_db")).thenReturn(schema); - Map tables = Maps.of("t_order", mockShardingSphereTable("t_order"), "t_order_item", mockShardingSphereTable("t_order_item")); - when(schema.getTables()).thenReturn(tables); + Collection tables = Arrays.asList(mockShardingSphereTable("t_order"), mockShardingSphereTable("t_order_item")); + when(schema.getAllTables()).thenReturn(tables); executor.setDatabase(database); } diff --git a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java index ca4ff31c6039e..043f84bfb3c34 100644 --- a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java +++ b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java @@ -40,6 +40,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import static org.hamcrest.CoreMatchers.is; @@ -125,27 +127,27 @@ void assertBuildRouteSQLRewriteResultWithEmptyPrimaryKeyMeta() { } private Map buildDatabaseWithoutPrimaryKey() { - Map tables = new HashMap<>(3, 1F); - tables.put("logicName1", new ShardingSphereTable("logicName1", Arrays.asList(new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), + List tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("logicName1", Arrays.asList(new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.INTEGER, false, false, false, true, false, false)), Collections.emptySet(), Collections.emptyList())); - tables.put("t_other", new ShardingSphereTable("t_other", Collections.singletonList( + tables.add(new ShardingSphereTable("t_other", Collections.singletonList( new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptySet(), Collections.emptyList())); - return Collections.singletonMap("name", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap())); + return Collections.singletonMap("name", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList())); } private Map buildDatabase() { - Map tables = new HashMap<>(3, 1F); - tables.put("logicName1", new ShardingSphereTable("logicName1", Arrays.asList(new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("logicName1", Arrays.asList(new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.INTEGER, false, false, false, true, false, false)), Collections.emptySet(), Collections.emptyList())); - tables.put("logicName2", new ShardingSphereTable("logicName2", Arrays.asList(new ShardingSphereColumn("item_id", Types.INTEGER, true, false, false, true, false, false), + tables.add(new ShardingSphereTable("logicName2", Arrays.asList(new ShardingSphereColumn("item_id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("order_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("c_date", Types.TIMESTAMP, false, false, false, true, false, false)), Collections.emptySet(), Collections.emptyList())); - tables.put("t_other", new ShardingSphereTable("t_other", Collections.singletonList( + tables.add(new ShardingSphereTable("t_other", Collections.singletonList( new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false)), Collections.emptySet(), Collections.emptyList())); - return Collections.singletonMap("name", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap())); + return Collections.singletonMap("name", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList())); } } diff --git a/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java b/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java index 02174b6ae9dd6..f93435dd978ff 100644 --- a/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java +++ b/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java @@ -38,6 +38,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class CDCSchemaTableUtilsTest { @@ -65,17 +66,19 @@ void assertParseTableExpression() { } private ShardingSphereSchema mockedPublicSchema() { - Map tables = new HashMap<>(2, 1F); - tables.put("t_order", mock(ShardingSphereTable.class)); - tables.put("t_order2", mock(ShardingSphereTable.class)); - return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()); + ShardingSphereTable table1 = mock(ShardingSphereTable.class); + when(table1.getName()).thenReturn("t_order"); + ShardingSphereTable table2 = mock(ShardingSphereTable.class); + when(table2.getName()).thenReturn("t_order2"); + return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Arrays.asList(table1, table2), Collections.emptyList()); } private ShardingSphereSchema mockedTestSchema() { - Map tables = new HashMap<>(2, 1F); - tables.put("t_order_item", mock(ShardingSphereTable.class)); - tables.put("t_order_item2", mock(ShardingSphereTable.class)); - return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()); + ShardingSphereTable table1 = mock(ShardingSphereTable.class); + when(table1.getName()).thenReturn("t_order_item"); + ShardingSphereTable table2 = mock(ShardingSphereTable.class); + when(table2.getName()).thenReturn("t_order_item2"); + return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Arrays.asList(table1, table2), Collections.emptyList()); } @Test diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java index b0e6668498c04..602af68f127ef 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistService.java @@ -152,7 +152,7 @@ public void persistReloadDatabaseByAlter(final String databaseName, final Shardi Map toBeDeletedSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); Map toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); toBeAddedSchemas.forEach((key, value) -> databaseMetaDataFacade.getSchema().alterByRuleAltered(databaseName, value)); - toBeDeletedSchemas.forEach((key, value) -> databaseMetaDataFacade.getTable().drop(databaseName, key, value.getTables())); + toBeDeletedSchemas.forEach((key, value) -> databaseMetaDataFacade.getTable().drop(databaseName, key, value.getAllTables())); } /** @@ -166,6 +166,6 @@ public void persistReloadDatabaseByDrop(final String databaseName, final Shardin Map toBeAlterSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); Map toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas()); toBeAddedSchemas.forEach((key, value) -> databaseMetaDataFacade.getSchema().alterByRuleDropped(databaseName, key, value)); - toBeAlterSchemas.forEach((key, value) -> databaseMetaDataFacade.getTable().drop(databaseName, key, value.getTables())); + toBeAlterSchemas.forEach((key, value) -> databaseMetaDataFacade.getTable().drop(databaseName, key, value.getAllTables())); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java index 8e7eaab2ca3d9..ce0cc30628be3 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java @@ -19,7 +19,6 @@ import org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericSchemaManager; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.metadata.persist.service.metadata.table.TableMetaDataPersistService; import org.apache.shardingsphere.metadata.persist.service.metadata.table.ViewMetaDataPersistService; @@ -27,6 +26,7 @@ import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; @@ -76,12 +76,12 @@ public void drop(final String databaseName, final String schemaName) { */ public void alterByRefresh(final String databaseName, final ShardingSphereSchema schema) { String schemaName = schema.getName().toLowerCase(); - if (schema.getTables().isEmpty() && schema.getViews().isEmpty()) { + if (schema.isEmpty()) { add(databaseName, schemaName); } - Map currentTables = tableMetaDataPersistService.load(databaseName, schemaName); - tableMetaDataPersistService.persist(databaseName, schemaName, GenericSchemaManager.getToBeAddedTables(schema.getTables(), currentTables)); - GenericSchemaManager.getToBeDroppedTables(schema.getTables(), currentTables).forEach((key, value) -> tableMetaDataPersistService.drop(databaseName, schemaName, key)); + ShardingSphereSchema currentSchema = new ShardingSphereSchema(schemaName, tableMetaDataPersistService.load(databaseName, schemaName), Collections.emptyList()); + tableMetaDataPersistService.persist(databaseName, schemaName, GenericSchemaManager.getToBeAddedTables(schema, currentSchema)); + GenericSchemaManager.getToBeDroppedTables(schema, currentSchema).forEach(each -> tableMetaDataPersistService.drop(databaseName, schemaName, each.getName())); } /** @@ -92,10 +92,10 @@ public void alterByRefresh(final String databaseName, final ShardingSphereSchema */ public void alterByRuleAltered(final String databaseName, final ShardingSphereSchema schema) { String schemaName = schema.getName().toLowerCase(); - if (schema.getTables().isEmpty() && schema.getViews().isEmpty()) { + if (schema.isEmpty()) { add(databaseName, schemaName); } - tableMetaDataPersistService.persist(databaseName, schemaName, schema.getTables()); + tableMetaDataPersistService.persist(databaseName, schemaName, schema.getAllTables()); } /** @@ -106,7 +106,7 @@ public void alterByRuleAltered(final String databaseName, final ShardingSphereSc * @param schema to be altered schema */ public void alterByRuleDropped(final String databaseName, final String schemaName, final ShardingSphereSchema schema) { - tableMetaDataPersistService.persist(databaseName, schemaName, schema.getTables()); + tableMetaDataPersistService.persist(databaseName, schemaName, schema.getAllTables()); } /** diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistService.java index 397d9a802d783..0bb9c6fcbf6c0 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistService.java @@ -29,11 +29,8 @@ import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; -import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.stream.Collectors; /** @@ -53,9 +50,8 @@ public final class TableMetaDataPersistService { * @param schemaName to be loaded schema name * @return loaded tables */ - public Map load(final String databaseName, final String schemaName) { - List tableNames = repository.getChildrenKeys(TableMetaDataNode.getMetaDataTablesNode(databaseName, schemaName)); - return tableNames.stream().collect(Collectors.toMap(String::toLowerCase, each -> load(databaseName, schemaName, each), (a, b) -> b, () -> new LinkedHashMap<>(tableNames.size(), 1F))); + public Collection load(final String databaseName, final String schemaName) { + return repository.getChildrenKeys(TableMetaDataNode.getMetaDataTablesNode(databaseName, schemaName)).stream().map(each -> load(databaseName, schemaName, each)).collect(Collectors.toList()); } /** @@ -79,16 +75,14 @@ public ShardingSphereTable load(final String databaseName, final String schemaNa * @param schemaName to be persisted schema name * @param tables to be persisted tables */ - public void persist(final String databaseName, final String schemaName, final Map tables) { + public void persist(final String databaseName, final String schemaName, final Collection tables) { Collection metaDataVersions = new LinkedList<>(); - for (Entry entry : tables.entrySet()) { - String tableName = entry.getKey().toLowerCase(); + for (ShardingSphereTable each : tables) { + String tableName = each.getName().toLowerCase(); List versions = metaDataVersionPersistService.getVersions(TableMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName)); String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); - if (null != entry.getValue()) { - repository.persist(TableMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion), - YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(entry.getValue()))); - } + repository.persist( + TableMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion), YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(each))); if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, tableName))) { repository.persist(TableMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), MetaDataVersion.DEFAULT_VERSION); } @@ -119,7 +113,7 @@ public void drop(final String databaseName, final String schemaName, final Strin * @param schemaName to be dropped schema name * @param tables to be dropped tables */ - public void drop(final String databaseName, final String schemaName, final Map tables) { - tables.forEach((key, value) -> drop(databaseName, schemaName, key)); + public void drop(final String databaseName, final String schemaName, final Collection tables) { + tables.forEach(each -> drop(databaseName, schemaName, each.getName())); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistService.java index 68092d1b16a6e..07f5c2cc93758 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistService.java @@ -29,11 +29,8 @@ import org.apache.shardingsphere.mode.spi.PersistRepository; import java.util.Collection; -import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.stream.Collectors; /** @@ -53,9 +50,8 @@ public final class ViewMetaDataPersistService { * @param schemaName schema name * @return loaded views */ - public Map load(final String databaseName, final String schemaName) { - List viewNames = repository.getChildrenKeys(ViewMetaDataNode.getMetaDataViewsNode(databaseName, schemaName)); - return viewNames.stream().collect(Collectors.toMap(String::toLowerCase, each -> load(databaseName, schemaName, each), (a, b) -> b, () -> new LinkedHashMap<>(viewNames.size(), 1F))); + public Collection load(final String databaseName, final String schemaName) { + return repository.getChildrenKeys(ViewMetaDataNode.getMetaDataViewsNode(databaseName, schemaName)).stream().map(each -> load(databaseName, schemaName, each)).collect(Collectors.toList()); } /** @@ -79,14 +75,14 @@ public ShardingSphereView load(final String databaseName, final String schemaNam * @param schemaName schema name * @param views views */ - public void persist(final String databaseName, final String schemaName, final Map views) { + public void persist(final String databaseName, final String schemaName, final Collection views) { Collection metaDataVersions = new LinkedList<>(); - for (Entry entry : views.entrySet()) { - String viewName = entry.getKey().toLowerCase(); + for (ShardingSphereView each : views) { + String viewName = each.getName().toLowerCase(); List versions = metaDataVersionPersistService.getVersions(ViewMetaDataNode.getViewVersionsNode(databaseName, schemaName, viewName)); String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); repository.persist(ViewMetaDataNode.getViewVersionNode(databaseName, schemaName, viewName, nextActiveVersion), - YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(entry.getValue()))); + YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(each))); if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, viewName))) { repository.persist(ViewMetaDataNode.getViewActiveVersionNode(databaseName, schemaName, viewName), MetaDataVersion.DEFAULT_VERSION); } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java index 72d6ab3c9636c..a67484567a5e5 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/MetaDataPersistServiceTest.java @@ -151,7 +151,7 @@ void assertPersistReloadDatabaseByAlter() { when(GenericSchemaManager.getToBeAddedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_added", toBeAddedSchema)); metaDataPersistService.persistReloadDatabaseByAlter("foo_db", mock(ShardingSphereDatabase.class), mock(ShardingSphereDatabase.class)); verify(databaseMetaDataFacade.getSchema()).alterByRuleAltered("foo_db", toBeAddedSchema); - verify(databaseMetaDataFacade.getTable()).drop("foo_db", "to_be_deleted", Collections.emptyMap()); + verify(databaseMetaDataFacade.getTable()).drop("foo_db", "to_be_deleted", Collections.emptyList()); } @Test @@ -162,6 +162,6 @@ void assertPersistReloadDatabaseByDrop() { when(GenericSchemaManager.getToBeAddedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_altered", toBeAlterSchema)); metaDataPersistService.persistReloadDatabaseByDrop("foo_db", mock(ShardingSphereDatabase.class), mock(ShardingSphereDatabase.class)); verify(databaseMetaDataFacade.getSchema()).alterByRuleDropped("foo_db", "to_be_altered", toBeAlterSchema); - verify(databaseMetaDataFacade.getTable()).drop("foo_db", "to_be_deleted", Collections.emptyMap()); + verify(databaseMetaDataFacade.getTable()).drop("foo_db", "to_be_deleted", Collections.emptyList()); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistServiceTest.java index fd3fea4faf08f..abea1cc14cc7f 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistServiceTest.java @@ -36,6 +36,8 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -78,31 +80,36 @@ void assertDrop() { void assertAlterByRefreshWithoutTablesAndViews() { persistService.alterByRefresh("foo_db", new ShardingSphereSchema("foo_schema")); verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables", ""); - verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.emptyList()); } @Test void assertAlterByRefreshWithTables() { - Map tables = Collections.singletonMap("foo_tbl", mock(ShardingSphereTable.class)); - persistService.alterByRefresh("foo_db", new ShardingSphereSchema("foo_schema", tables, Collections.emptyMap())); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("foo_tbl"); + persistService.alterByRefresh("foo_db", new ShardingSphereSchema("foo_schema", Collections.singleton(table), Collections.emptyList())); verify(repository, times(0)).persist("/metadata/foo_db/schemas/foo_schema/tables", ""); - verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", tables); + verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.singletonList(table)); } @Test void assertAlterByRefreshWithViews() { - persistService.alterByRefresh("foo_db", new ShardingSphereSchema("foo_schema", Collections.emptyMap(), Collections.singletonMap("foo_view", mock(ShardingSphereView.class)))); + ShardingSphereView view = mock(ShardingSphereView.class); + when(view.getName()).thenReturn("foo_view"); + persistService.alterByRefresh("foo_db", new ShardingSphereSchema("foo_schema", Collections.emptyList(), Collections.singleton(view))); verify(repository, times(0)).persist("/metadata/foo_db/schemas/foo_schema/tables", ""); - verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.emptyList()); } @Test void assertLoad() { when(repository.getChildrenKeys("/metadata/foo_db/schemas")).thenReturn(Collections.singletonList("foo_schema")); ShardingSphereTable table = mock(ShardingSphereTable.class); - when(tableMetaDataPersistService.load("foo_db", "foo_schema")).thenReturn(Collections.singletonMap("foo_tbl", table)); + when(table.getName()).thenReturn("foo_tbl"); + when(tableMetaDataPersistService.load("foo_db", "foo_schema")).thenReturn(Collections.singleton(table)); ShardingSphereView view = mock(ShardingSphereView.class); - when(viewMetaDataPersistService.load("foo_db", "foo_schema")).thenReturn(Collections.singletonMap("foo_view", view)); + when(view.getName()).thenReturn("foo_view"); + when(viewMetaDataPersistService.load("foo_db", "foo_schema")).thenReturn(Collections.singleton(view)); Map actual = persistService.load("foo_db"); assertThat(actual.size(), is(1)); assertThat(actual.get("foo_schema").getTable("foo_tbl"), is(table)); @@ -113,27 +120,30 @@ void assertLoad() { void assertAlterSchemaByAlterConfigurationByRefresh() { persistService.alterByRuleAltered("foo_db", new ShardingSphereSchema("foo_schema")); verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables", ""); - verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(tableMetaDataPersistService).persist(eq("foo_db"), eq("foo_schema"), anyCollection()); } @Test void assertAlterSchemaByAlterConfigurationWithNotEmptyTablesByRefresh() { - Map tables = Collections.singletonMap("foo_tbl", mock(ShardingSphereTable.class)); - persistService.alterByRuleAltered("foo_db", new ShardingSphereSchema("foo_schema", tables, Collections.emptyMap())); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("foo_tbl"); + persistService.alterByRuleAltered("foo_db", new ShardingSphereSchema("foo_schema", Collections.singletonList(table), Collections.emptyList())); verify(repository, times(0)).persist("/metadata/foo_db/schemas/foo_schema/tables", ""); - verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", tables); + verify(tableMetaDataPersistService).persist(eq("foo_db"), eq("foo_schema"), anyCollection()); } @Test void assertAlterSchemaByAlterConfigurationWithNotEmptyViewsByRefresh() { - persistService.alterByRuleAltered("foo_db", new ShardingSphereSchema("foo_schema", Collections.emptyMap(), Collections.singletonMap("foo_view", mock(ShardingSphereView.class)))); + ShardingSphereView view = mock(ShardingSphereView.class); + when(view.getName()).thenReturn("foo_view"); + persistService.alterByRuleAltered("foo_db", new ShardingSphereSchema("foo_schema", Collections.emptyList(), Collections.singleton(view))); verify(repository, times(0)).persist("/metadata/foo_db/schemas/foo_schema/tables", ""); - verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(tableMetaDataPersistService).persist(eq("foo_db"), eq("foo_schema"), anyCollection()); } @Test void assertAlterByRefreshByDropConfiguration() { persistService.alterByRuleDropped("foo_db", "foo_schema", mock(ShardingSphereSchema.class)); - verify(tableMetaDataPersistService).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(tableMetaDataPersistService).persist(eq("foo_db"), eq("foo_schema"), anyCollection()); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistServiceTest.java index fc7d6d95806d3..1113287ff88e2 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/TableMetaDataPersistServiceTest.java @@ -26,8 +26,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collection; import java.util.Collections; -import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -54,16 +54,18 @@ void assertLoad() { when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/tables")).thenReturn(Collections.singletonList("foo_tbl")); when(repository.query("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version")).thenReturn("0"); when(repository.query("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/versions/0")).thenReturn("{name: foo_tbl}"); - Map actual = persistService.load("foo_db", "foo_schema"); + Collection actual = persistService.load("foo_db", "foo_schema"); assertThat(actual.size(), is(1)); - assertThat(actual.get("foo_tbl").getName(), is("foo_tbl")); + assertThat(actual.iterator().next().getName(), is("foo_tbl")); } @Test void assertPersistWithoutVersion() { when(repository.query("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version")).thenReturn("", "0"); - persistService.persist("foo_db", "foo_schema", Collections.singletonMap("foo_tbl", mock(ShardingSphereTable.class))); - verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/versions/0", "{}" + System.lineSeparator()); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("foo_tbl"); + persistService.persist("foo_db", "foo_schema", Collections.singleton(table)); + verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/versions/0", "name: foo_tbl" + System.lineSeparator()); verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version", "0"); } @@ -71,8 +73,10 @@ void assertPersistWithoutVersion() { void assertPersistWithVersion() { when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/versions")).thenReturn(Collections.singletonList("10")); when(repository.query("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version")).thenReturn("10"); - persistService.persist("foo_db", "foo_schema", Collections.singletonMap("foo_tbl", mock(ShardingSphereTable.class))); - verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/versions/11", "{}" + System.lineSeparator()); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("foo_tbl"); + persistService.persist("foo_db", "foo_schema", Collections.singleton(table)); + verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/versions/11", "name: foo_tbl" + System.lineSeparator()); verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version", "11"); } @@ -84,7 +88,9 @@ void assertDropTable() { @Test void assertDropTables() { - persistService.drop("foo_db", "foo_schema", Collections.singletonMap("foo_tbl", mock(ShardingSphereTable.class))); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("foo_tbl"); + persistService.drop("foo_db", "foo_schema", Collections.singleton(table)); verify(repository).delete("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl"); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistServiceTest.java index 2d597508fac38..6a1747d20e539 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistServiceTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/metadata/table/ViewMetaDataPersistServiceTest.java @@ -26,8 +26,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collection; import java.util.Collections; -import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -54,16 +54,18 @@ void assertLoad() { when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/views")).thenReturn(Collections.singletonList("foo_view")); when(repository.query("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")).thenReturn("0"); when(repository.query("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/0")).thenReturn("{name: foo_view}"); - Map actual = persistService.load("foo_db", "foo_schema"); + Collection actual = persistService.load("foo_db", "foo_schema"); assertThat(actual.size(), is(1)); - assertThat(actual.get("foo_view").getName(), is("foo_view")); + assertThat(actual.iterator().next().getName(), is("foo_view")); } @Test void assertPersistWithoutVersion() { when(repository.query("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")).thenReturn("", "0"); - persistService.persist("foo_db", "foo_schema", Collections.singletonMap("foo_view", mock(ShardingSphereView.class))); - verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/0", "{}" + System.lineSeparator()); + ShardingSphereView view = mock(ShardingSphereView.class); + when(view.getName()).thenReturn("foo_view"); + persistService.persist("foo_db", "foo_schema", Collections.singleton(view)); + verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/0", "name: foo_view" + System.lineSeparator()); verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version", "0"); } @@ -71,8 +73,10 @@ void assertPersistWithoutVersion() { void assertPersistWithVersion() { when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions")).thenReturn(Collections.singletonList("10")); when(repository.query("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")).thenReturn("10"); - persistService.persist("foo_db", "foo_schema", Collections.singletonMap("foo_view", mock(ShardingSphereView.class))); - verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/11", "{}" + System.lineSeparator()); + ShardingSphereView view = mock(ShardingSphereView.class); + when(view.getName()).thenReturn("foo_view"); + persistService.persist("foo_db", "foo_schema", Collections.singleton(view)); + verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view/versions/11", "name: foo_view" + System.lineSeparator()); verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version", "11"); } diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java index bf62b88c3871b..4302b72872e93 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java @@ -72,7 +72,7 @@ private void checkTables(final UnloadSingleTableStatement sqlStatement) { private Collection getAllTableNames(final ShardingSphereDatabase database) { String defaultSchemaName = new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(database.getName()); - return database.getSchema(defaultSchemaName).getTables().values().stream().map(ShardingSphereTable::getName).collect(Collectors.toList()); + return database.getSchema(defaultSchemaName).getAllTables().stream().map(ShardingSphereTable::getName).collect(Collectors.toList()); } private void checkTableExist(final Collection allTables, final String tableName) { diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java index bc44ef0d702be..c5e4e585567d6 100644 --- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java +++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java @@ -266,7 +266,7 @@ private void registerTableScanExecutor(final Schema sqlFederationSchema, final D EnumerableScanExecutor scanExecutor = new EnumerableScanExecutor(prepareEngine, jdbcExecutor, callback, optimizerContext, executorContext, federationContext, metaData.getGlobalRuleMetaData(), statistics); // TODO register only the required tables - for (ShardingSphereTable each : metaData.getDatabase(databaseName).getSchema(schemaName).getTables().values()) { + for (ShardingSphereTable each : metaData.getDatabase(databaseName).getSchema(schemaName).getAllTables()) { Table table = sqlFederationSchema.getTable(each.getName()); if (table instanceof SQLFederationTable) { ((SQLFederationTable) table).setScanExecutor(scanExecutor); diff --git a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/metadata/schema/SQLFederationSchema.java b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/metadata/schema/SQLFederationSchema.java index abdbcfb60cfef..73e1f141ef340 100644 --- a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/metadata/schema/SQLFederationSchema.java +++ b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/metadata/schema/SQLFederationSchema.java @@ -51,8 +51,8 @@ public SQLFederationSchema(final String schemaName, final ShardingSphereSchema s } private Map createTableMap(final ShardingSphereSchema schema, final DatabaseType protocolType, final JavaTypeFactory javaTypeFactory) { - Map result = new CaseInsensitiveMap<>(schema.getTables().size(), 1F); - for (ShardingSphereTable each : schema.getTables().values()) { + Map result = new CaseInsensitiveMap<>(schema.getAllTables().size(), 1F); + for (ShardingSphereTable each : schema.getAllTables()) { if (schema.containsView(each.getName())) { result.put(each.getName(), getViewTable(schema, each, protocolType, javaTypeFactory)); } else { diff --git a/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java b/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java index ea98d7a4d471b..d9c24fdaf017d 100644 --- a/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java +++ b/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java @@ -49,9 +49,9 @@ import java.io.IOException; import java.sql.Types; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.util.LinkedList; import java.util.Properties; import java.util.stream.Stream; @@ -69,20 +69,20 @@ class SQLStatementCompilerIT { @BeforeEach void init() { - Map tables = new HashMap<>(12, 1F); - tables.put("t_order_federate", createOrderFederationTableMetaData()); - tables.put("t_user_info", createUserInfoTableMetaData()); - tables.put("t_order", createTOrderTableMetaData()); - tables.put("t_order_item", createTOrderItemTableMetaData()); - tables.put("t_single_table", createTSingleTableMetaData()); - tables.put("t_order_federate_sharding", createTOrderFederateShardingMetaData()); - tables.put("t_order_item_federate_sharding", createTOrderItemFederateShardingMetaData()); - tables.put("t_merchant", createTMerchantMetaData()); - tables.put("t_product", createTProductMetaData()); - tables.put("t_product_detail", createTProductDetailMetaData()); - tables.put("multi_types_first", createMultiTypesFirstTableMetaData()); - tables.put("multi_types_second", createMultiTypesSecondTableMetaData()); - sqlStatementCompiler = new SQLStatementCompiler(createSqlToRelConverter(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()))); + Collection tables = new LinkedList<>(); + tables.add(createOrderFederationTableMetaData()); + tables.add(createUserInfoTableMetaData()); + tables.add(createTOrderTableMetaData()); + tables.add(createTOrderItemTableMetaData()); + tables.add(createTSingleTableMetaData()); + tables.add(createTOrderFederateShardingMetaData()); + tables.add(createTOrderItemFederateShardingMetaData()); + tables.add(createTMerchantMetaData()); + tables.add(createTProductMetaData()); + tables.add(createTProductDetailMetaData()); + tables.add(createMultiTypesFirstTableMetaData()); + tables.add(createMultiTypesSecondTableMetaData()); + sqlStatementCompiler = new SQLStatementCompiler(createSqlToRelConverter(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList()))); } private ShardingSphereTable createOrderFederationTableMetaData() { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index 8fd5f189df8fd..2d01b34e15eed 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -127,7 +127,7 @@ public Map getStorageUnits(final String databaseName) { public void reloadSchema(final ShardingSphereDatabase database, final String schemaName, final String dataSourceName) { try { ShardingSphereSchema reloadedSchema = loadSchema(database, schemaName, dataSourceName); - if (reloadedSchema.getTables().isEmpty()) { + if (reloadedSchema.getAllTables().isEmpty()) { database.dropSchema(schemaName); persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getSchema().drop(database.getName(), schemaName); } else { @@ -146,7 +146,7 @@ private ShardingSphereSchema loadSchema(final ShardingSphereDatabase database, f Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnits().get(dataSourceName).getDataSource()), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName); ShardingSphereSchema result = GenericSchemaBuilder.build(material).get(schemaName); - result.getViews().putAll(persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(database.getName(), schemaName)); + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(database.getName(), schemaName).forEach(result::putView); return result; } @@ -189,8 +189,7 @@ public void reloadTable(final ShardingSphereDatabase database, final String sche private void persistTable(final ShardingSphereDatabase database, final String schemaName, final String tableName, final GenericSchemaBuilderMaterial material) throws SQLException { ShardingSphereSchema schema = GenericSchemaBuilder.build(Collections.singleton(tableName), material).getOrDefault(schemaName, new ShardingSphereSchema(schemaName)); - persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getTable() - .persist(database.getName(), schemaName, Collections.singletonMap(tableName, schema.getTable(tableName))); + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().persist(database.getName(), schemaName, Collections.singleton(schema.getTable(tableName))); } /** diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java index b556082f375b0..61c5ecb25907b 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java @@ -123,7 +123,7 @@ public void forceRefreshDatabaseMetaData(final ShardingSphereDatabase database) if (schema.isEmpty()) { metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(database.getName(), schemaName); } - metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(database.getName(), schemaName, schema.getTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(database.getName(), schemaName, schema.getAllTables()); }); } catch (final SQLException ex) { log.error("Refresh database meta data: {} failed", database.getName(), ex); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java index 122372c7b7851..99e9cd6d74df2 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java @@ -242,7 +242,7 @@ private static void persistMetaData(final MetaDataContexts metaDataContexts, fin if (schema.isEmpty()) { persistService.getDatabaseMetaDataFacade().getSchema().add(each.getName(), schemaName); } - persistService.getDatabaseMetaDataFacade().getTable().persist(each.getName(), schemaName, schema.getTables()); + persistService.getDatabaseMetaDataFacade().getTable().persist(each.getName(), schemaName, schema.getAllTables()); })); for (Entry databaseDataEntry : metaDataContexts.getStatistics().getDatabaseData().entrySet()) { for (Entry schemaDataEntry : databaseDataEntry.getValue().getSchemaData().entrySet()) { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java index f6f66a4d80e0a..390c2a1d1c12b 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java @@ -115,7 +115,7 @@ private void refreshMetadata(final String databaseName, final ShardingSphereData private Map buildShardingSphereSchemas(final ShardingSphereDatabase database) { Map result = new LinkedHashMap<>(database.getSchemas().size(), 1F); - database.getSchemas().forEach((key, value) -> result.put(key, new ShardingSphereSchema(key, value.getTables(), value.getViews()))); + database.getSchemas().forEach((key, value) -> result.put(key, new ShardingSphereSchema(key, value.getAllTables(), value.getAllViews()))); return result; } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java index 1c55b920b4b83..a6a38f29d8dee 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/StorageUnitManager.java @@ -121,8 +121,8 @@ private Map buildShardingSphereDatabase(final Sh private Map buildSchemas(final ShardingSphereDatabase originalDatabase) { Map result = new LinkedHashMap<>(originalDatabase.getSchemas().size(), 1F); - originalDatabase.getSchemas().keySet().forEach(schemaName -> result.put(schemaName.toLowerCase(), new ShardingSphereSchema( - schemaName, originalDatabase.getSchema(schemaName).getTables(), metaDataPersistService.getDatabaseMetaDataFacade().getView().load(originalDatabase.getName(), schemaName)))); + originalDatabase.getSchemas().keySet().forEach(schemaName -> result.put(schemaName.toLowerCase(), new ShardingSphereSchema(schemaName, originalDatabase.getSchema(schemaName).getAllTables(), + metaDataPersistService.getDatabaseMetaDataFacade().getView().load(originalDatabase.getName(), schemaName)))); return result; } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index 5797530910f85..3cb6770fd7d42 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; @@ -50,6 +51,9 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Collections; import java.util.HashMap; import java.util.Optional; @@ -79,7 +83,7 @@ class ContextManagerTest { private ContextManager contextManager; @BeforeEach - void setUp() { + void setUp() throws SQLException { when(metaDataContexts.getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties())); ShardingSphereDatabase database = mockDatabase(); when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true); @@ -90,7 +94,7 @@ void setUp() { contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); } - private ShardingSphereDatabase mockDatabase() { + private ShardingSphereDatabase mockDatabase() throws SQLException { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(result.getName()).thenReturn("foo_db"); when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); @@ -100,9 +104,17 @@ private ShardingSphereDatabase mockDatabase() { when(rule.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute)); when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); when(result.containsSchema("foo_schema")).thenReturn(true); - when(result.getSchemas()).thenReturn(new HashMap<>(Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME)))); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(table.getName()).thenReturn("foo_tbl"); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(table), Collections.emptyList()); + when(result.getSchemas()).thenReturn(new HashMap<>(Collections.singletonMap("foo_schema", schema))); StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS); when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS); + ResultSet resultSet = mock(ResultSet.class); + when(resultSet.next()).thenReturn(true, false); + when(connection.getMetaData().getTables(null, null, "foo_tbl", null)).thenReturn(resultSet); + when(storageUnit.getDataSource()).thenReturn(new MockedDataSource(connection)); when(result.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); return result; } @@ -139,7 +151,7 @@ void assertGetStorageUnits() { } @Test - void assertReloadSchema() { + void assertReloadSchema() throws SQLException { when(metaDataContexts.getMetaData().getDatabase("foo_db").getName()).thenReturn("foo_db"); ShardingSphereDatabase database = mockDatabase(); contextManager.reloadSchema(database, "foo_schema", "foo_ds"); @@ -147,16 +159,16 @@ void assertReloadSchema() { } @Test - void assertReloadTable() { + void assertReloadTable() throws SQLException { ShardingSphereDatabase database = mockDatabase(); - contextManager.reloadTable(database, "foo_schema", "foo_table"); + contextManager.reloadTable(database, "foo_schema", "foo_tbl"); assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnits().containsKey("foo_ds")); } @Test - void assertReloadTableWithDataSourceName() { + void assertReloadTableWithDataSourceName() throws SQLException { ShardingSphereDatabase database = mockDatabase(); - contextManager.reloadTable(database, "foo_schema", "foo_ds", "foo_table"); + contextManager.reloadTable(database, "foo_schema", "foo_ds", "foo_tbl"); assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnits().containsKey("foo_ds")); } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManagerTest.java index 11a304d6974c5..0e1483ebb7831 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManagerTest.java @@ -158,7 +158,7 @@ void assertAlterSchemaForTableAltered() { ShardingSphereColumn toBeChangedColumn = new ShardingSphereColumn("foo_col", Types.VARCHAR, false, false, false, true, false, false); ShardingSphereTable toBeChangedTable = new ShardingSphereTable("foo_tbl", Collections.singleton(toBeChangedColumn), Collections.emptyList(), Collections.emptyList()); schemaMetaDataManager.alterSchema("foo_db", "foo_schema", toBeChangedTable, null); - ShardingSphereTable table = metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getTables().get("foo_tbl"); + ShardingSphereTable table = metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getTable("foo_tbl"); assertThat(table.getAllColumns().size(), is(1)); assertTrue(table.containsColumn("foo_col")); } @@ -197,19 +197,19 @@ void assertAlterSchemaForNothingTableDropped() { void assertAlterSchemaForTableDropped() { when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", createToBeAlteredSchema())); schemaMetaDataManager.alterSchema("foo_db", "foo_schema", "foo_tbl", null); - assertFalse(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getTables().containsKey("foo_tbl")); + assertFalse(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").containsTable("foo_tbl")); } @Test void assertAlterSchemaForViewDropped() { when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", createToBeAlteredSchema())); schemaMetaDataManager.alterSchema("foo_db", "foo_schema", "foo_view", null); - assertFalse(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getViews().containsKey("foo_view")); + assertFalse(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").containsView("foo_view")); } private ShardingSphereSchema createToBeAlteredSchema() { ShardingSphereTable beforeChangedTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - ShardingSphereView beforeChangedView = new ShardingSphereView("foo_tbl", ""); - return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("foo_tbl", beforeChangedTable), Collections.singletonMap("foo_view", beforeChangedView)); + ShardingSphereView beforeChangedView = new ShardingSphereView("foo_view", ""); + return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singleton(beforeChangedTable), Collections.singleton(beforeChangedView)); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java index afc9124493577..903d86224aa0e 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistService.java @@ -17,14 +17,12 @@ package org.apache.shardingsphere.mode.manager.cluster.persist; -import org.apache.groovy.util.Maps; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO; import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO; import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; @@ -95,8 +93,8 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { if (schema.isEmpty()) { metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, alterSchemaPOJO.getRenameSchemaName()); } - metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getTables()); - metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getViews()); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getAllTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, alterSchemaPOJO.getRenameSchemaName(), schema.getAllViews()); metaDataPersistService.getDatabaseMetaDataFacade().getSchema().drop(databaseName, schemaName); } @@ -109,11 +107,9 @@ public void dropSchema(final String databaseName, final Collection schem public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO) { String databaseName = alterSchemaMetaDataPOJO.getDatabaseName(); String schemaName = alterSchemaMetaDataPOJO.getSchemaName(); - Map tables = alterSchemaMetaDataPOJO.getAlteredTables().stream().collect(Collectors.toMap(ShardingSphereTable::getName, table -> table)); - Map views = alterSchemaMetaDataPOJO.getAlteredViews().stream().collect(Collectors.toMap(ShardingSphereView::getName, view -> view)); DatabaseMetaDataPersistFacade databaseMetaDataFacade = metaDataPersistService.getDatabaseMetaDataFacade(); - databaseMetaDataFacade.getTable().persist(databaseName, schemaName, tables); - databaseMetaDataFacade.getView().persist(databaseName, schemaName, views); + databaseMetaDataFacade.getTable().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredTables()); + databaseMetaDataFacade.getView().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredViews()); alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataFacade.getTable().drop(databaseName, schemaName, each)); alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataFacade.getView().delete(databaseName, schemaName, each)); } @@ -259,7 +255,7 @@ public void alterProperties(final Properties props) { @Override public void createTable(final String databaseName, final String schemaName, final ShardingSphereTable table, final String logicDataSourceName) { - metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, Maps.of(table.getName(), table)); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, Collections.singleton(table)); } @Override diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java index 670042b24ab91..47feac6319c60 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/ClusterMetaDataManagerPersistServiceTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.mode.manager.cluster.persist; import lombok.SneakyThrows; -import org.apache.groovy.util.Maps; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; @@ -98,7 +97,8 @@ void assertCreateSchema() { @Test void assertAlterNotEmptySchema() { - assertAlterSchema(mock(ShardingSphereSchema.class)); + ShardingSphereSchema schema = mock(ShardingSphereSchema.class); + assertAlterSchema(schema); verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema(), times(0)).add("foo_db", "bar_schema"); } @@ -113,8 +113,8 @@ void assertAlterEmptySchema() { private void assertAlterSchema(final ShardingSphereSchema schema) { when(metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(schema); metaDataManagerPersistService.alterSchema(new AlterSchemaPOJO("foo_db", "foo_schema", "bar_schema", Collections.singleton("foo_ds"))); - verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "bar_schema", Collections.emptyMap()); - verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist("foo_db", "bar_schema", Collections.emptyMap()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist(eq("foo_db"), eq("bar_schema"), anyCollection()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist(eq("foo_db"), eq("bar_schema"), anyCollection()); verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).drop("foo_db", "foo_schema"); } @@ -127,8 +127,8 @@ void assertDropSchema() { @Test void assertAlterSchemaMetaData() { metaDataManagerPersistService.alterSchemaMetaData(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds"))); - verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Collections.emptyMap()); - verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist("foo_db", "foo_schema", Collections.emptyMap()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Collections.emptyList()); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist("foo_db", "foo_schema", Collections.emptyList()); } @Test @@ -189,9 +189,8 @@ void assertAlterProperties() { @Test void assertCreateTable() { ShardingSphereTable table = mock(ShardingSphereTable.class); - when(table.getName()).thenReturn("foo_tbl"); metaDataManagerPersistService.createTable("foo_db", "foo_schema", table, "foo_ds"); - verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Maps.of("foo_tbl", table)); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Collections.singleton(table)); } @Test diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java index febe330f39b95..09abed96a5ada 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistService.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.mode.manager.standalone.persist; import com.google.common.base.Strings; -import org.apache.groovy.util.Maps; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; @@ -56,10 +55,8 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.Map; -import java.util.Map.Entry; import java.util.Optional; import java.util.Properties; -import java.util.stream.Collectors; /** * Standalone meta data manager persist service. @@ -114,8 +111,8 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) { if (alteredSchema.isEmpty()) { metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, alteredSchemaName); } - metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, alteredSchemaName, alteredSchema.getTables()); - metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, alteredSchemaName, alteredSchema.getViews()); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, alteredSchemaName, alteredSchema.getAllTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, alteredSchemaName, alteredSchema.getAllViews()); metaDataPersistService.getDatabaseMetaDataFacade().getSchema().drop(databaseName, alterSchemaPOJO.getSchemaName()); } @@ -133,34 +130,34 @@ private void addDataNode(final ShardingSphereDatabase database, final String log }); } - private void addDataNode(final ShardingSphereDatabase database, final String logicDataSourceName, final String schemaName, final Map toBeAddedTables, - final Map toBeAddedViews) { + private void addDataNode(final ShardingSphereDatabase database, final String logicDataSourceName, final String schemaName, final Collection toBeAddedTables, + final Collection toBeAddedViews) { addTablesToDataNode(database, schemaName, logicDataSourceName, toBeAddedTables); addViewsToDataNode(database, schemaName, logicDataSourceName, toBeAddedTables, toBeAddedViews); } - private void addTablesToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, final Map toBeAddedTables) { - for (Entry entry : toBeAddedTables.entrySet()) { - if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) { - database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey())); + private void addTablesToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, final Collection toBeAddedTables) { + for (ShardingSphereTable each : toBeAddedTables) { + if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(each.getName(), database)) { + database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, each.getName())); } - database.getSchema(schemaName).putTable(entry.getValue()); + database.getSchema(schemaName).putTable(each); } } private void addViewsToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, - final Map toBeAddedTables, final Map toBeAddedViews) { - for (Entry entry : toBeAddedViews.entrySet()) { - if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) { - database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceName, schemaName, entry.getKey())); + final Collection toBeAddedTables, final Collection toBeAddedViews) { + for (ShardingSphereView view : toBeAddedViews) { + if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(view.getName(), database)) { + database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceName, schemaName, view.getName())); } - Optional.ofNullable(toBeAddedTables.get(entry.getKey().toLowerCase())).ifPresent(optional -> database.getSchema(schemaName).putTable(optional)); - database.getSchema(schemaName).putView(entry.getValue()); + toBeAddedTables.stream().filter(each -> each.getName().toLowerCase().equals(view.getName())).findFirst().ifPresent(optional -> database.getSchema(schemaName).putTable(optional)); + database.getSchema(schemaName).putView(view); } } private void removeSchemaMetaData(final ShardingSphereDatabase database, final String schemaName) { - ShardingSphereSchema schema = new ShardingSphereSchema(schemaName, database.getSchema(schemaName).getTables(), database.getSchema(schemaName).getViews()); + ShardingSphereSchema schema = new ShardingSphereSchema(schemaName, database.getSchema(schemaName).getAllTables(), database.getSchema(schemaName).getAllViews()); database.dropSchema(schemaName); removeDataNode(database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class), Collections.singletonList(schemaName), schema.getAllTableNames()); } @@ -197,7 +194,7 @@ public void dropSchema(final String databaseName, final Collection schem ShardingSphereMetaData metaData = metaDataContextManager.getMetaDataContexts().get().getMetaData(); ShardingSphereDatabase database = metaData.getDatabase(databaseName); for (String each : schemaNames) { - ShardingSphereSchema schema = new ShardingSphereSchema(each, database.getSchema(each).getTables(), database.getSchema(each).getViews()); + ShardingSphereSchema schema = new ShardingSphereSchema(each, database.getSchema(each).getAllTables(), database.getSchema(each).getAllViews()); database.dropSchema(each); Optional.of(schema).ifPresent(optional -> tobeRemovedTables.addAll(optional.getAllTableNames())); tobeRemovedSchemas.add(each.toLowerCase()); @@ -212,13 +209,11 @@ public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDat String schemaName = alterSchemaMetaDataPOJO.getSchemaName(); ShardingSphereMetaData metaData = metaDataContextManager.getMetaDataContexts().get().getMetaData(); ShardingSphereDatabase database = metaData.getDatabase(databaseName); - Map tables = alterSchemaMetaDataPOJO.getAlteredTables().stream().collect(Collectors.toMap(ShardingSphereTable::getName, table -> table)); - Map views = alterSchemaMetaDataPOJO.getAlteredViews().stream().collect(Collectors.toMap(ShardingSphereView::getName, view -> view)); - addDataNode(database, alterSchemaMetaDataPOJO.getLogicDataSourceName(), schemaName, tables, views); + addDataNode(database, alterSchemaMetaDataPOJO.getLogicDataSourceName(), schemaName, alterSchemaMetaDataPOJO.getAlteredTables(), alterSchemaMetaDataPOJO.getAlteredViews()); removeDataNode(database, schemaName, alterSchemaMetaDataPOJO.getDroppedTables(), alterSchemaMetaDataPOJO.getDroppedViews()); metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, tables); - metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, schemaName, views); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredTables()); + metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredViews()); alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getTable().drop(databaseName, schemaName, each)); alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getView().delete(databaseName, schemaName, each)); } @@ -237,7 +232,7 @@ public void registerStorageUnits(final String databaseName, final Map ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); - metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, Maps.of(table.getName(), table)); + metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, Collections.singleton(table)); } @Override diff --git a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java index 059bafaf5f84f..4952148407a37 100644 --- a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java +++ b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.mode.manager.standalone.persist; import lombok.SneakyThrows; -import org.apache.groovy.util.Maps; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType; @@ -51,12 +50,12 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedList; import java.util.Optional; import java.util.Properties; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; @@ -112,8 +111,8 @@ void assertAlterSchemaWithEmptyAlteredSchema() { when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); metaDataManagerPersistService.alterSchema(new AlterSchemaPOJO("foo_db", "foo_schema", "bar_schema", Collections.singleton("foo_ds"))); verify(databaseMetaDataFacade.getSchema(), times(0)).add("foo_db", "bar_schema"); - verify(databaseMetaDataFacade.getTable()).persist("foo_db", "bar_schema", new HashMap<>()); - verify(databaseMetaDataFacade.getView()).persist("foo_db", "bar_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getTable()).persist(eq("foo_db"), eq("bar_schema"), anyCollection()); + verify(databaseMetaDataFacade.getView()).persist(eq("foo_db"), eq("bar_schema"), anyCollection()); verify(databaseMetaDataFacade.getSchema()).drop("foo_db", "foo_schema"); } @@ -131,8 +130,8 @@ void assertAlterSchemaWithNotEmptyAlteredSchema() { when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); metaDataManagerPersistService.alterSchema(new AlterSchemaPOJO("foo_db", "foo_schema", "bar_schema", Collections.singleton("foo_ds"))); verify(databaseMetaDataFacade.getSchema()).add("foo_db", "bar_schema"); - verify(databaseMetaDataFacade.getTable()).persist("foo_db", "bar_schema", new HashMap<>()); - verify(databaseMetaDataFacade.getView()).persist("foo_db", "bar_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getTable()).persist(eq("foo_db"), eq("bar_schema"), anyCollection()); + verify(databaseMetaDataFacade.getView()).persist(eq("foo_db"), eq("bar_schema"), anyCollection()); verify(databaseMetaDataFacade.getSchema()).drop("foo_db", "foo_schema"); } @@ -151,7 +150,7 @@ void assertAlterSchemaMetaData() { DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS); when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); metaDataManagerPersistService.alterSchemaMetaData(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds"))); - verify(databaseMetaDataFacade.getTable()).persist("foo_db", "foo_schema", new HashMap<>()); + verify(databaseMetaDataFacade.getTable()).persist("foo_db", "foo_schema", new LinkedList<>()); } @Test @@ -232,7 +231,7 @@ void assertAlterProperties() { void assertCreateTable() { ShardingSphereTable table = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE); metaDataManagerPersistService.createTable("foo_db", "foo_schema", table, "foo_ds"); - verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Maps.of("foo_tbl", table)); + verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Collections.singleton(table)); } @Test diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLQueryBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLQueryBackendHandlerTest.java index ee923a390f4a7..32aa3e020b6b0 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLQueryBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLQueryBackendHandlerTest.java @@ -50,7 +50,6 @@ import java.util.Collection; import java.util.Collections; -import java.util.Map; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -91,15 +90,15 @@ void assertExecute() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("foo_db"); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - when(database.getSchema("foo_db")).thenReturn(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, createTableMap(), Collections.emptyMap())); + when(database.getSchema("foo_db")).thenReturn(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, createTables(), Collections.emptyList())); when(ProxyContext.getInstance().getContextManager().getDatabase("foo_db")).thenReturn(database); assertDoesNotThrow(() -> new DistSQLQueryBackendHandler(createSqlStatement(), mock(ConnectionSession.class, RETURNS_DEEP_STUBS)).execute()); } - private Map createTableMap() { + private Collection createTables() { Collection columns = Collections.singletonList(new ShardingSphereColumn("order_id", 0, false, false, false, true, false, false)); Collection indexes = Collections.singletonList(new ShardingSphereIndex("primary", Collections.emptyList(), false)); - return Collections.singletonMap("t_order", new ShardingSphereTable("t_order", columns, indexes, Collections.emptyList())); + return Collections.singleton(new ShardingSphereTable("t_order", columns, indexes, Collections.emptyList())); } private ShowTableMetaDataStatement createSqlStatement() { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowTableMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowTableMetaDataExecutorTest.java index 57a50b9087e06..710d1bd78b111 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowTableMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowTableMetaDataExecutorTest.java @@ -39,7 +39,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; -import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -78,14 +77,14 @@ private ShardingSphereDatabase mockDatabase() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); when(result.getName()).thenReturn("foo_db"); - when(result.getSchema("foo_db")).thenReturn(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, createTableMap(), Collections.emptyMap())); + when(result.getSchema("foo_db")).thenReturn(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, createTables(), Collections.emptyList())); return result; } - private Map createTableMap() { + private Collection createTables() { Collection columns = Collections.singletonList(new ShardingSphereColumn("order_id", 0, false, false, false, true, false, false)); Collection indexes = Collections.singletonList(new ShardingSphereIndex("primary", Collections.emptyList(), false)); - return Collections.singletonMap("t_order", new ShardingSphereTable("t_order", columns, indexes, Collections.emptyList())); + return Collections.singleton(new ShardingSphereTable("t_order", columns, indexes, Collections.emptyList())); } private ShowTableMetaDataStatement createSqlStatement() { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java index fb70225ba8a3f..8a194212844ad 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java @@ -129,7 +129,7 @@ private ContextManager mockContextManager(final String feature) { .thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "MySQL")))); if (null != feature) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, createTables(), Collections.emptyMap())); + when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, createTables(), Collections.emptyList())); Map storageUnits = createStorageUnits(); when(database.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); when(result.getMetaDataContexts().getMetaData().getDatabases()).thenReturn(Collections.singletonMap(feature, database)); @@ -149,9 +149,9 @@ private Map createStorageUnits() { return result; } - private Map createTables() { + private Collection createTables() { Collection columns = Collections.singleton(new ShardingSphereColumn("order_id", 0, false, false, false, true, false, false)); Collection indexes = Collections.singleton(new ShardingSphereIndex("primary", Collections.emptyList(), false)); - return Collections.singletonMap("t_order", new ShardingSphereTable("t_order", columns, indexes, Collections.emptyList())); + return Collections.singletonList(new ShardingSphereTable("t_order", columns, indexes, Collections.emptyList())); } } diff --git a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutor.java b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutor.java index d0c3a332c7ddf..20f68183d38e5 100644 --- a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutor.java +++ b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutor.java @@ -99,7 +99,7 @@ private Collection getTables(final String databaseName) { if (null == ProxyContext.getInstance().getContextManager().getDatabase(databaseName).getSchema(databaseName)) { return Collections.emptyList(); } - Collection tables = ProxyContext.getInstance().getContextManager().getDatabase(databaseName).getSchema(databaseName).getTables().values(); + Collection tables = ProxyContext.getInstance().getContextManager().getDatabase(databaseName).getSchema(databaseName).getAllTables(); Collection filteredTables = filterByLike(tables); return filteredTables.stream().sorted(Comparator.comparing(ShardingSphereTable::getName)).collect(Collectors.toList()); } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java index d6b0330bda60f..86fa671fee2db 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java @@ -51,8 +51,10 @@ import org.mockito.quality.Strictness; import java.sql.SQLException; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -204,12 +206,12 @@ private ContextManager mockContextManager(final Map getDatabases() { - Map tables = new HashMap<>(4, 1F); - tables.put("t_account", new ShardingSphereTable("t_account", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - tables.put("t_account_bak", new ShardingSphereTable("t_account_bak", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - tables.put("t_account_detail", new ShardingSphereTable("t_account_detail", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - tables.put("t_test", new ShardingSphereTable("T_TEST", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()); + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("t_account", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + tables.add(new ShardingSphereTable("t_account_bak", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + tables.add(new ShardingSphereTable("t_account_detail", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + tables.add(new ShardingSphereTable("T_TEST", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList()); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getSchema(String.format(DATABASE_PATTERN, 0))).thenReturn(schema); when(database.isComplete()).thenReturn(true); diff --git a/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java b/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java index 210d89cb4e287..30327ee5d773b 100644 --- a/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java +++ b/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java @@ -137,7 +137,7 @@ private Map createShardingSphereDatabaseMap() { new ShardingSphereColumn("datfrozenxid64", 1111, false, false, false, true, false, false), new ShardingSphereColumn("datminmxid", 1111, false, false, false, true, false, false)); ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, - Collections.singletonMap("pg_database", new ShardingSphereTable("pg_database", columns, Collections.emptyList(), Collections.emptyList())), Collections.emptyMap()); + Collections.singleton(new ShardingSphereTable("pg_database", columns, Collections.emptyList(), Collections.emptyList())), Collections.emptyList()); result.put("sharding_db", new ShardingSphereDatabase("sharding_db", TypedSPILoader.getService(DatabaseType.class, "openGauss"), mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), mock(RuleMetaData.class), Collections.singletonMap("pg_catalog", schema))); return result; diff --git a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutorTest.java b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutorTest.java index 9580dd3958ec3..33d77217b1be5 100644 --- a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutorTest.java +++ b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutorTest.java @@ -233,7 +233,7 @@ private ContextManager mockContextManager() { new ShardingSphereColumn("name", Types.VARCHAR, false, false, false, false, false, false), new ShardingSphereColumn("age", Types.SMALLINT, false, false, false, false, true, false)), Collections.emptyList(), Collections.emptyList()); ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME); - schema.getTables().put("user", table); + schema.putTable(table); ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", TypedSPILoader.getService(DatabaseType.class, "MySQL"), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.singletonMap("foo_db", schema)); when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); diff --git a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareParameterMarkerExtractorTest.java b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareParameterMarkerExtractorTest.java index c994839b59be3..11820bfea4437 100644 --- a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareParameterMarkerExtractorTest.java +++ b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareParameterMarkerExtractorTest.java @@ -56,7 +56,7 @@ private ShardingSphereSchema prepareSchema() { new ShardingSphereColumn("name", Types.VARCHAR, false, false, false, false, false, false), new ShardingSphereColumn("age", Types.SMALLINT, false, false, false, false, true, false)), Collections.emptyList(), Collections.emptyList()); ShardingSphereSchema result = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME); - result.getTables().put("user", table); + result.putTable(table); return result; } } diff --git a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java index e8af9da9113b7..6e9fed9b0425b 100644 --- a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java +++ b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java @@ -172,7 +172,7 @@ private MetaDataContexts mockMetaDataContexts() { ShardingSphereTable table = new ShardingSphereTable("t", Arrays.asList(new ShardingSphereColumn("id", Types.BIGINT, true, false, false, false, true, false), new ShardingSphereColumn("v", Types.INTEGER, false, false, false, false, true, false)), Collections.emptyList(), Collections.emptyList()); ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME); - schema.getTables().put("t", table); + schema.putTable(table); ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", TypedSPILoader.getService(DatabaseType.class, "MySQL"), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.singletonMap("foo_db", schema)); when(result.getMetaData().getDatabase("foo_db")).thenReturn(database); diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java index cb307da06d253..f9b8649f2c47f 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/parse/PostgreSQLComParseExecutorTest.java @@ -197,8 +197,8 @@ private ContextManager mockContextManager() { new ShardingSphereColumn("c", Types.VARCHAR, false, false, false, false, true, false), new ShardingSphereColumn("pad", Types.VARCHAR, false, false, false, false, true, false)), Collections.emptyList(), Collections.emptyList()); ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME); - schema.getTables().put("t_test", testTable); - schema.getTables().put("sbtest1", sbTestTable); + schema.putTable(testTable); + schema.putTable(sbTestTable); ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.singletonMap("public", schema)); when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); diff --git a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java index db968f5b4bd0d..eb3b103885463 100644 --- a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java +++ b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/PipelineContextUtils.java @@ -77,7 +77,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Set; @@ -134,18 +133,18 @@ private static ClusterPersistRepository getClusterPersistRepository(final Cluste } private static MetaDataContexts renewMetaDataContexts(final MetaDataContexts old, final MetaDataPersistService persistService) { - Map tables = new HashMap<>(3, 1F); - tables.put("t_order", new ShardingSphereTable("t_order", Arrays.asList( + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("t_order", Arrays.asList( new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.VARCHAR, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_order_item", new ShardingSphereTable("t_order_item", Arrays.asList( + tables.add(new ShardingSphereTable("t_order_item", Arrays.asList( new ShardingSphereColumn("item_id", Types.INTEGER, true, false, false, true, false, false), new ShardingSphereColumn("order_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.VARCHAR, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - old.getMetaData().getDatabase("logic_db").getSchema("logic_db").putTables(tables); + tables.forEach(each -> old.getMetaData().getDatabase("logic_db").getSchema("logic_db").putTable(each)); return MetaDataContextsFactory.create(persistService, old.getMetaData()); } diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java index 0577556d62055..c26e709edd650 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/EncryptSQLRewriterIT.java @@ -44,7 +44,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -66,38 +66,37 @@ protected YamlRootConfiguration createRootConfiguration(final SQLRewriteEngineTe @Override protected Map mockSchemas(final String schemaName) { - Map tables = new LinkedHashMap<>(); - tables.put("t_account", new ShardingSphereTable("t_account", Arrays.asList( + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("t_account", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("certificate_number", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_account_bak", new ShardingSphereTable("t_account_bak", Arrays.asList( + tables.add(new ShardingSphereTable("t_account_bak", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("certificate_number", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_account_detail", new ShardingSphereTable("t_account_detail", Arrays.asList( + tables.add(new ShardingSphereTable("t_account_detail", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("certificate_number", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_order", new ShardingSphereTable("t_order", Arrays.asList( + tables.add(new ShardingSphereTable("t_order", Arrays.asList( new ShardingSphereColumn("ORDER_ID", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("USER_ID", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("CONTENT", Types.VARCHAR, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_user", new ShardingSphereTable("t_user", Arrays.asList( + tables.add(new ShardingSphereTable("t_user", Arrays.asList( new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("user_name", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("email", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("telephone", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - ShardingSphereSchema result = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()); - return Collections.singletonMap(schemaName, result); + return Collections.singletonMap(schemaName, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList())); } @Override diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/MixSQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/MixSQLRewriterIT.java index 4323f00242301..ee9562b969b09 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/MixSQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/MixSQLRewriterIT.java @@ -38,7 +38,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Objects; @@ -53,25 +53,24 @@ protected YamlRootConfiguration createRootConfiguration(final SQLRewriteEngineTe @Override protected Map mockSchemas(final String schemaName) { - Map tables = new LinkedHashMap<>(); - tables.put("t_account", new ShardingSphereTable("t_account", Arrays.asList( + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("t_account", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, true, true, false, true, false, false), new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.singletonList(new ShardingSphereIndex("index_name", Collections.emptyList(), false)), Collections.emptyList())); - tables.put("t_account_bak", new ShardingSphereTable("t_account_bak", Arrays.asList( + tables.add(new ShardingSphereTable("t_account_bak", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, true, true, false, true, false, false), new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_account_detail", new ShardingSphereTable("t_account_detail", Arrays.asList( + tables.add(new ShardingSphereTable("t_account_detail", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.emptyList(), Collections.emptyList())); - ShardingSphereSchema result = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()); - return Collections.singletonMap(schemaName, result); + return Collections.singletonMap(schemaName, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList())); } @Override diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java index 7a573f87942db..e2e7e938b1b8d 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java @@ -41,7 +41,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -66,46 +66,45 @@ protected void mockRules(final Collection rules, final Strin @Override protected Map mockSchemas(final String schemaName) { - Map tables = new LinkedHashMap<>(); - tables.put("t_account", new ShardingSphereTable("t_account", Arrays.asList( + Collection tables = new LinkedList<>(); + tables.add(new ShardingSphereTable("t_account", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, true, true, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.singletonList(new ShardingSphereIndex("status_idx_exist", Collections.emptyList(), false)), Collections.emptyList())); - tables.put("t_account_detail", new ShardingSphereTable("t_account_detail", Arrays.asList( + tables.add(new ShardingSphereTable("t_account_detail", Arrays.asList( new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("amount", Types.DECIMAL, false, false, false, true, false, false), new ShardingSphereColumn("status", Types.TINYINT, false, false, false, false, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_user", new ShardingSphereTable("t_user", Arrays.asList( + tables.add(new ShardingSphereTable("t_user", Arrays.asList( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("content", Types.VARCHAR, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_user_extend", new ShardingSphereTable("t_user_extend", Arrays.asList( + tables.add(new ShardingSphereTable("t_user_extend", Arrays.asList( new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("content", Types.VARCHAR, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_single", new ShardingSphereTable("t_single", Arrays.asList( + tables.add(new ShardingSphereTable("t_single", Arrays.asList( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false)), Collections.singletonList(new ShardingSphereIndex("single_id_idx", Collections.emptyList(), false)), Collections.emptyList())); - tables.put("t_single_extend", new ShardingSphereTable("t_single_extend", Collections.singletonList( + tables.add(new ShardingSphereTable("t_single_extend", Collections.singletonList( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_config", new ShardingSphereTable("t_config", Arrays.asList( + tables.add(new ShardingSphereTable("t_config", Arrays.asList( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false)), Collections.singletonList(new ShardingSphereIndex("broadcast_id_idx", Collections.emptyList(), false)), Collections.emptyList())); - tables.put("T_ROLE", new ShardingSphereTable("T_ROLE", Arrays.asList( + tables.add(new ShardingSphereTable("T_ROLE", Arrays.asList( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("ROLE_ID", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("T_ROLE_ADMIN", new ShardingSphereTable("T_ROLE_ADMIN", Arrays.asList( + tables.add(new ShardingSphereTable("T_ROLE_ADMIN", Arrays.asList( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("ROLE_ID", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - tables.put("t_account_view", new ShardingSphereTable("t_account_view", Arrays.asList( + tables.add(new ShardingSphereTable("t_account_view", Arrays.asList( new ShardingSphereColumn("id", Types.INTEGER, false, false, false, true, false, false), new ShardingSphereColumn("account_id", Types.INTEGER, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); - ShardingSphereSchema result = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()); - return Collections.singletonMap(schemaName, result); + return Collections.singletonMap(schemaName, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList())); } @Override