91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么理解sharding-jdbc的ShardingMasterSlaveRouter

發布時間:2021-10-21 09:13:15 來源:億速云 閱讀:133 作者:柒染 欄目:大數據

本篇文章給大家分享的是有關怎么理解sharding-jdbc的ShardingMasterSlaveRouter,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

主要研究一下sharding-jdbc的ShardingMasterSlaveRouter

ShardingMasterSlaveRouter

incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouter.java

@RequiredArgsConstructor
public final class ShardingMasterSlaveRouter {
    
    private final Collection<MasterSlaveRule> masterSlaveRules;
    
    /**
     * Route Master slave after sharding.
     * 
     * @param sqlRouteResult SQL route result
     * @return route result
     */
    public SQLRouteResult route(final SQLRouteResult sqlRouteResult) {
        for (MasterSlaveRule each : masterSlaveRules) {
            route(each, sqlRouteResult);
        }
        return sqlRouteResult;
    }
    
    private void route(final MasterSlaveRule masterSlaveRule, final SQLRouteResult sqlRouteResult) {
        Collection<TableUnit> toBeRemoved = new LinkedList<>();
        Collection<TableUnit> toBeAdded = new LinkedList<>();
        for (TableUnit each : sqlRouteResult.getRoutingResult().getTableUnits().getTableUnits()) {
            if (!masterSlaveRule.getName().equalsIgnoreCase(each.getDataSourceName())) {
                continue;
            }
            toBeRemoved.add(each);
            String actualDataSourceName;
            if (isMasterRoute(sqlRouteResult.getSqlStatement().getType())) {
                MasterVisitedManager.setMasterVisited();
                actualDataSourceName = masterSlaveRule.getMasterDataSourceName();
            } else {
                actualDataSourceName = masterSlaveRule.getLoadBalanceAlgorithm().getDataSource(
                        masterSlaveRule.getName(), masterSlaveRule.getMasterDataSourceName(), new ArrayList<>(masterSlaveRule.getSlaveDataSourceNames()));
            }
            toBeAdded.add(createNewTableUnit(actualDataSourceName, each));
        }
        sqlRouteResult.getRoutingResult().getTableUnits().getTableUnits().removeAll(toBeRemoved);
        sqlRouteResult.getRoutingResult().getTableUnits().getTableUnits().addAll(toBeAdded);
    }
    
    private boolean isMasterRoute(final SQLType sqlType) {
        return SQLType.DQL != sqlType || MasterVisitedManager.isMasterVisited() || HintManager.isMasterRouteOnly();
    }
    
    private TableUnit createNewTableUnit(final String actualDataSourceName, final TableUnit originalTableUnit) {
        TableUnit result = new TableUnit(actualDataSourceName, originalTableUnit.getDataSourceName());
        result.getRoutingTables().addAll(originalTableUnit.getRoutingTables());
        return result;
    }
}
  • ShardingMasterSlaveRouter提供了route方法,返回sqlRouteResult,這里修改了sqlRouteResult的routingResult的tableUnits的TableUnit集合

SQLRouteResult

incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLRouteResult.java

@RequiredArgsConstructor
@Getter
@Setter
public final class SQLRouteResult {
    
    private final SQLStatement sqlStatement;
    
    private final GeneratedKey generatedKey;
    
    // For multiple thread read cached sqlStatement, clone limit on SQLRouteResult, because limit will be modified after cache
    // TODO need more good design here
    private Limit limit;
    
    private RoutingResult routingResult;
    
    private OptimizeResult optimizeResult;
    
    private final Collection<RouteUnit> routeUnits = new LinkedHashSet<>();
    
    public SQLRouteResult(final SQLStatement sqlStatement) {
        this(sqlStatement, null);
    }
}
  • SQLRouteResult擁有RoutingResult屬性

RoutingResult

incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/RoutingResult.java

@Getter
public class RoutingResult {
    
    private final TableUnits tableUnits = new TableUnits();
    
    /**
     * Judge is route for single database and table only or not.
     *
     * @return is route for single database and table only or not
     */
    public boolean isSingleRouting() {
        return 1 == tableUnits.getTableUnits().size();
    }
}
  • RoutingResult擁有TableUnits屬性

TableUnits

incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/TableUnits.java

@Getter
@ToString
public final class TableUnits {
    
    private final Collection<TableUnit> tableUnits = new LinkedHashSet<>();
    
    /**
     * Get all data source names.
     *
     * @return all data source names
     */
    public Collection<String> getDataSourceNames() {
        Collection<String> result = new HashSet<>(tableUnits.size(), 1);
        for (TableUnit each : tableUnits) {
            result.add(each.getDataSourceName());
        }
        return result;
    }
    
    /**
     * Get routing table via data source name and actual table name.
     *
     * @param dataSourceName data source name
     * @param actualTableName actual table name
     * @return routing table
     */
    public Optional<RoutingTable> getRoutingTable(final String dataSourceName, final String actualTableName) {
        for (TableUnit each : tableUnits) {
            Optional<RoutingTable> result = each.getRoutingTable(dataSourceName, actualTableName);
            if (result.isPresent()) {
                return result;
            }
        }
        return Optional.absent();
    }
    
