完成所有单元测试的编译

This commit is contained in:
xuejiaming 2022-07-02 21:43:19 +08:00
parent f4625cd8e7
commit 9aca4866ce
49 changed files with 1194 additions and 752 deletions

View File

@ -110,7 +110,7 @@ namespace Sample.MySql
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.ApplicationServices.UseAutoShardingCore(); app.ApplicationServices.UseAutoShardingCreate();
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();

View File

@ -11,6 +11,6 @@ namespace ShardingCore.Bootstrappers
/// </summary> /// </summary>
public interface IShardingBootstrapper public interface IShardingBootstrapper
{ {
void AutoShardingTable(); void AutoShardingCreate();
} }
} }

View File

@ -35,7 +35,7 @@ namespace ShardingCore.Bootstrappers
_shardingProvider = shardingProvider; _shardingProvider = shardingProvider;
_dbContextCreator = dbContextCreator; _dbContextCreator = dbContextCreator;
} }
public void AutoShardingTable() public void AutoShardingCreate()
{ {
CheckRequirement(); CheckRequirement();
StartAutoShardingJob(); StartAutoShardingJob();

View File

@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.QueryTrackers; using ShardingCore.Core.QueryTrackers;
using ShardingCore.Core.ShardingPage.Abstractions; using ShardingCore.Core.ShardingPage.Abstractions;
using ShardingCore.Core.TrackerManagers; using ShardingCore.Core.TrackerManagers;
@ -14,14 +15,18 @@ using ShardingCore.DynamicDataSources;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableCreator;
namespace ShardingCore.Core.RuntimeContexts namespace ShardingCore.Core.RuntimeContexts
{ {
public interface IShardingRuntimeContext public interface IShardingRuntimeContext
{ {
IShardingComparer GetShardingComparer();
IShardingCompilerExecutor GetShardingCompilerExecutor(); IShardingCompilerExecutor GetShardingCompilerExecutor();
IShardingReadWriteManager GetShardingReadWriteManager(); IShardingReadWriteManager GetShardingReadWriteManager();
IShardingRouteManager GetShardingRouteManager();
ITrackerManager GetTrackerManager(); ITrackerManager GetTrackerManager();
IParallelTableManager GetParallelTableManager(); IParallelTableManager GetParallelTableManager();
IDbContextCreator GetDbContextCreator(); IDbContextCreator GetDbContextCreator();
@ -29,7 +34,9 @@ namespace ShardingCore.Core.RuntimeContexts
// IVirtualDataSourceManager GetVirtualDataSourceManager(); // IVirtualDataSourceManager GetVirtualDataSourceManager();
IVirtualDataSource GetVirtualDataSource(); IVirtualDataSource GetVirtualDataSource();
ITableRouteManager GetTableRouteManager(); ITableRouteManager GetTableRouteManager();
IShardingTableCreator GetShardingTableCreator();
IRouteTailFactory GetRouteTailFactory(); IRouteTailFactory GetRouteTailFactory();
IReadWriteConnectorFactory GetReadWriteConnectorFactory();
IQueryTracker GetQueryTracker(); IQueryTracker GetQueryTracker();
IUnionAllMergeManager GetUnionAllMergeManager(); IUnionAllMergeManager GetUnionAllMergeManager();
IShardingPageManager GetShardingPageManager(); IShardingPageManager GetShardingPageManager();
@ -41,7 +48,7 @@ namespace ShardingCore.Core.RuntimeContexts
void UseApplicationServiceProvider(IServiceProvider applicationServiceProvider); void UseApplicationServiceProvider(IServiceProvider applicationServiceProvider);
void Initialize(); void Initialize();
void AutoShardingTable(); void AutoShardingCreate();
object GetService(Type serviceType); object GetService(Type serviceType);
TService GetService<TService>(); TService GetService<TService>();
object GetRequiredService(Type serviceType); object GetRequiredService(Type serviceType);

View File

@ -19,6 +19,8 @@ using ShardingCore.Logger;
using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableCreator;
namespace ShardingCore.Core.RuntimeContexts namespace ShardingCore.Core.RuntimeContexts
{ {
@ -51,13 +53,17 @@ namespace ShardingCore.Core.RuntimeContexts
isInited = true; isInited = true;
_serviceProvider = _serviceMap.BuildServiceProvider(); _serviceProvider = _serviceMap.BuildServiceProvider();
_serviceProvider.GetRequiredService<IShardingInitializer>().Initialize(); _serviceProvider.GetRequiredService<IShardingInitializer>().Initialize();
} }
} }
public void AutoShardingTable() public void AutoShardingCreate()
{ {
GetRequiredService<IShardingBootstrapper>().AutoShardingTable(); GetRequiredService<IShardingBootstrapper>().AutoShardingCreate();
}
public IShardingComparer GetShardingComparer()
{
return GetRequiredService<IShardingComparer>();
} }
public IShardingCompilerExecutor GetShardingCompilerExecutor() public IShardingCompilerExecutor GetShardingCompilerExecutor()
@ -70,6 +76,7 @@ namespace ShardingCore.Core.RuntimeContexts
return GetRequiredService<IShardingReadWriteManager>(); return GetRequiredService<IShardingReadWriteManager>();
} }
public ITrackerManager GetTrackerManager() public ITrackerManager GetTrackerManager()
{ {
return GetRequiredService<ITrackerManager>(); return GetRequiredService<ITrackerManager>();
@ -100,6 +107,16 @@ namespace ShardingCore.Core.RuntimeContexts
return GetRequiredService<ITableRouteManager>(); return GetRequiredService<ITableRouteManager>();
} }
public IReadWriteConnectorFactory GetReadWriteConnectorFactory()
{
return GetRequiredService<IReadWriteConnectorFactory>();
}
public IShardingTableCreator GetShardingTableCreator()
{
return GetRequiredService<IShardingTableCreator>();
}
public IRouteTailFactory GetRouteTailFactory() public IRouteTailFactory GetRouteTailFactory()
{ {
return GetRequiredService<IRouteTailFactory>(); return GetRequiredService<IRouteTailFactory>();

View File

@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.EFCores
{
public interface IShardingDbContextAvailable
{
IShardingDbContext GetShardingDbContext();
}
}

View File

@ -10,6 +10,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core; using ShardingCore.Core;
using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.RuntimeContexts;
@ -19,7 +20,7 @@ namespace ShardingCore.EFCores
/// <summary> /// <summary>
/// 当前查询编译拦截 /// 当前查询编译拦截
/// </summary> /// </summary>
public class ShardingQueryCompiler : IQueryCompiler public class ShardingQueryCompiler : IQueryCompiler,IShardingDbContextAvailable
{ {
private readonly IShardingDbContext _shardingDbContext; private readonly IShardingDbContext _shardingDbContext;
private readonly IShardingCompilerExecutor _shardingCompilerExecutor; private readonly IShardingCompilerExecutor _shardingCompilerExecutor;
@ -87,5 +88,9 @@ namespace ShardingCore.EFCores
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endif #endif
public IShardingDbContext GetShardingDbContext()
{
return _shardingDbContext;
}
} }
} }

View File

@ -1,66 +1,106 @@
// using System.Collections.Generic; using System.Collections.Generic;
// using System.Linq; using System.Linq;
// using System.Threading.Tasks; using System.Threading.Tasks;
// using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
// using ShardingCore.Core.ShardingPage.Abstractions; using Microsoft.EntityFrameworkCore.Query.Internal;
// using ShardingCore.EFCores;
// namespace ShardingCore.Extensions.ShardingPageExtensions using ShardingCore.Exceptions;
// { using ShardingCore.Sharding.Abstractions;
// /*
// * @Author: xjm namespace ShardingCore.Extensions.ShardingPageExtensions
// * @Description: {
// * @Date: 2021/9/3 10:36:51 /*
// * @Ver: 1.0 * @Author: xjm
// * @Email: 326308290@qq.com * @Description:
// */ * @Date: 2021/9/3 10:36:51
// public static class ShardingPageExtension * @Ver: 1.0
// { * @Email: 326308290@qq.com
// public static async Task<ShardingPagedResult<T>> ToShardingPageAsync<T>(this IQueryable<T> source, int pageIndex, int pageSize) */
// { public static class ShardingPageExtension
// //设置每次获取多少页 {
// var take = pageSize <= 0 ? 1 : pageSize; private static IShardingDbContext GetShardingDbContext<T>(IQueryable<T> source)
// //设置当前页码最小1 {
// var index = pageIndex <= 0 ? 1 : pageIndex;
// //需要跳过多少页 var entityQueryProvider = source.Provider as EntityQueryProvider??throw new ShardingCoreInvalidOperationException($"cant use sharding page that {nameof(IQueryable)} provider not {nameof(EntityQueryProvider)}");
// var skip = (index - 1) * take;
// var shardingPageManager = ShardingContainer.GetService<IShardingPageManager>(); var shardingQueryCompiler = entityQueryProvider.GetFieldValue("_queryCompiler") as ShardingQueryCompiler??throw new ShardingCoreInvalidOperationException($"cant use sharding page that {nameof(EntityQueryProvider)} not contains {nameof(ShardingQueryCompiler)} filed named _queryCompiler");
// using (shardingPageManager.CreateScope()) var dbContextAvailable = shardingQueryCompiler as IShardingDbContextAvailable;
// { if (dbContextAvailable == null)
// //获取每次总记录数 {
// var count = await source.CountAsync(); throw new ShardingCoreInvalidOperationException($"cant use sharding page that {nameof(ShardingQueryCompiler)} not impl {nameof(IShardingDbContextAvailable)}");
// if (count <= skip) }
// return new ShardingPagedResult<T>(new List<T>(0), count);
// //获取剩余条数 return dbContextAvailable.GetShardingDbContext();
// var remainingCount = count - skip; }
// //当剩余条数小于take数就取remainingCount /// <summary>
// var realTake = remainingCount < take ? remainingCount : take; /// 配置了分页configuration
// var data = await source.Skip(skip).Take(realTake).ToListAsync(); /// count+list的分页list会根据count进行优化
// return new ShardingPagedResult<T>(data, count); /// </summary>
// } /// <param name="source"></param>
// } /// <param name="pageIndex"></param>
// public static ShardingPagedResult<T> ToShardingPage<T>(this IQueryable<T> source, int pageIndex, int pageSize) /// <param name="pageSize"></param>
// { /// <typeparam name="T"></typeparam>
// //设置每次获取多少页 /// <returns></returns>
// var take = pageSize <= 0 ? 1 : pageSize; public static async Task<ShardingPagedResult<T>> ToShardingPageAsync<T>(this IQueryable<T> source, int pageIndex, int pageSize)
// //设置当前页码最小1 {
// var index = pageIndex <= 0 ? 1 : pageIndex; var shardingDbContext = GetShardingDbContext(source);
// //需要跳过多少页 var shardingRuntimeContext = shardingDbContext.GetShardingRuntimeContext();
// var skip = (index - 1) * take;
// //设置每次获取多少页
// var shardingPageManager = ShardingContainer.GetService<IShardingPageManager>(); var take = pageSize <= 0 ? 1 : pageSize;
// using (shardingPageManager.CreateScope()) //设置当前页码最小1
// { var index = pageIndex <= 0 ? 1 : pageIndex;
// //获取每次总记录数 //需要跳过多少页
// var count = source.Count(); var skip = (index - 1) * take;
// if (count <= skip) var shardingPageManager = shardingRuntimeContext.GetShardingPageManager();
// return new ShardingPagedResult<T>(new List<T>(0), count); using (shardingPageManager.CreateScope())
// //获取剩余条数 {
// var remainingCount = count - skip; //获取每次总记录数
// //当剩余条数小于take数就取remainingCount var count = await source.CountAsync();
// var realTake = remainingCount < take ? remainingCount : take; if (count <= skip)
// var data = source.Skip(skip).Take(realTake).ToList(); return new ShardingPagedResult<T>(new List<T>(0), count);
// return new ShardingPagedResult<T>(data, count); //获取剩余条数
// } var remainingCount = count - skip;
// } //当剩余条数小于take数就取remainingCount
// } var realTake = remainingCount < take ? remainingCount : take;
// } var data = await source.Skip(skip).Take(realTake).ToListAsync();
return new ShardingPagedResult<T>(data, count);
}
}
/// <summary>
/// 配置了分页configuration
/// count+list的分页list会根据count进行优化
/// </summary>
/// <param name="source"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static ShardingPagedResult<T> ToShardingPage<T>(this IQueryable<T> source, int pageIndex, int pageSize)
{
var shardingDbContext = GetShardingDbContext(source);
var shardingRuntimeContext = shardingDbContext.GetShardingRuntimeContext();
//设置每次获取多少页
var take = pageSize <= 0 ? 1 : pageSize;
//设置当前页码最小1
var index = pageIndex <= 0 ? 1 : pageIndex;
//需要跳过多少页
var skip = (index - 1) * take;
var shardingPageManager = shardingRuntimeContext.GetShardingPageManager();
using (shardingPageManager.CreateScope())
{
//获取每次总记录数
var count = source.Count();
if (count <= skip)
return new ShardingPagedResult<T>(new List<T>(0), count);
//获取剩余条数
var remainingCount = count - skip;
//当剩余条数小于take数就取remainingCount
var realTake = remainingCount < take ? remainingCount : take;
var data = source.Skip(skip).Take(realTake).ToList();
return new ShardingPagedResult<T>(data, count);
}
}
}
}

View File

