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 a36b6a8a361 Refactor DistSQLQueryExecuteEngine (#29981)
a36b6a8a361 is described below

commit a36b6a8a3617f90782b95b194374b223f80172c5
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Feb 4 17:11:50 2024 +0800

    Refactor DistSQLQueryExecuteEngine (#29981)
---
 .../type/query/DistSQLQueryExecuteEngine.java      | 16 ++--
 .../rule/engine/RuleDefinitionExecuteEngine.java   | 86 ----------------------
 2 files changed, 10 insertions(+), 92 deletions(-)

diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
index 33ba3643324..2f0af218af2 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.distsql.handler.type.query;
 
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorConnectionContextAware;
 import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
 import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware;
@@ -39,21 +38,26 @@ import java.util.Optional;
 /**
  * DistSQL query execute engine.
  */
-@RequiredArgsConstructor
 public abstract class DistSQLQueryExecuteEngine {
     
     private final DistSQLStatement sqlStatement;
     
-    private final String currentDatabaseName;
-    
     private final ContextManager contextManager;
     
+    private final String databaseName;
+    
     @Getter
     private Collection<String> columnNames;
     
     @Getter
     private Collection<LocalDataQueryResultRow> rows;
     
+    public DistSQLQueryExecuteEngine(final DistSQLStatement sqlStatement, 
final String currentDatabaseName, final ContextManager contextManager) {
+        this.sqlStatement = sqlStatement;
+        this.contextManager = contextManager;
+        databaseName = DatabaseNameUtils.getDatabaseName(sqlStatement, 
currentDatabaseName);
+    }
+    
     /**
      * Execute query.
      *
@@ -64,7 +68,7 @@ public abstract class DistSQLQueryExecuteEngine {
         DistSQLQueryExecutor<DistSQLStatement> executor = 
TypedSPILoader.getService(DistSQLQueryExecutor.class, sqlStatement.getClass());
         columnNames = executor.getColumnNames();
         if (executor instanceof DistSQLExecutorDatabaseAware) {
-            ((DistSQLExecutorDatabaseAware) 
executor).setDatabase(getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
 currentDatabaseName)));
+            ((DistSQLExecutorDatabaseAware) 
executor).setDatabase(getDatabase(databaseName));
         }
         if (executor instanceof DistSQLExecutorConnectionContextAware) {
             ((DistSQLExecutorConnectionContextAware) 
executor).setConnectionContext(getDistSQLConnectionContext());
@@ -89,7 +93,7 @@ public abstract class DistSQLQueryExecuteEngine {
     
     private Optional<ShardingSphereRule> findRule(final 
Class<ShardingSphereRule> ruleClass) {
         Optional<ShardingSphereRule> globalRule = 
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ruleClass);
-        return globalRule.isPresent() ? globalRule : 
getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, 
currentDatabaseName)).getRuleMetaData().findSingleRule(ruleClass);
+        return globalRule.isPresent() ? globalRule : 
getDatabase(databaseName).getRuleMetaData().findSingleRule(ruleClass);
     }
     
     protected abstract ShardingSphereDatabase getDatabase(String databaseName);
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/RuleDefinitionExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/RuleDefinitionExecuteEngine.java
deleted file mode 100644
index fa29350550e..00000000000
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/rdl/rule/engine/RuleDefinitionExecuteEngine.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine;
-
-import lombok.RequiredArgsConstructor;
-import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.database.DatabaseRuleDefinitionExecuteEngine;
-import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.global.GlobalRuleDefinitionExecuteEngine;
-import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.legacy.LegacyGlobalRuleDefinitionExecuteEngine;
-import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor;
-import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
-import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
-import 
org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.legacy.LegacyDatabaseRuleDefinitionExecuteEngine;
-
-import java.util.Optional;
-
-/**
- * Rule definition execute engine.
- */
-@RequiredArgsConstructor
-public abstract class RuleDefinitionExecuteEngine {
-    
-    private final RuleDefinitionStatement sqlStatement;
-    
-    private final String currentDatabaseName;
-    
-    private final ContextManager contextManager;
-    
-    /**
-     * Execute update.
-     */
-    @SuppressWarnings("rawtypes")
-    public void executeUpdate() {
-        Optional<DatabaseRuleDefinitionExecutor> databaseExecutor = 
TypedSPILoader.findService(DatabaseRuleDefinitionExecutor.class, 
sqlStatement.getClass());
-        if (databaseExecutor.isPresent()) {
-            executeDatabaseRule(databaseExecutor.get());
-        } else {
-            
executeGlobalRule(TypedSPILoader.getService(GlobalRuleDefinitionExecutor.class, 
sqlStatement.getClass()));
-        }
-    }
-    
-    @SuppressWarnings("rawtypes")
-    private void executeDatabaseRule(final DatabaseRuleDefinitionExecutor 
databaseExecutor) {
-        if (isNormalRuleUpdater()) {
-            new DatabaseRuleDefinitionExecuteEngine(
-                    sqlStatement, contextManager, 
getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, 
currentDatabaseName)), databaseExecutor).executeUpdate();
-        } else {
-            new LegacyDatabaseRuleDefinitionExecuteEngine(
-                    sqlStatement, contextManager, 
getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, 
currentDatabaseName)), databaseExecutor).executeUpdate();
-        }
-    }
-    
-    @SuppressWarnings("rawtypes")
-    private void executeGlobalRule(final GlobalRuleDefinitionExecutor 
globalExecutor) {
-        if (isNormalRuleUpdater()) {
-            new GlobalRuleDefinitionExecuteEngine(sqlStatement, 
contextManager, globalExecutor).executeUpdate();
-        } else {
-            new LegacyGlobalRuleDefinitionExecuteEngine(sqlStatement, 
contextManager, globalExecutor).executeUpdate();
-        }
-    }
-    
-    private boolean isNormalRuleUpdater() {
-        String modeType = 
contextManager.getInstanceContext().getModeConfiguration().getType();
-        return "Cluster".equals(modeType) || "Standalone".equals(modeType);
-    }
-    
-    protected abstract ShardingSphereDatabase getDatabase(String databaseName);
-}

Reply via email to