Search in sources :

Example 1 with ShardingDataSource

use of org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource in project chao-cloud by chaojunzi.

the class ShardingActualNodesComplete method sourceOfTableName.

/**
 * 根据表名生成表结构<br>
 * 格式: table:ds.table_202001
 *
 * @param shardingDataSource 数据源
 * @param tableNodes         源表_节点
 * @param defaultDsName      默认数据源
 */
default void sourceOfTableName(ShardingDataSource shardingDataSource, Map<String, List<String>> tableNodes, String defaultDsName) {
    DbType type = DbType.getDbType(shardingDataSource.getDatabaseType().getName());
    switch(type) {
        case // 目前只支持mysql
        MYSQL:
            Map<String, DataSource> dsMap = shardingDataSource.getDataSourceMap();
            // 节点分组
            Map<String, Map<String, List<String>>> dsTableNodes = build(tableNodes);
            // 生成表结构
            dsTableNodes.forEach((dsName, tNodesMap) -> {
                DataSource ds = dsMap.get(dsName);
                List<String> tables = MetaUtil.getTables(ds);
                // 生成数据表
                tNodesMap.forEach((sourceTable, nodes) -> {
                    // 整合需要缓存表节点
                    cacheTableNodes(dsName, sourceTable, nodes, tables);
                    // 需要生成的表
                    Collection<String> needTables = CollUtil.subtract(nodes, tables);
                    // 生成表节点
                    if (CollUtil.isEmpty(needTables)) {
                        return;
                    }
                    String sourceTableDDL = getSourceTableDDL(dsMap.get(defaultDsName), sourceTable);
                    createTable(sourceTable, sourceTableDDL, dsName, ds, needTables);
                });
            });
        default:
            break;
    }
}
Also used : Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DbType(com.baomidou.mybatisplus.annotation.DbType) ShardingDataSource(org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource) DataSource(javax.sql.DataSource)

Example 2 with ShardingDataSource

use of org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource in project chao-cloud by chaojunzi.

the class ShardingActualNodesComplete method refreshDatasource.

/**
 * 动态刷新数据源
 *
 * @param dsName    数据源名称
 * @param tableName 逻辑表
 * @param nodes     真实表
 */
/**
 * 动态刷新数据源
 */
static void refreshDatasource(String dsName, String tableName, Collection<String> nodes) {
    ShardingDataSource shardingDataSource = SpringUtil.getBean(ShardingDataSource.class);
    TableRule tableRule = shardingDataSource.getRuntimeContext().getRule().getTableRule(tableName);
    // 1.动态刷新:actualDataNodes
    List<DataNode> actualDataNodes = tableRule.getActualDataNodes();
    nodes.forEach(n -> {
        DataNode node = new DataNode(dsName, n);
        if (!CollUtil.contains(actualDataNodes, node)) {
            actualDataNodes.add(node);
        }
    });
    // 
    Set<String> actualTables = Sets.newHashSet();
    Map<DataNode, Integer> dataNodeIndexMap = Maps.newHashMap();
    // 修改节点索引
    CollUtil.forEach(actualDataNodes, (n, i) -> {
        actualTables.add(n.getTableName());
        dataNodeIndexMap.put(n, i);
    });
    EntityUtil.setProperty(tableRule, TableRule::getActualDataNodes, actualDataNodes);
    // 2.动态刷新:actualTables
    EntityUtil.setProperty(tableRule, new TypeReference<Set<String>>() {
    }, actualTables);
    // 3.动态刷新:dataNodeIndexMap
    EntityUtil.setProperty(tableRule, new TypeReference<Map<DataNode, Integer>>() {
    }, dataNodeIndexMap);
    // 4.动态刷新:datasourceToTablesMap
    Map<String, Collection<String>> datasourceToTablesMap = EntityUtil.getProperty(tableRule, new TypeReference<Map<String, Collection<String>>>() {
    });
    if (datasourceToTablesMap.containsKey(dsName)) {
        Collection<String> tables = datasourceToTablesMap.get(dsName);
        tables.addAll(nodes);
    } else {
        datasourceToTablesMap.put(dsName, actualTables);
    }
}
Also used : TableRule(org.apache.shardingsphere.core.rule.TableRule) Set(java.util.Set) ShardingDataSource(org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource) DataNode(org.apache.shardingsphere.underlying.common.rule.DataNode) Collection(java.util.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ShardingDataSource (org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource)2 DbType (com.baomidou.mybatisplus.annotation.DbType)1 Collection (java.util.Collection)1 Set (java.util.Set)1 DataSource (javax.sql.DataSource)1 TableRule (org.apache.shardingsphere.core.rule.TableRule)1 DataNode (org.apache.shardingsphere.underlying.common.rule.DataNode)1