You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2023/05/17 09:16:46 UTC

[shardingsphere] branch master updated: Simplify Map operations by compute methods (#25735)

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 4603d7d526a Simplify Map operations by compute methods (#25735)
4603d7d526a is described below

commit 4603d7d526a63b41d3a8c83cfc161b00fc426e84
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Wed May 17 17:16:38 2023 +0800

    Simplify Map operations by compute methods (#25735)
    
    * Simplify Map operations in DataRecordMerger
    
    https://sonarcloud.io/project/issues?resolved=false&severities=MAJOR&types=CODE_SMELL&id=apache_shardingsphere&open=AYfA19FutGAAaUtvu0eb
    
    * Simplify lines in DataNodeUtils with Map.computeIfAbsent
    
    https://sonarcloud.io/project/issues?resolved=false&severities=MAJOR&types=CODE_SMELL&id=apache_shardingsphere&open=AYePEBBzmlEb3_Pqvlfc
    
    * Refactor Map operations in GroupByMemoryMergedResult
    
    https://sonarcloud.io/project/issues?resolved=false&severities=MAJOR&types=CODE_SMELL&id=apache_shardingsphere&open=AYePD-cXmlEb3_PqvlPr
    
    * Simplify Map operations in ShadowTableRule
    
    https://sonarcloud.io/project/issues?resolved=false&severities=MAJOR&types=CODE_SMELL&id=apache_shardingsphere&open=AYePD_KNmlEb3_PqvlUl
---
 .../apache/shardingsphere/shadow/rule/ShadowTableRule.java    |  5 +----
 .../sharding/merge/dql/groupby/GroupByMemoryMergedResult.java | 11 ++++-------
 .../apache/shardingsphere/infra/datanode/DataNodeUtils.java   |  6 +-----
 .../data/pipeline/core/importer/DataRecordMerger.java         |  6 +-----
 4 files changed, 7 insertions(+), 21 deletions(-)

diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java
index c84f78996a3..0daa95a722b 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowTableRule.java
@@ -61,10 +61,7 @@ public final class ShadowTableRule {
             ShadowAlgorithm shadowAlgorithm = shadowAlgorithms.get(each);
             if (shadowAlgorithm instanceof ColumnShadowAlgorithm) {
                 ShadowOperationType operationType = ((ColumnShadowAlgorithm<?>) shadowAlgorithm).getShadowOperationType();
-                if (!result.containsKey(operationType)) {
-                    result.put(operationType, new LinkedList<>());
-                }
-                result.get(operationType).add(new ShadowAlgorithmNameRule(((ColumnShadowAlgorithm<?>) shadowAlgorithm).getShadowColumn(), each));
+                result.computeIfAbsent(operationType, unused -> new LinkedList<>()).add(new ShadowAlgorithmNameRule(((ColumnShadowAlgorithm<?>) shadowAlgorithm).getShadowColumn(), each));
             }
         }
         return result;
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
index df30f18a307..08e17f3051a 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.sharding.merge.dql.groupby;
 
-import com.google.common.collect.Maps;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationDistinctProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
@@ -46,6 +45,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Objects;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 /**
  * Memory merged result for group by.
@@ -80,12 +81,8 @@ public final class GroupByMemoryMergedResult extends MemoryMergedResult<Sharding
         if (!dataMap.containsKey(groupByValue)) {
             dataMap.put(groupByValue, new MemoryQueryResultRow(queryResult));
         }
-        if (!aggregationMap.containsKey(groupByValue)) {
-            Map<AggregationProjection, AggregationUnit> map = Maps
-                    .toMap(selectStatementContext.getProjectionsContext()
-                            .getAggregationProjections(), input -> AggregationUnitFactory.create(input.getType(), input instanceof AggregationDistinctProjection));
-            aggregationMap.put(groupByValue, map);
-        }
+        aggregationMap.computeIfAbsent(groupByValue, unused -> selectStatementContext.getProjectionsContext().getAggregationProjections().stream()
+                .collect(Collectors.toMap(Function.identity(), input -> AggregationUnitFactory.create(input.getType(), input instanceof AggregationDistinctProjection))));
     }
     
     private void aggregate(final SelectStatementContext selectStatementContext, final QueryResult queryResult,
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java
index 0de98de2bf9..33238b9bf62 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java
@@ -43,11 +43,7 @@ public final class DataNodeUtils {
     public static Map<String, List<DataNode>> getDataNodeGroups(final Collection<DataNode> dataNodes) {
         Map<String, List<DataNode>> result = new LinkedHashMap<>(dataNodes.size(), 1);
         for (DataNode each : dataNodes) {
-            String dataSourceName = each.getDataSourceName();
-            if (!result.containsKey(dataSourceName)) {
-                result.put(dataSourceName, new LinkedList<>());
-            }
-            result.get(dataSourceName).add(each);
+            result.computeIfAbsent(each.getDataSourceName(), unused -> new LinkedList<>()).add(each);
         }
         return result;
     }
diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/importer/DataRecordMerger.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/importer/DataRecordMerger.java
index 42dd3f29152..1dd7a577672 100644
--- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/importer/DataRecordMerger.java
+++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/importer/DataRecordMerger.java
@@ -55,11 +55,7 @@ public final class DataRecordMerger {
             }
             tableNames.add(each.getTableName());
             Key key = getKeyFromDataRecord(each);
-            if (duplicateKeyMap.containsKey(key)) {
-                duplicateKeyMap.put(key, true);
-                continue;
-            }
-            duplicateKeyMap.put(key, false);
+            duplicateKeyMap.put(key, duplicateKeyMap.containsKey(key));
         }
         List<GroupedDataRecord> result = new ArrayList<>(100);
         if (insertCount == dataRecords.size()) {