Skip to content

Commit

Permalink
Add YamlJDBCConfiguration.rebuild() (#30157)
Browse files Browse the repository at this point in the history
* Refactor YamlShardingSphereDataSourceFactory

* Add YamlJDBCConfiguration.build()

* Add YamlJDBCConfiguration.build()
  • Loading branch information
terrymanu authored Feb 17, 2024
1 parent f9edb9e commit 1352668
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.globalclock.core.yaml.config.YamlGlobalClockRuleConfiguration;
import org.apache.shardingsphere.infra.util.yaml.YamlConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.logging.yaml.config.YamlLoggingRuleConfiguration;
import org.apache.shardingsphere.parser.yaml.config.YamlSQLParserRuleConfiguration;
Expand Down Expand Up @@ -52,21 +53,52 @@ public final class YamlJDBCConfiguration implements YamlConfiguration {

private YamlModeConfiguration mode;

private Properties props = new Properties();

private YamlAuthorityRuleConfiguration authority;

private YamlSQLParserRuleConfiguration sqlParser;

private YamlTransactionRuleConfiguration transaction;

private YamlSQLParserRuleConfiguration sqlParser;
private YamlGlobalClockRuleConfiguration globalClock;

private YamlSQLFederationRuleConfiguration sqlFederation;

private YamlSQLTranslatorRuleConfiguration sqlTranslator;

private YamlTrafficRuleConfiguration traffic;

private YamlLoggingRuleConfiguration logging;

private YamlGlobalClockRuleConfiguration globalClock;
private Properties props = new Properties();

private YamlSQLFederationRuleConfiguration sqlFederation;
/**
* Rebuild YAML JDBC configuration.
*/
public void rebuild() {
rules.removeIf(YamlGlobalRuleConfiguration.class::isInstance);
if (null != authority) {
rules.add(authority);
}
if (null != sqlParser) {
rules.add(sqlParser);
}
if (null != transaction) {
rules.add(transaction);
}
if (null != globalClock) {
rules.add(globalClock);
}
if (null != sqlFederation) {
rules.add(sqlFederation);
}
if (null != sqlTranslator) {
rules.add(sqlTranslator);
}
if (null != traffic) {
rules.add(traffic);
}
if (null != logging) {
rules.add(logging);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
Expand All @@ -45,10 +44,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlShardingSphereDataSourceFactory {

private static final YamlRuleConfigurationSwapperEngine RULE_CONFIG_SWAPPER = new YamlRuleConfigurationSwapperEngine();

private static final YamlDataSourceConfigurationSwapper DATA_SOURCE_SWAPPER = new YamlDataSourceConfigurationSwapper();

/**
* Create ShardingSphere data source.
*
Expand All @@ -59,7 +54,7 @@ public final class YamlShardingSphereDataSourceFactory {
*/
public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
YamlJDBCConfiguration rootConfig = YamlEngine.unmarshal(yamlFile, YamlJDBCConfiguration.class);
return createDataSource(DATA_SOURCE_SWAPPER.swapToDataSources(rootConfig.getDataSources()), rootConfig);
return createDataSource(new YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources()), rootConfig);
}

/**
Expand All @@ -72,7 +67,7 @@ public static DataSource createDataSource(final File yamlFile) throws SQLExcepti
*/
public static DataSource createDataSource(final byte[] yamlBytes) throws SQLException, IOException {
YamlJDBCConfiguration rootConfig = YamlEngine.unmarshal(yamlBytes, YamlJDBCConfiguration.class);
return createDataSource(DATA_SOURCE_SWAPPER.swapToDataSources(rootConfig.getDataSources()), rootConfig);
return createDataSource(new YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources()), rootConfig);
}

/**
Expand Down Expand Up @@ -134,37 +129,9 @@ private static DataSource createDataSource(final DataSource dataSource, final Ya
}

private static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final YamlJDBCConfiguration jdbcConfig) throws SQLException {
rebuildGlobalRuleConfiguration(jdbcConfig);
ModeConfiguration modeConfig = null == jdbcConfig.getMode() ? null : new YamlModeConfigurationSwapper().swapToObject(jdbcConfig.getMode());
Collection<RuleConfiguration> ruleConfigs = RULE_CONFIG_SWAPPER.swapToRuleConfigurations(jdbcConfig.getRules());
jdbcConfig.rebuild();
Collection<RuleConfiguration> ruleConfigs = new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(jdbcConfig.getRules());
return ShardingSphereDataSourceFactory.createDataSource(jdbcConfig.getDatabaseName(), modeConfig, dataSourceMap, ruleConfigs, jdbcConfig.getProps());
}

private static void rebuildGlobalRuleConfiguration(final YamlJDBCConfiguration jdbcConfig) {
jdbcConfig.getRules().removeIf(YamlGlobalRuleConfiguration.class::isInstance);
if (null != jdbcConfig.getAuthority()) {
jdbcConfig.getRules().add(jdbcConfig.getAuthority());
}
if (null != jdbcConfig.getTransaction()) {
jdbcConfig.getRules().add(jdbcConfig.getTransaction());
}
if (null != jdbcConfig.getGlobalClock()) {
jdbcConfig.getRules().add(jdbcConfig.getGlobalClock());
}
if (null != jdbcConfig.getSqlParser()) {
jdbcConfig.getRules().add(jdbcConfig.getSqlParser());
}
if (null != jdbcConfig.getSqlTranslator()) {
jdbcConfig.getRules().add(jdbcConfig.getSqlTranslator());
}
if (null != jdbcConfig.getTraffic()) {
jdbcConfig.getRules().add(jdbcConfig.getTraffic());
}
if (null != jdbcConfig.getLogging()) {
jdbcConfig.getRules().add(jdbcConfig.getLogging());
}
if (null != jdbcConfig.getSqlFederation()) {
jdbcConfig.getRules().add(jdbcConfig.getSqlFederation());
}
}
}

0 comments on commit 1352668

Please sign in to comment.