未验证 提交 aaeed27c 编写于 作者: L Liang Zhang 提交者: GitHub

Add ResourceAware to simplify rule builder (#8132)

* Add generic of ShardingRouteEngineFactory

* Add generic of ShardingTableBroadcastRoutingEngine

* Add SingleTableRule

* Add ResourceAware

* Add ResourceAware

* Remove dataSourceNames parameter in ShardingSphereRuleBuilder

* Refactor ShardingRule
上级 e6f3153f
......@@ -22,15 +22,13 @@ import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import java.util.Collection;
/**
* Algorithm provided encrypt rule builder.
*/
public final class AlgorithmProvidedEncryptRuleBuilder implements ShardingSphereRuleBuilder<EncryptRule, AlgorithmProvidedEncryptRuleConfiguration> {
@Override
public EncryptRule build(final AlgorithmProvidedEncryptRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public EncryptRule build(final AlgorithmProvidedEncryptRuleConfiguration ruleConfig) {
return new EncryptRule(ruleConfig);
}
......
......@@ -22,15 +22,13 @@ import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import java.util.Collection;
/**
* Encrypt rule builder.
*/
public final class EncryptRuleBuilder implements ShardingSphereRuleBuilder<EncryptRule, EncryptRuleConfiguration> {
@Override
public EncryptRule build(final EncryptRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public EncryptRule build(final EncryptRuleConfiguration ruleConfig) {
return new EncryptRule(ruleConfig);
}
......
......@@ -41,6 +41,6 @@ public final class AlgorithmProvidedEncryptRuleBuilderTest {
public void assertBuild() {
AlgorithmProvidedEncryptRuleConfiguration ruleConfig = mock(AlgorithmProvidedEncryptRuleConfiguration.class);
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(EncryptRule.class));
assertThat(builder.build(ruleConfig), instanceOf(EncryptRule.class));
}
}
......@@ -41,6 +41,6 @@ public final class EncryptRuleBuilderTest {
public void assertBuild() {
EncryptRuleConfiguration ruleConfig = mock(EncryptRuleConfiguration.class);
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(EncryptRule.class));
assertThat(builder.build(ruleConfig), instanceOf(EncryptRule.class));
}
}
......@@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
......@@ -70,9 +71,10 @@ public final class EncryptSQLRewriterParameterizedTest extends AbstractSQLRewrit
@Override
protected Collection<SQLRewriteUnit> createSQLRewriteUnits() throws IOException {
YamlRootRuleConfigurations ruleConfigurations = createRuleConfigurations();
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(
new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(ruleConfigurations.getRules()), ruleConfigurations.getDataSources().keySet());
SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType());
String databaseType = null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType();
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(
ruleConfigurations.getRules()), DatabaseTypeRegistry.getTrunkDatabaseType(databaseType), ruleConfigurations.getDataSources());
SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType);
ShardingSphereSchema schema = mockSchema();
ConfigurationProperties props = new ConfigurationProperties(ruleConfigurations.getProps());
SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
......
......@@ -17,12 +17,10 @@
package org.apache.shardingsphere.ha.rule.biulder;
import org.apache.shardingsphere.ha.rule.HARule;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.ha.algorithm.config.AlgorithmProvidedHARuleConfiguration;
import org.apache.shardingsphere.ha.constant.HAOrder;
import java.util.Collection;
import org.apache.shardingsphere.ha.rule.HARule;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
/**
* Algorithm provided HA rule builder.
......@@ -30,7 +28,7 @@ import java.util.Collection;
public final class AlgorithmProvidedHARuleBuilder implements ShardingSphereRuleBuilder<HARule, AlgorithmProvidedHARuleConfiguration> {
@Override
public HARule build(final AlgorithmProvidedHARuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public HARule build(final AlgorithmProvidedHARuleConfiguration ruleConfig) {
return new HARule(ruleConfig);
}
......
......@@ -19,10 +19,8 @@ package org.apache.shardingsphere.ha.rule.biulder;
import org.apache.shardingsphere.ha.api.config.HARuleConfiguration;
import org.apache.shardingsphere.ha.constant.HAOrder;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.ha.rule.HARule;
import java.util.Collection;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
/**
* HA rule builder.
......@@ -30,7 +28,7 @@ import java.util.Collection;
public final class HARuleBuilder implements ShardingSphereRuleBuilder<HARule, HARuleConfiguration> {
@Override
public HARule build(final HARuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public HARule build(final HARuleConfiguration ruleConfig) {
return new HARule(ruleConfig);
}
......
......@@ -47,6 +47,6 @@ public final class AlgorithmProvidedHARuleBuilderTest {
when(algorithmProvidedRuleConfig.getDataSources()).thenReturn(Collections.singletonList(ruleConfig));
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(
Collections.singletonList(algorithmProvidedRuleConfig), ShardingSphereRuleBuilder.class).get(algorithmProvidedRuleConfig);
assertThat(builder.build(algorithmProvidedRuleConfig, Collections.emptyList()), instanceOf(HARule.class));
assertThat(builder.build(algorithmProvidedRuleConfig), instanceOf(HARule.class));
}
}
......@@ -46,6 +46,6 @@ public final class HARuleBuilderTest {
"name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName", true);
when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(dataSourceRuleConfig));
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(HARule.class));
assertThat(builder.build(ruleConfig), instanceOf(HARule.class));
}
}
......@@ -22,15 +22,13 @@ import org.apache.shardingsphere.replicaquery.algorithm.config.AlgorithmProvided
import org.apache.shardingsphere.replicaquery.constant.ReplicaQueryOrder;
import org.apache.shardingsphere.replicaquery.rule.ReplicaQueryRule;
import java.util.Collection;
/**
* Algorithm provided replica query rule builder.
*/
public final class AlgorithmProvidedReplicaQueryRuleBuilder implements ShardingSphereRuleBuilder<ReplicaQueryRule, AlgorithmProvidedReplicaQueryRuleConfiguration> {
@Override
public ReplicaQueryRule build(final AlgorithmProvidedReplicaQueryRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public ReplicaQueryRule build(final AlgorithmProvidedReplicaQueryRuleConfiguration ruleConfig) {
return new ReplicaQueryRule(ruleConfig);
}
......
......@@ -17,20 +17,18 @@
package org.apache.shardingsphere.replicaquery.rule.biulder;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.replicaquery.api.config.ReplicaQueryRuleConfiguration;
import org.apache.shardingsphere.replicaquery.constant.ReplicaQueryOrder;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.replicaquery.rule.ReplicaQueryRule;
import java.util.Collection;
/**
* Replica query rule builder.
*/
public final class ReplicaQueryRuleBuilder implements ShardingSphereRuleBuilder<ReplicaQueryRule, ReplicaQueryRuleConfiguration> {
@Override
public ReplicaQueryRule build(final ReplicaQueryRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public ReplicaQueryRule build(final ReplicaQueryRuleConfiguration ruleConfig) {
return new ReplicaQueryRule(ruleConfig);
}
......
......@@ -47,6 +47,6 @@ public final class AlgorithmProvidedReplicaQueryRuleBuilderTest {
when(algorithmProvidedRuleConfig.getDataSources()).thenReturn(Collections.singletonList(ruleConfig));
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(
Collections.singletonList(algorithmProvidedRuleConfig), ShardingSphereRuleBuilder.class).get(algorithmProvidedRuleConfig);
assertThat(builder.build(algorithmProvidedRuleConfig, Collections.emptyList()), instanceOf(ReplicaQueryRule.class));
assertThat(builder.build(algorithmProvidedRuleConfig), instanceOf(ReplicaQueryRule.class));
}
}
......@@ -46,6 +46,6 @@ public final class ReplicaQueryRuleBuilderTest {
"name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName");
when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(dataSourceRuleConfig));
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(ReplicaQueryRule.class));
assertThat(builder.build(ruleConfig), instanceOf(ReplicaQueryRule.class));
}
}
......@@ -22,15 +22,13 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Collection;
/**
* Shadow rule builder.
*/
public final class ShadowRuleBuilder implements ShardingSphereRuleBuilder<ShadowRule, ShadowRuleConfiguration> {
@Override
public ShadowRule build(final ShadowRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public ShadowRule build(final ShadowRuleConfiguration ruleConfig) {
return new ShadowRule(ruleConfig);
}
......
......@@ -36,10 +36,11 @@ public final class ShadowRuleBuilderTest {
ShardingSphereServiceLoader.register(ShardingSphereRuleBuilder.class);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void assertBuild() {
ShadowRuleConfiguration ruleConfig = mock(ShadowRuleConfiguration.class);
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(ShadowRule.class));
assertThat(builder.build(ruleConfig), instanceOf(ShadowRule.class));
}
}
/*
* 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.metadata;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.SingleTableRule;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
/**
* Single table rule loader.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SingleTableRuleLoader {
/**
* Load single table rules.
*
* @param databaseType database type
* @param dataSourceMap data source map
* @param shardingRule sharding rule
* @return single table rule map
* @throws SQLException SQL exception
*/
@SuppressWarnings("CollectionWithoutInitialCapacity")
public static Map<String, SingleTableRule> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule) throws SQLException {
Collection<String> excludedTables = getExcludedTables(shardingRule);
Map<String, SingleTableRule> result = new HashMap<>();
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
result.putAll(load(databaseType, entry.getKey(), entry.getValue(), excludedTables));
}
return result;
}
private static Map<String, SingleTableRule> load(final DatabaseType databaseType,
final String dataSourceName, final DataSource dataSource, final Collection<String> excludedTables) throws SQLException {
Collection<String> tables = SchemaMetaDataLoader.loadAllTableNames(dataSource, databaseType);
Map<String, SingleTableRule> result = new HashMap<>(tables.size(), 1);
for (String each : tables) {
if (!excludedTables.contains(each)) {
result.put(each, new SingleTableRule(each, dataSourceName));
}
}
return result;
}
private static Collection<String> getExcludedTables(final ShardingRule shardingRule) {
Collection<String> result = new HashSet<>(shardingRule.getTables());
result.addAll(shardingRule.getAllActualTables());
return result;
}
}
......@@ -24,6 +24,7 @@ import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.type.TableContainedRule;
......@@ -43,6 +44,7 @@ import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorith
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
......@@ -84,10 +86,10 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained
private final KeyGenerateAlgorithm defaultKeyGenerateAlgorithm;
public ShardingRule(final ShardingRuleConfiguration config, final Collection<String> dataSourceNames) {
public ShardingRule(final ShardingRuleConfiguration config, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
Preconditions.checkArgument(null != config, "Sharding rule configuration cannot be null.");
Preconditions.checkArgument(null != dataSourceNames && !dataSourceNames.isEmpty(), "Data sources cannot be empty.");
this.dataSourceNames = getDataSourceNames(config.getTables(), dataSourceNames);
Preconditions.checkArgument(null != dataSourceMap && !dataSourceMap.isEmpty(), "Data sources cannot be empty.");
dataSourceNames = getDataSourceNames(config.getTables(), dataSourceMap.keySet());
config.getShardingAlgorithms().forEach((key, value) -> shardingAlgorithms.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, ShardingAlgorithm.class)));
config.getKeyGenerators().forEach((key, value) -> keyGenerators.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, KeyGenerateAlgorithm.class)));
tableRules = new LinkedList<>(createTableRules(config.getTables(), config.getDefaultKeyGenerateStrategy()));
......@@ -100,10 +102,10 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained
? TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) : keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
}
public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration config, final Collection<String> dataSourceNames) {
public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration config, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
Preconditions.checkArgument(null != config, "Sharding rule configuration cannot be null.");
Preconditions.checkArgument(null != dataSourceNames && !dataSourceNames.isEmpty(), "Data sources cannot be empty.");
this.dataSourceNames = getDataSourceNames(config.getTables(), dataSourceNames);
Preconditions.checkArgument(null != dataSourceMap && !dataSourceMap.isEmpty(), "Data sources cannot be empty.");
dataSourceNames = getDataSourceNames(config.getTables(), dataSourceMap.keySet());
shardingAlgorithms.putAll(config.getShardingAlgorithms());
keyGenerators.putAll(config.getKeyGenerators());
tableRules = new LinkedList<>(createTableRules(config.getTables(), config.getDefaultKeyGenerateStrategy()));
......
/*
* 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.rule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* Single table rule.
*/
@RequiredArgsConstructor
@Getter
public final class SingleTableRule {
private final String tableName;
private final String dataSourceName;
}
......@@ -17,21 +17,30 @@
package org.apache.shardingsphere.sharding.rule.builder;
import lombok.Setter;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.infra.rule.builder.aware.ResourceAware;
import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
import java.util.Collection;
import javax.sql.DataSource;
import java.util.Map;
/**
* Algorithm provided sharding rule builder.
*/
public final class AlgorithmProvidedShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, AlgorithmProvidedShardingRuleConfiguration> {
@Setter
public final class AlgorithmProvidedShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, AlgorithmProvidedShardingRuleConfiguration>, ResourceAware {
private DatabaseType databaseType;
private Map<String, DataSource> dataSourceMap;
@Override
public ShardingRule build(final AlgorithmProvidedShardingRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
return new ShardingRule(ruleConfig, dataSourceNames);
public ShardingRule build(final AlgorithmProvidedShardingRuleConfiguration ruleConfig) {
return new ShardingRule(ruleConfig, databaseType, dataSourceMap);
}
@Override
......
......@@ -17,21 +17,30 @@
package org.apache.shardingsphere.sharding.rule.builder;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import lombok.Setter;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.infra.rule.builder.aware.ResourceAware;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Collection;
import javax.sql.DataSource;
import java.util.Map;
/**
* Sharding rule builder.
*/
public final class ShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, ShardingRuleConfiguration> {
@Setter
public final class ShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, ShardingRuleConfiguration>, ResourceAware {
private DatabaseType databaseType;
private Map<String, DataSource> dataSourceMap;
@Override
public ShardingRule build(final ShardingRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
return new ShardingRule(ruleConfig, dataSourceNames);
public ShardingRule build(final ShardingRuleConfiguration ruleConfig) {
return new ShardingRule(ruleConfig, databaseType, dataSourceMap);
}
@Override
......
......@@ -17,6 +17,7 @@
package org.apache.shardingsphere.sharding.rule.builder;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
......@@ -24,6 +25,7 @@ import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShar
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.Test;
import javax.sql.DataSource;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
......@@ -36,10 +38,13 @@ public final class AlgorithmProvidedShardingRuleBuilderTest {
ShardingSphereServiceLoader.register(ShardingSphereRuleBuilder.class);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void assertBuild() {
AlgorithmProvidedShardingRuleConfiguration ruleConfig = mock(AlgorithmProvidedShardingRuleConfiguration.class);
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.singletonList("name")), instanceOf(ShardingRule.class));
((AlgorithmProvidedShardingRuleBuilder) builder).setDatabaseType(mock(DatabaseType.class));
((AlgorithmProvidedShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class)));
assertThat(builder.build(ruleConfig), instanceOf(ShardingRule.class));
}
}
......@@ -17,6 +17,7 @@
package org.apache.shardingsphere.sharding.rule.builder;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
......@@ -24,6 +25,7 @@ import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.Test;
import javax.sql.DataSource;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
......@@ -36,10 +38,13 @@ public final class ShardingRuleBuilderTest {
ShardingSphereServiceLoader.register(ShardingSphereRuleBuilder.class);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void assertBuild() {
ShardingRuleConfiguration ruleConfig = mock(ShardingRuleConfiguration.class);
ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig);
assertThat(builder.build(ruleConfig, Collections.singletonList("name")), instanceOf(ShardingRule.class));
((ShardingRuleBuilder) builder).setDatabaseType(mock(DatabaseType.class));
((ShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class)));
assertThat(builder.build(ruleConfig), instanceOf(ShardingRule.class));
}
}
......@@ -22,12 +22,13 @@ import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine;
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
......@@ -77,9 +78,10 @@ public final class MixSQLRewriterParameterizedTest extends AbstractSQLRewriterPa
@Override
protected Collection<SQLRewriteUnit> createSQLRewriteUnits() throws IOException {
YamlRootRuleConfigurations ruleConfigurations = createRuleConfigurations();
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(
new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(ruleConfigurations.getRules()), ruleConfigurations.getDataSources().keySet());
SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType());
String databaseType = null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType();
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(
ruleConfigurations.getRules()), DatabaseTypeRegistry.getTrunkDatabaseType(databaseType), ruleConfigurations.getDataSources());
SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType);
ShardingSphereSchema schema = mockSchema();
ConfigurationProperties props = new ConfigurationProperties(ruleConfigurations.getProps());
SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
......
......@@ -22,12 +22,13 @@ import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine;
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
......@@ -77,9 +78,10 @@ public final class ShardingSQLRewriterParameterizedTest extends AbstractSQLRewri
@Override
protected Collection<SQLRewriteUnit> createSQLRewriteUnits() throws IOException {
YamlRootRuleConfigurations yamlRootRuleConfigs = createYamlRootRuleConfigurations();
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(
new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(yamlRootRuleConfigs.getRules()), yamlRootRuleConfigs.getDataSources().keySet());
SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType());
String databaseType = null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType();
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(
yamlRootRuleConfigs.getRules()), DatabaseTypeRegistry.getTrunkDatabaseType(databaseType), yamlRootRuleConfigs.getDataSources());
SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType);
ShardingSphereSchema schema = mockSchema();
ConfigurationProperties props = new ConfigurationProperties(yamlRootRuleConfigs.getProps());
SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
......
......@@ -71,7 +71,7 @@ public final class ShardingRouteEngineFactory {
* @return new instance of routing engine
*/
public static ShardingRouteEngine newInstance(final ShardingRule shardingRule, final ShardingSphereMetaData metaData,
final SQLStatementContext sqlStatementContext, final ShardingConditions shardingConditions, final ConfigurationProperties props) {
final SQLStatementContext<?> sqlStatementContext, final ShardingConditions shardingConditions, final ConfigurationProperties props) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
if (sqlStatement instanceof TCLStatement) {
......
......@@ -44,7 +44,7 @@ public final class ShardingTableBroadcastRoutingEngine implements ShardingRouteE
private final ShardingSphereSchema schema;
private final SQLStatementContext sqlStatementContext;
private final SQLStatementContext<?> sqlStatementContext;
@Override
public void route(final RouteContext routeContext, final ShardingRule shardingRule) {
......
......@@ -21,8 +21,6 @@ import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPI;
import java.util.Collection;
/**
* ShardingSphere rule builder.
*
......@@ -35,8 +33,7 @@ public interface ShardingSphereRuleBuilder<R extends ShardingSphereRule, T exten
* Build ShardingSphere rule.
*
* @param ruleConfig rule configuration
* @param dataSourceNames data source names
* @return ShardingSphere rule
*/
R build(T ruleConfig, Collection<String> dataSourceNames);
R build(T ruleConfig);
}
......@@ -20,11 +20,15 @@ package org.apache.shardingsphere.infra.rule.builder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.aware.ResourceAware;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;
/**
......@@ -41,12 +45,24 @@ public final class ShardingSphereRulesBuilder {
* Build rules.
*
* @param ruleConfigurations rule configurations
* @param dataSourceNames data source names
* @param databaseType database type
* @param dataSourceMap data source map
* @return rules
*/
@SuppressWarnings("unchecked")
public static Collection<ShardingSphereRule> build(final Collection<RuleConfiguration> ruleConfigurations, final Collection<String> dataSourceNames) {
return OrderedSPIRegistry.getRegisteredServices(
ruleConfigurations, ShardingSphereRuleBuilder.class).entrySet().stream().map(entry -> entry.getValue().build(entry.getKey(), dataSourceNames)).collect(Collectors.toList());
@SuppressWarnings({"unchecked", "rawtypes"})
public static Collection<ShardingSphereRule> build(final Collection<RuleConfiguration> ruleConfigurations, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
Map<RuleConfiguration, ShardingSphereRuleBuilder> builders = OrderedSPIRegistry.getRegisteredServices(ruleConfigurations, ShardingSphereRuleBuilder.class);
setResources(builders.values(), databaseType, dataSourceMap);
return builders.entrySet().stream().map(entry -> entry.getValue().build(entry.getKey())).collect(Collectors.toList());
}
@SuppressWarnings("rawtypes")
private static void setResources(final Collection<ShardingSphereRuleBuilder> builders, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
for (ShardingSphereRuleBuilder each : builders) {
if (each instanceof ResourceAware) {
((ResourceAware) each).setDatabaseType(databaseType);
((ResourceAware) each).setDataSourceMap(dataSourceMap);
}
}
}
}
/*
* 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.infra.rule.builder.aware;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import javax.sql.DataSource;
import java.util.Map;
/**
* Resource aware.
*/
public interface ResourceAware {
/**
* Set database type.
*
* @param databaseType database type
*/
void setDatabaseType(DatabaseType databaseType);
/**
* Set data source map.
*
* @param dataSourceMap data source map
*/
void setDataSourceMap(Map<String, DataSource> dataSourceMap);
}
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.rule;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRulesBuilder;
import org.apache.shardingsphere.infra.rule.fixture.TestRuleConfiguration;
import org.apache.shardingsphere.infra.rule.fixture.TestShardingSphereRuleBuilder;
......@@ -28,13 +29,14 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
public final class ShardingSphereRulesBuilderTest {
@Test
public void assertBuild() {
RuleConfiguration ruleConfig = new TestRuleConfiguration();
Collection<ShardingSphereRule> shardingSphereRules = ShardingSphereRulesBuilder.build(Collections.singletonList(ruleConfig), Collections.singletonList(""));
assertThat(shardingSphereRules, is(Collections.singletonList(TestShardingSphereRuleBuilder.getShardingSphereRule())));
Collection<ShardingSphereRule> shardingSphereRules = ShardingSphereRulesBuilder.build(Collections.singletonList(ruleConfig), mock(DatabaseType.class), Collections.emptyMap());
assertThat(shardingSphereRules, is(Collections.singletonList(TestShardingSphereRuleBuilder.getRule())));
}
}
......@@ -22,16 +22,14 @@ import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import java.util.Collection;
public final class TestShardingSphereRuleBuilder implements ShardingSphereRuleBuilder {
@Getter
private static ShardingSphereRule shardingSphereRule = new TestShardingSphereRule();
private static ShardingSphereRule rule = new TestShardingSphereRule();
@Override
public ShardingSphereRule build(final RuleConfiguration ruleConfig, final Collection dataSourceNames) {
return shardingSphereRule;
public ShardingSphereRule build(final RuleConfiguration ruleConfig) {
return rule;
}
@Override
......
......@@ -98,7 +98,7 @@ public final class MetaDataContextsBuilder {
private ShardingSphereMetaData buildMetaData(final String schemaName) throws SQLException {
Map<String, DataSource> dataSourceMap = dataSources.get(schemaName);
Collection<RuleConfiguration> ruleConfigs = this.ruleConfigs.get(schemaName);
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(ruleConfigs, dataSourceMap.keySet());
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(ruleConfigs, databaseType, dataSourceMap);
ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(ruleConfigs, rules);
return new ShardingSphereMetaData(schemaName, buildResource(dataSourceMap), ruleMetaData, buildSchema(schemaName, dataSourceMap, rules));
}
......
......@@ -19,12 +19,10 @@ package org.apache.shardingsphere.infra.context.fixture;
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder;
import java.util.Collection;
public final class FixtureRuleBuilder implements ShardingSphereRuleBuilder<FixtureRule, FixtureRuleConfiguration> {
@Override
public FixtureRule build(final FixtureRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) {
public FixtureRule build(final FixtureRuleConfiguration ruleConfig) {
return new FixtureRule();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册