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

panjuan 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 179d71a  Move build transaction contexts to TransactionContextsBuilder 
(#14106)
179d71a is described below

commit 179d71a696b485c9ab8fe56dfd7e11cf987bb94e
Author: Haoran Meng <[email protected]>
AuthorDate: Thu Dec 16 12:18:05 2021 +0800

    Move build transaction contexts to TransactionContextsBuilder (#14106)
    
    * Move build transaction contexts to TransactionContextsBuilder
    
    * Move build transaction contexts to TransactionContextsBuilder
    
    Co-authored-by: shardingsphere <[email protected]>
---
 .../context/TransactionContextsBuilder.java        | 64 ++++++++++++++++++++++
 .../cluster/ClusterContextManagerBuilder.java      | 29 +---------
 .../memory/MemoryContextManagerBuilder.java        | 27 +--------
 .../StandaloneContextManagerBuilder.java           | 26 +--------
 4 files changed, 71 insertions(+), 75 deletions(-)

diff --git 
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/context/TransactionContextsBuilder.java
 
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/context/TransactionContextsBuilder.java
new file mode 100644
index 0000000..90459c9
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/context/TransactionContextsBuilder.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.transaction.context;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import 
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
+import org.apache.shardingsphere.transaction.rule.TransactionRule;
+import 
org.apache.shardingsphere.transaction.rule.builder.DefaultTransactionRuleConfigurationBuilder;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Transaction contexts builder.
+ */
+@RequiredArgsConstructor
+public final class TransactionContextsBuilder {
+    
+    private final Map<String, ShardingSphereMetaData> metaDataMap;
+    
+    private final Collection<ShardingSphereRule> globalRules;
+    
+    /**
+     * Build transaction contexts.
+     * 
+     * @return transaction contexts
+     */
+    public TransactionContexts build() {
+        Map<String, ShardingSphereTransactionManagerEngine> engines = new 
HashMap<>(metaDataMap.keySet().size(), 1);
+        TransactionRule transactionRule = getTransactionRule();
+        for (String each : metaDataMap.keySet()) {
+            ShardingSphereTransactionManagerEngine engine = new 
ShardingSphereTransactionManagerEngine();
+            ShardingSphereResource resource = 
metaDataMap.get(each).getResource();
+            engine.init(resource.getDatabaseType(), resource.getDataSources(), 
transactionRule);
+            engines.put(each, engine);
+        }
+        return new TransactionContexts(engines);
+    }
+    
+    private TransactionRule getTransactionRule() {
+        Optional<TransactionRule> transactionRule = 
globalRules.stream().filter(each -> each instanceof TransactionRule).map(each 
-> (TransactionRule) each).findFirst();
+        return transactionRule.orElseGet(() -> new TransactionRule(new 
DefaultTransactionRuleConfigurationBuilder().build()));
+    }
+}
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 fd4f987..d5c7029 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
@@ -22,7 +22,7 @@ import 
org.apache.shardingsphere.infra.config.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.infra.metadata.schema.QualifiedSchema;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -35,7 +35,6 @@ import 
org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.ClusterContextManagerCoordinator;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.RegistryCenter;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.StorageNodeStatus;
-import org.apache.shardingsphere.infra.metadata.schema.QualifiedSchema;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.MetaDataContextsBuilder;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
@@ -44,10 +43,8 @@ import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositor
 import org.apache.shardingsphere.schedule.core.api.ModeScheduleContext;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.spi.typed.TypedSPIRegistry;
-import 
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
-import org.apache.shardingsphere.transaction.rule.TransactionRule;
-import 
org.apache.shardingsphere.transaction.rule.builder.DefaultTransactionRuleConfigurationBuilder;
+import 
org.apache.shardingsphere.transaction.context.TransactionContextsBuilder;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
@@ -56,9 +53,7 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import java.util.Properties;
-
 import java.util.stream.Collectors;
 
 /**
@@ -108,7 +103,7 @@ public final class ClusterContextManagerBuilder implements 
ContextManagerBuilder
         persistMetaData(schemas);
         metaDataContexts = new MetaDataContextsBuilder(clusterDataSources, 
clusterSchemaRuleConfigs, metaDataPersistService.getGlobalRuleService().load(), 
schemas, rules, clusterProps)
                 .build(metaDataPersistService);
-        transactionContexts = createTransactionContexts(metaDataContexts);
+        transactionContexts = new 
TransactionContextsBuilder(metaDataContexts.getMetaDataMap(), 
metaDataContexts.getGlobalRuleMetaData().getRules()).build();
     }
     
     private void afterBuildContextManager() {
@@ -207,18 +202,6 @@ public final class ClusterContextManagerBuilder implements 
ContextManagerBuilder
             each -> each, each -> 
metaDataPersistService.getSchemaRuleService().load(each), (oldValue, 
currentValue) -> oldValue, LinkedHashMap::new));
     }
     
-    private TransactionContexts createTransactionContexts(final 
MetaDataContexts metaDataContexts) {
-        Map<String, ShardingSphereTransactionManagerEngine> engines = new 
HashMap<>(metaDataContexts.getAllSchemaNames().size(), 1);
-        TransactionRule transactionRule = getTransactionRule(metaDataContexts);
-        for (String each : metaDataContexts.getAllSchemaNames()) {
-            ShardingSphereTransactionManagerEngine engine = new 
ShardingSphereTransactionManagerEngine();
-            ShardingSphereResource resource = 
metaDataContexts.getMetaData(each).getResource();
-            engine.init(resource.getDatabaseType(), resource.getDataSources(), 
transactionRule);
-            engines.put(each, engine);
-        }
-        return new TransactionContexts(engines);
-    }
-    
     private Map<String, Map<String, DataSource>> getChangedDataSources(final 
Map<String, Map<String, DataSourceConfiguration>> 
changedDataSourceConfigurations) {
         Map<String, Map<String, DataSource>> result = new 
LinkedHashMap<>(changedDataSourceConfigurations.size(), 1);
         for (Entry<String, Map<String, DataSourceConfiguration>> entry : 
changedDataSourceConfigurations.entrySet()) {
@@ -227,12 +210,6 @@ public final class ClusterContextManagerBuilder implements 
ContextManagerBuilder
         return result;
     }
     
-    private TransactionRule getTransactionRule(final MetaDataContexts 
metaDataContexts) {
-        Optional<TransactionRule> transactionRule = 
metaDataContexts.getGlobalRuleMetaData().getRules().stream().filter(
-            each -> each instanceof TransactionRule).map(each -> 
(TransactionRule) each).findFirst();
-        return transactionRule.orElseGet(() -> new TransactionRule(new 
DefaultTransactionRuleConfigurationBuilder().build()));
-    }
-    
     private void disableDataSources() {
         metaDataContexts.getMetaDataMap().forEach((key, value)
             -> value.getRuleMetaData().getRules().stream().filter(each -> each 
instanceof StatusContainedRule).forEach(each -> disableDataSources(key, 
(StatusContainedRule) each)));
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 dab5db8..9b9b084 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
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.mode.manager.memory;
 
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -29,17 +28,13 @@ import 
org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.MetaDataContextsBuilder;
 import org.apache.shardingsphere.schedule.core.api.ModeScheduleContext;
-import 
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
-import org.apache.shardingsphere.transaction.rule.TransactionRule;
-import 
org.apache.shardingsphere.transaction.rule.builder.DefaultTransactionRuleConfigurationBuilder;
+import 
org.apache.shardingsphere.transaction.context.TransactionContextsBuilder;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Properties;
 
 /**
@@ -54,30 +49,12 @@ public final class MemoryContextManagerBuilder implements 
ContextManagerBuilder
         Map<String, Collection<ShardingSphereRule>> rules = 
SchemaRulesBuilder.buildRules(dataSourcesMap, schemaRuleConfigs, props);
         Map<String, ShardingSphereSchema> schemas = new 
SchemaLoader(dataSourcesMap, schemaRuleConfigs, rules, props).load();
         MetaDataContexts metaDataContexts = new 
MetaDataContextsBuilder(dataSourcesMap, schemaRuleConfigs, globalRuleConfigs, 
schemas, rules, props).build(null);
-        TransactionContexts transactionContexts = 
createTransactionContexts(metaDataContexts);
+        TransactionContexts transactionContexts = new 
TransactionContextsBuilder(metaDataContexts.getMetaDataMap(), 
metaDataContexts.getGlobalRuleMetaData().getRules()).build();
         ContextManager result = new ContextManager();
         result.init(metaDataContexts, transactionContexts, new 
ModeScheduleContext(modeConfig));
         return result;
     }
     
-    private TransactionContexts createTransactionContexts(final 
MetaDataContexts metaDataContexts) {
-        Map<String, ShardingSphereTransactionManagerEngine> engines = new 
HashMap<>(metaDataContexts.getAllSchemaNames().size(), 1);
-        TransactionRule transactionRule = getTransactionRule(metaDataContexts);
-        for (String each : metaDataContexts.getAllSchemaNames()) {
-            ShardingSphereTransactionManagerEngine engine = new 
ShardingSphereTransactionManagerEngine();
-            ShardingSphereResource resource = 
metaDataContexts.getMetaData(each).getResource();
-            engine.init(resource.getDatabaseType(), resource.getDataSources(), 
transactionRule);
-            engines.put(each, engine);
-        }
-        return new TransactionContexts(engines);
-    }
-    
-    private TransactionRule getTransactionRule(final MetaDataContexts 
metaDataContexts) {
-        Optional<TransactionRule> transactionRule = 
metaDataContexts.getGlobalRuleMetaData().getRules().stream().filter(
-            each -> each instanceof TransactionRule).map(each -> 
(TransactionRule) each).findFirst();
-        return transactionRule.orElseGet(() -> new TransactionRule(new 
DefaultTransactionRuleConfigurationBuilder().build()));
-    }
-    
     @Override
     public String getType() {
         return "Memory";
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 4fc9f1b..c8e1770 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
@@ -22,7 +22,6 @@ import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration
 import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import 
org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration;
-import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.schema.loader.SchemaLoader;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -37,10 +36,8 @@ import 
org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRep
 import org.apache.shardingsphere.schedule.core.api.ModeScheduleContext;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.spi.typed.TypedSPIRegistry;
-import 
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
-import org.apache.shardingsphere.transaction.rule.TransactionRule;
-import 
org.apache.shardingsphere.transaction.rule.builder.DefaultTransactionRuleConfigurationBuilder;
+import 
org.apache.shardingsphere.transaction.context.TransactionContextsBuilder;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
@@ -49,7 +46,6 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import java.util.Properties;
 import java.util.stream.Collectors;
 
@@ -78,7 +74,7 @@ public final class StandaloneContextManagerBuilder implements 
ContextManagerBuil
         Map<String, ShardingSphereSchema> schemas = new 
SchemaLoader(standaloneDataSources, standaloneSchemaRules, rules, 
standaloneProps).load();
         MetaDataContexts metaDataContexts = new 
MetaDataContextsBuilder(standaloneDataSources, standaloneSchemaRules, 
metaDataPersistService.getGlobalRuleService().load(), schemas,
                 rules, standaloneProps).build(metaDataPersistService);
-        TransactionContexts transactionContexts = 
createTransactionContexts(metaDataContexts);
+        TransactionContexts transactionContexts = new 
TransactionContextsBuilder(metaDataContexts.getMetaDataMap(), 
metaDataContexts.getGlobalRuleMetaData().getRules()).build();
         ContextManager result = new ContextManager();
         result.init(metaDataContexts, transactionContexts, new 
ModeScheduleContext(modeConfig));
         return result;
@@ -175,24 +171,6 @@ public final class StandaloneContextManagerBuilder 
implements ContextManagerBuil
             each -> each, each -> 
metaDataPersistService.getSchemaRuleService().load(each), (oldValue, 
currentValue) -> oldValue, LinkedHashMap::new));
     }
     
-    private TransactionContexts createTransactionContexts(final 
MetaDataContexts metaDataContexts) {
-        Map<String, ShardingSphereTransactionManagerEngine> engines = new 
HashMap<>(metaDataContexts.getAllSchemaNames().size(), 1);
-        TransactionRule transactionRule = getTransactionRule(metaDataContexts);
-        for (String each : metaDataContexts.getAllSchemaNames()) {
-            ShardingSphereTransactionManagerEngine engine = new 
ShardingSphereTransactionManagerEngine();
-            ShardingSphereResource resource = 
metaDataContexts.getMetaData(each).getResource();
-            engine.init(resource.getDatabaseType(), resource.getDataSources(), 
transactionRule);
-            engines.put(each, engine);
-        }
-        return new TransactionContexts(engines);
-    }
-    
-    private TransactionRule getTransactionRule(final MetaDataContexts 
metaDataContexts) {
-        Optional<TransactionRule> transactionRule = 
metaDataContexts.getGlobalRuleMetaData().getRules().stream().filter(
-            each -> each instanceof TransactionRule).map(each -> 
(TransactionRule) each).findFirst();
-        return transactionRule.orElseGet(() -> new TransactionRule(new 
DefaultTransactionRuleConfigurationBuilder().build()));
-    }
-    
     @Override
     public String getType() {
         return "Standalone";

Reply via email to