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

sunnianjun 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 d7ff477ff34 Merge YamlDataNodeRuleConfigurationSwapperEngine and 
YamlDataNodeGlobalRuleConfigurationSwapperEngine (#31049)
d7ff477ff34 is described below

commit d7ff477ff347f4e784c1691fb9eb5392345aef93
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Apr 28 23:10:15 2024 +0800

    Merge YamlDataNodeRuleConfigurationSwapperEngine and 
YamlDataNodeGlobalRuleConfigurationSwapperEngine (#31049)
    
    * Merge YamlDataNodeRuleConfigurationSwapperEngine and 
YamlDataNodeGlobalRuleConfigurationSwapperEngine
    
    * Refactor YamlDataNodeRuleConfigurationSwapperEngine
---
 ...taNodeGlobalRuleConfigurationSwapperEngine.java | 77 ----------------------
 ...YamlDataNodeRuleConfigurationSwapperEngine.java | 32 +++++----
 .../database/type/DropDatabaseRuleOperator.java    |  5 +-
 .../database/rule/DatabaseRulePersistService.java  |  7 +-
 .../config/global/GlobalRulePersistService.java    | 10 +--
 .../context/ConfigurationContextManager.java       |  5 +-
 6 files changed, 33 insertions(+), 103 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/YamlDataNodeGlobalRuleConfigurationSwapperEngine.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/YamlDataNodeGlobalRuleConfigurationSwapperEngine.java
deleted file mode 100644
index c5b6fd35971..00000000000
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/YamlDataNodeGlobalRuleConfigurationSwapperEngine.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * 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.infra.yaml.config.swapper.rule;
-
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
-import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
-import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlDataNodeConfigurationSwapper;
-
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Optional;
-
-/**
- * YAML data node global rule configuration swapper engine.
- */
-public final class YamlDataNodeGlobalRuleConfigurationSwapperEngine {
-    
-    /**
-     * Swap to YAML global rule configurations.
-     *
-     * @param ruleConfigs rule configurations
-     * @return YAML global rule configurations
-     */
-    @SuppressWarnings("rawtypes")
-    public Map<RuleConfiguration, YamlDataNodeConfigurationSwapper> 
swapToYamlRuleConfigurations(final Collection<RuleConfiguration> ruleConfigs) {
-        return 
OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
ruleConfigs);
-    }
-    
-    /**
-     * Swap from YAML global rule configurations to rule configurations.
-     *
-     * @param repositoryTuples repository tuples
-     * @return global rule configurations
-     */
-    @SuppressWarnings({"unchecked", "rawtypes"})
-    public Collection<RuleConfiguration> swapToRuleConfigurations(final 
Collection<RepositoryTuple> repositoryTuples) {
-        Collection<RuleConfiguration> result = new LinkedList<>();
-        for (YamlDataNodeConfigurationSwapper each : 
OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class)) {
-            each.swapToObject(repositoryTuples).ifPresent(optional -> 
result.add((RuleConfiguration) optional));
-        }
-        return result;
-    }
-    
-    /**
-     * Swap from single YAML global rule configuration to rule configurations.
-     *
-     * @param ruleName rule name
-     * @param repositoryTuples repository tuples
-     * @return global rule configuration
-     */
-    @SuppressWarnings({"unchecked", "rawtypes"})
-    public Optional<RuleConfiguration> swapSingleRuleToRuleConfiguration(final 
String ruleName, final Collection<RepositoryTuple> repositoryTuples) {
-        for (YamlDataNodeConfigurationSwapper each : 
OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class)) {
-            if (ruleName.equals(each.getRuleTagName().toLowerCase())) {
-                return each.swapToObject(repositoryTuples);
-            }
-        }
-        return Optional.empty();
-    }
-}
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/YamlDataNodeRuleConfigurationSwapperEngine.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/YamlDataNodeRuleConfigurationSwapperEngine.java
index 08ee2e80979..7c5c3131ac2 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/YamlDataNodeRuleConfigurationSwapperEngine.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/rule/YamlDataNodeRuleConfigurationSwapperEngine.java
@@ -24,7 +24,7 @@ import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlDataNodeConfigurati
 
 import java.util.Collection;
 import java.util.LinkedList;
-import java.util.Map;
+import java.util.Optional;
 
 /**
  * YAML data node rule configuration swapper engine.
@@ -32,28 +32,34 @@ import java.util.Map;
 public final class YamlDataNodeRuleConfigurationSwapperEngine {
     
     /**
-     * Swap to YAML rule configurations.
+     * Swap from YAML global rule configurations to rule configurations.
      *
-     * @param ruleConfigs rule configurations
-     * @return YAML rule configurations
+     * @param repositoryTuples repository tuples
+     * @return global rule configurations
      */
