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

Refactor table addressing mapper (#8102)

* Remove TableAddressingMetaData

* Rename TableAddressingMapperDataLoader

* Rename TableAddressingMapperDecorator
上级 a6b2375c
......@@ -18,32 +18,33 @@
package org.apache.shardingsphere.sharding.metadata;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.metadata.schema.model.addressing.TableAddressingMetaData;
import org.apache.shardingsphere.infra.metadata.schema.loader.spi.TableAddressingMetaDataDecorator;
import org.apache.shardingsphere.infra.metadata.schema.loader.spi.TableAddressingMapperDecorator;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Table addressing meta data decorator of sharding.
* Table addressing mapper decorator of sharding.
*/
public final class ShardingTableAddressingMetaDataDecorator implements TableAddressingMetaDataDecorator<ShardingRule> {
public final class ShardingTableAddressingMapperDecorator implements TableAddressingMapperDecorator<ShardingRule> {
@Override
public void decorate(final ShardingRule rule, final TableAddressingMetaData metaData) {
rule.getTableRules().forEach(each -> decorate(each, metaData));
public void decorate(final ShardingRule rule, final Map<String, Collection<String>> tableAddressingMapper) {
rule.getTableRules().forEach(each -> decorate(each, tableAddressingMapper));
}
private void decorate(final TableRule tableRule, final TableAddressingMetaData metaData) {
private void decorate(final TableRule tableRule, final Map<String, Collection<String>> tableDataSourceNamesMapper) {
boolean found = false;
for (String each : tableRule.getActualDataNodes().stream().map(DataNode::getTableName).collect(Collectors.toSet())) {
found = null != metaData.getTableDataSourceNamesMapper().remove(each) || found;
found = null != tableDataSourceNamesMapper.remove(each) || found;
}
if (found) {
metaData.getTableDataSourceNamesMapper().put(tableRule.getLogicTable(), new LinkedList<>(tableRule.getActualDatasourceNames()));
tableDataSourceNamesMapper.put(tableRule.getLogicTable(), new LinkedList<>(tableRule.getActualDatasourceNames()));
}
}
......
......@@ -301,7 +301,7 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
}
private ShardingSphereMetaData getChangedMetaData(final ShardingSphereMetaData oldMetaData, final ShardingSphereSchema schema, final String schemaName) {
// TODO refresh tableAddressingMetaData
// TODO refresh table addressing mapper
return new ShardingSphereMetaData(schemaName, oldMetaData.getResource(), oldMetaData.getRuleMetaData(), schema);
}
......
......@@ -21,7 +21,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.loader.addressing.TableAddressingMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.loader.addressing.TableAddressingMapperDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.physical.PhysicalTableMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
......@@ -51,13 +51,13 @@ public final class SchemaMetaDataLoader {
*/
public static ShardingSphereSchema load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) throws SQLException {
ShardingSphereSchema result = loadSchemaMetaData(databaseType, dataSourceMap, rules, props);
setAddressingDataSources(databaseType, dataSourceMap, rules, result);
ShardingSphereSchema result = loadSchema(databaseType, dataSourceMap, rules, props);
setTableAddressingMapper(databaseType, dataSourceMap, rules, result);
return result;
}
private static ShardingSphereSchema loadSchemaMetaData(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) throws SQLException {
private static ShardingSphereSchema loadSchema(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) throws SQLException {
ShardingSphereSchema result = new ShardingSphereSchema();
for (ShardingSphereRule rule : rules) {
if (rule instanceof TableContainedRule) {
......@@ -71,9 +71,9 @@ public final class SchemaMetaDataLoader {
return result;
}
private static void setAddressingDataSources(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
private static void setTableAddressingMapper(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final Collection<ShardingSphereRule> rules, final ShardingSphereSchema schema) throws SQLException {
for (Entry<String, Collection<String>> entry : TableAddressingMetaDataLoader.load(databaseType, dataSourceMap, rules).getTableDataSourceNamesMapper().entrySet()) {
for (Entry<String, Collection<String>> entry : TableAddressingMapperDataLoader.load(databaseType, dataSourceMap, rules).entrySet()) {
String tableName = entry.getKey();
if (!schema.containsTable(tableName)) {
schema.put(tableName, new PhysicalTableMetaData());
......
......@@ -20,9 +20,8 @@ package org.apache.shardingsphere.infra.metadata.schema.loader.addressing;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.model.addressing.TableAddressingMetaData;
import org.apache.shardingsphere.infra.metadata.schema.loader.physical.PhysicalSchemaMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.loader.spi.TableAddressingMetaDataDecorator;
import org.apache.shardingsphere.infra.metadata.schema.loader.spi.TableAddressingMapperDecorator;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
......@@ -31,52 +30,54 @@ import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
/**
* Table addressing meta data loader.
* Table addressing mapper loader.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TableAddressingMetaDataLoader {
public final class TableAddressingMapperDataLoader {
static {
ShardingSphereServiceLoader.register(TableAddressingMetaDataDecorator.class);
ShardingSphereServiceLoader.register(TableAddressingMapperDecorator.class);
}
/**
* Load table addressing meta data.
* Load table addressing mapper with related data sources.
*
* @param databaseType database type
* @param dataSourceMap data source map
* @param rules ShardingSphere rules
* @return table addressing meta data
* @return table addressing mapper
* @throws SQLException SQL exception
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static TableAddressingMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> rules) throws SQLException {
TableAddressingMetaData result = initializeMetaData(databaseType, dataSourceMap);
for (Entry<ShardingSphereRule, TableAddressingMetaDataDecorator> entry : OrderedSPIRegistry.getRegisteredServices(rules, TableAddressingMetaDataDecorator.class).entrySet()) {
public static Map<String, Collection<String>> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> rules) throws SQLException {
Map<String, Collection<String>> result = initializeTableAddressingMapper(databaseType, dataSourceMap);
for (Entry<ShardingSphereRule, TableAddressingMapperDecorator> entry : OrderedSPIRegistry.getRegisteredServices(rules, TableAddressingMapperDecorator.class).entrySet()) {
entry.getValue().decorate(entry.getKey(), result);
}
return result;
}
private static TableAddressingMetaData initializeMetaData(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) throws SQLException {
TableAddressingMetaData result = new TableAddressingMetaData();
private static Map<String, Collection<String>> initializeTableAddressingMapper(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) throws SQLException {
Map<String, Collection<String>> result = new HashMap<>();
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
appendMetaData(result, databaseType, entry.getKey(), entry.getValue());
}
return result;
}
private static void appendMetaData(final TableAddressingMetaData metaData, final DatabaseType databaseType, final String dataSourceName, final DataSource dataSource) throws SQLException {
private static void appendMetaData(final Map<String, Collection<String>> tableAddressingMapper,
final DatabaseType databaseType, final String dataSourceName, final DataSource dataSource) throws SQLException {
for (String each : PhysicalSchemaMetaDataLoader.loadTableNames(dataSource, databaseType, Collections.emptyList())) {
if (!metaData.getTableDataSourceNamesMapper().containsKey(each)) {
metaData.getTableDataSourceNamesMapper().put(each, new LinkedHashSet<>());
if (!tableAddressingMapper.containsKey(each)) {
tableAddressingMapper.put(each, new LinkedHashSet<>());
}
metaData.getTableDataSourceNamesMapper().get(each).add(dataSourceName);
tableAddressingMapper.get(each).add(dataSourceName);
}
}
}
......@@ -17,22 +17,24 @@
package org.apache.shardingsphere.infra.metadata.schema.loader.spi;
import org.apache.shardingsphere.infra.metadata.schema.model.addressing.TableAddressingMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPI;
import java.util.Collection;
import java.util.Map;
/**
* Table addressing meta data decorator.
* Table addressing mapper decorator.
*
* @param <T> type of ShardingSphere rule
*/
public interface TableAddressingMetaDataDecorator<T extends ShardingSphereRule> extends OrderedSPI<T> {
public interface TableAddressingMapperDecorator<T extends ShardingSphereRule> extends OrderedSPI<T> {
/**
* Build ShardingSphere rule.
* Decorate table addressing mapper with data source names.
*
* @param rule ShardingSphere rule
* @param metaData table addressing meta data
* @param tableAddressingMapper decorated table addressing mapper
*/
void decorate(T rule, TableAddressingMetaData metaData);
void decorate(T rule, Map<String, Collection<String>> tableAddressingMapper);
}
/*
* 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.metadata.schema.model.addressing;
import lombok.Getter;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Table addressing meta data.
*/
@Getter
public final class TableAddressingMetaData {
private final Map<String, Collection<String>> tableDataSourceNamesMapper = new ConcurrentHashMap<>();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册