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

Create ShardingAutoTableAlgorithm (#5943)

上级 ee8432f7
/*
* 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.sharding.api.sharding;
/**
* Sharding auto table algorithm.
*
*/
public interface ShardingAutoTableAlgorithm {
/**
* Get auto tables amount.
*
* @return the auto tables amount
*/
int getAutoTablesAmount();
}
...@@ -17,21 +17,24 @@ ...@@ -17,21 +17,24 @@
package org.apache.shardingsphere.sharding.rule; package org.apache.shardingsphere.sharding.rule;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datanode.DataNodeUtil;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.sharding.api.config.KeyGeneratorConfiguration; import org.apache.shardingsphere.sharding.api.config.KeyGeneratorConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.strategy.algorithm.sharding.inline.InlineExpressionParser; import org.apache.shardingsphere.sharding.strategy.algorithm.sharding.inline.InlineExpressionParser;
import org.apache.shardingsphere.sharding.strategy.route.ShardingStrategy; import org.apache.shardingsphere.sharding.strategy.route.ShardingStrategy;
import org.apache.shardingsphere.sharding.strategy.route.ShardingStrategyFactory; import org.apache.shardingsphere.sharding.strategy.route.ShardingStrategyFactory;
import org.apache.shardingsphere.sharding.strategy.route.none.NoneShardingStrategy; import org.apache.shardingsphere.sharding.strategy.route.none.NoneShardingStrategy;
import org.apache.shardingsphere.sharding.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datanode.DataNodeUtil;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
...@@ -95,7 +98,23 @@ public final class TableRule { ...@@ -95,7 +98,23 @@ public final class TableRule {
tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategy() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategy()); tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategy() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategy());
final KeyGeneratorConfiguration keyGeneratorConfiguration = tableRuleConfig.getKeyGenerator(); final KeyGeneratorConfiguration keyGeneratorConfiguration = tableRuleConfig.getKeyGenerator();
generateKeyColumn = null != keyGeneratorConfiguration && !Strings.isNullOrEmpty(keyGeneratorConfiguration.getColumn()) ? keyGeneratorConfiguration.getColumn() : defaultGenerateKeyColumn; generateKeyColumn = null != keyGeneratorConfiguration && !Strings.isNullOrEmpty(keyGeneratorConfiguration.getColumn()) ? keyGeneratorConfiguration.getColumn() : defaultGenerateKeyColumn;
keyGenerateAlgorithm = containsKeyGenerateAlgorithm(tableRuleConfig) ? tableRuleConfig.getKeyGenerator().getKeyGenerateAlgorithm() : null; keyGenerateAlgorithm = containsKeyGenerateAlgorithm(tableRuleConfig.getKeyGenerator()) ? tableRuleConfig.getKeyGenerator().getKeyGenerateAlgorithm() : null;
checkRule(dataNodes);
}
public TableRule(final ShardingAutoTableRuleConfiguration tableRuleConfig, final Collection<String> dataSourceNames, final String defaultGenerateKeyColumn) {
Preconditions.checkArgument(null == tableRuleConfig.getShardingStrategy() || tableRuleConfig.getShardingStrategy() instanceof ShardingAutoTableAlgorithm,
"ShardingAutoTableAlgorithm is required.");
logicTable = tableRuleConfig.getLogicTable().toLowerCase();
tableShardingStrategy = null == tableRuleConfig.getShardingStrategy() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getShardingStrategy());
List<String> dataNodes = null;
dataNodeIndexMap = new HashMap<>(dataNodes.size(), 1);
actualDataNodes = isEmptyDataNodes(dataNodes) ? generateDataNodes(tableRuleConfig.getLogicTable(), dataSourceNames) : generateDataNodes(dataNodes, dataSourceNames);
actualTables = getActualTables();
databaseShardingStrategy = null;
final KeyGeneratorConfiguration keyGeneratorConfiguration = tableRuleConfig.getKeyGenerator();
generateKeyColumn = null != keyGeneratorConfiguration && !Strings.isNullOrEmpty(keyGeneratorConfiguration.getColumn()) ? keyGeneratorConfiguration.getColumn() : defaultGenerateKeyColumn;
keyGenerateAlgorithm = containsKeyGenerateAlgorithm(tableRuleConfig.getKeyGenerator()) ? tableRuleConfig.getKeyGenerator().getKeyGenerateAlgorithm() : null;
checkRule(dataNodes); checkRule(dataNodes);
} }
...@@ -107,8 +126,8 @@ public final class TableRule { ...@@ -107,8 +126,8 @@ public final class TableRule {
datasourceToTablesMap.computeIfAbsent(datasourceName, key -> new LinkedHashSet<>()).add(tableName); datasourceToTablesMap.computeIfAbsent(datasourceName, key -> new LinkedHashSet<>()).add(tableName);
} }
private boolean containsKeyGenerateAlgorithm(final ShardingTableRuleConfiguration shardingTableRuleConfiguration) { private boolean containsKeyGenerateAlgorithm(final KeyGeneratorConfiguration keyGenerator) {
return null != shardingTableRuleConfiguration.getKeyGenerator() && null != shardingTableRuleConfiguration.getKeyGenerator().getKeyGenerateAlgorithm(); return null != keyGenerator && null != keyGenerator.getKeyGenerateAlgorithm();
} }
private boolean isEmptyDataNodes(final List<String> dataNodes) { private boolean isEmptyDataNodes(final List<String> dataNodes) {
......
...@@ -17,8 +17,9 @@ ...@@ -17,8 +17,9 @@
package org.apache.shardingsphere.sharding.strategy.route; package org.apache.shardingsphere.sharding.strategy.route;
import org.apache.shardingsphere.sharding.strategy.route.value.RouteValue;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.sharding.spi.algorithm.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.strategy.route.value.RouteValue;
import java.util.Collection; import java.util.Collection;
...@@ -34,6 +35,13 @@ public interface ShardingStrategy { ...@@ -34,6 +35,13 @@ public interface ShardingStrategy {
*/ */
Collection<String> getShardingColumns(); Collection<String> getShardingColumns();
/**
* Get sharding algorithm.
*
* @return sharding algorithm
*/
ShardingAlgorithm getShardingAlgorithm();
/** /**
* Sharding. * Sharding.
* *
......
...@@ -38,9 +38,9 @@ import java.util.TreeSet; ...@@ -38,9 +38,9 @@ import java.util.TreeSet;
/** /**
* Complex sharding strategy. * Complex sharding strategy.
*/ */
@Getter
public final class ComplexShardingStrategy implements ShardingStrategy { public final class ComplexShardingStrategy implements ShardingStrategy {
@Getter
private final Collection<String> shardingColumns; private final Collection<String> shardingColumns;
private final ComplexKeysShardingAlgorithm shardingAlgorithm; private final ComplexKeysShardingAlgorithm shardingAlgorithm;
......
...@@ -33,9 +33,9 @@ import java.util.TreeSet; ...@@ -33,9 +33,9 @@ import java.util.TreeSet;
/** /**
* Hint sharding strategy. * Hint sharding strategy.
*/ */
@Getter
public final class HintShardingStrategy implements ShardingStrategy { public final class HintShardingStrategy implements ShardingStrategy {
@Getter
private final Collection<String> shardingColumns; private final Collection<String> shardingColumns;
private final HintShardingAlgorithm shardingAlgorithm; private final HintShardingAlgorithm shardingAlgorithm;
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.strategy.route.none; package org.apache.shardingsphere.sharding.strategy.route.none;
import lombok.Getter; import lombok.Getter;
import org.apache.shardingsphere.sharding.spi.algorithm.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.strategy.route.ShardingStrategy; import org.apache.shardingsphere.sharding.strategy.route.ShardingStrategy;
import org.apache.shardingsphere.sharding.strategy.route.value.RouteValue; import org.apache.shardingsphere.sharding.strategy.route.value.RouteValue;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
...@@ -33,6 +34,11 @@ public final class NoneShardingStrategy implements ShardingStrategy { ...@@ -33,6 +34,11 @@ public final class NoneShardingStrategy implements ShardingStrategy {
private final Collection<String> shardingColumns = Collections.emptyList(); private final Collection<String> shardingColumns = Collections.emptyList();
@Override
public ShardingAlgorithm getShardingAlgorithm() {
return null;
}
@Override @Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final Collection<RouteValue> shardingValues, final ConfigurationProperties properties) { public Collection<String> doSharding(final Collection<String> availableTargetNames, final Collection<RouteValue> shardingValues, final ConfigurationProperties properties) {
return availableTargetNames; return availableTargetNames;
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.strategy.route.standard; package org.apache.shardingsphere.sharding.strategy.route.standard;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.sharding.api.config.strategy.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
...@@ -39,6 +40,7 @@ public final class StandardShardingStrategy implements ShardingStrategy { ...@@ -39,6 +40,7 @@ public final class StandardShardingStrategy implements ShardingStrategy {
private final String shardingColumn; private final String shardingColumn;
@Getter
private final StandardShardingAlgorithm shardingAlgorithm; private final StandardShardingAlgorithm shardingAlgorithm;
public StandardShardingStrategy(final StandardShardingStrategyConfiguration standardShardingStrategyConfig) { public StandardShardingStrategy(final StandardShardingStrategyConfiguration standardShardingStrategyConfig) {
......
...@@ -36,6 +36,8 @@ public final class YamlShardingRuleConfiguration implements YamlRuleConfiguratio ...@@ -36,6 +36,8 @@ public final class YamlShardingRuleConfiguration implements YamlRuleConfiguratio
private Map<String, YamlTableRuleConfiguration> tables = new LinkedHashMap<>(); private Map<String, YamlTableRuleConfiguration> tables = new LinkedHashMap<>();
private Map<String, YamlShardingAutoTableRuleConfiguration> autoTables = new LinkedHashMap<>();
private Collection<String> bindingTables = new ArrayList<>(); private Collection<String> bindingTables = new ArrayList<>();
private Collection<String> broadcastTables = new ArrayList<>(); private Collection<String> broadcastTables = new ArrayList<>();
......
...@@ -18,9 +18,11 @@ ...@@ -18,9 +18,11 @@
package org.apache.shardingsphere.sharding.yaml.swapper; package org.apache.shardingsphere.sharding.yaml.swapper;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper; import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
import org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.constant.ShardingOrder; import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration; import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.YamlTableRuleConfiguration; import org.apache.shardingsphere.sharding.yaml.config.YamlTableRuleConfiguration;
...@@ -33,6 +35,8 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi ...@@ -33,6 +35,8 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
private final TableRuleConfigurationYamlSwapper tableRuleConfigurationYamlSwapper = new TableRuleConfigurationYamlSwapper(); private final TableRuleConfigurationYamlSwapper tableRuleConfigurationYamlSwapper = new TableRuleConfigurationYamlSwapper();
private final ShardingAutoTableRuleConfigurationYamlSwapper autoTableRuleConfigurationYamlSwapper = new ShardingAutoTableRuleConfigurationYamlSwapper();
private final ShardingStrategyConfigurationYamlSwapper shardingStrategyConfigurationYamlSwapper = new ShardingStrategyConfigurationYamlSwapper(); private final ShardingStrategyConfigurationYamlSwapper shardingStrategyConfigurationYamlSwapper = new ShardingStrategyConfigurationYamlSwapper();
private final KeyGeneratorConfigurationYamlSwapper keyGeneratorConfigurationYamlSwapper = new KeyGeneratorConfigurationYamlSwapper(); private final KeyGeneratorConfigurationYamlSwapper keyGeneratorConfigurationYamlSwapper = new KeyGeneratorConfigurationYamlSwapper();
...@@ -43,6 +47,9 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi ...@@ -43,6 +47,9 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
for (ShardingTableRuleConfiguration each : data.getTables()) { for (ShardingTableRuleConfiguration each : data.getTables()) {
result.getTables().put(each.getLogicTable(), tableRuleConfigurationYamlSwapper.swap(each)); result.getTables().put(each.getLogicTable(), tableRuleConfigurationYamlSwapper.swap(each));
} }
for (ShardingAutoTableRuleConfiguration each : data.getAutoTables()) {
result.getAutoTables().put(each.getLogicTable(), autoTableRuleConfigurationYamlSwapper.swap(each));
}
result.getBindingTables().addAll(data.getBindingTableGroups()); result.getBindingTables().addAll(data.getBindingTableGroups());
result.getBroadcastTables().addAll(data.getBroadcastTables()); result.getBroadcastTables().addAll(data.getBroadcastTables());
if (null != data.getDefaultDatabaseShardingStrategy()) { if (null != data.getDefaultDatabaseShardingStrategy()) {
...@@ -65,6 +72,11 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi ...@@ -65,6 +72,11 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
tableRuleConfig.setLogicTable(entry.getKey()); tableRuleConfig.setLogicTable(entry.getKey());
result.getTables().add(tableRuleConfigurationYamlSwapper.swap(tableRuleConfig)); result.getTables().add(tableRuleConfigurationYamlSwapper.swap(tableRuleConfig));
} }
for (Entry<String, YamlShardingAutoTableRuleConfiguration> entry : yamlConfiguration.getAutoTables().entrySet()) {
YamlShardingAutoTableRuleConfiguration tableRuleConfig = entry.getValue();
tableRuleConfig.setLogicTable(entry.getKey());
result.getAutoTables().add(autoTableRuleConfigurationYamlSwapper.swap(tableRuleConfig));
}
result.getBindingTableGroups().addAll(yamlConfiguration.getBindingTables()); result.getBindingTableGroups().addAll(yamlConfiguration.getBindingTables());
result.getBroadcastTables().addAll(yamlConfiguration.getBroadcastTables()); result.getBroadcastTables().addAll(yamlConfiguration.getBroadcastTables());
if (null != yamlConfiguration.getDefaultDatabaseStrategy()) { if (null != yamlConfiguration.getDefaultDatabaseStrategy()) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册