@ -216,10 +216,25 @@ namespace ShardingCore
/// 使用自动创建表 /// 使用自动创建表
/// </summary> /// </summary>
/// <param name="serviceProvider"></param> /// <param name="serviceProvider"></param>
public static void UseAutoShardingCore(this IServiceProvider serviceProvider) public static void UseAutoShardingCreate(this IServiceProvider serviceProvider)
{ {
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>(); var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
shardingRuntimeContext.AutoShardingTable(); shardingRuntimeContext.AutoShardingCreate();
}
/// <summary>
/// 自动尝试补偿表
/// </summary>
/// <param name="serviceProvider"></param>
public static void UseAutoTryCompensateTable(this IServiceProvider serviceProvider)
{
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer();
var allDataSourceNames = virtualDataSource.GetAllDataSourceNames();
foreach (var dataSourceName in allDataSourceNames)
{
dataSourceInitializer.InitConfigure(dataSourceName,true,true);
}
} }

View File

@ -6,10 +6,8 @@ namespace ShardingCore.Test.Domain.Entities
public class Order public class Order
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
[ShardingDataSourceKey]
public string Area { get; set; } public string Area { get; set; }
public long Money { get; set; } public long Money { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,15 +7,12 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions; using ShardingCore.Extensions.ShardingPageExtensions;
@ -36,50 +32,39 @@ using Xunit;
namespace ShardingCore.Test namespace ShardingCore.Test
{ {
/*
* @Author: xjm
* @Description:
* @Date: Friday, 15 January 2021 17:22:10
* @Email: 326308290@qq.com
*/
public class ShardingTest public class ShardingTest
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly ITableRouteRuleEngineFactory<ShardingDefaultDbContext> _tableRouteRuleEngineFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory, ITableRouteRuleEngineFactory<ShardingDefaultDbContext> tableRouteRuleEngineFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
_tableRouteRuleEngineFactory = tableRouteRuleEngineFactory; var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault());
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
@ -129,8 +114,8 @@ namespace ShardingCore.Test
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(), null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(), null); var y = new EntityMetadata(typeof(LogDay));
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
@ -204,8 +189,6 @@ namespace ShardingCore.Test
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity(); var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity();
var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity(); var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity();
var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity(); var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity();
@ -287,7 +270,7 @@ namespace ShardingCore.Test
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
[Fact] [Fact]
@ -938,17 +921,11 @@ namespace ShardingCore.Test
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var tableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName =tableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName);
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator)); Assert.True(string.IsNullOrWhiteSpace(tableRoute.EntityMetadata.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -1184,9 +1161,9 @@ namespace ShardingCore.Test
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -1194,9 +1171,9 @@ namespace ShardingCore.Test
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {

View File

@ -3,10 +3,7 @@ using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
@ -18,13 +15,15 @@ using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
using ShardingCore.Test.Domain.Entities; using ShardingCore.Test.Domain.Entities;
using ShardingCore.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Extensions.ShardingQueryableExtensions; using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using Xunit; using Xunit;
namespace ShardingCore.Test namespace ShardingCore.Test
@ -35,38 +34,34 @@ namespace ShardingCore.Test
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
@ -116,8 +111,10 @@ namespace ShardingCore.Test
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); //, "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
var y = new EntityMetadata(typeof(LogDay));
//, "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
@ -185,8 +182,6 @@ namespace ShardingCore.Test
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>() var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>()
{ new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) }); { new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) });
@ -246,7 +241,7 @@ namespace ShardingCore.Test
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
//[Fact] //[Fact]
@ -725,17 +720,10 @@ namespace ShardingCore.Test
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var virtualTableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName = virtualTableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName); Assert.True(string.IsNullOrWhiteSpace(virtualTableRoute.EntityMetadata.TableSeparator));
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -964,9 +952,9 @@ namespace ShardingCore.Test
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -974,9 +962,9 @@ namespace ShardingCore.Test
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {

View File

@ -30,7 +30,7 @@ namespace ShardingCore.Test.Shardings
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;

View File

@ -35,7 +35,7 @@ namespace ShardingCore.Test.Shardings
return true; return true;
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;

View File

@ -4,8 +4,6 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Extensions;
using ShardingCore.Test.Domain.Entities; using ShardingCore.Test.Domain.Entities;
using ShardingCore.VirtualRoutes.Years; using ShardingCore.VirtualRoutes.Years;

View File

@ -39,7 +39,7 @@ namespace ShardingCore.Test.Shardings
public override void Configure(EntityMetadataDataSourceBuilder<Order> builder) public override void Configure(EntityMetadataDataSourceBuilder<Order> builder)
{ {
builder.ShardingProperty(o => o.Area);
} }
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)

View File

@ -15,16 +15,16 @@ namespace ShardingCore.Test.Shardings
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var allTails = base.GetAllTails(); var allTails = base.CalcTailsOnStart();
allTails.Add("202112"); allTails.Add("202112");
return allTails; return allTails;
} }
public override void Configure(EntityMetadataTableBuilder<Order> builder) public override void Configure(EntityMetadataTableBuilder<Order> builder)
{ {
builder.ShardingProperty(o => o.CreateTime);
} }
public override IPaginationConfiguration<Order> CreatePaginationConfiguration() public override IPaginationConfiguration<Order> CreatePaginationConfiguration()

View File

@ -24,8 +24,7 @@ namespace ShardingCore.Test.Shardings
return TimeFormatToTail(time); return TimeFormatToTail(time);
} }
public override List<string> GetTails()
public override List<string> GetAllTails()
{ {
var beginTime = new DateTime(2020, 1, 1); var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1); var endTime = new DateTime(2021, 12, 1);

View File

@ -10,6 +10,7 @@ using ShardingCore.Bootstrappers;
using ShardingCore.Helpers; using ShardingCore.Helpers;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.TableExists; using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
using ShardingCore.Test.Domain.Entities; using ShardingCore.Test.Domain.Entities;
using ShardingCore.Test.Shardings; using ShardingCore.Test.Shardings;
@ -35,12 +36,8 @@ namespace ShardingCore.Test
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
{ {
services.AddShardingDbContext<ShardingDefaultDbContext>() services.AddShardingDbContext<ShardingDefaultDbContext>()
.AddEntityConfig(op => .UseRouteConfig(op =>
{ {
//如果您使用code-first建议选择false
op.CreateShardingTableOnStart = true;
//如果您使用code-first建议修改为fsle
op.EnsureCreatedWithOutShardingTable = true;
//当无法获取路由时会返回默认值而不是报错 //当无法获取路由时会返回默认值而不是报错
op.ThrowIfQueryRouteNotMatch = false; op.ThrowIfQueryRouteNotMatch = false;
@ -59,10 +56,8 @@ namespace ShardingCore.Test
op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>(); op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>();
}) })
.AddConfig(op => .UseConfig(op =>
{ {
op.ConfigId="c1";
op.Priority = 1;
op.UseShardingQuery((conStr, builder) => op.UseShardingQuery((conStr, builder) =>
{ {
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger); builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
@ -94,15 +89,14 @@ namespace ShardingCore.Test
} }
}; };
}, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); }, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime);
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<ShardingDefaultDbContext>()); }).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>(ServiceLifetime.Singleton).AddShardingCore();
}).EnsureConfig();
} }
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
public void Configure(IServiceProvider serviceProvider) public void Configure(IServiceProvider serviceProvider)
{ {
var shardingBootstrapper = serviceProvider.GetService<IShardingBootstrapper>(); var shardingBootstrapper = serviceProvider.GetRequiredService<IShardingBootstrapper>();
shardingBootstrapper.Start(); shardingBootstrapper.AutoShardingCreate();
// 有一些测试数据要初始化可以放在这里 // 有一些测试数据要初始化可以放在这里
InitData(serviceProvider).GetAwaiter().GetResult(); InitData(serviceProvider).GetAwaiter().GetResult();
} }

View File

@ -6,10 +6,8 @@ namespace ShardingCore.Test2x.Domain.Entities
public class Order public class Order
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
[ShardingDataSourceKey]
public string Area { get; set; } public string Area { get; set; }
public long Money { get; set; } public long Money { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
} }

View File

@ -6,7 +6,7 @@ namespace ShardingCore.Test2x.Domain.Entities
* @Date: Thursday, 14 January 2021 15:36:43 * @Date: Thursday, 14 January 2021 15:36:43
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public class SysUserMod public class SysUserMod: IId
{ {
/// <summary> /// <summary>
/// 用户Id用于分表 /// 用户Id用于分表
@ -23,4 +23,10 @@ namespace ShardingCore.Test2x.Domain.Entities
public int AgeGroup { get; set; } public int AgeGroup { get; set; }
} }
public interface IId
{
string Id { get; set; }
}
} }

View File

