ShardingUnicastRoutingEngine implements ShardingRouteEngine { @Override public void route(final RouteContext routeContext, final ShardingRule shardingRule) { String dataSourceName = getRandomDataSourceName(shardingRule.getDataSourceNames()); RouteMapper dataSourceMapper = new RouteMapper(dataSourceName, dataSourceName); if (shardingRule.isAllBroadcastTables(logicTables)) { List<RouteMapper> tableMappers = new ArrayList<>(logicTables.size()); for (String each : logicTables) { tableMappers.add(new RouteMapper(each, each)); } routeContext.getRouteUnits().add(new RouteUnit(dataSourceMapper, tableMappers)); } else if (logicTables.isEmpty()) { routeContext.getRouteUnits().add(new RouteUnit(dataSourceMapper, Collections.emptyList())); } else if (1 == logicTables.size()) { String logicTableName = logicTables.iterator().next(); if (!shardingRule.findTableRule(logicTableName).isPresent()) { routeContext.getRouteUnits().add(new RouteUnit(dataSourceMapper, Collections.emptyList())); return; } DataNode dataNode = shardingRule.getDataNode(logicTableName); routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(dataNode.getDataSourceName(), dataNode.getDataSourceName()), Collections.singletonList(new RouteMapper(logicTableName, dataNode.getTableName())))); } else { List<RouteMapper> tableMappers = new ArrayList<>(logicTables.size()); Set<String> availableDatasourceNames = null; boolean first = true; for (String each : logicTables) { TableRule tableRule = shardingRule.getTableRule(each); DataNode dataNode = tableRule.getActualDataNodes().get(0); tableMappers.add(new RouteMapper(each, dataNode.getTableName())); Set<String> currentDataSourceNames = new HashSet<>(tableRule.getActualDatasourceNames().size()); for (DataNode eachDataNode : tableRule.getActualDataNodes()) { currentDataSourceNames.add(eachDataNode.getDataSourceName()); } if (first) { availableDatasourceNames = currentDataSourceNames; first = false; } else { availableDatasourceNames = Sets.intersection(availableDatasourceNames, currentDataSourceNames); } } if (availableDatasourceNames.isEmpty()) { throw new ShardingSphereConfigurationException("Cannot find actual datasource intersection for logic tables: %s", logicTables); } dataSourceName = getRandomDataSourceName(availableDatasourceNames); routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), tableMappers)); } }  @Override void route(final RouteContext routeContext, final ShardingRule shardingRule);  }
@Test public void assertRoutingForShardingTable() { ShardingUnicastRoutingEngine unicastRoutingEngine = new ShardingUnicastRoutingEngine(Collections.singleton("t_order")); RouteContext routeContext = new RouteContext(); unicastRoutingEngine.route(routeContext, shardingRule); assertThat(routeContext.getRouteUnits().size(), is(1)); assertFalse("ds2".equalsIgnoreCase(routeContext.getRouteUnits().iterator().next().getDataSourceMapper().getLogicName())); }
@Test public void assertRoutingForBroadcastTable() { ShardingUnicastRoutingEngine unicastRoutingEngine = new ShardingUnicastRoutingEngine(Collections.singleton("t_config")); RouteContext routeContext = new RouteContext(); unicastRoutingEngine.route(routeContext, shardingRule); assertThat(routeContext.getRouteUnits().size(), is(1)); }
@Test public void assertRoutingForNoTable() { ShardingUnicastRoutingEngine unicastRoutingEngine = new ShardingUnicastRoutingEngine(Collections.emptyList()); RouteContext routeContext = new RouteContext(); unicastRoutingEngine.route(routeContext, shardingRule); assertThat(routeContext.getRouteUnits().size(), is(1)); }
@Test public void assertRoutingForShardingTableAndBroadcastTable() { Set<String> sets = new HashSet<>(); sets.add("t_order"); sets.add("t_config"); ShardingUnicastRoutingEngine unicastRoutingEngine = new ShardingUnicastRoutingEngine(sets); RouteContext routeContext = new RouteContext(); unicastRoutingEngine.route(routeContext, shardingRule); assertThat(routeContext.getRouteUnits().size(), is(1)); }
@Test(expected = ShardingSphereConfigurationException.class) public void assertRouteForWithNoIntersection() { Set<String> sets = new HashSet<>(); sets.add("t_order"); sets.add("t_config"); sets.add("t_product"); ShardingUnicastRoutingEngine unicastRoutingEngine = new ShardingUnicastRoutingEngine(sets); RouteContext routeContext = new RouteContext(); unicastRoutingEngine.route(routeContext, shardingRule); }
@Test public void assertRoutingForTableWithoutTableRule() { ShardingUnicastRoutingEngine unicastRoutingEngine = new ShardingUnicastRoutingEngine(Collections.singleton("t_other")); RouteContext routeContext = new RouteContext(); unicastRoutingEngine.route(routeContext, shardingRule); assertThat(routeContext.getRouteUnits().size(), is(1)); }