添加取模分表的后缀位数

This commit is contained in:
xuejmnet 2021-02-04 16:42:49 +08:00
parent b723b28d02
commit 817fa249aa
6 changed files with 27 additions and 8 deletions

View File

@ -295,6 +295,7 @@ context.Where(shardingBatchUpdateEntry.Where).Update(shardingBatchUpdateEntry.Up
使用时需要注意 使用时需要注意
- 实体对象是否继承`IShardingEntity` - 实体对象是否继承`IShardingEntity`
- 实体对象是否有`ShardingKey` - 实体对象是否有`ShardingKey`
- 实体对象是否映射配置已实现`IEntityTypeConfiguration<TEntity>`
- 实体对象是否已经实现了一个虚拟路由 - 实体对象是否已经实现了一个虚拟路由
- startup是否已经添加虚拟路由 - startup是否已经添加虚拟路由
@ -326,7 +327,7 @@ context.Where(shardingBatchUpdateEntry.Where).Update(shardingBatchUpdateEntry.Up
- [支持更多数据库查询] - [支持更多数据库查询]
# 最后 # 最后
该框架借鉴了大部分分表组件的思路,目前提供的接口都已经实现,并且支持跨表查询,基于分页查询该框架也使用了流式查询保证不会再skip大数据的时候内存会爆炸,至于groupby目前已经在开发支持了,相信不久后就会发布新版本,目前这个库只是一个刚刚成型的库还有很多不完善的地方希望大家多多包涵,如果喜欢的话也希望大家给个star. 该框架借鉴了大部分分表组件的思路,目前提供的接口都已经实现,并且支持跨表查询,基于分页查询该框架也使用了流式查询保证不会再skip大数据的时候内存会爆炸,目前这个库只是一个刚刚成型的库还有很多不完善的地方希望大家多多包涵,如果喜欢的话也希望大家给个star.
该文档是我晚上赶工赶出来的也想趁热打铁希望更多的人关注,也希望更多的人可以交流。 该文档是我晚上赶工赶出来的也想趁热打铁希望更多的人关注,也希望更多的人可以交流。
凭借各大开源生态圈提供的优秀代码和思路才有的这个框架,希望可以为.Net生态提供一份微薄之力,该框架本人会一直长期维护,有大神技术支持可以联系下方方式欢迎star :) 凭借各大开源生态圈提供的优秀代码和思路才有的这个框架,希望可以为.Net生态提供一份微薄之力,该框架本人会一直长期维护,有大神技术支持可以联系下方方式欢迎star :)

View File

@ -16,7 +16,7 @@ namespace Sample.SqlServer.Shardings
*/ */
public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod> public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod>
{ {
public SysUserModVirtualRoute() : base(3) public SysUserModVirtualRoute() : base(2,3)
{ {
} }
} }

View File

@ -21,11 +21,19 @@ namespace ShardingCore.VirtualRoutes.Mods
public abstract class AbstractSimpleShardingModKeyIntVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,int> where T:class,IShardingEntity public abstract class AbstractSimpleShardingModKeyIntVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,int> where T:class,IShardingEntity
{ {
protected readonly int Mod; protected readonly int Mod;
protected AbstractSimpleShardingModKeyIntVirtualRoute(int mod) protected readonly int TailLength;
protected readonly char PaddingChar;
protected AbstractSimpleShardingModKeyIntVirtualRoute(int tailLength,int mod,char paddingChar='0')
{ {
if(tailLength<1)
throw new ArgumentException($"{nameof(tailLength)} less than 1 ");
if (mod < 1) if (mod < 1)
throw new ArgumentException($"{nameof(mod)} less than 1 "); throw new ArgumentException($"{nameof(mod)} less than 1 ");
if (string.IsNullOrWhiteSpace(paddingChar.ToString()))
throw new ArgumentException($"{nameof(paddingChar)} cant empty ");
TailLength = tailLength;
Mod = mod; Mod = mod;
PaddingChar = paddingChar;
} }
protected override int ConvertToShardingKey(object shardingKey) protected override int ConvertToShardingKey(object shardingKey)
{ {
@ -35,7 +43,7 @@ namespace ShardingCore.VirtualRoutes.Mods
public override string ShardingKeyToTail(object shardingKey) public override string ShardingKeyToTail(object shardingKey)
{ {
var shardingKeyInt = ConvertToShardingKey(shardingKey); var shardingKeyInt = ConvertToShardingKey(shardingKey);
return Math.Abs(shardingKeyInt % Mod).ToString(); return Math.Abs(shardingKeyInt % Mod).ToString().PadLeft(TailLength,PaddingChar);
} }
public override List<string> GetAllTails() public override List<string> GetAllTails()

View File

@ -22,9 +22,19 @@ namespace ShardingCore.VirtualRoutes.Mods
public abstract class AbstractSimpleShardingModKeyStringVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,string> where T:class,IShardingEntity public abstract class AbstractSimpleShardingModKeyStringVirtualRoute<T>:AbstractShardingOperatorVirtualRoute<T,string> where T:class,IShardingEntity
{ {
protected readonly int Mod; protected readonly int Mod;
protected AbstractSimpleShardingModKeyStringVirtualRoute(int mod) protected readonly int TailLength;
protected readonly char PaddingChar;
protected AbstractSimpleShardingModKeyStringVirtualRoute(int tailLength,int mod,char paddingChar='0')
{ {
if(tailLength<1)
throw new ArgumentException($"{nameof(tailLength)} less than 1 ");
if (mod < 1)
throw new ArgumentException($"{nameof(mod)} less than 1 ");
if (string.IsNullOrWhiteSpace(paddingChar.ToString()))
throw new ArgumentException($"{nameof(paddingChar)} cant empty ");
TailLength = tailLength;
Mod = mod; Mod = mod;
PaddingChar = paddingChar;
} }
/// <summary> /// <summary>
/// 如何将shardingkey转成对应的tail /// 如何将shardingkey转成对应的tail
@ -34,7 +44,7 @@ namespace ShardingCore.VirtualRoutes.Mods
public override string ShardingKeyToTail(object shardingKey) public override string ShardingKeyToTail(object shardingKey)
{ {
var shardingKeyStr = ConvertToShardingKey(shardingKey); var shardingKeyStr = ConvertToShardingKey(shardingKey);
return Math.Abs(ShardingCoreHelper.GetStringHashCode(shardingKeyStr) % Mod).ToString(); return Math.Abs(ShardingCoreHelper.GetStringHashCode(shardingKeyStr) % Mod).ToString().PadLeft(TailLength,PaddingChar);;
} }
protected override string ConvertToShardingKey(object shardingKey) protected override string ConvertToShardingKey(object shardingKey)
{ {

View File

@ -16,7 +16,7 @@ namespace ShardingCore.Test50.MySql.Shardings
*/ */
public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod> public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod>
{ {
public SysUserModVirtualRoute() : base(3) public SysUserModVirtualRoute() : base(2,3)
{ {
} }

View File

@ -16,7 +16,7 @@ namespace ShardingCore.Test50.Shardings
*/ */
public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod> public class SysUserModVirtualRoute : AbstractSimpleShardingModKeyStringVirtualRoute<SysUserMod>
{ {
public SysUserModVirtualRoute() : base(3) public SysUserModVirtualRoute() : base(2,3)
{ {
} }