Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RepositoryTupleSwapper's impl #31061

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;
import org.apache.shardingsphere.mode.path.RuleNodePath;
import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -37,27 +37,23 @@
*/
public final class BroadcastRuleConfigurationRepositoryTupleSwapper implements RepositoryTupleSwapper<BroadcastRuleConfiguration> {

private final YamlBroadcastRuleConfigurationSwapper ruleConfigSwapper = new YamlBroadcastRuleConfigurationSwapper();

private final RuleNodePath broadcastRuleNodePath = new BroadcastRuleNodePathProvider().getRuleNodePath();

@Override
public Collection<RepositoryTuple> swapToRepositoryTuples(final BroadcastRuleConfiguration data) {
return data.getTables().isEmpty()
? Collections.emptyList()
: Collections.singleton(new RepositoryTuple(BroadcastRuleNodePathProvider.TABLES, YamlEngine.marshal(swapToTablesYamlConfiguration(data))));
}

private YamlBroadcastRuleConfiguration swapToTablesYamlConfiguration(final BroadcastRuleConfiguration data) {
YamlBroadcastRuleConfiguration result = new YamlBroadcastRuleConfiguration();
result.getTables().addAll(data.getTables());
return result;
: Collections.singleton(new RepositoryTuple(BroadcastRuleNodePathProvider.TABLES, YamlEngine.marshal(ruleConfigSwapper.swapToYamlConfiguration(data))));
}

@Override
public Optional<BroadcastRuleConfiguration> swapToObject(final Collection<RepositoryTuple> repositoryTuples) {
List<RepositoryTuple> validTuples = repositoryTuples.stream().filter(each -> broadcastRuleNodePath.getRoot().isValidatedPath(each.getKey())).collect(Collectors.toList());
for (RepositoryTuple each : validTuples) {
if (broadcastRuleNodePath.getRoot().isValidatedPath(each.getKey())) {
return Optional.of(new BroadcastRuleConfiguration(YamlEngine.unmarshal(each.getValue(), YamlBroadcastRuleConfiguration.class).getTables()));
return Optional.of(ruleConfigSwapper.swapToObject(YamlEngine.unmarshal(each.getValue(), YamlBroadcastRuleConfiguration.class)));
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@
package org.apache.shardingsphere.encrypt.yaml.swapper;

import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
import org.apache.shardingsphere.encrypt.metadata.nodepath.EncryptRuleNodePathProvider;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlEncryptTableRuleConfigurationSwapper;
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.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;
import org.apache.shardingsphere.mode.path.RuleNodePath;
import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;

import java.util.Collection;
import java.util.LinkedHashMap;
Expand All @@ -45,22 +42,19 @@
*/
public final class EncryptRuleConfigurationRepositoryTupleSwapper implements RepositoryTupleSwapper<EncryptRuleConfiguration> {

private final YamlEncryptTableRuleConfigurationSwapper tableSwapper = new YamlEncryptTableRuleConfigurationSwapper();

private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
private final YamlEncryptRuleConfigurationSwapper ruleConfigSwapper = new YamlEncryptRuleConfigurationSwapper();

private final RuleNodePath encryptRuleNodePath = new EncryptRuleNodePathProvider().getRuleNodePath();

@Override
public Collection<RepositoryTuple> swapToRepositoryTuples(final EncryptRuleConfiguration data) {
Collection<RepositoryTuple> result = new LinkedList<>();
for (Entry<String, AlgorithmConfiguration> entry : data.getEncryptors().entrySet()) {
result.add(new RepositoryTuple(
encryptRuleNodePath.getNamedItem(EncryptRuleNodePathProvider.ENCRYPTORS).getPath(entry.getKey()), YamlEngine.marshal(algorithmSwapper.swapToYamlConfiguration(entry.getValue()))));
YamlEncryptRuleConfiguration yamlConfig = ruleConfigSwapper.swapToYamlConfiguration(data);
for (Entry<String, YamlAlgorithmConfiguration> entry : yamlConfig.getEncryptors().entrySet()) {
result.add(new RepositoryTuple(encryptRuleNodePath.getNamedItem(EncryptRuleNodePathProvider.ENCRYPTORS).getPath(entry.getKey()), YamlEngine.marshal(entry.getValue())));
}
for (EncryptTableRuleConfiguration each : data.getTables()) {
result.add(new RepositoryTuple(
encryptRuleNodePath.getNamedItem(EncryptRuleNodePathProvider.TABLES).getPath(each.getName()), YamlEngine.marshal(tableSwapper.swapToYamlConfiguration(each))));
for (YamlEncryptTableRuleConfiguration each : yamlConfig.getTables().values()) {
result.add(new RepositoryTuple(encryptRuleNodePath.getNamedItem(EncryptRuleNodePathProvider.TABLES).getPath(each.getName()), YamlEngine.marshal(each)));
}
return result;
}
Expand All @@ -71,15 +65,18 @@ public Optional<EncryptRuleConfiguration> swapToObject(final Collection<Reposito
if (validTuples.isEmpty()) {
return Optional.empty();
}
Collection<EncryptTableRuleConfiguration> tables = new LinkedList<>();
Map<String, AlgorithmConfiguration> encryptors = new LinkedHashMap<>();
Map<String, YamlEncryptTableRuleConfiguration> tables = new LinkedHashMap<>();
Map<String, YamlAlgorithmConfiguration> encryptors = new LinkedHashMap<>();
for (RepositoryTuple each : validTuples) {
encryptRuleNodePath.getNamedItem(EncryptRuleNodePathProvider.TABLES).getName(each.getKey())
.ifPresent(optional -> tables.add(tableSwapper.swapToObject(YamlEngine.unmarshal(each.getValue(), YamlEncryptTableRuleConfiguration.class))));
.ifPresent(optional -> tables.put(optional, YamlEngine.unmarshal(each.getValue(), YamlEncryptTableRuleConfiguration.class)));
encryptRuleNodePath.getNamedItem(EncryptRuleNodePathProvider.ENCRYPTORS).getName(each.getKey())
.ifPresent(optional -> encryptors.put(optional, algorithmSwapper.swapToObject(YamlEngine.unmarshal(each.getValue(), YamlAlgorithmConfiguration.class))));
.ifPresent(optional -> encryptors.put(optional, YamlEngine.unmarshal(each.getValue(), YamlAlgorithmConfiguration.class)));
}
return Optional.of(new EncryptRuleConfiguration(tables, encryptors));
YamlEncryptRuleConfiguration yamlRuleConfig = new YamlEncryptRuleConfiguration();
yamlRuleConfig.setTables(tables);
yamlRuleConfig.setEncryptors(encryptors);
return Optional.of(ruleConfigSwapper.swapToObject(yamlRuleConfig));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@

package org.apache.shardingsphere.mask.yaml.swapper;

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.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.constant.MaskOrder;
import org.apache.shardingsphere.mask.metadata.nodepath.MaskRuleNodePathProvider;
import org.apache.shardingsphere.mask.yaml.config.YamlMaskRuleConfiguration;
import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper;
import org.apache.shardingsphere.mode.path.RuleNodePath;
import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;

import java.util.Collection;
import java.util.LinkedHashMap;
Expand All @@ -45,21 +42,19 @@
*/
public final class MaskRuleConfigurationRepositoryTupleSwapper implements RepositoryTupleSwapper<MaskRuleConfiguration> {

private final YamlMaskTableRuleConfigurationSwapper tableSwapper = new YamlMaskTableRuleConfigurationSwapper();

private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
private final YamlMaskRuleConfigurationSwapper ruleConfigSwapper = new YamlMaskRuleConfigurationSwapper();

private final RuleNodePath maskRuleNodePath = new MaskRuleNodePathProvider().getRuleNodePath();

@Override
public Collection<RepositoryTuple> swapToRepositoryTuples(final MaskRuleConfiguration data) {
Collection<RepositoryTuple> result = new LinkedList<>();
for (Entry<String, AlgorithmConfiguration> entry : data.getMaskAlgorithms().entrySet()) {
result.add(new RepositoryTuple(
maskRuleNodePath.getNamedItem(MaskRuleNodePathProvider.MASK_ALGORITHMS).getPath(entry.getKey()), YamlEngine.marshal(algorithmSwapper.swapToYamlConfiguration(entry.getValue()))));
YamlMaskRuleConfiguration yamlRuleConfig = ruleConfigSwapper.swapToYamlConfiguration(data);
for (Entry<String, YamlAlgorithmConfiguration> entry : yamlRuleConfig.getMaskAlgorithms().entrySet()) {
result.add(new RepositoryTuple(maskRuleNodePath.getNamedItem(MaskRuleNodePathProvider.MASK_ALGORITHMS).getPath(entry.getKey()), YamlEngine.marshal(entry.getValue())));
}
for (MaskTableRuleConfiguration each : data.getTables()) {
result.add(new RepositoryTuple(maskRuleNodePath.getNamedItem(MaskRuleNodePathProvider.TABLES).getPath(each.getName()), YamlEngine.marshal(tableSwapper.swapToYamlConfiguration(each))));
for (YamlMaskTableRuleConfiguration each : yamlRuleConfig.getTables().values()) {
result.add(new RepositoryTuple(maskRuleNodePath.getNamedItem(MaskRuleNodePathProvider.TABLES).getPath(each.getName()), YamlEngine.marshal(each)));
}
return result;
}
Expand All @@ -70,15 +65,18 @@ public Optional<MaskRuleConfiguration> swapToObject(final Collection<RepositoryT
if (validTuples.isEmpty()) {
return Optional.empty();
}
Collection<MaskTableRuleConfiguration> tables = new LinkedList<>();
Map<String, AlgorithmConfiguration> algorithms = new LinkedHashMap<>();
Map<String, YamlMaskTableRuleConfiguration> tables = new LinkedHashMap<>();
Map<String, YamlAlgorithmConfiguration> maskAlgorithms = new LinkedHashMap<>();
for (RepositoryTuple each : validTuples) {
maskRuleNodePath.getNamedItem(MaskRuleNodePathProvider.TABLES).getName(each.getKey())
.ifPresent(optional -> tables.add(tableSwapper.swapToObject(YamlEngine.unmarshal(each.getValue(), YamlMaskTableRuleConfiguration.class))));
.ifPresent(optional -> tables.put(optional, YamlEngine.unmarshal(each.getValue(), YamlMaskTableRuleConfiguration.class)));
maskRuleNodePath.getNamedItem(MaskRuleNodePathProvider.MASK_ALGORITHMS).getName(each.getKey())
.ifPresent(optional -> algorithms.put(optional, algorithmSwapper.swapToObject(YamlEngine.unmarshal(each.getValue(), YamlAlgorithmConfiguration.class))));
.ifPresent(optional -> maskAlgorithms.put(optional, YamlEngine.unmarshal(each.getValue(), YamlAlgorithmConfiguration.class)));
}
return Optional.of(new MaskRuleConfiguration(tables, algorithms));
YamlMaskRuleConfiguration yamlRuleConfig = new YamlMaskRuleConfiguration();
yamlRuleConfig.setTables(tables);
yamlRuleConfig.setMaskAlgorithms(maskAlgorithms);
return Optional.of(ruleConfigSwapper.swapToObject(yamlRuleConfig));
}

@Override
Expand Down