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 2024/05/01 04:54:12 UTC

(shardingsphere) branch master updated: Refactor BroadcastRuleConfigurationRepositoryTupleSwapper (#31088)

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 4d34e232ea2 Refactor BroadcastRuleConfigurationRepositoryTupleSwapper (#31088)
4d34e232ea2 is described below

commit 4d34e232ea2dc7fc8ce51454bc3906a68ee91fd8
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed May 1 12:54:06 2024 +0800

    Refactor BroadcastRuleConfigurationRepositoryTupleSwapper (#31088)
---
 ...astRuleConfigurationRepositoryTupleSwapper.java | 25 ++++++++++++++--------
 ...uleConfigurationRepositoryTupleSwapperTest.java |  2 +-
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapper.java b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapper.java
index 06284a87599..965bf87795d 100644
--- a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapper.java
+++ b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapper.java
@@ -26,8 +26,8 @@ import org.apache.shardingsphere.mode.path.rule.RuleNodePath;
 import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;
 
 import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
 import java.util.Optional;
 import java.util.stream.Collectors;
 
@@ -40,20 +40,27 @@ public final class BroadcastRuleConfigurationRepositoryTupleSwapper implements R
     
     @Override
     public Collection<RepositoryTuple> swapToRepositoryTuples(final YamlBroadcastRuleConfiguration yamlRuleConfig) {
-        return yamlRuleConfig.getTables().isEmpty()
-                ? Collections.emptyList()
-                : Collections.singleton(new RepositoryTuple(BroadcastRuleNodePathProvider.TABLES, YamlEngine.marshal(yamlRuleConfig)));
+        Collection<RepositoryTuple> result = new LinkedList<>();
+        if (!yamlRuleConfig.getTables().isEmpty()) {
+            result.add(new RepositoryTuple(BroadcastRuleNodePathProvider.TABLES, YamlEngine.marshal(yamlRuleConfig.getTables())));
+        }
+        return result;
     }
     
+    @SuppressWarnings("unchecked")
     @Override
     public Optional<YamlBroadcastRuleConfiguration> swapToObject(final Collection<RepositoryTuple> repositoryTuples) {
-        List<RepositoryTuple> validTuples = repositoryTuples.stream().filter(each -> ruleNodePath.getRoot().isValidatedPath(each.getKey())).collect(Collectors.toList());
+        Collection<RepositoryTuple> validTuples = repositoryTuples.stream().filter(each -> ruleNodePath.getRoot().isValidatedPath(each.getKey())).collect(Collectors.toList());
+        if (validTuples.isEmpty()) {
+            return Optional.empty();
+        }
+        YamlBroadcastRuleConfiguration result = new YamlBroadcastRuleConfiguration();
         for (RepositoryTuple each : validTuples) {
-            if (ruleNodePath.getRoot().isValidatedPath(each.getKey())) {
-                return Optional.of(YamlEngine.unmarshal(each.getValue(), YamlBroadcastRuleConfiguration.class));
+            if (ruleNodePath.getUniqueItem(BroadcastRuleNodePathProvider.TABLES).isValidatedPath(each.getKey())) {
+                result.getTables().addAll(YamlEngine.unmarshal(each.getValue(), LinkedHashSet.class));
             }
         }
-        return Optional.empty();
+        return Optional.of(result);
     }
     
     @Override
diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapperTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapperTest.java
index c39063b6a26..4252ef09a37 100644
--- a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapperTest.java
+++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/yaml/swapper/BroadcastRuleConfigurationRepositoryTupleSwapperTest.java
@@ -53,7 +53,7 @@ class BroadcastRuleConfigurationRepositoryTupleSwapperTest {
     
     @Test
     void assertSwapToObject() {
-        RepositoryTuple repositoryTuple = new RepositoryTuple("/metadata/foo_db/rules/broadcast/tables", "tables:\n- foo_table\n- foo_table2\n");
+        RepositoryTuple repositoryTuple = new RepositoryTuple("/metadata/foo_db/rules/broadcast/tables/versions/0", "\n- foo_table\n- foo_table2\n");
         Optional<YamlBroadcastRuleConfiguration> actual = swapper.swapToObject(Collections.singleton(repositoryTuple));
         assertTrue(actual.isPresent());
         assertThat(actual.get().getTables().size(), is(2));