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

zhaojinchao 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 88f5109e94a Refactor ImportDatabaseConfigurationHandler (#18493)
88f5109e94a is described below

commit 88f5109e94a63e8a9b6e34ef514ca10ef4314a7c
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Jun 21 20:10:10 2022 +0800

    Refactor ImportDatabaseConfigurationHandler (#18493)
---
 .../ImportResourceNotExistedException.java         | 33 ------------------
 .../ImportDatabaseNotExistedException.java         | 34 ------------------
 .../ImportDatabaseConfigurationHandler.java        | 40 ++++++++++------------
 3 files changed, 19 insertions(+), 88 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/ImportResourceNotExistedException.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/ImportResourceNotExistedException.java
deleted file mode 100644
index 955a60027e3..00000000000
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/ImportResourceNotExistedException.java
+++ /dev/null
@@ -1,33 +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.distsql.exception.resource;
-
-import lombok.Getter;
-
-/**
- * Import resource does not exist exception.
- */
-@Getter
-public final class ImportResourceNotExistedException extends 
ResourceDefinitionViolationException {
-    
-    private static final long serialVersionUID = -7044446309219726180L;
-    
-    public ImportResourceNotExistedException(final String fileName) {
-        super(1105, String.format("Data sources configuration in file `%s` is 
required.", fileName));
-    }
-}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/ImportDatabaseNotExistedException.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/ImportDatabaseNotExistedException.java
deleted file mode 100644
index 47b6f4dd0db..00000000000
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/ImportDatabaseNotExistedException.java
+++ /dev/null
@@ -1,34 +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.exception;
-
-import lombok.Getter;
-import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
-
-/**
- * Import database does not exist exception.
- */
-@Getter
-public final class ImportDatabaseNotExistedException extends DistSQLException {
-    
-    private static final long serialVersionUID = 4803138422791056535L;
-    
-    public ImportDatabaseNotExistedException(final String fileName) {
-        super(1106, String.format("Property `databaseName` in file `%s` is 
required.", fileName));
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/ImportDatabaseConfigurationHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/ImportDatabaseConfigurationHandler.java
index 7f5ce7019e2..1ea763b128f 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/ImportDatabaseConfigurationHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/ImportDatabaseConfigurationHandler.java
@@ -17,7 +17,7 @@
 
 package 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.updatable;
 
-import com.google.common.base.Strings;
+import com.google.common.base.Preconditions;
 import com.zaxxer.hikari.HikariDataSource;
 import 
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
@@ -31,9 +31,7 @@ import 
org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
 import 
org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
 import 
org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesValidator;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
-import 
org.apache.shardingsphere.infra.distsql.exception.resource.ImportResourceNotExistedException;
 import 
org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourcesException;
-import 
org.apache.shardingsphere.infra.exception.ImportDatabaseNotExistedException;
 import org.apache.shardingsphere.infra.exception.DatabaseNotExistedException;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -41,7 +39,6 @@ import 
org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration;
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper;
@@ -68,7 +65,6 @@ import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -88,32 +84,35 @@ public final class ImportDatabaseConfigurationHandler 
extends UpdatableRALBacken
     
     @Override
     protected void update(final ContextManager contextManager) throws 
DistSQLException {
-        File yamlFile = new File(getSqlStatement().getFilePath());
+        File file = new File(getSqlStatement().getFilePath());
         YamlProxyDatabaseConfiguration yamlConfig;
         try {
-            yamlConfig = YamlEngine.unmarshal(yamlFile, 
YamlProxyDatabaseConfiguration.class);
+            yamlConfig = YamlEngine.unmarshal(file, 
YamlProxyDatabaseConfiguration.class);
         } catch (final IOException ex) {
             throw new ShardingSphereException(ex);
         }
         String databaseName = yamlConfig.getDatabaseName();
-        DistSQLException.predictionThrow(!Strings.isNullOrEmpty(databaseName), 
() -> new ImportDatabaseNotExistedException(yamlFile.getName()));
-        checkDatabaseName(databaseName);
-        DistSQLException.predictionThrow(null != yamlConfig.getDataSources() 
&& !yamlConfig.getDataSources().isEmpty(), () -> new 
ImportResourceNotExistedException(yamlFile.getName()));
-        alterResourcesConfig(databaseName, yamlConfig.getDataSources());
-        alterRulesConfig(databaseName, yamlConfig.getRules());
+        checkDatabaseName(databaseName, file);
+        checkDataSource(yamlConfig.getDataSources(), file);
+        updateResources(databaseName, yamlConfig.getDataSources());
+        updateRules(databaseName, yamlConfig.getRules());
     }
     
-    private void checkDatabaseName(final String databaseName) {
+    private void checkDatabaseName(final String databaseName, final File file) 
{
+        Preconditions.checkNotNull(databaseName, String.format("Property 
`databaseName` in file `%s` is required.", file.getName()));
         if 
(!ProxyContext.getInstance().getAllDatabaseNames().contains(databaseName)) {
             throw new DatabaseNotExistedException(databaseName);
         }
     }
     
-    private void alterResourcesConfig(final String databaseName, final 
Map<String, YamlProxyDataSourceConfiguration> yamlDataSourceMap) throws 
DistSQLException {
+    private void checkDataSource(final Map<String, 
YamlProxyDataSourceConfiguration> dataSources, final File file) {
+        Preconditions.checkState(!dataSources.isEmpty(), "Data sources 
configuration in file `%s` is required.", file.getName());
+    }
+    
+    private void updateResources(final String databaseName, final Map<String, 
YamlProxyDataSourceConfiguration> yamlDataSourceMap) throws DistSQLException {
         Map<String, DataSourceProperties> toBeUpdatedResourcePropsMap = new 
LinkedHashMap<>(yamlDataSourceMap.size(), 1);
-        for (Entry<String, YamlProxyDataSourceConfiguration> each : 
yamlDataSourceMap.entrySet()) {
-            DataSourceProperties dataSourceProps = 
DataSourcePropertiesCreator.create(HikariDataSource.class.getName(), 
dataSourceConfigSwapper.swap(each.getValue()));
-            toBeUpdatedResourcePropsMap.put(each.getKey(), dataSourceProps);
+        for (Entry<String, YamlProxyDataSourceConfiguration> entry : 
yamlDataSourceMap.entrySet()) {
+            toBeUpdatedResourcePropsMap.put(entry.getKey(), 
DataSourcePropertiesCreator.create(HikariDataSource.class.getName(), 
dataSourceConfigSwapper.swap(entry.getValue())));
         }
         try {
             validator.validate(toBeUpdatedResourcePropsMap);
@@ -128,8 +127,8 @@ public final class ImportDatabaseConfigurationHandler 
extends UpdatableRALBacken
         }
     }
     
-    private void alterRulesConfig(final String databaseName, final 
Collection<YamlRuleConfiguration> yamlRuleConfigs) throws DistSQLException {
-        if (null == yamlRuleConfigs || yamlRuleConfigs.isEmpty()) {
+    private void updateRules(final String databaseName, final 
Collection<YamlRuleConfiguration> yamlRuleConfigs) throws DistSQLException {
+        if (yamlRuleConfigs.isEmpty()) {
             return;
         }
         Collection<RuleConfiguration> toBeUpdatedRuleConfigs = new 
LinkedList<>();
@@ -162,7 +161,6 @@ public final class ImportDatabaseConfigurationHandler 
extends UpdatableRALBacken
         database.getRuleMetaData().getConfigurations().clear();
         
database.getRuleMetaData().getConfigurations().addAll(toBeUpdatedRuleConfigs);
         
ProxyContext.getInstance().getContextManager().renewMetaDataContexts(metaDataContexts);
-        Optional<MetaDataPersistService> metaDataPersistService = 
metaDataContexts.getPersistService();
-        metaDataPersistService.ifPresent(optional -> 
optional.getDatabaseRulePersistService().persist(databaseName, 
toBeUpdatedRuleConfigs));
+        metaDataContexts.getPersistService().ifPresent(optional -> 
optional.getDatabaseRulePersistService().persist(databaseName, 
toBeUpdatedRuleConfigs));
     }
 }

Reply via email to