From e89cbc806d851d64d8026836b85d5ac2c3f42d53 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 26 Sep 2022 21:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BF=AB=E6=8D=B7=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E8=B7=AF=E7=94=B1=E9=92=88=E5=AF=B9=E5=88=86=E8=A1=A8?= =?UTF-8?q?=E5=92=8C=E5=88=86=E5=BA=93=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 16 ++++----- samples/Sample.MySql/Program.cs | 1 + ...tShardingComparerVirtualDataSourceRoute.cs | 34 ++++++++++++++++++ ...stractShardingComparerVirtualTableRoute.cs | 36 +++++++++++++++++++ ...stractShardingOperatorVirtualTableRoute.cs | 1 - .../EFCores/ShardingModelCustomizer.cs | 4 +++ .../Helpers/DynamicShardingHelper.cs | 7 ++-- 7 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingComparerVirtualDataSourceRoute.cs create mode 100644 src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingComparerVirtualTableRoute.cs diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 0572a531..76575c25 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -38,13 +38,13 @@ namespace Sample.MySql.Controllers public IQueryable GetAll() { - var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); - var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); - //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 - var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod)); - //一定要先在路由里面添加尾巴 - virtualTableRoute.Append("20220921"); - shardingTableCreator.CreateTable("ds0","20220921"); + // var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); + // var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); + // //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 + // var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod)); + // //一定要先在路由里面添加尾巴 + // virtualTableRoute.Append("20220921"); + // shardingTableCreator.CreateTable("ds0","20220921"); return _defaultTableDbContext.Set(); } @@ -83,7 +83,7 @@ namespace Sample.MySql.Controllers }).ToListAsync(); - var firstOrDefault = _defaultTableDbContext.Set().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault(); + // var firstOrDefault = _defaultTableDbContext.Set().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault(); var sysUserMods1 = _defaultTableDbContext.Set() .Select(o => new ssss(){ Id = o.Id, C = GetAll().Count(x => x.Id == o.Id) }).ToList(); diff --git a/samples/Sample.MySql/Program.cs b/samples/Sample.MySql/Program.cs index b7b33031..49d989cf 100644 --- a/samples/Sample.MySql/Program.cs +++ b/samples/Sample.MySql/Program.cs @@ -13,6 +13,7 @@ namespace Sample.MySql { public static void Main(string[] args) { + var length = "B-SF-C30-190(S4)-D100P6KS60RCM-Ⅳ(2)Q-Ⅴ(3)T-Ⅳ(4)-GB/T 14902".Length; CreateHostBuilder(args).Build().Run(); } diff --git a/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingComparerVirtualDataSourceRoute.cs b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingComparerVirtualDataSourceRoute.cs new file mode 100644 index 00000000..8c18d021 --- /dev/null +++ b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingComparerVirtualDataSourceRoute.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using ShardingCore.Core.EntityMetadatas; + +namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions +{ + public abstract class AbstractShardingComparerVirtualDataSourceRouteclass : AbstractShardingOperatorVirtualDataSourceRoute where TEntity : class + { + + protected abstract IComparer GetComparer(); + public override Func GetRouteToFilter(TKey shardingKey, ShardingOperatorEnum shardingOperator) + { + var dataSourceName = ShardingKeyToDataSourceName(shardingKey); + var comparer = GetComparer(); + switch (shardingOperator) + { + case ShardingOperatorEnum.GreaterThan: + case ShardingOperatorEnum.GreaterThanOrEqual: + return dataSource => comparer.Compare(dataSource, dataSourceName) >= 0; + case ShardingOperatorEnum.LessThan: + case ShardingOperatorEnum.LessThanOrEqual: + return dataSource => comparer.Compare(dataSource, dataSourceName) <= 0; + case ShardingOperatorEnum.Equal: return dataSource => comparer.Compare(dataSource, dataSourceName)==0; + default: + { +#if DEBUG + Console.WriteLine($"shardingOperator is not equal scan all table tail"); +#endif + return dataSource => true; + } + } + } + } +} diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingComparerVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingComparerVirtualTableRoute.cs new file mode 100644 index 00000000..20fb6ae8 --- /dev/null +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingComparerVirtualTableRoute.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using ShardingCore.Core.EntityMetadatas; + +namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions +{ + + public abstract class AbstractShardingComparerVirtualTableRoute : AbstractShardingOperatorVirtualTableRoute where TEntity : class + { + protected abstract IComparer GetComparer(); + + public override Func GetRouteToFilter(TKey shardingKey, ShardingOperatorEnum shardingOperator) + { + var t = ShardingKeyToTail(shardingKey); + var comparer = GetComparer(); + switch (shardingOperator) + { + case ShardingOperatorEnum.GreaterThan: + case ShardingOperatorEnum.GreaterThanOrEqual: + return tail => comparer.Compare(tail, t) >= 0; + case ShardingOperatorEnum.LessThan: + case ShardingOperatorEnum.LessThanOrEqual: + return tail => comparer.Compare(tail, t) <= 0; + case ShardingOperatorEnum.Equal: return tail => comparer.Compare(tail, t)==0; + default: + { +#if DEBUG + Console.WriteLine($"shardingOperator is not equal scan all table tail"); +#endif + return tail => true; + } + } + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs index abfc4f7f..f0d46de5 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs @@ -22,7 +22,6 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions /// /// /// - /// /// /// protected override List DoRouteWithPredicate(DataSourceRouteResult dataSourceRouteResult, IQueryable queryable) diff --git a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs index b4245a93..080f66cd 100644 --- a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs +++ b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs @@ -13,6 +13,7 @@ using ShardingCore.Core; using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; +using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Extensions.InternalExtensions; @@ -77,6 +78,9 @@ namespace ShardingCore.EFCores { var clrType = mutableEntityType.ClrType; var entityMetadata = entityMetadataManager.TryGet(clrType); + if (entityMetadata == null) + throw new ShardingCoreInvalidOperationException($"not found entity type:[{clrType}]'s entity metadata"); + var shardingEntity = entityMetadata.EntityType; var tableSeparator = entityMetadata.TableSeparator; var entity = modelBuilder.Entity(shardingEntity); diff --git a/src/ShardingCore/Helpers/DynamicShardingHelper.cs b/src/ShardingCore/Helpers/DynamicShardingHelper.cs index ccb9ecb5..6aae59e4 100644 --- a/src/ShardingCore/Helpers/DynamicShardingHelper.cs +++ b/src/ShardingCore/Helpers/DynamicShardingHelper.cs @@ -132,7 +132,6 @@ namespace ShardingCore.Helpers dbContextOptionBuilder.UseShardingOptions(shardingRuntimeContext); return dbContextOptionBuilder.Options; } - /// /// 动态添加读写分离链接字符串 /// @@ -140,16 +139,16 @@ namespace ShardingCore.Helpers /// /// /// + /// /// - public static void DynamicAppendReadWriteConnectionString(IShardingRuntimeContext shardingRuntimeContext, string dataSourceName, + public static bool DynamicAppendReadWriteConnectionString(IShardingRuntimeContext shardingRuntimeContext, string dataSourceName, string connectionString, string readNodeName=null) { var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); if (virtualDataSource.ConnectionStringManager is IReadWriteConnectionStringManager readWriteAppendConnectionString) { - readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString, readNodeName); - return; + return readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString, readNodeName); } throw new ShardingCoreInvalidOperationException(