Пример #1
0
        public TableRule(TableRuleConfiguration tableRuleConfig, ShardingDataSourceNames shardingDataSourceNames,
                         string defaultGenerateKeyColumn)
        {
            LogicTable = tableRuleConfig.LogicTable.ToLower();
            List <string> dataNodes = tableRuleConfig.ActualDataNodes ?? new List <string>(0);

            // List<string> dataNodes = new InlineExpressionParser(tableRuleConfig.GetActualDataNodes()).splitAndEvaluate();
            // dataNodeIndexMap = new HashMap<>(dataNodes.size(), 1);
            dataNodeIndexMap = new Dictionary <DataNode, int>(dataNodes.Count);
            ActualDataNodes  = IsEmptyDataNodes(dataNodes)
                ? GenerateDataNodes(tableRuleConfig.LogicTable, shardingDataSourceNames.DataSourceNames) : GenerateDataNodes(dataNodes, shardingDataSourceNames.DataSourceNames);
            //ActualDataNodes = GenerateDataNodes(tableRuleConfig.LogicTable, shardingDataSourceNames.DataSourceNames);
            actualTables             = GetActualTables();
            DatabaseShardingStrategy = null == tableRuleConfig.DatabaseShardingStrategyConfig
                ? null
                : ShardingStrategyFactory.NewInstance(tableRuleConfig.DatabaseShardingStrategyConfig);
            TableShardingStrategy = null == tableRuleConfig.TableShardingStrategyConfig
                ? null
                : ShardingStrategyFactory.NewInstance(tableRuleConfig.TableShardingStrategyConfig);
            KeyGeneratorConfiguration keyGeneratorConfiguration = tableRuleConfig.KeyGeneratorConfig;

            generateKeyColumn =
                null != keyGeneratorConfiguration && !string.IsNullOrWhiteSpace(keyGeneratorConfiguration.Column)
                    ? keyGeneratorConfiguration.Column
                    : defaultGenerateKeyColumn;
            ShardingKeyGenerator = ContainsKeyGeneratorConfiguration(tableRuleConfig)
                ? new ShardingKeyGeneratorServiceLoader().NewService(tableRuleConfig.KeyGeneratorConfig.Type,
                                                                     tableRuleConfig.KeyGeneratorConfig.Properties)
                : null;
            // CheckRule(dataNodes);
        }
Пример #2
0
        static TableRuleConfiguration CreateSysUserModTableRule()
        {
            TableRuleConfiguration result = new TableRuleConfiguration("SysUserMod", new List <string>()
            {
                "ds0.SysUserMod_00",
                "ds0.SysUserMod_01",
                "ds0.SysUserMod_02"
            });

            //1、指定逻辑索引(oracle/PostgreSQL需要配置)
            //        result.setLogicIndex("order_id");
            result.DatabaseShardingStrategyConfig = new NoneShardingStrategyConfiguration();
            //4、配置分库策略,缺省表示使用默认分库策略
            //result.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
            //result.setDatabaseShardingStrategyConfig(new HintShardingStrategyConfiguration(new OrderDataBaseHintShardingAlgorithm()));
            //5、配置分表策略,缺省表示使用默认分表策略
            result.TableShardingStrategyConfig = new StandardShardingStrategyConfiguration("Id", new SysUserModId());
            //result.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_${order_id % 2}"));
            //result.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id",new orderPreciseShardingAlgorithm(),new orderRangeShardingAlgorithm()));
            //result.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("order_id,user_id",new orderComplexKeysShardingAlgorithm()));
            //result.setTableShardingStrategyConfig(new HintShardingStrategyConfiguration(new OrderTableHintShardingAlgorithm()));
            //6、指定自增字段以及key的生成方式
            //result.setKeyGeneratorColumnName("order_id");
            //result.setKeyGenerator(new DefaultKeyGenerator());
            return(result);
        }
Пример #3
0
 private bool ContainsKeyGeneratorConfiguration(TableRuleConfiguration tableRuleConfiguration)
 {
     return(null != tableRuleConfiguration.KeyGeneratorConfig &&
            !string.IsNullOrWhiteSpace(tableRuleConfiguration.KeyGeneratorConfig.Type));
 }