ShardingStandardRoutingEngine implements ShardingRouteEngine { @Override public void route(final RouteContext routeContext, final ShardingRule shardingRule) { Collection<DataNode> dataNodes = getDataNodes(shardingRule, shardingRule.getTableRule(logicTableName)); routeContext.getOriginalDataNodes().addAll(originalDataNodes); for (DataNode each : dataNodes) { routeContext.getRouteUnits().add( new RouteUnit(new RouteMapper(each.getDataSourceName(), each.getDataSourceName()), Collections.singletonList(new RouteMapper(logicTableName, each.getTableName())))); } }  @Override void route(final RouteContext routeContext, final ShardingRule shardingRule);  }
@Test public void assertRouteByNonConditions() { ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_order", new ShardingConditions(Collections.emptyList())); RouteContext routeContext = new RouteContext(); standardRoutingEngine.route(routeContext, createBasedShardingRule()); List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits()); assertThat(routeContext.getRouteUnits().size(), is(4)); assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0")); assertThat(routeUnits.get(0).getTableMappers().size(), is(1)); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getActualName(), is("t_order_0")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_order")); assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(), is("ds_0")); assertThat(routeUnits.get(1).getTableMappers().size(), is(1)); assertThat(routeUnits.get(1).getTableMappers().iterator().next().getActualName(), is("t_order_1")); assertThat(routeUnits.get(1).getTableMappers().iterator().next().getLogicName(), is("t_order")); assertThat(routeUnits.get(2).getDataSourceMapper().getActualName(), is("ds_1")); assertThat(routeUnits.get(2).getTableMappers().size(), is(1)); assertThat(routeUnits.get(2).getTableMappers().iterator().next().getActualName(), is("t_order_0")); assertThat(routeUnits.get(2).getTableMappers().iterator().next().getLogicName(), is("t_order")); assertThat(routeUnits.get(3).getDataSourceMapper().getActualName(), is("ds_1")); assertThat(routeUnits.get(3).getTableMappers().size(), is(1)); assertThat(routeUnits.get(3).getTableMappers().iterator().next().getActualName(), is("t_order_1")); assertThat(routeUnits.get(3).getTableMappers().iterator().next().getLogicName(), is("t_order")); }
@Test public void assertRouteByShardingConditions() { ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_order", createShardingConditions("t_order")); RouteContext routeContext = new RouteContext(); standardRoutingEngine.route(routeContext, createBasedShardingRule()); 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 assertRouteByHint() { ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_hint_test", new ShardingConditions(Collections.emptyList())); HintManager hintManager = HintManager.getInstance(); hintManager.addDatabaseShardingValue("t_hint_test", 1); hintManager.addTableShardingValue("t_hint_test", 1); RouteContext routeContext = new RouteContext(); standardRoutingEngine.route(routeContext, createHintShardingRule()); 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_hint_test_1")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_hint_test")); }
@Test public void assertRouteByMixedWithHintDatasource() { ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_hint_ds_test", createShardingConditions("t_hint_ds_test")); HintManager hintManager = HintManager.getInstance(); hintManager.addDatabaseShardingValue("t_hint_ds_test", 1); RouteContext routeContext = new RouteContext(); standardRoutingEngine.route(routeContext, createMixedShardingRule()); 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_hint_ds_test_1")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_hint_ds_test")); }
@Test public void assertRouteByMixedWithHintDatasourceOnly() { ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_hint_ds_test", new ShardingConditions(Collections.emptyList())); HintManager hintManager = HintManager.getInstance(); hintManager.addDatabaseShardingValue("t_hint_ds_test", 1); RouteContext routeContext = new RouteContext(); standardRoutingEngine.route(routeContext, createMixedShardingRule()); List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits()); assertThat(routeContext.getRouteUnits().size(), is(2)); 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_hint_ds_test_0")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_hint_ds_test")); assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(), is("ds_1")); assertThat(routeUnits.get(1).getTableMappers().size(), is(1)); assertThat(routeUnits.get(1).getTableMappers().iterator().next().getActualName(), is("t_hint_ds_test_1")); assertThat(routeUnits.get(1).getTableMappers().iterator().next().getLogicName(), is("t_hint_ds_test")); }
@Test public void assertRouteByMixedWithHintTable() { ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_hint_table_test", createShardingConditions("t_hint_table_test")); HintManager hintManager = HintManager.getInstance(); hintManager.addTableShardingValue("t_hint_table_test", 1); RouteContext routeContext = new RouteContext(); standardRoutingEngine.route(routeContext, createMixedShardingRule()); 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_hint_table_test_1")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_hint_table_test")); }
@Test public void assertRouteByMixedWithHintTableOnly() { ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_hint_table_test", new ShardingConditions(Collections.emptyList())); HintManager hintManager = HintManager.getInstance(); hintManager.addTableShardingValue("t_hint_table_test", 1); RouteContext routeContext = new RouteContext(); standardRoutingEngine.route(routeContext, createMixedShardingRule()); List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits()); assertThat(routeContext.getRouteUnits().size(), is(2)); assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0")); assertThat(routeUnits.get(0).getTableMappers().size(), is(1)); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getActualName(), is("t_hint_table_test_1")); assertThat(routeUnits.get(0).getTableMappers().iterator().next().getLogicName(), is("t_hint_table_test")); assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(), is("ds_1")); assertThat(routeUnits.get(1).getTableMappers().size(), is(1)); assertThat(routeUnits.get(1).getTableMappers().iterator().next().getActualName(), is("t_hint_table_test_1")); assertThat(routeUnits.get(1).getTableMappers().iterator().next().getLogicName(), is("t_hint_table_test")); }