Search in sources :

Example 1 with DataNode

use of io.shardingjdbc.core.rule.DataNode in project sharding-jdbc by shardingjdbc.

the class ShardingNamespaceTest method assertTableRuleWithAttributesDataSource.

@Test
public void assertTableRuleWithAttributesDataSource() {
    ShardingRule shardingRule = getShardingRule("tableRuleWithAttributesDataSource");
    assertThat(shardingRule.getTableRules().size(), is(1));
    TableRule tableRule = shardingRule.getTableRules().iterator().next();
    assertThat(tableRule.getLogicTable(), is("t_order"));
    assertThat(tableRule.getActualDataNodes().size(), is(8));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_0")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_1")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_2")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_3")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_0")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_1")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_2")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_3")));
    assertTrue(Arrays.equals(tableRule.getDatabaseShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("standardStrategy", StandardShardingStrategyConfiguration.class).getShardingColumn() }));
    assertTrue(Arrays.equals(tableRule.getTableShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("inlineStrategy", InlineShardingStrategyConfiguration.class).getShardingColumn() }));
    assertThat(tableRule.getGenerateKeyColumn(), is("order_id"));
    assertThat(tableRule.getKeyGenerator().getClass().getName(), is("io.shardingjdbc.spring.fixture.IncrementKeyGenerator"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) Test(org.junit.Test)

Example 2 with DataNode

use of io.shardingjdbc.core.rule.DataNode in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingNamespaceTest method assertDefaultActualDataNodes.

@Test
public void assertDefaultActualDataNodes() {
    ShardingDataSource multiTableRulesDataSource = this.applicationContext.getBean("multiTableRulesDataSource", ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(multiTableRulesDataSource, "shardingContext", true);
    ShardingRule shardingRule = (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
    assertThat(shardingRule.getTableRules().size(), is(2));
    Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
    TableRule orderRule = tableRules.next();
    assertThat(orderRule.getActualDataNodes().size(), is(2));
    assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order")));
    assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order")));
    TableRule orderItemRule = tableRules.next();
    assertThat(orderItemRule.getActualDataNodes().size(), is(2));
    assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_item")));
    assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_item")));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 3 with DataNode

use of io.shardingjdbc.core.rule.DataNode in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingNamespaceTest method assertTableRuleWithAttributesDataSource.

@Test
public void assertTableRuleWithAttributesDataSource() {
    ShardingRule shardingRule = getShardingRule("tableRuleWithAttributesDataSource");
    assertThat(shardingRule.getTableRules().size(), is(1));
    TableRule tableRule = shardingRule.getTableRules().iterator().next();
    assertThat(tableRule.getLogicTable(), is("t_order"));
    assertThat(tableRule.getActualDataNodes().size(), is(8));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_0")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_1")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_2")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_3")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_0")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_1")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_2")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_3")));
    assertTrue(Arrays.equals(tableRule.getDatabaseShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("standardStrategy", StandardShardingStrategyConfiguration.class).getShardingColumn() }));
    assertTrue(Arrays.equals(tableRule.getTableShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("inlineStrategy", InlineShardingStrategyConfiguration.class).getShardingColumn() }));
    assertThat(tableRule.getGenerateKeyColumn(), is("order_id"));
    assertThat(tableRule.getKeyGenerator().getClass().getName(), is(IncrementKeyGenerator.class.getCanonicalName()));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) Test(org.junit.Test)

Example 4 with DataNode

use of io.shardingjdbc.core.rule.DataNode in project sharding-jdbc by shardingjdbc.

the class TableBroadcastRoutingEngine method getAllTableUnits.

private Collection<TableUnit> getAllTableUnits(final String logicTableName) {
    Collection<TableUnit> result = new LinkedList<>();
    TableRule tableRule = shardingRule.getTableRule(logicTableName);
    for (DataNode each : tableRule.getActualDataNodes()) {
        result.add(new TableUnit(each.getDataSourceName(), logicTableName, each.getTableName()));
    }
    return result;
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) DataNode(io.shardingjdbc.core.rule.DataNode) TableUnit(io.shardingjdbc.core.routing.type.TableUnit) LinkedList(java.util.LinkedList)

Example 5 with DataNode

use of io.shardingjdbc.core.rule.DataNode in project sharding-jdbc by shardingjdbc.

the class StandardRoutingEngine method routeTables.

private Collection<DataNode> routeTables(final TableRule tableRule, final String routedDataSource, final List<ShardingValue> tableShardingValues) {
    Collection<String> availableTargetTables = tableRule.getActualTableNames(routedDataSource);
    Collection<String> routedTables = tableShardingValues.isEmpty() ? availableTargetTables : shardingRule.getTableShardingStrategy(tableRule).doSharding(availableTargetTables, tableShardingValues);
    Preconditions.checkState(!routedTables.isEmpty(), "no table route info");
    Collection<DataNode> result = new LinkedList<>();
    for (String each : routedTables) {
        result.add(new DataNode(routedDataSource, each));
    }
    return result;
}
Also used : DataNode(io.shardingjdbc.core.rule.DataNode) LinkedList(java.util.LinkedList)

Aggregations

DataNode (io.shardingjdbc.core.rule.DataNode)7 TableRule (io.shardingjdbc.core.rule.TableRule)6 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)4 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)4 Test (org.junit.Test)4 LinkedList (java.util.LinkedList)3 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)2 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)2 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)2 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)1 SpringShardingDataSource (io.shardingjdbc.spring.datasource.SpringShardingDataSource)1