ShardingComplexRoutingEngine implements ShardingRouteEngine { @Override public void route(final RouteContext routeContext, final ShardingRule shardingRule) { Collection<String> bindingTableNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); Collection<RouteContext> routeContexts = new ArrayList<>(logicTables.size()); for (String each : logicTables) { RouteContext newRouteContext = new RouteContext(); Optional<TableRule> tableRule = shardingRule.findTableRule(each); if (tableRule.isPresent()) { if (!bindingTableNames.contains(each)) { new ShardingStandardRoutingEngine(tableRule.get().getLogicTable(), shardingConditions, props).route(newRouteContext, shardingRule); routeContexts.add(newRouteContext); } shardingRule.findBindingTableRule(each).ifPresent(bindingTableRule -> bindingTableNames.addAll( bindingTableRule.getTableRules().stream().map(TableRule::getLogicTable).collect(Collectors.toList()))); } } if (routeContexts.isEmpty()) { throw new ShardingSphereException("Cannot find table rule and default data source with logic tables: '%s'", logicTables); } if (1 == routeContexts.size()) { RouteContext newRouteContext = routeContexts.iterator().next(); routeContext.getOriginalDataNodes().addAll(newRouteContext.getOriginalDataNodes()); routeContext.getRouteUnits().addAll(newRouteContext.getRouteUnits()); return; } RouteContext newRouteContext = new RouteContext(); new ShardingCartesianRoutingEngine(routeContexts).route(newRouteContext, shardingRule); routeContext.getOriginalDataNodes().addAll(newRouteContext.getOriginalDataNodes()); routeContext.getRouteUnits().addAll(newRouteContext.getRouteUnits()); }  @Override void route(final RouteContext routeContext, final ShardingRule shardingRule);  }
@Test public void assertRoutingForBindingTables() { ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Arrays.asList("t_order", "t_order_item"), createShardingConditions("t_order"), new ConfigurationProperties(new Properties())); RouteContext routeContext = new RouteContext(); complexRoutingEngine.route(routeContext, createBindingShardingRule()); List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits()); assertThat(routeContext.getRouteUnits().size(), is(1)); assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_1")); assertThat(routeUnits.get(0).getTableMappers().size(), is(1)); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getActualName(), is("t_order_1")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_order")); }
@Test public void assertRoutingForShardingTableJoinBroadcastTable() { ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Arrays.asList("t_order", "t_config"), createShardingConditions("t_order"), new ConfigurationProperties(new Properties())); RouteContext routeContext = new RouteContext(); complexRoutingEngine.route(routeContext, createBroadcastShardingRule()); List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits()); assertThat(routeContext.getRouteUnits().size(), is(1)); assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_1")); assertThat(routeUnits.get(0).getTableMappers().size(), is(1)); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getActualName(), is("t_order_1")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_order")); }
@Test(expected = ShardingSphereException.class) public void assertRoutingForNonLogicTable() { ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Collections.emptyList(), createShardingConditions("t_order"), new ConfigurationProperties(new Properties())); RouteContext routeContext = new RouteContext(); complexRoutingEngine.route(routeContext, mock(ShardingRule.class)); }