优化顺序分页和单元测试覆盖率的提高

This commit is contained in:
xuejiaming 2021-11-22 15:09:19 +08:00
parent 273c7504e8
commit 0fb4ba0b10
89 changed files with 1738 additions and 1442 deletions

View File

@ -13,14 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.3x", "src3x\Sh
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CC2C88C0-65F2-445D-BE78-973B840FE281}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50", "test\ShardingCore.Test50\ShardingCore.Test50.csproj", "{7EE133B6-5A02-41B7-9D89-41D9EA14184E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServer", "samples\Sample.SqlServer\Sample.SqlServer.csproj", "{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50.MySql", "test\ShardingCore.Test50.MySql\ShardingCore.Test50.MySql.csproj", "{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AutoByDate.SqlServer", "samples\Samples.AutoByDate.SqlServer\Samples.AutoByDate.SqlServer.csproj", "{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.MySql", "samples\Sample.MySql\Sample.MySql.csproj", "{90675788-D5C3-415A-9C18-FF159A75B4D5}"
@ -29,16 +25,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServer3x", "sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AbpSharding", "samples\Samples.AbpSharding\Samples.AbpSharding.csproj", "{1136B8C9-3539-42FA-97FD-CAA6F146FCF0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50_3x", "test\ShardingCore.Test50_3x\ShardingCore.Test50_3x.csproj", "{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.BulkConsole", "samples\Sample.BulkConsole\Sample.BulkConsole.csproj", "{2443CC8B-FB7D-47A7-9663-F3848BB30A36}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src2x", "src2x", "{F91949B0-02D5-4E3B-ACF4-AFA6C99A1E04}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.2x", "src2x\ShardingCore.2x\ShardingCore.2x.csproj", "{A07C597D-339D-4378-BE4C-A2AF7473340B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test50_2x", "test\ShardingCore.Test50_2x\ShardingCore.Test50_2x.csproj", "{E4DAA43A-B64D-45CF-81B8-7B8FD338D686}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServerShardingDataSource", "samples\Sample.SqlServerShardingDataSource\Sample.SqlServerShardingDataSource.csproj", "{0193E3CF-F2FD-449A-B2D5-7F68E551FDBF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src5x", "src5x", "{EB1C9149-78C9-4D99-BE3F-B80FE2015E96}"
@ -51,7 +43,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServerShardingTab
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.SqlServerShardingAll", "samples\Sample.SqlServerShardingAll\Sample.SqlServerShardingAll.csproj", "{FD2C6D03-8D6D-4C1C-B534-4C785A4B1B06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test6x", "test\ShardingCore.Test6x\ShardingCore.Test6x.csproj", "{71D85D50-7F2B-4FEF-BC6F-A80DBDDAC7E8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShardingCore.Test6x", "test\ShardingCore.Test6x\ShardingCore.Test6x.csproj", "{71D85D50-7F2B-4FEF-BC6F-A80DBDDAC7E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test5x", "test\ShardingCore.Test5x\ShardingCore.Test5x.csproj", "{59BDEB59-1674-4A71-8F3F-0A690F48ADE6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test3x", "test\ShardingCore.Test3x\ShardingCore.Test3x.csproj", "{E64E09EF-2DC0-4948-A948-256EF5F95C53}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShardingCore.Test2x", "test\ShardingCore.Test2x\ShardingCore.Test2x.csproj", "{5ED4AF17-F16D-4857-B19C-018831109991}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -67,18 +65,10 @@ Global
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414}.Release|Any CPU.Build.0 = Release|Any CPU
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EE133B6-5A02-41B7-9D89-41D9EA14184E}.Release|Any CPU.Build.0 = Release|Any CPU
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F}.Release|Any CPU.Build.0 = Release|Any CPU
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3}.Release|Any CPU.Build.0 = Release|Any CPU
{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -95,10 +85,6 @@ Global
{1136B8C9-3539-42FA-97FD-CAA6F146FCF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1136B8C9-3539-42FA-97FD-CAA6F146FCF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1136B8C9-3539-42FA-97FD-CAA6F146FCF0}.Release|Any CPU.Build.0 = Release|Any CPU
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88}.Release|Any CPU.Build.0 = Release|Any CPU
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2443CC8B-FB7D-47A7-9663-F3848BB30A36}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -107,10 +93,6 @@ Global
{A07C597D-339D-4378-BE4C-A2AF7473340B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A07C597D-339D-4378-BE4C-A2AF7473340B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A07C597D-339D-4378-BE4C-A2AF7473340B}.Release|Any CPU.Build.0 = Release|Any CPU
{E4DAA43A-B64D-45CF-81B8-7B8FD338D686}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4DAA43A-B64D-45CF-81B8-7B8FD338D686}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4DAA43A-B64D-45CF-81B8-7B8FD338D686}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4DAA43A-B64D-45CF-81B8-7B8FD338D686}.Release|Any CPU.Build.0 = Release|Any CPU
{0193E3CF-F2FD-449A-B2D5-7F68E551FDBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0193E3CF-F2FD-449A-B2D5-7F68E551FDBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0193E3CF-F2FD-449A-B2D5-7F68E551FDBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -135,6 +117,18 @@ Global
{71D85D50-7F2B-4FEF-BC6F-A80DBDDAC7E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71D85D50-7F2B-4FEF-BC6F-A80DBDDAC7E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71D85D50-7F2B-4FEF-BC6F-A80DBDDAC7E8}.Release|Any CPU.Build.0 = Release|Any CPU
{59BDEB59-1674-4A71-8F3F-0A690F48ADE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59BDEB59-1674-4A71-8F3F-0A690F48ADE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59BDEB59-1674-4A71-8F3F-0A690F48ADE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59BDEB59-1674-4A71-8F3F-0A690F48ADE6}.Release|Any CPU.Build.0 = Release|Any CPU
{E64E09EF-2DC0-4948-A948-256EF5F95C53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E64E09EF-2DC0-4948-A948-256EF5F95C53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E64E09EF-2DC0-4948-A948-256EF5F95C53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E64E09EF-2DC0-4948-A948-256EF5F95C53}.Release|Any CPU.Build.0 = Release|Any CPU
{5ED4AF17-F16D-4857-B19C-018831109991}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5ED4AF17-F16D-4857-B19C-018831109991}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5ED4AF17-F16D-4857-B19C-018831109991}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5ED4AF17-F16D-4857-B19C-018831109991}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -142,23 +136,22 @@ Global
GlobalSection(NestedProjects) = preSolution
{3CAF09A6-6ABD-41D9-BA57-9A822B8095F7} = {490FAE47-4476-4508-B216-505FC850447F}
{E0FDBFA9-AE5F-42FB-8A60-4E5D9E7D5414} = {62AAE0FE-4099-4A48-AA3C-F76F14C62655}
{7EE133B6-5A02-41B7-9D89-41D9EA14184E} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{D48E5EC2-CF83-4B17-8BBA-BDE52ADFAB1F} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{C8FAB96F-F13E-4094-883C-2D38D39EE4A3} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{C34FCF48-1A98-4268-BFEE-6C9BFC7FD539} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{90675788-D5C3-415A-9C18-FF159A75B4D5} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{447D5357-F095-45DE-9DA5-2D9997237366} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{1136B8C9-3539-42FA-97FD-CAA6F146FCF0} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{C0A59BB0-F0B8-4AC6-B192-0249E784FC88} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{2443CC8B-FB7D-47A7-9663-F3848BB30A36} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{A07C597D-339D-4378-BE4C-A2AF7473340B} = {F91949B0-02D5-4E3B-ACF4-AFA6C99A1E04}
{E4DAA43A-B64D-45CF-81B8-7B8FD338D686} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{0193E3CF-F2FD-449A-B2D5-7F68E551FDBF} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{68A9F118-EF0A-4D03-8845-77D084561A28} = {EB1C9149-78C9-4D99-BE3F-B80FE2015E96}
{648DCBBE-BE8F-4EAC-8367-FE7BC558DA8C} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{88FA5615-1BAA-4021-87EF-9D4A60257FE4} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{FD2C6D03-8D6D-4C1C-B534-4C785A4B1B06} = {EDF8869A-C1E1-491B-BC9F-4A33F4DE1C73}
{71D85D50-7F2B-4FEF-BC6F-A80DBDDAC7E8} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{59BDEB59-1674-4A71-8F3F-0A690F48ADE6} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{E64E09EF-2DC0-4948-A948-256EF5F95C53} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
{5ED4AF17-F16D-4857-B19C-018831109991} = {CC2C88C0-65F2-445D-BE78-973B840FE281}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8C07A667-E8B4-43C7-8053-721584BAD291}

View File

@ -165,10 +165,10 @@ namespace Sample.SqlServer.Controllers
using (var tran = _defaultTableDbContext.Database.BeginTransaction())
{
var dbContexts = _defaultTableDbContext.BulkShardingTableEnumerable(list);
dbContexts.ForEach(kv =>
foreach (var kv in dbContexts)
{
kv.Key.BulkInsert(kv.Value.ToList());
});
}
var a = 0;
var b = 1 / a;
tran.Commit();

View File

@ -14,13 +14,6 @@ namespace ShardingCore.Exceptions
*/
public class ShardingCoreException: Exception
{
public ShardingCoreException()
{
}
protected ShardingCoreException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
public ShardingCoreException(string message) : base(message)
{

View File

@ -14,16 +14,16 @@ namespace ShardingCore.Extensions
*/
public static class GenericExtension
{
public static Type[] GetGenericArguments(this Type type, Type genericType)
{
return type.GetInterfaces() //取类型的接口
.Where(i => IsGenericType(i)) //筛选出相应泛型接口
.SelectMany(i => i.GetGenericArguments()) //选择所有接口的泛型参数
.ToArray(); //ToArray
//public static Type[] GetGenericArguments(this Type type, Type genericType)
//{
// return type.GetInterfaces() //取类型的接口
// .Where(i => IsGenericType(i)) //筛选出相应泛型接口
// .SelectMany(i => i.GetGenericArguments()) //选择所有接口的泛型参数
// .ToArray(); //ToArray
bool IsGenericType(Type type1)
=> type1.IsGenericType && type1.GetGenericTypeDefinition() == genericType;
}
// bool IsGenericType(Type type1)
// => type1.IsGenericType && type1.GetGenericTypeDefinition() == genericType;
//}
public static bool HasImplementedRawGeneric(this Type type, Type generic)
{

View File

@ -51,42 +51,6 @@ namespace ShardingCore.Extensions
{
return values.Contains(thisValue);
}
/// <summary>
/// 给IEnumerable拓展ForEach方法
/// </summary>
/// <typeparam name="T">模型类</typeparam>
/// <param name="enumberable">数据源</param>
/// <param name="func">方法</param>
public static void ForEach<T>(this IEnumerable<T> enumberable, Action<T> func)
{
foreach (var item in enumberable)
{
func(item);
}
}
/// <summary>
/// 是否有差异
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasDifference<T>(this IEnumerable<T> source)
{
return source.Distinct().Count() > 1;
}
/// <summary>
/// 是否有差异
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source"></param>
/// <param name="keySelector"></param>
/// <returns></returns>
public static bool HasDifference<T,TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
{
return source.Select(keySelector).Distinct().Count() > 1;
}
}
}

View File

@ -1,88 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
namespace ShardingCore.Helpers
{
internal static class XmlHelper
{
/// <summary>
/// 通过XML获取类属性注释
/// 参考:https://github.com/dotnetcore/FreeSql/blob/d266446062b1dfcd694f7d213191cd2383410025/FreeSql/Internal/CommonUtils.cs
/// </summary>
/// <param name="type">类型</param>
/// <returns>Dictkey=属性名value=注释</returns>
public static Dictionary<string, string> GetProperyCommentBySummary(Type type)
{
if (type.Assembly.IsDynamic) return null;
//动态生成的程序集,访问不了 Assembly.Location/Assembly.CodeBase
var regex = new Regex(@"\.(dll|exe)", RegexOptions.IgnoreCase);
var xmlPath = regex.Replace(type.Assembly.Location, ".xml");
if (File.Exists(xmlPath) == false)
{
if (string.IsNullOrEmpty(type.Assembly.CodeBase)) return null;
xmlPath = regex.Replace(type.Assembly.CodeBase, ".xml");
if (xmlPath.StartsWith("file:///") && Uri.TryCreate(xmlPath, UriKind.Absolute, out var tryuri))
xmlPath = tryuri.LocalPath;
if (File.Exists(xmlPath) == false) return null;
}
var dic = new Dictionary<string, string>();
var sReader = new StringReader(File.ReadAllText(xmlPath));
using (var xmlReader = XmlReader.Create(sReader))
{
XPathDocument xpath = null;
try
{
xpath = new XPathDocument(xmlReader);
}
catch
{
return null;
}
var xmlNav = xpath.CreateNavigator();
var className = (type.IsNested ? $"{type.Namespace}.{type.DeclaringType.Name}.{type.Name}" : $"{type.Namespace}.{type.Name}").Trim('.');
var node = xmlNav.SelectSingleNode($"/doc/members/member[@name='T:{className}']/summary");
if (node != null)
{
var comment = node.InnerXml.Trim(' ', '\r', '\n', '\t');
if (string.IsNullOrEmpty(comment) == false) dic.Add("", comment); //class注释
}
var props = type.GetPropertiesDictIgnoreCase().Values;
foreach (var prop in props)
{
className = (prop.DeclaringType.IsNested ? $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.DeclaringType.Name}.{prop.DeclaringType.Name}" : $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.Name}").Trim('.');
node = xmlNav.SelectSingleNode($"/doc/members/member[@name='P:{className}.{prop.Name}']/summary");
if (node == null) continue;
var comment = node.InnerXml.Trim(' ', '\r', '\n', '\t');
if (string.IsNullOrEmpty(comment)) continue;
dic.Add(prop.Name, comment);
}
}
return dic;
}
private static ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> _dicGetPropertiesDictIgnoreCase
= new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
private static Dictionary<string, PropertyInfo> GetPropertiesDictIgnoreCase(this Type that) => that == null ? null : _dicGetPropertiesDictIgnoreCase.GetOrAdd(that, tp =>
{
var props = that.GetProperties().GroupBy(p => p.DeclaringType).Reverse().SelectMany(p => p); //将基类的属性位置放在前面 #164
var dict = new Dictionary<string, PropertyInfo>(StringComparer.CurrentCultureIgnoreCase);
foreach (var prop in props)
{
if (dict.ContainsKey(prop.Name)) continue;
dict.Add(prop.Name, prop);
}
return dict;
});
}
}

View File

@ -64,7 +64,7 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
var currentRealSkip = currentSkip;
var currentRealTake = routeQueryResult.QueryResult-currentSkip;
if (currentSkip != 0)
if (currentSkip != 0L)
currentSkip = 0;
if (currentTake.HasValue)
{
@ -75,7 +75,7 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
}
else
{
currentRealTake = currentTake.Value-currentRealTake;
currentTake = currentTake.Value - currentRealTake;
}
}
var sequenceResult = new SequenceResult(currentRealSkip, currentRealTake, routeQueryResult);

View File

@ -113,10 +113,16 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
.FirstOrDefault(o => o.AppendIfOrderNone && typeof(TEntity).ContainPropertyName(o.PropertyName) && PaginationMatch(o));
}
}
if (dataSourceSequenceOrderConfig != null || tableSequenceOrderConfig != null)
if (isShardingDataSource&& dataSourceSequenceOrderConfig != null)
{
return new AppendOrderSequenceEnumeratorAsyncStreamMergeEngine<TShardingDbContext, TEntity>(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults);
}
else if (isShardingTable && tableSequenceOrderConfig != null)
{
return new AppendOrderSequenceEnumeratorAsyncStreamMergeEngine<TShardingDbContext, TEntity>(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults);
}
return null;
}
@ -151,9 +157,23 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
tableSequenceOrderConfig = orderCount == 1 ? GetPaginationFullMatch(virtualTable.PaginationMetadata.PaginationConfigs, primaryOrder) : GetPaginationPrimaryMatch(virtualTable.PaginationMetadata.PaginationConfigs, primaryOrder);
}
}
if (dataSourceSequenceOrderConfig != null || tableSequenceOrderConfig != null)
if (isShardingDataSource)
{
return new SequenceEnumeratorAsyncStreamMergeEngine<TShardingDbContext, TEntity>(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults, primaryOrder.IsAsc);
if (dataSourceSequenceOrderConfig != null)
{
return new SequenceEnumeratorAsyncStreamMergeEngine<TShardingDbContext, TEntity>(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults, primaryOrder.IsAsc);
}
}
else
{
if (isShardingTable)
{
if(tableSequenceOrderConfig != null)
{
return new SequenceEnumeratorAsyncStreamMergeEngine<TShardingDbContext, TEntity>(_streamMergeContext, dataSourceSequenceOrderConfig, tableSequenceOrderConfig, _shardingPageManager.Current.RouteQueryResults, primaryOrder.IsAsc);
}
}
}
var total = _shardingPageManager.Current.RouteQueryResults.Sum(o => o.QueryResult);
@ -207,7 +227,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
private PaginationSequenceConfig GetPaginationFullMatch(ISet<PaginationSequenceConfig> paginationSequenceConfigs, PropertyOrder primaryOrder)
{
return paginationSequenceConfigs.Where(o => !o.PaginationMatchEnum.HasFlag(PaginationMatchEnum.PrimaryMatch)).FirstOrDefault(o => PaginationPrimaryMatch(o, primaryOrder));
return paginationSequenceConfigs.FirstOrDefault(o => PaginationPrimaryMatch(o, primaryOrder));
}
private PaginationSequenceConfig GetPaginationPrimaryMatch(ISet<PaginationSequenceConfig> paginationSequenceConfigs, PropertyOrder primaryOrder)
{
@ -227,9 +247,10 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines
if (propertyOrder.PropertyExpression != paginationSequenceConfig.PropertyName)
return false;
if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner) && !paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Owner))
return typeof(TEntity) == paginationSequenceConfig.OrderPropertyInfo.DeclaringType;
if (paginationSequenceConfig.PaginationMatchEnum.HasFlag(PaginationMatchEnum.Named))
return propertyOrder.PropertyExpression == paginationSequenceConfig.PropertyName;
return false;
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace ShardingCore.Test2x.Domain.Entities
{
public class LogDay
{
public Guid Id { get; set; }
public string LogLevel { get; set; }
public string LogBody { get; set; }
public DateTime LogTime { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using ShardingCore.Core;
namespace ShardingCore.Test2x.Domain.Entities
{
public class Order:IShardingDataSource,IShardingTable
{
public Guid Id { get; set; }
[ShardingDataSourceKey]
public string Area { get; set; }
public long Money { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; }
}
}

View File

@ -1,6 +1,4 @@
using ShardingCore.Core;
namespace ShardingCore.Test50.Domain.Entities
namespace ShardingCore.Test2x.Domain.Entities
{
/*
* @Author: xjm
@ -9,12 +7,10 @@ namespace ShardingCore.Test50.Domain.Entities
* @Email: 326308290@qq.com
*/
public class SysUserMod
//:IShardingTable
{
/// <summary>
/// 用户Id用于分表
/// </summary>
//[ShardingTableKey(TableSeparator = "_")]
public string Id { get; set; }
/// <summary>
/// 用户名称

View File

@ -1,6 +1,4 @@
using ShardingCore.Core;
namespace ShardingCore.Test50_2x.Domain.Entities
namespace ShardingCore.Test2x.Domain.Entities
{
/*
* @Author: xjm
@ -8,14 +6,13 @@ namespace ShardingCore.Test50_2x.Domain.Entities
* @Date: Monday, 01 February 2021 15:43:22
* @Email: 326308290@qq.com
*/
public class SysUserSalary:IShardingTable
public class SysUserSalary
{
public string Id { get; set; }
public string UserId { get; set; }
/// <summary>
/// 每月的金额
/// </summary>
[ShardingTableKey]
public int DateOfMonth { get; set; }
/// <summary>
/// 工资

View File

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test2x.Domain.Entities;
namespace ShardingCore.Test2x.Domain.Maps
{
public class LogDayMap:IEntityTypeConfiguration<LogDay>
{
public void Configure(EntityTypeBuilder<LogDay> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.LogLevel).IsRequired().IsUnicode(false).HasMaxLength(32);
builder.Property(o => o.LogBody).IsRequired().HasMaxLength(256);
builder.ToTable(nameof(LogDay));
}
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test2x.Domain.Entities;
namespace ShardingCore.Test2x.Domain.Maps
{
public class OrderMap:IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Area).IsRequired().IsUnicode(false).HasMaxLength(20);
builder.ToTable(nameof(Order));
}
}
}

View File

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50.Domain.Entities;
using ShardingCore.Test2x.Domain.Entities;
namespace ShardingCore.Test50.Domain.Maps
namespace ShardingCore.Test2x.Domain.Maps
{
/*
* @Author: xjm

View File

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50.Domain.Entities;
using ShardingCore.Test2x.Domain.Entities;
namespace ShardingCore.Test50.Domain.Maps
namespace ShardingCore.Test2x.Domain.Maps
{
/*
* @Author: xjm

View File

@ -16,16 +16,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Content Include="Configs\DbConfig.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src2x\ShardingCore.2x\ShardingCore.2x.csproj" />
</ItemGroup>
</Project>
</Project>

View File

@ -1,15 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Test50.Domain.Maps;
using ShardingCore.Test2x.Domain.Maps;
namespace ShardingCore.Test50
namespace ShardingCore.Test2x
{
/*
* @Author: xjm
@ -29,6 +24,8 @@ namespace ShardingCore.Test50
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new SysUserModMap());
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
modelBuilder.ApplyConfiguration(new OrderMap());
modelBuilder.ApplyConfiguration(new LogDayMap());
}
public IRouteTail RouteTail { get; set; }

View File

@ -1,14 +1,23 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Extensions;
using ShardingCore.Test50.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Test2x.Domain.Entities;
using Xunit;
namespace ShardingCore.Test50
namespace ShardingCore.Test2x
{
/*
* @Author: xjm
@ -20,13 +29,87 @@ namespace ShardingCore.Test50
{
private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager;
private readonly IConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager;
private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager;
private readonly IShardingComparer<ShardingDefaultDbContext> _shardingComparer;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager;
public ShardingTest(ShardingDefaultDbContext virtualDbContext,IShardingRouteManager shardingRouteManager)
public ShardingTest(ShardingDefaultDbContext virtualDbContext,IShardingRouteManager shardingRouteManager, IConnectionStringManager<ShardingDefaultDbContext> connectionStringManager,IConfiguration configuration,
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IShardingComparer<ShardingDefaultDbContext> shardingComparer,IVirtualDataSource<ShardingDefaultDbContext> virtualDataSource,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager)
{
_virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager;
_connectionStringManager = connectionStringManager;
_configuration = configuration;
this._entityMetadataManager = entityMetadataManager;
_shardingComparer = shardingComparer;
_virtualDataSource = virtualDataSource;
_virtualTableManager = virtualTableManager;
}
[Fact]
public void GenericTest()
{
var a = new DefaultPhysicDataSource("aaa","aaa",true);
var b = new DefaultPhysicDataSource("aaa","aaa1",false);
Assert.Equal(a,b);
var x = new EntityMetadata(typeof(LogDay),"aa",typeof(ShardingDefaultDbContext),new List<PropertyInfo>());
var y = new EntityMetadata(typeof(LogDay),"aa1",typeof(ShardingDefaultDbContext),new List<PropertyInfo>());
Assert.Equal(x, y);
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 bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays);
Assert.Equal(100, bulkShardingTableEnumerable.Count);
var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays);
Assert.Equal(1, bulkShardingEnumerable.Count);
foreach (var (key, value) in bulkShardingEnumerable)
{
Assert.Equal(100, value.Count);
}
}
[Fact]
public void TestEntityMetadataManager()
{
var objMetadata0 = _entityMetadataManager.TryGet(typeof(object));
Assert.Null(objMetadata0);
var objMetadata1 = _entityMetadataManager.TryGet<object>();
Assert.Null(objMetadata1);
var objMetadata2 = _entityMetadataManager.TryGet(typeof(SysUserMod));
Assert.NotNull(objMetadata2);
var objMetadata3 = _entityMetadataManager.TryGet<SysUserMod>();
Assert.NotNull(objMetadata3);
var sysUserModIsShardingTable0 = _entityMetadataManager.IsShardingTable(typeof(SysUserMod));
Assert.True(sysUserModIsShardingTable0);
var sysUserModIsShardingTable1 = _entityMetadataManager.IsShardingTable<SysUserMod>();
Assert.True(sysUserModIsShardingTable1);
var sysUserModIsShardingDataSource0 = _entityMetadataManager.IsShardingDataSource(typeof(SysUserMod));
Assert.False(sysUserModIsShardingDataSource0);
var sysUserModIsShardingDataSource1 = _entityMetadataManager.IsShardingDataSource<SysUserMod>();
Assert.False(sysUserModIsShardingDataSource1);
}
[Fact]
public void TestShardingComparer()
{
var x = new Guid("7CDE28F8-D548-B96D-1C61-39FFE37AE492");
var y = new Guid("3425D899-291D-921B-DDE4-49FFE37AE493");
//asc y<x c# compare guid
var compare0 = x.CompareTo(y);
Assert.True(compare0>0);
//asc x<y db compare uniqueidentifier
var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0);
}
[Fact]
public void TestConnectionStringManager()
{
var connectionString = _connectionStringManager.GetConnectionString(_virtualDataSource.DefaultDataSourceName);
Assert.Equal(connectionString, "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;");
}
//[Fact]
//public async Task Route_TEST()
//{
@ -49,7 +132,7 @@ namespace ShardingCore.Test50
{
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.MustTable.TryAdd(typeof(SysUserMod), new HashSet<string>() { "00" });
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserMod>("00");
var mod00s = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(333, mod00s.Count);
@ -397,6 +480,122 @@ namespace ShardingCore.Test50
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
[Fact]
public async Task OrderCountTest()
{
var asyncCount = await _virtualDbContext.Set<Order>().CountAsync();
Assert.Equal(320, asyncCount);
var syncCount = _virtualDbContext.Set<Order>().Count();
Assert.Equal(320, syncCount);
var countA =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="A");
var countB =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="B");
var countC =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="C");
Assert.Equal(320, countA+ countB+ countC);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<Order>().Where(o=>o.CreateTime>=fourBegin&&o.CreateTime<fiveBegin).CountAsync();
Assert.Equal(30,fourCount);
}
[Fact]
public async Task OrderFirstTest()
{
var threeMonth = new DateTime(2021,3,1);
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o=>o.CreateTime== threeMonth);//µÚ59Ìõ 1ÔÂ31Ìì2ÔÂ28Ìì
Assert.NotNull(order);
Assert.Equal(59,order.Money);
Assert.Equal("C",order.Area);
}
[Fact]
public async Task OrderOrderTest()
{
var orders = await _virtualDbContext.Set<Order>().OrderBy(o => o.CreateTime).ToListAsync();
Assert.Equal(320,orders.Count);
var i = 0;
foreach (var order in orders)
{
Assert.Equal(i,order.Money);
i++;
}
var threeMonth = new DateTime(2021, 3, 1);
var orderPage = await _virtualDbContext.Set<Order>().Where(o=>o.CreateTime > threeMonth).OrderByDescending(o => o.CreateTime).ToShardingPageAsync(1,20);
Assert.Equal(20, orderPage.Data.Count);
Assert.Equal(260,orderPage.Total);
var j = 319;
foreach (var order in orderPage.Data)
{
Assert.Equal(j, order.Money);
j--;
}
}
[Fact]
public async Task LogDayCountTest()
{
var countAsync = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(3000,countAsync);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(300, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogDay>("20210102");
var countAsync1 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(10, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogDay>("20210103", "20210104");
var countAsync2 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(20, countAsync2);
}
}
[Fact]
public void LogDayTableSeparatorTest()
{
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName();
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(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable();
Assert.True(isShardingTable);
var isShardingDataSource = entityMetadata.IsShardingDataSource();
Assert.False(isShardingDataSource);
var emptySeparator = string.IsNullOrWhiteSpace(entityMetadata.TableSeparator);
Assert.True(emptySeparator);
Assert.Null(entityMetadata.AutoCreateDataSourceTable);
}
[Fact]
public async Task LogDayShardingPage()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var page= await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o=>o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(300, page.Total);
var page1 = _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPage(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total);
}
// [Fact]
// public async Task Group_API_Test()
// {

View File

@ -0,0 +1,35 @@
using System;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.Test2x.Shardings.PaginationConfigs;
using ShardingCore.VirtualRoutes.Days;
namespace ShardingCore.Test2x.Shardings
{
public class LogDayVirtualTableRoute:AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<LogDay>
{
protected override bool EnableHintRoute => true;
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 1);
}
public override bool StartJob()
{
return true;
}
public override void Configure(EntityMetadataTableBuilder<LogDay> builder)
{
builder.ShardingProperty(o => o.LogTime);
builder.TableSeparator(string.Empty);
}
public override IPaginationConfiguration<LogDay> CreatePaginationConfiguration()
{
return new LogDayPaginationConfiguration();
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions;
using ShardingCore.Test2x.Domain.Entities;
namespace ShardingCore.Test2x.Shardings
{
public class OrderAreaShardingVirtualDataSourceRoute:AbstractShardingOperatorVirtualDataSourceRoute<Order,string>
{
private readonly List<string> _dataSources = new List<string>()
{
"A", "B", "C"
};
protected override string ConvertToShardingKey(object shardingKey)
{
return shardingKey?.ToString() ?? string.Empty;
}
//我们设置区域就是数据库
public override string ShardingKeyToDataSourceName(object shardingKey)
{
return ConvertToShardingKey(shardingKey);
}
public override List<string> GetAllDataSourceNames()
{
return _dataSources;
}
public override bool AddDataSourceName(string dataSourceName)
{
if (_dataSources.Any(o => o == dataSourceName))
return false;
_dataSources.Add(dataSourceName);
return true;
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.VirtualRoutes.Months;
namespace ShardingCore.Test2x.Shardings
{
public class OrderCreateTimeVirtualTableRoute:AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<Order>
{
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 1);
}
public override bool StartJob()
{
return true;
}
public override IPaginationConfiguration<Order> CreatePaginationConfiguration()
{
return new OrderCreateTimePaginationConfiguration();
}
}
public class OrderCreateTimePaginationConfiguration : IPaginationConfiguration<Order>
{
public void Configure(PaginationBuilder<Order> builder)
{
builder.PaginationSequence(o => o.CreateTime)
.UseQueryMatch(PaginationMatchEnum.Owner | PaginationMatchEnum.Named | PaginationMatchEnum.PrimaryMatch)
.UseAppendIfOrderNone().UseRouteComparer(Comparer<string>.Default);
}
}
}

View File

@ -0,0 +1,13 @@
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test2x.Domain.Entities;
namespace ShardingCore.Test2x.Shardings.PaginationConfigs
{
public class LogDayPaginationConfiguration: IPaginationConfiguration<LogDay>
{
public void Configure(PaginationBuilder<LogDay> builder)
{
builder.PaginationSequence(o => o.LogTime).UseQueryMatch(PaginationMatchEnum.Named|PaginationMatchEnum.Owner|PaginationMatchEnum.PrimaryMatch);
}
}
}

View File

@ -1,8 +1,8 @@
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Test50.Domain.Entities;
using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test50.Shardings
namespace ShardingCore.Test2x.Shardings
{
/*
* @Author: xjm

View File

@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Test50_3x.Domain.Entities;
using ShardingCore.Test2x.Domain.Entities;
namespace ShardingCore.Test50_3x.Shardings
namespace ShardingCore.Test2x.Shardings
{
/*
* @Author: xjm
@ -71,5 +71,10 @@ namespace ShardingCore.Test50_3x.Shardings
}
}
}
public override void Configure(EntityMetadataTableBuilder<SysUserSalary> builder)
{
builder.ShardingProperty(o => o.DateOfMonth);
}
}
}

View File

@ -3,16 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ShardingCore.Bootstrapers;
using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Test50_3x.Domain.Entities;
using ShardingCore.Test50_3x.Shardings;
using ShardingCore.Test2x.Domain.Entities;
using ShardingCore.Test2x.Shardings;
namespace ShardingCore.Test50_3x
namespace ShardingCore.Test2x
{
/*
* @Author: xjm
@ -26,16 +24,6 @@ namespace ShardingCore.Test50_3x
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
// // 自定义 host 构建
public void ConfigureHost(IHostBuilder hostBuilder)
{
hostBuilder
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("Configs/DbConfig.json");
//builder.AddJsonFile("Configs/MacDbConfig.json");
});
}
// 支持的形式:
// ConfigureServices(IServiceCollection services)
@ -47,18 +35,42 @@ namespace ShardingCore.Test50_3x
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
.Begin(o =>
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.CreateShardingTableOnStart = false;
o.EnsureCreatedWithOutShardingTable = false;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
.AddDefaultDataSource("ds0", hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
.AddDefaultDataSource("A", "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;")
.AddShardingDataSource(sp =>
{
return new Dictionary<string, string>()
{
{ "B", "Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;" },
{ "C", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;" },
};
})
.AddShardingDataSourceRoute(o =>
{
o.AddShardingDatabaseRoute<OrderAreaShardingVirtualDataSourceRoute>();
})
.AddShardingTableRoute(op =>
{
op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
op.AddShardingTableRoute<OrderCreateTimeVirtualTableRoute>();
op.AddShardingTableRoute<LogDayVirtualTableRoute>();
}).End();
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// ,op =>
// {
// op.EnsureCreatedWithOutShardingTable = true;
// op.CreateShardingTableOnStart = true;
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// });
}
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
@ -117,10 +129,52 @@ namespace ShardingCore.Test50_3x
}
}
await virtualDbContext.AddRangeAsync(userMods);
await virtualDbContext.AddRangeAsync(userSalaries);
var areas = new List<string>(){"A","B","C"};
List<Order> orders = new List<Order>(360);
var begin = new DateTime(2021, 1, 1);
for (int i = 0; i < 320; i++)
{
orders.Add(new Order()
{
Id = Guid.NewGuid(),
Area = areas[i%3],
CreateTime = begin,
Money = i
});
begin = begin.AddDays(1);
}
await virtualDbContext.SaveChangesAsync();
List<LogDay> logDays = new List<LogDay>(3600);
var levels = new List<string>(){"info","warning","error"};
var begin1 = new DateTime(2021, 1, 1);
for (int i = 0; i < 300; i++)
{
var ltime = begin1;
for (int j = 0; j < 10; j++)
{
logDays.Add(new LogDay()
{
Id = Guid.NewGuid(),
LogLevel = levels[j%3],
LogBody = $"{i}_{j}",
LogTime = ltime.AddHours(1)
});
ltime = ltime.AddHours(1);
}
begin1 = begin1.AddDays(1);
}
using (var tran = virtualDbContext.Database.BeginTransaction())
{
await virtualDbContext.AddRangeAsync(userMods);
await virtualDbContext.AddRangeAsync(userSalaries);
await virtualDbContext.AddRangeAsync(orders);
await virtualDbContext.AddRangeAsync(logDays);
await virtualDbContext.SaveChangesAsync();
tran.Commit();
}
}
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace ShardingCore.Test3x.Domain.Entities
{
public class LogDay
{
public Guid Id { get; set; }
public string LogLevel { get; set; }
public string LogBody { get; set; }
public DateTime LogTime { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using ShardingCore.Core;
namespace ShardingCore.Test3x.Domain.Entities
{
public class Order:IShardingDataSource,IShardingTable
{
public Guid Id { get; set; }
[ShardingDataSourceKey]
public string Area { get; set; }
public long Money { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; }
}
}

View File

@ -1,6 +1,4 @@
using ShardingCore.Core;
namespace ShardingCore.Test50_2x.Domain.Entities
namespace ShardingCore.Test3x.Domain.Entities
{
/*
* @Author: xjm
@ -8,12 +6,11 @@ namespace ShardingCore.Test50_2x.Domain.Entities
* @Date: Thursday, 14 January 2021 15:36:43
* @Email: 326308290@qq.com
*/
public class SysUserMod:IShardingTable
public class SysUserMod
{
/// <summary>
/// 用户Id用于分表
/// </summary>
[ShardingTableKey(TableSeparator = "_")]
public string Id { get; set; }
/// <summary>
/// 用户名称

View File

@ -1,6 +1,4 @@
using ShardingCore.Core;
namespace ShardingCore.Test50_3x.Domain.Entities
namespace ShardingCore.Test3x.Domain.Entities
{
/*
* @Author: xjm
@ -8,14 +6,13 @@ namespace ShardingCore.Test50_3x.Domain.Entities
* @Date: Monday, 01 February 2021 15:43:22
* @Email: 326308290@qq.com
*/
public class SysUserSalary:IShardingTable
public class SysUserSalary
{
public string Id { get; set; }
public string UserId { get; set; }
/// <summary>
/// 每月的金额
/// </summary>
[ShardingTableKey]
public int DateOfMonth { get; set; }
/// <summary>
/// 工资

View File

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test3x.Domain.Entities;
namespace ShardingCore.Test3x.Domain.Maps
{
public class LogDayMap:IEntityTypeConfiguration<LogDay>
{
public void Configure(EntityTypeBuilder<LogDay> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.LogLevel).IsRequired().IsUnicode(false).HasMaxLength(32);
builder.Property(o => o.LogBody).IsRequired().HasMaxLength(256);
builder.ToTable(nameof(LogDay));
}
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test3x.Domain.Entities;
namespace ShardingCore.Test3x.Domain.Maps
{
public class OrderMap:IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Area).IsRequired().IsUnicode(false).HasMaxLength(20);
builder.ToTable(nameof(Order));
}
}
}

View File

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50_3x.Domain.Entities;
using ShardingCore.Test3x.Domain.Entities;
namespace ShardingCore.Test50_3x.Domain.Maps
namespace ShardingCore.Test3x.Domain.Maps
{
/*
* @Author: xjm

View File

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50_2x.Domain.Entities;
using ShardingCore.Test3x.Domain.Entities;
namespace ShardingCore.Test50_2x.Domain.Maps
namespace ShardingCore.Test3x.Domain.Maps
{
/*
* @Author: xjm

View File

@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
@ -15,17 +16,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Content Include="Configs\DbConfig.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<None Remove="Configs\MacDbConfig.json" />
<ProjectReference Include="..\..\src3x\ShardingCore.3x\ShardingCore.3x.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src3x\ShardingCore.3x\ShardingCore.3x.csproj" />
</ItemGroup>
</Project>

View File

@ -1,11 +1,10 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Test50_3x.Domain.Maps;
using ShardingCore.Test3x.Domain.Maps;
namespace ShardingCore.Test50_3x
namespace ShardingCore.Test3x
{
/*
* @Author: xjm
@ -25,6 +24,8 @@ namespace ShardingCore.Test50_3x
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new SysUserModMap());
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
modelBuilder.ApplyConfiguration(new OrderMap());
modelBuilder.ApplyConfiguration(new LogDayMap());
}
public IRouteTail RouteTail { get; set; }

View File

@ -1,12 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Test50_2x.Domain.Entities;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Test3x.Domain.Entities;
using Xunit;
namespace ShardingCore.Test50_2x
namespace ShardingCore.Test3x
{
/*
* @Author: xjm
@ -18,13 +29,87 @@ namespace ShardingCore.Test50_2x
{
private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager;
private readonly IConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager;
private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager;
private readonly IShardingComparer<ShardingDefaultDbContext> _shardingComparer;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager;
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager)
public ShardingTest(ShardingDefaultDbContext virtualDbContext,IShardingRouteManager shardingRouteManager, IConnectionStringManager<ShardingDefaultDbContext> connectionStringManager,IConfiguration configuration,
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IShardingComparer<ShardingDefaultDbContext> shardingComparer,IVirtualDataSource<ShardingDefaultDbContext> virtualDataSource,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager)
{
_virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager;
_connectionStringManager = connectionStringManager;
_configuration = configuration;
this._entityMetadataManager = entityMetadataManager;
_shardingComparer = shardingComparer;
_virtualDataSource = virtualDataSource;
_virtualTableManager = virtualTableManager;
}
[Fact]
public void GenericTest()
{
var a = new DefaultPhysicDataSource("aaa","aaa",true);
var b = new DefaultPhysicDataSource("aaa","aaa1",false);
Assert.Equal(a,b);
var x = new EntityMetadata(typeof(LogDay),"aa",typeof(ShardingDefaultDbContext),new List<PropertyInfo>());
var y = new EntityMetadata(typeof(LogDay),"aa1",typeof(ShardingDefaultDbContext),new List<PropertyInfo>());
Assert.Equal(x, y);
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 bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays);
Assert.Equal(100, bulkShardingTableEnumerable.Count);
var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays);
Assert.Equal(1, bulkShardingEnumerable.Count);
foreach (var (key, value) in bulkShardingEnumerable)
{
Assert.Equal(100, value.Count);
}
}
[Fact]
public void TestEntityMetadataManager()
{
var objMetadata0 = _entityMetadataManager.TryGet(typeof(object));
Assert.Null(objMetadata0);
var objMetadata1 = _entityMetadataManager.TryGet<object>();
Assert.Null(objMetadata1);
var objMetadata2 = _entityMetadataManager.TryGet(typeof(SysUserMod));
Assert.NotNull(objMetadata2);
var objMetadata3 = _entityMetadataManager.TryGet<SysUserMod>();
Assert.NotNull(objMetadata3);
var sysUserModIsShardingTable0 = _entityMetadataManager.IsShardingTable(typeof(SysUserMod));
Assert.True(sysUserModIsShardingTable0);
var sysUserModIsShardingTable1 = _entityMetadataManager.IsShardingTable<SysUserMod>();
Assert.True(sysUserModIsShardingTable1);
var sysUserModIsShardingDataSource0 = _entityMetadataManager.IsShardingDataSource(typeof(SysUserMod));
Assert.False(sysUserModIsShardingDataSource0);
var sysUserModIsShardingDataSource1 = _entityMetadataManager.IsShardingDataSource<SysUserMod>();
Assert.False(sysUserModIsShardingDataSource1);
}
[Fact]
public void TestShardingComparer()
{
var x = new Guid("7CDE28F8-D548-B96D-1C61-39FFE37AE492");
var y = new Guid("3425D899-291D-921B-DDE4-49FFE37AE493");
//asc y<x c# compare guid
var compare0 = x.CompareTo(y);
Assert.True(compare0>0);
//asc x<y db compare uniqueidentifier
var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0);
}
[Fact]
public void TestConnectionStringManager()
{
var connectionString = _connectionStringManager.GetConnectionString(_virtualDataSource.DefaultDataSourceName);
Assert.Equal(connectionString, "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;");
}
//[Fact]
//public async Task Route_TEST()
//{
@ -47,7 +132,7 @@ namespace ShardingCore.Test50_2x
{
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.MustTable.TryAdd(typeof(SysUserMod), new HashSet<string>() { "00" });
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserMod>("00");
var mod00s = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(333, mod00s.Count);
@ -122,7 +207,7 @@ namespace ShardingCore.Test50_2x
Name = u.Name
}).ToListAsync();
var list2 = list.OrderBy(o => o.Age).Select(o => o.Age).Distinct().ToList();
var list2 = list.OrderBy(o=>o.Age).Select(o=>o.Age).Distinct().ToList();
Assert.Equal(24000, list.Count());
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
@ -166,8 +251,8 @@ namespace ShardingCore.Test50_2x
[Fact]
public async Task ToList_Id_In_Test()
{
var ids = new[] { "1", "2", "3", "4" };
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToListAsync();
var ids = new[] {"1", "2", "3", "4"};
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => new List<string> { "1", "2", "3", "4" }.Contains(o.Id)).ToListAsync();
foreach (var id in ids)
{
Assert.Contains(sysUserMods, o => o.Id == id);
@ -179,8 +264,11 @@ namespace ShardingCore.Test50_2x
[Fact]
public async Task ToList_Id_Eq_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "3").ToListAsync();
var id= 3;
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == id.ToString()).ToListAsync();
Assert.Single(mods);
var mods1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "4").ToListAsync();
Assert.Single(mods1);
Assert.Equal("3", mods[0].Id);
}
@ -324,14 +412,14 @@ namespace ShardingCore.Test50_2x
public async Task Max_Join_Test()
{
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
});
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
});
var maxSalary = await queryable.MaxAsync(o => o.Salary);
Assert.Equal(1390000, maxSalary);
}
@ -365,25 +453,25 @@ namespace ShardingCore.Test50_2x
[Fact]
public async Task Group_Test()
{
var ids = new[] { "200", "300" };
var dateOfMonths = new[] { 202111, 202110 };
var ids = new[] {"200", "300"};
var dateOfMonths = new[] {202111, 202110};
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
}
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();
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);
@ -392,6 +480,122 @@ namespace ShardingCore.Test50_2x
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
[Fact]
public async Task OrderCountTest()
{
var asyncCount = await _virtualDbContext.Set<Order>().CountAsync();
Assert.Equal(320, asyncCount);
var syncCount = _virtualDbContext.Set<Order>().Count();
Assert.Equal(320, syncCount);
var countA =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="A");
var countB =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="B");
var countC =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="C");
Assert.Equal(320, countA+ countB+ countC);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<Order>().Where(o=>o.CreateTime>=fourBegin&&o.CreateTime<fiveBegin).CountAsync();
Assert.Equal(30,fourCount);
}
[Fact]
public async Task OrderFirstTest()
{
var threeMonth = new DateTime(2021,3,1);
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o=>o.CreateTime== threeMonth);//µÚ59Ìõ 1ÔÂ31Ìì2ÔÂ28Ìì
Assert.NotNull(order);
Assert.Equal(59,order.Money);
Assert.Equal("C",order.Area);
}
[Fact]
public async Task OrderOrderTest()
{
var orders = await _virtualDbContext.Set<Order>().OrderBy(o => o.CreateTime).ToListAsync();
Assert.Equal(320,orders.Count);
var i = 0;
foreach (var order in orders)
{
Assert.Equal(i,order.Money);
i++;
}
var threeMonth = new DateTime(2021, 3, 1);
var orderPage = await _virtualDbContext.Set<Order>().Where(o=>o.CreateTime > threeMonth).OrderByDescending(o => o.CreateTime).ToShardingPageAsync(1,20);
Assert.Equal(20, orderPage.Data.Count);
Assert.Equal(260,orderPage.Total);
var j = 319;
foreach (var order in orderPage.Data)
{
Assert.Equal(j, order.Money);
j--;
}
}
[Fact]
public async Task LogDayCountTest()
{
var countAsync = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(3000,countAsync);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(300, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogDay>("20210102");
var countAsync1 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(10, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogDay>("20210103", "20210104");
var countAsync2 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(20, countAsync2);
}
}
[Fact]
public void LogDayTableSeparatorTest()
{
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName();
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(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable();
Assert.True(isShardingTable);
var isShardingDataSource = entityMetadata.IsShardingDataSource();
Assert.False(isShardingDataSource);
var emptySeparator = string.IsNullOrWhiteSpace(entityMetadata.TableSeparator);
Assert.True(emptySeparator);
Assert.Null(entityMetadata.AutoCreateDataSourceTable);
}
[Fact]
public async Task LogDayShardingPage()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var page= await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o=>o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(300, page.Total);
var page1 = _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPage(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total);
}
// [Fact]
// public async Task Group_API_Test()
// {

View File

@ -0,0 +1,35 @@
using System;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test3x.Domain.Entities;
using ShardingCore.Test3x.Shardings.PaginationConfigs;
using ShardingCore.VirtualRoutes.Days;
namespace ShardingCore.Test3x.Shardings
{
public class LogDayVirtualTableRoute:AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<LogDay>
{
protected override bool EnableHintRoute => true;
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 1);
}
public override bool StartJob()
{
return true;
}
public override void Configure(EntityMetadataTableBuilder<LogDay> builder)
{
builder.ShardingProperty(o => o.LogTime);
builder.TableSeparator(string.Empty);
}
public override IPaginationConfiguration<LogDay> CreatePaginationConfiguration()
{
return new LogDayPaginationConfiguration();
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions;
using ShardingCore.Test3x.Domain.Entities;
namespace ShardingCore.Test3x.Shardings
{
public class OrderAreaShardingVirtualDataSourceRoute:AbstractShardingOperatorVirtualDataSourceRoute<Order,string>
{
private readonly List<string> _dataSources = new List<string>()
{
"A", "B", "C"
};
protected override string ConvertToShardingKey(object shardingKey)
{
return shardingKey?.ToString() ?? string.Empty;
}
//我们设置区域就是数据库
public override string ShardingKeyToDataSourceName(object shardingKey)
{
return ConvertToShardingKey(shardingKey);
}
public override List<string> GetAllDataSourceNames()
{
return _dataSources;
}
public override bool AddDataSourceName(string dataSourceName)
{
if (_dataSources.Any(o => o == dataSourceName))
return false;
_dataSources.Add(dataSourceName);
return true;
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test3x.Domain.Entities;
using ShardingCore.VirtualRoutes.Months;
namespace ShardingCore.Test3x.Shardings
{
public class OrderCreateTimeVirtualTableRoute:AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<Order>
{
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 1);
}
public override bool StartJob()
{
return true;
}
public override IPaginationConfiguration<Order> CreatePaginationConfiguration()
{
return new OrderCreateTimePaginationConfiguration();
}
}
public class OrderCreateTimePaginationConfiguration : IPaginationConfiguration<Order>
{
public void Configure(PaginationBuilder<Order> builder)
{
builder.PaginationSequence(o => o.CreateTime)
.UseQueryMatch(PaginationMatchEnum.Owner | PaginationMatchEnum.Named | PaginationMatchEnum.PrimaryMatch)
.UseAppendIfOrderNone().UseRouteComparer(Comparer<string>.Default);
}
}
}

View File

@ -0,0 +1,13 @@
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test3x.Domain.Entities;
namespace ShardingCore.Test3x.Shardings.PaginationConfigs
{
public class LogDayPaginationConfiguration: IPaginationConfiguration<LogDay>
{
public void Configure(PaginationBuilder<LogDay> builder)
{
builder.PaginationSequence(o => o.LogTime).UseQueryMatch(PaginationMatchEnum.Named|PaginationMatchEnum.Owner|PaginationMatchEnum.PrimaryMatch);
}
}
}

View File

@ -0,0 +1,27 @@
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Test3x.Domain.Entities;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test3x.Shardings
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
{
protected override bool EnableHintRoute => true;
public SysUserModVirtualTableRoute() : base(2,3)
{
}
public override void Configure(EntityMetadataTableBuilder<SysUserMod> builder)
{
builder.ShardingProperty(o => o.Id);
builder.TableSeparator("_");
}
}
}

View File

@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Test50.Domain.Entities;
using ShardingCore.Test3x.Domain.Entities;
namespace ShardingCore.Test50.Shardings
namespace ShardingCore.Test3x.Shardings
{
/*
* @Author: xjm
@ -71,5 +71,10 @@ namespace ShardingCore.Test50.Shardings
}
}
}
public override void Configure(EntityMetadataTableBuilder<SysUserSalary> builder)
{
builder.ShardingProperty(o => o.DateOfMonth);
}
}
}

View File

@ -3,18 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ShardingCore.Bootstrapers;
using ShardingCore.EFCores;
using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Test50_2x.Domain.Entities;
using ShardingCore.Test50_2x.Shardings;
using ShardingCore.Test3x.Domain.Entities;
using ShardingCore.Test3x.Shardings;
namespace ShardingCore.Test50_2x
namespace ShardingCore.Test3x
{
/*
* @Author: xjm
@ -28,16 +24,6 @@ namespace ShardingCore.Test50_2x
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
// // 自定义 host 构建
public void ConfigureHost(IHostBuilder hostBuilder)
{
hostBuilder
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("Configs/DbConfig.json");
//builder.AddJsonFile("Configs/MacDbConfig.json");
});
}
// 支持的形式:
// ConfigureServices(IServiceCollection services)
@ -45,24 +31,46 @@ namespace ShardingCore.Test50_2x
// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
{
//services.AddDbContext<DefaultDbContext>();
services.AddShardingDbContext<ShardingDefaultDbContext>((conn,o) =>
services.AddShardingDbContext<ShardingDefaultDbContext>((conn, o) =>
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
.Begin(o =>
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.CreateShardingTableOnStart = false;
o.EnsureCreatedWithOutShardingTable = false;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
.AddDefaultDataSource("ds0", hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
.AddDefaultDataSource("A", "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;")
.AddShardingDataSource(sp =>
{
return new Dictionary<string, string>()
{
{ "B", "Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;" },
{ "C", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;" },
};
})
.AddShardingDataSourceRoute(o =>
{
o.AddShardingDatabaseRoute<OrderAreaShardingVirtualDataSourceRoute>();
})
.AddShardingTableRoute(op =>
{
op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
op.AddShardingTableRoute<OrderCreateTimeVirtualTableRoute>();
op.AddShardingTableRoute<LogDayVirtualTableRoute>();
}).End();
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// ,op =>
// {
// op.EnsureCreatedWithOutShardingTable = true;
// op.CreateShardingTableOnStart = true;
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// });
}
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
@ -121,10 +129,52 @@ namespace ShardingCore.Test50_2x
}
}
await virtualDbContext.AddRangeAsync(userMods);
await virtualDbContext.AddRangeAsync(userSalaries);
var areas = new List<string>(){"A","B","C"};
List<Order> orders = new List<Order>(360);
var begin = new DateTime(2021, 1, 1);
for (int i = 0; i < 320; i++)
{
orders.Add(new Order()
{
Id = Guid.NewGuid(),
Area = areas[i%3],
CreateTime = begin,
Money = i
});
begin = begin.AddDays(1);
}
await virtualDbContext.SaveChangesAsync();
List<LogDay> logDays = new List<LogDay>(3600);
var levels = new List<string>(){"info","warning","error"};
var begin1 = new DateTime(2021, 1, 1);
for (int i = 0; i < 300; i++)
{
var ltime = begin1;
for (int j = 0; j < 10; j++)
{
logDays.Add(new LogDay()
{
Id = Guid.NewGuid(),
LogLevel = levels[j%3],
LogBody = $"{i}_{j}",
LogTime = ltime.AddHours(1)
});
ltime = ltime.AddHours(1);
}
begin1 = begin1.AddDays(1);
}
using (var tran = virtualDbContext.Database.BeginTransaction())
{
await virtualDbContext.AddRangeAsync(userMods);
await virtualDbContext.AddRangeAsync(userSalaries);
await virtualDbContext.AddRangeAsync(orders);
await virtualDbContext.AddRangeAsync(logDays);
await virtualDbContext.SaveChangesAsync();
tran.Commit();
}
}
}
}

View File

@ -1,6 +0,0 @@
{
"MySql": {
"ConnectionString": "server=127.0.0.1;port=3306;user=root;password=root;database=ShardingCoreDB;sslMode=None;"
}
}

View File

@ -1,31 +0,0 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Test50.MySql.Domain.Maps;
namespace ShardingCore.Test50.MySql
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/3/31 15:28:11
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public class DefaultDbContext : DbContext, IShardingTableDbContext
{
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new SysUserModMap());
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
}
public IRouteTail RouteTail { get; set; }
}
}

View File

@ -1,29 +0,0 @@
using ShardingCore.Core;
namespace ShardingCore.Test50.MySql.Domain.Entities
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 14 January 2021 15:36:43
* @Email: 326308290@qq.com
*/
public class SysUserMod:IShardingTable
{
/// <summary>
/// 用户Id用于分表
/// </summary>
[ShardingTableKey(TableSeparator = "_")]
public string Id { get; set; }
/// <summary>
/// 用户名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 用户姓名
/// </summary>
public int Age { get; set; }
public int AgeGroup { get; set; }
}
}

View File

@ -1,27 +0,0 @@
// using ShardingCore.Core;
//
// namespace ShardingCore.Test50.MySql.Domain.Entities
// {
// /*
// * @Author: xjm
// * @Description:
// * @Date: Wednesday, 20 January 2021 10:43:19
// * @Email: 326308290@qq.com
// */
// public class SysUserRange:IShardingTable
// {
// /// <summary>
// /// 分表分库range切分
// /// </summary>
// [ShardingKey(TableSeparator = "_",AutoCreateTableOnStart = true)]
// public string Id { get; set; }
// /// <summary>
// /// 姓名
// /// </summary>
// public string Name { get; set; }
// /// <summary>
// /// 年龄
// /// </summary>
// public int Age { get; set; }
// }
// }

View File

@ -1,44 +0,0 @@
using System;
using ShardingCore.Core;
namespace ShardingCore.Test50.MySql.Domain.Entities
{
/*
* @Author: xjm
* @Description:
* @Date: Monday, 01 February 2021 15:40:46
* @Email: 326308290@qq.com
*/
public class SysUserSalary:IShardingTable
{
public string Id { get; set; }
public string UserId { get; set; }
/// <summary>
/// 每月的金额
/// </summary>
[ShardingTableKey]
public int DateOfMonth { get; set; }
/// <summary>
/// 工资
/// </summary>
public int Salary { get; set; }
/// <summary>
/// 工资
/// </summary>
public long SalaryLong { get; set; }
/// <summary>
/// 工资
/// </summary>
public decimal SalaryDecimal { get; set; }
/// <summary>
/// 工资
/// </summary>
public double SalaryDouble { get; set; }
/// <summary>
/// 工资
/// </summary>
public float SalaryFloat { get; set; }
}
}

View File

@ -1,13 +0,0 @@
namespace ShardingCore.Test50.MySql.Domain.Entities
{
/*
* @Author: xjm
* @Description:
* @Date: Friday, 22 January 2021 14:17:29
* @Email: 326308290@qq.com
*/
public class UserGroup
{
}
}

View File

@ -1,23 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50.MySql.Domain.Entities;
namespace ShardingCore.Test50.MySql.Domain.Maps
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 14 January 2021 15:37:33
* @Email: 326308290@qq.com
*/
public class SysUserModMap:IEntityTypeConfiguration<SysUserMod>
{
public void Configure(EntityTypeBuilder<SysUserMod> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
builder.Property(o => o.Name).HasMaxLength(128);
builder.ToTable(nameof(SysUserMod));
}
}
}

View File

@ -1,23 +0,0 @@
// using Microsoft.EntityFrameworkCore;
// using Microsoft.EntityFrameworkCore.Metadata.Builders;
// using ShardingCore.Test50.MySql.Domain.Entities;
//
// namespace ShardingCore.Test50.MySql.Domain.Maps
// {
// /*
// * @Author: xjm
// * @Description:
// * @Date: Wednesday, 20 January 2021 10:45:47
// * @Email: 326308290@qq.com
// */
// public class SysUserRangeMap:IEntityTypeConfiguration<SysUserRange>
// {
// public void Configure(EntityTypeBuilder<SysUserRange> builder)
// {
// builder.HasKey(o => o.Id);
// builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
// builder.Property(o => o.Name).HasMaxLength(128);
// builder.ToTable(nameof(SysUserRange));
// }
// }
// }

View File

@ -1,24 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50.MySql.Domain.Entities;
namespace ShardingCore.Test50.MySql.Domain.Maps
{
/*
* @Author: xjm
* @Description:
* @Date: Monday, 01 February 2021 15:42:35
* @Email: 326308290@qq.com
*/
public class SysUserSalaryMap:IEntityTypeConfiguration<SysUserSalary>
{
public void Configure(EntityTypeBuilder<SysUserSalary> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
builder.Property(o => o.UserId).IsRequired().HasMaxLength(128);
builder.ToTable(nameof(SysUserSalary));
}
}
}

View File

@ -1,31 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PackageId>ShardingCore.Test50.MySql</PackageId>
<Product>ShardingCore.Test50.MySql</Product>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Xunit.DependencyInjection" Version="7.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Content Include="Configs\DbConfig.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src3x\ShardingCore.3x\ShardingCore.3x.csproj" />
</ItemGroup>
</Project>

View File

@ -1,353 +0,0 @@
// using System.Linq;
// using System.Threading.Tasks;
// using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
// using ShardingCore.DbContexts.VirtualDbContexts;
// using ShardingCore.Extensions;
// using ShardingCore.Test50.MySql.Domain.Entities;
// using Xunit;
//
// namespace ShardingCore.Test50.MySql
// {
// /*
// * @Author: xjm
// * @Description:
// * @Date: Friday, 15 January 2021 17:22:10
// * @Email: 326308290@qq.com
// */
// public class ShardingTest
// {
// private readonly DefaultDbContext _virtualDbContext;
// private readonly ITableRouteRuleEngineFactory _tableRouteRuleEngineFactory;
//
// public ShardingTest(DefaultDbContext virtualDbContext,ITableRouteRuleEngineFactory tableRouteRuleEngineFactory)
// {
// _virtualDbContext = virtualDbContext;
// _tableRouteRuleEngineFactory = tableRouteRuleEngineFactory;
// }
//
// //[Fact]
// //public async Task Route_TEST()
// //{
// // var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
// // var routeResults1 = _tableRouteRuleEngineFactory.Route(queryable1);
// // Assert.Equal(1,routeResults1.Count());
// // Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
// // Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
// // Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
// // var ids = new[] {"339", "124","142"};
// // var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
// // var routeResult2s = _tableRouteRuleEngineFactory.Route(queryable2);
// // Assert.Equal(2,routeResult2s.Count());
// // Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
// // Assert.Equal(2,routeResult2s.SelectMany(o=>o.ReplaceTables).Count());
// // Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail)));
// //}
//
// [Fact]
// public async Task ToList_All_Test()
// {
// var mods = await _virtualDbContext.Set<SysUserMod>().ToShardingListAsync();
// Assert.Equal(1000, mods.Count);
// }
//
// [Fact]
// public async Task ToList_Join_Test()
// {
// var list = await (from u in _virtualDbContext.Set<SysUserMod>()
// join salary in _virtualDbContext.Set<SysUserSalary>()
// on u.Id equals salary.UserId
// select new
// {
// Salary = salary.Salary,
// DateOfMonth = salary.DateOfMonth,
// Name = u.Name
// }).ToShardingListAsync();
// Assert.Equal(24000, list.Count());
// Assert.Equal(24, list.Count(o => o.Name == "name_200"));
//
//
// var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
// join salary in _virtualDbContext.Set<SysUserSalary>()
// on u.Id equals salary.UserId
// select new
// {
// Salary = salary.Salary,
// DateOfMonth = salary.DateOfMonth,
// Name = u.Name
// });
// var list1 = await queryable.ToShardingListAsync();
// Assert.Equal(24, list1.Count());
// Assert.DoesNotContain(list1, o => o.Name != "name_300");
// }
//
// [Fact]
// public async Task ToList_OrderBy_Asc_Desc_Test()
// {
// var modascs = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ToShardingListAsync();
// Assert.Equal(1000, modascs.Count);
// var i = 1;
// foreach (var age in modascs)
// {
// Assert.Equal(i, age.Age);
// i++;
// }
//
// var moddescs = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ToShardingListAsync();
// Assert.Equal(1000, moddescs.Count);
// var j = 1000;
// foreach (var age in moddescs)
// {
// Assert.Equal(j, age.Age);
// j--;
// }
// }
//
// [Fact]
// public async Task ToList_Id_In_Test()
// {
// var ids = new[] {"1", "2", "3", "4"};
// var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToShardingListAsync();
// foreach (var id in ids)
// {
// Assert.Contains(sysUserMods, o => o.Id == id);
// }
//
// Assert.DoesNotContain(sysUserMods, o => o.Age > 4);
// }
//
// [Fact]
// public async Task ToList_Id_Eq_Test()
// {
// var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "3").ToShardingListAsync();
// Assert.Single(mods);
// Assert.Equal("3", mods[0].Id);
// }
//
// [Fact]
// public async Task ToList_Id_Not_Eq_Test()
// {
// var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").ToShardingListAsync();
// Assert.Equal(999, mods.Count);
// Assert.DoesNotContain(mods, o => o.Id == "3");
// }
//
// [Fact]
// public async Task ToList_Id_Not_Eq_Skip_Test()
// {
// var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").OrderBy(o => o.Age).Skip(2).ToShardingListAsync();
// Assert.Equal(997, mods.Count);
// Assert.DoesNotContain(mods, o => o.Id == "3");
// Assert.Equal(4, mods[0].Age);
// Assert.Equal(5, mods[1].Age);
//
// var modsDesc = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "3").OrderByDescending(o => o.Age).Skip(13).ToShardingListAsync();
// Assert.Equal(986, modsDesc.Count);
// Assert.DoesNotContain(mods, o => o.Id == "3");
// Assert.Equal(987, modsDesc[0].Age);
// Assert.Equal(986, modsDesc[1].Age);
// }
//
// [Fact]
// public async Task ToList_Name_Eq_Test()
// {
// var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_3").ToShardingListAsync();
// Assert.Single(mods);
// Assert.Equal("3", mods[0].Id);
// }
//
// [Fact]
// public async Task ToList_Id_Eq_Not_In_Db_Test()
// {
// var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1001").ToShardingListAsync();
// Assert.Empty(mods);
// }
//
// [Fact]
// public async Task ToList_Name_Eq_Not_In_Db_Test()
// {
// var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1001").ToShardingListAsync();
// Assert.Empty(mods);
// }
//
// [Fact]
// public async Task FirstOrDefault_Order_By_Id_Test()
// {
// var sysUserModAge = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Age).ShardingFirstOrDefaultAsync();
// Assert.True(sysUserModAge != null && sysUserModAge.Id == "1");
// var sysUserModAgeDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Age).ShardingFirstOrDefaultAsync();
// Assert.True(sysUserModAgeDesc != null && sysUserModAgeDesc.Id == "1000");
// var sysUserMod = await _virtualDbContext.Set<SysUserMod>().OrderBy(o => o.Id).ShardingFirstOrDefaultAsync();
// Assert.True(sysUserMod != null && sysUserMod.Id == "1");
//
// var sysUserModDesc = await _virtualDbContext.Set<SysUserMod>().OrderByDescending(o => o.Id).ShardingFirstOrDefaultAsync();
// Assert.True(sysUserModDesc != null && sysUserModDesc.Id == "999");
// }
//
// [Fact]
// public async Task FirstOrDefault2()
// {
// var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "1").ShardingFirstOrDefaultAsync();
// Assert.NotNull(sysUserMod);
// Assert.True(sysUserMod.Id == "1");
// }
//
// [Fact]
// public async Task FirstOrDefault3()
// {
// var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_2").ShardingFirstOrDefaultAsync();
// Assert.NotNull(sysUserMod);
// Assert.Equal("2", sysUserMod.Id);
// }
//
// [Fact]
// public async Task FirstOrDefault4()
// {
// var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id != "1").ShardingFirstOrDefaultAsync();
// Assert.NotNull(sysUserMod);
// Assert.True(sysUserMod.Id != "1");
// }
//
// [Fact]
// public async Task FirstOrDefault5()
// {
// var sysUserMod = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1001").ShardingFirstOrDefaultAsync();
// Assert.Null(sysUserMod);
// }
//
// [Fact]
// public async Task Count_Test()
// {
// var a = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name == "name_1000").ShardingCountAsync();
// Assert.Equal(1, a);
// var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").ShardingCountAsync();
// Assert.Equal(999, b);
// }
//
// [Fact]
// public async Task Sum_Test()
// {
// var a = await _virtualDbContext.Set<SysUserMod>().ShardingSumAsync(o => o.Age);
// var expected = 0;
// for (int i = 1; i <= 1000; i++)
// {
// expected += i;
// }
//
// Assert.Equal(expected, a);
// var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").ShardingSumAsync(o => o.Age);
// Assert.Equal(expected - 1000, b);
// }
//
// [Fact]
// public async Task Max_Test()
// {
// var a = await _virtualDbContext.Set<SysUserMod>().ShardingMaxAsync(o => o.Age);
// Assert.Equal(1000, a);
// var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1000").ShardingMaxAsync(o => o.Age);
// Assert.Equal(999, b);
// var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age < 500).ShardingMaxAsync(o => o.Age);
// Assert.Equal(499, c);
// var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age <= 500).ShardingMaxAsync(o => o.Age);
// Assert.Equal(500, e);
// }
//
// [Fact]
// public async Task Max_Join_Test()
// {
// var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
// join salary in _virtualDbContext.Set<SysUserSalary>()
// on u.Id equals salary.UserId
// select new
// {
// Salary = salary.Salary,
// DateOfMonth = salary.DateOfMonth,
// Name = u.Name
// });
// var maxSalary = await queryable.ShardingMaxAsync(o => o.Salary);
// Assert.Equal(1390000, maxSalary);
// }
//
// [Fact]
// public async Task Min_Test()
// {
// var a = await _virtualDbContext.Set<SysUserMod>().ShardingMinAsync(o => o.Age);
// Assert.Equal(1, a);
// var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1").ShardingMinAsync(o => o.Age);
// Assert.Equal(2, b);
// var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age > 500).ShardingMinAsync(o => o.Age);
// Assert.Equal(501, c);
// var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age >= 500).ShardingMinAsync(o => o.Age);
// Assert.Equal(500, e);
// }
//
// [Fact]
// public async Task Any_Test()
// {
// var a = await _virtualDbContext.Set<SysUserMod>().ShardingAnyAsync(o => o.Age == 100);
// Assert.True(a);
// var b = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Name != "name_1").ShardingAnyAsync(o => o.Age == 1);
// Assert.False(b);
// var c = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age > 500).ShardingAnyAsync(o => o.Age <= 500);
// Assert.False(c);
// var e = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Age >= 500).ShardingAnyAsync(o => o.Age <= 500);
// Assert.True(e);
// }
//
// [Fact]
// public async Task Group_Test()
// {
// var ids = new[] {"200", "300"};
// var dateOfMonths = new[] {202111, 202110};
// 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)
// }).ToShardingListAsync();
// 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_API_Test()
// {
// var ids = new[] {"200", "300"};
// var dateOfMonths = new[] {202111, 202110};
// var group = await _virtualDbContext.Set<SysUserSalary>()
// .Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
// .ShardingGroupByAsync(g => new {UId = g.UserId}, g => 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)
// });
// 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);
// }
// }
// }

View File

@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Test50.MySql.Domain.Entities;
using ShardingCore.VirtualRoutes;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test50.MySql.Shardings
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
{
public SysUserModVirtualTableRoute() : base(2,3)
{
}
}
}

View File

@ -1,70 +0,0 @@
// using System;
// using System.Collections.Generic;
// using System.Linq.Expressions;
// using ShardingCore.Core.VirtualRoutes;
// using ShardingCore.Core.VirtualRoutes.Abstractions;
// using ShardingCore.Helpers;
// using ShardingCore.Test50.MySql.Domain.Entities;
//
// namespace ShardingCore.Test50.MySql.Shardings
// {
// /*
// * @Author: xjm
// * @Description:
// * @Date: Wednesday, 20 January 2021 10:46:37
// * @Email: 326308290@qq.com
// */
// public class SysUserRangeVirtualRoute: AbstractShardingOperatorVirtualTableRoute<SysUserRange, string>
// {
// private int _mod = 1000;
// protected override string ConvertToShardingKey(object shardingKey)
// {
// return shardingKey.ToString();
// }
//
// public override string ShardingKeyToTail(object shardingKey)
// {
// var shardingKeyStr = ConvertToShardingKey(shardingKey);
// var m = Math.Abs(ShardingCoreHelper.GetStringHashCode(shardingKeyStr) % _mod);
// if (m > 800)//801-999
// {
// return "3";
// } else if (m > 600)//601-800
// {
// return "2";
// } else if (m > 300)//301-600
// {
// return "1";
// } else //0-300
// {
// return "0";
// }
// }
//
// public override List<string> GetAllTails()
// {
// return new(){"0", "1","2","3"};
// }
//
// protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
// {
// var t = ShardingKeyToTail(shardingKey);
// switch (shardingOperator)
// {
// case ShardingOperatorEnum.Equal: return tail => tail == t;
// default:
// {
// Console.WriteLine($"shardingOperator is not equal scan all table tail");
// return tail => true;
// }
// }
// }
//
// // public override List<IPhysicTable> AfterPhysicTableFilter(List<IPhysicTable> allPhysicTables, List<IPhysicTable> filterPhysicTables)
// // {
// // if (filterPhysicTables.Count > 1)
// // throw new Exception($"query {nameof(SysUserRange)} not support cross table");
// // return base.AfterPhysicTableFilter(allPhysicTables, filterPhysicTables);
// // }
// }
// }

View File

@ -1,127 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ShardingCore.Bootstrapers;
using ShardingCore.Test50.MySql.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ShardingCore.Test50.MySql
{
/*
* @Author: xjm
* @Description:
* @Date: Friday, 15 January 2021 15:37:46
* @Email: 326308290@qq.com
*/
public class Startup
{
// // 自定义 host 构建
public void ConfigureHost(IHostBuilder hostBuilder)
{
hostBuilder
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("Configs/DbConfig.json");
});
}
// 支持的形式:
// ConfigureServices(IServiceCollection services)
// ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
{
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// ,op =>
// {
// op.EnsureCreatedWithOutShardingTable = true;
// op.CreateShardingTableOnStart = true;
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// });
}
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法
public void Configure(IServiceProvider serviceProvider)
{
var shardingBootstrapper = serviceProvider.GetService<IShardingBootstrapper>();
shardingBootstrapper.Start();
// 有一些测试数据要初始化可以放在这里
InitData(serviceProvider).GetAwaiter().GetResult();
}
/// <summary>
/// 添加种子数据
/// </summary>
/// <param name="serviceProvider"></param>
/// <returns></returns>
private async Task InitData(IServiceProvider serviceProvider)
{
using (var scope = serviceProvider.CreateScope())
{
var virtualDbContext = scope.ServiceProvider.GetService<DefaultDbContext>();
if (!await virtualDbContext.Set<SysUserMod>().AnyAsync(o => true))
{
var ids = Enumerable.Range(1, 1000);
var userMods = new List<SysUserMod>();
var userSalaries = new List<SysUserSalary>();
var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1);
foreach (var id in ids)
{
userMods.Add(new SysUserMod()
{
Id = id.ToString(),
Age = id,
Name = $"name_{id}",
AgeGroup=Math.Abs(id%10)
});
var tempTime = beginTime;
var i = 0;
while (tempTime<=endTime)
{
var dateOfMonth = $@"{tempTime:yyyyMM}";
userSalaries.Add(new SysUserSalary()
{
Id = $@"{id}{dateOfMonth}",
UserId = id.ToString(),
DateOfMonth = int.Parse(dateOfMonth),
Salary = 700000+id*100*i,
SalaryLong = 700000+id*100*i,
SalaryDecimal = (700000+id*100*i)/100m,
SalaryDouble = (700000+id*100*i)/100d,
SalaryFloat = (700000+id*100*i)/100f
});
tempTime=tempTime.AddMonths(1);
i++;
}
}
await virtualDbContext.AddRangeAsync(userMods);
await virtualDbContext.AddRangeAsync(userSalaries);
try
{
await virtualDbContext.SaveChangesAsync();
}
catch (Exception e)
{
throw e;
}
}
}
}
}
}

View File

@ -1,9 +0,0 @@
{
"SqlServer": {
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;"
},
"MySql": {
"ConnectionString": "server=127.0.0.1;port=3306;user=root;password=root;database=ShardingCoreDB;sslMode=None;"
}
}

View File

@ -1,33 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Xunit.DependencyInjection" Version="7.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Content Include="Configs\DbConfig.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<None Remove="Configs\MacDbConfig.json" />
<Content Include="Configs\MacDbConfig.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src5x\ShardingCore.5x\ShardingCore.5x.csproj" />
</ItemGroup>
</Project>

View File

@ -1,5 +0,0 @@
{
"SqlServer": {
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;"
}
}

View File

@ -1,20 +0,0 @@
using ShardingCore.Test50_2x.Domain.Entities;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test50_2x.Shardings
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
{
protected override bool EnableHintRoute => true;
public SysUserModVirtualTableRoute() : base(2,3)
{
}
}
}

View File

@ -1,75 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Test50_2x.Domain.Entities;
namespace ShardingCore.Test50_2x.Shardings
{
/*
* @Author: xjm
* @Description:
* @Date: Monday, 01 February 2021 15:54:55
* @Email: 326308290@qq.com
*/
public class SysUserSalaryVirtualTableRoute:AbstractShardingOperatorVirtualTableRoute<SysUserSalary,int>
{
protected override int ConvertToShardingKey(object shardingKey)
{
return Convert.ToInt32(shardingKey);
}
public override string ShardingKeyToTail(object shardingKey)
{
var time = ConvertToShardingKey(shardingKey);
return TimeFormatToTail(time);
}
public override List<string> GetAllTails()
{
var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1);
var list = new List<string>(24);
var tempTime = beginTime;
while (tempTime <= endTime)
{
list.Add($"{tempTime:yyyyMM}");
tempTime = tempTime.AddMonths(1);
}
return list;
}
protected string TimeFormatToTail(int time)
{
var dateOfMonth=DateTime.ParseExact($"{time}","yyyyMM",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.AdjustToUniversal);
return $"{dateOfMonth:yyyyMM}";
}
protected override Expression<Func<string, bool>> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
return tail => String.Compare(tail, t, StringComparison.Ordinal) < 0;
case ShardingOperatorEnum.LessThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
}
}

