Skip to content

Commit

Permalink
Add name in ShardingSphereSchema to facilitate the use of schemaName (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
strongduanmu authored Aug 13, 2024
1 parent 726d7e0 commit 2765b64
Show file tree
Hide file tree
Showing 48 changed files with 111 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.engine.SQLBindEngine;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
Expand Down Expand Up @@ -119,7 +120,7 @@ private TimestampServiceRule createTimeServiceRule() {
}

private ShardingSphereDatabase createDatabase(final ShardingRule shardingRule, final TimestampServiceRule timestampServiceRule) {
ShardingSphereSchema schema = new ShardingSphereSchema();
ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME);
schema.getTables().put("t_warehouse", 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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.groovy.util.Maps;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereConstraint;
Expand Down Expand Up @@ -68,7 +69,7 @@ private ShardingSphereSchema createSchema() {
tables.put("t_order",
new ShardingSphereTable("t_order", Collections.emptyList(), Collections.emptyList(), Collections.singleton(new ShardingSphereConstraint("t_order_foreign_key", "t_user"))));
tables.put("t_user", new ShardingSphereTable("t_user", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
return new ShardingSphereSchema(tables, Collections.emptyMap());
return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.groovy.util.Maps;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
Expand Down Expand Up @@ -57,7 +58,7 @@ class ShowTableStatusMergedResultTest {
@BeforeEach
void setUp() {
shardingRule = buildShardingRule();
schema = new ShardingSphereSchema(Collections.singletonMap("table",
schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table",
new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.groovy.util.Maps;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
Expand Down Expand Up @@ -48,7 +49,7 @@ class ShowTablesMergedResultTest {
@BeforeEach
void setUp() {
shardingRule = createShardingRule();
schema = new ShardingSphereSchema(Collections.singletonMap("table",
schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("table",
new ShardingSphereTable("table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap());
}

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(Collections.singletonMap("tbl", table), Collections.emptyMap());
ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap());
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(Collections.singletonMap("tbl", table), Collections.emptyMap());
ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap());
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(Collections.singletonMap("tbl", table), Collections.emptyMap());
ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap());
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(Collections.singletonMap("tbl", table), Collections.emptyMap());
ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tbl", table), Collections.emptyMap());
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 @@ -114,6 +114,6 @@ private static Map<String, ShardingSphereSchema> buildSchemas() {
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)),
Collections.emptyList(), Collections.emptyList()));
return Collections.singletonMap(DefaultDatabase.LOGIC_NAME, new ShardingSphereSchema(tables, Collections.emptyMap()));
return Collections.singletonMap(DefaultDatabase.LOGIC_NAME, new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyMap()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
Expand Down Expand Up @@ -64,7 +65,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(ImmutableMap.of(table.getName(), table, table2.getName(), table2), Collections.emptyMap());
schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, ImmutableMap.of(table.getName(), table, table2.getName(), table2), Collections.emptyMap());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ 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(Stream.of(table).collect(Collectors.toMap(ShardingSphereTable::getName, value -> value)), Collections.emptyMap());
ShardingSphereSchema schema =
new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Stream.of(table).collect(Collectors.toMap(ShardingSphereTable::getName, value -> value)), Collections.emptyMap());
ColumnSegment columnSegment = createColumnSegment(null, "COL");
Map<String, String> actual = new TablesContext(Arrays.asList(tableSegment1, tableSegment2),
TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), DefaultDatabase.LOGIC_NAME).findTableNames(Collections.singletonList(columnSegment), schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ private static Map<String, ShardingSphereSchema> revise(final Map<String, Schema

private static Map<String, ShardingSphereSchema> convertToSchemaMap(final Map<String, SchemaMetaData> schemaMetaDataMap, final GenericSchemaBuilderMaterial material) {
if (schemaMetaDataMap.isEmpty()) {
return Collections.singletonMap(material.getDefaultSchemaName(), new ShardingSphereSchema());
return Collections.singletonMap(material.getDefaultSchemaName(), new ShardingSphereSchema(material.getDefaultSchemaName()));
}
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(tables, new LinkedHashMap<>()));
result.put(entry.getKey().toLowerCase(), new ShardingSphereSchema(entry.getKey(), tables, new LinkedHashMap<>()));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ private static ShardingSphereSchema createSchema(final String schemaName, final
tables.put(metaData.getName(), swapper.swapToObject(metaData));
}
}
return new ShardingSphereSchema(tables, Collections.emptyMap());
return new ShardingSphereSchema(schemaName, tables, Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static Map<String, ShardingSphereSchema> getToBeAddedTablesBySchemas(fina
}

private static ShardingSphereSchema getToBeAddedTablesBySchema(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) {
return new ShardingSphereSchema(getToBeAddedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>());
return new ShardingSphereSchema(currentSchema.getName(), getToBeAddedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>());
}

/**
Expand Down Expand Up @@ -78,7 +78,7 @@ public static Map<String, ShardingSphereSchema> getToBeDeletedTablesBySchemas(fi
}

private static ShardingSphereSchema getToBeDeletedTablesBySchema(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) {
return new ShardingSphereSchema(getToBeDeletedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>());
return new ShardingSphereSchema(currentSchema.getName(), getToBeDeletedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@
@Getter
public final class ShardingSphereSchema {

private final String name;

private final Map<String, ShardingSphereTable> tables;

private final Map<String, ShardingSphereView> views;

@SuppressWarnings("CollectionWithoutInitialCapacity")
public ShardingSphereSchema() {
public ShardingSphereSchema(final String name) {
this.name = name;
tables = new ConcurrentHashMap<>();
views = new ConcurrentHashMap<>();
}

public ShardingSphereSchema(final Map<String, ShardingSphereTable> tables, final Map<String, ShardingSphereView> views) {
public ShardingSphereSchema(final String name, final Map<String, ShardingSphereTable> tables, final Map<String, ShardingSphereView> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
@Setter
public final class YamlShardingSphereSchema implements YamlConfiguration {

private String name;

private Map<String, YamlShardingSphereTable> tables;

private Map<String, YamlShardingSphereView> views;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@ public YamlShardingSphereSchema swapToYamlConfiguration(final ShardingSphereSche
Map<String, YamlShardingSphereView> views = schema.getAllViewNames().stream()
.collect(Collectors.toMap(each -> each, each -> swapYamlView(schema.getView(each)), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
YamlShardingSphereSchema result = new YamlShardingSphereSchema();
result.setName(schema.getName());
result.setTables(tables);
result.setViews(views);
return result;
}

@Override
public ShardingSphereSchema swapToObject(final YamlShardingSphereSchema yamlConfig) {
return Optional.ofNullable(yamlConfig).map(this::swapSchema).orElseGet(ShardingSphereSchema::new);
return Optional.ofNullable(yamlConfig).map(this::swapSchema).orElseGet(() -> new ShardingSphereSchema(yamlConfig.getName()));
}

private ShardingSphereSchema swapSchema(final YamlShardingSphereSchema schema) {
Map<String, ShardingSphereTable> tables = null == schema.getTables() || schema.getTables().isEmpty() ? new LinkedHashMap<>()
: schema.getTables().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> swapTable(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Map<String, ShardingSphereView> views = null == schema.getViews() || schema.getViews().isEmpty() ? new LinkedHashMap<>()
: schema.getViews().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> swapView(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
return new ShardingSphereSchema(tables, views);
return new ShardingSphereSchema(schema.getName(), tables, views);
}

private ShardingSphereTable swapTable(final YamlShardingSphereTable table) {
Expand Down
Loading

0 comments on commit 2765b64

Please sign in to comment.