You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2024/03/13 02:49:08 UTC

(shardingsphere) branch master updated: Add SingleImportRuleConfigurationProvider (#30470)

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

jianglongtao 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 91d1715cc08 Add SingleImportRuleConfigurationProvider (#30470)
91d1715cc08 is described below

commit 91d1715cc08802de27e23e1808742ccc31cdf0ad
Author: yx9o <ya...@163.com>
AuthorDate: Wed Mar 13 10:49:01 2024 +0800

    Add SingleImportRuleConfigurationProvider (#30470)
    
    * Add SingleImportRuleConfigurationProvider
    
    * Update
---
 .../SingleImportRuleConfigurationProvider.java     | 53 ++++++++++++++++++++++
 ...le.spi.database.ImportRuleConfigurationProvider | 18 ++++++++
 .../YamlDatabaseConfigurationImportExecutor.java   | 24 ++--------
 3 files changed, 75 insertions(+), 20 deletions(-)

diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/provider/SingleImportRuleConfigurationProvider.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/provider/SingleImportRuleConfigurationProvider.java
new file mode 100644
index 00000000000..b637fd4b981
--- /dev/null
+++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/provider/SingleImportRuleConfigurationProvider.java
@@ -0,0 +1,53 @@
+/*
+ * 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.single.distsql.handler.provider;
+
+import org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
+import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
+import org.apache.shardingsphere.single.rule.SingleRule;
+
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+/**
+ * Single import rule configuration provider.
+ */
+public final class SingleImportRuleConfigurationProvider implements ImportRuleConfigurationProvider {
+    
+    @Override
+    public void check(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig) {
+    }
+    
+    @Override
+    public DatabaseRule build(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig, final InstanceContext instanceContext) {
+        return new SingleRule((SingleRuleConfiguration) ruleConfig, database.getName(), database.getProtocolType(),
+                database.getResourceMetaData().getStorageUnits().entrySet().stream()
+                        .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
+                database.getRuleMetaData().getRules());
+    }
+    
+    @Override
+    public Class<? extends RuleConfiguration> getType() {
+        return SingleRuleConfiguration.class;
+    }
+}
diff --git a/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider b/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
new file mode 100644
index 00000000000..ecea9ee9d59
--- /dev/null
+++ b/kernel/single/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.single.distsql.handler.provider.SingleImportRuleConfigurationProvider
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
index 7720a2984dd..9a0dcddf56e 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
@@ -46,8 +46,6 @@ import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConf
 import org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.exception.MissingDatabaseNameException;
-import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
-import org.apache.shardingsphere.single.rule.SingleRule;
 
 import java.sql.SQLException;
 import java.util.Collection;
@@ -58,7 +56,6 @@ import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.TreeMap;
-import java.util.stream.Collectors;
 
 /**
  * Yaml database configuration import executor.
@@ -139,14 +136,10 @@ public final class YamlDatabaseConfigurationImportExecutor {
     }
     
     private void addRule(final Collection<RuleConfiguration> ruleConfigs, final RuleConfiguration ruleConfig, final ShardingSphereDatabase database) {
-        if (ruleConfig instanceof SingleRuleConfiguration) {
-            addSingleRuleConfiguration((SingleRuleConfiguration) ruleConfig, ruleConfigs, database);
-        } else {
-            ImportRuleConfigurationProvider provider = TypedSPILoader.getService(ImportRuleConfigurationProvider.class, ruleConfig.getClass());
-            provider.check(database, ruleConfig);
-            ruleConfigs.add(ruleConfig);
-            database.getRuleMetaData().getRules().add(provider.build(database, ruleConfig, ProxyContext.getInstance().getContextManager().getInstanceContext()));
-        }
+        ImportRuleConfigurationProvider provider = TypedSPILoader.getService(ImportRuleConfigurationProvider.class, ruleConfig.getClass());
+        provider.check(database, ruleConfig);
+        ruleConfigs.add(ruleConfig);
+        database.getRuleMetaData().getRules().add(provider.build(database, ruleConfig, ProxyContext.getInstance().getContextManager().getInstanceContext()));
     }
     
     @SuppressWarnings({"rawtypes", "unchecked"})
@@ -160,15 +153,6 @@ public final class YamlDatabaseConfigurationImportExecutor {
         return result;
     }
     
-    private void addSingleRuleConfiguration(final SingleRuleConfiguration ruleConfig, final Collection<RuleConfiguration> ruleConfigs, final ShardingSphereDatabase database) {
-        ruleConfigs.add(ruleConfig);
-        database.getRuleMetaData().getRules().add(
-                new SingleRule(ruleConfig, database.getName(), database.getProtocolType(),
-                        database.getResourceMetaData().getStorageUnits().entrySet().stream()
-                                .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
-                        database.getRuleMetaData().getRules()));
-    }
-    
     private void dropDatabase(final String databaseName) {
         ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager().dropDatabase(databaseName);
     }