コード例 #1
0
ファイル: ShardingUtil.cs プロジェクト: zxl359592450/dal
        public static IList <Statement> GetShardStatementByEntity <T>(String logicDbName, IShardingStrategy shardingStrategy,
                                                                      IList <T> list, SqlTable table, IDictionary hints, Func <IList <T>, IDictionary, Statement> func) where T : class, new()
        {
            if (String.IsNullOrEmpty(logicDbName))
            {
                return(null);
            }
            if (list == null || list.Count == 0)
            {
                return(null);
            }
            if (table == null)
            {
                return(null);
            }

            var statements   = new List <Statement>();
            var shardingType = GetShardingType(shardingStrategy);

            if (shardingType == ShardingType.ShardByDB)
            {
                var dict = ShuffledByDb(logicDbName, shardingStrategy, list, table.ColumnList, hints);

                if (dict != null && dict.Count > 0)
                {
                    foreach (var item in dict)
                    {
                        var newHints = HintsUtil.CloneHints(hints);
                        newHints[DALExtStatementConstant.SHARDID] = item.Key;
                        Statement statement = func.Invoke(item.Value, newHints);
                        statements.Add(statement);
                    }
                }
            }
            else if (shardingType == ShardingType.ShardByTable)
            {
                var dict = ShuffledByTable(logicDbName, shardingStrategy, list, table.ColumnList, hints);

                if (dict != null && dict.Count > 0)
                {
                    foreach (var item in dict)
                    {
                        var newHints = HintsUtil.CloneHints(hints);
                        newHints[DALExtStatementConstant.TABLEID] = item.Key;
                        Statement statement = func.Invoke(item.Value, newHints);
                        statements.Add(statement);
                    }
                }
            }
            else
            {
                var dict = ShuffledByDbTable(logicDbName, shardingStrategy, list, table.ColumnList, hints);

                if (dict != null && dict.Count > 0)
                {
                    foreach (var item in dict)
                    {
                        foreach (var item2 in item.Value)
                        {
                            var newHints = HintsUtil.CloneHints(hints);
                            newHints[DALExtStatementConstant.SHARDID] = item.Key;
                            newHints[DALExtStatementConstant.TABLEID] = item2.Key;
                            Statement statement = func.Invoke(item2.Value, newHints);
                            statements.Add(statement);
                        }
                    }
                }
            }

            return(statements);
        }
コード例 #2
0
ファイル: ShardingUtil.cs プロジェクト: zxl359592450/dal
        public static IList <Statement> GetShardStatement(String logicDbName, IShardingStrategy shardingStrategy,
                                                          StatementParameterCollection parameters, IDictionary hints, Func <IDictionary, Statement> func)
        {
            IList <Statement> statements;

            if (shardingStrategy == null)
            {
                return(null);
            }
            var shardingType = GetShardingType(shardingStrategy);

            if (shardingType != ShardingType.ShardByDBAndTable)
            {
                IList <String> shards = null;

                //Get shards from hints
                if (hints != null)
                {
                    IList <String> temp = null;

                    if (shardingType == ShardingType.ShardByDB)
                    {
                        if (hints.Contains(DALExtStatementConstant.SHARD_IDS))
                        {
                            temp = hints[DALExtStatementConstant.SHARD_IDS] as List <String>;
                        }
                        else if (hints.Contains(DALExtStatementConstant.SHARDID))
                        {
                            temp = new List <String> {
                                hints[DALExtStatementConstant.SHARDID] as String
                            };
                        }
                    }
                    else if (shardingType == ShardingType.ShardByTable)
                    {
                        if (hints.Contains(DALExtStatementConstant.TABLE_IDS))
                        {
                            temp = hints[DALExtStatementConstant.TABLE_IDS] as List <String>;
                        }
                        else if (hints.Contains(DALExtStatementConstant.TABLEID))
                        {
                            temp = new List <String> {
                                hints[DALExtStatementConstant.TABLEID] as String
                            };
                        }
                    }

                    if (temp != null)
                    {
                        shards = temp;
                    }
                }

                //Get shards from parameters
                if (shards == null)
                {
                    if (parameters != null)
                    {
                        if (shardingType == ShardingType.ShardByDB)
                        {
                            String shardId = GetShardId <Object>(logicDbName, shardingStrategy, null, null, parameters, hints);
                            if (!String.IsNullOrEmpty(shardId))
                            {
                                shards = new List <String> {
                                    shardId
                                }
                            }
                            ;
                        }
                        else if (shardingType == ShardingType.ShardByTable)
                        {
                            String tableId = GetTableId <Object>(logicDbName, shardingStrategy, null, null, parameters, hints);
                            if (!String.IsNullOrEmpty(tableId))
                            {
                                shards = new List <String> {
                                    tableId
                                }
                            }
                            ;
                        }
                    }
                }

                if (shards == null)
                {
                    throw new DalException("Please provide shard information.");
                }

                //Build statements
                statements = new List <Statement>();

                foreach (var item in shards)
                {
                    var newHints = HintsUtil.CloneHints(hints);

                    switch (shardingType)
                    {
                    case ShardingType.ShardByDB:
                        newHints[DALExtStatementConstant.SHARDID] = item;
                        break;

                    case ShardingType.ShardByTable:
                        newHints[DALExtStatementConstant.TABLEID] = item;
                        break;
                    }

                    Statement statement = func.Invoke(newHints);
                    statements.Add(statement);
                }
            }
            else
            {
                statements = new List <Statement>();
                IDictionary <String, IList <String> > shardDict = null;
                if (hints != null && hints.Contains(DALExtStatementConstant.SHARD_TABLE_DICT))
                {
                    shardDict = hints[DALExtStatementConstant.SHARD_TABLE_DICT] as IDictionary <String, IList <String> >;
                }

                if (shardDict == null)
                {
                    var newHints = HintsUtil.CloneHints(hints);
                    newHints[DALExtStatementConstant.SHARDID] = GetShardIdByHints(hints);
                    newHints[DALExtStatementConstant.TABLEID] = GetTableIdByHints(hints);
                    Statement statement = func.Invoke(newHints);
                    statements.Add(statement);
                }
                else
                {
                    foreach (var shard in shardDict)
                    {
                        foreach (var table in shard.Value)
                        {
                            var newHints = HintsUtil.CloneHints(hints);
                            newHints[DALExtStatementConstant.SHARDID] = shard.Key;
                            newHints[DALExtStatementConstant.TABLEID] = table;
                            Statement statement = func.Invoke(newHints);
                            statements.Add(statement);
                        }
                    }
                }
            }

            return(statements);
        }