@ -7,11 +7,10 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
@ -25,6 +24,7 @@ using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions; using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Sharding.ShardingDbContextExecutors; using ShardingCore.Sharding.ShardingDbContextExecutors;
using ShardingCore.Sharding.StreamMergeEngines;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
using ShardingCore.Test2x.Domain.Entities; using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.Utils; using ShardingCore.Utils;
@ -32,48 +32,39 @@ using Xunit;
namespace ShardingCore.Test2x namespace ShardingCore.Test2x
{ {
/*
* @Author: xjm
* @Description:
* @Date: Friday, 15 January 2021 17:22:10
* @Email: 326308290@qq.com
*/
public class ShardingTest public class ShardingTest
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
@ -81,32 +72,32 @@ namespace ShardingCore.Test2x
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer(); // var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>(); // var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute(); // var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
// // var xxxx = "202102";
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102); // var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata, // var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102); // var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata, // var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var xxxx1 = 202102; // var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1); // var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata, // var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101); // var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata, // var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101); // var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata, // var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101); // var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata, // var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth); // var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata, // var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth); // var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata, // var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2)); // Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3)); // Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4)); // Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
@ -123,12 +114,14 @@ namespace ShardingCore.Test2x
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var y = new EntityMetadata(typeof(LogDay));
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
var bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays); var bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays);
Assert.Equal(100, bulkShardingTableEnumerable.Count); Assert.Equal(100, bulkShardingTableEnumerable.Count);
var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays); var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays);
Assert.Equal(1, bulkShardingEnumerable.Count); Assert.Equal(1, bulkShardingEnumerable.Count);
@ -182,12 +175,12 @@ namespace ShardingCore.Test2x
Assert.True(bulkShardingExpression.ContainsKey("B")); Assert.True(bulkShardingExpression.ContainsKey("B"));
var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString()); var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString());
Assert.Equal(1, bulkShardingTableExpression.Count()); Assert.Equal(1, bulkShardingTableExpression.Count());
var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123"); var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123");
Assert.Equal(1, noShardingExpression.Count()); Assert.Equal(1, noShardingExpression.Count());
var isShardingDbContext = _virtualDbContext.IsShardingDbContext(); var isShardingDbContext = _virtualDbContext.IsShardingDbContext();
Assert.True(isShardingDbContext); Assert.True(isShardingDbContext);
var isShardingTableDbContext = _virtualDbContext.IsShardingTableDbContext(); var isShardingTableDbContext = _virtualDbContext.IsShardingTableDbContext();
@ -196,9 +189,6 @@ namespace ShardingCore.Test2x
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity(); var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity();
var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity(); var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity();
var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity(); var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity();
@ -221,6 +211,10 @@ namespace ShardingCore.Test2x
{ new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), }); { new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), });
Assert.Equal(x1x1, x2x2); Assert.Equal(x1x1, x2x2);
Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode()); Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode());
var succeedAddConnectionString = _shardingConnectionStringResolver.AddConnectionString("X", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;",null);
Assert.True(succeedAddConnectionString);
var connectionString = _shardingConnectionStringResolver.GetConnectionString("X", null);
Assert.Equal("Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;", connectionString);
} }
public class SequenceClass public class SequenceClass
@ -235,9 +229,13 @@ namespace ShardingCore.Test2x
var multiOrder = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 232398109278351360).FirstOrDefaultAsync(); var multiOrder = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 232398109278351360).FirstOrDefaultAsync();
Assert.NotNull(multiOrder); Assert.NotNull(multiOrder);
var allMultiOrders = await _virtualDbContext.Set<MultiShardingOrder>().ToListAsync();
Assert.Equal(8, allMultiOrders.Count);
var longs = new[] { 232398109278351360, 255197859283087360 }; var longs = new[] { 232398109278351360, 255197859283087360 };
var multiOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => longs.Contains(o.Id)).ToListAsync(); var multiOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => longs.Contains(o.Id)).ToListAsync();
Assert.Equal(2, multiOrders.Count); Assert.Equal(2, multiOrders.Count);
var multinNotOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => !longs.Contains(o.Id)).ToListAsync();
Assert.Equal(6, multinNotOrders.Count);
var dateTime = new DateTime(2021, 11, 1); var dateTime = new DateTime(2021, 11, 1);
var multiOrder404 = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 250345338962063360 && o.CreateTime < dateTime).FirstOrDefaultAsync(); var multiOrder404 = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 250345338962063360 && o.CreateTime < dateTime).FirstOrDefaultAsync();
Assert.Null(multiOrder404); Assert.Null(multiOrder404);
@ -272,7 +270,7 @@ namespace ShardingCore.Test2x
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
[Fact] [Fact]
@ -368,6 +366,17 @@ namespace ShardingCore.Test2x
[Fact] [Fact]
public async Task ToList_Join_Test() public async Task ToList_Join_Test()
{ {
var list111 = await (from u in _virtualDbContext.Set<SysUserMod>()
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
u.Id,
u.Age,
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
}).CountAsync();
var list = await (from u in _virtualDbContext.Set<SysUserMod>() var list = await (from u in _virtualDbContext.Set<SysUserMod>()
join salary in _virtualDbContext.Set<SysUserSalary>() join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId on u.Id equals salary.UserId
@ -410,7 +419,7 @@ namespace ShardingCore.Test2x
var list3 = await queryable1.ToListAsync(); var list3 = await queryable1.ToListAsync();
Assert.Equal(1, list3.Count()); Assert.Equal(1, list3.Count());
Assert.Contains(list3, o => o.Name == "name_300"); Assert.Contains(list3, o => o.Name == "name_300");
var firstOrDefaultAsync = await queryable1.OrderBy(o=>o.DateOfMonth).FirstOrDefaultAsync(); var firstOrDefaultAsync = await queryable1.OrderBy(o => o.DateOfMonth).FirstOrDefaultAsync();
Assert.NotNull(firstOrDefaultAsync); Assert.NotNull(firstOrDefaultAsync);
var firstOrDefault = queryable1.OrderBy(o => o.DateOfMonth).FirstOrDefault(); var firstOrDefault = queryable1.OrderBy(o => o.DateOfMonth).FirstOrDefault();
Assert.NotNull(firstOrDefault); Assert.NotNull(firstOrDefault);
@ -466,10 +475,25 @@ namespace ShardingCore.Test2x
[Fact] [Fact]
public async Task ToList_Id_Not_Eq_Test() public async Task ToList_Id_Not_Eq_Test()
{ {
var methodValue = new MethodValue() { AA = "7" };
var mods123 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == methodValue.GetAa()).FirstOrDefaultAsync();
Assert.NotNull(mods123);
Assert.Equal(mods123.Id, "7");
var mods12 = await _virtualDbContext.Set<SysUserMod>().Where(o => new List<string> { "3", "4" }.Contains(o.Id)).ToListAsync();
Assert.Contains(mods12, o => o.Id == "3" || o.Id == "4");
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync(); var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync();
Assert.Equal(999, mods.Count); Assert.Equal(999, mods.Count);
Assert.DoesNotContain(mods, o => o.Id == "3"); Assert.DoesNotContain(mods, o => o.Id == "3");
} }
public class MethodValue
{
public string AA { get; set; }
public string GetAa()
{
return AA;
}
}
[Fact] [Fact]
public async Task ToList_Id_Not_Eq_Skip_Test() public async Task ToList_Id_Not_Eq_Skip_Test()
@ -498,6 +522,10 @@ namespace ShardingCore.Test2x
[Fact] [Fact]
public async Task ToList_Id_Eq_Not_In_Db_Test() public async Task ToList_Id_Eq_Not_In_Db_Test()
{ {
var mod1s = await _virtualDbContext.Set<SysUserMod>().Where(o =>string.Compare(((IId)o).Id,"1001") ==0).ToListAsync();
var mod2s = await _virtualDbContext.Set<SysUserMod>().Where(o => string.Compare("1001", o.Id) == 0).ToListAsync();
var mod3s = await _virtualDbContext.Set<SysUserMod>().Where(o => "1001".CompareTo(o.Id)==0).ToListAsync();
var mod4s = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.CompareTo("1001") == 0).ToListAsync();
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync(); var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync();
Assert.Empty(mods); Assert.Empty(mods);
} }
@ -524,9 +552,23 @@ namespace ShardingCore.Test2x
} }
[Fact] [Fact]
public async Task FirstOrDefault2() public async Task FirstOrDefault2()
{ {
var sysUserModabxxxxx1 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx71 = await _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102).FirstOrDefaultAsync();
var xxxx1 = 202102;
var sysUserModabxxxxx72 = await _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1).FirstOrDefaultAsync();
var sysUserModabxxxxx2 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101).FirstOrDefaultAsync();
var sysUserModabxxxxx3 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx4 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx5 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth < 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx6 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth <= 202102).FirstOrDefaultAsync();
var next = "1";
var sysUserMod1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == next).FirstOrDefaultAsync();
var sysUserModabxxx = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
var sysUserModabxxx11 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2" || o.Name == "name_3").FirstOrDefaultAsync();
var x = new Object[] { "1", "2" }; var x = new Object[] { "1", "2" };
var sysUserModab = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefaultAsync(); var sysUserModab = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefaultAsync();
Assert.NotNull(sysUserModab); Assert.NotNull(sysUserModab);
@ -557,8 +599,26 @@ namespace ShardingCore.Test2x
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync(); var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
Assert.Equal("2", sysUserMod.Id); Assert.Equal("2", sysUserMod.Id);
var sysUserMod2 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").Select(o => new
{
Name=o.Name
}).FirstOrDefaultAsync();
var sysUserMod1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").Select(o => new TestClass(o.Name,""){Id = o.Id}).FirstOrDefaultAsync();
Assert.NotNull(sysUserMod1);
} }
public class TestClass
{
public string Name { get; }
public string Aa { get; }
public string Id { get; set; }
public TestClass(string name,string aa)
{
Name = name;
Aa = aa;
}
}
[Fact] [Fact]
public async Task FirstOrDefault4() public async Task FirstOrDefault4()
@ -575,6 +635,7 @@ namespace ShardingCore.Test2x
Assert.Null(sysUserMod); Assert.Null(sysUserMod);
} }
[Fact] [Fact]
public async Task Count_Test() public async Task Count_Test()
{ {
@ -685,6 +746,90 @@ namespace ShardingCore.Test2x
Assert.Equal(1140000, group[0].MaxSalary); Assert.Equal(1140000, group[0].MaxSalary);
} }
[Fact]
public async Task Group_Test1()
{
var ids = new[] { "200", "300" };
var dateOfMonths = new[] { 202111, 202110 };
var dateTime = DateTime.Now;
var sql = from u in _virtualDbContext.Set<SysUserSalary>()
group u by u.UserId
into g
select new
{
UI = g.Key,
x=g.Sum(o=>o.SalaryDecimal),
Now = dateTime
};
var listAsync = await sql.ToListAsync();
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
group u by new
{
UId = u.UserId
}
into g
select new
{
GroupUserId = g.Key.UId,
Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary)
}).ToListAsync();
Assert.Equal(2, group.Count);
Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
//[Fact]
//public async Task Group_Recently_Test()
//{
// //var list =(from us in _virtualDbContext.Set<SysUserSalary>().Where(o => ids.Contains(o.UserId))
// // group us by new
// // {
// // UserId=us.UserId
// // }
// // into g
// // select new
// // {
// // UserId=g.Key.UserId,
// // DateOfMonth = g.Max(o=>o.DateOfMonth)
// // }).ToList();
// //var y = list;
// var ids = new List<string>(){ "200", "300" };
// List<SysUserSalary> result = new List<SysUserSalary>(ids.Count);
// var routeFilter = new List<SysUserSalary>().AsQueryable().Where(o => ids.Contains(o.UserId));
// //<2F><>ȡ<EFBFBD><C8A1>·<EFBFBD><C2B7>ʱ<EFBFBD><EFBFBD><E4B5B9>
// var tableRouteResults = _tableRouteRuleEngineFactory.Route(routeFilter)
// .Select(o => o.ReplaceTables.First().Tail).OrderByDescending(o => o).ToList();
// foreach (var tableRouteResult in tableRouteResults)
// {
// if(ids.IsEmpty())
// break;
// using (_shardingRouteManager.CreateScope())
// {
// _shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserSalary>(tableRouteResult);
// var queryable = _virtualDbContext.Set<SysUserSalary>().Where(o => ids.Contains(o.UserId))
// .GroupBy(o => new { o.UserId }, i => i,
// (i, u) => new {
// Data = u.OrderByDescending(o => o.DateOfMonth).FirstOrDefault()
// });
// var r =await queryable.ToListAsync();
// result.AddRange(r.Select(o=>o.Data));
// var removeUserIds = result.Select(u => u.UserId).ToHashSet();
// ids.RemoveAll(o => removeUserIds.Contains(o));
// }
// }
//}
[Fact] [Fact]
public async Task OrderCountTest() public async Task OrderCountTest()
{ {
@ -776,17 +921,11 @@ namespace ShardingCore.Test2x
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var tableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName =tableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName);
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator)); Assert.True(string.IsNullOrWhiteSpace(tableRoute.EntityMetadata.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -820,7 +959,7 @@ namespace ShardingCore.Test2x
var fourBegin = new DateTime(2021, 4, 1).Date; var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date; var fiveBegin = new DateTime(2021, 5, 1).Date;
var moneyAverage = await _virtualDbContext.Set<Order>() var moneyAverage = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync(); .Where(o => o.CreateTime >= fourBegin&& o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync();
Assert.Equal(105, moneyAverage); Assert.Equal(105, moneyAverage);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
@ -1022,9 +1161,9 @@ namespace ShardingCore.Test2x
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -1032,9 +1171,9 @@ namespace ShardingCore.Test2x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
@ -1224,6 +1363,7 @@ namespace ShardingCore.Test2x
Assert.Equal(300, countAsync1); Assert.Equal(300, countAsync1);
} }
Assert.Null(_shardingRouteManager.Current); Assert.Null(_shardingRouteManager.Current);
} }
[Fact] [Fact]
public async Task LogYearLongShardingPage() public async Task LogYearLongShardingPage()
@ -1310,13 +1450,13 @@ namespace ShardingCore.Test2x
logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd"); logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Update(logNoSharding); _virtualDbContext.Update(logNoSharding);
logNoShardings.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd")); logNoShardings.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.UpdateRange(logNoShardings); _virtualDbContext.UpdateRange(logNoShardings);
logNoSharding1.Body = DateTime.Now.ToString("yyyyMMdd"); logNoSharding1.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Set<LogNoSharding>().Update(logNoSharding1); _virtualDbContext.Set<LogNoSharding>().Update(logNoSharding1);
logNoSharding1s.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd")); logNoSharding1s.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.Set<LogNoSharding>().UpdateRange(logNoSharding1s); _virtualDbContext.Set<LogNoSharding>().UpdateRange(logNoSharding1s);
await _virtualDbContext.SaveChangesAsync(); await _virtualDbContext.SaveChangesAsync();
@ -1363,7 +1503,7 @@ namespace ShardingCore.Test2x
{ {
await _virtualDbContext.AddAsync((object)logNoSharding); await _virtualDbContext.AddAsync((object)logNoSharding);
await _virtualDbContext.AddRangeAsync(logNoShardings.Select(o=>(object)o).ToArray()); await _virtualDbContext.AddRangeAsync(logNoShardings.Select(o => (object)o).ToArray());
await _virtualDbContext.SaveChangesAsync(); await _virtualDbContext.SaveChangesAsync();
tran.Commit(); tran.Commit();
@ -1496,6 +1636,7 @@ namespace ShardingCore.Test2x
Assert.Equal(10, page1.Data.Count); Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total); Assert.Equal(300, page1.Total);
} }
[Fact] [Fact]
public async Task AsRouteTest() public async Task AsRouteTest()
{ {
@ -1531,12 +1672,11 @@ namespace ShardingCore.Test2x
.Where(o => o.CreateTime == fiveBegin).Select(o => o.Money).SumAsync(); .Where(o => o.CreateTime == fiveBegin).Select(o => o.Money).SumAsync();
Assert.Equal(0, sum); Assert.Equal(0, sum);
} }
[Fact] [Fact]
public async Task QueryInner_Test() public async Task QueryInner_Test()
{ {
var sysUserMods = _virtualDbContext.Set<SysUserMod>().Select(o => o); var sysUserMods = _virtualDbContext.Set<SysUserMod>().Select(o=>o);
var sysUserModInts = await _virtualDbContext.Set<SysUserModInt>().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToListAsync(); var sysUserModInts = await _virtualDbContext.Set<SysUserModInt>().Where(o=>sysUserMods.Select(i=>i.Age).Any(i=>i==o.Age)).ToListAsync();
Assert.Equal(1000, sysUserModInts.Count); Assert.Equal(1000, sysUserModInts.Count);
} }
// [Fact] // [Fact]

View File

