public static Map<DataSourceWrapper, List<ShardRouteInfo>> groupRouteInfosByDataSource(DDRDataSource ddrDataSource,
                                                                                           boolean readOnly,
                                                                                           List<ShardRouteInfo> routeInfos) {
        if (routeInfos == null || routeInfos.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        Map<DataSourceWrapper, List<ShardRouteInfo>> result = new LinkedHashMap<>();
        Map<String, DataSourceWrapper> dataSourceWrapperMap = new HashMap<>();
        for (ShardRouteInfo routeInfo : routeInfos) {
            DataSourceWrapper dataSourceWrapper = dataSourceWrapperMap.get(routeInfo.getScName());
            if (dataSourceWrapper == null) {
                DataSourceParam param = new DataSourceParam();
                Set<String> schemas = new HashSet<>();
                schemas.add(routeInfo.getScName());
                param.setScNames(schemas);
                param.setReadOnly(readOnly);
                dataSourceWrapper = ddrDataSource.getDataSource(param);
                for (String scName : dataSourceWrapper.getSchemas()) {
                    Object preValue = dataSourceWrapperMap.put(scName, dataSourceWrapper);
                    if (preValue != null) {
                        throw new IllegalStateException("Duplicate dataSource binding on schema: " + scName);
                    }
                }
            }
            List<ShardRouteInfo> list = result.get(dataSourceWrapper);
            if (list == null) {
                list = new ArrayList<>();
                result.put(dataSourceWrapper, list);
            }
            list.add(routeInfo);
        }
        return result;
    }
public static Map < DataSourceWrapper , List < ShardRouteInfo > > groupRouteInfosByDataSource ( DDRDataSource ddrDataSource , boolean readOnly , List < ShardRouteInfo > routeInfos ) { if ( routeInfos == null || routeInfos . isEmpty ( ) ) { return Collections . EMPTY_MAP ; } Map < DataSourceWrapper , List < ShardRouteInfo > > result = new LinkedHashMap < > ( ) ; Map < String , DataSourceWrapper > dataSourceWrapperMap = new HashMap < > ( ) ; for ( ShardRouteInfo routeInfo : routeInfos ) { DataSourceWrapper dataSourceWrapper = dataSourceWrapperMap . get ( routeInfo . getScName ( ) ) ; if ( dataSourceWrapper == null ) { DataSourceParam param = new DataSourceParam ( ) ; Set < String > schemas = new HashSet < > ( ) ; schemas . add ( routeInfo . getScName ( ) ) ; param . setScNames ( schemas ) ; param . setReadOnly ( readOnly ) ; dataSourceWrapper = ddrDataSource . getDataSource ( param ) ; for ( String scName : dataSourceWrapper . getSchemas ( ) ) { Object preValue = dataSourceWrapperMap . put ( scName , dataSourceWrapper ) ; if ( preValue != null ) { throw new IllegalStateException ( "Duplicate dataSource binding on schema: " + scName ) ; } } } List < ShardRouteInfo > list = result . get ( dataSourceWrapper ) ; if ( list == null ) { list = new ArrayList < > ( ) ; result . put ( dataSourceWrapper , list ) ; } list . add ( routeInfo ) ; } return result ; }
该方法通过把分片信息按数据源进行分组用以解决分布式事务的问题