This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 21061b2c7f5 Refactor 
ContextManager.createMetaDataContextsWithAlteredDatabaseRules (#18820)
21061b2c7f5 is described below

commit 21061b2c7f56b52d8d2ec5874dc7cae8d317ca20
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jul 4 14:01:23 2022 +0800

    Refactor ContextManager.createMetaDataContextsWithAlteredDatabaseRules 
(#18820)
    
    * Refactor ContextManager.createMetaDataContextsWithAlteredDatabaseRules
    
    * For code format
---
 .../infra/database/type/DatabaseTypeEngine.java    | 26 ++++++++++++++++------
 .../database/ShardingSphereDatabasesFactory.java   | 17 ++++++++++++++
 .../mode/manager/ContextManager.java               | 24 ++++++++++++--------
 3 files changed, 51 insertions(+), 16 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
index 2404bb01f8f..2d2245742b2 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
@@ -40,10 +40,26 @@ public final class DatabaseTypeEngine {
     
     private static final String DEFAULT_DATABASE_TYPE = "MySQL";
     
+    /**
+     * Get protocol type.
+     * 
+     * @param databaseConfig database configuration
+     * @param props props
+     * @return protocol type
+     */
+    public static DatabaseType getProtocolType(final DatabaseConfiguration 
databaseConfig, final ConfigurationProperties props) {
+        Optional<DatabaseType> configuredDatabaseType = 
findConfiguredDatabaseType(props);
+        if (configuredDatabaseType.isPresent()) {
+            return configuredDatabaseType.get();
+        }
+        Collection<DataSource> dataSources = 
databaseConfig.getDataSources().isEmpty() ? 
databaseConfig.getDataSources().values() : Collections.emptyList();
+        return getDatabaseType(dataSources);
+    }
+    
     /**
      * Get protocol type.
      *
-     * @param databaseConfigs database configs
+     * @param databaseConfigs database configurations
      * @param props props
      * @return protocol type
      */
@@ -53,7 +69,7 @@ public final class DatabaseTypeEngine {
             return configuredDatabaseType.get();
         }
         Collection<DataSource> dataSources = databaseConfigs.values().stream()
-                
.filter(DatabaseTypeEngine::hasDataSource).findFirst().map(optional -> 
optional.getDataSources().values()).orElseGet(Collections::emptyList);
+                .filter(each -> 
each.getDataSources().isEmpty()).findFirst().map(optional -> 
optional.getDataSources().values()).orElseGet(Collections::emptyList);
         return getDatabaseType(dataSources);
     }
     
@@ -65,7 +81,7 @@ public final class DatabaseTypeEngine {
      */
     public static DatabaseType getStorageType(final Map<String, ? extends 
DatabaseConfiguration> databaseConfigs) {
         return getDatabaseType(
-                
databaseConfigs.values().stream().filter(DatabaseTypeEngine::hasDataSource).findFirst().map(optional
 -> optional.getDataSources().values()).orElseGet(Collections::emptyList));
+                databaseConfigs.values().stream().filter(each -> 
each.getDataSources().isEmpty()).findFirst().map(optional -> 
optional.getDataSources().values()).orElseGet(Collections::emptyList));
     }
     
     /**
@@ -107,10 +123,6 @@ public final class DatabaseTypeEngine {
         return configuredDatabaseType.isEmpty() ? Optional.empty() : 
Optional.of(DatabaseTypeEngine.getTrunkDatabaseType(configuredDatabaseType));
     }
     
-    private static boolean hasDataSource(final DatabaseConfiguration 
databaseConfig) {
-        return !databaseConfig.getDataSources().isEmpty();
-    }
-    
     private static boolean matchURLs(final String url, final DatabaseType 
databaseType) {
         return 
databaseType.getJdbcUrlPrefixes().stream().anyMatch(url::startsWith);
     }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
index 6a0b28e24a6..50e15d156c9 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java
@@ -33,6 +33,23 @@ import java.util.Map.Entry;
  */
 public final class ShardingSphereDatabasesFactory {
     
+    /**
+     * Create databases.
+     *
+     * @param databaseName database name
+     * @param databaseConfig database configuration
+     * @param props properties
+     * @param instanceContext instance context
+     * @return created database
+     * @throws SQLException SQL exception
+     */
+    public static ShardingSphereDatabase create(final String databaseName, 
final DatabaseConfiguration databaseConfig,
+                                                final ConfigurationProperties 
props, final InstanceContext instanceContext) throws SQLException {
+        DatabaseType protocolType = 
DatabaseTypeEngine.getProtocolType(databaseConfig, props);
+        DatabaseType storageType = 
DatabaseTypeEngine.getDatabaseType(databaseConfig.getDataSources().values());
+        return ShardingSphereDatabase.create(databaseName, protocolType, 
storageType, databaseConfig, props, instanceContext);
+    }
+    
     /**
      * Create databases.
      * 
diff --git 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index 89cf47d2bd4..32af1998fdf 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -255,15 +255,21 @@ public final class ContextManager implements 
AutoCloseable {
     }
     
     private MetaDataContexts 
createMetaDataContextsWithAlteredDatabaseRules(final String databaseName, final 
Collection<RuleConfiguration> ruleConfigs) throws SQLException {
-        ShardingSphereDatabase toBeChangedDatabase = 
metaDataContexts.getMetaData().getDatabases().get(databaseName);
-        ConfigurationProperties props = 
metaDataContexts.getMetaData().getProps();
-        Map<String, ShardingSphereDatabase> changedDatabases = 
ShardingSphereDatabasesFactory.create(
-                Collections.singletonMap(databaseName, new 
DataSourceProvidedDatabaseConfiguration(toBeChangedDatabase.getResource().getDataSources(),
 ruleConfigs)), props, instanceContext);
-        metaDataContexts.getMetaData().getDatabases().putAll(changedDatabases);
-        ShardingSphereRuleMetaData globalMetaData = new 
ShardingSphereRuleMetaData(
-                
GlobalRulesBuilder.buildRules(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(),
 metaDataContexts.getMetaData().getDatabases(), instanceContext));
-        ShardingSphereMetaData metaData = new 
ShardingSphereMetaData(changedDatabases, globalMetaData, props);
-        return new 
MetaDataContexts(metaDataContexts.getPersistService().orElse(null), metaData, 
OptimizerContextFactory.create(metaDataContexts.getMetaData().getDatabases(), 
globalMetaData));
+        Map<String, ShardingSphereDatabase> changedDatabases = 
createChangedDatabases(databaseName, ruleConfigs);
+        ShardingSphereRuleMetaData changedGlobalMetaData = new 
ShardingSphereRuleMetaData(
+                
GlobalRulesBuilder.buildRules(metaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(),
 changedDatabases, instanceContext));
+        return new 
MetaDataContexts(metaDataContexts.getPersistService().orElse(null),
+                new ShardingSphereMetaData(changedDatabases, 
changedGlobalMetaData, metaDataContexts.getMetaData().getProps()),
+                OptimizerContextFactory.create(changedDatabases, 
changedGlobalMetaData));
+    }
+    
+    private Map<String, ShardingSphereDatabase> createChangedDatabases(final 
String databaseName, final Collection<RuleConfiguration> ruleConfigs) throws 
SQLException {
+        ShardingSphereDatabase changedDatabase = 
ShardingSphereDatabasesFactory.create(databaseName,
+                new 
DataSourceProvidedDatabaseConfiguration(metaDataContexts.getMetaData().getDatabases().get(databaseName).getResource().getDataSources(),
 ruleConfigs),
+                metaDataContexts.getMetaData().getProps(), instanceContext);
+        Map<String, ShardingSphereDatabase> result = new 
LinkedHashMap<>(metaDataContexts.getMetaData().getDatabases());
+        result.put(databaseName, changedDatabase);
+        return result;
     }
     
     /**

Reply via email to