未验证 提交 a3b9039f 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Provide inline implement for standard algorithm (#5195)

* Provide inline implement for standard algorithm

* fix
上级 5560b579
......@@ -50,16 +50,22 @@ shardingRule:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGeneratorColumnName: order_id
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${order_id % 2}
keyGeneratorColumnName: order_item_id
bindingTables:
- t_order,t_order_item
......
/*
* 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.core.strategy.sharding;
import com.google.common.base.Preconditions;
import groovy.lang.Closure;
import groovy.util.Expando;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
import org.apache.shardingsphere.underlying.common.config.inline.InlineExpressionParser;
import java.util.Collection;
import java.util.Properties;
/**
* Inline sharding algorithm.
*/
public final class InlineShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
private static final String ALLOW_RANGE_QUERY = "allow.range.query.with.inline.sharding";
private static final String ALGORITHM_EXPRESSION = "algorithm.expression";
private Properties properties = new Properties();
@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
Preconditions.checkNotNull(properties.get(ALGORITHM_EXPRESSION), "Inline sharding algorithm expression cannot be null.");
Closure<?> closure = new InlineExpressionParser(properties.get(ALGORITHM_EXPRESSION).toString()).evaluateClosure();
Closure<?> result = closure.rehydrate(new Expando(), null, null);
result.setResolveStrategy(Closure.DELEGATE_ONLY);
result.setProperty(shardingValue.getColumnName(), shardingValue.getValue());
return result.call().toString();
}
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
if (isAllowRangeQuery()) {
return availableTargetNames;
}
throw new UnsupportedOperationException("Since the property of `allow.range.query.with.inline.sharding` is false, inline sharding algorithm can not tackle with range query.");
}
private boolean isAllowRangeQuery() {
return null != properties.get(ALLOW_RANGE_QUERY) && Boolean.parseBoolean(properties.get(ALLOW_RANGE_QUERY).toString());
}
@Override
public String getType() {
return "INLINE";
}
@Override
public Properties getProperties() {
return properties;
}
@Override
public void setProperties(final Properties properties) {
this.properties = properties;
}
}
#
# 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.
#
org.apache.shardingsphere.core.strategy.sharding.InlineShardingAlgorithm
......@@ -100,8 +100,9 @@ public final class YamlEngineShadowShardingConfigurationTest {
private void assertTOrder(final YamlRootShadowConfiguration actual) {
assertThat(actual.getShadowRule().getShardingRule().getTables().get("t_order").getActualDataNodes(), is("ds_${0..1}.t_order_${0..1}"));
assertThat(actual.getShadowRule().getShardingRule().getTables().get("t_order").getTableStrategy().getInline().getShardingColumn(), is("order_id"));
assertThat(actual.getShadowRule().getShardingRule().getTables().get("t_order").getTableStrategy().getInline().getAlgorithmExpression(), is("t_order_${order_id % 2}"));
assertThat(actual.getShadowRule().getShardingRule().getTables().get("t_order").getTableStrategy().getStandard().getShardingColumn(), is("order_id"));
assertThat(actual.getShadowRule().getShardingRule().getTables().get("t_order").getTableStrategy().getStandard().getShardingAlgorithm().getProps().getProperty("algorithm.expression"),
is("t_order_${order_id % 2}"));
assertThat(actual.getShadowRule().getShardingRule().getTables().get("t_order").getKeyGenerator().getColumn(), is("order_id"));
assertThat(actual.getShadowRule().getShardingRule().getTables().get("t_order").getKeyGenerator().getType(), is("SNOWFLAKE"));
}
......
......@@ -99,8 +99,9 @@ public final class YamlEngineShardingConfigurationTest {
private void assertTOrder(final YamlRootShardingConfiguration actual) {
assertThat(actual.getShardingRule().getTables().get("t_order").getActualDataNodes(), is("ds_${0..1}.t_order_${0..1}"));
assertThat(actual.getShardingRule().getTables().get("t_order").getTableStrategy().getInline().getShardingColumn(), is("order_id"));
assertThat(actual.getShardingRule().getTables().get("t_order").getTableStrategy().getInline().getAlgorithmExpression(), is("t_order_${order_id % 2}"));
assertThat(actual.getShardingRule().getTables().get("t_order").getTableStrategy().getStandard().getShardingColumn(), is("order_id"));
assertThat(actual.getShardingRule().getTables().get("t_order").getTableStrategy().getStandard().getShardingAlgorithm().getProps().getProperty("algorithm.expression"),
is("t_order_${order_id % 2}"));
assertThat(actual.getShardingRule().getTables().get("t_order").getKeyGenerator().getColumn(), is("order_id"));
assertThat(actual.getShardingRule().getTables().get("t_order").getKeyGenerator().getType(), is("SNOWFLAKE"));
}
......
......@@ -92,9 +92,12 @@ shadowRule:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
......@@ -112,9 +115,12 @@ shadowRule:
broadcastTables:
- t_config
defaultDatabaseStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: ds_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: ds_${order_id % 2}
defaultTableStrategy:
none:
masterSlaveRules:
......
......@@ -79,9 +79,12 @@ shardingRule:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
......@@ -99,9 +102,12 @@ shardingRule:
broadcastTables:
- t_config
defaultDatabaseStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: ds_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: ds_${order_id % 2}
defaultTableStrategy:
none:
masterSlaveRules:
......
......@@ -27,27 +27,36 @@ shardingRule:
t_account:
actualDataNodes: db.t_account_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_${account_id % 2}
keyGenerator:
type: TEST
column: account_id
t_account_bak:
actualDataNodes: db.t_account_bak_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_bak_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_bak_${account_id % 2}
keyGenerator:
type: TEST
column: account_id
t_account_detail:
actualDataNodes: db.t_account_detail_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_${account_id % 2}
bindingTables:
- t_account, t_account_detail
......
......@@ -27,27 +27,36 @@ shardingRule:
t_account:
actualDataNodes: db.t_account_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_${account_id % 2}
keyGenerator:
type: TEST
column: account_id
t_account_bak:
actualDataNodes: db.t_account_bak_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_bak_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_bak_${account_id % 2}
keyGenerator:
type: TEST
column: account_id
t_account_detail:
actualDataNodes: db.t_account_detail_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_${account_id % 2}
bindingTables:
- t_account, t_account_detail
......
......@@ -27,17 +27,23 @@ shardingRule:
t_account:
actualDataNodes: db.t_account_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_${account_id % 2}
keyGenerator:
type: TEST
column: account_id
t_account_detail:
actualDataNodes: db.t_account_detail_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: account_id
algorithmExpression: t_account_detail_${account_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_account_detail_${account_id % 2}
bindingTables:
- t_account, t_account_detail
......@@ -24,13 +24,19 @@ shadowRule:
t_order_item:
actualDataNodes: db.t_order_item
databaseStrategy:
inline:
standard:
shardingColumn: item_id
algorithmExpression: db
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: db
tableStrategy:
inline:
standard:
shardingColumn: item_id
algorithmExpression: t_order_item
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item
keyGenerator:
type: Constant
column: item_id
......
......@@ -42,9 +42,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: INCREMENT
column: order_id
......@@ -56,9 +59,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${order_id % 2}
bindingTables:
- t_order,t_order_item
......
......@@ -41,10 +41,13 @@ shardingRule:
shardingColumn: user_id
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
tableStrategy:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: INCREMENT
column: order_id
......@@ -56,9 +59,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
bindingTables:
- t_order,t_order_item
......
......@@ -28,9 +28,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: INCREMENT
column: order_id
......@@ -42,9 +45,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${order_id % 2}
bindingTables:
- t_order,t_order_item
......
......@@ -27,9 +27,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: INCREMENT
column: order_id
......@@ -41,9 +44,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${order_id % 2}
bindingTables:
- t_order,t_order_item
defaultDatabaseStrategy:
......
......@@ -54,9 +54,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: INCREMENT
column: order_id
......@@ -68,9 +71,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: id
algorithmExpression: t_order_item_${id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${id % 2}
bindingTables:
- t_order,t_order_item
......
......@@ -54,9 +54,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: INCREMENT
column: order_id
......@@ -68,9 +71,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${order_id % 2}
bindingTables:
- t_order,t_order_item
......
......@@ -28,9 +28,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: INCREMENT
column: order_id
......@@ -42,9 +45,12 @@ shardingRule:
shardingAlgorithm:
type: STANDARD_TEST
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${order_id % 2}
bindingTables:
- t_order,t_order_item
......
......@@ -32,13 +32,19 @@ shardingRule:
t_order:
actualDataNodes: ds_ms.t_order_${0..1}
databaseStrategy:
inline:
standard:
shardingColumn: user_id
algorithmExpression: t_order_${user_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${user_id % 2}
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
......@@ -47,9 +53,12 @@ shardingRule:
t_order_item:
actualDataNodes: ds_ms.t_order_item_${0..1}
tableStrategy:
inline:
standard:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: t_order_item_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_item_id
......@@ -63,9 +72,12 @@ shardingRule:
slaveDataSourceNames:
- ds_s
defaultDatabaseStrategy:
inline:
standard:
shardingColumn: user_id
algorithmExpression: ds_ms_${user_id % 2}
shardingAlgorithm:
type: INLINE
props:
algorithm.expression: ds_ms_${user_id % 2}
defaultTableStrategy:
none:
props:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册