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

panjuan 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 348d9a0  refactor ShardingUnicastRoutingEngine (#10203)
348d9a0 is described below

commit 348d9a0b6f0063df6111b32b2ce363000c3bce20
Author: MingxingLAI <80867129+mingxing...@users.noreply.github.com>
AuthorDate: Tue Apr 27 16:47:38 2021 +0800

    refactor ShardingUnicastRoutingEngine (#10203)
    
    * Using stream replace for loop in ShardingUnicastRoutingEngine
    
    * Extracting multiple tables route in ShardingUnicastRoutingEngine to an 
independent function
    
    * Using stream to replace forloop for reducing the complexity of the 
function
    
    * Eliminating availableDatasourceNames may throw a NullPointerException 
warning
---
 .../type/unicast/ShardingUnicastRoutingEngine.java | 52 +++++++++++-----------
 1 file changed, 26 insertions(+), 26 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
index 882a10c..632d5ec 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
@@ -19,6 +19,7 @@ package 
org.apache.shardingsphere.sharding.route.engine.type.unicast;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
+import java.util.stream.Collectors;
 import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
 import org.apache.shardingsphere.infra.datanode.DataNode;
@@ -50,10 +51,7 @@ public final class ShardingUnicastRoutingEngine implements 
ShardingRouteEngine {
         String dataSourceName = 
getRandomDataSourceName(shardingRule.getDataSourceNames());
         RouteMapper dataSourceMapper = new RouteMapper(dataSourceName, 
dataSourceName);
         if (shardingRule.isAllBroadcastTables(logicTables)) {
-            List<RouteMapper> tableMappers = new 
ArrayList<>(logicTables.size());
-            for (String each : logicTables) {
-                tableMappers.add(new RouteMapper(each, each));
-            }
+            List<RouteMapper> tableMappers = logicTables.stream().map(each -> 
new RouteMapper(each, each)).collect(Collectors.toCollection(() -> new 
ArrayList<>(logicTables.size())));
             routeContext.getRouteUnits().add(new RouteUnit(dataSourceMapper, 
tableMappers));
         } else if (logicTables.isEmpty()) {
             routeContext.getRouteUnits().add(new RouteUnit(dataSourceMapper, 
Collections.emptyList()));
@@ -67,30 +65,32 @@ public final class ShardingUnicastRoutingEngine implements 
ShardingRouteEngine {
             routeContext.getRouteUnits().add(new RouteUnit(new 
RouteMapper(dataNode.getDataSourceName(), dataNode.getDataSourceName()),
                     Collections.singletonList(new RouteMapper(logicTableName, 
dataNode.getTableName()))));
         } else {
-            List<RouteMapper> tableMappers = new 
ArrayList<>(logicTables.size());
-            Set<String> availableDatasourceNames = null;
-            boolean first = true;
-            for (String each : logicTables) {
-                TableRule tableRule = shardingRule.getTableRule(each);
-                DataNode dataNode = tableRule.getActualDataNodes().get(0);
-                tableMappers.add(new RouteMapper(each, 
dataNode.getTableName()));
-                Set<String> currentDataSourceNames = new 
HashSet<>(tableRule.getActualDatasourceNames().size());
-                for (DataNode eachDataNode : tableRule.getActualDataNodes()) {
-                    
currentDataSourceNames.add(eachDataNode.getDataSourceName());
-                }
-                if (first) {
-                    availableDatasourceNames = currentDataSourceNames;
-                    first = false;
-                } else {
-                    availableDatasourceNames = 
Sets.intersection(availableDatasourceNames, currentDataSourceNames);
-                }
-            }
-            if (availableDatasourceNames.isEmpty()) {
-                throw new ShardingSphereConfigurationException("Cannot find 
actual datasource intersection for logic tables: %s", logicTables);
+            routeWithMultipleTables(routeContext, shardingRule);
+        }
+    }
+
+    private void routeWithMultipleTables(final RouteContext routeContext, 
final ShardingRule shardingRule) throws ShardingSphereConfigurationException {
+        List<RouteMapper> tableMappers = new ArrayList<>(logicTables.size());
+        Set<String> availableDatasourceNames = Collections.emptySet();
+        boolean first = true;
+        for (String each : logicTables) {
+            TableRule tableRule = shardingRule.getTableRule(each);
+            DataNode dataNode = tableRule.getActualDataNodes().get(0);
+            tableMappers.add(new RouteMapper(each, dataNode.getTableName()));
+            Set<String> currentDataSourceNames = 
tableRule.getActualDataNodes().stream().map(DataNode::getDataSourceName).collect(
+                    Collectors.toCollection(() -> new 
HashSet<>(tableRule.getActualDatasourceNames().size())));
+            if (first) {
+                availableDatasourceNames = currentDataSourceNames;
+                first = false;
+            } else {
+                availableDatasourceNames = 
Sets.intersection(availableDatasourceNames, currentDataSourceNames);
             }
-            dataSourceName = getRandomDataSourceName(availableDatasourceNames);
-            routeContext.getRouteUnits().add(new RouteUnit(new 
RouteMapper(dataSourceName, dataSourceName), tableMappers));
         }
+        if (availableDatasourceNames.isEmpty()) {
+            throw new ShardingSphereConfigurationException("Cannot find actual 
datasource intersection for logic tables: %s", logicTables);
+        }
+        String dataSourceName = 
getRandomDataSourceName(availableDatasourceNames);
+        routeContext.getRouteUnits().add(new RouteUnit(new 
RouteMapper(dataSourceName, dataSourceName), tableMappers));
     }
     
     private String getRandomDataSourceName(final Collection<String> 
dataSourceNames) {

Reply via email to