Skip to content

Commit

Permalink
Move GenericSchemaBuilderMaterial.protocolType to GenericSchemaBuilder (
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Dec 10, 2024
1 parent 11c1c95 commit 7eabecf
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp
final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException {
ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
Collection<ShardingSphereRule> databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData);
Map<String, ShardingSphereSchema> schemas = new ConcurrentHashMap<>(GenericSchemaBuilder.build(new GenericSchemaBuilderMaterial(
protocolType, resourceMetaData.getStorageUnits(), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name))));
Map<String, ShardingSphereSchema> schemas = new ConcurrentHashMap<>(GenericSchemaBuilder.build(protocolType,
new GenericSchemaBuilderMaterial(resourceMetaData.getStorageUnits(), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name))));
SystemSchemaBuilder.build(name, protocolType, props).forEach(schemas::putIfAbsent);
return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(databaseRules), schemas.values());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,28 @@ public final class GenericSchemaBuilder {
/**
* Build generic schema.
*
* @param protocolType database type
* @param material generic schema builder material
* @return generic schema map
* @throws SQLException SQL exception
*/
public static Map<String, ShardingSphereSchema> build(final GenericSchemaBuilderMaterial material) throws SQLException {
return build(getAllTableNames(material.getRules()), material);
public static Map<String, ShardingSphereSchema> build(final DatabaseType protocolType, final GenericSchemaBuilderMaterial material) throws SQLException {
return build(getAllTableNames(material.getRules()), protocolType, material);
}

/**
* Build generic schema.
*
* @param tableNames table names
* @param protocolType database type
* @param material generic schema builder material
* @return generic schema map
* @throws SQLException SQL exception
*/
public static Map<String, ShardingSphereSchema> build(final Collection<String> tableNames, final GenericSchemaBuilderMaterial material) throws SQLException {
public static Map<String, ShardingSphereSchema> build(final Collection<String> tableNames, final DatabaseType protocolType, final GenericSchemaBuilderMaterial material) throws SQLException {
Map<String, SchemaMetaData> result = loadSchemas(tableNames, material);
if (!isSameProtocolAndStorageTypes(material.getProtocolType(), material.getStorageUnits())) {
result = translate(result, material);
if (!isSameProtocolAndStorageTypes(protocolType, material.getStorageUnits())) {
result = translate(result, protocolType, material);
}
return revise(result, material);
}
Expand All @@ -103,13 +105,13 @@ private static boolean isSameProtocolAndStorageTypes(final DatabaseType protocol
return storageUnits.values().stream().map(StorageUnit::getStorageType).allMatch(protocolType::equals);
}

private static Map<String, SchemaMetaData> translate(final Map<String, SchemaMetaData> schemaMetaDataMap, final GenericSchemaBuilderMaterial material) {
private static Map<String, SchemaMetaData> translate(final Map<String, SchemaMetaData> schemaMetaDataMap, final DatabaseType protocolType, final GenericSchemaBuilderMaterial material) {
Collection<TableMetaData> tableMetaDataList = new LinkedList<>();
for (StorageUnit each : material.getStorageUnits().values()) {
String defaultSchemaName = new DatabaseTypeRegistry(each.getStorageType()).getDefaultSchemaName(material.getDefaultSchemaName());
tableMetaDataList.addAll(Optional.ofNullable(schemaMetaDataMap.get(defaultSchemaName)).map(SchemaMetaData::getTables).orElseGet(Collections::emptyList));
}
String frontendSchemaName = new DatabaseTypeRegistry(material.getProtocolType()).getDefaultSchemaName(material.getDefaultSchemaName());
String frontendSchemaName = new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(material.getDefaultSchemaName());
Map<String, SchemaMetaData> result = new LinkedHashMap<>();
result.put(frontendSchemaName, new SchemaMetaData(frontendSchemaName, tableMetaDataList));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

Expand All @@ -34,8 +33,6 @@
@Getter
public final class GenericSchemaBuilderMaterial {

private final DatabaseType protocolType;

private final Map<String, StorageUnit> storageUnits;

private final Collection<ShardingSphereRule> rules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,39 @@
@StaticMockSettings(MetaDataLoader.class)
class GenericSchemaBuilderTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");

private GenericSchemaBuilderMaterial material;

@BeforeEach
void setUp() {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
ShardingSphereRule rule = mock(ShardingSphereRule.class);
when(rule.getAttributes()).thenReturn(new RuleAttributes(mock(TableMapperRuleAttribute.class)));
StorageUnit storageUnit = mock(StorageUnit.class);
when(storageUnit.getStorageType()).thenReturn(databaseType);
when(storageUnit.getDataSource()).thenReturn(new MockedDataSource());
material = new GenericSchemaBuilderMaterial(
databaseType, Collections.singletonMap("foo_schema", storageUnit), Collections.singleton(rule), new ConfigurationProperties(new Properties()), "foo_schema");
material = new GenericSchemaBuilderMaterial(Collections.singletonMap("foo_schema", storageUnit), Collections.singleton(rule), new ConfigurationProperties(new Properties()), "foo_schema");
}

@Test
void assertLoadWithExistedTableName() throws SQLException {
Collection<String> tableNames = Collections.singletonList("data_node_routed_table1");
when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames, material));
assertFalse(GenericSchemaBuilder.build(tableNames, material).get("foo_schema").getAllTables().isEmpty());
assertFalse(GenericSchemaBuilder.build(tableNames, databaseType, material).get("foo_schema").getAllTables().isEmpty());
}

@Test
void assertLoadWithNotExistedTableName() throws SQLException {
Collection<String> tableNames = Collections.singletonList("invalid_table");
when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames, material));
assertTrue(GenericSchemaBuilder.build(tableNames, material).get("foo_schema").getAllTables().isEmpty());
assertTrue(GenericSchemaBuilder.build(tableNames, databaseType, material).get("foo_schema").getAllTables().isEmpty());
}

@Test
void assertLoadAllTables() throws SQLException {
Collection<String> tableNames = Arrays.asList("data_node_routed_table1", "data_node_routed_table2");
when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames, material));
Map<String, ShardingSphereSchema> actual = GenericSchemaBuilder.build(tableNames, material);
Map<String, ShardingSphereSchema> actual = GenericSchemaBuilder.build(tableNames, databaseType, material);
assertThat(actual.size(), is(1));
assertTables(new ShardingSphereSchema("foo_schema", actual.values().iterator().next().getAllTables(), Collections.emptyList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ void assertGetSchemaMetaDataLoaderMaterialsWhenConfigCheckMetaDataEnable() {
when(rule0.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute));
ShardingSphereRule rule1 = mock(ShardingSphereRule.class);
when(rule1.getAttributes()).thenReturn(new RuleAttributes());
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
mock(DatabaseType.class), mockStorageUnits(), Arrays.asList(rule0, rule1), mock(ConfigurationProperties.class), "sharding_db");
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(mockStorageUnits(), Arrays.asList(rule0, rule1), mock(ConfigurationProperties.class), "sharding_db");
Collection<MetaDataLoaderMaterial> actual = SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"), material, true);
assertThat(actual.size(), is(2));
Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
Expand All @@ -74,8 +73,7 @@ void assertGetSchemaMetaDataLoaderMaterialsWhenNotConfigCheckMetaDataEnable() {
when(rule0.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute));
ShardingSphereRule rule1 = mock(ShardingSphereRule.class);
when(rule1.getAttributes()).thenReturn(new RuleAttributes());
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
mock(DatabaseType.class), mockStorageUnits(), Arrays.asList(rule0, rule1), mock(ConfigurationProperties.class), "sharding_db");
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(mockStorageUnits(), Arrays.asList(rule0, rule1), mock(ConfigurationProperties.class), "sharding_db");
Collection<MetaDataLoaderMaterial> actual = SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"), material, false);
assertThat(actual.size(), is(1));
Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
Expand All @@ -92,8 +90,7 @@ void assertGetSchemaMetaDataLoaderMaterialsWhenNotConfigCheckMetaDataEnableForSi
when(rule0.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute));
ShardingSphereRule rule1 = mock(ShardingSphereRule.class);
when(rule1.getAttributes()).thenReturn(new RuleAttributes());
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
mock(DatabaseType.class), mockStorageUnits(), Arrays.asList(rule0, rule1), mock(ConfigurationProperties.class), "public");
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(mockStorageUnits(), Arrays.asList(rule0, rule1), mock(ConfigurationProperties.class), "public");
Collection<MetaDataLoaderMaterial> actual = SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_single"), material, false);
assertThat(actual.size(), is(1));
Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ public void reloadSchema(final ShardingSphereDatabase database, final String sch

private ShardingSphereSchema loadSchema(final ShardingSphereDatabase database, final String schemaName, final String dataSourceName) throws SQLException {
database.reloadRules();
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getProtocolType(),
Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnits().get(dataSourceName)),
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnits().get(dataSourceName)),
database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName);
ShardingSphereSchema result = GenericSchemaBuilder.build(material).get(schemaName);
ShardingSphereSchema result = GenericSchemaBuilder.build(database.getProtocolType(), material).get(schemaName);
persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(database.getName(), schemaName).forEach(result::putView);
return result;
}
Expand All @@ -157,7 +156,7 @@ private ShardingSphereSchema loadSchema(final ShardingSphereDatabase database, f
* @param tableName to be reloaded table name
*/
public void reloadTable(final ShardingSphereDatabase database, final String schemaName, final String tableName) {
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getProtocolType(),
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
database.getResourceMetaData().getStorageUnits(), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName);
try {
persistTable(database, schemaName, tableName, material);
Expand All @@ -176,7 +175,7 @@ public void reloadTable(final ShardingSphereDatabase database, final String sche
*/
public void reloadTable(final ShardingSphereDatabase database, final String schemaName, final String dataSourceName, final String tableName) {
StorageUnit storageUnit = database.getResourceMetaData().getStorageUnits().get(dataSourceName);
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getProtocolType(),
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
Collections.singletonMap(dataSourceName, storageUnit), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName);
try {
persistTable(database, schemaName, tableName, material);
Expand All @@ -186,7 +185,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));
ShardingSphereSchema schema = GenericSchemaBuilder.build(Collections.singleton(tableName), database.getProtocolType(), material).getOrDefault(schemaName, new ShardingSphereSchema(schemaName));
persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().persist(database.getName(), schemaName, Collections.singleton(schema.getTable(tableName)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ private ShardingSphereTable getTable(final ShardingSphereDatabase database, fina
if (TableRefreshUtils.isSingleTable(tableName, database)) {
ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName));
}
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(tableName), material);
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(tableName), database.getProtocolType(), material);
return Optional.ofNullable(schemas.get(schemaName)).map(optional -> optional.getTable(tableName))
.orElseGet(() -> new ShardingSphereTable(tableName, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
if (isSingleTable) {
ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName));
}
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(tableName), material);
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(tableName), database.getProtocolType(), material);
Optional<ShardingSphereTable> actualTableMetaData = Optional.ofNullable(schemas.get(schemaName)).map(optional -> optional.getTable(tableName));
Preconditions.checkState(actualTableMetaData.isPresent(), "Load actual table metadata '%s' failed.", tableName);
metaDataManagerPersistService.createTable(database.getName(), schemaName, actualTableMetaData.get(), logicDataSourceNames.isEmpty() ? null : logicDataSourceNames.iterator().next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ private ShardingSphereTable getTable(final ShardingSphereDatabase database, fina
if (TableRefreshUtils.isSingleTable(tableName, database) && !logicDataSourceNames.isEmpty()) {
ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName));
}
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(tableName), material);
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(tableName), database.getProtocolType(), material);
return Optional.ofNullable(schemas.get(schemaName)).map(optional -> optional.getTable(tableName))
.orElseGet(() -> new ShardingSphereTable(tableName, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ private ShardingSphereSchema getSchema(final ShardingSphereDatabase database, fi
if (TableRefreshUtils.isSingleTable(viewName, database)) {
ruleMetaData.getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, viewName));
}
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
database.getProtocolType(), database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(viewName), material);
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getResourceMetaData().getStorageUnits(), ruleMetaData.getRules(), props, schemaName);
Map<String, ShardingSphereSchema> schemas = GenericSchemaBuilder.build(Collections.singletonList(viewName), database.getProtocolType(), material);
Optional<ShardingSphereTable> actualViewMetaData = Optional.ofNullable(schemas.get(schemaName)).map(optional -> optional.getTable(viewName));
ShardingSphereSchema result = new ShardingSphereSchema(schemaName);
actualViewMetaData.ifPresent(result::putTable);
Expand Down
Loading

0 comments on commit 7eabecf

Please sign in to comment.