Example #1
0
        private void GetShards()
        {
            // Gather shard_tables
            DataTable tblShards = ExecuteCommand("dump shard_tables");

            FabricShardTables.Clear();
            FabricShardTablesPerTable.Clear();
            foreach (DataRow row in tblShards.Rows)
            {
                FabricShardTable shardTable = new FabricShardTable()
                {
                    SchemaName = row["schema_name"] as string,
                    TableName  = row["table_name"] as string,
                    ColumnName = row["column_name"] as string,
                    MappingId  = int.Parse(row["mapping_id"] as string)
                };
                FabricShardTables.Add(shardTable.MappingId, shardTable);
                FabricShardTablesPerTable.Add(string.Format("{0}.{1}", shardTable.SchemaName, shardTable.TableName), shardTable);
            }
            // Gather shard_index'es
            DataTable tblShardIndexes = ExecuteCommand("dump shard_index");

            foreach (DataRow rowIdx in tblShardIndexes.Rows)
            {
                FabricShardIndex Index = new FabricShardIndex()
                {
                    GroupId    = rowIdx["group_id"] as string,
                    LowerBound = rowIdx["lower_bound"] as string,
                    ShardId    = int.Parse(rowIdx["shard_id"] as string),
                    MappingId  = int.Parse(rowIdx["mapping_id"] as string)
                };
                FabricShardTable table = FabricShardTables[Index.MappingId];
                table.Indexes.Add(Index);
            }
            // Gather global group
            DataTable tblGlobal = ExecuteCommand("dump shard_maps");

            foreach (DataRow rowGlobal in tblGlobal.Rows)
            {
                int mappingId        = int.Parse(rowGlobal["mapping_id"] as string);
                FabricShardTable tbl = FabricShardTables[mappingId];
                tbl.GlobalGroupId = rowGlobal["global_group_id"] as string;
                tbl.TypeShard     = (FabricShardIndexType)Enum.Parse(typeof(FabricShardIndexType), rowGlobal["type_name"] as string, true);
                tbl.Indexes.ForEach(i => i.Type = tbl.TypeShard);
            }
            foreach (KeyValuePair <int, FabricShardTable> kvp in FabricShardTables)
            {
                kvp.Value.Indexes.Sort(delegate(FabricShardIndex x, FabricShardIndex y)
                {
                    return(ShardKeyCompare(x.LowerBound, y.LowerBound, x.Type) * -1);
                });
            }
        }
Example #2
0
        private ReplicationServer GetServerByShard(FabricServerModeEnum mode)
        {
            FabricShardTable shardTable = FabricShardTablesPerTable[tableProperty];
            object           key        = keyProperty;

            if (shardTable.TypeShard == FabricShardIndexType.Hash)
            {
                key = GetMd5Hash(key.ToString());
            }
            foreach (FabricShardIndex idx in shardTable.Indexes)
            {
                if (ShardKeyCompare(key, idx.LowerBound, idx.Type) >= 0)
                {
                    return(GetServerByGroup(mode, idx.GroupId));
                }
            }
            if (shardTable.TypeShard == FabricShardIndexType.Hash)
            {
                return(GetServerByGroup(mode, shardTable.Indexes.First().GroupId));
            }
            return(null);
        }
 private void GetShards()
 {
   // Gather shard_tables
   DataTable tblShards = ExecuteCommand("dump shard_tables");
   FabricShardTables.Clear();
   FabricShardTablesPerTable.Clear();
   foreach (DataRow row in tblShards.Rows)
   {
     FabricShardTable shardTable = new FabricShardTable()
     {
       SchemaName = row["schema_name"] as string,
       TableName = row["table_name"] as string,
       ColumnName = row["column_name"] as string,
       MappingId = int.Parse(row["mapping_id"] as string)
     };
     FabricShardTables.Add(shardTable.MappingId, shardTable);
     FabricShardTablesPerTable.Add(string.Format("{0}.{1}", shardTable.SchemaName, shardTable.TableName), shardTable);
   }
   // Gather shard_index'es
   DataTable tblShardIndexes = ExecuteCommand("dump shard_index");
   foreach (DataRow rowIdx in tblShardIndexes.Rows)
   {
     FabricShardIndex Index = new FabricShardIndex()
     {
       GroupId = rowIdx["group_id"] as string,
       LowerBound = rowIdx["lower_bound"] as string,
       ShardId = int.Parse(rowIdx["shard_id"] as string),
       MappingId = int.Parse(rowIdx["mapping_id"] as string)
     };
     FabricShardTable table = FabricShardTables[Index.MappingId];
     table.Indexes.Add(Index);
   }
   // Gather global group
   DataTable tblGlobal = ExecuteCommand("dump shard_maps");
   foreach (DataRow rowGlobal in tblGlobal.Rows)
   {
     int mappingId = int.Parse(rowGlobal["mapping_id"] as string);
     FabricShardTable tbl = FabricShardTables[mappingId];
     tbl.GlobalGroupId = rowGlobal["global_group_id"] as string;
     tbl.TypeShard = (FabricShardIndexType)Enum.Parse(typeof(FabricShardIndexType), rowGlobal["type_name"] as string, true);
     tbl.Indexes.ForEach(i => i.Type = tbl.TypeShard);
   }
   foreach (KeyValuePair<int, FabricShardTable> kvp in FabricShardTables)
   {
     kvp.Value.Indexes.Sort(delegate(FabricShardIndex x, FabricShardIndex y)
     {
       return ShardKeyCompare(x.LowerBound, y.LowerBound, x.Type) * -1;
     });
   }
 }