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

[shardingsphere] branch master updated: Replace `DefaultShadowAlgorithmResultSet` with `ShowDefaultShadowAlgorithmExecutor` (#23828)

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

jianglongtao 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 24aa49c4f02 Replace `DefaultShadowAlgorithmResultSet` with `ShowDefaultShadowAlgorithmExecutor` (#23828)
24aa49c4f02 is described below

commit 24aa49c4f02e5b1f57ffc1e4a69c1a44b02e5e7b
Author: Zichao <57...@users.noreply.github.com>
AuthorDate: Mon Jan 30 19:05:02 2023 +1300

    Replace `DefaultShadowAlgorithmResultSet` with `ShowDefaultShadowAlgorithmExecutor` (#23828)
---
 .../rql/rule-query/shadow/count-shadow-rule.cn.md  |  2 +-
 .../rql/rule-query/shadow/count-shadow-rule.en.md  |  2 +-
 .../shadow/show-default-shadow-algorithm.cn.md     | 81 +++++++++++++++++++++
 ...m.en.md => show-default-shadow-algorithm.en.md} | 36 ++++-----
 .../rule-query/shadow/show-shadow-algorithm.en.md  |  3 -
 ...ava => ShowDefaultShadowAlgorithmExecutor.java} | 44 +++++------
 ...hardingsphere.distsql.handler.query.RQLExecutor |  1 +
 ...here.distsql.handler.resultset.DistSQLResultSet |  1 -
 .../ShowDefaultShadowAlgorithmExecutorTest.java    | 85 ++++++++++++++++++++++
 9 files changed, 203 insertions(+), 52 deletions(-)

diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.cn.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.cn.md
index fb844296d42..30ad980e893 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.cn.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.cn.md
@@ -1,6 +1,6 @@
 +++
 title = "COUNT SHADOW RULE"
-weight = 5
+weight = 6
 +++
 
 ### 描述
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.en.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.en.md
index 81c73765444..3be305f94f6 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.en.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/count-shadow-rule.en.md
@@ -1,6 +1,6 @@
 +++
 title = "COUNT SHADOW RULE"
-weight = 5
+weight = 6
 +++
 
 ### Description
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-default-shadow-algorithm.cn.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-default-shadow-algorithm.cn.md
new file mode 100644
index 00000000000..60c98cb4380
--- /dev/null
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-default-shadow-algorithm.cn.md
@@ -0,0 +1,81 @@
++++
+title = "SHOW DEFAULT SHADOW ALGORITHM"
+weight = 5
++++
+
+### 描述
+
+`SHOW DEFAULT SHADOW ALGORITHM` 语法用于查询指定逻辑库中的默认影子算法。
+
+### 语法
+
+{{< tabs >}}
+{{% tab name="语法" %}}
+```sql
+ShowEncryptAlgorithm::=
+  'SHOW' 'DEFAULT' 'SHADOW' 'ALGORITHMS' ('FROM' databaseName)?
+
+databaseName ::=
+  identifier
+```
+{{% /tab %}}
+{{% tab name="铁路图" %}}
+<iframe frameborder="0" name="diagram" id="diagram" width="100%" height="100%"></iframe>
+{{% /tab %}}
+{{< /tabs >}}
+
+### 补充说明
+
+- 未指定 `databaseName` 时,默认是当前使用的 `DATABASE`。 如果也未使用 `DATABASE` 则会提示 `No database selected`。
+
+### 返回值说明
+
+| 列                    | 说明        |
+| ----------------------| ---------- |
+| shadow_algorithm_name | 影子算法名称 |
+| type                  | 算法类型    |
+| props                 | 算法参数    |
+
+### 示例
+
+- 查询指定逻辑库中的默认影子算法
+
+```sql
+SHOW DEFAULT SHADOW ALGORITHMS FROM shadow_db;
+```
+
+```sql
+mysql> SHOW DEFAULT SHADOW ALGORITHMS FROM shadow_db;
++-------------------------+-------------+-----------------------------------------+
+| shadow_algorithm_name   | type        | props                                   |
++-------------------------+-------------+-----------------------------------------+
+| user_id_match_algorithm | VALUE_MATCH | column=user_id,operation=insert,value=1 |
++-------------------------+-------------+-----------------------------------------+
+1 row in set (0.00 sec)
+```
+
+- 查询当前逻辑库中的默认影子算法
+
+```sql
+SHOW DEFAULT SHADOW ALGORITHMS;
+```
+
+```sql
+mysql> SHOW DEFAULT SHADOW ALGORITHMS;
++-------------------------+-------------+-----------------------------------------+
+| shadow_algorithm_name   | type        | props                                   |
++-------------------------+-------------+-----------------------------------------+
+| user_id_match_algorithm | VALUE_MATCH | column=user_id,operation=insert,value=1 |
++-------------------------+-------------+-----------------------------------------+
+1 row in set (0.00 sec)
+```
+
+
+### 保留字
+
+`SHOW`、`DEFAULT`、`SHADOW`、`ALGORITHMS`、`FROM`
+
+### 相关链接
+
+- [保留字](/cn/user-manual/shardingsphere-proxy/distsql/syntax/reserved-word/)
+
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-shadow-algorithm.en.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-default-shadow-algorithm.en.md
similarity index 74%
copy from docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-shadow-algorithm.en.md
copy to docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-default-shadow-algorithm.en.md
index 5ce55059191..01f04526e18 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-shadow-algorithm.en.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-default-shadow-algorithm.en.md
@@ -1,11 +1,11 @@
 +++
-title = "SHOW SHADOW ALGORITHM"
-weight = 4
+title = "SHOW DEFAULT SHADOW ALGORITHM"
+weight = 5
 +++
 
 ### Description
 
-The `SHOW SHADOW ALGORITHM` syntax is used to query shadow algorithms for specified database.
+The `SHOW DEFAULT SHADOW ALGORITHM` syntax is used to query default shadow algorithms for specified database.
 
 ### Syntax
 
@@ -35,26 +35,22 @@ databaseName ::=
 | shadow_algorithm_name | Shadow algorithm name       |
 | type                  | Shadow algorithm type       |
 | props                 | Shadow algorithm properties |
-| is_default            | Default                     |
-
-
-
 
 ### Example
 
 - Query shadow algorithms for specified database.
 
 ```sql
-SHOW SHADOW ALGORITHMS FROM shadow_db;
+SHOW DEFAULT SHADOW ALGORITHMS FROM shadow_db;
 ```
 
 ```sql
-mysql> SHOW SHADOW ALGORITHMS FROM shadow_db;
-+-------------------------+-------------+-----------------------------------------+------------+
-| shadow_algorithm_name   | type        | props                                   | is_default |
-+-------------------------+-------------+-----------------------------------------+------------+
-| user_id_match_algorithm | VALUE_MATCH | column=user_id,operation=insert,value=1 | false      |
-+-------------------------+-------------+-----------------------------------------+------------+
+mysql> SHOW DEFAULT SHADOW ALGORITHMS FROM shadow_db;
++-------------------------+-------------+-----------------------------------------+
+| shadow_algorithm_name   | type        | props                                   |
++-------------------------+-------------+-----------------------------------------+
+| user_id_match_algorithm | VALUE_MATCH | column=user_id,operation=insert,value=1 |
++-------------------------+-------------+-----------------------------------------+
 1 row in set (0.00 sec)
 ```
 
@@ -66,17 +62,17 @@ SHOW SHADOW ALGORITHMS;
 
 ```sql
 mysql> SHOW SHADOW ALGORITHMS;
-+-------------------------+-------------+-----------------------------------------+------------+
-| shadow_algorithm_name   | type        | props                                   | is_default |
-+-------------------------+-------------+-----------------------------------------+------------+
-| user_id_match_algorithm | VALUE_MATCH | column=user_id,operation=insert,value=1 | false      |
-+-------------------------+-------------+-----------------------------------------+------------+
++-------------------------+-------------+-----------------------------------------+
+| shadow_algorithm_name   | type        | props                                   |
++-------------------------+-------------+-----------------------------------------+
+| user_id_match_algorithm | VALUE_MATCH | column=user_id,operation=insert,value=1 |
++-------------------------+-------------+-----------------------------------------+
 1 row in set (0.00 sec)
 ```
 
 ### Reserved word
 
-`SHOW`, `SHADOW`, `ALGORITHMS`, `FROM`
+`SHOW`, `DEFAULT`,`SHADOW`, `ALGORITHMS`, `FROM`
 
 ### Related links
 
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-shadow-algorithm.en.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-shadow-algorithm.en.md
index 5ce55059191..4b762db7e94 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-shadow-algorithm.en.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rql/rule-query/shadow/show-shadow-algorithm.en.md
@@ -37,9 +37,6 @@ databaseName ::=
 | props                 | Shadow algorithm properties |
 | is_default            | Default                     |
 
-
-
-
 ### Example
 
 - Query shadow algorithms for specified database.
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/DefaultShadowAlgorithmResultSet.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowDefaultShadowAlgorithmExecutor.java
similarity index 60%
rename from features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/DefaultShadowAlgorithmResultSet.java
rename to features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowDefaultShadowAlgorithmExecutor.java
index 11837b0ec33..7ad800e1340 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/DefaultShadowAlgorithmResultSet.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowDefaultShadowAlgorithmExecutor.java
@@ -17,19 +17,20 @@
 
 package org.apache.shardingsphere.shadow.distsql.handler.query;
 
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
 import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
 import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
 import org.apache.shardingsphere.shadow.distsql.parser.statement.ShowDefaultShadowAlgorithmStatement;
 import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Optional;
@@ -37,21 +38,26 @@ import java.util.Properties;
 import java.util.stream.Collectors;
 
 /**
- * Result set for default shadow algorithm.
+ * Show default shadow algorithm executor.
  */
-public final class DefaultShadowAlgorithmResultSet implements DatabaseDistSQLResultSet {
-    
-    private Iterator<Entry<String, AlgorithmConfiguration>> data = Collections.emptyIterator();
+public final class ShowDefaultShadowAlgorithmExecutor implements RQLExecutor<ShowDefaultShadowAlgorithmStatement> {
     
     @Override
-    public void init(final ShardingSphereDatabase database, final SQLStatement sqlStatement) {
+    public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultShadowAlgorithmStatement sqlStatement) {
         Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
-        if (rule.isPresent()) {
-            ShadowRuleConfiguration config = (ShadowRuleConfiguration) rule.get().getConfiguration();
-            String defaultAlgorithm = config.getDefaultShadowAlgorithmName();
-            data = config.getShadowAlgorithms().entrySet().stream().filter(each -> each.getKey().equals(defaultAlgorithm))
-                    .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (oldValue, currentValue) -> currentValue)).entrySet().iterator();
+        if (!rule.isPresent()) {
+            return Collections.emptyList();
+        }
+        ShadowRuleConfiguration config = (ShadowRuleConfiguration) rule.get().getConfiguration();
+        String defaultAlgorithm = config.getDefaultShadowAlgorithmName();
+        Iterator<Entry<String, AlgorithmConfiguration>> data = config.getShadowAlgorithms().entrySet().stream().filter(each -> each.getKey().equals(defaultAlgorithm))
+                .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (oldValue, currentValue) -> currentValue)).entrySet().iterator();
+        Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+        while (data.hasNext()) {
+            Entry<String, AlgorithmConfiguration> entry = data.next();
+            result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), convertToString(entry.getValue().getProps())));
         }