-    @SuppressWarnings("rawtypes")
-    public Map<RuleConfiguration, YamlDataNodeConfigurationSwapper> 
swapToYamlRuleConfigurations(final Collection<RuleConfiguration> ruleConfigs) {
-        return 
OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
ruleConfigs);
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public Collection<RuleConfiguration> swapToRuleConfigurations(final 
Collection<RepositoryTuple> repositoryTuples) {
+        Collection<RuleConfiguration> result = new LinkedList<>();
+        for (YamlDataNodeConfigurationSwapper each : 
OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class)) {
+            each.swapToObject(repositoryTuples).ifPresent(optional -> 
result.add((RuleConfiguration) optional));
+        }
+        return result;
     }
     
     /**
-     * Swap from YAML rule configurations to rule configurations.
+     * Swap from single YAML global rule configuration to rule configurations.
      *
+     * @param ruleName rule name
      * @param repositoryTuples repository tuples
-     * @return rule configurations
+     * @return global rule configuration
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
-    public Collection<RuleConfiguration> swapToRuleConfigurations(final 
Collection<RepositoryTuple> repositoryTuples) {
-        Collection<RuleConfiguration> result = new LinkedList<>();
+    public Optional<RuleConfiguration> swapSingleRuleToRuleConfiguration(final 
String ruleName, final Collection<RepositoryTuple> repositoryTuples) {
         for (YamlDataNodeConfigurationSwapper each : 
OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class)) {
-            each.swapToObject(repositoryTuples).ifPresent(optional -> 
result.add((RuleConfiguration) optional));
+            if (ruleName.equals(each.getRuleTagName().toLowerCase())) {
+                return each.swapToObject(repositoryTuples);
+            }
         }
-        return result;
+        return Optional.empty();
     }
 }
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperator.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperator.java
index a5f6d430a40..fbbc7fef8b5 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperator.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/engine/update/rdl/rule/engine/database/type/DropDatabaseRuleOperator.java
@@ -28,7 +28,8 @@ import 
org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
 import 
org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapperEngine;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlDataNodeConfigurationSwapper;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 
 import java.util.Collection;
@@ -62,7 +63,7 @@ public final class DropDatabaseRuleOperator implements 
DatabaseRuleOperator {
         modeContextManager.removeRuleConfigurationItem(database.getName(), 
toBeDroppedRuleConfig);
         RuleConfiguration toBeAlteredRuleConfig = 
executor.buildToBeAlteredRuleConfiguration(sqlStatement);
         if (null != toBeAlteredRuleConfig && ((DatabaseRuleConfiguration) 
toBeAlteredRuleConfig).isEmpty()) {
-            new 
YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(Collections.singleton(currentRuleConfig)).values().stream().findFirst()
+            
OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
Collections.singleton(currentRuleConfig)).values().stream().findFirst()
                     .ifPresent(optional -> 
modeContextManager.removeRuleConfiguration(database.getName(), 
optional.getRuleTagName().toLowerCase()));
             return Collections.emptyList();
         }
diff --git 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java
 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java
index 6d54596810f..cc66c1d9218 100644
--- 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java
+++ 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/rule/DatabaseRulePersistService.java
@@ -20,6 +20,7 @@ package 
org.apache.shardingsphere.metadata.persist.service.config.database.rule;
 import com.google.common.base.Strings;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
 import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
 import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlDataNodeConfigurationSwapper;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapperEngine;
@@ -53,7 +54,7 @@ public final class DatabaseRulePersistService implements 
DatabaseBasedPersistSer
     @SuppressWarnings({"unchecked", "rawtypes"})
     @Override
     public void persist(final String databaseName, final 
Collection<RuleConfiguration> configs) {
-        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: new 
YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs).entrySet())
 {
+        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
configs).entrySet()) {
             Collection<RepositoryTuple> repositoryTuples = 
entry.getValue().swapToRepositoryTuples(entry.getKey());
             if (!repositoryTuples.isEmpty()) {
                 persistDataNodes(databaseName, 
entry.getValue().getRuleTagName().toLowerCase(), repositoryTuples);
@@ -76,7 +77,7 @@ public final class DatabaseRulePersistService implements 
DatabaseBasedPersistSer
     @Override
     public Collection<MetaDataVersion> persistConfigurations(final String 
databaseName, final Collection<RuleConfiguration> configs) {
         Collection<MetaDataVersion> result = new LinkedList<>();
-        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: new 
YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs).entrySet())
 {
+        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
configs).entrySet()) {
             Collection<RepositoryTuple> repositoryTuples = 
entry.getValue().swapToRepositoryTuples(entry.getKey());
             if (!repositoryTuples.isEmpty()) {
                 result.addAll(persistDataNodes(databaseName, 
entry.getValue().getRuleTagName().toLowerCase(), repositoryTuples));
@@ -103,7 +104,7 @@ public final class DatabaseRulePersistService implements 
DatabaseBasedPersistSer
     @Override
     public Collection<MetaDataVersion> deleteConfigurations(final String 
databaseName, final Collection<RuleConfiguration> configs) {
         Collection<MetaDataVersion> result = new LinkedList<>();
-        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: new 
YamlDataNodeRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs).entrySet())
 {
+        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
configs).entrySet()) {
             Collection<RepositoryTuple> repositoryTuples = 
entry.getValue().swapToRepositoryTuples(entry.getKey());
             if (repositoryTuples.isEmpty()) {
                 continue;
diff --git 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java
 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java
index 02a69bf1e72..8ef88320743 100644
--- 
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java
+++ 
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java
@@ -20,9 +20,10 @@ package 
org.apache.shardingsphere.metadata.persist.service.config.global;
 import com.google.common.base.Strings;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
 import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
 import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlDataNodeConfigurationSwapper;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapperEngine;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapperEngine;
 import org.apache.shardingsphere.metadata.persist.node.GlobalNode;
 import 
org.apache.shardingsphere.metadata.persist.service.config.RepositoryTuplePersistService;
 import 
org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService;
@@ -57,8 +58,7 @@ public final class GlobalRulePersistService implements 
GlobalPersistService<Coll
     @Override
     public void persist(final Collection<RuleConfiguration> globalRuleConfigs) 
{
         Collection<MetaDataVersion> metaDataVersions = new LinkedList<>();
-        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: new YamlDataNodeGlobalRuleConfigurationSwapperEngine()
-                .swapToYamlRuleConfigurations(globalRuleConfigs).entrySet()) {
+        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
globalRuleConfigs).entrySet()) {
             Collection<RepositoryTuple> repositoryTuples = 
entry.getValue().swapToRepositoryTuples(entry.getKey());
             if (!repositoryTuples.isEmpty()) {
                 metaDataVersions.addAll(persistTuples(repositoryTuples));
@@ -84,12 +84,12 @@ public final class GlobalRulePersistService implements 
GlobalPersistService<Coll
     @Override
     public Collection<RuleConfiguration> load() {
         Collection<RepositoryTuple> repositoryTuples = 
repositoryTuplePersistService.loadRepositoryTuples(GlobalNode.getGlobalRuleRootNode());
-        return repositoryTuples.isEmpty() ? Collections.emptyList() : new 
YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapToRuleConfigurations(repositoryTuples);
+        return repositoryTuples.isEmpty() ? Collections.emptyList() : new 
YamlDataNodeRuleConfigurationSwapperEngine().swapToRuleConfigurations(repositoryTuples);
     }
     
     @Override
     public RuleConfiguration load(final String ruleName) {
         Collection<RepositoryTuple> repositoryTuples = 
repositoryTuplePersistService.loadRepositoryTuples(GlobalNode.getGlobalRuleNode(ruleName));
-        return new 
YamlDataNodeGlobalRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName,
 repositoryTuples).orElse(null);
+        return new 
YamlDataNodeRuleConfigurationSwapperEngine().swapSingleRuleToRuleConfiguration(ruleName,
 repositoryTuples).orElse(null);
     }
 }
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java
index 73831f41e9e..4f4045c1fd6 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java
@@ -38,8 +38,8 @@ import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import 
org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder;
 import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
 import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlDataNodeConfigurationSwapper;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapperEngine;
 import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory;
 import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory;
 import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService;
@@ -376,8 +376,7 @@ public final class ConfigurationContextManager {
     @SuppressWarnings("rawtypes")
     @SneakyThrows(Exception.class)
     private void closeStaleTransactionRule(final RuleConfiguration ruleConfig) 
{
-        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: new YamlDataNodeGlobalRuleConfigurationSwapperEngine()
-                
.swapToYamlRuleConfigurations(Collections.singleton(ruleConfig)).entrySet()) {
+        for (Entry<RuleConfiguration, YamlDataNodeConfigurationSwapper> entry 
: OrderedSPILoader.getServices(YamlDataNodeConfigurationSwapper.class, 
Collections.singleton(ruleConfig)).entrySet()) {
             if 
("transaction".equalsIgnoreCase(entry.getValue().getRuleTagName())) {
                 Optional<TransactionRule> transactionRule = 
metaDataContexts.get().getMetaData().getGlobalRuleMetaData().findSingleRule(TransactionRule.class);
                 if (!transactionRule.isPresent()) {

Reply via email to