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/01 11:19:49 UTC

[shardingsphere] branch master updated: Add new mask rule configuration swapper (#25989)

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 57eca77b892 Add new mask rule configuration swapper (#25989)
57eca77b892 is described below

commit 57eca77b892263f4455f9d2301b20e62f469a0fd
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Thu Jun 1 19:19:40 2023 +0800

    Add new mask rule configuration swapper (#25989)
---
 features/mask/core/pom.xml                         |  5 ++
 .../NewYamlMaskRuleConfigurationSwapper.java       | 79 ++++++++++++++++++++++
 .../NewYamlMaskRuleConfigurationSwapperTest.java   | 62 +++++++++++++++++
 .../metadata/config/mask/MaskNodeConverter.java    | 53 +++++++++++++++
 4 files changed, 199 insertions(+)

diff --git a/features/mask/core/pom.xml b/features/mask/core/pom.xml
index f863a2f057f..c96cb8a2f63 100644
--- a/features/mask/core/pom.xml
+++ b/features/mask/core/pom.xml
@@ -38,6 +38,11 @@
             <artifactId>shardingsphere-infra-merge</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-metadata-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
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
new file mode 100644
index 00000000000..705e0284181
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapper.java
@@ -0,0 +1,79 @@
+/*
+ * 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.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.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.metadata.persist.node.metadata.config.mask.MaskNodeConverter;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Map.Entry;
+
+/**
+ * TODO Rename to YamlMaskRuleConfigurationSwapper when metadata structure adjustment completed.
+ * New YAML mask rule configuration swapper.
+ */
+public final class NewYamlMaskRuleConfigurationSwapper implements NewYamlRuleConfigurationSwapper<MaskRuleConfiguration> {
+    
+    // TODO to be used
+    private final YamlMaskTableRuleConfigurationSwapper tableSwapper = new YamlMaskTableRuleConfigurationSwapper();
+    
+    private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
+    
+    @Override
+    public Collection<YamlDataNode> swapToDataNodes(final MaskRuleConfiguration data) {
+        Collection<YamlDataNode> result = new LinkedHashSet<>();
+        for (MaskTableRuleConfiguration each : data.getTables()) {
+            result.add(new YamlDataNode(MaskNodeConverter.getTableNamePath(each.getName()), YamlEngine.marshal(each)));
+        }
+        for (Entry<String, AlgorithmConfiguration> entry : data.getMaskAlgorithms().entrySet()) {
+            result.add(new YamlDataNode(MaskNodeConverter.getMaskAlgorithmNamePath(entry.getKey()), YamlEngine.marshal(entry.getValue())));
+        }
+        return result;
+    }
+    
+    @Override
+    public MaskRuleConfiguration swapToObject(final Collection<YamlDataNode> dataNodes) {
+        // TODO to be completed.
+        return new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap());
+    }
+        
+    @Override
+    public Class<MaskRuleConfiguration> getTypeClass() {
+        return MaskRuleConfiguration.class;
+    }
+    
+    @Override
+    public String getRuleTagName() {
+        return "MASK";
+    }
+    
+    @Override
+    public int getOrder() {
+        return MaskOrder.ORDER;
+    }
+}
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
new file mode 100644
index 00000000000..90b0b9c8f2e
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/NewYamlMaskRuleConfigurationSwapperTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.yaml.swapper;
+
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class NewYamlMaskRuleConfigurationSwapperTest {
+    
+    private final NewYamlMaskRuleConfigurationSwapper swapper = new NewYamlMaskRuleConfigurationSwapper();
+    
+    @Test
+    void assertSwapEmptyConfigToDataNodes() {
+        MaskRuleConfiguration config = new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap());
+        Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+        assertThat(result.size(), is(0));
+    }
+    
+    @Test
+    void assertSwapFullConfigToDataNodes() {
+        MaskRuleConfiguration config = createMaximumMaskRule();
+        Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+        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"));
+    }
+    
+    private MaskRuleConfiguration createMaximumMaskRule() {
+        Collection<MaskTableRuleConfiguration> tables = new LinkedList<>();
+        tables.add(new MaskTableRuleConfiguration("foo", Collections.singleton(new MaskColumnRuleConfiguration("foo_column", "FIXTURE"))));
+        return new MaskRuleConfiguration(tables, Collections.singletonMap("FIXTURE", new AlgorithmConfiguration("FIXTURE", new Properties())));
+    }
+}
diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/mask/MaskNodeConverter.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/mask/MaskNodeConverter.java
new file mode 100644
index 00000000000..360d19f0d12
--- /dev/null
+++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/mask/MaskNodeConverter.java
@@ -0,0 +1,53 @@
+/*
+ * 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.metadata.persist.node.metadata.config.mask;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * TODO move to features module
+ * Mask node converter.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class MaskNodeConverter {
+    
+    private static final String TABLES = "tables";
+    
+    private static final String MASK_ALGORITHMS = "mask_algorithms";
+  
+    /**
+     * Get table name path.
+     * 
+     * @param tableName table name
+     * @return table name path
+     */
+    public static String getTableNamePath(final String tableName) {
+        return String.join("/", TABLES, tableName);
+    }
+    
+    /**
+     * Get mask algorithm path.
+     * 
+     * @param maskAlgorithmName mask algorithm name
+     * @return mask algorithm path
+     */
+    public static String getMaskAlgorithmNamePath(final String maskAlgorithmName) {
+        return String.join("/", MASK_ALGORITHMS, maskAlgorithmName);
+    }
+}