You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/06/07 10:33:11 UTC
[shardingsphere] branch master updated: Improve swap yaml data to rule configuration in mask (#26099)
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 22fc17befca Improve swap yaml data to rule configuration in mask (#26099)
22fc17befca is described below
commit 22fc17befca29812c056be69a70b59cd1cf18e0c
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Wed Jun 7 18:33:04 2023 +0800
Improve swap yaml data to rule configuration in mask (#26099)
---
.../mask/metadata/converter/MaskNodeConverter.java | 76 ++++++++++++++++++++--
.../NewYamlMaskRuleConfigurationSwapper.java | 22 +++++--
.../metadata/converter/MaskNodeConverterTest.java | 64 ++++++++++++++++++
.../NewYamlMaskRuleConfigurationSwapperTest.java | 30 ++++++++-
...ReadwriteSplittingRuleConfigurationSwapper.java | 6 +-
5 files changed, 186 insertions(+), 12 deletions(-)
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/metadata/converter/MaskNodeConverter.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/metadata/converter/MaskNodeConverter.java
index aee2eaac42f..3bcfde93901 100644
--- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/metadata/converter/MaskNodeConverter.java
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/metadata/converter/MaskNodeConverter.java
@@ -20,15 +20,23 @@ package org.apache.shardingsphere.mask.metadata.converter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/**
* Mask node converter.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MaskNodeConverter {
- private static final String TABLES = "tables";
+ private static final String ROOT_NODE = "mask";
+
+ private static final String TABLES_NODE = "tables";
+
+ private static final String ALGORITHMS_NODE = "algorithms";
- private static final String MASK_ALGORITHMS = "mask_algorithms";
+ private static final String RULES_NODE_PREFIX = "/([\\w\\-]+)/([\\w\\-]+)/rules/";
/**
* Get table name path.
@@ -37,7 +45,7 @@ public final class MaskNodeConverter {
* @return table name path
*/
public static String getTableNamePath(final String tableName) {
- return String.join("/", TABLES, tableName);
+ return String.join("/", TABLES_NODE, tableName);
}
/**
@@ -47,6 +55,66 @@ public final class MaskNodeConverter {
* @return mask algorithm path
*/
public static String getMaskAlgorithmNamePath(final String maskAlgorithmName) {
- return String.join("/", MASK_ALGORITHMS, maskAlgorithmName);
+ return String.join("/", ALGORITHMS_NODE, maskAlgorithmName);
+ }
+
+ /**
+ * Is mask path.
+ *
+ * @param rulePath rule path
+ * @return true or false
+ */
+ public static boolean isMaskPath(final String rulePath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "\\.*", Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(rulePath);
+ return matcher.find();
+ }
+
+ /**
+ * Is mask table path.
+ *
+ * @param rulePath rule path
+ * @return true or false
+ */
+ public static boolean isTablePath(final String rulePath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + TABLES_NODE + "\\.*", Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(rulePath);
+ return matcher.find();
+ }
+
+ /**
+ * Is mask algorithm path.
+ *
+ * @param rulePath rule path
+ * @return true or false
+ */
+ public static boolean isAlgorithmPath(final String rulePath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + ALGORITHMS_NODE + "\\.*", Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(rulePath);
+ return matcher.find();
+ }
+
+ /**
+ * Get table name.
+ *
+ * @param rulePath rule path
+ * @return table name
+ */
+ public static Optional<String> getTableName(final String rulePath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + TABLES_NODE + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(rulePath);
+ return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
+ }
+
+ /**
+ * Get algorithm name.
+ *
+ * @param rulePath rule path
+ * @return algorithm name
+ */
+ public static Optional<String> getAlgorithmName(final String rulePath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + ALGORITHMS_NODE + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(rulePath);
+ return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapper.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapper.java
index 272048b5310..d39288be014 100644
--- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapper.java
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapper.java
@@ -20,17 +20,21 @@ package org.apache.shardingsphere.mask.yaml.swapper;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapper;
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.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper;
import org.apache.shardingsphere.mask.metadata.converter.MaskNodeConverter;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper;
import java.util.Collection;
-import java.util.Collections;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.Map;
import java.util.Map.Entry;
/**
@@ -57,8 +61,18 @@ public final class NewYamlMaskRuleConfigurationSwapper implements NewYamlRuleCon
@Override
public MaskRuleConfiguration swapToObject(final Collection<YamlDataNode> dataNodes) {
- // TODO to be completed.
- return new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap());
+ Collection<MaskTableRuleConfiguration> tables = new LinkedList<>();
+ Map<String, AlgorithmConfiguration> algorithms = new LinkedHashMap<>();
+ for (YamlDataNode each : dataNodes) {
+ if (MaskNodeConverter.isTablePath(each.getKey())) {
+ MaskNodeConverter.getTableName(each.getKey())
+ .ifPresent(tableName -> tables.add(tableSwapper.swapToObject(YamlEngine.unmarshal(each.getValue(), YamlMaskTableRuleConfiguration.class))));
+ } else if (MaskNodeConverter.isAlgorithmPath(each.getKey())) {
+ MaskNodeConverter.getAlgorithmName(each.getKey())
+ .ifPresent(loadBalancerName -> algorithms.put(loadBalancerName, algorithmSwapper.swapToObject(YamlEngine.unmarshal(each.getValue(), YamlAlgorithmConfiguration.class))));
+ }
+ }
+ return new MaskRuleConfiguration(tables, algorithms);
}
@Override
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/metadata/converter/MaskNodeConverterTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/metadata/converter/MaskNodeConverterTest.java
new file mode 100644
index 00000000000..eb6899dc6be
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/metadata/converter/MaskNodeConverterTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.mask.metadata.converter;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MaskNodeConverterTest {
+
+ @Test
+ void assertGetTableNamePath() {
+ assertThat(MaskNodeConverter.getTableNamePath("foo_table"), is("tables/foo_table"));
+ }
+
+ @Test
+ void assertGetAlgorithmPath() {
+ assertThat(MaskNodeConverter.getMaskAlgorithmNamePath("MD5"), is("algorithms/MD5"));
+ }
+
+ @Test
+ void assertCheckIsTargetRuleByRulePath() {
+ assertTrue(MaskNodeConverter.isMaskPath("/metadata/foo_db/rules/mask/tables/foo_table"));
+ assertFalse(MaskNodeConverter.isMaskPath("/metadata/foo_db/rules/foo/tables/foo_table"));
+ assertTrue(MaskNodeConverter.isTablePath("/metadata/foo_db/rules/mask/tables/foo_table"));
+ assertFalse(MaskNodeConverter.isTablePath("/metadata/foo_db/rules/mask/algorithms/MD5"));
+ assertTrue(MaskNodeConverter.isAlgorithmPath("/metadata/foo_db/rules/mask/algorithms/MD5"));
+ assertFalse(MaskNodeConverter.isAlgorithmPath("/metadata/foo_db/rules/mask/tables/foo_table"));
+ }
+
+ @Test
+ void assertGetTableNameByRulePath() {
+ Optional<String> actual = MaskNodeConverter.getTableName("/metadata/foo_db/rules/mask/tables/foo_table");
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is("foo_table"));
+ }
+
+ @Test
+ void assertGetAlgorithmNameByRulePath() {
+ Optional<String> actual = MaskNodeConverter.getAlgorithmName("/metadata/foo_db/rules/mask/algorithms/MD5");
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is("MD5"));
+ }
+}
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java
index 90b0b9c8f2e..5e8bc20f89f 100644
--- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java
@@ -51,7 +51,7 @@ class NewYamlMaskRuleConfigurationSwapperTest {
assertThat(result.size(), is(2));
Iterator<YamlDataNode> iterator = result.iterator();
assertThat(iterator.next().getKey(), is("tables/foo"));
- assertThat(iterator.next().getKey(), is("mask_algorithms/FIXTURE"));
+ assertThat(iterator.next().getKey(), is("algorithms/FIXTURE"));
}
private MaskRuleConfiguration createMaximumMaskRule() {
@@ -59,4 +59,32 @@ class NewYamlMaskRuleConfigurationSwapperTest {
tables.add(new MaskTableRuleConfiguration("foo", Collections.singleton(new MaskColumnRuleConfiguration("foo_column", "FIXTURE"))));
return new MaskRuleConfiguration(tables, Collections.singletonMap("FIXTURE", new AlgorithmConfiguration("FIXTURE", new Properties())));
}
+
+ @Test
+ void assertSwapToObjectEmpty() {
+ Collection<YamlDataNode> config = new LinkedList<>();
+ MaskRuleConfiguration result = swapper.swapToObject(config);
+ assertThat(result.getTables().size(), is(0));
+ assertThat(result.getMaskAlgorithms().size(), is(0));
+ }
+
+ @Test
+ void assertSwapToObject() {
+ Collection<YamlDataNode> config = new LinkedList<>();
+ config.add(new YamlDataNode("/metadata/foo_db/rules/mask/tables/foo", "columns:\n"
+ + " foo_column:\n"
+ + " logicColumn: foo_column\n"
+ + " maskAlgorithm: FIXTURE\n"
+ + "name: foo\n"));
+ config.add(new YamlDataNode("/metadata/foo_db/rules/mask/algorithms/FIXTURE", "type: FIXTURE\n"));
+ MaskRuleConfiguration result = swapper.swapToObject(config);
+ assertThat(result.getTables().size(), is(1));
+ assertThat(result.getTables().iterator().next().getName(), is("foo"));
+ assertThat(result.getTables().iterator().next().getColumns().size(), is(1));
+ assertThat(result.getTables().iterator().next().getColumns().iterator().next().getLogicColumn(), is("foo_column"));
+ assertThat(result.getTables().iterator().next().getColumns().iterator().next().getMaskAlgorithm(), is("FIXTURE"));
+ assertThat(result.getMaskAlgorithms().size(), is(1));
+ assertThat(result.getMaskAlgorithms().get("FIXTURE").getType(), is("FIXTURE"));
+ assertThat(result.getMaskAlgorithms().get("FIXTURE").getProps().size(), is(0));
+ }
}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/NewYamlReadwriteSplittingRuleConfigurationSwapper.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/NewYamlReadwriteSplittingRuleConfigurationSwapper.java
index 4e3d43004dc..7947ac50e8f 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/NewYamlReadwriteSplittingRuleConfigurationSwapper.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/yaml/swapper/NewYamlReadwriteSplittingRuleConfigurationSwapper.java
@@ -70,11 +70,11 @@ public final class NewYamlReadwriteSplittingRuleConfigurationSwapper implements
@Override
public ReadwriteSplittingRuleConfiguration swapToObject(final Collection<YamlDataNode> dataNodes) {
Collection<ReadwriteSplittingDataSourceRuleConfiguration> dataSources = new LinkedList<>();
- Map<String, AlgorithmConfiguration> loadBalancerMap = new LinkedHashMap<>(dataNodes.size(), 1F);
+ Map<String, AlgorithmConfiguration> loadBalancerMap = new LinkedHashMap<>();
for (YamlDataNode each : dataNodes) {
if (ReadwriteSplittingNodeConverter.isDataSourcePath(each.getKey())) {
ReadwriteSplittingNodeConverter.getGroupName(each.getKey())
- .ifPresent(groupName -> dataSources.add(swapToObject(groupName, YamlEngine.unmarshal(each.getValue(), YamlReadwriteSplittingDataSourceRuleConfiguration.class))));
+ .ifPresent(groupName -> dataSources.add(swapDataSource(groupName, YamlEngine.unmarshal(each.getValue(), YamlReadwriteSplittingDataSourceRuleConfiguration.class))));
} else if (ReadwriteSplittingNodeConverter.isLoadBalancerPath(each.getKey())) {
ReadwriteSplittingNodeConverter.getLoadBalancerName(each.getKey())
.ifPresent(
@@ -84,7 +84,7 @@ public final class NewYamlReadwriteSplittingRuleConfigurationSwapper implements
return new ReadwriteSplittingRuleConfiguration(dataSources, loadBalancerMap);
}
- private ReadwriteSplittingDataSourceRuleConfiguration swapToObject(final String name, final YamlReadwriteSplittingDataSourceRuleConfiguration yamlDataSourceRuleConfig) {
+ private ReadwriteSplittingDataSourceRuleConfiguration swapDataSource(final String name, final YamlReadwriteSplittingDataSourceRuleConfiguration yamlDataSourceRuleConfig) {
return new ReadwriteSplittingDataSourceRuleConfiguration(name, yamlDataSourceRuleConfig.getWriteDataSourceName(), yamlDataSourceRuleConfig.getReadDataSourceNames(),
getTransactionalReadQueryStrategy(yamlDataSourceRuleConfig), yamlDataSourceRuleConfig.getLoadBalancerName());
}