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 2021/09/10 08:56:39 UTC
[shardingsphere] branch master updated: Update statement route
(#12316)
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 0a710ab Update statement route (#12316)
0a710ab is described below
commit 0a710abb4f7d565223a9b0c944e400073cf2270e
Author: gin <ja...@163.com>
AuthorDate: Fri Sep 10 16:56:07 2021 +0800
Update statement route (#12316)
* Add update case example.
* Refactor shadow determiner.
* Refactor abstract shadow route engine.
* Add shadow update statement routing engine.
* Fix shadow rule configuration yaml swapper.
* Fix shadow ut.
---
.../ShadowSpringNamespaceMybatisExample.java | 15 ++-
.../namespace/mybatis/repository/OrderMapper.java | 31 +++--
.../namespace/mybatis/service/OrderService.java | 22 +---
.../namespace/mybatis/service/ShadowService.java | 2 +
.../mybatis/service/impl/OrderServiceImpl.java | 92 ++++++++++++++
.../mybatis/service/impl/ShadowServiceImpl.java | 29 ++++-
.../META-INF/application-shadow-databases.xml | 3 +-
.../mappers/{ShadowMapper.xml => OrderMapper.xml} | 32 ++++-
.../resources/META-INF/mappers/ShadowMapper.xml | 6 -
.../shadow/route/ShadowSQLRouter.java | 15 +--
.../future/engine/AbstractShadowRouteEngine.java | 107 ++++++++++++++---
.../route/future/engine/ShadowRouteEngine.java | 8 +-
.../future/engine/ShadowRouteEngineFactory.java | 14 ++-
.../determiner/ShadowAlgorithmDeterminer.java | 12 +-
.../determiner/ShadowDetermineCondition.java | 119 +++++++++++++++++++
.../engine/determiner/ShadowDeterminerFactory.java | 24 +---
.../algorithm/ColumnShadowAlgorithmDeterminer.java | 42 ++-----
.../algorithm/NoteShadowAlgorithmDeterminer.java | 17 ++-
...nyAlgorithmApplicableShadowTableDeterminer.java | 46 -------
.../dml/ShadowDeleteStatementRoutingEngine.java | 5 +-
.../dml/ShadowInsertStatementRoutingEngine.java | 82 ++++++++-----
.../dml/ShadowSelectStatementRoutingEngine.java | 5 +-
.../dml/ShadowUpdateStatementRoutingEngine.java | 87 ++++++++++++--
.../impl/ShadowNonMDLStatementRoutingEngine.java | 5 +-
.../route/future/engine/util/ShadowExtractor.java | 129 ++++++++++++++++++++
.../validator/ShadowStatementValidatorFactory.java | 54 ---------
.../dml/ShadowInsertStatementValidator.java | 36 ------
.../ShadowRuleConfigurationYamlSwapper.java | 3 +-
.../engine/AbstractShadowRouteEngineTest.java | 68 -----------
.../engine/ShadowRouteEngineFactoryTest.java | 6 +-
.../determiner/ShadowDeterminerFactoryTest.java | 27 +----
.../ColumnShadowAlgorithmDeterminerTest.java | 132 ++++++++++++---------
.../NoteShadowAlgorithmDeterminerTest.java | 82 -------------
...gorithmApplicableShadowTableDeterminerTest.java | 95 ---------------
.../ShadowInsertStatementRoutingEngineTest.java | 12 +-
35 files changed, 778 insertions(+), 686 deletions(-)
diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/ShadowSpringNamespaceMybatisExample.java b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/ShadowSpringNamespace [...]
index ff20ff3..19c8462 100644
--- a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/ShadowSpringNamespaceMybatisExample.java
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/ShadowSpringNamespaceMybatisExample.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.example.shadow.spring.namespace.mybatis;
+import org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.service.OrderService;
import org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.service.ShadowService;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -29,8 +30,18 @@ public final class ShadowSpringNamespaceMybatisExample {
public static void main(final String[] args) throws SQLException {
try (ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(CONFIG_FILE)) {
- ShadowService shadowService = applicationContext.getBean("shadowService", ShadowService.class);
- shadowService.executeInsertCase();
+ executeShadowService(applicationContext);
+ executeOrderService(applicationContext);
}
}
+
+ private static void executeOrderService(final ConfigurableApplicationContext applicationContext) {
+ OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
+ orderService.executeUpdateCase();
+ }
+
+ private static void executeShadowService(final ConfigurableApplicationContext applicationContext) {
+ ShadowService shadowService = applicationContext.getBean("shadowService", ShadowService.class);
+ shadowService.executeUpdateCase();
+ }
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/ShadowStatementValidator.java b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/repository/OrderMapper.java
similarity index 56%
rename from shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/ShadowStatementValidator.java
rename to examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/repository/OrderMapper.java
index dd86bba..2f5a412 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/ShadowStatementValidator.java
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/repository/OrderMapper.java
@@ -15,23 +15,22 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.shadow.route.future.engine.validator;
+package org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.repository;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.domain.OrderInfo;
-/**
- * Shadow statement validator.
- *
- * @param <T> type of SQL statement
- */
-public interface ShadowStatementValidator<T extends SQLStatement> {
+import java.util.List;
+import java.util.Map;
+
+@Mapper
+public interface OrderMapper {
+
+ void saveOne(OrderInfo orderInfo);
+
+ void saveBatch(List<OrderInfo> orderList);
+
+ void updateOne(OrderInfo orderInfo);
- /**
- * Validate whether shadow operation is supported before route.
- *
- * @param sqlStatementContext SQL statement context
- * @return is shadow or not
- */
- boolean preValidate(SQLStatementContext<T> sqlStatementContext);
+ void updateByUserIds(Map<String, Object> updateMap);
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowTableDeterminer.java b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/OrderService.java
similarity index 56%
rename from shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowTableDeterminer.java
rename to examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/OrderService.java
index de41965..1156525 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowTableDeterminer.java
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/OrderService.java
@@ -15,23 +15,11 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.shadow.route.future.engine.determiner;
+package org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.service;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-
-/**
- * Shadow table determiner.
- */
-public interface ShadowTableDeterminer {
+public interface OrderService {
+
+ void executeInsertCase();
- /**
- * Is shadow in shadow table.
- *
- * @param insertStatementContext insert statement context
- * @param shadowRule related shadow tables
- * @param tableName table name
- * @return is shadow or not
- */
- boolean isShadow(InsertStatementContext insertStatementContext, ShadowRule shadowRule, String tableName);
+ void executeUpdateCase();
}
diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/ShadowService.java b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/ShadowService.java
index 7e1eadc..9d00d76 100644
--- a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/ShadowService.java
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/ShadowService.java
@@ -20,4 +20,6 @@ package org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.servic
public interface ShadowService {
void executeInsertCase();
+
+ void executeUpdateCase();
}
diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/impl/OrderServiceImpl.java b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/impl/OrderServiceImpl.java
new file mode 100644
index 0000000..2f152cd
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/impl/OrderServiceImpl.java
@@ -0,0 +1,92 @@
+/*
+ * 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.example.shadow.spring.namespace.mybatis.service.impl;
+
+import org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.domain.OrderInfo;
+import org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.repository.OrderMapper;
+import org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.service.OrderService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+@Service(value = "orderService")
+public final class OrderServiceImpl implements OrderService {
+
+ @Resource
+ private OrderMapper orderMapper;
+
+ @Override
+ public void executeInsertCase() {
+ executeOneInsertCase();
+ executeBatchInsertCase();
+ }
+
+ @Override
+ public void executeUpdateCase() {
+ executeOneUpdateCase();
+ executeInUpdateCase();
+ }
+
+ private void executeInUpdateCase() {
+ Map<String, Object> updateMap = new LinkedHashMap<>();
+ List<Integer> userIds = new LinkedList<>();
+ userIds.add(1);
+ userIds.add(2);
+ updateMap.put("userIds",userIds);
+ updateMap.put("content","update_case_2");
+ orderMapper.updateByUserIds(updateMap);
+
+ }
+
+ private void executeOneUpdateCase() {
+ OrderInfo orderInfo = new OrderInfo();
+ orderInfo.setUserId(1);
+ orderInfo.setContent("update_case_1");
+ orderMapper.updateOne(orderInfo);
+ }
+
+
+ private void executeOneInsertCase() {
+ OrderInfo orderInfo = new OrderInfo();
+ orderInfo.setUserId(1);
+ orderInfo.setContent("insert_case_1");
+ orderMapper.saveOne(orderInfo);
+ }
+
+ private void executeBatchInsertCase() {
+ List<OrderInfo> orders = new ArrayList<>();
+ OrderInfo orderInfo1 = new OrderInfo();
+ orderInfo1.setUserId(1);
+ orderInfo1.setContent("insert_case_2");
+ orders.add(orderInfo1);
+ OrderInfo orderInfo2 = new OrderInfo();
+ orderInfo2.setUserId(2);
+ orderInfo2.setContent("insert_case_2");
+ orders.add(orderInfo2);
+ OrderInfo orderInfo3 = new OrderInfo();
+ orderInfo3.setUserId(1);
+ orderInfo3.setContent("insert_case_2");
+ orders.add(orderInfo3);
+ orderMapper.saveBatch(orders);
+ }
+}
diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/impl/ShadowServiceImpl.java b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/impl/ShadowService [...]
index 8f18170..58b05c6 100644
--- a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/impl/ShadowServiceImpl.java
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/java/org/apache/shardingsphere/example/shadow/spring/namespace/mybatis/service/impl/ShadowServiceImpl.java
@@ -47,7 +47,7 @@ public final class ShadowServiceImpl implements ShadowService {
Collection<String> result = new LinkedList<>();
String insert_case_1 = "INSERT INTO t_order (user_id, content) VALUES (1, 'insert_case_1')";
result.add(insert_case_1);
- String insert_case_2 = "INSERT INTO t_order (user_id, content) VALUES (1, 'insert_case_2'), (1, 'insert_case_2')";
+ String insert_case_2 = "INSERT INTO t_order (user_id, content) VALUES (1, 'insert_case_2'), (1, 'insert_case_2'), (1, 'insert_case_2'), (1, 'insert_case_2')";
result.add(insert_case_2);
String insert_case_3 = "INSERT INTO t_order (user_id, content) VALUES (1, 'insert_case_3'), (2, 'insert_case_3')";
result.add(insert_case_3);
@@ -55,4 +55,31 @@ public final class ShadowServiceImpl implements ShadowService {
result.add(insert_case_4);
return result;
}
+
+ @Override
+ public void executeUpdateCase() {
+ Collection<String> updateSQLs = initUpdateCase();
+ for (String each : updateSQLs) {
+ execute(each);
+ }
+ }
+
+ private Collection<String> initUpdateCase() {
+ Collection<String> result = new LinkedList<>();
+ String update_case_1 = "UPDATE t_order SET user_id = 2, content = 'update_case_1' WHERE user_id = 1 and content = 'update_case_1'";
+ result.add(update_case_1);
+ String update_case_2 = "UPDATE t_order SET user_id = 2, content = 'update_case_1' WHERE user_id = 2 and content = 'update_case_1'";
+ result.add(update_case_2);
+ String update_case_3 = "UPDATE t_order SET user_id = 2, content = 'update_case_1' WHERE user_id = 1 or content = 'aa'";
+ result.add(update_case_3);
+ String update_case_4 = "UPDATE t_order SET user_id = 2, content = 'update_case_1' WHERE user_id = 2 or content = 'aa'";
+ result.add(update_case_4);
+ String update_case_5 = "UPDATE t_order SET user_id = 2, content = 'update_case_1' WHERE user_id in (1, 2, 3) or content BETWEEN 'aaa' AND 'bbb'";
+ result.add(update_case_5);
+ String update_case_6 = "UPDATE t_order SET user_id = 2, content = 'update_case_1' WHERE user_id = 2 or content = 'aa'";
+ result.add(update_case_6);
+ String update_case_7 = "UPDATE t_order SET user_id = 2, content = 'update_case_1' WHERE user_id BETWEEN 0 AND 2 or content = 'aa'";
+ result.add(update_case_7);
+ return result;
+ }
}
diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/application-shadow-databases.xml b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/application-shadow-databases.xml
index ed19f73..82d149f 100644
--- a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/application-shadow-databases.xml
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/application-shadow-databases.xml
@@ -49,7 +49,7 @@
<shadow:shadow-algorithm id="user-id-match-algorithm" type="COLUMN_REGEX_MATCH">
<props>
- <prop key="operation">insert</prop>
+ <prop key="operation">update</prop>
<prop key="column">user_id</prop>
<prop key="regex">[1]</prop>
</props>
@@ -65,7 +65,6 @@
<shadow:data-source id="shadow-data-source" source-data-source-name="ds" shadow-data-source-name="ds_shadow"/>
<shadow:shadow-table name="t_order">
<shadow:algorithm shadow-algorithm-ref= "user-id-match-algorithm" />
- <shadow:algorithm shadow-algorithm-ref= "simple-note-algorithm" />
</shadow:shadow-table>
<shadow:shadow-table name="t_user">
<shadow:algorithm shadow-algorithm-ref= "simple-note-algorithm" />
diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/ShadowMapper.xml b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/OrderMapper.xml
similarity index 55%
copy from examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/ShadowMapper.xml
copy to examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/OrderMapper.xml
index fad6fdd..37db9af 100644
--- a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/ShadowMapper.xml
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/OrderMapper.xml
@@ -17,14 +17,40 @@
-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.repository.ShadowMapper">
+<mapper namespace="org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.repository.OrderMapper">
<resultMap id="ShadowInfoMap" type="org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.domain.OrderInfo">
<result column="id" property="id" jdbcType="INTEGER"/>
<result column="user_id" property="userId" jdbcType="INTEGER"/>
<result column="content" property="content" jdbcType="VARCHAR"/>
</resultMap>
- <insert id="execute" parameterType="java.lang.String">
- ${sql}
+ <insert id="saveOne" parameterType="org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.domain.OrderInfo">
+ insert into t_order(user_id, content) values (#{userId}, #{content})
</insert>
+
+ <insert id="saveBatch" parameterType="java.util.List">
+ insert into t_order(user_id, content) values
+ <foreach collection="list" item="order" separator=",">
+ (#{order.userId}, #{order.content})
+ </foreach>
+ </insert>
+
+ <update id="updateOne" parameterType="org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.domain.OrderInfo">
+ update t_order
+ <set>
+ <if test="content != null">content=#{content}</if>
+ </set>
+ where user_id = #{userId}
+ </update>
+
+ <update id="updateByUserIds" parameterType="java.util.Map">
+ update t_order
+ <set>
+ <if test="content != null">content=#{content}</if>
+ </set>
+ where user_id in
+ <foreach collection="userIds" item="userId" separator="," open="(" close=")">
+ #{userId}
+ </foreach>
+ </update>
</mapper>
diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/ShadowMapper.xml b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/ShadowMapper.xml
index fad6fdd..3d9580b 100644
--- a/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/ShadowMapper.xml
+++ b/examples/shardingsphere-jdbc-example/other-feature-example/future-shadow-example/future-shadow-spring-namespace-mybatis-example/src/main/resources/META-INF/mappers/ShadowMapper.xml
@@ -18,12 +18,6 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.repository.ShadowMapper">
- <resultMap id="ShadowInfoMap" type="org.apache.shardingsphere.example.shadow.spring.namespace.mybatis.domain.OrderInfo">
- <result column="id" property="id" jdbcType="INTEGER"/>
- <result column="user_id" property="userId" jdbcType="INTEGER"/>
- <result column="content" property="content" jdbcType="VARCHAR"/>
- </resultMap>
-
<insert id="execute" parameterType="java.lang.String">
${sql}
</insert>
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
index 0a00982..27d56a3 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
@@ -27,8 +27,6 @@ import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.route.future.engine.ShadowRouteEngineFactory;
-import org.apache.shardingsphere.shadow.route.future.engine.validator.ShadowStatementValidator;
-import org.apache.shardingsphere.shadow.route.future.engine.validator.ShadowStatementValidatorFactory;
import org.apache.shardingsphere.shadow.route.judge.ShadowDataSourceJudgeEngine;
import org.apache.shardingsphere.shadow.route.judge.impl.PreparedShadowDataSourceJudgeEngine;
import org.apache.shardingsphere.shadow.route.judge.impl.SimpleShadowDataSourceJudgeEngine;
@@ -40,7 +38,6 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import java.util.Optional;
/**
* Shadow SQL router.
@@ -88,7 +85,7 @@ public final class ShadowSQLRouter implements SQLRouter<ShadowRule> {
public void decorateRouteContext(final RouteContext routeContext,
final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ShadowRule rule, final ConfigurationProperties props) {
if (rule.isEnable()) {
- doShadowDecorateFuture(routeContext, logicSQL, metaData, rule, props);
+ doShadowDecorateFuture(routeContext, logicSQL, rule);
} else {
doShadowDecorate(routeContext, logicSQL, rule);
}
@@ -116,14 +113,8 @@ public final class ShadowSQLRouter implements SQLRouter<ShadowRule> {
routeContext.getRouteUnits().addAll(toBeAdded);
}
- @SuppressWarnings({"rawtypes", "unchecked"})
- private void doShadowDecorateFuture(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ShadowRule rule, final ConfigurationProperties props) {
- SQLStatementContext<?> sqlStatementContext = logicSQL.getSqlStatementContext();
- Optional<ShadowStatementValidator> shadowStatementValidator = ShadowStatementValidatorFactory.newInstance(sqlStatementContext.getSqlStatement());
- if (shadowStatementValidator.isPresent() && !shadowStatementValidator.get().preValidate(sqlStatementContext)) {
- return;
- }
- ShadowRouteEngineFactory.newInstance(logicSQL).route(routeContext, logicSQL, metaData, rule, props);
+ private void doShadowDecorateFuture(final RouteContext routeContext, final LogicSQL logicSQL, final ShadowRule rule) {
+ ShadowRouteEngineFactory.newInstance(logicSQL).route(routeContext, rule);
}
@Override
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/AbstractShadowRouteEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/AbstractShadowRouteEngine.java
index 82ed8bd..1a335e1 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/AbstractShadowRouteEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/AbstractShadowRouteEngine.java
@@ -20,45 +20,118 @@ package org.apache.shardingsphere.shadow.route.future.engine;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowTableDeterminer;
+import org.apache.shardingsphere.shadow.api.shadow.column.ColumnShadowAlgorithm;
+import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDetermineCondition;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDeterminerFactory;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import java.util.Collection;
-import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
-import java.util.Objects;
import java.util.Optional;
+import java.util.stream.Collectors;
/**
* Abstract shadow route engine.
*/
public abstract class AbstractShadowRouteEngine implements ShadowRouteEngine {
- private final Map<String, ShadowTableDeterminer> shadowTableDeterminers = new LinkedHashMap<>();
+ @Override
+ public void route(final RouteContext routeContext, final ShadowRule shadowRule) {
+ if (isShadow(shadowRule)) {
+ shadowDMLStatementRouteDecorate(routeContext, shadowRule);
+ }
+ }
+
+ private boolean isShadow(final ShadowRule shadowRule) {
+ Collection<String> relatedShadowTables = getRelatedShadowTables(getAllTables(), shadowRule);
+ for (String each : relatedShadowTables) {
+ Optional<Collection<ShadowAlgorithm>> relatedShadowAlgorithms = shadowRule.getRelatedShadowAlgorithms(each);
+ if (relatedShadowAlgorithms.isPresent() && isShadowTable(relatedShadowAlgorithms.get(), shadowRule, each)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private Collection<String> getRelatedShadowTables(final Collection<SimpleTableSegment> simpleTableSegments, final ShadowRule shadowRule) {
+ return shadowRule.getRelatedShadowTables(simpleTableSegments.stream().map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toCollection(LinkedList::new)));
+ }
+
+ private boolean isShadowTable(final Collection<ShadowAlgorithm> shadowAlgorithms, final ShadowRule shadowRule, final String tableName) {
+ ShadowDetermineCondition shadowDetermineCondition = createShadowDetermineCondition();
+ for (ShadowAlgorithm each : shadowAlgorithms) {
+ if (isShadowAlgorithm(shadowDetermineCondition, each, shadowRule, tableName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean isShadowAlgorithm(final ShadowDetermineCondition shadowDetermineCondition, final ShadowAlgorithm shadowAlgorithm, final ShadowRule shadowRule, final String tableName) {
+ if (shadowAlgorithm instanceof NoteShadowAlgorithm) {
+ return isShadowNoteAlgorithm(shadowDetermineCondition, shadowAlgorithm, shadowRule, tableName);
+ } else if (shadowAlgorithm instanceof ColumnShadowAlgorithm) {
+ return isShadowColumnAlgorithm(shadowDetermineCondition, shadowAlgorithm, shadowRule, tableName);
+ } else {
+ return false;
+ }
+ }
+
+ private boolean isShadowColumnAlgorithm(final ShadowDetermineCondition shadowDetermineCondition, final ShadowAlgorithm shadowAlgorithm, final ShadowRule shadowRule, final String tableName) {
+ if (!shadowDetermineCondition.isColumnValuesMappingsInitialized()) {
+ Optional<Map<String, Collection<Comparable<?>>>> columnValuesMappings = parseColumnValuesMappings();
+ if (!columnValuesMappings.isPresent()) {
+ return false;
+ }
+ shadowDetermineCondition.initColumnValuesMappings(columnValuesMappings.get());
+ }
+ return ShadowDeterminerFactory.newInstance(shadowAlgorithm).isShadow(shadowDetermineCondition, shadowRule, tableName);
+ }
- protected Map<String, ShadowTableDeterminer> getShadowTableDeterminers() {
- return shadowTableDeterminers;
+ private boolean isShadowNoteAlgorithm(final ShadowDetermineCondition shadowDetermineCondition, final ShadowAlgorithm shadowAlgorithm, final ShadowRule shadowRule, final String tableName) {
+ if (!shadowDetermineCondition.isSqlNotesInitialized()) {
+ Optional<Collection<String>> sqlNotes = parseSqlNotes();
+ if (!sqlNotes.isPresent()) {
+ return false;
+ }
+ shadowDetermineCondition.initSqlNotes(sqlNotes.get());
+ }
+ return ShadowDeterminerFactory.newInstance(shadowAlgorithm).isShadow(shadowDetermineCondition, shadowRule, tableName);
}
/**
- * Get shadow table determiner by table name.
+ * Parse column values mappings.
*
- * @param tableName table name
- * @return shadow table determiner
+ * @return column values mappings
*/
- protected Optional<ShadowTableDeterminer> getShadowTableDeterminer(final String tableName) {
- ShadowTableDeterminer shadowTableDeterminer = shadowTableDeterminers.get(tableName);
- return Objects.isNull(shadowTableDeterminer) ? Optional.empty() : Optional.of(shadowTableDeterminer);
- }
+ protected abstract Optional<Map<String, Collection<Comparable<?>>>> parseColumnValuesMappings();
+
+ /**
+ * Parse sql notes.
+ *
+ * @return sql notes
+ */
+ protected abstract Optional<Collection<String>> parseSqlNotes();
/**
- * Do shadow decorate in DML statement.
+ * Create shadow determine condition.
*
- * @param routeContext route context
- * @param shadowRule shadow rule
+ * @return new instance of shadow determine condition
*/
- protected void shadowDMLStatementRouteDecorate(final RouteContext routeContext, final ShadowRule shadowRule) {
+ protected abstract ShadowDetermineCondition createShadowDetermineCondition();
+
+ /**
+ * Get all tables.
+ *
+ * @return all tables
+ */
+ protected abstract Collection<SimpleTableSegment> getAllTables();
+
+ private void shadowDMLStatementRouteDecorate(final RouteContext routeContext, final ShadowRule shadowRule) {
Collection<RouteUnit> toBeAdded = new LinkedList<>();
routeContext.getRouteUnits().forEach(each -> toBeAdded.add(createActualShadowRouteUnit(each, shadowRule)));
routeContext.getRouteUnits().clear();
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngine.java
index 4a7e916..91e1348 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngine.java
@@ -17,9 +17,6 @@
package org.apache.shardingsphere.shadow.route.future.engine;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
@@ -32,10 +29,7 @@ public interface ShadowRouteEngine {
* Route.
*
* @param routeContext route context
- * @param logicSQL logic SQL
- * @param metaData shardingSphere meta data
* @param shadowRule shadow rule
- * @param props configuration properties
*/
- void route(RouteContext routeContext, LogicSQL logicSQL, ShardingSphereMetaData metaData, ShadowRule shadowRule, ConfigurationProperties props);
+ void route(RouteContext routeContext, ShadowRule shadowRule);
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactory.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactory.java
index cccd254..11d5ecb 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactory.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactory.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.shadow.route.future.engine;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.shadow.route.future.engine.dml.ShadowDeleteStatementRoutingEngine;
import org.apache.shardingsphere.shadow.route.future.engine.dml.ShadowInsertStatementRoutingEngine;
import org.apache.shardingsphere.shadow.route.future.engine.dml.ShadowSelectStatementRoutingEngine;
@@ -46,11 +48,11 @@ public final class ShadowRouteEngineFactory {
public static ShadowRouteEngine newInstance(final LogicSQL logicSQL) {
SQLStatement sqlStatement = logicSQL.getSqlStatementContext().getSqlStatement();
if (sqlStatement instanceof InsertStatement) {
- return createShadowInsertStatementRoutingEngine();
+ return createShadowInsertStatementRoutingEngine(logicSQL);
} else if (sqlStatement instanceof DeleteStatement) {
return createShadowDeleteStatementRoutingEngine();
} else if (sqlStatement instanceof UpdateStatement) {
- return createShadowUpdateStatementRoutingEngine();
+ return createShadowUpdateStatementRoutingEngine(logicSQL);
} else if (sqlStatement instanceof SelectStatement) {
return createShadowSelectStatementRoutingEngine();
} else {
@@ -66,15 +68,15 @@ public final class ShadowRouteEngineFactory {
return new ShadowSelectStatementRoutingEngine();
}
- private static ShadowRouteEngine createShadowUpdateStatementRoutingEngine() {
- return new ShadowUpdateStatementRoutingEngine();
+ private static ShadowRouteEngine createShadowUpdateStatementRoutingEngine(final LogicSQL logicSQL) {
+ return new ShadowUpdateStatementRoutingEngine((UpdateStatementContext) logicSQL.getSqlStatementContext(), logicSQL.getParameters());
}
private static ShadowRouteEngine createShadowDeleteStatementRoutingEngine() {
return new ShadowDeleteStatementRoutingEngine();
}
- private static ShadowRouteEngine createShadowInsertStatementRoutingEngine() {
- return new ShadowInsertStatementRoutingEngine();
+ private static ShadowRouteEngine createShadowInsertStatementRoutingEngine(final LogicSQL logicSQL) {
+ return new ShadowInsertStatementRoutingEngine((InsertStatementContext) logicSQL.getSqlStatementContext());
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowAlgorithmDeterminer.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowAlgorithmDeterminer.java
index 686a2aa..de9d13e 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowAlgorithmDeterminer.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowAlgorithmDeterminer.java
@@ -17,9 +17,7 @@
package org.apache.shardingsphere.shadow.route.future.engine.determiner;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-
-import java.util.Collection;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
/**
* Shadow algorithm determiner.
@@ -29,10 +27,10 @@ public interface ShadowAlgorithmDeterminer {
/**
* Is shadow in shadow algorithm.
*
- * @param insertStatementContext insert statement context
- * @param relatedShadowTables related shadow tables
+ * @param shadowDetermineCondition shadow determine condition
+ * @param shadowRule shadow rule
* @param tableName table name
- * @return is pass or not
+ * @return is shadow or not
*/
- boolean isShadow(InsertStatementContext insertStatementContext, Collection<String> relatedShadowTables, String tableName);
+ boolean isShadow(ShadowDetermineCondition shadowDetermineCondition, ShadowRule shadowRule, String tableName);
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDetermineCondition.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDetermineCondition.java
new file mode 100644
index 0000000..8eeee24
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDetermineCondition.java
@@ -0,0 +1,119 @@
+/*
+ * 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.route.future.engine.determiner;
+
+import org.apache.shardingsphere.shadow.api.shadow.column.ShadowOperationType;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Shadow determine condition.
+ */
+public final class ShadowDetermineCondition {
+
+ private boolean sqlNotesInitialized;
+
+ private boolean columnValuesMappingsInitialized;
+
+ private final ShadowOperationType shadowOperationType;
+
+ private final Map<String, Collection<Comparable<?>>> columnValuesMappings = new LinkedHashMap<>();
+
+ private final Collection<String> sqlNotes = new LinkedList<>();
+
+ public ShadowDetermineCondition(final ShadowOperationType shadowOperationType) {
+ this.shadowOperationType = shadowOperationType;
+ sqlNotesInitialized = false;
+ columnValuesMappingsInitialized = false;
+ }
+
+ /**
+ * Initialize SQL notes.
+ *
+ * @param notes notes
+ */
+ public void initSqlNotes(final Collection<String> notes) {
+ if (sqlNotesInitialized) {
+ return;
+ }
+ sqlNotes.addAll(notes);
+ sqlNotesInitialized = true;
+ }
+
+ /**
+ * Is SQL notes initialized.
+ *
+ * @return is initialized or not
+ */
+ public boolean isSqlNotesInitialized() {
+ return sqlNotesInitialized;
+ }
+
+ /**
+ * Get SQL notes.
+ *
+ * @return SQL notes
+ */
+ public Optional<Collection<String>> getSqlNotes() {
+ return sqlNotes.isEmpty() ? Optional.empty() : Optional.of(sqlNotes);
+ }
+
+ /**
+ * Initialize column values mappings.
+ *
+ * @param columnValuesMappings column values mappings
+ */
+ public void initColumnValuesMappings(final Map<String, Collection<Comparable<?>>> columnValuesMappings) {
+ if (columnValuesMappingsInitialized) {
+ return;
+ }
+ this.columnValuesMappings.putAll(columnValuesMappings);
+ columnValuesMappingsInitialized = true;
+ }
+
+ /**
+ * Is column values mappings initialized.
+ *
+ * @return is initialized or not
+ */
+ public boolean isColumnValuesMappingsInitialized() {
+ return columnValuesMappingsInitialized;
+ }
+
+ /**
+ * Get column values mappings.
+ *
+ * @return column values mappings
+ */
+ public Optional<Map<String, Collection<Comparable<?>>>> getColumnValuesMappings() {
+ return columnValuesMappings.isEmpty() ? Optional.empty() : Optional.of(columnValuesMappings);
+ }
+
+ /**
+ * Get shadow operation type.
+ *
+ * @return shadow operation type
+ */
+ public ShadowOperationType getShadowOperationType() {
+ return shadowOperationType;
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactory.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactory.java
index 33e077b..03d2dcd 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactory.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactory.java
@@ -22,43 +22,21 @@ import org.apache.shardingsphere.shadow.api.shadow.column.ColumnShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
import org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm.ColumnShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm.NoteShadowAlgorithmDeterminer;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.table.AnyAlgorithmApplicableShadowTableDeterminer;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.Optional;
-import java.util.stream.Collectors;
-
/**
* Shadow determiner factory.
*/
public final class ShadowDeterminerFactory {
/**
- * Create new instance of shadow table determiner.
- *
- * @param tableName table name
- * @param shadowRule shadow rule
- * @return new instance of shadow table determiner
- */
- public static Optional<ShadowTableDeterminer> getShadowTableDeterminer(final String tableName, final ShadowRule shadowRule) {
- return shadowRule.getRelatedShadowAlgorithms(tableName).map(shadowAlgorithms -> new AnyAlgorithmApplicableShadowTableDeterminer(createShadowAlgorithmDeterminers(shadowAlgorithms)));
- }
-
- private static Collection<ShadowAlgorithmDeterminer> createShadowAlgorithmDeterminers(final Collection<ShadowAlgorithm> shadowAlgorithms) {
- return shadowAlgorithms.stream().map(ShadowDeterminerFactory::getShadowAlgorithmDeterminer).collect(Collectors.toCollection(LinkedList::new));
- }
-
- /**
* Create new instance of Shadow algorithm determiner.
*
* @param shadowAlgorithm shadow algorithm.
* @return new instance of Shadow algorithm determiner
*/
@SuppressWarnings(value = "unchecked")
- public static ShadowAlgorithmDeterminer getShadowAlgorithmDeterminer(final ShadowAlgorithm shadowAlgorithm) {
+ public static ShadowAlgorithmDeterminer newInstance(final ShadowAlgorithm shadowAlgorithm) {
if (shadowAlgorithm instanceof NoteShadowAlgorithm) {
return new NoteShadowAlgorithmDeterminer((NoteShadowAlgorithm<Comparable<?>>) shadowAlgorithm);
} else if (shadowAlgorithm instanceof ColumnShadowAlgorithm) {
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminer.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminer.java
index 7cdcd41..ed5a2aa 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminer.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminer.java
@@ -18,17 +18,16 @@
package org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.shadow.api.shadow.column.ColumnShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.shadow.column.PreciseColumnShadowValue;
import org.apache.shardingsphere.shadow.api.shadow.column.ShadowOperationType;
import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowAlgorithmDeterminer;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDetermineCondition;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Collection;
-import java.util.Iterator;
import java.util.LinkedList;
-import java.util.List;
+import java.util.Map;
import java.util.Optional;
/**
@@ -40,25 +39,19 @@ public final class ColumnShadowAlgorithmDeterminer implements ShadowAlgorithmDet
private final ColumnShadowAlgorithm<Comparable<?>> columnShadowAlgorithm;
@Override
- public boolean isShadow(final InsertStatementContext insertStatementContext, final Collection<String> relatedShadowTables, final String tableName) {
- Collection<String> columnNames = insertStatementContext.getInsertColumnNames();
- Iterator<String> columnNamesIt = columnNames.iterator();
- List<InsertValueContext> insertValueContexts = insertStatementContext.getInsertValueContexts();
- int index = 0;
- while (columnNamesIt.hasNext()) {
- Optional<Collection<Comparable<?>>> columnValues = getColumnValues(insertValueContexts, index);
- if (columnValues.isPresent()) {
- if (isShadowColumn(createPreciseColumnShadowValues(columnNamesIt.next(), columnValues.get(), tableName, ShadowOperationType.INSERT), columnShadowAlgorithm, relatedShadowTables)) {
+ public boolean isShadow(final ShadowDetermineCondition shadowDetermineCondition, final ShadowRule shadowRule, final String tableName) {
+ Optional<Map<String, Collection<Comparable<?>>>> columnValuesMappings = shadowDetermineCondition.getColumnValuesMappings();
+ if (columnValuesMappings.isPresent()) {
+ for (Map.Entry<String, Collection<Comparable<?>>> entry : columnValuesMappings.get().entrySet()) {
+ if (isShadowColumn(createColumnShadowValues(entry.getKey(), entry.getValue(), tableName, shadowDetermineCondition.getShadowOperationType()), shadowRule.getAllShadowTableNames())) {
return true;
}
}
- index++;
}
return false;
}
- private boolean isShadowColumn(final Collection<PreciseColumnShadowValue<Comparable<?>>> preciseColumnShadowValues, final ColumnShadowAlgorithm<Comparable<?>> columnShadowAlgorithm,
- final Collection<String> relatedShadowTables) {
+ private boolean isShadowColumn(final Collection<PreciseColumnShadowValue<Comparable<?>>> preciseColumnShadowValues, final Collection<String> relatedShadowTables) {
for (PreciseColumnShadowValue<Comparable<?>> each : preciseColumnShadowValues) {
if (!columnShadowAlgorithm.isShadow(relatedShadowTables, each)) {
return false;
@@ -67,25 +60,12 @@ public final class ColumnShadowAlgorithmDeterminer implements ShadowAlgorithmDet
return true;
}
- private Collection<PreciseColumnShadowValue<Comparable<?>>> createPreciseColumnShadowValues(final String columnName, final Collection<Comparable<?>> columnValues, final String tableName,
- final ShadowOperationType operationType) {
+ private Collection<PreciseColumnShadowValue<Comparable<?>>> createColumnShadowValues(final String columnName, final Collection<Comparable<?>> columnValues, final String tableName,
+ final ShadowOperationType operationType) {
Collection<PreciseColumnShadowValue<Comparable<?>>> result = new LinkedList<>();
for (Comparable<?> each : columnValues) {
result.add(new PreciseColumnShadowValue<>(tableName, operationType, columnName, each));
}
return result;
}
-
- private Optional<Collection<Comparable<?>>> getColumnValues(final List<InsertValueContext> insertValueContexts, final int index) {
- Collection<Comparable<?>> result = new LinkedList<>();
- for (InsertValueContext each : insertValueContexts) {
- Object valueObject = each.getValue(index);
- if (valueObject instanceof Comparable<?>) {
- result.add((Comparable<?>) valueObject);
- } else {
- return Optional.empty();
- }
- }
- return Optional.of(result);
- }
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminer.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminer.java
index 4a3c827..863e667 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminer.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminer.java
@@ -18,10 +18,11 @@
package org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.shadow.note.PreciseNoteShadowValue;
import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowAlgorithmDeterminer;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDetermineCondition;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Collection;
import java.util.LinkedList;
@@ -35,21 +36,19 @@ public final class NoteShadowAlgorithmDeterminer implements ShadowAlgorithmDeter
private final NoteShadowAlgorithm<Comparable<?>> noteShadowAlgorithm;
@Override
- public boolean isShadow(final InsertStatementContext insertStatementContext, final Collection<String> relatedShadowTables, final String tableName) {
- Collection<PreciseNoteShadowValue<Comparable<?>>> noteShadowValues = createNoteShadowValues(insertStatementContext, tableName);
- for (PreciseNoteShadowValue<Comparable<?>> noteShadowValue : noteShadowValues) {
- if (noteShadowAlgorithm.isShadow(relatedShadowTables, noteShadowValue)) {
+ public boolean isShadow(final ShadowDetermineCondition shadowDetermineCondition, final ShadowRule shadowRule, final String tableName) {
+ Collection<PreciseNoteShadowValue<Comparable<?>>> noteShadowValues = createNoteShadowValues(shadowDetermineCondition, tableName);
+ for (PreciseNoteShadowValue<Comparable<?>> each : noteShadowValues) {
+ if (noteShadowAlgorithm.isShadow(shadowRule.getAllShadowTableNames(), each)) {
return true;
}
}
return false;
}
- // FIXME refactor the method when sql parses the note and puts it in the statement context
- private Collection<PreciseNoteShadowValue<Comparable<?>>> createNoteShadowValues(final InsertStatementContext insertStatementContext, final String logicTableName) {
+ private Collection<PreciseNoteShadowValue<Comparable<?>>> createNoteShadowValues(final ShadowDetermineCondition shadowDetermineCondition, final String tableName) {
Collection<PreciseNoteShadowValue<Comparable<?>>> result = new LinkedList<>();
- result.add(new PreciseNoteShadowValue<>(logicTableName, "/*table=t_user,shadow=true*/"));
- result.add(new PreciseNoteShadowValue<>(logicTableName, "/*aaa=bbb*/"));
+ shadowDetermineCondition.getSqlNotes().ifPresent(notes -> notes.forEach(each -> result.add(new PreciseNoteShadowValue<>(tableName, each))));
return result;
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/table/AnyAlgorithmApplicableShadowTableDeterminer.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/table/AnyAlgorithmApplicableShadowTableDeterminer.java
deleted file mode 100644
index b110fc1..0000000
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/table/AnyAlgorithmApplicableShadowTableDeterminer.java
+++ /dev/null
@@ -1,46 +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.shadow.route.future.engine.determiner.table;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowAlgorithmDeterminer;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowTableDeterminer;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-
-import java.util.Collection;
-
-/**
- * any algorithm applicable shadow table determiner.
- */
-@RequiredArgsConstructor
-public final class AnyAlgorithmApplicableShadowTableDeterminer implements ShadowTableDeterminer {
-
- private final Collection<ShadowAlgorithmDeterminer> shadowAlgorithmDeterminers;
-
- @Override
- public boolean isShadow(final InsertStatementContext insertStatementContext, final ShadowRule shadowRule, final String tableName) {
- Collection<String> shadowTableNames = shadowRule.getAllShadowTableNames();
- for (ShadowAlgorithmDeterminer each : shadowAlgorithmDeterminers) {
- if (each.isShadow(insertStatementContext, shadowTableNames, tableName)) {
- return true;
- }
- }
- return false;
- }
-}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowDeleteStatementRoutingEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowDeleteStatementRoutingEngine.java
index 461d382..496ffec 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowDeleteStatementRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowDeleteStatementRoutingEngine.java
@@ -17,9 +17,6 @@
package org.apache.shardingsphere.shadow.route.future.engine.dml;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.shadow.route.future.engine.ShadowRouteEngine;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
@@ -30,7 +27,7 @@ import org.apache.shardingsphere.shadow.rule.ShadowRule;
public final class ShadowDeleteStatementRoutingEngine implements ShadowRouteEngine {
@Override
- public void route(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ShadowRule shadowRule, final ConfigurationProperties props) {
+ public void route(final RouteContext routeContext, final ShadowRule shadowRule) {
// TODO decorate route in delete statement case
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngine.java
index ed52539..072a93c 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngine.java
@@ -17,57 +17,79 @@
package org.apache.shardingsphere.shadow.route.future.engine.dml;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.shadow.api.shadow.column.ShadowOperationType;
import org.apache.shardingsphere.shadow.route.future.engine.AbstractShadowRouteEngine;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDeterminerFactory;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowTableDeterminer;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDetermineCondition;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
import java.util.Optional;
-import java.util.stream.Collectors;
/**
* Shadow insert statement routing engine.
*/
+@RequiredArgsConstructor
public final class ShadowInsertStatementRoutingEngine extends AbstractShadowRouteEngine {
+ private final InsertStatementContext insertStatementContext;
+
@Override
- public void route(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ShadowRule shadowRule, final ConfigurationProperties props) {
- if (isShadow(logicSQL, shadowRule)) {
- shadowDMLStatementRouteDecorate(routeContext, shadowRule);
+ protected Optional<Map<String, Collection<Comparable<?>>>> parseColumnValuesMappings() {
+ Map<String, Collection<Comparable<?>>> result = new LinkedHashMap<>();
+ Collection<String> columnNames = parseColumnNames();
+ Iterator<String> columnNamesIt = columnNames.iterator();
+ List<InsertValueContext> insertValueContexts = insertStatementContext.getInsertValueContexts();
+ int index = 0;
+ while (columnNamesIt.hasNext()) {
+ String columnName = columnNamesIt.next();
+ Optional<Collection<Comparable<?>>> columnValues = getColumnValues(insertValueContexts, index);
+ columnValues.ifPresent(values -> result.put(columnName, values));
+ index++;
}
+ return result.isEmpty() ? Optional.empty() : Optional.of(result);
}
- private boolean isShadow(final LogicSQL logicSQL, final ShadowRule shadowRule) {
- InsertStatementContext insertStatementContext = (InsertStatementContext) logicSQL.getSqlStatementContext();
- Collection<String> relatedShadowTables = getRelatedShadowTables(insertStatementContext, shadowRule);
- initShadowTableDeterminer(relatedShadowTables, shadowRule);
- for (String each : relatedShadowTables) {
- Optional<ShadowTableDeterminer> shadowTableDeterminer = getShadowTableDeterminer(each);
- if (shadowTableDeterminer.isPresent()) {
- if (shadowTableDeterminer.get().isShadow(insertStatementContext, shadowRule, each)) {
- return true;
- }
+ private Optional<Collection<Comparable<?>>> getColumnValues(final List<InsertValueContext> insertValueContexts, final int index) {
+ Collection<Comparable<?>> result = new LinkedList<>();
+ for (InsertValueContext each : insertValueContexts) {
+ Object valueObject = each.getValue(index);
+ if (valueObject instanceof Comparable<?>) {
+ result.add((Comparable<?>) valueObject);
+ } else {
+ return Optional.empty();
}
}
- return false;
+ return result.isEmpty() ? Optional.empty() : Optional.of(result);
}
- private Collection<String> getRelatedShadowTables(final InsertStatementContext insertStatementContext, final ShadowRule shadowRule) {
- return shadowRule.getRelatedShadowTables(insertStatementContext.getAllTables().stream().map(each -> each.getTableName().getIdentifier().getValue())
- .collect(Collectors.toCollection(LinkedList::new)));
+ private Collection<String> parseColumnNames() {
+ return insertStatementContext.getInsertColumnNames();
}
- private void initShadowTableDeterminer(final Collection<String> relatedShadowTables, final ShadowRule shadowRule) {
- for (String each : relatedShadowTables) {
- Optional<ShadowTableDeterminer> shadowTableDeterminer = ShadowDeterminerFactory.getShadowTableDeterminer(each, shadowRule);
- shadowTableDeterminer.ifPresent(tableDeterminer -> getShadowTableDeterminers().put(each, tableDeterminer));
- }
+ @Override
+ protected ShadowDetermineCondition createShadowDetermineCondition() {
+ return new ShadowDetermineCondition(ShadowOperationType.INSERT);
+ }
+
+ @Override
+ protected Collection<SimpleTableSegment> getAllTables() {
+ return insertStatementContext.getAllTables();
+ }
+
+ // FIXME refactor the method when sql parses the note and puts it in the statement context
+ @Override
+ protected Optional<Collection<String>> parseSqlNotes() {
+ Collection<String> result = new LinkedList<>();
+ result.add("/*foo=bar,shadow=true*/");
+ result.add("/*aaa=bbb*/");
+ return Optional.of(result);
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowSelectStatementRoutingEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowSelectStatementRoutingEngine.java
index 6828c68..692f16a 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowSelectStatementRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowSelectStatementRoutingEngine.java
@@ -17,9 +17,6 @@
package org.apache.shardingsphere.shadow.route.future.engine.dml;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.shadow.route.future.engine.ShadowRouteEngine;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
@@ -30,7 +27,7 @@ import org.apache.shardingsphere.shadow.rule.ShadowRule;
public final class ShadowSelectStatementRoutingEngine implements ShadowRouteEngine {
@Override
- public void route(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ShadowRule shadowRule, final ConfigurationProperties props) {
+ public void route(final RouteContext routeContext, final ShadowRule shadowRule) {
// TODO decorate route in select statement case
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowUpdateStatementRoutingEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowUpdateStatementRoutingEngine.java
index 15f9561..be3b6d7 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowUpdateStatementRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowUpdateStatementRoutingEngine.java
@@ -17,20 +17,89 @@
package org.apache.shardingsphere.shadow.route.future.engine.dml;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.shadow.route.future.engine.ShadowRouteEngine;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
+import org.apache.shardingsphere.shadow.api.shadow.column.ShadowOperationType;
+import org.apache.shardingsphere.shadow.route.future.engine.AbstractShadowRouteEngine;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDetermineCondition;
+import org.apache.shardingsphere.shadow.route.future.engine.util.ShadowExtractor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUtil;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
/**
* Shadow update statement routing engine.
*/
-public final class ShadowUpdateStatementRoutingEngine implements ShadowRouteEngine {
+@RequiredArgsConstructor
+public final class ShadowUpdateStatementRoutingEngine extends AbstractShadowRouteEngine {
+
+ private final UpdateStatementContext updateStatementContext;
+
+ private final List<Object> parameters;
+
+ @Override
+ protected Optional<Map<String, Collection<Comparable<?>>>> parseColumnValuesMappings() {
+ Map<String, Collection<Comparable<?>>> result = new LinkedHashMap<>();
+ Optional<WhereSegment> where = updateStatementContext.getWhere();
+ where.ifPresent(whereSegment -> parseWhereSegment(whereSegment, result));
+ return result.isEmpty() ? Optional.empty() : Optional.of(result);
+ }
+
+ private void parseWhereSegment(final WhereSegment whereSegment, final Map<String, Collection<Comparable<?>>> columnValuesMappings) {
+ ExpressionExtractUtil.getAndPredicates(whereSegment.getExpr()).forEach(each -> parseAndPredicate(each.getPredicates(), columnValuesMappings));
+ }
+
+ private void parseAndPredicate(final Collection<ExpressionSegment> predicates, final Map<String, Collection<Comparable<?>>> columnValuesMappings) {
+ for (ExpressionSegment each : predicates) {
+ parseExpressionSegment(each, columnValuesMappings);
+ }
+ }
+
+ private void parseExpressionSegment(final ExpressionSegment expressionSegment, final Map<String, Collection<Comparable<?>>> columnValuesMappings) {
+ if (expressionSegment instanceof BinaryOperationExpression) {
+ parseBinaryOperationExpression((BinaryOperationExpression) expressionSegment, columnValuesMappings);
+ }
+ if (expressionSegment instanceof InExpression) {
+ parseInExpression((InExpression) expressionSegment, columnValuesMappings);
+ }
+ }
+
+ private void parseInExpression(final InExpression expression, final Map<String, Collection<Comparable<?>>> columnValuesMappings) {
+ Optional<String> columnName = ShadowExtractor.extractColumnName(expression);
+ columnName.ifPresent(s -> ShadowExtractor.extractValues(expression.getRight(), parameters).ifPresent(values -> columnValuesMappings.put(s, values)));
+ }
+
+ private void parseBinaryOperationExpression(final BinaryOperationExpression expression, final Map<String, Collection<Comparable<?>>> columnValuesMappings) {
+ Optional<String> columnName = ShadowExtractor.extractColumnName(expression);
+ columnName.ifPresent(s -> ShadowExtractor.extractValues(expression.getRight(), parameters).ifPresent(values -> columnValuesMappings.put(s, values)));
+ }
+
+ @Override
+ protected ShadowDetermineCondition createShadowDetermineCondition() {
+ return new ShadowDetermineCondition(ShadowOperationType.UPDATE);
+ }
+
+ @Override
+ protected Collection<SimpleTableSegment> getAllTables() {
+ return updateStatementContext.getAllTables();
+ }
+ // FIXME refactor the method when sql parses the note and puts it in the statement context
@Override
- public void route(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ShadowRule shadowRule, final ConfigurationProperties props) {
- // TODO decorate route in update statement case
+ protected Optional<Collection<String>> parseSqlNotes() {
+ Collection<String> result = new LinkedList<>();
+ result.add("/*foo=bar,shadow=true*/");
+ result.add("/*aaa=bbb*/");
+ return Optional.of(result);
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/impl/ShadowNonMDLStatementRoutingEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/impl/ShadowNonMDLStatementRoutingEngine.java
index 4c8d392..1960726 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/impl/ShadowNonMDLStatementRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/impl/ShadowNonMDLStatementRoutingEngine.java
@@ -17,9 +17,6 @@
package org.apache.shardingsphere.shadow.route.future.engine.impl;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.shadow.route.future.engine.ShadowRouteEngine;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
@@ -30,7 +27,7 @@ import org.apache.shardingsphere.shadow.rule.ShadowRule;
public final class ShadowNonMDLStatementRoutingEngine implements ShadowRouteEngine {
@Override
- public void route(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ShadowRule shadowRule, final ConfigurationProperties props) {
+ public void route(final RouteContext routeContext, final ShadowRule shadowRule) {
// TODO decorate route in non-MDL statement case
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/util/ShadowExtractor.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/util/ShadowExtractor.java
new file mode 100644
index 0000000..612e90d
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/util/ShadowExtractor.java
@@ -0,0 +1,129 @@
+/*
+ * 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.route.future.engine.util;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.SimpleExpressionSegment;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Shadow extractor.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShadowExtractor {
+
+ /**
+ * Get left value binary operation Expression if left value expression is column segment.
+ *
+ * @param expression binary operation expression
+ * @return column name
+ */
+ public static Optional<String> extractColumnName(final BinaryOperationExpression expression) {
+ ExpressionSegment left = expression.getLeft();
+ if (left instanceof ColumnSegment) {
+ return Optional.of(extractColumnName((ColumnSegment) left));
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Get left value in Expression if left value expression is column segment.
+ *
+ * @param expression in expression
+ * @return column name
+ */
+ public static Optional<String> extractColumnName(final InExpression expression) {
+ ExpressionSegment left = expression.getLeft();
+ if (left instanceof ColumnSegment) {
+ return Optional.of(extractColumnName((ColumnSegment) left));
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Get value in column segment.
+ *
+ * @param columnSegment column segment
+ * @return column name
+ */
+ private static String extractColumnName(final ColumnSegment columnSegment) {
+ return columnSegment.getIdentifier().getValue();
+ }
+
+ /**
+ * Get values in expression segment.
+ *
+ * @param expression expression segment
+ * @param parameters parameters
+ * @return values
+ */
+ public static Optional<Collection<Comparable<?>>> extractValues(final ExpressionSegment expression, final List<Object> parameters) {
+ Collection<Comparable<?>> result = new LinkedList<>();
+ if (expression instanceof SimpleExpressionSegment) {
+ Optional<Comparable<?>> value = extractValueInSimpleExpressionSegment(expression, parameters);
+ value.ifPresent(result::add);
+ }
+ if (expression instanceof ListExpression) {
+ Optional<Collection<Comparable<?>>> values = extractValuesInListExpression(expression, parameters);
+ values.ifPresent(result::addAll);
+ }
+ return result.isEmpty() ? Optional.empty() : Optional.of(result);
+ }
+
+ private static Optional<Collection<Comparable<?>>> extractValuesInListExpression(final ExpressionSegment expression, final List<Object> parameters) {
+ Collection<Comparable<?>> result = new LinkedList<>();
+ if (expression instanceof ListExpression) {
+ ((ListExpression) expression).getItems().forEach(each -> extractValueInSimpleExpressionSegment(each, parameters).ifPresent(result::add));
+ }
+ return result.isEmpty() ? Optional.empty() : Optional.of(result);
+ }
+
+ private static Optional<Comparable<?>> extractValueInSimpleExpressionSegment(final ExpressionSegment expression, final List<Object> parameters) {
+ if (expression instanceof LiteralExpressionSegment) {
+ return extractValueInLiteralExpressionSegment((LiteralExpressionSegment) expression);
+ }
+ if (expression instanceof ParameterMarkerExpressionSegment) {
+ return extractValueInLiteralExpressionSegment((ParameterMarkerExpressionSegment) expression, parameters);
+ }
+ return Optional.empty();
+ }
+
+ private static Optional<Comparable<?>> extractValueInLiteralExpressionSegment(final ParameterMarkerExpressionSegment expression, final List<Object> parameters) {
+ return castToComparable(parameters.get(expression.getParameterMarkerIndex()));
+ }
+
+ private static Optional<Comparable<?>> extractValueInLiteralExpressionSegment(final LiteralExpressionSegment expression) {
+ return castToComparable(expression.getLiterals());
+ }
+
+ private static Optional<Comparable<?>> castToComparable(final Object object) {
+ return object instanceof Comparable<?> ? Optional.of((Comparable<?>) object) : Optional.empty();
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/ShadowStatementValidatorFactory.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/ShadowStatementValidatorFactory.java
deleted file mode 100644
index 0d2bffa..0000000
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/ShadowStatementValidatorFactory.java
+++ /dev/null
@@ -1,54 +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.shadow.route.future.engine.validator;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.shadow.route.future.engine.validator.dml.ShadowInsertStatementValidator;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
-
-import java.util.Optional;
-
-/**
- * Shadow statement validator factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ShadowStatementValidatorFactory {
-
- /**
- * New instance of shadow statement validator.
- *
- * @param sqlStatement sql statement
- * @return instance of shadow statement validator
- */
- public static Optional<ShadowStatementValidator> newInstance(final SQLStatement sqlStatement) {
- if (sqlStatement instanceof DMLStatement) {
- return getDMLStatementValidator(sqlStatement);
- }
- return Optional.empty();
- }
-
- private static Optional<ShadowStatementValidator> getDMLStatementValidator(final SQLStatement sqlStatement) {
- if (sqlStatement instanceof InsertStatement) {
- return Optional.of(new ShadowInsertStatementValidator());
- }
- return Optional.empty();
- }
-}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/dml/ShadowInsertStatementValidator.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/dml/ShadowInsertStatementValidator.java
deleted file mode 100644
index 19ddd3e..0000000
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/future/engine/validator/dml/ShadowInsertStatementValidator.java
+++ /dev/null
@@ -1,36 +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.shadow.route.future.engine.validator.dml;
-
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.shadow.route.future.engine.validator.ShadowStatementValidator;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
-
-import java.util.Objects;
-
-/**
- * Shadow insert statement validator.
- */
-public final class ShadowInsertStatementValidator implements ShadowStatementValidator<InsertStatement> {
-
- @Override
- public boolean preValidate(final SQLStatementContext<InsertStatement> sqlStatementContext) {
- return Objects.isNull(((InsertStatementContext) sqlStatementContext).getInsertSelectContext());
- }
-}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java
index 8a73ff8..2a5f7ac 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/ShadowRuleConfigurationYamlSwapper.java
@@ -39,6 +39,7 @@ public final class ShadowRuleConfigurationYamlSwapper implements YamlRuleConfigu
@Override
public YamlShadowRuleConfiguration swapToYamlConfiguration(final ShadowRuleConfiguration dataConfiguration) {
YamlShadowRuleConfiguration result = new YamlShadowRuleConfiguration();
+ result.setEnable(dataConfiguration.isEnable());
parseBasicShadowRule(dataConfiguration, result);
parseDataSources(dataConfiguration, result);
parseShadowTables(dataConfiguration, result);
@@ -88,7 +89,7 @@ public final class ShadowRuleConfigurationYamlSwapper implements YamlRuleConfigu
// fixme remove method when the api refactoring is complete
private ShadowRuleConfiguration createBasicShadowRule(final YamlShadowRuleConfiguration yamlConfiguration) {
- return new ShadowRuleConfiguration(yamlConfiguration.getColumn(), yamlConfiguration.getSourceDataSourceNames(), yamlConfiguration.getShadowDataSourceNames());
+ return new ShadowRuleConfiguration(yamlConfiguration.getColumn(), yamlConfiguration.getSourceDataSourceNames(), yamlConfiguration.getShadowDataSourceNames(), yamlConfiguration.isEnable());
}
@Override
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/AbstractShadowRouteEngineTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/AbstractShadowRouteEngineTest.java
deleted file mode 100644
index 701aa9b..0000000
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/AbstractShadowRouteEngineTest.java
+++ /dev/null
@@ -1,68 +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.shadow.route.future.engine;
-
-import com.google.common.collect.Lists;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.shadow.route.future.engine.dml.ShadowInsertStatementRoutingEngine;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import org.junit.Test;
-
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.Map;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public final class AbstractShadowRouteEngineTest {
-
- @Test
- public void assertDoShadowDecorate() {
- AbstractShadowRouteEngine abstractShadowRouteEngine = new ShadowInsertStatementRoutingEngine();
- abstractShadowRouteEngine.shadowDMLStatementRouteDecorate(createRouteContext(), createShadowRule());
- }
-
- private ShadowRule createShadowRule() {
- ShadowRule shadowRule = mock(ShadowRule.class);
- when(shadowRule.getShadowDataSourceMappings()).thenReturn(createShadowDataSourceMappings());
- return shadowRule;
- }
-
- private Map<String, String> createShadowDataSourceMappings() {
- Map<String, String> result = new LinkedHashMap<>();
- result.put("ds", "ds_shadow");
- result.put("ds1", "ds1_shadow");
- return result;
- }
-
- private RouteContext createRouteContext() {
- RouteContext routeContext = mock(RouteContext.class);
- when(routeContext.getRouteUnits()).thenReturn(createRouteUnits());
- return routeContext;
- }
-
- private Collection<RouteUnit> createRouteUnits() {
- Collection<RouteUnit> result = new LinkedList<>();
- result.add(new RouteUnit(new RouteMapper("logic_ds", "ds"), Lists.newArrayList()));
- return result;
- }
-}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactoryTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactoryTest.java
index 76c3866..7802c51 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactoryTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/ShadowRouteEngineFactoryTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.shadow.route.future.engine;
import com.google.common.collect.Lists;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.shadow.route.future.engine.dml.ShadowInsertStatementRoutingEngine;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.junit.Test;
@@ -33,11 +34,12 @@ public final class ShadowRouteEngineFactoryTest {
@Test
public void assertNewInstance() {
- assertThat(ShadowRouteEngineFactory.newInstance(new LogicSQL(createSqlStatementContext(), "", Lists.newArrayList())) instanceof ShadowInsertStatementRoutingEngine, is(true));
+ ShadowRouteEngine shadowRouteEngine = ShadowRouteEngineFactory.newInstance(new LogicSQL(createSqlStatementContext(), "", Lists.newArrayList()));
+ assertThat(shadowRouteEngine instanceof ShadowInsertStatementRoutingEngine, is(true));
}
private SQLStatementContext<InsertStatement> createSqlStatementContext() {
- SQLStatementContext<InsertStatement> result = mock(SQLStatementContext.class);
+ InsertStatementContext result = mock(InsertStatementContext.class);
when(result.getSqlStatement()).thenReturn(mock(InsertStatement.class));
return result;
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactoryTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactoryTest.java
index 9e51022..d9ede2a 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactoryTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/ShadowDeterminerFactoryTest.java
@@ -17,44 +17,21 @@
package org.apache.shardingsphere.shadow.route.future.engine.determiner;
-import org.apache.shardingsphere.shadow.algorithm.shadow.note.SimpleSQLNoteShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.shadow.column.ColumnShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
import org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm.ColumnShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm.NoteShadowAlgorithmDeterminer;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.table.AnyAlgorithmApplicableShadowTableDeterminer;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.junit.Test;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.Optional;
-
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class ShadowDeterminerFactoryTest {
@Test
public void assertSuccessNewInstance() {
- Optional<ShadowTableDeterminer> shadowTableDeterminer = ShadowDeterminerFactory.getShadowTableDeterminer("t_user", createShadowRule());
- shadowTableDeterminer.ifPresent(tableDeterminer -> assertThat(tableDeterminer instanceof AnyAlgorithmApplicableShadowTableDeterminer, is(true)));
- assertThat(ShadowDeterminerFactory.getShadowAlgorithmDeterminer(mock(NoteShadowAlgorithm.class)) instanceof NoteShadowAlgorithmDeterminer, is(true));
- assertThat(ShadowDeterminerFactory.getShadowAlgorithmDeterminer(mock(ColumnShadowAlgorithm.class)) instanceof ColumnShadowAlgorithmDeterminer, is(true));
- }
-
- private ShadowRule createShadowRule() {
- ShadowRule shadowRule = mock(ShadowRule.class);
- when(shadowRule.getRelatedShadowAlgorithms("t_user")).thenReturn(createRelatedShadowAlgorithms());
- return shadowRule;
- }
-
- private Optional<Collection<ShadowAlgorithm>> createRelatedShadowAlgorithms() {
- Collection<ShadowAlgorithm> result = new LinkedList<>();
- result.add(new SimpleSQLNoteShadowAlgorithm());
- return Optional.of(result);
+ assertThat(ShadowDeterminerFactory.newInstance(mock(NoteShadowAlgorithm.class)) instanceof NoteShadowAlgorithmDeterminer, is(true));
+ assertThat(ShadowDeterminerFactory.newInstance(mock(ColumnShadowAlgorithm.class)) instanceof ColumnShadowAlgorithmDeterminer, is(true));
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminerTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminerTest.java
index 21290da..f554783 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminerTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/ColumnShadowAlgorithmDeterminerTest.java
@@ -17,104 +17,118 @@
package org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm;
-import com.google.common.collect.Lists;
-import org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.algorithm.shadow.column.ColumnRegexMatchShadowAlgorithm;
+import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.shadow.api.shadow.column.ColumnShadowAlgorithm;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.shadow.api.shadow.column.ShadowOperationType;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowAlgorithmDeterminer;
+import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDetermineCondition;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import org.junit.Before;
import org.junit.Test;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
-import java.util.List;
+import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class ColumnShadowAlgorithmDeterminerTest {
- @Test
- public void assertIsShadow() {
- assertTrueCase();
- assertFalseCase();
+ private ShadowAlgorithmDeterminer shadowAlgorithmDeterminer;
+
+ @Before
+ public void init() {
+ shadowAlgorithmDeterminer = new ColumnShadowAlgorithmDeterminer(createColumnShadowAlgorithms());
+ }
+
+ private ColumnShadowAlgorithm<Comparable<?>> createColumnShadowAlgorithms() {
+ ColumnShadowAlgorithm<Comparable<?>> result = new ColumnRegexMatchShadowAlgorithm();
+ result.setProps(createProperties());
+ result.init();
+ return result;
+ }
+
+ private Properties createProperties() {
+ Properties properties = new Properties();
+ properties.setProperty("column", "user_id");
+ properties.setProperty("operation", "insert");
+ properties.setProperty("regex", "[1]");
+ return properties;
}
- private void assertFalseCase() {
- InsertStatementContext insertStatementContext = mock(InsertStatementContext.class);
- when(insertStatementContext.getInsertColumnNames()).thenReturn(createColumnNames());
- when(insertStatementContext.getInsertValueContexts()).thenReturn(createInsertValueContextsFalseCase());
- ColumnShadowAlgorithmDeterminer columnShadowAlgorithmDeterminer = new ColumnShadowAlgorithmDeterminer(createColumnShadowAlgorithm());
- assertThat(columnShadowAlgorithmDeterminer.isShadow(insertStatementContext, createRelatedShadowTables(), "t_user"), is(false));
+ @Test
+ public void assertIsShadow() {
+ assertThat(shadowAlgorithmDeterminer.isShadow(createShadowDetermineCondition(), new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()), "t_order"), is(true));
}
- private List<InsertValueContext> createInsertValueContextsFalseCase() {
- List<InsertValueContext> result = new LinkedList<>();
- result.add(new InsertValueContext(createAssignmentsFalseCase(), Lists.newLinkedList(), 0));
+ private AlgorithmProvidedShadowRuleConfiguration createAlgorithmProvidedShadowRuleConfiguration() {
+ AlgorithmProvidedShadowRuleConfiguration result = new AlgorithmProvidedShadowRuleConfiguration("shadow", Arrays.asList("ds", "ds1"), Arrays.asList("shadow_ds", "shadow_ds1"));
+ result.setEnable(true);
+ result.setDataSources(createDataSources());
+ result.setTables(createTables());
+ result.setShadowAlgorithms(createShadowAlgorithms());
return result;
}
- private Collection<ExpressionSegment> createAssignmentsFalseCase() {
- Collection<ExpressionSegment> result = new LinkedList<>();
- result.add(new LiteralExpressionSegment(0, 1, 1));
- result.add(new LiteralExpressionSegment(2, 6, "jack"));
- result.add(new LiteralExpressionSegment(7, 8, 2));
+ private Map<String, ShadowAlgorithm> createShadowAlgorithms() {
+ Map<String, ShadowAlgorithm> result = new LinkedHashMap<>();
+ result.put("user_id-insert-regex-algorithm", createColumnShadowAlgorithm("user_id", "insert"));
return result;
}
- private void assertTrueCase() {
- InsertStatementContext insertStatementContext = mock(InsertStatementContext.class);
- when(insertStatementContext.getInsertColumnNames()).thenReturn(createColumnNames());
- when(insertStatementContext.getInsertValueContexts()).thenReturn(createInsertValueContextsTrueCase());
- ColumnShadowAlgorithmDeterminer columnShadowAlgorithmDeterminer = new ColumnShadowAlgorithmDeterminer(createColumnShadowAlgorithm());
- assertThat(columnShadowAlgorithmDeterminer.isShadow(insertStatementContext, createRelatedShadowTables(), "t_user"), is(true));
+ private ShadowAlgorithm createColumnShadowAlgorithm(final String column, final String operation) {
+ ColumnRegexMatchShadowAlgorithm columnRegexMatchShadowAlgorithm = new ColumnRegexMatchShadowAlgorithm();
+ columnRegexMatchShadowAlgorithm.setProps(createColumnProperties(column, operation));
+ columnRegexMatchShadowAlgorithm.init();
+ return columnRegexMatchShadowAlgorithm;
}
- private List<InsertValueContext> createInsertValueContextsTrueCase() {
- List<InsertValueContext> result = new LinkedList<>();
- result.add(new InsertValueContext(createAssignmentsTrueCase(), Lists.newLinkedList(), 0));
- return result;
+ private Properties createColumnProperties(final String column, final String operation) {
+ Properties properties = new Properties();
+ properties.setProperty("column", column);
+ properties.setProperty("operation", operation);
+ properties.setProperty("regex", "[1]");
+ return properties;
}
- private Collection<ExpressionSegment> createAssignmentsTrueCase() {
- Collection<ExpressionSegment> result = new LinkedList<>();
- result.add(new LiteralExpressionSegment(0, 1, 1));
- result.add(new LiteralExpressionSegment(2, 6, "jack"));
- result.add(new LiteralExpressionSegment(7, 8, 1));
+ private Map<String, ShadowTableConfiguration> createTables() {
+ Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
+ result.put("t_order", new ShadowTableConfiguration(createShadowAlgorithmNames()));
return result;
}
- private Collection<String> createRelatedShadowTables() {
+ private Collection<String> createShadowAlgorithmNames() {
Collection<String> result = new LinkedList<>();
- result.add("t_user");
- result.add("t_order");
+ result.add("user_id-insert-regex-algorithm");
return result;
}
- private ColumnShadowAlgorithm<Comparable<?>> createColumnShadowAlgorithm() {
- ColumnRegexMatchShadowAlgorithm result = new ColumnRegexMatchShadowAlgorithm();
- result.setProps(createProperties());
- result.init();
+ private Map<String, ShadowDataSourceConfiguration> createDataSources() {
+ Map<String, ShadowDataSourceConfiguration> result = new LinkedHashMap<>();
+ result.put("ds-data-source", new ShadowDataSourceConfiguration("ds", "ds_shadow"));
+ result.put("ds1-data-source", new ShadowDataSourceConfiguration("ds1", "ds1_shadow"));
return result;
}
- private Properties createProperties() {
- Properties properties = new Properties();
- properties.setProperty("column", "age");
- properties.setProperty("operation", "insert");
- properties.setProperty("regex", "[1]");
- return properties;
+ private ShadowDetermineCondition createShadowDetermineCondition() {
+ ShadowDetermineCondition shadowDetermineCondition = new ShadowDetermineCondition(ShadowOperationType.INSERT);
+ shadowDetermineCondition.initColumnValuesMappings(createColumnValuesMappings());
+ return shadowDetermineCondition;
}
- private List<String> createColumnNames() {
- List<String> result = new LinkedList<>();
- result.add("id");
- result.add("name");
- result.add("age");
+ private Map<String, Collection<Comparable<?>>> createColumnValuesMappings() {
+ Map<String, Collection<Comparable<?>>> result = new LinkedHashMap<>();
+ Collection<Comparable<?>> values = new LinkedList<>();
+ values.add(1);
+ result.put("user_id", values);
return result;
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminerTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminerTest.java
deleted file mode 100644
index 2b21710..0000000
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminerTest.java
+++ /dev/null
@@ -1,82 +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.shadow.route.future.engine.determiner.algorithm;
-
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.shadow.algorithm.shadow.note.SimpleSQLNoteShadowAlgorithm;
-import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
-import org.junit.Test;
-
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-
-public final class NoteShadowAlgorithmDeterminerTest {
-
- @Test
- public void assertIsShadow() {
- assertTrueCase();
- assertFalseCase();
- }
-
- private void assertTrueCase() {
- assertThat(new NoteShadowAlgorithmDeterminer(createAlgorithmTrueCase()).isShadow(mock(InsertStatementContext.class), createShadowTablesTrueCase(), "t_user"), is(true));
- }
-
- private Collection<String> createShadowTablesTrueCase() {
- Collection<String> result = new LinkedList<>();
- result.add("t_user");
- result.add("t_order");
- return result;
- }
-
- private NoteShadowAlgorithm createAlgorithmTrueCase() {
- SimpleSQLNoteShadowAlgorithm simpleSQLNoteShadowAlgorithm = new SimpleSQLNoteShadowAlgorithm();
- Properties properties = new Properties();
- properties.setProperty("shadow", "true");
- simpleSQLNoteShadowAlgorithm.setProps(properties);
- simpleSQLNoteShadowAlgorithm.init();
- return simpleSQLNoteShadowAlgorithm;
- }
-
- private void assertFalseCase() {
- assertThat(new NoteShadowAlgorithmDeterminer(createAlgorithmFalseCase()).isShadow(mock(InsertStatementContext.class), createShadowTablesTrueCase(), "t_user"), is(false));
- assertThat(new NoteShadowAlgorithmDeterminer(createAlgorithmTrueCase()).isShadow(mock(InsertStatementContext.class), createShadowTablesFalseCase(), "t_user"), is(false));
- assertThat(new NoteShadowAlgorithmDeterminer(createAlgorithmTrueCase()).isShadow(mock(InsertStatementContext.class), createShadowTablesTrueCase(), "t_auto"), is(false));
- }
-
- private Collection<String> createShadowTablesFalseCase() {
- Collection<String> result = new LinkedList<>();
- result.add("t_auto");
- result.add("t_order");
- return result;
- }
-
- private NoteShadowAlgorithm createAlgorithmFalseCase() {
- SimpleSQLNoteShadowAlgorithm simpleSQLNoteShadowAlgorithm = new SimpleSQLNoteShadowAlgorithm();
- Properties properties = new Properties();
- properties.setProperty("shadow", "false");
- simpleSQLNoteShadowAlgorithm.setProps(properties);
- simpleSQLNoteShadowAlgorithm.init();
- return simpleSQLNoteShadowAlgorithm;
- }
-}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/table/AnyAlgorithmApplicableShadowTableDeterminerTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/table/AnyAlgorithmApplicableShadowTableDeterminerTest.java
deleted file mode 100644
index 5062b5c..0000000
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/table/AnyAlgorithmApplicableShadowTableDeterminerTest.java
+++ /dev/null
@@ -1,95 +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.shadow.route.future.engine.determiner.table;
-
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.shadow.algorithm.shadow.note.SimpleSQLNoteShadowAlgorithm;
-import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowAlgorithmDeterminer;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDeterminerFactory;
-import org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowTableDeterminer;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
-import org.junit.Test;
-
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public final class AnyAlgorithmApplicableShadowTableDeterminerTest {
-
- @Test
- public void assertIsShadow() {
- assertTrueCase();
- assertFalseCase();
- }
-
- private void assertTrueCase() {
- ShadowTableDeterminer shadowTableDeterminer = new AnyAlgorithmApplicableShadowTableDeterminer(createShadowAlgorithmDeterminersTrueCase());
- assertThat(shadowTableDeterminer.isShadow(mock(InsertStatementContext.class), createShadowRule(), "t_user"), is(true));
- }
-
- private Collection<ShadowAlgorithmDeterminer> createShadowAlgorithmDeterminersTrueCase() {
- Collection<ShadowAlgorithmDeterminer> result = new LinkedList<>();
- result.add(ShadowDeterminerFactory.getShadowAlgorithmDeterminer(createShadowAlgorithmsTrueCase()));
- return result;
- }
-
- private ShadowAlgorithm createShadowAlgorithmsTrueCase() {
- NoteShadowAlgorithm<String> result = new SimpleSQLNoteShadowAlgorithm();
- Properties properties = new Properties();
- properties.setProperty("shadow", "true");
- result.setProps(properties);
- result.init();
- return result;
- }
-
- private ShadowRule createShadowRule() {
- ShadowRule result = mock(ShadowRule.class);
- Collection<String> tables = new LinkedList<>();
- tables.add("t_user");
- tables.add("t_order");
- when(result.getAllShadowTableNames()).thenReturn(tables);
- return result;
- }
-
- private void assertFalseCase() {
- ShadowTableDeterminer shadowTableDeterminer = new AnyAlgorithmApplicableShadowTableDeterminer(createShadowAlgorithmDeterminersFalseCase());
- assertThat(shadowTableDeterminer.isShadow(mock(InsertStatementContext.class), createShadowRule(), "t_user"), is(false));
- }
-
- private Collection<ShadowAlgorithmDeterminer> createShadowAlgorithmDeterminersFalseCase() {
- Collection<ShadowAlgorithmDeterminer> result = new LinkedList<>();
- result.add(ShadowDeterminerFactory.getShadowAlgorithmDeterminer(createShadowAlgorithmsFalseCase()));
- return result;
- }
-
- private ShadowAlgorithm createShadowAlgorithmsFalseCase() {
- NoteShadowAlgorithm<String> result = new SimpleSQLNoteShadowAlgorithm();
- Properties properties = new Properties();
- properties.setProperty("shadow", "false");
- result.setProps(properties);
- result.init();
- return result;
- }
-}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngineTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngineTest.java
index a159b30..f97a0cd 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngineTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/dml/ShadowInsertStatementRoutingEngineTest.java
@@ -18,10 +18,7 @@
package org.apache.shardingsphere.shadow.route.future.engine.dml;
import com.google.common.collect.Lists;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
@@ -30,6 +27,7 @@ import org.apache.shardingsphere.shadow.algorithm.shadow.column.ColumnRegexMatch
import org.apache.shardingsphere.shadow.algorithm.shadow.note.SimpleSQLNoteShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.route.future.engine.ShadowRouteEngine;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
@@ -51,18 +49,16 @@ import static org.mockito.Mockito.when;
public final class ShadowInsertStatementRoutingEngineTest {
- private ShadowInsertStatementRoutingEngine shadowRoutingEngine;
+ private ShadowRouteEngine shadowRoutingEngine;
@Before
public void init() {
- shadowRoutingEngine = new ShadowInsertStatementRoutingEngine();
+ shadowRoutingEngine = new ShadowInsertStatementRoutingEngine(createInsertStatementContext());
}
@Test
public void assertRoute() {
- ShadowRule shadowRule = new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration());
- shadowRoutingEngine.route(createRouteContext(), new LogicSQL(createInsertStatementContext(), "", Lists.newArrayList()), mock(ShardingSphereMetaData.class), shadowRule,
- mock(ConfigurationProperties.class));
+ shadowRoutingEngine.route(createRouteContext(), new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
}
private AlgorithmProvidedShadowRuleConfiguration createAlgorithmProvidedShadowRuleConfiguration() {