@ -1,30 +1,29 @@
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions; using ShardingCore.Extensions.ShardingPageExtensions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.Helpers; using ShardingCore.Helpers;
using ShardingCore.Sharding; using ShardingCore.Sharding;
using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Test2x.Domain.Entities; using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.Utils;
using Xunit; using Xunit;
namespace ShardingCore.Test2x namespace ShardingCore.Test2x
@ -35,38 +34,34 @@ namespace ShardingCore.Test2x
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
@ -116,8 +111,10 @@ namespace ShardingCore.Test2x
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); //, "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
var y = new EntityMetadata(typeof(LogDay));
//, "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
@ -167,6 +164,7 @@ namespace ShardingCore.Test2x
Assert.False(isKey3); Assert.False(isKey3);
var isKey4 = userModMetadata.ShardingTableFieldIsKey(); var isKey4 = userModMetadata.ShardingTableFieldIsKey();
Assert.True(isKey4); Assert.True(isKey4);
_virtualDbContext.AddRange(logDays); _virtualDbContext.AddRange(logDays);
var bulkShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, Order>(o => new[] { "A", "B" }.Contains(o.Area)); var bulkShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, Order>(o => new[] { "A", "B" }.Contains(o.Area));
Assert.Equal(2, bulkShardingExpression.Count); Assert.Equal(2, bulkShardingExpression.Count);
@ -184,8 +182,6 @@ namespace ShardingCore.Test2x
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>() var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>()
{ new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) }); { new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) });
@ -201,6 +197,7 @@ namespace ShardingCore.Test2x
public string Id { get; set; } public string Id { get; set; }
public string T { get; set; } public string T { get; set; }
} }
[Fact] [Fact]
public void TestMultiShardingProperty() public void TestMultiShardingProperty()
{ {
@ -244,7 +241,7 @@ namespace ShardingCore.Test2x
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
//[Fact] //[Fact]
@ -497,9 +494,9 @@ namespace ShardingCore.Test2x
var sysUserMod = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefault(); var sysUserMod = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefault();
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
Assert.True(sysUserMod.Id == "1"); Assert.True(sysUserMod.Id == "1");
Assert.Equal(sysUserModaa, sysUserMod);
var sysUserModxx = _virtualDbContext.Set<SysUserMod>().Where(o => x.Contains(o.Id)).FirstOrDefault(); var sysUserModxx = _virtualDbContext.Set<SysUserMod>().Where(o => x.Contains(o.Id)).FirstOrDefault();
Assert.NotNull(sysUserModxx); Assert.NotNull(sysUserModxx);
Assert.Equal(sysUserModaa, sysUserMod);
Assert.True(x.Contains(sysUserModxx.Id)); Assert.True(x.Contains(sysUserModxx.Id));
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1"); var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
@ -509,6 +506,7 @@ namespace ShardingCore.Test2x
Assert.True(user198.Id == "198"); Assert.True(user198.Id == "198");
var userId198 = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefault(); var userId198 = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefault();
Assert.Equal(userId198, "198"); Assert.Equal(userId198, "198");
} }
[Fact] [Fact]
@ -722,17 +720,10 @@ namespace ShardingCore.Test2x
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var virtualTableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName = virtualTableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName); Assert.True(string.IsNullOrWhiteSpace(virtualTableRoute.EntityMetadata.TableSeparator));
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -865,7 +856,7 @@ namespace ShardingCore.Test2x
catch (Exception e) catch (Exception e)
{ {
Assert.True(typeof(InvalidOperationException) == e.GetType() || typeof(TargetInvocationException) == e.GetType()); Assert.True(typeof(InvalidOperationException) == e.GetType() || typeof(TargetInvocationException) == e.GetType());
Assert.True(e.Message.Contains("contains") || e.InnerException.Message.Contains("contains")); Assert.True(e.Message.Contains("contains")|| e.InnerException.Message.Contains("contains"));
} }
} }
} }
@ -961,9 +952,9 @@ namespace ShardingCore.Test2x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -971,9 +962,9 @@ namespace ShardingCore.Test2x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {

View File

@ -26,7 +26,8 @@ namespace ShardingCore.Test2x.Shardings
{ {
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails()
protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;
@ -45,5 +46,6 @@ namespace ShardingCore.Test2x.Shardings
} }
return tails; return tails;
} }
} }
} }

View File

@ -34,7 +34,7 @@ namespace ShardingCore.Test2x.Shardings
{ {
return true; return true;
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;

View File

@ -39,7 +39,7 @@ namespace ShardingCore.Test2x.Shardings
public override void Configure(EntityMetadataDataSourceBuilder<Order> builder) public override void Configure(EntityMetadataDataSourceBuilder<Order> builder)
{ {
builder.ShardingProperty(o => o.Area);
} }
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)

View File

@ -15,13 +15,12 @@ namespace ShardingCore.Test2x.Shardings
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var allTails = base.GetAllTails(); var allTails = base.CalcTailsOnStart();
allTails.Add("202112"); allTails.Add("202112");
return allTails; return allTails;
} }
public override void Configure(EntityMetadataTableBuilder<Order> builder) public override void Configure(EntityMetadataTableBuilder<Order> builder)
{ {

View File

@ -24,7 +24,7 @@ namespace ShardingCore.Test2x.Shardings
} }
public override List<string> GetAllTails() public override List<string> GetTails()
{ {
var beginTime = new DateTime(2020, 1, 1); var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1); var endTime = new DateTime(2021, 12, 1);

View File

@ -10,6 +10,7 @@ using ShardingCore.Bootstrappers;
using ShardingCore.Helpers; using ShardingCore.Helpers;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.TableExists; using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
using ShardingCore.Test2x.Domain.Entities; using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.Test2x.Shardings; using ShardingCore.Test2x.Shardings;
@ -35,12 +36,8 @@ namespace ShardingCore.Test2x
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
{ {
services.AddShardingDbContext<ShardingDefaultDbContext>() services.AddShardingDbContext<ShardingDefaultDbContext>()
.AddEntityConfig(op => .UseRouteConfig(op =>
{ {
//如果您使用code-first建议选择false
op.CreateShardingTableOnStart = true;
//如果您使用code-first建议修改为fsle
op.EnsureCreatedWithOutShardingTable = true;
//当无法获取路由时会返回默认值而不是报错 //当无法获取路由时会返回默认值而不是报错
op.ThrowIfQueryRouteNotMatch = false; op.ThrowIfQueryRouteNotMatch = false;
op.AddShardingDataSourceRoute<OrderAreaShardingVirtualDataSourceRoute>(); op.AddShardingDataSourceRoute<OrderAreaShardingVirtualDataSourceRoute>();
@ -58,9 +55,8 @@ namespace ShardingCore.Test2x
op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>(); op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>();
}) })
.AddConfig(op => .UseConfig(op =>
{ {
op.ConfigId = "c1";
op.UseShardingQuery((conStr, builder) => op.UseShardingQuery((conStr, builder) =>
{ {
@ -93,8 +89,9 @@ namespace ShardingCore.Test2x
} }
}; };
}, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); }, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime);
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<ShardingDefaultDbContext>()); })
}).EnsureConfig(); .ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>(ServiceLifetime.Singleton)
.AddShardingCore();
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15")) // services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// ,op => // ,op =>
// { // {
@ -110,8 +107,8 @@ namespace ShardingCore.Test2x
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
public void Configure(IServiceProvider serviceProvider) public void Configure(IServiceProvider serviceProvider)
{ {
var shardingBootstrapper = serviceProvider.GetService<IShardingBootstrapper>(); var shardingBootstrapper = serviceProvider.GetRequiredService<IShardingBootstrapper>();
shardingBootstrapper.Start(); shardingBootstrapper.AutoShardingCreate();
// 有一些测试数据要初始化可以放在这里 // 有一些测试数据要初始化可以放在这里
InitData(serviceProvider).GetAwaiter().GetResult(); InitData(serviceProvider).GetAwaiter().GetResult();
} }

View File

@ -6,10 +6,8 @@ namespace ShardingCore.Test3x.Domain.Entities
public class Order public class Order
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
[ShardingDataSourceKey]
public string Area { get; set; } public string Area { get; set; }
public long Money { get; set; } public long Money { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
} }

View File

@ -6,7 +6,7 @@ namespace ShardingCore.Test3x.Domain.Entities
* @Date: Thursday, 14 January 2021 15:36:43 * @Date: Thursday, 14 January 2021 15:36:43
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public class SysUserMod public class SysUserMod: IId
{ {
/// <summary> /// <summary>
/// 用户Id用于分表 /// 用户Id用于分表
@ -23,4 +23,10 @@ namespace ShardingCore.Test3x.Domain.Entities
public int AgeGroup { get; set; } public int AgeGroup { get; set; }
} }
public interface IId
{
string Id { get; set; }
}
} }

View File

