|
| 1 | +using NewLife; |
| 2 | +using NewLife.Data; |
| 3 | +using XCode.Configuration; |
| 4 | + |
| 5 | +namespace XCode.Shards; |
| 6 | + |
| 7 | +/// <summary>字段值分表策略。按指定业务字段的值分表,如按UserId字段分表,UserId=1000时表名为Log_1000</summary> |
| 8 | +/// <remarks> |
| 9 | +/// 适用于多租户等场景,整张分表的字段值固定一致。 |
| 10 | +/// 示例:UserId=1000 → Log_1000 |
| 11 | +/// </remarks> |
| 12 | +public class FieldShardPolicy : IShardPolicy |
| 13 | +{ |
| 14 | + #region 属性 |
| 15 | + /// <summary>实体工厂</summary> |
| 16 | + public IEntityFactory? Factory { get; set; } |
| 17 | + |
| 18 | + /// <summary>字段</summary> |
| 19 | + public FieldItem? Field { get; set; } |
| 20 | + |
| 21 | + /// <summary>连接名策略。格式化字符串,0位基础连接名,1位字段值,如{0}_{1}</summary> |
| 22 | + public String? ConnPolicy { get; set; } |
| 23 | + |
| 24 | + /// <summary>表名策略。格式化字符串,0位基础表名,1位字段值,如{0}_{1}</summary> |
| 25 | + public String? TablePolicy { get; set; } = "{0}_{1}"; |
| 26 | + |
| 27 | + private readonly String? _fieldName; |
| 28 | + #endregion |
| 29 | + |
| 30 | + #region 构造 |
| 31 | + /// <summary>实例化</summary> |
| 32 | + public FieldShardPolicy() { } |
| 33 | + |
| 34 | + /// <summary>指定字段实例化字段分表策略</summary> |
| 35 | + /// <param name="field">分表字段</param> |
| 36 | + /// <param name="factory">实体工厂</param> |
| 37 | + public FieldShardPolicy(FieldItem field, IEntityFactory? factory = null) |
| 38 | + { |
| 39 | + Field = field; |
| 40 | + Factory = factory ?? field.Factory; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary>指定字段名和工厂实例化字段分表策略</summary> |
| 44 | + /// <param name="fieldName">字段名</param> |
| 45 | + /// <param name="factory">实体工厂</param> |
| 46 | + public FieldShardPolicy(String fieldName, IEntityFactory factory) |
| 47 | + { |
| 48 | + _fieldName = fieldName; |
| 49 | + Factory = factory; |
| 50 | + |
| 51 | + // 异步加载字段 |
| 52 | + Task.Run(GetField); |
| 53 | + } |
| 54 | + |
| 55 | + private FieldItem? GetField() => Field ??= _fieldName == null ? null : Factory?.Table.FindByName(_fieldName); |
| 56 | + #endregion |
| 57 | + |
| 58 | + #region 分表 |
| 59 | + /// <summary>为实体对象或字段值计算分表分库</summary> |
| 60 | + /// <param name="value">实体对象或字段直接值(Int32/Int64/String 等)</param> |
| 61 | + /// <returns>分表模型,策略未配置时返回 null</returns> |
| 62 | + public virtual ShardModel? Shard(Object value) |
| 63 | + { |
| 64 | + if (value is IModel entity) return ShardByEntity(entity); |
| 65 | + return ShardByValue(value); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary>从实体对象中提取分表字段值并计算分表分库</summary> |
| 69 | + /// <param name="entity">实体对象</param> |
| 70 | + /// <returns>分表模型</returns> |
| 71 | + protected virtual ShardModel? ShardByEntity(IModel entity) |
| 72 | + { |
| 73 | + var fi = GetField() ?? throw new XCodeException("字段分表策略要求指定分表字段!"); |
| 74 | + |
| 75 | + var value = entity[fi.Name]; |
| 76 | + if (value == null) throw new XCodeException($"实体对象字段[{fi.Name}]为空,无法用于字段分表"); |
| 77 | + |
| 78 | + return ShardByValue(value); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary>按字段值计算分表分库</summary> |
| 82 | + /// <param name="fieldValue">字段值</param> |
| 83 | + /// <returns>分表模型,策略未配置时返回 null</returns> |
| 84 | + public virtual ShardModel? ShardByValue(Object fieldValue) |
| 85 | + { |
| 86 | + if (ConnPolicy.IsNullOrEmpty() && TablePolicy.IsNullOrEmpty()) return null; |
| 87 | + |
| 88 | + if (Factory == null) throw new XCodeException("字段分表策略要求指定实体工厂!"); |
| 89 | + var table = Factory.Table; |
| 90 | + |
| 91 | + var connName = table.ConnName; |
| 92 | + var tableName = table.TableName; |
| 93 | + if (!ConnPolicy.IsNullOrEmpty()) connName = String.Format(ConnPolicy, connName, fieldValue); |
| 94 | + if (!TablePolicy.IsNullOrEmpty()) tableName = String.Format(TablePolicy, tableName, fieldValue); |
| 95 | + |
| 96 | + return new(connName, tableName); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary>字段值分表策略不支持时间区间查询,直接返回空数组</summary> |
| 100 | + /// <param name="start">开始时间(不使用)</param> |
| 101 | + /// <param name="end">结束时间(不使用)</param> |
| 102 | + /// <returns>空数组</returns> |
| 103 | + public virtual ShardModel[] Shards(DateTime start, DateTime end) => []; |
| 104 | + |
| 105 | + /// <summary>从查询表达式中提取等值条件计算分表分库</summary> |
| 106 | + /// <param name="expression">查询表达式</param> |
| 107 | + /// <returns>分表模型数组;条件中没有分表字段时返回空数组</returns> |
| 108 | + public virtual ShardModel[] Shards(Expression expression) |
| 109 | + { |
| 110 | + var fi = GetField() ?? throw new XCodeException("字段分表策略要求指定分表字段!"); |
| 111 | + |
| 112 | + var exps = new List<FieldExpression>(); |
| 113 | + if (expression is WhereExpression where) |
| 114 | + exps = where.Where(e => e is FieldExpression fe && fe.Field.Name == fi.Name).Cast<FieldExpression>().ToList(); |
| 115 | + else if (expression is FieldExpression fe2 && fe2.Field.Name == fi.Name) |
| 116 | + exps.Add(fe2); |
| 117 | + |
| 118 | + if (exps.Count == 0) return []; |
| 119 | + |
| 120 | + // 仅支持等值条件 |
| 121 | + var eq = exps.FirstOrDefault(e => e.Action == "="); |
| 122 | + if (eq != null) |
| 123 | + { |
| 124 | + var model = ShardByValue(eq.Value); |
| 125 | + if (model != null) return [model]; |
| 126 | + } |
| 127 | + |
| 128 | + return []; |
| 129 | + } |
| 130 | + #endregion |
| 131 | +} |
0 commit comments