    /**
     * Get actual tables group via data source name and logic tables' names.
     * <p>
     * Actual tables in same group are belong one logic name. 
     * </p>
     *
     * @param dataSourceName data source name
     * @param logicTableNames logic tables' names
     * @return actual tables group
     */
    public List<Set<String>> getActualTableNameGroups(final String dataSourceName, final Set<String> logicTableNames) {
        List<Set<String>> result = new ArrayList<>();
        for (String logicTableName : logicTableNames) {
            Set<String> actualTableNames = getActualTableNames(dataSourceName, logicTableName);
            if (!actualTableNames.isEmpty()) {
                result.add(actualTableNames);
            }
        }
        return result;
    }
    
    private Set<String> getActualTableNames(final String dataSourceName, final String logicTableName) {
        Set<String> result = new HashSet<>(tableUnits.size(), 1);
        for (TableUnit each : tableUnits) {
            result.addAll(each.getActualTableNames(dataSourceName, logicTableName));
        }
        return result;
    }
    
    /**
     * Get map relationship between data source and logic tables via data sources' names.
     *
     * @param dataSourceNames data sources' names
     * @return  map relationship between data source and logic tables
     */
    public Map<String, Set<String>> getDataSourceLogicTablesMap(final Collection<String> dataSourceNames) {
        Map<String, Set<String>> result = new HashMap<>();
        for (String each : dataSourceNames) {
            Set<String> logicTableNames = getLogicTableNames(each);
            if (!logicTableNames.isEmpty()) {
                result.put(each, logicTableNames);
            }
        }
        return result;
    }
    
    private Set<String> getLogicTableNames(final String dataSourceName) {
        Set<String> result = new HashSet<>(tableUnits.size(), 1);
        for (TableUnit each : tableUnits) {
            if (each.getDataSourceName().equalsIgnoreCase(dataSourceName)) {
                result.addAll(each.getLogicTableNames(dataSourceName));
            }
        }
        return result;
    }
}
  • TableUnits內部是TableUnit的集合

TableUnit

incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/TableUnit.java

@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class TableUnit {
    
    private final String dataSourceName;
    
    private final String masterSlaveLogicDataSourceName;
    
    private final List<RoutingTable> routingTables = new LinkedList<>();
    
    public TableUnit(final String dataSourceName) {
        this.dataSourceName = dataSourceName;
        masterSlaveLogicDataSourceName = dataSourceName;
    }
    
    /**
     * Get routing table via data source name and actual table name.
     *
     * @param dataSourceName data source name
     * @param actualTableName actual table name
     * @return routing table
     */
    public Optional<RoutingTable> getRoutingTable(final String dataSourceName, final String actualTableName) {
        for (RoutingTable each : routingTables) {
            if (dataSourceName.equalsIgnoreCase(masterSlaveLogicDataSourceName) && each.getActualTableName().equalsIgnoreCase(actualTableName)) {
                return Optional.of(each);
            }
        }
        return Optional.absent();
    }
    
    /**
     * Get actual tables' names via data source name.
     *
     * @param dataSourceName data source name
     * @param logicTableName logic table name
     * @return  actual tables' names
     */
    public Set<String> getActualTableNames(final String dataSourceName, final String logicTableName) {
        Set<String> result = new HashSet<>(routingTables.size(), 1);
        for (RoutingTable each : routingTables) {
            if (dataSourceName.equalsIgnoreCase(this.dataSourceName) && each.getLogicTableName().equalsIgnoreCase(logicTableName)) {
                result.add(each.getActualTableName());
            }
        }
        return result;
    }
    
    /**
     * Get logic tables' names via data source name.
     *
     * @param dataSourceName data source name
     * @return  logic tables' names
     */
    public Set<String> getLogicTableNames(final String dataSourceName) {
        Set<String> result = new HashSet<>(routingTables.size(), 1);
        for (RoutingTable each : routingTables) {
            if (dataSourceName.equalsIgnoreCase(this.dataSourceName)) {
                result.add(each.getLogicTableName());
            }
        }
        return result;
    }
}
  • TableUnit包含了RoutingTable的集合

RoutingTable

incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/RoutingTable.java

@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class RoutingTable {
    
    private final String logicTableName;
    
    private final String actualTableName;
}
  • RoutingTable包含了logicTableName與actualTableName

小結

ShardingMasterSlaveRouter提供了route方法,返回sqlRouteResult,這里修改了sqlRouteResult的routingResult的tableUnits的TableUnit集合

以上就是怎么理解sharding-jdbc的ShardingMasterSlaveRouter,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

汝南县| 化德县| 克什克腾旗| 吴忠市| 克拉玛依市| 祁东县| 林周县| 靖安县| 福贡县| 鄂托克旗| 丹东市| 平谷区| 涡阳县| 琼海市| 宣恩县| 达拉特旗| 桃江县| 阳春市| 长春市| 安龙县| 沙田区| 台州市| 邵阳县| 甘孜县| 涿鹿县| 安达市| 连云港市| 丹江口市| 徐闻县| 尼玛县| 绥宁县| 阳江市| 涡阳县| 东丰县| 大石桥市| 芜湖市| 兰坪| 云安县| 伽师县| 荣成市| 辽源市|