From 69f8061634ec2a9a9c934ffafe8b66313cf20cb5 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sun, 3 Nov 2024 17:13:14 +0800 Subject: [PATCH] Refactor YamlShadowRuleConfigurationSwapper (#33511) --- .../YamlShadowRuleConfigurationSwapper.java | 62 ++++++++++--------- ...lShadowDataSourceConfigurationSwapper.java | 41 ++++++++++++ 2 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/datasource/YamlShadowDataSourceConfigurationSwapper.java diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowRuleConfigurationSwapper.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowRuleConfigurationSwapper.java index 3b5e56d5ba775..5505f10c9f5af 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowRuleConfigurationSwapper.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowRuleConfigurationSwapper.java @@ -17,85 +17,87 @@ package org.apache.shardingsphere.shadow.yaml.swapper; -import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper; +import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration; import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper; +import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper; import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration; import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.constant.ShadowOrder; import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration; import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration; +import org.apache.shardingsphere.shadow.yaml.config.table.YamlShadowTableConfiguration; +import org.apache.shardingsphere.shadow.yaml.swapper.datasource.YamlShadowDataSourceConfigurationSwapper; import org.apache.shardingsphere.shadow.yaml.swapper.table.YamlShadowTableConfigurationSwapper; import java.util.Collection; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; /** * YAML shadow rule configuration swapper. */ public final class YamlShadowRuleConfigurationSwapper implements YamlRuleConfigurationSwapper { - private final YamlShadowTableConfigurationSwapper tableConfigurationSwapper = new YamlShadowTableConfigurationSwapper(); + private final YamlShadowDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlShadowDataSourceConfigurationSwapper(); + + private final YamlShadowTableConfigurationSwapper tableConfigSwapper = new YamlShadowTableConfigurationSwapper(); private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper(); @Override public YamlShadowRuleConfiguration swapToYamlConfiguration(final ShadowRuleConfiguration data) { YamlShadowRuleConfiguration result = new YamlShadowRuleConfiguration(); + result.getDataSources().putAll(swapToYamlDataSources(data.getDataSources())); + result.getTables().putAll(swapToYamlShadowTables(data.getTables())); + result.getShadowAlgorithms().putAll(swapToYamlShadowAlgorithms(data.getShadowAlgorithms())); result.setDefaultShadowAlgorithmName(data.getDefaultShadowAlgorithmName()); setTableDefaultShadowDataSource(data.getTables(), data.getDataSources()); setTableDefaultShadowAlgorithm(data.getTables(), data.getDefaultShadowAlgorithmName()); - parseDataSources(data, result); - parseShadowTables(data, result); - parseShadowAlgorithms(data, result); return result; } - private void parseDataSources(final ShadowRuleConfiguration data, final YamlShadowRuleConfiguration yamlConfig) { - data.getDataSources().forEach(each -> yamlConfig.getDataSources().put(each.getName(), swapToDataSourceYamlConfiguration(each))); - } - - private YamlShadowDataSourceConfiguration swapToDataSourceYamlConfiguration(final ShadowDataSourceConfiguration data) { - YamlShadowDataSourceConfiguration result = new YamlShadowDataSourceConfiguration(); - result.setProductionDataSourceName(data.getProductionDataSourceName()); - result.setShadowDataSourceName(data.getShadowDataSourceName()); - return result; + private Map swapToYamlDataSources(final Collection dataSources) { + return dataSources.stream().collect(Collectors.toMap(ShadowDataSourceConfiguration::getName, dataSourceConfigSwapper::swapToYamlConfiguration, (a, b) -> b, LinkedHashMap::new)); } - private void parseShadowTables(final ShadowRuleConfiguration data, final YamlShadowRuleConfiguration yamlConfig) { - data.getTables().forEach((key, value) -> yamlConfig.getTables().put(key, tableConfigurationSwapper.swapToYamlConfiguration(value))); + private Map swapToYamlShadowTables(final Map data) { + return data.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> tableConfigSwapper.swapToYamlConfiguration(entry.getValue()), (a, b) -> b, LinkedHashMap::new)); } - private void parseShadowAlgorithms(final ShadowRuleConfiguration data, final YamlShadowRuleConfiguration yamlConfig) { - data.getShadowAlgorithms().forEach((key, value) -> yamlConfig.getShadowAlgorithms().put(key, algorithmSwapper.swapToYamlConfiguration(value))); + private Map swapToYamlShadowAlgorithms(final Map shadowAlgorithms) { + return shadowAlgorithms.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> algorithmSwapper.swapToYamlConfiguration(entry.getValue()), (a, b) -> b, LinkedHashMap::new)); } @Override public ShadowRuleConfiguration swapToObject(final YamlShadowRuleConfiguration yamlConfig) { ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - result.setDefaultShadowAlgorithmName(yamlConfig.getDefaultShadowAlgorithmName()); - parseYamlDataSources(yamlConfig, result); - parseYamlShadowTables(yamlConfig, result); - parseYamlShadowAlgorithms(yamlConfig, result); + result.setDataSources(swapToDataSources(yamlConfig.getDataSources())); + result.setTables(swapToShadowTables(yamlConfig.getTables())); + result.setShadowAlgorithms(swapToShadowAlgorithms(yamlConfig.getShadowAlgorithms())); setTableDefaultShadowDataSource(result.getTables(), result.getDataSources()); setTableDefaultShadowAlgorithm(result.getTables(), result.getDefaultShadowAlgorithmName()); + result.setDefaultShadowAlgorithmName(yamlConfig.getDefaultShadowAlgorithmName()); return result; } - private void parseYamlShadowAlgorithms(final YamlShadowRuleConfiguration yamlConfig, final ShadowRuleConfiguration data) { - yamlConfig.getShadowAlgorithms().forEach((key, value) -> data.getShadowAlgorithms().put(key, algorithmSwapper.swapToObject(value))); + private Collection swapToDataSources(final Map dataSources) { + return dataSources.entrySet().stream().map(entry -> swapToDataSource(entry.getKey(), entry.getValue())).collect(Collectors.toList()); } - private void parseYamlShadowTables(final YamlShadowRuleConfiguration yamlConfig, final ShadowRuleConfiguration data) { - yamlConfig.getTables().forEach((key, value) -> data.getTables().put(key, tableConfigurationSwapper.swapToObject(value))); + private ShadowDataSourceConfiguration swapToDataSource(final String name, final YamlShadowDataSourceConfiguration yamlConfig) { + return new ShadowDataSourceConfiguration(name, yamlConfig.getProductionDataSourceName(), yamlConfig.getShadowDataSourceName()); } - private void parseYamlDataSources(final YamlShadowRuleConfiguration yamlConfig, final ShadowRuleConfiguration data) { - yamlConfig.getDataSources().forEach((key, value) -> data.getDataSources().add(swapToDataSourceObject(key, value))); + private Map swapToShadowTables(final Map tables) { + return tables.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> tableConfigSwapper.swapToObject(entry.getValue()), (a, b) -> b, LinkedHashMap::new)); } - private ShadowDataSourceConfiguration swapToDataSourceObject(final String name, final YamlShadowDataSourceConfiguration yamlConfig) { - return new ShadowDataSourceConfiguration(name, yamlConfig.getProductionDataSourceName(), yamlConfig.getShadowDataSourceName()); + private Map swapToShadowAlgorithms(final Map shadowAlgorithms) { + return shadowAlgorithms.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> algorithmSwapper.swapToObject(entry.getValue()), (a, b) -> b, LinkedHashMap::new)); } private void setTableDefaultShadowDataSource(final Map shadowTables, final Collection shadowDataSources) { diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/datasource/YamlShadowDataSourceConfigurationSwapper.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/datasource/YamlShadowDataSourceConfigurationSwapper.java new file mode 100644 index 0000000000000..06007bcb61a58 --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/datasource/YamlShadowDataSourceConfigurationSwapper.java @@ -0,0 +1,41 @@ +/* + * 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.shadow.yaml.swapper.datasource; + +import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper; +import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration; +import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration; + +/** + * YAML shadow data source configuration swapper. + */ +public final class YamlShadowDataSourceConfigurationSwapper implements YamlConfigurationSwapper { + + @Override + public YamlShadowDataSourceConfiguration swapToYamlConfiguration(final ShadowDataSourceConfiguration data) { + YamlShadowDataSourceConfiguration result = new YamlShadowDataSourceConfiguration(); + result.setProductionDataSourceName(data.getProductionDataSourceName()); + result.setShadowDataSourceName(data.getShadowDataSourceName()); + return result; + } + + @Override + public ShadowDataSourceConfiguration swapToObject(final YamlShadowDataSourceConfiguration yamlConfig) { + throw new UnsupportedOperationException(); + } +}