Skip to content

Commit

Permalink
Refactor YamlShadowRuleConfigurationSwapper (#33511)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 3, 2024
1 parent a7c45b8 commit 69f8061
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<YamlShadowRuleConfiguration, ShadowRuleConfiguration> {

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<String, YamlShadowDataSourceConfiguration> swapToYamlDataSources(final Collection<ShadowDataSourceConfiguration> 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<String, YamlShadowTableConfiguration> swapToYamlShadowTables(final Map<String, ShadowTableConfiguration> 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<String, YamlAlgorithmConfiguration> swapToYamlShadowAlgorithms(final Map<String, AlgorithmConfiguration> 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<ShadowDataSourceConfiguration> swapToDataSources(final Map<String, YamlShadowDataSourceConfiguration> 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<String, ShadowTableConfiguration> swapToShadowTables(final Map<String, YamlShadowTableConfiguration> 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<String, AlgorithmConfiguration> swapToShadowAlgorithms(final Map<String, YamlAlgorithmConfiguration> 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<String, ShadowTableConfiguration> shadowTables, final Collection<ShadowDataSourceConfiguration> shadowDataSources) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<YamlShadowDataSourceConfiguration, ShadowDataSourceConfiguration> {

@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();
}
}

0 comments on commit 69f8061

Please sign in to comment.