+        return result;
     }
     
     @Override
@@ -59,20 +65,6 @@ public final class DefaultShadowAlgorithmResultSet implements DatabaseDistSQLRes
         return Arrays.asList("shadow_algorithm_name", "type", "props");
     }
     
-    @Override
-    public boolean next() {
-        return data.hasNext();
-    }
-    
-    @Override
-    public Collection<Object> getRowData() {
-        return buildTableRowData(data.next());
-    }
-    
-    private Collection<Object> buildTableRowData(final Entry<String, AlgorithmConfiguration> data) {
-        return Arrays.asList(data.getKey(), data.getValue().getType(), convertToString(data.getValue().getProps()));
-    }
-    
     private String convertToString(final Properties props) {
         return Objects.nonNull(props) ? PropertiesConverter.convert(props) : "";
     }
diff --git a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index cc62e23e483..a8181c572e7 100644
--- a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
+++ b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -18,3 +18,4 @@
 org.apache.shardingsphere.shadow.distsql.handler.query.ShowShadowRuleExecutor
 org.apache.shardingsphere.shadow.distsql.handler.query.ShowShadowTableRuleExecutor
 org.apache.shardingsphere.shadow.distsql.handler.query.ShowShadowAlgorithmExecutor
+org.apache.shardingsphere.shadow.distsql.handler.query.ShowDefaultShadowAlgorithmExecutor
diff --git a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index 8d5dce23696..18ab8c43454 100644
--- a/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ b/features/shadow/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -15,5 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.shadow.distsql.handler.query.DefaultShadowAlgorithmResultSet
 org.apache.shardingsphere.shadow.distsql.handler.query.CountShadowRuleResultSet
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java
new file mode 100644
index 00000000000..8262517c982
--- /dev/null
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.shadow.distsql.query;
+
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.distsql.handler.query.ShowDefaultShadowAlgorithmExecutor;
+import org.apache.shardingsphere.shadow.distsql.parser.statement.ShowDefaultShadowAlgorithmStatement;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ShowDefaultShadowAlgorithmExecutorTest {
+    
+    @Test
+    public void assertGetRowData() {
+        RQLExecutor<ShowDefaultShadowAlgorithmStatement> executor = new ShowDefaultShadowAlgorithmExecutor();
+        Collection<LocalDataQueryResultRow> actual = executor.getRows(mockDatabase(), mock(ShowDefaultShadowAlgorithmStatement.class));
+        assertThat(actual.size(), is(1));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("shadowAlgorithmName"));
+        assertThat(row.getCell(2), is("simple_hint"));
+        assertThat(row.getCell(3), is("foo=bar"));
+    }
+    
+    @Test
+    public void assertGetColumnNames() {
+        RQLExecutor<ShowDefaultShadowAlgorithmStatement> executor = new ShowDefaultShadowAlgorithmExecutor();
+        Collection<String> columns = executor.getColumnNames();
+        assertThat(columns.size(), is(3));
+        Iterator<String> iterator = columns.iterator();
+        assertThat(iterator.next(), is("shadow_algorithm_name"));
+        assertThat(iterator.next(), is("type"));
+        assertThat(iterator.next(), is("props"));
+    }
+    
+    private ShardingSphereDatabase mockDatabase() {
+        ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+        ShadowRule rule = mock(ShadowRule.class);
+        when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
+        when(result.getRuleMetaData()).thenReturn(new ShardingSphereRuleMetaData(Collections.singleton(rule)));
+        return result;
+    }
+    
+    private RuleConfiguration createRuleConfiguration() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Collections.singleton("shadowAlgorithmName")));
+        result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("simple_hint", PropertiesBuilder.build(new Property("foo", "bar"))));
+        result.setDefaultShadowAlgorithmName("shadowAlgorithmName");
+        return result;
+    }
+}