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 2022/02/23 13:10:13 UTC
[shardingsphere] branch master updated: Simplify logic and avoid map resize (#15598)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 c9f08df Simplify logic and avoid map resize (#15598)
c9f08df is described below
commit c9f08df2546bd0efca6884c73cab56290bedb7f4
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Wed Feb 23 21:09:09 2022 +0800
Simplify logic and avoid map resize (#15598)
* Simplify logic in TablesContext
* Simplify logic in ExecutionContextBuilder
* Simplify logic in RouteSQLRewriteEngine
* Simplify logic in WhereClauseShardingConditionEngine
---
.../condition/engine/impl/WhereClauseShardingConditionEngine.java | 8 +-------
.../shardingsphere/infra/binder/segment/table/TablesContext.java | 4 +---
.../infra/executor/sql/context/ExecutionContextBuilder.java | 2 +-
.../infra/rewrite/engine/RouteSQLRewriteEngine.java | 5 +----
4 files changed, 4 insertions(+), 15 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
index 832d4e1..ad2fc87 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
@@ -131,13 +131,7 @@ public final class WhereClauseShardingConditionEngine implements ShardingConditi
if (!shardingConditionValue.isPresent()) {
continue;
}
- if (!result.containsKey(column)) {
- Collection<ShardingConditionValue> shardingConditionValues = new LinkedList<>();
- shardingConditionValues.add(shardingConditionValue.get());
- result.put(column, shardingConditionValues);
- } else {
- result.get(column).add(shardingConditionValue.get());
- }
+ result.computeIfAbsent(column, unused -> new LinkedList<>()).add(shardingConditionValue.get());
}
}
return result;
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java
index d8b9d71..c76bbbd 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/table/TablesContext.java
@@ -153,9 +153,7 @@ public final class TablesContext {
if (null == each.getOwner()) {
continue;
}
- Collection<String> columnExpressions = result.getOrDefault(each.getOwner(), new LinkedList<>());
- columnExpressions.add(each.getExpression());
- result.put(each.getOwner(), columnExpressions);
+ result.computeIfAbsent(each.getOwner(), unused -> new LinkedList<>()).add(each.getExpression());
}
return result;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilder.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilder.java
index eed159d..227acc5 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilder.java
@@ -63,7 +63,7 @@ public final class ExecutionContextBuilder {
}
private static Collection<ExecutionUnit> build(final RouteSQLRewriteResult sqlRewriteResult) {
- Collection<ExecutionUnit> result = new LinkedHashSet<>();
+ Collection<ExecutionUnit> result = new LinkedHashSet<>(sqlRewriteResult.getSqlRewriteUnits().size(), 1f);
for (Entry<RouteUnit, SQLRewriteUnit> entry : sqlRewriteResult.getSqlRewriteUnits().entrySet()) {
result.add(new ExecutionUnit(entry.getKey().getDataSourceMapper().getActualName(),
new SQLUnit(entry.getValue().getSql(), entry.getValue().getParameters(), getRouteTableRouteMappers(entry.getKey().getTableMappers()))));
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java
index f24cd12..96d4da9 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java
@@ -104,10 +104,7 @@ public final class RouteSQLRewriteEngine {
Map<String, Collection<RouteUnit>> result = new LinkedHashMap<>(routeUnits.size(), 1);
for (RouteUnit each : routeUnits) {
String dataSourceName = each.getDataSourceMapper().getActualName();
- if (!result.containsKey(dataSourceName)) {
- result.put(dataSourceName, new LinkedList<>());
- }
- result.get(dataSourceName).add(each);
+ result.computeIfAbsent(dataSourceName, unused -> new LinkedList<>()).add(each);
}
return result;
}