@ -7,11 +7,10 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
@ -25,6 +24,7 @@ using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions; using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Sharding.ShardingDbContextExecutors; using ShardingCore.Sharding.ShardingDbContextExecutors;
using ShardingCore.Sharding.StreamMergeEngines;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
using ShardingCore.Test3x.Domain.Entities; using ShardingCore.Test3x.Domain.Entities;
using ShardingCore.Utils; using ShardingCore.Utils;
@ -32,48 +32,39 @@ using Xunit;
namespace ShardingCore.Test3x namespace ShardingCore.Test3x
{ {
/*
* @Author: xjm
* @Description:
* @Date: Friday, 15 January 2021 17:22:10
* @Email: 326308290@qq.com
*/
public class ShardingTest public class ShardingTest
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
@ -81,32 +72,32 @@ namespace ShardingCore.Test3x
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer(); // var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>(); // var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute(); // var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
// // var xxxx = "202102";
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102); // var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata, // var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102); // var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata, // var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var xxxx1 = 202102; // var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1); // var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata, // var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101); // var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata, // var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101); // var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata, // var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101); // var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata, // var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth); // var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata, // var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth); // var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata, // var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2)); // Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3)); // Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4)); // Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
@ -123,12 +114,14 @@ namespace ShardingCore.Test3x
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var y = new EntityMetadata(typeof(LogDay));
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
var bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays); var bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays);
Assert.Equal(100, bulkShardingTableEnumerable.Count); Assert.Equal(100, bulkShardingTableEnumerable.Count);
var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays); var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays);
Assert.Equal(1, bulkShardingEnumerable.Count); Assert.Equal(1, bulkShardingEnumerable.Count);
@ -182,12 +175,12 @@ namespace ShardingCore.Test3x
Assert.True(bulkShardingExpression.ContainsKey("B")); Assert.True(bulkShardingExpression.ContainsKey("B"));
var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString()); var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString());
Assert.Equal(1, bulkShardingTableExpression.Count()); Assert.Equal(1, bulkShardingTableExpression.Count());
var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123"); var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123");
Assert.Equal(1, noShardingExpression.Count()); Assert.Equal(1, noShardingExpression.Count());
var isShardingDbContext = _virtualDbContext.IsShardingDbContext(); var isShardingDbContext = _virtualDbContext.IsShardingDbContext();
Assert.True(isShardingDbContext); Assert.True(isShardingDbContext);
var isShardingTableDbContext = _virtualDbContext.IsShardingTableDbContext(); var isShardingTableDbContext = _virtualDbContext.IsShardingTableDbContext();
@ -196,17 +189,15 @@ namespace ShardingCore.Test3x
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity(); var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity();
var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity(); var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity();
var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity(); var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity();
var dics=new SortedDictionary<string, string>(new NoShardingFirstComparer()); var dics = new SortedDictionary<string, string>(new NoShardingFirstComparer());
var dicTails=new List<string>() { emptyTailIdentity, aTailIdentity, bTailIdentity }; var dicTails = new List<string>() { emptyTailIdentity, aTailIdentity, bTailIdentity };
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
dics.Clear(); dics.Clear();
var reOrderList=dicTails.OrderBy(o => Guid.NewGuid()).ToList(); var reOrderList = dicTails.OrderBy(o => Guid.NewGuid()).ToList();
foreach (var tail in reOrderList) foreach (var tail in reOrderList)
{ {
dics.Add(tail, null); dics.Add(tail, null);
@ -220,6 +211,10 @@ namespace ShardingCore.Test3x
{ new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), }); { new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), });
Assert.Equal(x1x1, x2x2); Assert.Equal(x1x1, x2x2);
Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode()); Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode());
var succeedAddConnectionString = _shardingConnectionStringResolver.AddConnectionString("X", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;",null);
Assert.True(succeedAddConnectionString);
var connectionString = _shardingConnectionStringResolver.GetConnectionString("X", null);
Assert.Equal("Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;", connectionString);
} }
public class SequenceClass public class SequenceClass
@ -234,9 +229,13 @@ namespace ShardingCore.Test3x
var multiOrder = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 232398109278351360).FirstOrDefaultAsync(); var multiOrder = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 232398109278351360).FirstOrDefaultAsync();
Assert.NotNull(multiOrder); Assert.NotNull(multiOrder);
var allMultiOrders = await _virtualDbContext.Set<MultiShardingOrder>().ToListAsync();
Assert.Equal(8, allMultiOrders.Count);
var longs = new[] { 232398109278351360, 255197859283087360 }; var longs = new[] { 232398109278351360, 255197859283087360 };
var multiOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => longs.Contains(o.Id)).ToListAsync(); var multiOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => longs.Contains(o.Id)).ToListAsync();
Assert.Equal(2, multiOrders.Count); Assert.Equal(2, multiOrders.Count);
var multinNotOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => !longs.Contains(o.Id)).ToListAsync();
Assert.Equal(6, multinNotOrders.Count);
var dateTime = new DateTime(2021, 11, 1); var dateTime = new DateTime(2021, 11, 1);
var multiOrder404 = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 250345338962063360 && o.CreateTime < dateTime).FirstOrDefaultAsync(); var multiOrder404 = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 250345338962063360 && o.CreateTime < dateTime).FirstOrDefaultAsync();
Assert.Null(multiOrder404); Assert.Null(multiOrder404);
@ -271,7 +270,7 @@ namespace ShardingCore.Test3x
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
[Fact] [Fact]
@ -367,6 +366,17 @@ namespace ShardingCore.Test3x
[Fact] [Fact]
public async Task ToList_Join_Test() public async Task ToList_Join_Test()
{ {
var list111 = await (from u in _virtualDbContext.Set<SysUserMod>()
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
u.Id,
u.Age,
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
}).CountAsync();
var list = await (from u in _virtualDbContext.Set<SysUserMod>() var list = await (from u in _virtualDbContext.Set<SysUserMod>()
join salary in _virtualDbContext.Set<SysUserSalary>() join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId on u.Id equals salary.UserId
@ -465,10 +475,25 @@ namespace ShardingCore.Test3x
[Fact] [Fact]
public async Task ToList_Id_Not_Eq_Test() public async Task ToList_Id_Not_Eq_Test()
{ {
var methodValue = new MethodValue() { AA = "7" };
var mods123 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == methodValue.GetAa()).FirstOrDefaultAsync();
Assert.NotNull(mods123);
Assert.Equal(mods123.Id, "7");
var mods12 = await _virtualDbContext.Set<SysUserMod>().Where(o => new List<string> { "3", "4" }.Contains(o.Id)).ToListAsync();
Assert.Contains(mods12, o => o.Id == "3" || o.Id == "4");
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync(); var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync();
Assert.Equal(999, mods.Count); Assert.Equal(999, mods.Count);
Assert.DoesNotContain(mods, o => o.Id == "3"); Assert.DoesNotContain(mods, o => o.Id == "3");
} }
public class MethodValue
{
public string AA { get; set; }
public string GetAa()
{
return AA;
}
}
[Fact] [Fact]
public async Task ToList_Id_Not_Eq_Skip_Test() public async Task ToList_Id_Not_Eq_Skip_Test()
@ -497,6 +522,10 @@ namespace ShardingCore.Test3x
[Fact] [Fact]
public async Task ToList_Id_Eq_Not_In_Db_Test() public async Task ToList_Id_Eq_Not_In_Db_Test()
{ {
var mod1s = await _virtualDbContext.Set<SysUserMod>().Where(o =>string.Compare(((IId)o).Id,"1001") ==0).ToListAsync();
var mod2s = await _virtualDbContext.Set<SysUserMod>().Where(o => string.Compare("1001", o.Id) == 0).ToListAsync();
var mod3s = await _virtualDbContext.Set<SysUserMod>().Where(o => "1001".CompareTo(o.Id)==0).ToListAsync();
var mod4s = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.CompareTo("1001") == 0).ToListAsync();
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync(); var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync();
Assert.Empty(mods); Assert.Empty(mods);
} }
@ -523,9 +552,23 @@ namespace ShardingCore.Test3x
} }
[Fact] [Fact]
public async Task FirstOrDefault2() public async Task FirstOrDefault2()
{ {
var sysUserModabxxxxx1 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx71 = await _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102).FirstOrDefaultAsync();
var xxxx1 = 202102;
var sysUserModabxxxxx72 = await _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1).FirstOrDefaultAsync();
var sysUserModabxxxxx2 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101).FirstOrDefaultAsync();
var sysUserModabxxxxx3 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx4 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx5 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth < 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx6 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth <= 202102).FirstOrDefaultAsync();
var next = "1";
var sysUserMod1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == next).FirstOrDefaultAsync();
var sysUserModabxxx = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
var sysUserModabxxx11 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2" || o.Name == "name_3").FirstOrDefaultAsync();
var x = new Object[] { "1", "2" }; var x = new Object[] { "1", "2" };
var sysUserModab = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefaultAsync(); var sysUserModab = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefaultAsync();
Assert.NotNull(sysUserModab); Assert.NotNull(sysUserModab);
@ -556,8 +599,26 @@ namespace ShardingCore.Test3x
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync(); var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
Assert.Equal("2", sysUserMod.Id); Assert.Equal("2", sysUserMod.Id);
var sysUserMod2 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").Select(o => new
{
Name=o.Name
}).FirstOrDefaultAsync();
var sysUserMod1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").Select(o => new TestClass(o.Name,""){Id = o.Id}).FirstOrDefaultAsync();
Assert.NotNull(sysUserMod1);
} }
public class TestClass
{
public string Name { get; }
public string Aa { get; }
public string Id { get; set; }
public TestClass(string name,string aa)
{
Name = name;
Aa = aa;
}
}
[Fact] [Fact]
public async Task FirstOrDefault4() public async Task FirstOrDefault4()
@ -574,6 +635,7 @@ namespace ShardingCore.Test3x
Assert.Null(sysUserMod); Assert.Null(sysUserMod);
} }
[Fact] [Fact]
public async Task Count_Test() public async Task Count_Test()
{ {
@ -684,6 +746,90 @@ namespace ShardingCore.Test3x
Assert.Equal(1140000, group[0].MaxSalary); Assert.Equal(1140000, group[0].MaxSalary);
} }
[Fact]
public async Task Group_Test1()
{
var ids = new[] { "200", "300" };
var dateOfMonths = new[] { 202111, 202110 };
var dateTime = DateTime.Now;
var sql = from u in _virtualDbContext.Set<SysUserSalary>()
group u by u.UserId
into g
select new
{
UI = g.Key,
x=g.Sum(o=>o.SalaryDecimal),
Now = dateTime
};
var listAsync = await sql.ToListAsync();
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
group u by new
{
UId = u.UserId
}
into g
select new
{
GroupUserId = g.Key.UId,
Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary)
}).ToListAsync();
Assert.Equal(2, group.Count);
Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
//[Fact]
//public async Task Group_Recently_Test()
//{
// //var list =(from us in _virtualDbContext.Set<SysUserSalary>().Where(o => ids.Contains(o.UserId))
// // group us by new
// // {
// // UserId=us.UserId
// // }
// // into g
// // select new
// // {
// // UserId=g.Key.UserId,
// // DateOfMonth = g.Max(o=>o.DateOfMonth)
// // }).ToList();
// //var y = list;
// var ids = new List<string>(){ "200", "300" };
// List<SysUserSalary> result = new List<SysUserSalary>(ids.Count);
// var routeFilter = new List<SysUserSalary>().AsQueryable().Where(o => ids.Contains(o.UserId));
// //<2F><>ȡ<EFBFBD><C8A1>·<EFBFBD><C2B7>ʱ<EFBFBD><EFBFBD><E4B5B9>
// var tableRouteResults = _tableRouteRuleEngineFactory.Route(routeFilter)
// .Select(o => o.ReplaceTables.First().Tail).OrderByDescending(o => o).ToList();
// foreach (var tableRouteResult in tableRouteResults)
// {
// if(ids.IsEmpty())
// break;
// using (_shardingRouteManager.CreateScope())
// {
// _shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserSalary>(tableRouteResult);
// var queryable = _virtualDbContext.Set<SysUserSalary>().Where(o => ids.Contains(o.UserId))
// .GroupBy(o => new { o.UserId }, i => i,
// (i, u) => new {
// Data = u.OrderByDescending(o => o.DateOfMonth).FirstOrDefault()
// });
// var r =await queryable.ToListAsync();
// result.AddRange(r.Select(o=>o.Data));
// var removeUserIds = result.Select(u => u.UserId).ToHashSet();
// ids.RemoveAll(o => removeUserIds.Contains(o));
// }
// }
//}
[Fact] [Fact]
public async Task OrderCountTest() public async Task OrderCountTest()
{ {
@ -775,17 +921,11 @@ namespace ShardingCore.Test3x
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var tableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName =tableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName);
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator)); Assert.True(string.IsNullOrWhiteSpace(tableRoute.EntityMetadata.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -819,7 +959,7 @@ namespace ShardingCore.Test3x
var fourBegin = new DateTime(2021, 4, 1).Date; var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date; var fiveBegin = new DateTime(2021, 5, 1).Date;
var moneyAverage = await _virtualDbContext.Set<Order>() var moneyAverage = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync(); .Where(o => o.CreateTime >= fourBegin&& o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync();
Assert.Equal(105, moneyAverage); Assert.Equal(105, moneyAverage);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
@ -1021,9 +1161,9 @@ namespace ShardingCore.Test3x
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -1031,9 +1171,9 @@ namespace ShardingCore.Test3x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
@ -1223,6 +1363,7 @@ namespace ShardingCore.Test3x
Assert.Equal(300, countAsync1); Assert.Equal(300, countAsync1);
} }
Assert.Null(_shardingRouteManager.Current); Assert.Null(_shardingRouteManager.Current);
} }
[Fact] [Fact]
public async Task LogYearLongShardingPage() public async Task LogYearLongShardingPage()
@ -1309,13 +1450,13 @@ namespace ShardingCore.Test3x
logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd"); logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Update(logNoSharding); _virtualDbContext.Update(logNoSharding);
logNoShardings.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd")); logNoShardings.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.UpdateRange(logNoShardings); _virtualDbContext.UpdateRange(logNoShardings);
logNoSharding1.Body = DateTime.Now.ToString("yyyyMMdd"); logNoSharding1.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Set<LogNoSharding>().Update(logNoSharding1); _virtualDbContext.Set<LogNoSharding>().Update(logNoSharding1);
logNoSharding1s.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd")); logNoSharding1s.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.Set<LogNoSharding>().UpdateRange(logNoSharding1s); _virtualDbContext.Set<LogNoSharding>().UpdateRange(logNoSharding1s);
await _virtualDbContext.SaveChangesAsync(); await _virtualDbContext.SaveChangesAsync();
@ -1362,7 +1503,7 @@ namespace ShardingCore.Test3x
{ {
await _virtualDbContext.AddAsync((object)logNoSharding); await _virtualDbContext.AddAsync((object)logNoSharding);
await _virtualDbContext.AddRangeAsync(logNoShardings.Select(o=>(object)o).ToArray()); await _virtualDbContext.AddRangeAsync(logNoShardings.Select(o => (object)o).ToArray());
await _virtualDbContext.SaveChangesAsync(); await _virtualDbContext.SaveChangesAsync();
await tran.CommitAsync(); await tran.CommitAsync();
@ -1495,6 +1636,7 @@ namespace ShardingCore.Test3x
Assert.Equal(10, page1.Data.Count); Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total); Assert.Equal(300, page1.Total);
} }
[Fact] [Fact]
public async Task AsRouteTest() public async Task AsRouteTest()
{ {
@ -1533,8 +1675,8 @@ namespace ShardingCore.Test3x
[Fact] [Fact]
public async Task QueryInner_Test() public async Task QueryInner_Test()
{ {
var sysUserMods = _virtualDbContext.Set<SysUserMod>().Select(o => o); var sysUserMods = _virtualDbContext.Set<SysUserMod>().Select(o=>o);
var sysUserModInts = await _virtualDbContext.Set<SysUserModInt>().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToListAsync(); var sysUserModInts = await _virtualDbContext.Set<SysUserModInt>().Where(o=>sysUserMods.Select(i=>i.Age).Any(i=>i==o.Age)).ToListAsync();
Assert.Equal(1000, sysUserModInts.Count); Assert.Equal(1000, sysUserModInts.Count);
} }
// [Fact] // [Fact]

View File

@ -1,31 +1,29 @@
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions; using ShardingCore.Extensions.ShardingPageExtensions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.Helpers; using ShardingCore.Helpers;
using ShardingCore.Sharding; using ShardingCore.Sharding;
using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
using ShardingCore.Test3x.Domain.Entities; using ShardingCore.Test3x.Domain.Entities;
using ShardingCore.Utils; using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using Xunit; using Xunit;
namespace ShardingCore.Test3x namespace ShardingCore.Test3x
@ -36,38 +34,34 @@ namespace ShardingCore.Test3x
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
@ -117,8 +111,10 @@ namespace ShardingCore.Test3x
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); //, "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
var y = new EntityMetadata(typeof(LogDay));
//, "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
@ -186,8 +182,6 @@ namespace ShardingCore.Test3x
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>() var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>()
{ new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) }); { new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) });
@ -203,6 +197,7 @@ namespace ShardingCore.Test3x
public string Id { get; set; } public string Id { get; set; }
public string T { get; set; } public string T { get; set; }
} }
[Fact] [Fact]
public void TestMultiShardingProperty() public void TestMultiShardingProperty()
{ {
@ -246,7 +241,7 @@ namespace ShardingCore.Test3x
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
//[Fact] //[Fact]
@ -499,9 +494,9 @@ namespace ShardingCore.Test3x
var sysUserMod = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefault(); var sysUserMod = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefault();
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
Assert.True(sysUserMod.Id == "1"); Assert.True(sysUserMod.Id == "1");
Assert.Equal(sysUserModaa, sysUserMod);
var sysUserModxx = _virtualDbContext.Set<SysUserMod>().Where(o => x.Contains(o.Id)).FirstOrDefault(); var sysUserModxx = _virtualDbContext.Set<SysUserMod>().Where(o => x.Contains(o.Id)).FirstOrDefault();
Assert.NotNull(sysUserModxx); Assert.NotNull(sysUserModxx);
Assert.Equal(sysUserModaa, sysUserMod);
Assert.True(x.Contains(sysUserModxx.Id)); Assert.True(x.Contains(sysUserModxx.Id));
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1"); var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
@ -511,6 +506,7 @@ namespace ShardingCore.Test3x
Assert.True(user198.Id == "198"); Assert.True(user198.Id == "198");
var userId198 = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefault(); var userId198 = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefault();
Assert.Equal(userId198, "198"); Assert.Equal(userId198, "198");
} }
[Fact] [Fact]
@ -724,17 +720,10 @@ namespace ShardingCore.Test3x
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var virtualTableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName = virtualTableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName); Assert.True(string.IsNullOrWhiteSpace(virtualTableRoute.EntityMetadata.TableSeparator));
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -867,7 +856,7 @@ namespace ShardingCore.Test3x
catch (Exception e) catch (Exception e)
{ {
Assert.True(typeof(InvalidOperationException) == e.GetType() || typeof(TargetInvocationException) == e.GetType()); Assert.True(typeof(InvalidOperationException) == e.GetType() || typeof(TargetInvocationException) == e.GetType());
Assert.True(e.Message.Contains("contains") || e.InnerException.Message.Contains("contains")); Assert.True(e.Message.Contains("contains")|| e.InnerException.Message.Contains("contains"));
} }
} }
} }
@ -963,9 +952,9 @@ namespace ShardingCore.Test3x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -973,9 +962,9 @@ namespace ShardingCore.Test3x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {

View File

@ -30,7 +30,7 @@ namespace ShardingCore.Test3x.Shardings
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;

View File

@ -35,7 +35,7 @@ namespace ShardingCore.Test3x.Shardings
return true; return true;
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;

View File

@ -39,7 +39,7 @@ namespace ShardingCore.Test3x.Shardings
public override void Configure(EntityMetadataDataSourceBuilder<Order> builder) public override void Configure(EntityMetadataDataSourceBuilder<Order> builder)
{ {
builder.ShardingProperty(o => o.Area);
} }
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)

View File

