Skip to content

Commit

Permalink
Refactor ShardingSphereSchema.tables and views (#33825)
Browse files Browse the repository at this point in the history
* Refactor ShardingSphereSchema

* Refactor ShardingSphereSchema.views

* Refactor ShardingSphereSchema.views

* Refactor ShardingSphereSchema.views

* Refactor ShardingSphereSchema.views

* Refactor ShardingSphereSchema.tables

* Refactor ShardingSphereSchema.tables

* Refactor ShardingSphereSchema.tables

* Refactor ShardingSphereSchema.tables

* Refactor ShardingSphereSchema.tables

* Refactor ShardingSphereSchema.tables

* Refactor ShardingSphereSchema.tables

* Refactor ShardingSphereSchema.tables
  • Loading branch information
terrymanu authored Nov 29, 2024
1 parent 8d76fc7 commit 5e6e453
Show file tree
Hide file tree
Showing 63 changed files with 427 additions and 414 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,11 +64,10 @@ private ShardingRule buildShardingRule() {
}

private ShardingSphereSchema createSchema() {
Map<String, ShardingSphereTable> 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<ShardingSphereTable> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,27 +102,27 @@ private static ShardingSphereMetaData createShardingSphereMetaData(final Shardin
}

private static Map<String, ShardingSphereSchema> buildSchemas() {
Map<String, ShardingSphereTable> 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<ShardingSphereTable> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> actual = new TablesContext(Arrays.asList(tableSegment1, tableSegment2),
databaseType, DefaultDatabase.LOGIC_NAME).findTableNames(Collections.singletonList(columnSegment), schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,14 @@ private static Map<String, ShardingSphereSchema> convertToSchemaMap(final Map<St
}
Map<String, ShardingSphereSchema> result = new ConcurrentHashMap<>(schemaMetaDataMap.size(), 1F);
for (Entry<String, SchemaMetaData> entry : schemaMetaDataMap.entrySet()) {
Map<String, ShardingSphereTable> 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<String, ShardingSphereTable> convertToTableMap(final Collection<TableMetaData> tableMetaDataList) {
Map<String, ShardingSphereTable> result = new LinkedHashMap<>(tableMetaDataList.size(), 1F);
for (TableMetaData each : tableMetaDataList) {
Collection<ShardingSphereColumn> columns = convertToColumns(each.getColumns());
Collection<ShardingSphereIndex> indexes = convertToIndexes(each.getIndexes());
Collection<ShardingSphereConstraint> constraints = convertToConstraints(each.getConstraints());
result.put(each.getName(), new ShardingSphereTable(each.getName(), columns, indexes, constraints, each.getType()));
}
return result;
private static Collection<ShardingSphereTable> convertToTables(final Collection<TableMetaData> 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<ShardingSphereColumn> convertToColumns(final Collection<ColumnMetaData> columnMetaDataList) {
Expand Down
Loading

0 comments on commit 5e6e453

Please sign in to comment.