public ComplexShardingStrategyConfiguration(string shardingColumns, IComplexKeysShardingAlgorithm <IComparable> shardingAlgorithm)
 {
     ShardingAssert.If(string.IsNullOrWhiteSpace(shardingColumns), "ShardingColumns is required.");
     ShardingAssert.ShouldBeNotNull(shardingAlgorithm, "ShardingAlgorithm is required.");
     this.ShardingColumns   = shardingColumns;
     this.ShardingAlgorithm = shardingAlgorithm;
 }
Example #2
0
 public StandardShardingStrategyConfiguration(string shardingColumn, IPreciseShardingAlgorithm <IComparable> preciseShardingAlgorithm, IRangeShardingAlgorithm <IComparable> rangeShardingAlgorithm)
 {
     ShardingAssert.If(string.IsNullOrWhiteSpace(shardingColumn), "ShardingColumns is required.");
     ShardingAssert.ShouldBeNotNull(preciseShardingAlgorithm, "PreciseShardingAlgorithm is required.");
     this.ShardingColumn           = shardingColumn;
     this.PreciseShardingAlgorithm = preciseShardingAlgorithm;
     this.RangeShardingAlgorithm   = rangeShardingAlgorithm;
 }
        private List <IComparable> GetOrderValues()
        {
            var result = new List <IComparable>(orderByItems.Count);

            foreach (var orderByItem in orderByItems)
            {
                var value = _streamDataReader.GetValue(orderByItem.GetIndex());
                ShardingAssert.If(value == null || !(value is IComparable), "Order by value must implements Comparable");
                result.Add((IComparable)value);
            }

            return(result);
        }
        private ICollection <string> RouteDataSources(ShardingRule shardingRule, TableRule tableRule, List <IRouteValue> databaseShardingValues)
        {
            if (databaseShardingValues.IsEmpty())
            {
                return(tableRule.GetActualDatasourceNames());
            }
            ICollection <string> result = new HashSet <string>(shardingRule.GetDatabaseShardingStrategy(tableRule).DoSharding(tableRule.GetActualDatasourceNames(), databaseShardingValues, this.Properties));

            ShardingAssert.If(result.IsEmpty(), "no database route info");
            ShardingAssert.Else(tableRule.GetActualDatasourceNames().All(o => result.Contains(o)), $"Some routed data sources do not belong to configured data sources. routed data sources: `{result}`, configured data sources: `{tableRule.GetActualDatasourceNames()}`");

            return(result);
        }
        private ICollection <DataNode> RouteTables(ShardingRule shardingRule, TableRule tableRule, string routedDataSource, List <IRouteValue> tableShardingValues)
        {
            ICollection <string> availableTargetTables = tableRule.GetActualTableNames(routedDataSource);
            ICollection <string> routedTables          = new HashSet <string>(tableShardingValues.IsEmpty() ? availableTargetTables
                    : shardingRule.GetTableShardingStrategy(tableRule).DoSharding(availableTargetTables, tableShardingValues, this.Properties));

            ShardingAssert.If(routedTables.IsEmpty(), "no table route info");
            ICollection <DataNode> result = new LinkedList <DataNode>();

            foreach (var routedTable in routedTables)
            {
                result.Add(new DataNode(routedDataSource, routedTable));
            }
            return(result);
        }
Example #6
0
 private void CheckSubQueryShardingValues(ISqlCommandContext <ISqlCommand> sqlStatementContext, ShardingRule shardingRule, ShardingConditions shardingConditions)
 {
     foreach (var tableName in sqlStatementContext.GetTablesContext().GetTableNames())
     {
         var tableRule = shardingRule.FindTableRule(tableName);
         if (tableRule != null && IsRoutingByHint(shardingRule, tableRule) &&
             HintManager.GetDatabaseShardingValues(tableName).Any() && HintManager.GetTableShardingValues(tableName).Any())
         {
             return;
         }
     }
     ShardingAssert.If(shardingConditions.Conditions.IsEmpty(), "Must have sharding column with subquery.");
     if (shardingConditions.Conditions.Count > 1)
     {
         ShardingAssert.Else(IsSameShardingCondition(shardingRule, shardingConditions), "Sharding value must same with subquery.");
     }
 }