优化 bulk和路由规则
This commit is contained in:
parent
4f8d35a6b0
commit
f121985ee7
|
@ -35,7 +35,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AbpSharding", "samp
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50_3x", "test\ShardingCore.Test50_3x\ShardingCore.Test50_3x.csproj", "{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test50_2x", "test\ShardingCore.Test50_2x\ShardingCore.Test50_2x.csproj", "{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50_2x", "test\ShardingCore.Test50_2x\ShardingCore.Test50_2x.csproj", "{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.BulkConsole", "samples\Sample.BulkConsole\Sample.BulkConsole.csproj", "{2443CC8B-FB7D-47A7-9663-F3848BB30A36}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -91,6 +93,10 @@ Global
|
|||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -108,6 +114,7 @@ Global
|
|||
{1136B8C9-3539-42FA-97FD-CAA6F146FCF0} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
{6709DD6A-6A44-4A49-BA4D-A50A40102C8B} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
|
||||
{2443CC8B-FB7D-47A7-9663-F3848BB30A36} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
:start
|
||||
::定义版本
|
||||
set EFCORE2=2.2.0.20-pre1
|
||||
set EFCORE3=3.2.0.20-pre1
|
||||
set EFCORE5=5.2.0.20-pre1
|
||||
set EFCORE2=2.2.0.20-pre2
|
||||
set EFCORE3=3.2.0.20-pre2
|
||||
set EFCORE5=5.2.0.20-pre2
|
||||
|
||||
::删除所有bin与obj下的文件
|
||||
@echo off
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.BulkConsole.Entities;
|
||||
using ShardingCore.Core.VirtualRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace Sample.BulkConsole.DbContexts
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/7 21:07:19
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class MyDbContext: DbContext,IShardingTableDbContext
|
||||
{
|
||||
public MyDbContext(DbContextOptions<MyDbContext> myDbContextOptions):base(myDbContextOptions)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.Entity<Order>(entity =>
|
||||
{
|
||||
entity.HasKey(o => o.Id);
|
||||
entity.Property(o => o.OrderNo).IsRequired().HasMaxLength(128).IsUnicode(false);
|
||||
entity.ToTable(nameof(Order));
|
||||
});
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sample.BulkConsole.Entities;
|
||||
using ShardingCore.Sharding;
|
||||
|
||||
namespace Sample.BulkConsole.DbContexts
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/7 21:12:12
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class MyShardingDbContext:AbstractShardingDbContext<MyDbContext>
|
||||
{
|
||||
public MyShardingDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.Entity<Order>(entity =>
|
||||
{
|
||||
entity.HasKey(o => o.Id);
|
||||
entity.Property(o => o.OrderNo).IsRequired().HasMaxLength(128).IsUnicode(false);
|
||||
entity.ToTable(nameof(Order));
|
||||
});
|
||||
}
|
||||
public override Type ShardingDbContextType => this.GetType();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using ShardingCore.Core;
|
||||
|
||||
namespace Sample.BulkConsole.Entities
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/7 21:08:43
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class Order:IShardingTable
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string OrderNo { get; set; }
|
||||
public int Seq { get; set; }
|
||||
[ShardingTableKey]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Sample.BulkConsole.Entities;
|
||||
using ShardingCore.Sharding.PaginationConfigurations;
|
||||
|
||||
namespace Sample.BulkConsole
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/8 10:29:22
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class OrderPaginationConfiguration:IPaginationConfiguration<Order>
|
||||
{
|
||||
public void Configure(PaginationBuilder<Order> builder)
|
||||
{
|
||||
builder.PaginationSequence(o => o.CreateTime);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using Sample.BulkConsole.Entities;
|
||||
using ShardingCore.Sharding.PaginationConfigurations;
|
||||
using ShardingCore.VirtualRoutes.Days;
|
||||
|
||||
namespace Sample.BulkConsole
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/9/7 21:16:47
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class OrderVirtualRoute:AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<Order>
|
||||
{
|
||||
public override DateTime GetBeginTime()
|
||||
{
|
||||
return DateTime.Now.Date.AddDays(-3);
|
||||
}
|
||||
|
||||
public override IPaginationConfiguration<Order> CreatePaginationConfiguration()
|
||||
{
|
||||
return new OrderPaginationConfiguration();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using EFCore.BulkExtensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Sample.BulkConsole.DbContexts;
|
||||
using Sample.BulkConsole.Entities;
|
||||
using ShardingCore;
|
||||
using ShardingCore.Extensions;
|
||||
|
||||
namespace Sample.BulkConsole
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
|
||||
});
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddShardingDbContext<MyShardingDbContext, MyDbContext>(
|
||||
o => o.UseSqlServer("Data Source=localhost;Initial Catalog=MyOrderSharding;Integrated Security=True;")
|
||||
, op =>
|
||||
{
|
||||
op.EnsureCreatedWithOutShardingTable = true;
|
||||
op.CreateShardingTableOnStart = true;
|
||||
op.UseShardingOptionsBuilder(
|
||||
(connection, builder) => builder.UseSqlServer(connection),//使用dbconnection创建dbcontext支持事务
|
||||
(conStr, builder) => builder.UseSqlServer(conStr).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
|
||||
);
|
||||
op.AddShardingTableRoute<OrderVirtualRoute>();
|
||||
});
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
serviceProvider.GetService<IShardingBootstrapper>().Start();
|
||||
using (var serviceScope = serviceProvider.CreateScope())
|
||||
{
|
||||
var myShardingDbContext = serviceScope.ServiceProvider.GetService<MyShardingDbContext>();
|
||||
if (!myShardingDbContext.Set<Order>().Any())
|
||||
{
|
||||
var begin = DateTime.Now.Date.AddDays(-3);
|
||||
var now = DateTime.Now;
|
||||
var current = begin;
|
||||
ICollection<Order> orders = new LinkedList<Order>();
|
||||
int i = 0;
|
||||
while (current < now)
|
||||
{
|
||||
orders.Add(new Order()
|
||||
{
|
||||
Id = i.ToString(),
|
||||
OrderNo = $"orderno-" + i.ToString(),
|
||||
Seq = i,
|
||||
CreateTime = current
|
||||
});
|
||||
i++;
|
||||
current = current.AddMilliseconds(100);
|
||||
}
|
||||
|
||||
var startNew = Stopwatch.StartNew();
|
||||
var bulkShardingEnumerable = myShardingDbContext.BulkShardingEnumerable(orders);
|
||||
startNew.Stop();
|
||||
Console.WriteLine($"订单总数:{i}条,myShardingDbContext.BulkShardingEnumerable(orders)用时:{startNew.ElapsedMilliseconds}毫秒");
|
||||
startNew.Restart();
|
||||
foreach (var keyValuePair in bulkShardingEnumerable)
|
||||
{
|
||||
keyValuePair.Key.BulkInsert(keyValuePair.Value.ToList());
|
||||
}
|
||||
startNew.Stop();
|
||||
Console.WriteLine($"订单总数:{i}条,myShardingDbContext.BulkInsert(orders)用时:{startNew.ElapsedMilliseconds}毫秒");
|
||||
|
||||
Console.WriteLine("ok");
|
||||
}
|
||||
var b = DateTime.Now.Date.AddDays(-2);
|
||||
var queryable = myShardingDbContext.Set<Order>().Where(o => o.CreateTime >= b).OrderBy(o => o.CreateTime);
|
||||
var startNew1 = Stopwatch.StartNew();
|
||||
|
||||
int skip = 0, take = 10;
|
||||
for (int i = 10000; i < 20000; i++)
|
||||
{
|
||||
skip = take * i;
|
||||
startNew1.Restart();
|
||||
var shardingPagedResult = queryable.ToShardingPage(i+1, take);
|
||||
startNew1.Stop();
|
||||
Console.WriteLine($"流式分页skip:[{skip}],take:[{take}]耗时用时:{startNew1.ElapsedMilliseconds}毫秒");
|
||||
}
|
||||
|
||||
Console.WriteLine("ok");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EFCore.BulkExtensions" Version="5.3.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ShardingCore\ShardingCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -101,7 +101,7 @@ namespace Samples.AbpSharding
|
|||
throw new ShardingCoreException("multi route not support track");
|
||||
if(!(routeTail is ISingleQueryRouteTail singleQueryRouteTail))
|
||||
throw new ShardingCoreException("multi route not support track");
|
||||
var cacheKey = routeTail.GetRouteTailIdenty();
|
||||
var cacheKey = routeTail.GetRouteTailIdentity();
|
||||
if (!_dbContextCaches.TryGetValue(cacheKey, out var dbContext))
|
||||
{
|
||||
dbContext = _shardingDbContextFactory.Create(ShardingDbContextType, GetShareShardingDbContextOptions(routeTail));
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace ShardingCore.Core.VirtualRoutes.RouteTails.Abstractions
|
|||
*/
|
||||
public interface IRouteTail
|
||||
{
|
||||
string GetRouteTailIdenty();
|
||||
string GetRouteTailIdentity();
|
||||
bool IsMultiEntityQuery();
|
||||
}
|
||||
}
|
|
@ -27,7 +27,7 @@ namespace ShardingCore.Core.VirtualRoutes.RouteTails
|
|||
_modelCacheKey = RANDOM_MODEL_CACHE_KEY+Guid.NewGuid().ToString("n");
|
||||
_entityTypes = routeResult.ReplaceTables.Select(o=>o.EntityType).ToHashSet();
|
||||
}
|
||||
public string GetRouteTailIdenty()
|
||||
public string GetRouteTailIdentity()
|
||||
{
|
||||
return _modelCacheKey;
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ namespace ShardingCore.Core.VirtualRoutes.RouteTails
|
|||
_tail= tail;
|
||||
_modelCacheKey = _tail.FormatRouteTail2ModelCacheKey();
|
||||
}
|
||||
public virtual string GetRouteTailIdenty()
|
||||
public virtual string GetRouteTailIdentity()
|
||||
{
|
||||
return _modelCacheKey;
|
||||
}
|
||||
|
|
|
@ -1,20 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Saturday, 19 December 2020 19:55:24
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Saturday, 19 December 2020 19:55:24
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractShardingOperatorVirtualTableRoute<T, TKey> : AbstractShardingFilterVirtualTableRoute<T, TKey> where T : class, IShardingTable
|
||||
{
|
||||
|
||||
|
@ -37,9 +38,9 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
|
|||
|
||||
public override IPhysicTable RouteWithValue(List<IPhysicTable> allPhysicTables, object shardingKey)
|
||||
{
|
||||
var filter = GetRouteToFilter(ConvertToShardingKey(shardingKey), ShardingOperatorEnum.Equal).Compile();
|
||||
var shardingKeyToTail = ShardingKeyToTail(ConvertToShardingKey(shardingKey));
|
||||
|
||||
var physicTables = allPhysicTables.Where(o => filter(o.Tail)).ToList();
|
||||
var physicTables = allPhysicTables.Where(o => o.Tail== shardingKeyToTail).ToList();
|
||||
if (physicTables.IsEmpty())
|
||||
{
|
||||
var routeConfig = ShardingKeyUtil.Parse(typeof(T));
|
||||
|
|
|
@ -1,25 +1,23 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.PaginationConfigurations;
|
||||
using ShardingCore.Utils;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace ShardingCore.Core.VirtualTables
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 18 December 2020 14:20:12
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 18 December 2020 14:20:12
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
/// <summary>
|
||||
/// 同数据库虚拟表
|
||||
/// </summary>
|
||||
|
|
|
@ -17,10 +17,10 @@ namespace ShardingCore.EFCores
|
|||
{
|
||||
public object Create(DbContext context)
|
||||
{
|
||||
if (context is IShardingTableDbContext shardingTableDbContext&&!string.IsNullOrWhiteSpace(shardingTableDbContext.RouteTail.GetRouteTailIdenty()))
|
||||
if (context is IShardingTableDbContext shardingTableDbContext&&!string.IsNullOrWhiteSpace(shardingTableDbContext.RouteTail.GetRouteTailIdentity()))
|
||||
{
|
||||
|
||||
return $"{context.GetType()}_{shardingTableDbContext.RouteTail.GetRouteTailIdenty()}";
|
||||
return $"{context.GetType()}_{shardingTableDbContext.RouteTail.GetRouteTailIdentity()}";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.VirtualRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
|
@ -61,7 +66,6 @@ namespace ShardingCore.Extensions
|
|||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据对象集合解析
|
||||
/// </summary>
|
||||
|
@ -72,15 +76,59 @@ namespace ShardingCore.Extensions
|
|||
public static IDictionary<DbContext, IEnumerable<TEntity>> BulkShardingEnumerable<TEntity>(this IShardingDbContext shardingDbContext,
|
||||
IEnumerable<TEntity> entities) where TEntity : class
|
||||
{
|
||||
return entities.Select(o =>
|
||||
var entityType = typeof(TEntity);
|
||||
var routeTailFactory = ShardingContainer.GetService<IRouteTailFactory>();
|
||||
if (!entityType.IsShardingTable())
|
||||
{
|
||||
var dbContext = shardingDbContext.CreateGenericDbContext(o);
|
||||
return new
|
||||
var routeTail = routeTailFactory.Create(string.Empty);
|
||||
var dbContext = shardingDbContext.GetDbContext(true, routeTail);
|
||||
return new Dictionary<DbContext, IEnumerable<TEntity>>()
|
||||
{
|
||||
DbContext = dbContext,
|
||||
Entity = o
|
||||
{dbContext,entities }
|
||||
};
|
||||
}).GroupBy(g => g.DbContext).ToDictionary(o=>o.Key,o=>o.Select(g=>g.Entity));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var virtualTableManager = ShardingContainer.GetService<IVirtualTableManager>();
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(shardingDbContext.ShardingDbContextType, entityType);
|
||||
|
||||
var virtualTableRoute = virtualTable.GetVirtualRoute();
|
||||
var hashSet = virtualTableRoute.GetAllTails().ToHashSet();
|
||||
var dic = new Dictionary<string, BulkDicEntry<TEntity>>();
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var shardingKey = entity.GetPropertyValue(virtualTable.ShardingConfig.ShardingField);
|
||||
var tail = virtualTableRoute.ShardingKeyToTail(shardingKey);
|
||||
if (!hashSet.Contains(tail))
|
||||
throw new ShardingKeyRouteNotMatchException(
|
||||
$"Entity:{entityType.FullName},ShardingKey:{shardingKey},ShardingTail:{tail}");
|
||||
|
||||
var routeTail = routeTailFactory.Create(tail);
|
||||
var routeTailIdentity = routeTail.GetRouteTailIdentity();
|
||||
if (!dic.TryGetValue(routeTailIdentity, out var bulkDicEntry))
|
||||
{
|
||||
var dbContext = shardingDbContext.GetDbContext(true, routeTail);
|
||||
bulkDicEntry = new BulkDicEntry<TEntity>(dbContext, new LinkedList<TEntity>());
|
||||
dic.Add(routeTailIdentity, bulkDicEntry);
|
||||
}
|
||||
|
||||
bulkDicEntry.InnerEntities.AddLast(entity);
|
||||
}
|
||||
|
||||
return dic.Values.ToDictionary(o => o.InnerDbContext, o => o.InnerEntities.Select(t => t));
|
||||
}
|
||||
}
|
||||
internal class BulkDicEntry<TEntity>
|
||||
{
|
||||
public BulkDicEntry(DbContext innerDbContext, LinkedList<TEntity> innerEntities)
|
||||
{
|
||||
InnerDbContext = innerDbContext;
|
||||
InnerEntities = innerEntities;
|
||||
}
|
||||
|
||||
public DbContext InnerDbContext { get; }
|
||||
public LinkedList<TEntity> InnerEntities { get; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据条件表达式解析
|
||||
|
|
|
@ -34,7 +34,7 @@ namespace ShardingCore.Sharding
|
|||
/// 分表分库的dbcontext
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class AbstractShardingDbContext<T> : DbContext, IShardingDbContext<T>, IShardingTransaction,IShardingReadWriteSupport where T : DbContext, IShardingTableDbContext
|
||||
public abstract class AbstractShardingDbContext<T> : DbContext, IShardingDbContext<T>, IShardingTransaction, IShardingReadWriteSupport where T : DbContext, IShardingTableDbContext
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, DbContext> _dbContextCaches = new ConcurrentDictionary<string, DbContext>();
|
||||
private readonly IShardingConfigOption shardingConfigOption;
|
||||
|
@ -55,12 +55,12 @@ namespace ShardingCore.Sharding
|
|||
_routeTailFactory = ShardingContainer.GetService<IRouteTailFactory>();
|
||||
_shardingDbContextOptionsBuilderConfig = ShardingContainer
|
||||
.GetService<IEnumerable<IShardingDbContextOptionsBuilderConfig>>()
|
||||
.FirstOrDefault(o => o.ShardingDbContextType == ShardingDbContextType)??throw new ArgumentNullException(nameof(IShardingDbContextOptionsBuilderConfig));
|
||||
.FirstOrDefault(o => o.ShardingDbContextType == ShardingDbContextType) ?? throw new ArgumentNullException(nameof(IShardingDbContextOptionsBuilderConfig));
|
||||
|
||||
_connectionStringManager = ShardingContainer.GetService<IEnumerable<IConnectionStringManager>>()
|
||||
.FirstOrDefault(o => o.ShardingDbContextType == ShardingDbContextType) ?? throw new ArgumentNullException(nameof(IConnectionStringManager));
|
||||
|
||||
shardingConfigOption =ShardingContainer.GetService<IEnumerable<IShardingConfigOption>>().FirstOrDefault(o=>o.ShardingDbContextType==ShardingDbContextType&&o.ActualDbContextType==typeof(T)) ?? throw new ArgumentNullException(nameof(IShardingConfigOption));
|
||||
shardingConfigOption = ShardingContainer.GetService<IEnumerable<IShardingConfigOption>>().FirstOrDefault(o => o.ShardingDbContextType == ShardingDbContextType && o.ActualDbContextType == typeof(T)) ?? throw new ArgumentNullException(nameof(IShardingConfigOption));
|
||||
if (shardingConfigOption.UseReadWrite)
|
||||
{
|
||||
_readWriteOptions = ShardingContainer
|
||||
|
@ -152,15 +152,15 @@ namespace ShardingCore.Sharding
|
|||
throw new ShardingCoreException("multi route not support track");
|
||||
if (!(routeTail is ISingleQueryRouteTail singleQueryRouteTail))
|
||||
throw new ShardingCoreException("multi route not support track");
|
||||
var cacheKey = routeTail.GetRouteTailIdenty();
|
||||
var cacheKey = routeTail.GetRouteTailIdentity();
|
||||
if (!_dbContextCaches.TryGetValue(cacheKey, out var dbContext))
|
||||
{
|
||||
dbContext = _shardingDbContextFactory.Create(ShardingDbContextType, GetShareShardingDbContextOptions(routeTail));
|
||||
_dbContextCaches.TryAdd(cacheKey, dbContext);
|
||||
}
|
||||
|
||||
if (IsBeginTransaction)
|
||||
dbContext.Database.UseTransaction(Database.CurrentTransaction.GetDbTransaction());
|
||||
|
||||
_dbContextCaches.TryAdd(cacheKey, dbContext);
|
||||
}
|
||||
return dbContext;
|
||||
}
|
||||
else
|
||||
|
@ -187,7 +187,7 @@ namespace ShardingCore.Sharding
|
|||
{
|
||||
if (typeof(TEntity).IsShardingTable())
|
||||
{
|
||||
var physicTable = _virtualTableManager.GetVirtualTable(ShardingDbContextType, typeof(TEntity)).RouteTo(new TableRouteConfig(predicate:@where));
|
||||
var physicTable = _virtualTableManager.GetVirtualTable(ShardingDbContextType, typeof(TEntity)).RouteTo(new TableRouteConfig(predicate: @where));
|
||||
if (physicTable.IsEmpty())
|
||||
throw new ShardingCoreException($"{@where.ShardingPrint()} cant found ant physic table");
|
||||
return physicTable.Select(o => GetDbContext(true, _routeTailFactory.Create(o.Tail)));
|
||||
|
@ -472,7 +472,7 @@ namespace ShardingCore.Sharding
|
|||
int i = 0;
|
||||
if (!isBeginTransaction)
|
||||
{
|
||||
using(var tran= Database.BeginTransaction())
|
||||
using (var tran = Database.BeginTransaction())
|
||||
{
|
||||
|
||||
foreach (var dbContextCache in _dbContextCaches)
|
||||
|
|
|
@ -94,13 +94,13 @@ namespace ShardingCore.Sharding.ShardingQueryExecutors
|
|||
}
|
||||
}
|
||||
|
||||
if (paginationMetadata.EnableUnevenShardingPage)
|
||||
{
|
||||
if (paginationMetadata.IsUseUneven(_shardingPageManager.Current.RouteQueryResults, _streamMergeContext.Skip.GetValueOrDefault()))
|
||||
{
|
||||
//if (paginationMetadata.EnableUnevenShardingPage)
|
||||
//{
|
||||
// if (paginationMetadata.IsUseUneven(_shardingPageManager.Current.RouteQueryResults, _streamMergeContext.Skip.GetValueOrDefault()))
|
||||
// {
|
||||
|
||||
}
|
||||
}
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Days
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Wednesday, 27 January 2021 08:41:05
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Wednesday, 27 January 2021 08:41:05
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<T>:AbstractShardingTimeKeyDateTimeVirtualTableRoute<T> where T:class,IShardingTable
|
||||
{
|
||||
public abstract DateTime GetBeginTime();
|
||||
|
@ -41,6 +41,7 @@ namespace ShardingCore.VirtualRoutes.Days
|
|||
|
||||
protected override Expression<Func<string, bool>> GetRouteToFilter(DateTime shardingKey, ShardingOperatorEnum shardingOperator)
|
||||
{
|
||||
|
||||
var t = TimeFormatToTail(shardingKey);
|
||||
switch (shardingOperator)
|
||||
{
|
||||
|
@ -67,5 +68,6 @@ namespace ShardingCore.VirtualRoutes.Days
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue