Skip to content

Commit

Permalink
Rename RuleIdentifier to RuleAttribute (#30479)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Mar 13, 2024
1 parent 19527a4 commit 59ca481
Show file tree
Hide file tree
Showing 188 changed files with 871 additions and 875 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.broadcast.rule;

import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.rule.identifier.type.datanode.DataNodeRule;
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -27,15 +27,15 @@
import java.util.stream.Collectors;

/**
* Broadcast data node rule.
* Broadcast data node rule attribute.
*/
public final class BroadcastDataNodeRule implements DataNodeRule {
public final class BroadcastDataNodeRuleAttribute implements DataNodeRuleAttribute {

private final Collection<String> tables;

private final Map<String, Collection<DataNode>> tableDataNodes;

public BroadcastDataNodeRule(final Collection<String> dataSourceNames, final Collection<String> tables) {
public BroadcastDataNodeRuleAttribute(final Collection<String> dataSourceNames, final Collection<String> tables) {
this.tables = tables;
tableDataNodes = tables.stream().collect(Collectors.toMap(String::toLowerCase, each -> generateDataNodes(each, dataSourceNames)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import lombok.Getter;
import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RuleIdentifiers;
import org.apache.shardingsphere.infra.rule.identifier.type.datasource.DataSourceMapperRule;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;

import javax.sql.DataSource;
import java.util.Collection;
Expand All @@ -47,30 +47,30 @@ public final class BroadcastRule implements DatabaseRule {

private final Collection<String> dataSourceNames;

private final RuleIdentifiers ruleIdentifiers;
private final RuleAttributes attributes;

public BroadcastRule(final BroadcastRuleConfiguration config, final String databaseName, final Map<String, DataSource> dataSources, final Collection<ShardingSphereRule> builtRules) {
configuration = config;
this.databaseName = databaseName;
dataSourceNames = getAggregatedDataSourceNames(dataSources, builtRules);
tables = createBroadcastTables(config.getTables());
ruleIdentifiers = new RuleIdentifiers(new BroadcastDataNodeRule(dataSourceNames, tables), new BroadcastTableMapperRule(tables));
attributes = new RuleAttributes(new BroadcastDataNodeRuleAttribute(dataSourceNames, tables), new BroadcastTableMapperRuleAttribute(tables));
}

private Collection<String> getAggregatedDataSourceNames(final Map<String, DataSource> dataSources, final Collection<ShardingSphereRule> builtRules) {
Collection<String> result = new LinkedList<>(dataSources.keySet());
for (ShardingSphereRule each : builtRules) {
Optional<DataSourceMapperRule> dataSourceMapperRule = each.getRuleIdentifiers().findIdentifier(DataSourceMapperRule.class);
if (dataSourceMapperRule.isPresent()) {
result = getAggregatedDataSourceNames(result, dataSourceMapperRule.get());
Optional<DataSourceMapperRuleAttribute> ruleAttribute = each.getAttributes().findAttribute(DataSourceMapperRuleAttribute.class);
if (ruleAttribute.isPresent()) {
result = getAggregatedDataSourceNames(result, ruleAttribute.get());
}
}
return result;
}

private Collection<String> getAggregatedDataSourceNames(final Collection<String> dataSourceNames, final DataSourceMapperRule dataSourceMapperRule) {
private Collection<String> getAggregatedDataSourceNames(final Collection<String> dataSourceNames, final DataSourceMapperRuleAttribute ruleAttribute) {
Collection<String> result = new LinkedList<>();
for (Entry<String, Collection<String>> entry : dataSourceMapperRule.getDataSourceMapper().entrySet()) {
for (Entry<String, Collection<String>> entry : ruleAttribute.getDataSourceMapper().entrySet()) {
for (String each : entry.getValue()) {
if (dataSourceNames.contains(each)) {
dataSourceNames.remove(each);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@

package org.apache.shardingsphere.broadcast.rule;

import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.infra.rule.identifier.type.table.TableNamesMapper;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.table.TableNamesMapper;

import java.util.Collection;

/**
* Broadcast table mapper rule.
* Broadcast table mapper rule attribute.
*/
public final class BroadcastTableMapperRule implements TableMapperRule {
public final class BroadcastTableMapperRuleAttribute implements TableMapperRuleAttribute {

private final TableNamesMapper logicalTableMapper;

public BroadcastTableMapperRule(final Collection<String> tables) {
public BroadcastTableMapperRuleAttribute(final Collection<String> tables) {
logicalTableMapper = new TableNamesMapper();
tables.forEach(logicalTableMapper::put);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
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.infra.rule.identifier.type.datanode.DataNodeRule;
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
Expand Down Expand Up @@ -90,7 +90,7 @@ void assertCreateBroadcastRouteContextWithSingleDataSource() throws SQLException
BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class);
when(currentConfig.getTables()).thenReturn(Collections.singleton("t_order"));
BroadcastRule broadcastRule = new BroadcastRule(currentConfig, DefaultDatabase.LOGIC_NAME, Collections.singletonMap("tmp_ds", new MockedDataSource(mockConnection())), Collections.emptyList());
broadcastRule.getRuleIdentifiers().getIdentifier(DataNodeRule.class).getAllDataNodes().put("t_order", Collections.singletonList(createDataNode("tmp_ds")));
broadcastRule.getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().put("t_order", Collections.singletonList(createDataNode("tmp_ds")));
ShardingSphereDatabase database = mockSingleDatabase();
RouteContext routeContext = new BroadcastSQLRouter().createRouteContext(
createQueryContext(), mock(RuleMetaData.class), database, broadcastRule, new ConfigurationProperties(new Properties()), new ConnectionContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class BroadcastTableMapperRuleTest {
class BroadcastTableMapperRuleAttributeTest {

private final BroadcastTableMapperRule tableMapperRule = new BroadcastTableMapperRule(Collections.singleton("foo_tbl"));
private final BroadcastTableMapperRuleAttribute ruleAttribute = new BroadcastTableMapperRuleAttribute(Collections.singleton("foo_tbl"));

@Test
void assertGetLogicTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
assertThat(new LinkedList<>(ruleAttribute.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
}

@Test
void assertGetDistributedTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
assertThat(new LinkedList<>(ruleAttribute.getDistributedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
}

@Test
void assertGetEnhancedTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.emptyList()));
assertThat(new LinkedList<>(ruleAttribute.getEnhancedTableMapper().getTableNames()), is(Collections.emptyList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RuleIdentifiers;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import java.util.LinkedHashMap;
Expand All @@ -48,7 +48,7 @@ public final class EncryptRule implements DatabaseRule {
private final Map<String, EncryptTable> tables;

@Getter
private final RuleIdentifiers ruleIdentifiers;
private final RuleAttributes attributes;

public EncryptRule(final String databaseName, final EncryptRuleConfiguration ruleConfig) {
this.databaseName = databaseName;
Expand All @@ -59,7 +59,7 @@ public EncryptRule(final String databaseName, final EncryptRuleConfiguration rul
each.getColumns().forEach(columnRuleConfig -> checkEncryptorType(columnRuleConfig, encryptors));
tables.put(each.getName().toLowerCase(), new EncryptTable(each, encryptors));
}
ruleIdentifiers = new RuleIdentifiers(new EncryptTableMapperRule(ruleConfig.getTables()));
attributes = new RuleAttributes(new EncryptTableMapperRuleAttribute(ruleConfig.getTables()));
}

private Map<String, EncryptAlgorithm> createEncryptors(final EncryptRuleConfiguration ruleConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
package org.apache.shardingsphere.encrypt.rule;

import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.infra.rule.identifier.type.table.TableNamesMapper;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.table.TableNamesMapper;

import java.util.Collection;

/**
* Encrypt table mapper rule.
* Encrypt table mapper rule attribute.
*/
public final class EncryptTableMapperRule implements TableMapperRule {
public final class EncryptTableMapperRuleAttribute implements TableMapperRuleAttribute {

private final TableNamesMapper logicalTableMapper;

public EncryptTableMapperRule(final Collection<EncryptTableRuleConfiguration> tables) {
public EncryptTableMapperRuleAttribute(final Collection<EncryptTableRuleConfiguration> tables) {
logicalTableMapper = new TableNamesMapper();
tables.stream().map(EncryptTableRuleConfiguration::getName).forEach(logicalTableMapper::put);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class EncryptTableMapperRuleTest {
class EncryptTableMapperRuleAttributeTest {

private final EncryptTableMapperRule tableMapperRule = new EncryptTableMapperRule(Collections.singleton(new EncryptTableRuleConfiguration("foo_tbl", Collections.emptyList())));
private final EncryptTableMapperRuleAttribute ruleAttribute = new EncryptTableMapperRuleAttribute(Collections.singleton(new EncryptTableRuleConfiguration("foo_tbl", Collections.emptyList())));

@Test
void assertGetLogicTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
assertThat(new LinkedList<>(ruleAttribute.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
}

@Test
void assertGetDistributedTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.emptyList()));
assertThat(new LinkedList<>(ruleAttribute.getDistributedTableMapper().getTableNames()), is(Collections.emptyList()));
}

@Test
void assertGetEnhancedTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
assertThat(new LinkedList<>(ruleAttribute.getEnhancedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.distsql.handler.executor.rql.rule.CountResultRowBuilder;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -33,7 +33,7 @@ public final class EncryptCountResultRowBuilder implements CountResultRowBuilder
@Override
public Collection<LocalDataQueryResultRow> generateRows(final EncryptRule rule, final String databaseName) {
return Collections.singleton(
new LocalDataQueryResultRow("encrypt", databaseName, rule.getRuleIdentifiers().getIdentifier(TableMapperRule.class).getLogicTableMapper().getTableNames().size()));
new LocalDataQueryResultRow("encrypt", databaseName, rule.getAttributes().getAttribute(TableMapperRuleAttribute.class).getLogicTableMapper().getTableNames().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package org.apache.shardingsphere.mask.rule;

import lombok.Getter;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RuleIdentifiers;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
Expand All @@ -42,13 +42,13 @@ public final class MaskRule implements DatabaseRule {
private final Map<String, MaskTable> tables = new LinkedHashMap<>();

@Getter
private final RuleIdentifiers ruleIdentifiers;
private final RuleAttributes attributes;

public MaskRule(final MaskRuleConfiguration ruleConfig) {
configuration = ruleConfig;
ruleConfig.getMaskAlgorithms().forEach((key, value) -> maskAlgorithms.put(key, TypedSPILoader.getService(MaskAlgorithm.class, value.getType(), value.getProps())));
ruleConfig.getTables().forEach(each -> tables.put(each.getName().toLowerCase(), new MaskTable(each)));
ruleIdentifiers = new RuleIdentifiers(new MaskTableMapperRule(ruleConfig.getTables()));
attributes = new RuleAttributes(new MaskTableMapperRuleAttribute(ruleConfig.getTables()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

package org.apache.shardingsphere.mask.rule;

import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.infra.rule.identifier.type.table.TableNamesMapper;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.table.TableNamesMapper;
import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;

import java.util.Collection;

/**
* Mask table mapper rule.
* Mask table mapper rule attribute.
*/
public final class MaskTableMapperRule implements TableMapperRule {
public final class MaskTableMapperRuleAttribute implements TableMapperRuleAttribute {

private final TableNamesMapper logicalTableMapper;

public MaskTableMapperRule(final Collection<MaskTableRuleConfiguration> tables) {
public MaskTableMapperRuleAttribute(final Collection<MaskTableRuleConfiguration> tables) {
logicalTableMapper = new TableNamesMapper();
tables.stream().map(MaskTableRuleConfiguration::getName).forEach(logicalTableMapper::put);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class MaskTableMapperRuleTest {
class MaskTableMapperRuleAttributeTest {

private final MaskTableMapperRule tableMapperRule = new MaskTableMapperRule(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList())));
private final MaskTableMapperRuleAttribute ruleAttribute = new MaskTableMapperRuleAttribute(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList())));

@Test
void assertGetLogicTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
assertThat(new LinkedList<>(ruleAttribute.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl")));
}

@Test
void assertGetDistributedTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.emptyList()));
assertThat(new LinkedList<>(ruleAttribute.getDistributedTableMapper().getTableNames()), is(Collections.emptyList()));
}

@Test
void assertGetEnhancedTableMapper() {
assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.emptyList()));
assertThat(new LinkedList<>(ruleAttribute.getEnhancedTableMapper().getTableNames()), is(Collections.emptyList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
Expand Down
Loading

0 comments on commit 59ca481

Please sign in to comment.