You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/02/18 23:41:22 UTC

[shardingsphere] branch master updated: Replace high frequency stream with loop (#15498)

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 8e3796f  Replace high frequency stream with loop (#15498)
8e3796f is described below

commit 8e3796fca4f635fd2c36735d114c1756e49a4fa8
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Sat Feb 19 07:39:43 2022 +0800

    Replace high frequency stream with loop (#15498)
    
    * Replace high frequency stream with loop
    
    * Rename variables
---
 .../apache/shardingsphere/sharding/rule/ShardingRule.java | 15 +++++++++++++--
 .../projection/engine/ProjectionsContextEngine.java       | 10 ++++++----
 .../binder/statement/dml/DeleteStatementContext.java      |  9 +++++----
 .../communication/DatabaseCommunicationEngine.java        | 13 +++++++++++--
 .../proxy/backend/communication/ProxySQLExecutor.java     | 13 ++++++++++---
 .../jdbc/datasource/JDBCBackendDataSource.java            | 11 +++++++----
 .../backend/text/TextProtocolBackendHandlerFactory.java   |  7 ++++++-
 .../apache/shardingsphere/spi/typed/TypedSPIRegistry.java | 12 ++++++------
 8 files changed, 64 insertions(+), 26 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index ac3d38b..92a77af 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -314,7 +314,13 @@ public final class ShardingRule implements SchemaRule, DataNodeContainedRule, Ta
     }
     
     private Optional<BindingTableRule> findBindingTableRule(final Collection<String> logicTableNames) {
-        return logicTableNames.stream().map(this::findBindingTableRule).filter(Optional::isPresent).findFirst().orElse(Optional.empty());
+        for (String each : logicTableNames) {
+            Optional<BindingTableRule> result = findBindingTableRule(each);
+            if (result.isPresent()) {
+                return result;
+            }
+        }
+        return Optional.empty();
     }
     
     /**
@@ -394,7 +400,12 @@ public final class ShardingRule implements SchemaRule, DataNodeContainedRule, Ta
      * @return whether a table rule exists for logic tables
      */
     public boolean tableRuleExists(final Collection<String> logicTableNames) {
-        return logicTableNames.stream().anyMatch(each -> isShardingTable(each) || isBroadcastTable(each));
+        for (String each : logicTableNames) {
+            if (isShardingTable(each) || isBroadcastTable(each)) {
+                return true;
+            }
+        }
+        return false;
     }
     
     /**
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
index 58e1208..4eed67f 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
@@ -118,11 +118,13 @@ public final class ProjectionsContextEngine {
         if (columns.isEmpty()) {
             return false;
         }
-        if (columnSegment.getOwner().isPresent()) {
-            return columns.stream().anyMatch(each -> isSameQualifiedName(each, columnSegment.getQualifiedName()));
-        } else {
-            return columns.stream().anyMatch(each -> isSameName(each, columnSegment.getQualifiedName()));
+        boolean columnSegmentPresent = columnSegment.getOwner().isPresent();
+        for (ColumnProjection each : columns) {
+            if (columnSegmentPresent ? isSameQualifiedName(each, columnSegment.getQualifiedName()) : isSameName(each, columnSegment.getQualifiedName())) {
+                return true;
+            }
         }
+        return false;
     }
     
     private Collection<ColumnProjection> getColumnProjections(final Projection projection) {
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/DeleteStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/DeleteStatementContext.java
index 19e95ee..685c997 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/DeleteStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/DeleteStatementContext.java
@@ -28,10 +28,9 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.Sim
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
-import java.util.function.Function;
-import java.util.stream.Collectors;
 
 /**
  * Delete statement context.
@@ -56,8 +55,10 @@ public final class DeleteStatementContext extends CommonSQLStatementContext<Dele
     }
     
     private Collection<SimpleTableSegment> filterAliasDeleteTable(final Collection<SimpleTableSegment> tableSegments) {
-        Map<String, SimpleTableSegment> aliasTableSegmentMap = tableSegments.stream().filter(each 
-            -> each.getAlias().isPresent()).collect(Collectors.toMap(each -> each.getAlias().get(), Function.identity(), (oldValue, currentValue) -> oldValue));
+        Map<String, SimpleTableSegment> aliasTableSegmentMap = new HashMap<>(tableSegments.size(), 1f);
+        for (SimpleTableSegment each : tableSegments) {
+            each.getAlias().ifPresent(alias -> aliasTableSegmentMap.putIfAbsent(alias, each));
+        }
         Collection<SimpleTableSegment> result = new LinkedList<>();
         for (SimpleTableSegment each : tableSegments) {
             SimpleTableSegment aliasDeleteTable = aliasTableSegmentMap.get(each.getTableName().getIdentifier().getValue());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
index 72185e2..bd29a01 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriv
 import org.apache.shardingsphere.infra.merge.MergeEngine;
 import org.apache.shardingsphere.infra.merge.result.MergedResult;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
@@ -156,11 +157,19 @@ public abstract class DatabaseCommunicationEngine<T> {
     }
     
     protected boolean isNeedAccumulate(final SQLStatementContext<?> sqlStatementContext) {
-        Optional<DataNodeContainedRule> dataNodeContainedRule =
-                metaData.getRuleMetaData().getRules().stream().filter(each -> each instanceof DataNodeContainedRule).findFirst().map(rule -> (DataNodeContainedRule) rule);
+        Optional<DataNodeContainedRule> dataNodeContainedRule = findDataNodeContainedRule();
         return dataNodeContainedRule.isPresent() && dataNodeContainedRule.get().isNeedAccumulate(sqlStatementContext.getTablesContext().getTableNames());
     }
     
+    private Optional<DataNodeContainedRule> findDataNodeContainedRule() {
+        for (ShardingSphereRule each : metaData.getRuleMetaData().getRules()) {
+            if (each instanceof DataNodeContainedRule) {
+                return Optional.of((DataNodeContainedRule) each);
+            }
+        }
+        return Optional.empty();
+    }
+    
     /**
      * Goto next result value.
      *
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java
index 24c93be..8fb29ae 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxySQLExecutor.java
@@ -120,10 +120,17 @@ public final class ProxySQLExecutor {
     
     private List<ExecuteResult> execute(final ExecutionContext executionContext, final Collection<ShardingSphereRule> rules,
                                         final int maxConnectionsSizePerQuery, final boolean isReturnGeneratedKeys) throws SQLException {
-        if (rules.stream().anyMatch(each -> each instanceof RawExecutionRule)) {
-            return rawExecute(executionContext, rules, maxConnectionsSizePerQuery);
+        return hasRawExecutionRule(rules) ? rawExecute(executionContext, rules, maxConnectionsSizePerQuery) 
+                : useDriverToExecute(executionContext, rules, maxConnectionsSizePerQuery, isReturnGeneratedKeys, SQLExecutorExceptionHandler.isExceptionThrown());
+    }
+    
+    private boolean hasRawExecutionRule(final Collection<ShardingSphereRule> rules) {
+        for (ShardingSphereRule each : rules) {
+            if (each instanceof RawExecutionRule) {
+                return true;
+            }
         }
-        return useDriverToExecute(executionContext, rules, maxConnectionsSizePerQuery, isReturnGeneratedKeys, SQLExecutorExceptionHandler.isExceptionThrown());
+        return false;
     }
     
     private List<ExecuteResult> rawExecute(final ExecutionContext executionContext, final Collection<ShardingSphereRule> rules, final int maxConnectionsSizePerQuery) throws SQLException {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/JDBCBackendDataSource.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/JDBCBackendDataSource.java
index 40cce43..cb319cf 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/JDBCBackendDataSource.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/JDBCBackendDataSource.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource;
 
 import com.google.common.base.Preconditions;
 import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.proxy.backend.communication.BackendDataSource;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.transaction.core.TransactionType;
@@ -32,7 +33,6 @@ import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.Optional;
 
 /**
  * Backend data source of JDBC.
@@ -108,8 +108,11 @@ public final class JDBCBackendDataSource implements BackendDataSource {
     }
     
     private TransactionRule getTransactionRule() {
-        Optional<TransactionRule> transactionRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData().getRules().stream().filter(
-            each -> each instanceof TransactionRule).map(each -> (TransactionRule) each).findFirst();
-        return transactionRule.orElseGet(() -> new TransactionRule(new DefaultTransactionRuleConfigurationBuilder().build()));
+        for (ShardingSphereRule each : ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData().getRules()) {
+            if (each instanceof TransactionRule) {
+                return (TransactionRule) each;
+            }
+        }
+        return new TransactionRule(new DefaultTransactionRuleConfigurationBuilder().build());
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index 6334294..061f4ca 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -129,7 +129,12 @@ public final class TextProtocolBackendHandlerFactory {
     }
     
     private static Optional<ExtraTextProtocolBackendHandler> findExtraTextProtocolBackendHandler(final SQLStatement sqlStatement) {
-        return ShardingSphereServiceLoader.getSingletonServiceInstances(ExtraTextProtocolBackendHandler.class).stream().filter(each -> each.accept(sqlStatement)).findFirst();
+        for (ExtraTextProtocolBackendHandler each : ShardingSphereServiceLoader.getSingletonServiceInstances(ExtraTextProtocolBackendHandler.class)) {
+            if (each.accept(sqlStatement)) {
+                return Optional.of(each);
+            }
+        }
+        return Optional.empty();
     }
     
     private static Optional<TextProtocolBackendHandler> findDatabaseOperateBackendHandler(final SQLStatement sqlStatement, final ConnectionSession connectionSession) throws SQLException {
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java
index 83e6d71..baf8ab5 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java
@@ -41,13 +41,13 @@ public final class TypedSPIRegistry {
      * @return registered service
      */
     public static <T extends TypedSPI> Optional<T> findRegisteredService(final Class<T> typedSPIClass, final String type, final Properties props) {
-        Optional<T> serviceInstance = ShardingSphereServiceLoader.newServiceInstances(typedSPIClass).stream().filter(each -> each.getType().equalsIgnoreCase(type)).findFirst();
-        if (!serviceInstance.isPresent()) {
-            return Optional.empty();
+        for (T each : ShardingSphereServiceLoader.newServiceInstances(typedSPIClass)) {
+            if (each.getType().equalsIgnoreCase(type)) {
+                setProperties(each, props);
+                return Optional.of(each);
+            }
         }
-        T result = serviceInstance.get();
-        setProperties(result, props);
-        return Optional.of(result);
+        return Optional.empty();
     }
     
     private static <T extends TypedSPI> void setProperties(final T service, final Properties props) {