View File

@ -1,5 +0,0 @@
{
"SqlServer": {
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;"
}
}

View File

@ -1,20 +0,0 @@
using ShardingCore.Test50_3x.Domain.Entities;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test50_3x.Shardings
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
{
protected override bool EnableHintRoute => true;
public SysUserModVirtualTableRoute() : base(2,3)
{
}
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace ShardingCore.Test5x.Domain.Entities
{
public class LogDay
{
public Guid Id { get; set; }
public string LogLevel { get; set; }
public string LogBody { get; set; }
public DateTime LogTime { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using ShardingCore.Core;
namespace ShardingCore.Test5x.Domain.Entities
{
public class Order:IShardingDataSource,IShardingTable
{
public Guid Id { get; set; }
[ShardingDataSourceKey]
public string Area { get; set; }
public long Money { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; }
}
}

View File

@ -1,6 +1,4 @@
using ShardingCore.Core;
namespace ShardingCore.Test50_3x.Domain.Entities
namespace ShardingCore.Test5x.Domain.Entities
{
/*
* @Author: xjm
@ -8,12 +6,11 @@ namespace ShardingCore.Test50_3x.Domain.Entities
* @Date: Thursday, 14 January 2021 15:36:43
* @Email: 326308290@qq.com
*/
public class SysUserMod:IShardingTable
public class SysUserMod
{
/// <summary>
/// 用户Id用于分表
/// </summary>
[ShardingTableKey(TableSeparator = "_")]
public string Id { get; set; }
/// <summary>
/// 用户名称

View File

@ -1,7 +1,4 @@
using System;
using ShardingCore.Core;
namespace ShardingCore.Test50.Domain.Entities
namespace ShardingCore.Test5x.Domain.Entities
{
/*
* @Author: xjm
@ -9,14 +6,13 @@ namespace ShardingCore.Test50.Domain.Entities
* @Date: Monday, 01 February 2021 15:43:22
* @Email: 326308290@qq.com
*/
public class SysUserSalary:IShardingTable
public class SysUserSalary
{
public string Id { get; set; }
public string UserId { get; set; }
/// <summary>
/// 每月的金额
/// </summary>
[ShardingTableKey]
public int DateOfMonth { get; set; }
/// <summary>
/// 工资

View File

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test5x.Domain.Entities;
namespace ShardingCore.Test5x.Domain.Maps
{
public class LogDayMap:IEntityTypeConfiguration<LogDay>
{
public void Configure(EntityTypeBuilder<LogDay> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.LogLevel).IsRequired().IsUnicode(false).HasMaxLength(32);
builder.Property(o => o.LogBody).IsRequired().HasMaxLength(256);
builder.ToTable(nameof(LogDay));
}
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test5x.Domain.Entities;
namespace ShardingCore.Test5x.Domain.Maps
{
public class OrderMap:IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Area).IsRequired().IsUnicode(false).HasMaxLength(20);
builder.ToTable(nameof(Order));
}
}
}

View File

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50_2x.Domain.Entities;
using ShardingCore.Test5x.Domain.Entities;
namespace ShardingCore.Test50_2x.Domain.Maps
namespace ShardingCore.Test5x.Domain.Maps
{
/*
* @Author: xjm

View File

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ShardingCore.Test50_3x.Domain.Entities;
using ShardingCore.Test5x.Domain.Entities;
namespace ShardingCore.Test50_3x.Domain.Maps
namespace ShardingCore.Test5x.Domain.Maps
{
/*
* @Author: xjm

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Xunit.DependencyInjection" Version="7.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src5x\ShardingCore.5x\ShardingCore.5x.csproj" />
</ItemGroup>
</Project>

View File

@ -1,11 +1,10 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Test50_2x.Domain.Maps;
using ShardingCore.Test5x.Domain.Maps;
namespace ShardingCore.Test50_2x
namespace ShardingCore.Test5x
{
/*
* @Author: xjm
@ -25,6 +24,8 @@ namespace ShardingCore.Test50_2x
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new SysUserModMap());
modelBuilder.ApplyConfiguration(new SysUserSalaryMap());
modelBuilder.ApplyConfiguration(new OrderMap());
modelBuilder.ApplyConfiguration(new LogDayMap());
}
public IRouteTail RouteTail { get; set; }

View File

@ -1,12 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Test50_3x.Domain.Entities;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingPageExtensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
using ShardingCore.Test5x.Domain.Entities;
using Xunit;
namespace ShardingCore.Test50_3x
namespace ShardingCore.Test5x
{
/*
* @Author: xjm
@ -18,13 +29,87 @@ namespace ShardingCore.Test50_3x
{
private readonly ShardingDefaultDbContext _virtualDbContext;
private readonly IShardingRouteManager _shardingRouteManager;
private readonly IConnectionStringManager<ShardingDefaultDbContext> _connectionStringManager;
private readonly IConfiguration _configuration;
private readonly IEntityMetadataManager<ShardingDefaultDbContext> _entityMetadataManager;
private readonly IShardingComparer<ShardingDefaultDbContext> _shardingComparer;
private readonly IVirtualDataSource<ShardingDefaultDbContext> _virtualDataSource;
private readonly IVirtualTableManager<ShardingDefaultDbContext> _virtualTableManager;
public ShardingTest(ShardingDefaultDbContext virtualDbContext, IShardingRouteManager shardingRouteManager)
public ShardingTest(ShardingDefaultDbContext virtualDbContext,IShardingRouteManager shardingRouteManager, IConnectionStringManager<ShardingDefaultDbContext> connectionStringManager,IConfiguration configuration,
IEntityMetadataManager<ShardingDefaultDbContext> entityMetadataManager,
IShardingComparer<ShardingDefaultDbContext> shardingComparer,IVirtualDataSource<ShardingDefaultDbContext> virtualDataSource,
IVirtualTableManager<ShardingDefaultDbContext> virtualTableManager)
{
_virtualDbContext = virtualDbContext;
_shardingRouteManager = shardingRouteManager;
_connectionStringManager = connectionStringManager;
_configuration = configuration;
this._entityMetadataManager = entityMetadataManager;
_shardingComparer = shardingComparer;
_virtualDataSource = virtualDataSource;
_virtualTableManager = virtualTableManager;
}
[Fact]
public void GenericTest()
{
var a = new DefaultPhysicDataSource("aaa","aaa",true);
var b = new DefaultPhysicDataSource("aaa","aaa1",false);
Assert.Equal(a,b);
var x = new EntityMetadata(typeof(LogDay),"aa",typeof(ShardingDefaultDbContext),new List<PropertyInfo>());
var y = new EntityMetadata(typeof(LogDay),"aa1",typeof(ShardingDefaultDbContext),new List<PropertyInfo>());
Assert.Equal(x, y);
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 bulkShardingTableEnumerable = _virtualDbContext.BulkShardingTableEnumerable(logDays);
Assert.Equal(100, bulkShardingTableEnumerable.Count);
var bulkShardingEnumerable = _virtualDbContext.BulkShardingEnumerable(logDays);
Assert.Equal(1, bulkShardingEnumerable.Count);
foreach (var (key, value) in bulkShardingEnumerable)
{
Assert.Equal(100, value.Count);
}
}
[Fact]
public void TestEntityMetadataManager()
{
var objMetadata0 = _entityMetadataManager.TryGet(typeof(object));
Assert.Null(objMetadata0);
var objMetadata1 = _entityMetadataManager.TryGet<object>();
Assert.Null(objMetadata1);
var objMetadata2 = _entityMetadataManager.TryGet(typeof(SysUserMod));
Assert.NotNull(objMetadata2);
var objMetadata3 = _entityMetadataManager.TryGet<SysUserMod>();
Assert.NotNull(objMetadata3);
var sysUserModIsShardingTable0 = _entityMetadataManager.IsShardingTable(typeof(SysUserMod));
Assert.True(sysUserModIsShardingTable0);
var sysUserModIsShardingTable1 = _entityMetadataManager.IsShardingTable<SysUserMod>();
Assert.True(sysUserModIsShardingTable1);
var sysUserModIsShardingDataSource0 = _entityMetadataManager.IsShardingDataSource(typeof(SysUserMod));
Assert.False(sysUserModIsShardingDataSource0);
var sysUserModIsShardingDataSource1 = _entityMetadataManager.IsShardingDataSource<SysUserMod>();
Assert.False(sysUserModIsShardingDataSource1);
}
[Fact]
public void TestShardingComparer()
{
var x = new Guid("7CDE28F8-D548-B96D-1C61-39FFE37AE492");
var y = new Guid("3425D899-291D-921B-DDE4-49FFE37AE493");
//asc y<x c# compare guid
var compare0 = x.CompareTo(y);
Assert.True(compare0>0);
//asc x<y db compare uniqueidentifier
var compare1 = _shardingComparer.Compare(x, y, true);
Assert.True(compare1 < 0);
}
[Fact]
public void TestConnectionStringManager()
{
var connectionString = _connectionStringManager.GetConnectionString(_virtualDataSource.DefaultDataSourceName);
Assert.Equal(connectionString, "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;");
}
//[Fact]
//public async Task Route_TEST()
//{
@ -47,7 +132,7 @@ namespace ShardingCore.Test50_3x
{
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.MustTable.TryAdd(typeof(SysUserMod), new HashSet<string>() { "00" });
_shardingRouteManager.Current.TryCreateOrAddMustTail<SysUserMod>("00");
var mod00s = await _virtualDbContext.Set<SysUserMod>().ToListAsync();
Assert.Equal(333, mod00s.Count);
@ -122,7 +207,7 @@ namespace ShardingCore.Test50_3x
Name = u.Name
}).ToListAsync();
var list2 = list.OrderBy(o => o.Age).Select(o => o.Age).Distinct().ToList();
var list2 = list.OrderBy(o=>o.Age).Select(o=>o.Age).Distinct().ToList();
Assert.Equal(24000, list.Count());
Assert.Equal(24, list.Count(o => o.Name == "name_200"));
@ -166,8 +251,8 @@ namespace ShardingCore.Test50_3x
[Fact]
public async Task ToList_Id_In_Test()
{
var ids = new[] { "1", "2", "3", "4" };
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => ids.Contains(o.Id)).ToListAsync();
var ids = new[] {"1", "2", "3", "4"};
var sysUserMods = await _virtualDbContext.Set<SysUserMod>().Where(o => new List<string> { "1", "2", "3", "4" }.Contains(o.Id)).ToListAsync();
foreach (var id in ids)
{
Assert.Contains(sysUserMods, o => o.Id == id);
@ -179,8 +264,11 @@ namespace ShardingCore.Test50_3x
[Fact]
public async Task ToList_Id_Eq_Test()
{
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "3").ToListAsync();
var id= 3;
var mods = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == id.ToString()).ToListAsync();
Assert.Single(mods);
var mods1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "4").ToListAsync();
Assert.Single(mods1);
Assert.Equal("3", mods[0].Id);
}
@ -324,14 +412,14 @@ namespace ShardingCore.Test50_3x
public async Task Max_Join_Test()
{
var queryable = (from u in _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "300")
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
});
join salary in _virtualDbContext.Set<SysUserSalary>()
on u.Id equals salary.UserId
select new
{
Salary = salary.Salary,
DateOfMonth = salary.DateOfMonth,
Name = u.Name
});
var maxSalary = await queryable.MaxAsync(o => o.Salary);
Assert.Equal(1390000, maxSalary);
}
@ -365,25 +453,25 @@ namespace ShardingCore.Test50_3x
[Fact]
public async Task Group_Test()
{
var ids = new[] { "200", "300" };
var dateOfMonths = new[] { 202111, 202110 };
var ids = new[] {"200", "300"};
var dateOfMonths = new[] {202111, 202110};
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
}
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();
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);
@ -392,6 +480,122 @@ namespace ShardingCore.Test50_3x
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
[Fact]
public async Task OrderCountTest()
{
var asyncCount = await _virtualDbContext.Set<Order>().CountAsync();
Assert.Equal(320, asyncCount);
var syncCount = _virtualDbContext.Set<Order>().Count();
Assert.Equal(320, syncCount);
var countA =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="A");
var countB =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="B");
var countC =await _virtualDbContext.Set<Order>().CountAsync(o=>o.Area=="C");
Assert.Equal(320, countA+ countB+ countC);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<Order>().Where(o=>o.CreateTime>=fourBegin&&o.CreateTime<fiveBegin).CountAsync();
Assert.Equal(30,fourCount);
}
[Fact]
public async Task OrderFirstTest()
{
var threeMonth = new DateTime(2021,3,1);
var order = await _virtualDbContext.Set<Order>().FirstOrDefaultAsync(o=>o.CreateTime== threeMonth);//µÚ59Ìõ 1ÔÂ31Ìì2ÔÂ28Ìì
Assert.NotNull(order);
Assert.Equal(59,order.Money);
Assert.Equal("C",order.Area);
}
[Fact]
public async Task OrderOrderTest()
{
var orders = await _virtualDbContext.Set<Order>().OrderBy(o => o.CreateTime).ToListAsync();
Assert.Equal(320,orders.Count);
var i = 0;
foreach (var order in orders)
{
Assert.Equal(i,order.Money);
i++;
}
var threeMonth = new DateTime(2021, 3, 1);
var orderPage = await _virtualDbContext.Set<Order>().Where(o=>o.CreateTime > threeMonth).OrderByDescending(o => o.CreateTime).ToShardingPageAsync(1,20);
Assert.Equal(20, orderPage.Data.Count);
Assert.Equal(260,orderPage.Total);
var j = 319;
foreach (var order in orderPage.Data)
{
Assert.Equal(j, order.Money);
j--;
}
}
[Fact]
public async Task LogDayCountTest()
{
var countAsync = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(3000,countAsync);
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var fourCount = await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime < fiveBegin).CountAsync();
Assert.Equal(300, fourCount);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddMustTail<LogDay>("20210102");
var countAsync1 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(10, countAsync1);
}
Assert.Null(_shardingRouteManager.Current);
using (_shardingRouteManager.CreateScope())
{
_shardingRouteManager.Current.TryCreateOrAddHintTail<LogDay>("20210103", "20210104");
var countAsync2 = await _virtualDbContext.Set<LogDay>().CountAsync();
Assert.Equal(20, countAsync2);
}
}
[Fact]
public void LogDayTableSeparatorTest()
{
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(LogDay));
var virtualTableName = virtualTable.GetVirtualTableName();
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(all);
var entityMetadata = _entityMetadataManager.TryGet<LogDay>();
Assert.NotNull(entityMetadata);
var isShardingTable = entityMetadata.IsShardingTable();
Assert.True(isShardingTable);
var isShardingDataSource = entityMetadata.IsShardingDataSource();
Assert.False(isShardingDataSource);
var emptySeparator = string.IsNullOrWhiteSpace(entityMetadata.TableSeparator);
Assert.True(emptySeparator);
Assert.Null(entityMetadata.AutoCreateDataSourceTable);
}
[Fact]
public async Task LogDayShardingPage()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var page= await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o=>o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(300, page.Total);
var page1 = _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPage(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total);
}
// [Fact]
// public async Task Group_API_Test()
// {

View File

@ -0,0 +1,35 @@
using System;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test5x.Domain.Entities;
using ShardingCore.Test5x.Shardings.PaginationConfigs;
using ShardingCore.VirtualRoutes.Days;
namespace ShardingCore.Test5x.Shardings
{
public class LogDayVirtualTableRoute:AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<LogDay>
{
protected override bool EnableHintRoute => true;
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 1);
}
public override bool StartJob()
{
return true;
}
public override void Configure(EntityMetadataTableBuilder<LogDay> builder)
{
builder.ShardingProperty(o => o.LogTime);
builder.TableSeparator(string.Empty);
}
public override IPaginationConfiguration<LogDay> CreatePaginationConfiguration()
{
return new LogDayPaginationConfiguration();
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions;
using ShardingCore.Test5x.Domain.Entities;
namespace ShardingCore.Test5x.Shardings
{
public class OrderAreaShardingVirtualDataSourceRoute:AbstractShardingOperatorVirtualDataSourceRoute<Order,string>
{
private readonly List<string> _dataSources = new List<string>()
{
"A", "B", "C"
};
protected override string ConvertToShardingKey(object shardingKey)
{
return shardingKey?.ToString() ?? string.Empty;
}
//我们设置区域就是数据库
public override string ShardingKeyToDataSourceName(object shardingKey)
{
return ConvertToShardingKey(shardingKey);
}
public override List<string> GetAllDataSourceNames()
{
return _dataSources;
}
public override bool AddDataSourceName(string dataSourceName)
{
if (_dataSources.Any(o => o == dataSourceName))
return false;
_dataSources.Add(dataSourceName);
return true;
}
protected override Expression<Func<string, bool>> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var t = ShardingKeyToDataSourceName(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
return tail => true;
}
}
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test5x.Domain.Entities;
using ShardingCore.VirtualRoutes.Months;
namespace ShardingCore.Test5x.Shardings
{
public class OrderCreateTimeVirtualTableRoute:AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<Order>
{
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 1);
}
public override bool StartJob()
{
return true;
}
public override IPaginationConfiguration<Order> CreatePaginationConfiguration()
{
return new OrderCreateTimePaginationConfiguration();
}
}
public class OrderCreateTimePaginationConfiguration : IPaginationConfiguration<Order>
{
public void Configure(PaginationBuilder<Order> builder)
{
builder.PaginationSequence(o => o.CreateTime)
.UseQueryMatch(PaginationMatchEnum.Owner | PaginationMatchEnum.Named | PaginationMatchEnum.PrimaryMatch)
.UseAppendIfOrderNone().UseRouteComparer(Comparer<string>.Default);
}
}
}

View File

@ -0,0 +1,13 @@
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test5x.Domain.Entities;
namespace ShardingCore.Test5x.Shardings.PaginationConfigs
{
public class LogDayPaginationConfiguration: IPaginationConfiguration<LogDay>
{
public void Configure(PaginationBuilder<LogDay> builder)
{
builder.PaginationSequence(o => o.LogTime).UseQueryMatch(PaginationMatchEnum.Named|PaginationMatchEnum.Owner|PaginationMatchEnum.PrimaryMatch);
}
}
}

View File

@ -0,0 +1,27 @@
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Test5x.Domain.Entities;
using ShardingCore.VirtualRoutes.Mods;
namespace ShardingCore.Test5x.Shardings
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 14 January 2021 15:39:27
* @Email: 326308290@qq.com
*/
public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
{
protected override bool EnableHintRoute => true;
public SysUserModVirtualTableRoute() : base(2,3)
{
}
public override void Configure(EntityMetadataTableBuilder<SysUserMod> builder)
{
builder.ShardingProperty(o => o.Id);
builder.TableSeparator("_");
}
}
}

View File

@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Test50.MySql.Domain.Entities;
using ShardingCore.Test5x.Domain.Entities;
namespace ShardingCore.Test50.MySql.Shardings
namespace ShardingCore.Test5x.Shardings
{
/*
* @Author: xjm
@ -71,5 +71,10 @@ namespace ShardingCore.Test50.MySql.Shardings
}
}
}
public override void Configure(EntityMetadataTableBuilder<SysUserSalary> builder)
{
builder.ShardingProperty(o => o.DateOfMonth);
}
}
}

View File

@ -1,25 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ShardingCore.Test50.Domain.Entities;
using ShardingCore.Test50.Shardings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ShardingCore.Bootstrapers;
using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Test5x.Domain.Entities;
using ShardingCore.Test5x.Shardings;
#if EFCORE5SQLSERVER
using ShardingCore.SqlServer;
#endif
#if EFCORE5MYSQL
using ShardingCore.MySql;
#endif
namespace ShardingCore.Test50
namespace ShardingCore.Test5x
{
/*
* @Author: xjm
@ -33,16 +25,6 @@ namespace ShardingCore.Test50
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
// // 自定义 host 构建
public void ConfigureHost(IHostBuilder hostBuilder)
{
hostBuilder
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("Configs/DbConfig.json");
//builder.AddJsonFile("Configs/MacDbConfig.json");
});
}
// 支持的形式:
// ConfigureServices(IServiceCollection services)
@ -54,17 +36,31 @@ namespace ShardingCore.Test50
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
.Begin(o =>
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.CreateShardingTableOnStart = false;
o.EnsureCreatedWithOutShardingTable = false;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>
builder.UseSqlServer(connection).UseLoggerFactory(efLogger))
.AddDefaultDataSource("ds0",hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
.AddDefaultDataSource("A", "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;")
.AddShardingDataSource(sp =>
{
return new Dictionary<string, string>()
{
{ "B", "Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;" },
{ "C", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;" },
};
})
.AddShardingDataSourceRoute(o =>
{
o.AddShardingDatabaseRoute<OrderAreaShardingVirtualDataSourceRoute>();
})
.AddShardingTableRoute(op =>
{
op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
op.AddShardingTableRoute<OrderCreateTimeVirtualTableRoute>();
op.AddShardingTableRoute<LogDayVirtualTableRoute>();
}).End();
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// ,op =>
@ -134,10 +130,48 @@ namespace ShardingCore.Test50
}
}
var areas = new List<string>(){"A","B","C"};
List<Order> orders = new List<Order>(360);
var begin = new DateTime(2021, 1, 1);
for (int i = 0; i < 320; i++)
{
orders.Add(new Order()
{
Id = Guid.NewGuid(),
Area = areas[i%3],
CreateTime = begin,
Money = i
});
begin = begin.AddDays(1);
}
List<LogDay> logDays = new List<LogDay>(3600);
var levels = new List<string>(){"info","warning","error"};
var begin1 = new DateTime(2021, 1, 1);
for (int i = 0; i < 300; i++)
{
var ltime = begin1;
for (int j = 0; j < 10; j++)
{
logDays.Add(new LogDay()
{
Id = Guid.NewGuid(),
LogLevel = levels[j%3],
LogBody = $"{i}_{j}",
LogTime = ltime.AddHours(1)
});
ltime = ltime.AddHours(1);
}
begin1 = begin1.AddDays(1);
}
using (var tran = virtualDbContext.Database.BeginTransaction())
{
await virtualDbContext.AddRangeAsync(userMods);
await virtualDbContext.AddRangeAsync(userSalaries);
await virtualDbContext.AddRangeAsync(orders);
await virtualDbContext.AddRangeAsync(logDays);
await virtualDbContext.SaveChangesAsync();
tran.Commit();

View File

@ -1,9 +0,0 @@
{
"SqlServer": {
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDBxxxx;Integrated Security=True;"
},
"MySql": {
"ConnectionString": "server=127.0.0.1;port=3306;user=root;password=root;database=ShardingCoreDB;sslMode=None;"
}
}

View File

@ -4,19 +4,6 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="Configs\DbConfig.json" />
<None Remove="Configs\MacDbConfig.json" />
</ItemGroup>
<ItemGroup>
<Content Include="Configs\DbConfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />

View File

@ -580,6 +580,23 @@ namespace ShardingCore.Test6x
Assert.True(emptySeparator);
Assert.Null(entityMetadata.AutoCreateDataSourceTable);
}
[Fact]
public async Task LogDayShardingPage()
{
var fourBegin = new DateTime(2021, 4, 1).Date;
var fiveBegin = new DateTime(2021, 5, 1).Date;
var page= await _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o=>o.LogTime)
.ToShardingPageAsync(2, 10);
Assert.Equal(10, page.Data.Count);
Assert.Equal(300, page.Total);
var page1 = _virtualDbContext.Set<LogDay>().Where(o => o.LogTime >= fourBegin && o.LogTime <= fiveBegin).OrderBy(o => o.LogTime)
.ToShardingPage(2, 10);
Assert.Equal(10, page1.Data.Count);
Assert.Equal(300, page1.Total);
}
// [Fact]
// public async Task Group_API_Test()
// {

View File

@ -4,7 +4,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test6x.Domain.Entities;
using ShardingCore.Test6x.Shardings.PaginationConfigs;
using ShardingCore.VirtualRoutes.Days;
namespace ShardingCore.Test6x.Shardings
@ -28,5 +30,10 @@ namespace ShardingCore.Test6x.Shardings
builder.ShardingProperty(o => o.LogTime);
builder.TableSeparator(string.Empty);
}
public override IPaginationConfiguration<LogDay> CreatePaginationConfiguration()
{
return new LogDayPaginationConfiguration();
}
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.Test6x.Domain.Entities;
namespace ShardingCore.Test6x.Shardings.PaginationConfigs
{
public class LogDayPaginationConfiguration: IPaginationConfiguration<LogDay>
{
public void Configure(PaginationBuilder<LogDay> builder)
{
builder.PaginationSequence(o => o.LogTime).UseQueryMatch(PaginationMatchEnum.Named|PaginationMatchEnum.Owner|PaginationMatchEnum.PrimaryMatch);
}
}
}

View File

@ -26,16 +26,6 @@ namespace ShardingCore.Test6x
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
// // 自定义 host 构建
public void ConfigureHost(IHostBuilder hostBuilder)
{
hostBuilder
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("Configs/DbConfig.json");
//builder.AddJsonFile("Configs/MacDbConfig.json");
});
}
// 支持的形式:
// ConfigureServices(IServiceCollection services)
@ -47,8 +37,8 @@ namespace ShardingCore.Test6x
o.UseSqlServer(conn).UseLoggerFactory(efLogger))
.Begin(o =>
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.CreateShardingTableOnStart = false;
o.EnsureCreatedWithOutShardingTable = false;
o.AutoTrackEntity = true;
})
.AddShardingTransaction((connection, builder) =>