This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 bef29882dec Modify the getDataNodesByTableName method without using 
stream (#31585)
bef29882dec is described below

commit bef29882dec9395584f1aee6d8034df8e4e01565
Author: ZhangCheng <[email protected]>
AuthorDate: Tue Jun 4 21:06:58 2024 +0800

    Modify the getDataNodesByTableName method without using stream (#31585)
---
 .../apache/shardingsphere/infra/datanode/DataNodes.java | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodes.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodes.java
index 0622fca1ed7..75d5983a4db 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodes.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodes.java
@@ -22,9 +22,9 @@ import 
org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import 
org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
 import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -52,15 +52,24 @@ public final class DataNodes {
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
     public Collection<DataNode> getDataNodes(final String tableName) {
-        Collection<DataNode> result = new LinkedList<>(
-                rules.stream().map(each -> getDataNodes(each, 
tableName)).filter(dataNodes -> 
!dataNodes.isEmpty()).findFirst().orElse(Collections.emptyList()));
+        Collection<DataNode> result = getDataNodesByTableName(tableName);
         for (Entry<ShardingSphereRule, DataNodeBuilder> entry : 
dataNodeBuilders.entrySet()) {
             result = entry.getValue().build(result, entry.getKey());
         }
         return result;
     }
     
-    private Collection<DataNode> getDataNodes(final ShardingSphereRule rule, 
final String tableName) {
+    private Collection<DataNode> getDataNodesByTableName(final String 
tableName) {
+        for (ShardingSphereRule each : rules) {
+            Collection<DataNode> dataNodes = getDataNodesByTableName(each, 
tableName);
+            if (!dataNodes.isEmpty()) {
+                return new ArrayList<>(dataNodes);
+            }
+        }
+        return Collections.emptyList();
+    }
+    
+    private Collection<DataNode> getDataNodesByTableName(final 
ShardingSphereRule rule, final String tableName) {
         return 
rule.getAttributes().findAttribute(DataNodeRuleAttribute.class).map(optional -> 
optional.getDataNodesByTableName(tableName)).orElse(Collections.emptyList());
     }
 }

Reply via email to