@ -15,16 +15,16 @@ namespace ShardingCore.Test3x.Shardings
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var allTails = base.GetAllTails(); var allTails = base.CalcTailsOnStart();
allTails.Add("202112"); allTails.Add("202112");
return allTails; return allTails;
} }
public override void Configure(EntityMetadataTableBuilder<Order> builder) public override void Configure(EntityMetadataTableBuilder<Order> builder)
{ {
builder.ShardingProperty(o => o.CreateTime);
} }
public override IPaginationConfiguration<Order> CreatePaginationConfiguration() public override IPaginationConfiguration<Order> CreatePaginationConfiguration()

View File

@ -25,7 +25,7 @@ namespace ShardingCore.Test3x.Shardings
} }
public override List<string> GetAllTails() public override List<string> GetTails()
{ {
var beginTime = new DateTime(2020, 1, 1); var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1); var endTime = new DateTime(2021, 12, 1);

View File

@ -10,6 +10,7 @@ using ShardingCore.Bootstrappers;
using ShardingCore.Helpers; using ShardingCore.Helpers;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.TableExists; using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
using ShardingCore.Test3x.Domain.Entities; using ShardingCore.Test3x.Domain.Entities;
using ShardingCore.Test3x.Shardings; using ShardingCore.Test3x.Shardings;
@ -35,12 +36,8 @@ namespace ShardingCore.Test3x
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
{ {
services.AddShardingDbContext<ShardingDefaultDbContext>() services.AddShardingDbContext<ShardingDefaultDbContext>()
.AddEntityConfig(op => .UseRouteConfig(op =>
{ {
//如果您使用code-first建议选择false
op.CreateShardingTableOnStart = true;
//如果您使用code-first建议修改为fsle
op.EnsureCreatedWithOutShardingTable = true;
//当无法获取路由时会返回默认值而不是报错 //当无法获取路由时会返回默认值而不是报错
op.ThrowIfQueryRouteNotMatch = false; op.ThrowIfQueryRouteNotMatch = false;
op.AddShardingDataSourceRoute<OrderAreaShardingVirtualDataSourceRoute>(); op.AddShardingDataSourceRoute<OrderAreaShardingVirtualDataSourceRoute>();
@ -58,10 +55,8 @@ namespace ShardingCore.Test3x
op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>(); op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>();
}) })
.AddConfig(op => .UseConfig(op =>
{ {
op.ConfigId = "c1";
op.UseShardingQuery((conStr, builder) => op.UseShardingQuery((conStr, builder) =>
{ {
builder.UseSqlServer(conStr).UseLoggerFactory(efLogger); builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);
@ -93,15 +88,14 @@ namespace ShardingCore.Test3x
} }
}; };
}, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); }, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime);
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<ShardingDefaultDbContext>()); }).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>(ServiceLifetime.Singleton).AddShardingCore();
}).EnsureConfig();
} }
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
public void Configure(IServiceProvider serviceProvider) public void Configure(IServiceProvider serviceProvider)
{ {
var shardingBootstrapper = serviceProvider.GetService<IShardingBootstrapper>(); var shardingBootstrapper = serviceProvider.GetService<IShardingBootstrapper>();
shardingBootstrapper.Start(); shardingBootstrapper.AutoShardingCreate();
// 有一些测试数据要初始化可以放在这里 // 有一些测试数据要初始化可以放在这里
InitData(serviceProvider).GetAwaiter().GetResult(); InitData(serviceProvider).GetAwaiter().GetResult();
} }

View File

@ -6,10 +6,8 @@ namespace ShardingCore.Test5x.Domain.Entities
public class Order public class Order
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
[ShardingDataSourceKey]
public string Area { get; set; } public string Area { get; set; }
public long Money { get; set; } public long Money { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
} }

View File

@ -6,7 +6,7 @@ namespace ShardingCore.Test5x.Domain.Entities
* @Date: Thursday, 14 January 2021 15:36:43 * @Date: Thursday, 14 January 2021 15:36:43
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public class SysUserMod public class SysUserMod: IId
{ {
/// <summary> /// <summary>
/// 用户Id用于分表 /// 用户Id用于分表
@ -23,4 +23,10 @@ namespace ShardingCore.Test5x.Domain.Entities
public int AgeGroup { get; set; } public int AgeGroup { get; set; }
} }
public interface IId
{
string Id { get; set; }
}
} }

View File

