未验证 提交 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 @@
package org.apache.shardingsphere.sharding.rule;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.Getter;
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.ShardingAutoTableRuleConfiguration;
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.route.ShardingStrategy;
import org.apache.shardingsphere.sharding.strategy.route.ShardingStrategyFactory;
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.Collections;
......@@ -95,10 +98,26 @@ public final class TableRule {
tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategy() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategy());
final KeyGeneratorConfiguration keyGeneratorConfiguration = tableRuleConfig.getKeyGenerator();
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);
}
private Set<String> getActualTables() {
return actualDataNodes.stream().map(DataNode::getTableName).collect(Collectors.toSet());
}
......@@ -107,8 +126,8 @@ public final class TableRule {
datasourceToTablesMap.computeIfAbsent(datasourceName, key -> new LinkedHashSet<>()).add(tableName);
}
private boolean containsKeyGenerateAlgorithm(final ShardingTableRuleConfiguration shardingTableRuleConfiguration) {
return null != shardingTableRuleConfiguration.getKeyGenerator() && null != shardingTableRuleConfiguration.getKeyGenerator().getKeyGenerateAlgorithm();
private boolean containsKeyGenerateAlgorithm(final KeyGeneratorConfiguration keyGenerator) {
return null != keyGenerator && null != keyGenerator.getKeyGenerateAlgorithm();
}
private boolean isEmptyDataNodes(final List<String> dataNodes) {
......
......@@ -17,8 +17,9 @@
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.sharding.spi.algorithm.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.strategy.route.value.RouteValue;
import java.util.Collection;
......@@ -33,6 +34,13 @@ public interface ShardingStrategy {
* @return sharding columns
*/
Collection<String> getShardingColumns();
/**
* Get sharding algorithm.
*
* @return sharding algorithm
*/
ShardingAlgorithm getShardingAlgorithm();
/**
* Sharding.
......
......@@ -38,9 +38,9 @@ import java.util.TreeSet;
/**
* Complex sharding strategy.
*/
@Getter
public final class ComplexShardingStrategy implements ShardingStrategy {
@Getter
private final Collection<String> shardingColumns;
private final ComplexKeysShardingAlgorithm shardingAlgorithm;
......
......@@ -33,9 +33,9 @@ import java.util.TreeSet;
/**
* Hint sharding strategy.
*/
@Getter
public final class HintShardingStrategy implements ShardingStrategy {
@Getter
private final Collection<String> shardingColumns;
private final HintShardingAlgorithm shardingAlgorithm;
......
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.strategy.route.none;
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.value.RouteValue;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
......@@ -32,7 +33,12 @@ import java.util.Collections;
public final class NoneShardingStrategy implements ShardingStrategy {
private final Collection<String> shardingColumns = Collections.emptyList();
@Override
public ShardingAlgorithm getShardingAlgorithm() {
return null;
}
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final Collection<RouteValue> shardingValues, final ConfigurationProperties properties) {
return availableTargetNames;
......
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.strategy.route.standard;
import com.google.common.base.Preconditions;
import lombok.Getter;
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.RangeShardingValue;
......@@ -38,7 +39,8 @@ import java.util.TreeSet;
public final class StandardShardingStrategy implements ShardingStrategy {
private final String shardingColumn;
@Getter
private final StandardShardingAlgorithm shardingAlgorithm;
public StandardShardingStrategy(final StandardShardingStrategyConfiguration standardShardingStrategyConfig) {
......
......@@ -35,7 +35,9 @@ import java.util.Map;
public final class YamlShardingRuleConfiguration implements YamlRuleConfiguration {
private Map<String, YamlTableRuleConfiguration> tables = new LinkedHashMap<>();
private Map<String, YamlShardingAutoTableRuleConfiguration> autoTables = new LinkedHashMap<>();
private Collection<String> bindingTables = new ArrayList<>();
private Collection<String> broadcastTables = new ArrayList<>();
......
......@@ -18,9 +18,11 @@
package org.apache.shardingsphere.sharding.yaml.swapper;
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.ShardingTableRuleConfiguration;
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.YamlTableRuleConfiguration;
......@@ -32,6 +34,8 @@ import java.util.Map.Entry;
public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfigurationSwapper<YamlShardingRuleConfiguration, ShardingRuleConfiguration> {
private final TableRuleConfigurationYamlSwapper tableRuleConfigurationYamlSwapper = new TableRuleConfigurationYamlSwapper();
private final ShardingAutoTableRuleConfigurationYamlSwapper autoTableRuleConfigurationYamlSwapper = new ShardingAutoTableRuleConfigurationYamlSwapper();
private final ShardingStrategyConfigurationYamlSwapper shardingStrategyConfigurationYamlSwapper = new ShardingStrategyConfigurationYamlSwapper();
......@@ -43,6 +47,9 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
for (ShardingTableRuleConfiguration each : data.getTables()) {
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.getBroadcastTables().addAll(data.getBroadcastTables());
if (null != data.getDefaultDatabaseShardingStrategy()) {
......@@ -65,6 +72,11 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
tableRuleConfig.setLogicTable(entry.getKey());
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.getBroadcastTables().addAll(yamlConfiguration.getBroadcastTables());
if (null != yamlConfiguration.getDefaultDatabaseStrategy()) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册