// Path: src/main/java/org/easyarch/test/sharding/algorithm/ModuloDatabaseShardingAlgorithm.java
// public class ModuloDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Integer> {
//     @Override
//     public String doEqualSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         System.out.println("datasource:"+dataSourceNames+" , ShardingValue:"+shardingValue);
//         for (String each : dataSourceNames) {
//             if (each.endsWith(shardingValue.getValue() % 2 + "")) {
//                 return each;
//             }
//         }
//         throw new IllegalArgumentException();
//     }
// 
//     @Override
//     public Collection<String> doInSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         for (Integer value : shardingValue.getValues()) {
//             for (String dataSourceName : dataSourceNames) {
//                 if (dataSourceName.endsWith(value % 2 + "")) {
//                     result.add(dataSourceName);
//                 }
//             }
//         }
//         return result;
//     }
// 
//     @Override
//     public Collection<String> doBetweenSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         Range<Integer> range = shardingValue.getValueRange();
//         for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
//             for (String each : dataSourceNames) {
//                 if (each.endsWith(i % 2 + "")) {
//                     result.add(each);
//                 }
//             }
//         }
//         return result;
//     }
// }
// 
// Path: src/main/java/org/easyarch/test/sharding/algorithm/ModuloTableShardingAlgorithm.java
// public class ModuloTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<Integer> {
//     @Override
//     public String doEqualSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         for (String each : dataSourceNames) {
//             if (each.endsWith(shardingValue.getValue() % 2 + "")) {
//                 return each;
//             }
//         }
//         throw new IllegalArgumentException();
//     }
// 
//     @Override
//     public Collection<String> doInSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         for (Integer value : shardingValue.getValues()) {
//             for (String dataSourceName : dataSourceNames) {
//                 if (dataSourceName.endsWith(value % 2 + "")) {
//                     result.add(dataSourceName);
//                 }
//             }
//         }
//         return result;
//     }
// 
//     @Override
//     public Collection<String> doBetweenSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         Range<Integer> range = shardingValue.getValueRange();
//         for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
//             for (String each : dataSourceNames) {
//                 if (each.endsWith(i % 2 + "")) {
//                     result.add(each);
//                 }
//             }
//         }
//         return result;
//     }
// }


import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.easyarch.test.sharding.algorithm.ModuloDatabaseShardingAlgorithm;
import org.easyarch.test.sharding.algorithm.ModuloTableShardingAlgorithm;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
package org.easyarch.test.sharding;



/**
 * Description :
 * Created by xingtianyu on 17-2-21
 * 上午10:11
 * description:
 */

public class ShardingTest {

    private static ShardingDataSource getShardingDataSource() {
        DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
        TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList(
                "db0.t_order_0", "db0.t_order_1",
                "db1.t_order_0", "db1.t_order_1"
        )).dataSourceRule(dataSourceRule).build();
        TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList(
                "db0.t_order_item_0", "db0.t_order_item_1",
                "db1.t_order_item_0", "db1.t_order_item_1"
        )).dataSourceRule(dataSourceRule).build();
        ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule))
                .bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
                .databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm()))
// Path: src/main/java/org/easyarch/test/sharding/algorithm/ModuloDatabaseShardingAlgorithm.java
// public class ModuloDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Integer> {
//     @Override
//     public String doEqualSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         System.out.println("datasource:"+dataSourceNames+" , ShardingValue:"+shardingValue);
//         for (String each : dataSourceNames) {
//             if (each.endsWith(shardingValue.getValue() % 2 + "")) {
//                 return each;
//             }
//         }
//         throw new IllegalArgumentException();
//     }
// 
//     @Override
//     public Collection<String> doInSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         for (Integer value : shardingValue.getValues()) {
//             for (String dataSourceName : dataSourceNames) {
//                 if (dataSourceName.endsWith(value % 2 + "")) {
//                     result.add(dataSourceName);
//                 }
//             }
//         }
//         return result;
//     }
// 
//     @Override
//     public Collection<String> doBetweenSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         Range<Integer> range = shardingValue.getValueRange();
//         for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
//             for (String each : dataSourceNames) {
//                 if (each.endsWith(i % 2 + "")) {
//                     result.add(each);
//                 }
//             }
//         }
//         return result;
//     }
// }
// 
// Path: src/main/java/org/easyarch/test/sharding/algorithm/ModuloTableShardingAlgorithm.java
// public class ModuloTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<Integer> {
//     @Override
//     public String doEqualSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         for (String each : dataSourceNames) {
//             if (each.endsWith(shardingValue.getValue() % 2 + "")) {
//                 return each;
//             }
//         }
//         throw new IllegalArgumentException();
//     }
// 
//     @Override
//     public Collection<String> doInSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         for (Integer value : shardingValue.getValues()) {
//             for (String dataSourceName : dataSourceNames) {
//                 if (dataSourceName.endsWith(value % 2 + "")) {
//                     result.add(dataSourceName);
//                 }
//             }
//         }
//         return result;
//     }
// 
//     @Override
//     public Collection<String> doBetweenSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) {
//         Collection<String> result = new LinkedHashSet<>(dataSourceNames.size());
//         Range<Integer> range = shardingValue.getValueRange();
//         for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
//             for (String each : dataSourceNames) {
//                 if (each.endsWith(i % 2 + "")) {
//                     result.add(each);
//                 }
//             }
//         }
//         return result;
//     }
// }


// Path: src/main/java/org/easyarch/test/sharding/ShardingTest.java
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.easyarch.test.sharding.algorithm.ModuloDatabaseShardingAlgorithm;
import org.easyarch.test.sharding.algorithm.ModuloTableShardingAlgorithm;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

package org.easyarch.test.sharding;



/**
 * Description :
 * Created by xingtianyu on 17-2-21
 * 上午10:11
 * description:
 */

public class ShardingTest {

    private static ShardingDataSource getShardingDataSource() {
        DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
        TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList(
                "db0.t_order_0", "db0.t_order_1",
                "db1.t_order_0", "db1.t_order_1"
        )).dataSourceRule(dataSourceRule).build();
        TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList(
                "db0.t_order_item_0", "db0.t_order_item_1",
                "db1.t_order_item_0", "db1.t_order_item_1"
        )).dataSourceRule(dataSourceRule).build();
        ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule))
                .bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
                .databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm()))
                .tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())).build();