@ -7,11 +7,10 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables; using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
@ -25,6 +24,7 @@ using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions; using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Sharding.ShardingDbContextExecutors; using ShardingCore.Sharding.ShardingDbContextExecutors;
using ShardingCore.Sharding.StreamMergeEngines;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
using ShardingCore.Test5x.Domain.Entities; using ShardingCore.Test5x.Domain.Entities;
using ShardingCore.Utils; using ShardingCore.Utils;
@ -32,48 +32,39 @@ using Xunit;
namespace ShardingCore.Test5x namespace ShardingCore.Test5x
{ {
/*
* @Author: xjm
* @Description:
* @Date: Friday, 15 January 2021 17:22:10
* @Email: 326308290@qq.com
*/
public class ShardingTest public class ShardingTest
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
@ -81,32 +72,32 @@ namespace ShardingCore.Test5x
// var expressionEqualityComparer = new RouteParseExpressionEqualityComparer(); // var expressionEqualityComparer = new RouteParseExpressionEqualityComparer();
// var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>(); // var virtualTable = _virtualTableManager.GetVirtualTable<SysUserSalary>();
// var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute(); // var virtualTableRoute = (AbstractShardingOperatorVirtualTableRoute<SysUserSalary, int>)virtualTable.GetVirtualRoute();
// // var xxxx = "202102";
// var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102); // var queryable1 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102);
// var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata, // var routeParseExpression1 = ShardingUtil.GetRouteParseExpression(queryable1, virtualTableRoute.EntityMetadata,
// (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102); // var queryable2 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102);
// var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata, // var routeParseExpression2 = ShardingUtil.GetRouteParseExpression(queryable2, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var xxxx1 = 202102; // var xxxx1 = 202102;
// var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1); // var queryable3 = _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1);
// var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata, // var routeParseExpression3 = ShardingUtil.GetRouteParseExpression(queryable3, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101); // var queryable4 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101);
// var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata, // var routeParseExpression4 = ShardingUtil.GetRouteParseExpression(queryable4, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101); // var queryable5 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202101);
// var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata, // var routeParseExpression5 = ShardingUtil.GetRouteParseExpression(queryable5, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101); // var queryable6 = _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202101);
// var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata, // var routeParseExpression6 = ShardingUtil.GetRouteParseExpression(queryable6, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth); // var queryable7 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 <= o.DateOfMonth);
// var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata, // var routeParseExpression7 = ShardingUtil.GetRouteParseExpression(queryable7, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth); // var queryable8 = _virtualDbContext.Set<SysUserSalary>().Where(o => 202101 == o.DateOfMonth);
// var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata, // var routeParseExpression8 = ShardingUtil.GetRouteParseExpression(queryable8, virtualTableRoute.EntityMetadata,
// (i, op,propertyName) => virtualTableRoute.GetRouteFilter(i, op,propertyName), true); // (i, op, propertyName) => virtualTableRoute.GetRouteFilter(i, op, propertyName), true);
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2)); // Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression2));
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3)); // Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression3));
// Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4)); // Assert.NotEqual(expressionEqualityComparer.GetHashCode(routeParseExpression1), expressionEqualityComparer.GetHashCode(routeParseExpression4));
@ -123,12 +114,14 @@ namespace ShardingCore.Test5x
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var y = new EntityMetadata(typeof(LogDay));
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
var bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays); var bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays);
Assert.Equal(100, bulkShardingTableEnumerable.Count); Assert.Equal(100, bulkShardingTableEnumerable.Count);
var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays); var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays);
Assert.Equal(1, bulkShardingEnumerable.Count); Assert.Equal(1, bulkShardingEnumerable.Count);
@ -182,12 +175,12 @@ namespace ShardingCore.Test5x
Assert.True(bulkShardingExpression.ContainsKey("B")); Assert.True(bulkShardingExpression.ContainsKey("B"));
var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString()); var bulkShardingTableExpression = _virtualDbContext.BulkShardingTableExpression<ShardingDefaultDbContext, SysUserMod>(o => o.Id == Guid.NewGuid().ToString());
Assert.Equal(1, bulkShardingTableExpression.Count()); Assert.Equal(1, bulkShardingTableExpression.Count());
var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123"); var noShardingExpression = _virtualDbContext.BulkShardingExpression<ShardingDefaultDbContext, LogNoSharding>(o => o.Id == "123");
Assert.Equal(1, noShardingExpression.Count()); Assert.Equal(1, noShardingExpression.Count());
var isShardingDbContext = _virtualDbContext.IsShardingDbContext(); var isShardingDbContext = _virtualDbContext.IsShardingDbContext();
Assert.True(isShardingDbContext); Assert.True(isShardingDbContext);
var isShardingTableDbContext = _virtualDbContext.IsShardingTableDbContext(); var isShardingTableDbContext = _virtualDbContext.IsShardingTableDbContext();
@ -196,17 +189,15 @@ namespace ShardingCore.Test5x
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity(); var emptyTailIdentity = new SingleQueryRouteTail(string.Empty).GetRouteTailIdentity();
var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity(); var aTailIdentity = new SingleQueryRouteTail("a").GetRouteTailIdentity();
var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity(); var bTailIdentity = new SingleQueryRouteTail("b").GetRouteTailIdentity();
var dics=new SortedDictionary<string, string>(new NoShardingFirstComparer()); var dics = new SortedDictionary<string, string>(new NoShardingFirstComparer());
var dicTails=new List<string>() { emptyTailIdentity, aTailIdentity, bTailIdentity }; var dicTails = new List<string>() { emptyTailIdentity, aTailIdentity, bTailIdentity };
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
dics.Clear(); dics.Clear();
var reOrderList=dicTails.OrderBy(o => Guid.NewGuid()).ToList(); var reOrderList = dicTails.OrderBy(o => Guid.NewGuid()).ToList();
foreach (var tail in reOrderList) foreach (var tail in reOrderList)
{ {
dics.Add(tail, null); dics.Add(tail, null);
@ -220,6 +211,10 @@ namespace ShardingCore.Test5x
{ new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), }); { new ParallelTableComparerType(typeof(SysUserSalary)),new ParallelTableComparerType(typeof(SysUserMod)), });
Assert.Equal(x1x1, x2x2); Assert.Equal(x1x1, x2x2);
Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode()); Assert.Equal(x1x1.GetHashCode(), x2x2.GetHashCode());
var succeedAddConnectionString = _shardingConnectionStringResolver.AddConnectionString("X", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;",null);
Assert.True(succeedAddConnectionString);
var connectionString = _shardingConnectionStringResolver.GetConnectionString("X", null);
Assert.Equal("Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;", connectionString);
} }
public class SequenceClass public class SequenceClass
@ -234,9 +229,13 @@ namespace ShardingCore.Test5x
var multiOrder = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 232398109278351360).FirstOrDefaultAsync(); var multiOrder = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 232398109278351360).FirstOrDefaultAsync();
Assert.NotNull(multiOrder); Assert.NotNull(multiOrder);
var allMultiOrders = await _virtualDbContext.Set<MultiShardingOrder>().ToListAsync();
Assert.Equal(8, allMultiOrders.Count);
var longs = new[] { 232398109278351360, 255197859283087360 }; var longs = new[] { 232398109278351360, 255197859283087360 };
var multiOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => longs.Contains(o.Id)).ToListAsync(); var multiOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => longs.Contains(o.Id)).ToListAsync();
Assert.Equal(2, multiOrders.Count); Assert.Equal(2, multiOrders.Count);
var multinNotOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => !longs.Contains(o.Id)).ToListAsync();
Assert.Equal(6, multinNotOrders.Count);
var dateTime = new DateTime(2021, 11, 1); var dateTime = new DateTime(2021, 11, 1);
var multiOrder404 = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 250345338962063360 && o.CreateTime < dateTime).FirstOrDefaultAsync(); var multiOrder404 = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 250345338962063360 && o.CreateTime < dateTime).FirstOrDefaultAsync();
Assert.Null(multiOrder404); Assert.Null(multiOrder404);
@ -271,7 +270,7 @@ namespace ShardingCore.Test5x
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
[Fact] [Fact]
@ -367,6 +366,17 @@ namespace ShardingCore.Test5x
[Fact] [Fact]
public async Task ToList_Join_Test() public async Task ToList_Join_Test()
{ {
var list111 = await (from u in _virtualDbContext.Set<SysUserMod>()
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
u.Id,
u.Age,
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
}).CountAsync();
var list = await (from u in _virtualDbContext.Set<SysUserMod>() var list = await (from u in _virtualDbContext.Set<SysUserMod>()
join salary in _virtualDbContext.Set<SysUserSalary>() join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId on u.Id equals salary.UserId
@ -411,7 +421,7 @@ namespace ShardingCore.Test5x
Assert.Contains(list3, o => o.Name == "name_300"); Assert.Contains(list3, o => o.Name == "name_300");
var firstOrDefaultAsync = await queryable1.OrderBy(o => o.DateOfMonth).FirstOrDefaultAsync(); var firstOrDefaultAsync = await queryable1.OrderBy(o => o.DateOfMonth).FirstOrDefaultAsync();
Assert.NotNull(firstOrDefaultAsync); Assert.NotNull(firstOrDefaultAsync);
var firstOrDefault = queryable1.OrderBy(o=>o.DateOfMonth).FirstOrDefault(); var firstOrDefault = queryable1.OrderBy(o => o.DateOfMonth).FirstOrDefault();
Assert.NotNull(firstOrDefault); Assert.NotNull(firstOrDefault);
Assert.Equal(firstOrDefaultAsync, firstOrDefault); Assert.Equal(firstOrDefaultAsync, firstOrDefault);
} }
@ -465,10 +475,25 @@ namespace ShardingCore.Test5x
[Fact] [Fact]
public async Task ToList_Id_Not_Eq_Test() public async Task ToList_Id_Not_Eq_Test()
{ {
var methodValue = new MethodValue() { AA = "7" };
var mods123 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == methodValue.GetAa()).FirstOrDefaultAsync();
Assert.NotNull(mods123);
Assert.Equal(mods123.Id, "7");
var mods12 = await _virtualDbContext.Set<SysUserMod>().Where(o => new List<string> { "3", "4" }.Contains(o.Id)).ToListAsync();
Assert.Contains(mods12, o => o.Id == "3" || o.Id == "4");
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync(); var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToListAsync();
Assert.Equal(999, mods.Count); Assert.Equal(999, mods.Count);
Assert.DoesNotContain(mods, o => o.Id == "3"); Assert.DoesNotContain(mods, o => o.Id == "3");
} }
public class MethodValue
{
public string AA { get; set; }
public string GetAa()
{
return AA;
}
}
[Fact] [Fact]
public async Task ToList_Id_Not_Eq_Skip_Test() public async Task ToList_Id_Not_Eq_Skip_Test()
@ -497,6 +522,10 @@ namespace ShardingCore.Test5x
[Fact] [Fact]
public async Task ToList_Id_Eq_Not_In_Db_Test() public async Task ToList_Id_Eq_Not_In_Db_Test()
{ {
var mod1s = await _virtualDbContext.Set<SysUserMod>().Where(o =>string.Compare(((IId)o).Id,"1001") ==0).ToListAsync();
var mod2s = await _virtualDbContext.Set<SysUserMod>().Where(o => string.Compare("1001", o.Id) == 0).ToListAsync();
var mod3s = await _virtualDbContext.Set<SysUserMod>().Where(o => "1001".CompareTo(o.Id)==0).ToListAsync();
var mod4s = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.CompareTo("1001") == 0).ToListAsync();
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync(); var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToListAsync();
Assert.Empty(mods); Assert.Empty(mods);
} }
@ -523,9 +552,23 @@ namespace ShardingCore.Test5x
} }
[Fact] [Fact]
public async Task FirstOrDefault2() public async Task FirstOrDefault2()
{ {
var sysUserModabxxxxx1 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx71 = await _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= 202102).FirstOrDefaultAsync();
var xxxx1 = 202102;
var sysUserModabxxxxx72 = await _virtualDbContext.Set<SysUserSalary>().Where(ox => ox.DateOfMonth >= xxxx1).FirstOrDefaultAsync();
var sysUserModabxxxxx2 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth >= 202101).FirstOrDefaultAsync();
var sysUserModabxxxxx3 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth > 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx4 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx5 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth < 202102).FirstOrDefaultAsync();
var sysUserModabxxxxx6 = await _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth <= 202102).FirstOrDefaultAsync();
var next = "1";
var sysUserMod1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == next).FirstOrDefaultAsync();
var sysUserModabxxx = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
var sysUserModabxxx11 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2" || o.Name == "name_3").FirstOrDefaultAsync();
var x = new Object[] { "1", "2" }; var x = new Object[] { "1", "2" };
var sysUserModab = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefaultAsync(); var sysUserModab = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefaultAsync();
Assert.NotNull(sysUserModab); Assert.NotNull(sysUserModab);
@ -556,8 +599,26 @@ namespace ShardingCore.Test5x
var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync(); var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").FirstOrDefaultAsync();
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
Assert.Equal("2", sysUserMod.Id); Assert.Equal("2", sysUserMod.Id);
var sysUserMod2 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").Select(o => new
{
Name=o.Name
}).FirstOrDefaultAsync();
var sysUserMod1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").Select(o => new TestClass(o.Name,""){Id = o.Id}).FirstOrDefaultAsync();
Assert.NotNull(sysUserMod1);
} }
public class TestClass
{
public string Name { get; }
public string Aa { get; }
public string Id { get; set; }
public TestClass(string name,string aa)
{
Name = name;
Aa = aa;
}
}
[Fact] [Fact]
public async Task FirstOrDefault4() public async Task FirstOrDefault4()
@ -574,6 +635,7 @@ namespace ShardingCore.Test5x
Assert.Null(sysUserMod); Assert.Null(sysUserMod);
} }
[Fact] [Fact]
public async Task Count_Test() public async Task Count_Test()
{ {
@ -684,6 +746,90 @@ namespace ShardingCore.Test5x
Assert.Equal(1140000, group[0].MaxSalary); Assert.Equal(1140000, group[0].MaxSalary);
} }
[Fact]
public async Task Group_Test1()
{
var ids = new[] { "200", "300" };
var dateOfMonths = new[] { 202111, 202110 };
var dateTime = DateTime.Now;
var sql = from u in _virtualDbContext.Set<SysUserSalary>()
group u by u.UserId
into g
select new
{
UI = g.Key,
x=g.Sum(o=>o.SalaryDecimal),
Now = dateTime
};
var listAsync = await sql.ToListAsync();
var group = await (from u in _virtualDbContext.Set<SysUserSalary>()
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
group u by new
{
UId = u.UserId
}
into g
select new
{
GroupUserId = g.Key.UId,
Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary)
}).ToListAsync();
Assert.Equal(2, group.Count);
Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
//[Fact]
//public async Task Group_Recently_Test()
//{
// //var list =(from us in _virtualDbContext.Set<SysUserSalary>().Where(o => ids.Contains(o.UserId))
// // group us by new
// // {
// // UserId=us.UserId
// // }
// // into g
// // select new
// // {
// // UserId=g.Key.UserId,
// // DateOfMonth = g.Max(o=>o.DateOfMonth)
// // }).ToList();
// //var y = list;
// var ids = new List<string>(){ "200", "300" };
// List<SysUserSalary> result = new List<SysUserSalary>(ids.Count);
// var routeFilter = new List<SysUserSalary>().AsQueryable().Where(o => ids.Contains(o.UserId));
// //<2F><>ȡ<EFBFBD><C8A1>·<EFBFBD><C2B7>ʱ<EFBFBD><EFBFBD><E4B5B9>
// var tableRouteResults = _tableRouteRuleEngineFactory.Route(routeFilter)
// .Select(o => o.ReplaceTables.First().Tail).OrderByDescending(o => o).ToList();
// foreach (var tableRouteResult in tableRouteResults)
// {
// if(ids.IsEmpty())
// break;
// using (_shardingRouteManager.CreateScope())
// {
// _shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserSalary>(tableRouteResult);
// var queryable = _virtualDbContext.Set<SysUserSalary>().Where(o => ids.Contains(o.UserId))
// .GroupBy(o => new { o.UserId }, i => i,
// (i, u) => new {
// Data = u.OrderByDescending(o => o.DateOfMonth).FirstOrDefault()
// });
// var r =await queryable.ToListAsync();
// result.AddRange(r.Select(o=>o.Data));
// var removeUserIds = result.Select(u => u.UserId).ToHashSet();
// ids.RemoveAll(o => removeUserIds.Contains(o));
// }
// }
//}
[Fact] [Fact]
public async Task OrderCountTest() public async Task OrderCountTest()
{ {
@ -775,17 +921,11 @@ namespace ShardingCore.Test5x
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var tableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName =tableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName);
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator)); Assert.True(string.IsNullOrWhiteSpace(tableRoute.EntityMetadata.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -819,7 +959,7 @@ namespace ShardingCore.Test5x
var fourBegin = new DateTime(2021, 4, 1).Date; var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date; var fiveBegin = new DateTime(2021, 5, 1).Date;
var moneyAverage = await _virtualDbContext.Set<Order>() var moneyAverage = await _virtualDbContext.Set<Order>()
.Where(o => o.CreateTime >= fourBegin && o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync(); .Where(o => o.CreateTime >= fourBegin&& o.CreateTime <= fiveBegin).Select(o => o.Money).AverageAsync();
Assert.Equal(105, moneyAverage); Assert.Equal(105, moneyAverage);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
@ -1021,9 +1161,9 @@ namespace ShardingCore.Test5x
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -1031,9 +1171,9 @@ namespace ShardingCore.Test5x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
@ -1223,6 +1363,7 @@ namespace ShardingCore.Test5x
Assert.Equal(300, countAsync1); Assert.Equal(300, countAsync1);
} }
Assert.Null(_shardingRouteManager.Current); Assert.Null(_shardingRouteManager.Current);
} }
[Fact] [Fact]
public async Task LogYearLongShardingPage() public async Task LogYearLongShardingPage()
@ -1309,13 +1450,13 @@ namespace ShardingCore.Test5x
logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd"); logNoSharding.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Update(logNoSharding); _virtualDbContext.Update(logNoSharding);
logNoShardings.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd")); logNoShardings.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.UpdateRange(logNoShardings); _virtualDbContext.UpdateRange(logNoShardings);
logNoSharding1.Body = DateTime.Now.ToString("yyyyMMdd"); logNoSharding1.Body = DateTime.Now.ToString("yyyyMMdd");
_virtualDbContext.Set<LogNoSharding>().Update(logNoSharding1); _virtualDbContext.Set<LogNoSharding>().Update(logNoSharding1);
logNoSharding1s.ForEach(o=>o.Body = DateTime.Now.ToString("yyyyMMdd")); logNoSharding1s.ForEach(o => o.Body = DateTime.Now.ToString("yyyyMMdd"));
_virtualDbContext.Set<LogNoSharding>().UpdateRange(logNoSharding1s); _virtualDbContext.Set<LogNoSharding>().UpdateRange(logNoSharding1s);
await _virtualDbContext.SaveChangesAsync(); await _virtualDbContext.SaveChangesAsync();
@ -1362,7 +1503,7 @@ namespace ShardingCore.Test5x
{ {
await _virtualDbContext.AddAsync((object)logNoSharding); await _virtualDbContext.AddAsync((object)logNoSharding);
await _virtualDbContext.AddRangeAsync(logNoShardings.Select(o=>(object)o).ToArray()); await _virtualDbContext.AddRangeAsync(logNoShardings.Select(o => (object)o).ToArray());
await _virtualDbContext.SaveChangesAsync(); await _virtualDbContext.SaveChangesAsync();
await tran.CommitAsync(); await tran.CommitAsync();
@ -1495,6 +1636,7 @@ namespace ShardingCore.Test5x
Assert.Equal(10, page1.Data.Count); Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total); Assert.Equal(300, page1.Total);
} }
[Fact] [Fact]
public async Task AsRouteTest() public async Task AsRouteTest()
{ {
@ -1533,8 +1675,8 @@ namespace ShardingCore.Test5x
[Fact] [Fact]
public async Task QueryInner_Test() public async Task QueryInner_Test()
{ {
var sysUserMods = _virtualDbContext.Set<SysUserMod>().Select(o => o); var sysUserMods = _virtualDbContext.Set<SysUserMod>().Select(o=>o);
var sysUserModInts = await _virtualDbContext.Set<SysUserModInt>().Where(o => sysUserMods.Select(i => i.Age).Any(i => i == o.Age)).ToListAsync(); var sysUserModInts = await _virtualDbContext.Set<SysUserModInt>().Where(o=>sysUserMods.Select(i=>i.Age).Any(i=>i==o.Age)).ToListAsync();
Assert.Equal(1000, sysUserModInts.Count); Assert.Equal(1000, sysUserModInts.Count);
} }
// [Fact] // [Fact]

View File

@ -1,31 +1,29 @@
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions; using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions; using ShardingCore.Extensions.ShardingPageExtensions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.Helpers; using ShardingCore.Helpers;
using ShardingCore.Sharding; using ShardingCore.Sharding;
using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
using ShardingCore.Test5x.Domain.Entities; using ShardingCore.Test5x.Domain.Entities;
using ShardingCore.Utils; using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualRoutes.Abstractions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using Xunit; using Xunit;
namespace ShardingCore.Test5x namespace ShardingCore.Test5x
@ -36,40 +34,35 @@ namespace ShardingCore.Test5x
{ {
private readonly ShardingDefaultDbContext _virtualDbContext; private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager; private readonly IShardingRouteManager _shardingRouteManager;
private readonly ActualConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager; private readonly ActualConnectionStringManager _connectionStringManager;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager; private readonly ITableRouteManager _tableRouteManager;
private readonly IShardingTableCreator<ShardingDefaultDbContext> _shardingTableCreator; private readonly IShardingTableCreator _shardingTableCreator;
private readonly IShardingReadWriteManager _shardingReadWriteManager; private readonly IShardingReadWriteManager _shardingReadWriteManager;
private readonly IRouteTailFactory _routeTailFactory; private readonly IRouteTailFactory _routeTailFactory;
private readonly IReadWriteConnectorFactory _readWriteConnectorFactory; private readonly IReadWriteConnectorFactory _readWriteConnectorFactory;
private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver; private readonly IShardingConnectionStringResolver _shardingConnectionStringResolver;
private readonly IShardingComparer _shardingComparer;
public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager, IConfiguration configuration, public ShardingTestSync(ShardingDefaultDbContext virtualDbContext, IShardingRuntimeContext shardingRuntimeContext, IConfiguration configuration)
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IVirtualDataSourceManager<ShardingDefaultDbContext> virtualDataSourceManager,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager,
IShardingTableCreator<ShardingDefaultDbContext> shardingTableCreator,
IShardingReadWriteManager shardingReadWriteManager, IRouteTailFactory routeTailFactory,
IReadWriteConnectorFactory readWriteConnectorFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager; _shardingRouteManager = shardingRuntimeContext.GetShardingRouteManager();
_virtualDataSource = virtualDataSourceManager.GetCurrentVirtualDataSource(); _shardingReadWriteManager = shardingRuntimeContext.GetShardingReadWriteManager();
_connectionStringManager = new ActualConnectionStringManager<ShardingDefaultDbContext>(_virtualDataSource); _virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
_connectionStringManager = new ActualConnectionStringManager(_shardingReadWriteManager,_virtualDataSource);
_configuration = configuration; _configuration = configuration;
this._entityMetadataManager = entityMetadataManager; _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_virtualTableManager = virtualTableManager; _tableRouteManager = shardingRuntimeContext.GetTableRouteManager();
_shardingTableCreator = shardingTableCreator; _shardingTableCreator = shardingRuntimeContext.GetShardingTableCreator();
_shardingReadWriteManager = shardingReadWriteManager; _routeTailFactory = shardingRuntimeContext.GetRouteTailFactory();
_routeTailFactory = routeTailFactory; _shardingComparer = shardingRuntimeContext.GetShardingComparer();
_readWriteConnectorFactory = readWriteConnectorFactory; _readWriteConnectorFactory = shardingRuntimeContext.GetReadWriteConnectorFactory();
var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value)); var readWriteConnectors = _virtualDataSource.ConfigurationParams.ReadWriteNodeSeparationConfigs.Select(o => _readWriteConnectorFactory.CreateConnector(_virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(), o.Key, o.Value));
_shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault()); _shardingConnectionStringResolver = new ReadWriteShardingConnectionStringResolver(readWriteConnectors, _virtualDataSource.ConfigurationParams.ReadStrategy.GetValueOrDefault(),_readWriteConnectorFactory);
} }
// [Fact] // [Fact]
// public void RouteParseCompileCacheTest() // public void RouteParseCompileCacheTest()
// { // {
@ -111,14 +104,17 @@ namespace ShardingCore.Test5x
// Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8)); // Assert.Equal(expressionEqualityComparer.GetHashCode(routeParseExpression6), expressionEqualityComparer.GetHashCode(routeParseExpression8));
// //
// } // }
[Fact] [Fact]
public void GenericTest() public void GenericTest()
{ {
var a = new DefaultPhysicDataSource("aaa", "aaa", true); var a = new DefaultPhysicDataSource("aaa", "aaa", true);
var b = new DefaultPhysicDataSource("aaa", "aaa1", false); var b = new DefaultPhysicDataSource("aaa", "aaa1", false);
Assert.Equal(a, b); Assert.Equal(a, b);
var x = new EntityMetadata(typeof(LogDay), "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); var x = new EntityMetadata(typeof(LogDay));
var y = new EntityMetadata(typeof(LogDay), "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null); //, "aa", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
var y = new EntityMetadata(typeof(LogDay));
//, "aa1", typeof(ShardingDefaultDbContext), new List<PropertyInfo>(),null
Assert.Equal(x, y); Assert.Equal(x, y);
var dateTime = new DateTime(2021, 1, 1); var dateTime = new DateTime(2021, 1, 1);
var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList(); var logDays = Enumerable.Range(0, 100).Select(o => new LogDay() { Id = Guid.NewGuid(), LogLevel = "info", LogBody = o.ToString(), LogTime = dateTime.AddDays(o) }).ToList();
@ -186,8 +182,6 @@ namespace ShardingCore.Test5x
Assert.True(shardingDbContext); Assert.True(shardingDbContext);
var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext(); var shardingTableDbContext = _virtualDbContext.GetType().IsShardingTableDbContext();
Assert.True(shardingTableDbContext); Assert.True(shardingTableDbContext);
var virtualTable = _virtualTableManager.GetVirtualTable<SysUserMod>();
Assert.NotNull(virtualTable);
var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>() var x1x1 = new ParallelTableGroupNode(new HashSet<ParallelTableComparerType>()
{ new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) }); { new ParallelTableComparerType(typeof(SysUserMod)), new ParallelTableComparerType(typeof(SysUserSalary)) });
@ -203,6 +197,7 @@ namespace ShardingCore.Test5x
public string Id { get; set; } public string Id { get; set; }
public string T { get; set; } public string T { get; set; }
} }
[Fact] [Fact]
public void TestMultiShardingProperty() public void TestMultiShardingProperty()
{ {
@ -246,7 +241,7 @@ namespace ShardingCore.Test5x
var compare0 = x.CompareTo(y); var compare0 = x.CompareTo(y);
Assert.True(compare0 > 0); Assert.True(compare0 > 0);
//asc x<y db compare uniqueidentifier //asc x<y db compare uniqueidentifier
var compare1 = _virtualDataSource.ConfigurationParams.ShardingComparer.Compare(x, y, true); var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0); Assert.True(compare1 < 0);
} }
//[Fact] //[Fact]
@ -364,7 +359,6 @@ namespace ShardingCore.Test5x
Assert.Equal(24, list1.Count()); Assert.Equal(24, list1.Count());
Assert.DoesNotContain(list1, o => o.Name != "name_300"); Assert.DoesNotContain(list1, o => o.Name != "name_300");
var queryable1 = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300") var queryable1 = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
join salary in _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202005) join salary in _virtualDbContext.Set<SysUserSalary>().Where(o => o.DateOfMonth == 202005)
on u.Id equals salary.UserId on u.Id equals salary.UserId
@ -489,6 +483,7 @@ namespace ShardingCore.Test5x
[Fact] [Fact]
public void FirstOrDefault2() public void FirstOrDefault2()
{ {
var x = new Object[] { "1", "2" }; var x = new Object[] { "1", "2" };
var sysUserModab = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefault(); var sysUserModab = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id.Equals("1")).FirstOrDefault();
Assert.NotNull(sysUserModab); Assert.NotNull(sysUserModab);
@ -499,9 +494,9 @@ namespace ShardingCore.Test5x
var sysUserMod = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefault(); var sysUserMod = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").FirstOrDefault();
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
Assert.True(sysUserMod.Id == "1"); Assert.True(sysUserMod.Id == "1");
Assert.Equal(sysUserModaa, sysUserMod);
var sysUserModxx = _virtualDbContext.Set<SysUserMod>().Where(o => x.Contains(o.Id)).FirstOrDefault(); var sysUserModxx = _virtualDbContext.Set<SysUserMod>().Where(o => x.Contains(o.Id)).FirstOrDefault();
Assert.NotNull(sysUserModxx); Assert.NotNull(sysUserModxx);
Assert.Equal(sysUserModaa, sysUserMod);
Assert.True(x.Contains(sysUserModxx.Id)); Assert.True(x.Contains(sysUserModxx.Id));
Assert.NotNull(sysUserMod); Assert.NotNull(sysUserMod);
var userMod = _virtualDbContext.Set<SysUserMod>().Find("1"); var userMod = _virtualDbContext.Set<SysUserMod>().Find("1");
@ -511,6 +506,7 @@ namespace ShardingCore.Test5x
Assert.True(user198.Id == "198"); Assert.True(user198.Id == "198");
var userId198 = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefault(); var userId198 = _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "198").Select(o => o.Id).FirstOrDefault();
Assert.Equal(userId198, "198"); Assert.Equal(userId198, "198");
} }
[Fact] [Fact]
@ -724,17 +720,10 @@ namespace ShardingCore.Test5x
[Fact] [Fact]
public void LogDayTableSeparatorTest() public void LogDayTableSeparatorTest()
{ {
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay)); var virtualTableRoute = _tableRouteManager.GetRoute(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName(); var virtualTableName = virtualTableRoute.EntityMetadata.LogicTableName;
Assert.Equal(nameof(LogDay), virtualTableName); Assert.Equal(nameof(LogDay), virtualTableName);
var table = _virtualTableManager.GetVirtualTable(virtualTableName); Assert.True(string.IsNullOrWhiteSpace(virtualTableRoute.EntityMetadata.TableSeparator));
var tryGetVirtualTable = _virtualTableManager.TryGetVirtualTable(typeof(LogDay));
Assert.NotNull(tryGetVirtualTable);
var tryGetVirtualTable1 = _virtualTableManager.TryGetVirtualTable(virtualTableName);
Assert.NotNull(tryGetVirtualTable1);
var all = virtualTable.GetAllPhysicTables().All(o => string.IsNullOrWhiteSpace(o.TableSeparator));
Assert.True(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>(); var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata); Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable(); var isShardingTable = entityMetadata.IsShardingTable();
@ -867,7 +856,7 @@ namespace ShardingCore.Test5x
catch (Exception e) catch (Exception e)
{ {
Assert.True(typeof(InvalidOperationException) == e.GetType() || typeof(TargetInvocationException) == e.GetType()); Assert.True(typeof(InvalidOperationException) == e.GetType() || typeof(TargetInvocationException) == e.GetType());
Assert.True(e.Message.Contains("contains") || e.InnerException.Message.Contains("contains")); Assert.True(e.Message.Contains("contains")|| e.InnerException.Message.Contains("contains"));
} }
} }
} }
@ -963,9 +952,9 @@ namespace ShardingCore.Test5x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {
_shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A"); _shardingRouteManager.Current.TryCreateOrAddMustDataSource<Order>("A");
@ -973,9 +962,9 @@ namespace ShardingCore.Test5x
Assert.NotNull(areaB); Assert.NotNull(areaB);
} }
} }
using (_shardingReadWriteManager.CreateScope<ShardingDefaultDbContext>()) using (_shardingReadWriteManager.CreateScope())
{ {
_shardingReadWriteManager.GetCurrent<ShardingDefaultDbContext>().SetReadWriteSeparation(100, true); _shardingReadWriteManager.GetCurrent().SetReadWriteSeparation(100, true);
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
using (_shardingRouteManager.CreateScope()) using (_shardingRouteManager.CreateScope())
{ {

View File

@ -30,7 +30,7 @@ namespace ShardingCore.Test5x.Shardings
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;

View File

@ -35,7 +35,7 @@ namespace ShardingCore.Test5x.Shardings
return true; return true;
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;

View File

@ -39,7 +39,7 @@ namespace ShardingCore.Test5x.Shardings
public override void Configure(EntityMetadataDataSourceBuilder<Order> builder) public override void Configure(EntityMetadataDataSourceBuilder<Order> builder)
{ {
builder.ShardingProperty(o => o.Area);
} }
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)

View File

@ -15,16 +15,16 @@ namespace ShardingCore.Test5x.Shardings
return new DateTime(2021, 1, 1); return new DateTime(2021, 1, 1);
} }
public override List<string> GetAllTails() protected override List<string> CalcTailsOnStart()
{ {
var allTails = base.GetAllTails(); var allTails = base.CalcTailsOnStart();
allTails.Add("202112"); allTails.Add("202112");
return allTails; return allTails;
} }
public override void Configure(EntityMetadataTableBuilder<Order> builder) public override void Configure(EntityMetadataTableBuilder<Order> builder)
{ {
builder.ShardingProperty(o => o.CreateTime);
} }
public override IPaginationConfiguration<Order> CreatePaginationConfiguration() public override IPaginationConfiguration<Order> CreatePaginationConfiguration()

View File

@ -24,7 +24,7 @@ namespace ShardingCore.Test5x.Shardings
} }
public override List<string> GetAllTails() public override List<string> GetTails()
{ {
var beginTime = new DateTime(2020, 1, 1); var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1); var endTime = new DateTime(2021, 12, 1);

View File

@ -10,6 +10,7 @@ using ShardingCore.Bootstrappers;
using ShardingCore.Helpers; using ShardingCore.Helpers;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.TableExists; using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
using ShardingCore.Test5x.Domain.Entities; using ShardingCore.Test5x.Domain.Entities;
using ShardingCore.Test5x.Shardings; using ShardingCore.Test5x.Shardings;
@ -35,12 +36,8 @@ namespace ShardingCore.Test5x
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
{ {
services.AddShardingDbContext<ShardingDefaultDbContext>() services.AddShardingDbContext<ShardingDefaultDbContext>()
.AddEntityConfig(op => .UseRouteConfig(op =>
{ {
//如果您使用code-first建议选择false
op.CreateShardingTableOnStart = true;
//如果您使用code-first建议修改为fsle
op.EnsureCreatedWithOutShardingTable = true;
//当无法获取路由时会返回默认值而不是报错 //当无法获取路由时会返回默认值而不是报错
op.ThrowIfQueryRouteNotMatch = false; op.ThrowIfQueryRouteNotMatch = false;
op.AddShardingDataSourceRoute<OrderAreaShardingVirtualDataSourceRoute>(); op.AddShardingDataSourceRoute<OrderAreaShardingVirtualDataSourceRoute>();
@ -58,9 +55,8 @@ namespace ShardingCore.Test5x
op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>(); op.AddShardingTableRoute<MultiShardingOrderVirtualTableRoute>();
}) })
.AddConfig(op => .UseConfig(op =>
{ {
op.ConfigId = "c1";
op.UseShardingQuery((conStr, builder) => op.UseShardingQuery((conStr, builder) =>
{ {
@ -93,8 +89,7 @@ namespace ShardingCore.Test5x
} }
}; };
}, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); }, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime);
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<ShardingDefaultDbContext>()); }).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>(ServiceLifetime.Singleton).AddShardingCore();
}).EnsureConfig();
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15")) // services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// ,op => // ,op =>
// { // {
@ -110,8 +105,9 @@ namespace ShardingCore.Test5x
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
public void Configure(IServiceProvider serviceProvider) public void Configure(IServiceProvider serviceProvider)
{ {
var shardingBootstrapper = serviceProvider.GetService<IShardingBootstrapper>(); serviceProvider.UseAutoShardingCreate();
shardingBootstrapper.Start(); serviceProvider.UseAutoTryCompensateTable();
// 有一些测试数据要初始化可以放在这里 // 有一些测试数据要初始化可以放在这里
InitData(serviceProvider).GetAwaiter().GetResult(); InitData(serviceProvider).GetAwaiter().GetResult();
} }