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

menghaoran 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 c021c77  Add MetaDataContextsBuilder.addSchema() to decouple embed Map 
(#15163)
c021c77 is described below

commit c021c77eca8ba105874a2c181178ffc6819cc2e1
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jan 28 23:51:09 2022 +0800

    Add MetaDataContextsBuilder.addSchema() to decouple embed Map (#15163)
    
    * Refactor MetaDataContextsBuilder
    
    * Add MetaDataContextsBuilder.addSchema()
    
    * Add MetaDataContextsBuilder.addSchema() to decouple embed Map
    
    * fix test case
---
 .../infra/properties/TypedProperties.java          |  2 +-
 .../mode/manager/ContextManager.java               | 47 +++++++++----------
 .../mode/metadata/MetaDataContextsBuilder.java     | 52 +++++++++++++++++-----
 .../mode/metadata/MetaDataContextsBuilderTest.java | 15 ++-----
 .../cluster/ClusterContextManagerBuilder.java      | 34 +++++---------
 .../memory/MemoryContextManagerBuilder.java        | 26 +++--------
 .../StandaloneContextManagerBuilder.java           | 17 +++----
 7 files changed, 90 insertions(+), 103 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/properties/TypedProperties.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/properties/TypedProperties.java
index a8b049f..9a8f71b 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/properties/TypedProperties.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/properties/TypedProperties.java
@@ -38,7 +38,7 @@ public abstract class TypedProperties<E extends Enum<?> & 
TypedPropertyKey> {
     private final Map<E, TypedPropertyValue> cache;
     
     protected TypedProperties(final Class<E> keyClass, final Properties props) 
{
-        this.props = props;
+        this.props = null == props ? new Properties() : props;
         cache = preload(keyClass);
     }
     
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 42173ff..eee4add 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
@@ -409,39 +409,34 @@ public final class ContextManager implements 
AutoCloseable {
         Map<String, DataSource> dataSourceMap = new 
HashMap<>(originalMetaData.getResource().getDataSources());
         
dataSourceMap.putAll(DataSourcePoolCreator.create(addedDataSourceProps));
         Properties props = metaDataContexts.getProps().getProps();
-        Map<String, SchemaConfiguration> schemaConfigs = 
Collections.singletonMap(
-                originalMetaData.getName(), new 
DataSourceProvidedSchemaConfiguration(dataSourceMap, 
originalMetaData.getRuleMetaData().getConfigurations()));
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(schemaConfigs, props);
-        Map<String, ShardingSphereSchema> schemas = 
getShardingSphereSchemas(schemaConfigs, rules, props);
-        metaDataContexts.getMetaDataPersistService().ifPresent(optional -> 
optional.getSchemaMetaDataService().persist(originalMetaData.getName(), 
schemas.get(originalMetaData.getName())));
-        return new MetaDataContextsBuilder(schemaConfigs, 
metaDataContexts.getGlobalRuleMetaData().getConfigurations(), schemas, rules, 
props)
-                
.build(metaDataContexts.getMetaDataPersistService().orElse(null));
+        MetaDataContextsBuilder metaDataContextsBuilder = new 
MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(),
 props);
+        metaDataContextsBuilder.addSchema(originalMetaData.getName(), new 
DataSourceProvidedSchemaConfiguration(dataSourceMap, 
originalMetaData.getRuleMetaData().getConfigurations()), props);
+        metaDataContexts.getMetaDataPersistService().ifPresent(
+            optional -> 
optional.getSchemaMetaDataService().persist(originalMetaData.getName(), 
metaDataContextsBuilder.getSchemaMap().get(originalMetaData.getName())));
+        return 
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
     }
     
     private MetaDataContexts buildChangedMetaDataContext(final 
ShardingSphereMetaData originalMetaData, final Collection<RuleConfiguration> 
ruleConfigs) throws SQLException {
-        Map<String, SchemaConfiguration> schemaConfigs = 
Collections.singletonMap(
-                originalMetaData.getName(), new 
DataSourceProvidedSchemaConfiguration(originalMetaData.getResource().getDataSources(),
 ruleConfigs));
         Properties props = metaDataContexts.getProps().getProps();
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(schemaConfigs, props);
-        Map<String, ShardingSphereSchema> schemas = 
getShardingSphereSchemas(schemaConfigs, rules, props);
-        metaDataContexts.getMetaDataPersistService().ifPresent(optional -> 
optional.getSchemaMetaDataService().persist(originalMetaData.getName(), 
schemas.get(originalMetaData.getName())));
-        return new MetaDataContextsBuilder(schemaConfigs, 
metaDataContexts.getGlobalRuleMetaData().getConfigurations(), schemas, rules, 
props)
-                
.build(metaDataContexts.getMetaDataPersistService().orElse(null));
+        MetaDataContextsBuilder metaDataContextsBuilder = new 
MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(),
 props);
+        metaDataContextsBuilder.addSchema(originalMetaData.getName(), new 
DataSourceProvidedSchemaConfiguration(originalMetaData.getResource().getDataSources(),
 ruleConfigs), props);
+        metaDataContexts.getMetaDataPersistService().ifPresent(
+            optional -> 
optional.getSchemaMetaDataService().persist(originalMetaData.getName(), 
metaDataContextsBuilder.getSchemaMap().get(originalMetaData.getName())));
+        return 
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
     }
     
     private MetaDataContexts 
buildChangedMetaDataContextWithChangedDataSource(final ShardingSphereMetaData 
originalMetaData, 
                                                                               
final Map<String, DataSourceProperties> newDataSourceProps) throws SQLException 
{
         Collection<String> deletedDataSources = 
getDeletedDataSources(originalMetaData, newDataSourceProps).keySet();
         Map<String, DataSource> changedDataSources = 
buildChangedDataSources(originalMetaData, newDataSourceProps);
-        Map<String, DataSourceProvidedSchemaConfiguration> schemaConfigs = 
Collections.singletonMap(originalMetaData.getName(), new 
DataSourceProvidedSchemaConfiguration(
-                
getNewDataSources(originalMetaData.getResource().getDataSources(), 
getAddedDataSources(originalMetaData, newDataSourceProps), changedDataSources, 
deletedDataSources),
-                originalMetaData.getRuleMetaData().getConfigurations()));
         Properties props = metaDataContexts.getProps().getProps();
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(schemaConfigs, props);
-        Map<String, ShardingSphereSchema> schemas = 
getShardingSphereSchemas(schemaConfigs, rules, props);
-        metaDataContexts.getMetaDataPersistService().ifPresent(optional -> 
optional.getSchemaMetaDataService().persist(originalMetaData.getName(), 
schemas.get(originalMetaData.getName())));
-        return new MetaDataContextsBuilder(schemaConfigs, 
metaDataContexts.getGlobalRuleMetaData().getConfigurations(), schemas, rules, 
props)
-                
.build(metaDataContexts.getMetaDataPersistService().orElse(null));
+        MetaDataContextsBuilder metaDataContextsBuilder = new 
MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(),
 props);
+        metaDataContextsBuilder.addSchema(originalMetaData.getName(), new 
DataSourceProvidedSchemaConfiguration(
+                
getNewDataSources(originalMetaData.getResource().getDataSources(), 
getAddedDataSources(originalMetaData, newDataSourceProps), changedDataSources, 
deletedDataSources),
+                originalMetaData.getRuleMetaData().getConfigurations()), 
props);
+        metaDataContexts.getMetaDataPersistService().ifPresent(
+            optional -> 
optional.getSchemaMetaDataService().persist(originalMetaData.getName(), 
metaDataContextsBuilder.getSchemaMap().get(originalMetaData.getName())));
+        return 
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
     }
     
     private Map<String, DataSource> getNewDataSources(final Map<String, 
DataSource> originalDataSources,
@@ -491,12 +486,10 @@ public final class ContextManager implements 
AutoCloseable {
     }
     
     private MetaDataContexts buildNewMetaDataContext(final String schemaName) 
throws SQLException {
-        Map<String, DataSourceProvidedSchemaConfiguration> schemaConfigs = 
Collections.singletonMap(schemaName, new 
DataSourceProvidedSchemaConfiguration(new HashMap<>(), new LinkedList<>()));
         Properties props = metaDataContexts.getProps().getProps();
-        Map<String, ShardingSphereSchema> schemas = 
Collections.singletonMap(schemaName, new ShardingSphereSchema());
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(schemaConfigs, props);
-        return new MetaDataContextsBuilder(schemaConfigs, 
metaDataContexts.getGlobalRuleMetaData().getConfigurations(), schemas, rules, 
props)
-                
.build(metaDataContexts.getMetaDataPersistService().orElse(null));
+        MetaDataContextsBuilder metaDataContextsBuilder = new 
MetaDataContextsBuilder(metaDataContexts.getGlobalRuleMetaData().getConfigurations(),
 props);
+        metaDataContextsBuilder.addSchema(schemaName, new 
DataSourceProvidedSchemaConfiguration(new HashMap<>(), new LinkedList<>()), 
props);
+        return 
metaDataContextsBuilder.build(metaDataContexts.getMetaDataPersistService().orElse(null));
     }
     
     private void closeDataSources(final ShardingSphereMetaData removeMetaData) 
{
diff --git 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
index 46d7462..0cd9100 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilder.java
@@ -17,22 +17,30 @@
 
 package org.apache.shardingsphere.mode.metadata;
 
+import lombok.Getter;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.config.schema.SchemaConfiguration;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeRecognizer;
 import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
 import 
org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContextFactory;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
+import org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder;
+import 
org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilderMaterials;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 
+import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Properties;
@@ -42,29 +50,49 @@ import java.util.Properties;
  */
 public final class MetaDataContextsBuilder {
     
-    private final Map<String, ? extends SchemaConfiguration> schemaConfigs;
+    private final Map<String, SchemaConfiguration> schemaConfigMap = new 
LinkedHashMap<>();
     
-    private final Collection<RuleConfiguration> globalRuleConfigs;
+    private final Map<String, Collection<ShardingSphereRule>> schemaRulesMap = 
new LinkedHashMap<>();
     
-    private final Map<String, ShardingSphereSchema> schemas;
+    @Getter
+    private final Map<String, ShardingSphereSchema> schemaMap = new 
LinkedHashMap<>();
     
-    private final Map<String, Collection<ShardingSphereRule>> rules;
+    private final Collection<RuleConfiguration> globalRuleConfigs;
     
     private final ConfigurationProperties props;
     
     private final ExecutorEngine executorEngine;
     
-    public MetaDataContextsBuilder(final Map<String, ? extends 
SchemaConfiguration> schemaConfigs, final Collection<RuleConfiguration> 
globalRuleConfigs,
-                                   final Map<String, ShardingSphereSchema> 
schemas, final Map<String, Collection<ShardingSphereRule>> rules, final 
Properties props) {
-        this.schemaConfigs = schemaConfigs;
+    public MetaDataContextsBuilder(final Collection<RuleConfiguration> 
globalRuleConfigs, final Properties props) {
         this.globalRuleConfigs = globalRuleConfigs;
-        this.schemas = schemas;
-        this.rules = rules;
         this.props = new ConfigurationProperties(null == props ? new 
Properties() : props);
         executorEngine = new 
ExecutorEngine(this.props.<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
     }
     
     /**
+     * Add schema information.
+     * 
+     * @param schemaName schema name
+     * @param schemaConfig schema configuration
+     * @param props properties
+     * @throws SQLException SQL exception
+     */
+    public void addSchema(final String schemaName, final SchemaConfiguration 
schemaConfig, final Properties props) throws SQLException {
+        Map<String, DataSource> dataSourceMap = schemaConfig.getDataSources();
+        Collection<ShardingSphereRule> schemaRules = 
getSchemaRules(schemaName, schemaConfig.getRuleConfigurations(), dataSourceMap, 
props);
+        ShardingSphereSchema schema = SchemaLoader.load(dataSourceMap, 
schemaRules, props);
+        schemaConfigMap.put(schemaName, schemaConfig);
+        schemaRulesMap.put(schemaName, schemaRules);
+        schemaMap.put(schemaName, schema);
+    }
+    
+    private Collection<ShardingSphereRule> getSchemaRules(final String 
schemaName,
+                                                          final 
Collection<RuleConfiguration> schemaRuleConfigs, final Map<String, DataSource> 
dataSourceMap, final Properties props) {
+        DatabaseType databaseType = 
DatabaseTypeRecognizer.getDatabaseType(dataSourceMap.values());
+        return SchemaRulesBuilder.buildRules(new 
SchemaRulesBuilderMaterials(schemaName, schemaRuleConfigs, databaseType, 
dataSourceMap, new ConfigurationProperties(props)));
+    }
+    
+    /**
      * Build meta data contexts.
      * 
      * @param metaDataPersistService persist service
@@ -78,10 +106,10 @@ public final class MetaDataContextsBuilder {
     }
     
     private Map<String, ShardingSphereMetaData> getMetaDataMap() throws 
SQLException {
-        Map<String, ShardingSphereMetaData> result = new 
HashMap<>(schemaConfigs.size(), 1);
-        for (Entry<String, ? extends SchemaConfiguration> entry : 
schemaConfigs.entrySet()) {
+        Map<String, ShardingSphereMetaData> result = new 
HashMap<>(schemaConfigMap.size(), 1);
+        for (Entry<String, ? extends SchemaConfiguration> entry : 
schemaConfigMap.entrySet()) {
             String schemaName = entry.getKey();
-            result.put(schemaName, ShardingSphereMetaData.create(schemaName, 
schemas.get(schemaName), entry.getValue(), rules.get(schemaName)));
+            result.put(schemaName, ShardingSphereMetaData.create(schemaName, 
schemaMap.get(schemaName), entry.getValue(), schemaRulesMap.get(schemaName)));
         }
         return result;
     }
diff --git 
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilderTest.java
 
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilderTest.java
index ab588ea..65bc7a5 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilderTest.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsBuilderTest.java
@@ -22,7 +22,6 @@ import org.apache.shardingsphere.authority.rule.AuthorityRule;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import 
org.apache.shardingsphere.infra.config.schema.impl.DataSourceProvidedSchemaConfiguration;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.mode.metadata.fixture.FixtureRule;
@@ -31,8 +30,6 @@ import 
org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.parser.rule.SQLParserRule;
 import org.apache.shardingsphere.transaction.rule.TransactionRule;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
 
 import java.sql.SQLException;
 import java.util.Collection;
@@ -45,7 +42,6 @@ import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
-@RunWith(MockitoJUnitRunner.class)
 public final class MetaDataContextsBuilderTest {
     
     @Test
@@ -55,11 +51,9 @@ public final class MetaDataContextsBuilderTest {
         ShardingSphereUser user = new ShardingSphereUser("root", "root", "");
         AuthorityRuleConfiguration authorityRuleConfig = new 
AuthorityRuleConfiguration(Collections.singleton(user),
                 new 
ShardingSphereAlgorithmConfiguration("ALL_PRIVILEGES_PERMITTED", new 
Properties()));
-        MetaDataContexts actual = new MetaDataContextsBuilder(
-                Collections.singletonMap("logic_db", new 
DataSourceProvidedSchemaConfiguration(Collections.emptyMap(), 
Collections.singletonList(new FixtureRuleConfiguration()))),
-                Collections.singleton(authorityRuleConfig), 
Collections.singletonMap("logic_db", mock(ShardingSphereSchema.class)),
-                Collections.singletonMap("logic_db", 
Collections.singletonList(mock(FixtureRule.class))), props)
-                .build(mock(MetaDataPersistService.class));
+        MetaDataContextsBuilder builder = new 
MetaDataContextsBuilder(Collections.singleton(authorityRuleConfig), props);
+        builder.addSchema("logic_db", new 
DataSourceProvidedSchemaConfiguration(Collections.emptyMap(), 
Collections.singletonList(new FixtureRuleConfiguration())), props);
+        MetaDataContexts actual = 
builder.build(mock(MetaDataPersistService.class));
         assertRules(actual);
         
assertTrue(actual.getMetaData("logic_db").getResource().getDataSources().isEmpty());
         assertThat(actual.getProps().getProps().size(), is(1));
@@ -68,8 +62,7 @@ public final class MetaDataContextsBuilderTest {
     
     @Test
     public void assertBuildWithoutGlobalRuleConfigurations() throws 
SQLException {
-        MetaDataContexts actual = new MetaDataContextsBuilder(
-                Collections.emptyMap(), Collections.emptyList(), 
Collections.emptyMap(), Collections.emptyMap(), new 
Properties()).build(mock(MetaDataPersistService.class));
+        MetaDataContexts actual = new 
MetaDataContextsBuilder(Collections.emptyList(), new 
Properties()).build(mock(MetaDataPersistService.class));
         assertThat(actual.getGlobalRuleMetaData().getRules().size(), is(3));
         
assertThat(actual.getGlobalRuleMetaData().getRules().stream().filter(each -> 
each instanceof AuthorityRule).count(), is(1L));
         
assertThat(actual.getGlobalRuleMetaData().getRules().stream().filter(each -> 
each instanceof TransactionRule).count(), is(1L));
diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index c3c94f4..6c9f486 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -30,9 +30,6 @@ import 
org.apache.shardingsphere.infra.instance.InstanceContext;
 import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
 import org.apache.shardingsphere.infra.instance.definition.InstanceType;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
@@ -99,17 +96,15 @@ public final class ClusterContextManagerBuilder implements 
ContextManagerBuilder
         persistInstanceConfigurations(parameter.getLabels(), 
parameter.getInstanceDefinition(), parameter.getModeConfig().isOverwrite());
         Collection<String> schemaNames = 
parameter.getInstanceDefinition().getInstanceType() == InstanceType.JDBC ? 
parameter.getSchemaConfigs().keySet()
                 : 
metaDataPersistService.getSchemaMetaDataService().loadAllNames();
-        Map<String, Map<String, DataSource>> clusterDataSources = 
loadDataSourcesMap(metaDataPersistService, parameter.getSchemaConfigs(), 
schemaNames);
-        Map<String, Collection<RuleConfiguration>> clusterSchemaRuleConfigs = 
loadSchemaRules(metaDataPersistService, schemaNames);
-        Map<String, DataSourceProvidedSchemaConfiguration> schemaConfigs = new 
LinkedHashMap<>(clusterDataSources.size(), 1);
-        for (String each : clusterDataSources.keySet()) {
-            schemaConfigs.put(each, new 
DataSourceProvidedSchemaConfiguration(clusterDataSources.get(each), 
clusterSchemaRuleConfigs.get(each)));
-        }
+        Map<String, Map<String, DataSource>> loadedDataSources = 
loadDataSourcesMap(metaDataPersistService, parameter.getSchemaConfigs(), 
schemaNames);
         Properties loadedProps = 
metaDataPersistService.getPropsService().load();
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(schemaConfigs, loadedProps);
-        Map<String, ShardingSphereSchema> schemas = 
getShardingSphereSchemas(schemaConfigs, rules, loadedProps);
-        persistMetaData(schemas);
-        metaDataContexts = new MetaDataContextsBuilder(schemaConfigs, 
metaDataPersistService.getGlobalRuleService().load(), schemas, rules, 
loadedProps).build(metaDataPersistService);
+        MetaDataContextsBuilder metaDataContextsBuilder = new 
MetaDataContextsBuilder(metaDataPersistService.getGlobalRuleService().load(), 
loadedProps);
+        Map<String, Collection<RuleConfiguration>> loadedSchemaRuleConfigs = 
loadSchemaRules(metaDataPersistService, schemaNames);
+        for (String each : loadedDataSources.keySet()) {
+            metaDataContextsBuilder.addSchema(each, new 
DataSourceProvidedSchemaConfiguration(loadedDataSources.get(each), 
loadedSchemaRuleConfigs.get(each)), loadedProps);
+        }
+        persistMetaData(metaDataContextsBuilder.getSchemaMap());
+        metaDataContexts = 
metaDataContextsBuilder.build(metaDataPersistService);
         transactionContexts = new 
TransactionContextsBuilder(metaDataContexts.getMetaDataMap(), 
metaDataContexts.getGlobalRuleMetaData().getRules()).build();
         instanceContext = new 
InstanceContext(metaDataPersistService.getComputeNodePersistService().loadComputeNodeInstance(
                 parameter.getInstanceDefinition()), new 
ClusterWorkerIdGenerator(repository, metaDataPersistService, 
parameter.getInstanceDefinition()), getType());
@@ -216,17 +211,10 @@ public final class ClusterContextManagerBuilder 
implements ContextManagerBuilder
             each -> each, each -> 
metaDataPersistService.getSchemaRuleService().load(each), (oldValue, 
currentValue) -> oldValue, LinkedHashMap::new));
     }
     
-    private Map<String, ShardingSphereSchema> getShardingSphereSchemas(final 
Map<String, ? extends SchemaConfiguration> schemaConfigs, final Map<String, 
Collection<ShardingSphereRule>> rules, 
-                                                                       final 
Properties props) throws SQLException {
-        Map<String, ShardingSphereSchema> result = new 
LinkedHashMap<>(schemaConfigs.size(), 1);
-        for (String each : schemaConfigs.keySet()) {
-            result.put(each, 
SchemaLoader.load(schemaConfigs.get(each).getDataSources(), rules.get(each), 
props));
+    private void persistMetaData(final Map<String, ShardingSphereSchema> 
schemaMap) {
+        for (Entry<String, ShardingSphereSchema> entry : schemaMap.entrySet()) 
{
+            
metaDataPersistService.getSchemaMetaDataService().persist(entry.getKey(), 
entry.getValue());
         }
-        return result;
-    }
-    
-    private void persistMetaData(final Map<String, ShardingSphereSchema> 
schemas) {
-        schemas.forEach((key, value) -> 
metaDataPersistService.getSchemaMetaDataService().persist(key, value));
     }
     
     private void afterBuildContextManager(final ContextManagerBuilderParameter 
parameter) {
diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-memory-mode/shardingsphere-memory-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/memory/MemoryContextManagerBuilder.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-memory-mode/shardingsphere-memory-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/memory/MemoryContextManagerBuilder.java
index f01cb20..e7079ac 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-memory-mode/shardingsphere-memory-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/memory/MemoryContextManagerBuilder.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-memory-mode/shardingsphere-memory-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/memory/MemoryContextManagerBuilder.java
@@ -20,10 +20,6 @@ package org.apache.shardingsphere.mode.manager.memory;
 import org.apache.shardingsphere.infra.config.schema.SchemaConfiguration;
 import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
 import org.apache.shardingsphere.infra.instance.InstanceContext;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder;
 import org.apache.shardingsphere.infra.rule.identifier.type.InstanceAwareRule;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
@@ -35,9 +31,7 @@ import 
org.apache.shardingsphere.transaction.context.TransactionContexts;
 import 
org.apache.shardingsphere.transaction.context.TransactionContextsBuilder;
 
 import java.sql.SQLException;
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Properties;
 
 /**
@@ -47,9 +41,12 @@ public final class MemoryContextManagerBuilder implements 
ContextManagerBuilder
     
     @Override
     public ContextManager build(final ContextManagerBuilderParameter 
parameter) throws SQLException {
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(parameter.getSchemaConfigs(), 
parameter.getProps());
-        Map<String, ShardingSphereSchema> schemas = 
getShardingSphereSchemas(parameter.getSchemaConfigs(), rules, 
parameter.getProps());
-        MetaDataContexts metaDataContexts = new 
MetaDataContextsBuilder(parameter.getSchemaConfigs(), 
parameter.getGlobalRuleConfigs(), schemas, rules, 
parameter.getProps()).build(null);
+        Properties props = null == parameter.getProps() ? new Properties() : 
parameter.getProps();
+        MetaDataContextsBuilder metaDataContextsBuilder = new 
MetaDataContextsBuilder(parameter.getGlobalRuleConfigs(), props);
+        for (Entry<String, ? extends SchemaConfiguration> entry : 
parameter.getSchemaConfigs().entrySet()) {
+            metaDataContextsBuilder.addSchema(entry.getKey(), 
entry.getValue(), props);
+        }
+        MetaDataContexts metaDataContexts = 
metaDataContextsBuilder.build(null);
         TransactionContexts transactionContexts = new 
TransactionContextsBuilder(metaDataContexts.getMetaDataMap(), 
metaDataContexts.getGlobalRuleMetaData().getRules()).build();
         ContextManager result = new ContextManager();
         result.init(metaDataContexts, transactionContexts, 
buildInstanceContext(parameter));
@@ -57,15 +54,6 @@ public final class MemoryContextManagerBuilder implements 
ContextManagerBuilder
         return result;
     }
     
-    private Map<String, ShardingSphereSchema> getShardingSphereSchemas(final 
Map<String, ? extends SchemaConfiguration> schemaConfigs, final Map<String, 
Collection<ShardingSphereRule>> rules,
-                                                                       final 
Properties props) throws SQLException {
-        Map<String, ShardingSphereSchema> result = new 
LinkedHashMap<>(schemaConfigs.size(), 1);
-        for (String each : schemaConfigs.keySet()) {
-            result.put(each, 
SchemaLoader.load(schemaConfigs.get(each).getDataSources(), rules.get(each), 
props));
-        }
-        return result;
-    }
-    
     private InstanceContext buildInstanceContext(final 
ContextManagerBuilderParameter parameter) {
         ComputeNodeInstance instance = new ComputeNodeInstance();
         instance.setInstanceDefinition(parameter.getInstanceDefinition());
diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
index 6c03c60..32d288b 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
@@ -31,7 +31,6 @@ import 
org.apache.shardingsphere.infra.instance.definition.InstanceType;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder;
 import org.apache.shardingsphere.infra.rule.identifier.type.InstanceAwareRule;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
@@ -75,16 +74,14 @@ public final class StandaloneContextManagerBuilder 
implements ContextManagerBuil
         persistConfigurations(metaDataPersistService, 
parameter.getSchemaConfigs(), parameter.getGlobalRuleConfigs(), 
parameter.getProps(), parameter.getModeConfig().isOverwrite());
         Collection<String> schemaNames = 
parameter.getInstanceDefinition().getInstanceType() == InstanceType.JDBC ? 
parameter.getSchemaConfigs().keySet()
                 : 
metaDataPersistService.getSchemaMetaDataService().loadAllNames();
-        Map<String, Map<String, DataSource>> standaloneDataSources = 
loadDataSourcesMap(metaDataPersistService, parameter.getSchemaConfigs(), 
schemaNames);
-        Map<String, Collection<RuleConfiguration>> standaloneSchemaRules = 
loadSchemaRules(metaDataPersistService, schemaNames);
-        Map<String, SchemaConfiguration> schemaConfigs = new 
LinkedHashMap<>(standaloneDataSources.size(), 1);
-        for (String each : standaloneDataSources.keySet()) {
-            schemaConfigs.put(each, new 
DataSourceProvidedSchemaConfiguration(standaloneDataSources.get(each), 
standaloneSchemaRules.get(each)));
-        }
+        Map<String, Map<String, DataSource>> loadedDataSources = 
loadDataSourcesMap(metaDataPersistService, parameter.getSchemaConfigs(), 
schemaNames);
         Properties loadedProps = 
metaDataPersistService.getPropsService().load();
-        Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(schemaConfigs, loadedProps);
-        MetaDataContexts metaDataContexts = new MetaDataContextsBuilder(
-                schemaConfigs, 
metaDataPersistService.getGlobalRuleService().load(), 
getShardingSphereSchemas(schemaConfigs, rules, loadedProps), rules, 
loadedProps).build(metaDataPersistService);
+        MetaDataContextsBuilder metaDataContextsBuilder = new 
MetaDataContextsBuilder(metaDataPersistService.getGlobalRuleService().load(), 
loadedProps);
+        Map<String, Collection<RuleConfiguration>> loadedSchemaRules = 
loadSchemaRules(metaDataPersistService, schemaNames);
+        for (String each : loadedDataSources.keySet()) {
+            metaDataContextsBuilder.addSchema(each, new 
DataSourceProvidedSchemaConfiguration(loadedDataSources.get(each), 
loadedSchemaRules.get(each)), loadedProps);
+        }
+        MetaDataContexts metaDataContexts = 
metaDataContextsBuilder.build(metaDataPersistService);
         TransactionContexts transactionContexts = new 
TransactionContextsBuilder(metaDataContexts.getMetaDataMap(), 
metaDataContexts.getGlobalRuleMetaData().getRules()).build();
         ContextManager result = new ContextManager();
         result.init(metaDataContexts, transactionContexts, new 
InstanceContext(metaDataPersistService.getComputeNodePersistService().loadComputeNodeInstance(parameter.getInstanceDefinition()),
 

Reply via email to