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/05/10 11:15:27 UTC

[shardingsphere] branch master updated: Use ShardingSphere SPI in FeaturedDistSQLStatementParserEngine (#17525)

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

zhaojinchao 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 c4957a8b8f7 Use ShardingSphere SPI in FeaturedDistSQLStatementParserEngine (#17525)
c4957a8b8f7 is described below

commit c4957a8b8f70f520699e1e97a9462ebb704cd7ce
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue May 10 19:15:12 2022 +0800

    Use ShardingSphere SPI in FeaturedDistSQLStatementParserEngine (#17525)
---
 .../shardingsphere-distsql-parser/pom.xml          |  5 ++
 .../FeaturedDistSQLStatementParserEngine.java      | 18 ++-----
 ...eaturedDistSQLStatementParserFacadeFactory.java | 56 ++++++++++++++++++++++
 .../spi/FeaturedDistSQLStatementParserFacade.java  | 12 ++---
 ...abaseDiscoveryDistSQLStatementParserFacade.java |  2 +-
 .../EncryptDistSQLStatementParserFacade.java       |  2 +-
 ...writeSplittingDistSQLStatementParserFacade.java |  2 +-
 .../facade/ShadowDistSQLStatementParserFacade.java |  2 +-
 .../ScalingDistSQLStatementParserFacade.java       |  2 +-
 .../ShardingDistSQLStatementParserFacade.java      |  2 +-
 10 files changed, 74 insertions(+), 29 deletions(-)

diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/pom.xml b/shardingsphere-distsql/shardingsphere-distsql-parser/pom.xml
index 32d35d2581b..ba0b74d9be8 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/pom.xml
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/pom.xml
@@ -27,6 +27,11 @@
     <artifactId>shardingsphere-distsql-parser</artifactId>
     
     <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-spi</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-distsql-statement</artifactId>
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
index 9a4bf1d5908..c39da7c9170 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
@@ -28,23 +28,11 @@ import org.apache.shardingsphere.sql.parser.core.SQLParserFactory;
 import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
-import java.util.HashMap;
-import java.util.Map;
-import java.util.ServiceLoader;
-
 /**
  * Featured dist SQL statement parser engine.
  */
 public final class FeaturedDistSQLStatementParserEngine {
     
-    private static final Map<String, FeaturedDistSQLStatementParserFacade> FACADES = new HashMap<>();
-    
-    static {
-        for (FeaturedDistSQLStatementParserFacade each : ServiceLoader.load(FeaturedDistSQLStatementParserFacade.class)) {
-            FACADES.put(each.getFeatureType(), each);
-        }
-    }
-    
     /**
      * Parse SQL.
      *
@@ -57,10 +45,10 @@ public final class FeaturedDistSQLStatementParserEngine {
     }
     
     private FeaturedDistSQLParseASTNode parseToASTNode(final String sql) {
-        for (FeaturedDistSQLStatementParserFacade each : FACADES.values()) {
+        for (FeaturedDistSQLStatementParserFacade each : FeaturedDistSQLStatementParserFacadeFactory.getAllInstances()) {
             try {
                 ParseASTNode parseASTNode = (ParseASTNode) SQLParserFactory.newInstance(sql, each.getLexerClass(), each.getParserClass()).parse();
-                return new FeaturedDistSQLParseASTNode(each.getFeatureType(), parseASTNode);
+                return new FeaturedDistSQLParseASTNode(each.getType(), parseASTNode);
             } catch (final ParseCancellationException | SQLParsingException ignored) {
             }
         }
@@ -70,7 +58,7 @@ public final class FeaturedDistSQLStatementParserEngine {
     @SneakyThrows(ReflectiveOperationException.class)
     @SuppressWarnings("rawtypes")
     private SQLStatement getSQLStatement(final String sql, final String featureType, final ParseASTNode parseASTNode) {
-        SQLVisitor visitor = FACADES.get(featureType).getVisitorClass().getDeclaredConstructor().newInstance();
+        SQLVisitor visitor = FeaturedDistSQLStatementParserFacadeFactory.getInstance(featureType).getVisitorClass().getDeclaredConstructor().newInstance();
         if (parseASTNode.getRootNode() instanceof ErrorNode) {
             throw new SQLParsingException("Unsupported SQL of `%s`", sql);
         }
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserFacadeFactory.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserFacadeFactory.java
new file mode 100644
index 00000000000..c83561a5751
--- /dev/null
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserFacadeFactory.java
@@ -0,0 +1,56 @@
+/*
+ * 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.parser.core.featured;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.distsql.parser.engine.spi.FeaturedDistSQLStatementParserFacade;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
+
+import java.util.Collection;
+
+/**
+ * Featured dist SQL statement parser facade factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class FeaturedDistSQLStatementParserFacadeFactory {
+    
+    static {
+        ShardingSphereServiceLoader.register(FeaturedDistSQLStatementParserFacade.class);
+    }
+    
+    /**
+     * Get instance of featured dist SQL statement parser facade.
+     * 
+     * @param type feature type
+     * @return instance of featured dist SQL statement parser facade
+     */
+    public static FeaturedDistSQLStatementParserFacade getInstance(final String type) {
+        return TypedSPIRegistry.getRegisteredService(FeaturedDistSQLStatementParserFacade.class, type);
+    }
+    
+    /**
+     * Get all instances of featured dist SQL statement parser facade.
+     * 
+     * @return all instances of featured dist SQL statement parser facade
+     */
+    public static Collection<FeaturedDistSQLStatementParserFacade> getAllInstances() {
+        return ShardingSphereServiceLoader.getServiceInstances(FeaturedDistSQLStatementParserFacade.class);
+    }
+}
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java
index 4e21ac7f9ee..6ae9d56b7e3 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/engine/spi/FeaturedDistSQLStatementParserFacade.java
@@ -17,13 +17,16 @@
 
 package org.apache.shardingsphere.distsql.parser.engine.spi;
 
+import org.apache.shardingsphere.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.spi.type.typed.TypedSPI;
 import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
 import org.apache.shardingsphere.sql.parser.spi.SQLParserFacade;
 
 /**
  * Featured dist SQL statement parser facade.
  */
-public interface FeaturedDistSQLStatementParserFacade extends SQLParserFacade {
+@SingletonSPI
+public interface FeaturedDistSQLStatementParserFacade extends SQLParserFacade, TypedSPI {
     
     /**
      * Get visitor class.
@@ -31,11 +34,4 @@ public interface FeaturedDistSQLStatementParserFacade extends SQLParserFacade {
      * @return visitor class
      */
     Class<? extends SQLVisitor> getVisitorClass();
-    
-    /**
-     * Get feature type.
-     *
-     * @return feature type
-     */
-    String getFeatureType();
 }
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/facade/DatabaseDiscoveryDistSQLStatementParserFacade.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/facade/DatabaseDiscovery [...]
index ed2c69c67f0..ca374f7c02d 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/facade/DatabaseDiscoveryDistSQLStatementParserFacade.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/facade/DatabaseDiscoveryDistSQLStatementParserFacade.java
@@ -46,7 +46,7 @@ public final class DatabaseDiscoveryDistSQLStatementParserFacade implements Feat
     }
     
     @Override
-    public String getFeatureType() {
+    public String getType() {
         return "database-discovery";
     }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-parser/src/main/java/org/apache/shardingsphere/encrypt/distsql/parser/facade/EncryptDistSQLStatementParserFacade.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-parser/src/main/java/org/apache/shardingsphere/encrypt/distsql/parser/facade/EncryptDistSQLStatementParserFacade.java
index 2c19050ba46..400ece86fdb 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-parser/src/main/java/org/apache/shardingsphere/encrypt/distsql/parser/facade/EncryptDistSQLStatementParserFacade.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-parser/src/main/java/org/apache/shardingsphere/encrypt/distsql/parser/facade/EncryptDistSQLStatementParserFacade.java
@@ -46,7 +46,7 @@ public final class EncryptDistSQLStatementParserFacade implements FeaturedDistSQ
     }
     
     @Override
-    public String getFeatureType() {
+    public String getType() {
         return "encrypt";
     }
 }
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-parser/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/parser/facade/ReadwriteSplittingDistSQLStatementParserFacade.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-parser/src/main/java/org/apache/shardingsphere/r [...]
index 4ea5a6d213d..a21162b3e5f 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-parser/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/parser/facade/ReadwriteSplittingDistSQLStatementParserFacade.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-parser/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/parser/facade/ReadwriteSplittingDistSQLStatementParserFacade.java
@@ -46,7 +46,7 @@ public final class ReadwriteSplittingDistSQLStatementParserFacade implements Fea
     }
     
     @Override
-    public String getFeatureType() {
+    public String getType() {
         return "readwrite-splitting";
     }
 }
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/facade/ShadowDistSQLStatementParserFacade.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/facade/ShadowDistSQLStatementParserFacade.java
index 23e3591b941..2c02b32d64b 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/facade/ShadowDistSQLStatementParserFacade.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/facade/ShadowDistSQLStatementParserFacade.java
@@ -46,7 +46,7 @@ public final class ShadowDistSQLStatementParserFacade implements FeaturedDistSQL
     }
     
     @Override
-    public String getFeatureType() {
+    public String getType() {
         return "shadow";
     }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/facade/ScalingDistSQLStatementParserFacade.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/facade/ScalingDistSQLStatementParserFacade.java
index 4471bff93ab..546f799e88d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/facade/ScalingDistSQLStatementParserFacade.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/scaling/distsql/parser/facade/ScalingDistSQLStatementParserFacade.java
@@ -46,7 +46,7 @@ public final class ScalingDistSQLStatementParserFacade implements FeaturedDistSQ
     }
     
     @Override
-    public String getFeatureType() {
+    public String getType() {
         return "scaling";
     }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/facade/ShardingDistSQLStatementParserFacade.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/facade/ShardingDistSQLStatementParserFacade.java
index 1a8c14dd405..afd1b27dd24 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/facade/ShardingDistSQLStatementParserFacade.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/facade/ShardingDistSQLStatementParserFacade.java
@@ -46,7 +46,7 @@ public final class ShardingDistSQLStatementParserFacade implements FeaturedDistS
     }
     
     @Override
-    public String getFeatureType() {
+    public String getType() {
         return "sharding";
     }
 }