diff --git a/TYLDDB-CSharp.sln b/TYLDDB-CSharp.sln index bd725a6..b4cfc93 100644 --- a/TYLDDB-CSharp.sln +++ b/TYLDDB-CSharp.sln @@ -30,8 +30,8 @@ Global {FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Debug|Any CPU.Build.0 = Debug|Any CPU {FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Release|Any CPU.ActiveCfg = Release|Any CPU {FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Release|Any CPU.Build.0 = Release|Any CPU - {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.ActiveCfg = Release|Any CPU + {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Debug|Any CPU.Build.0 = Release|Any CPU {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Release|Any CPU.ActiveCfg = Release|Any CPU {F95CF6BC-2255-47F8-AAB3-B7812BB2894D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index 5f4d0b1..53c0096 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -1,157 +1,181 @@ using TimeRecord; using TYLDDB; -string dbFilePath = "./example.lddb"; -List testData = []; - -#region 实例化 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 实例化 -var lddb = new LDDB(); -#endregion - -#region 读取文件 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件 -HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间 -lddb.FilePath = dbFilePath; -readDbTimer.Start(); -lddb.ReadingFile(); -readDbTimer.Stop(); -WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds()); -#endregion - -#region 读取数据库 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库 -HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间 -loadDbTimer.Start(); -lddb.LoadDatabase("database1"); -Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 -loadDbTimer.Stop(); -WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V1): ", loadDbTimer.ElapsedMilliseconds()); - -HighPrecisionTimer loadDbV2Timer = new(); // 从发起读取数据库到成功返回读取内容的总时间 -loadDbV2Timer.Start(); -lddb.LoadDatabase_V2("database1"); -Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 -loadDbV2Timer.Stop(); -WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V2): ", loadDbV2Timer.ElapsedMilliseconds()); -#endregion - -#region 获取所有数据库名称 -///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称 -HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间 -readAllDbNameTimer.Start(); -lddb.ReadAllDatabaseName(); -readAllDbNameTimer.Stop(); -if(lddb.AllDatabaseName != null) +internal class Program { - foreach (var dbName in lddb.AllDatabaseName) + private static void Main() { - Console.WriteLine(dbName); + Test test = new(); + test.TestMethod(); } + } -WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); -#endregion - -///////////////////////////////////////////////////////////////////////////////////////////////////////// 数据库解析缓存 -HighPrecisionTimer parseDbTimer = new(); -parseDbTimer.Start(); -await lddb.Parse_V1(); -parseDbTimer.Stop(); -WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); -HighPrecisionTimer parseDbTimerAsync = new(); -parseDbTimerAsync.Start(); -await lddb.ParseAsync_V1(); -parseDbTimerAsync.Stop(); -WriteTime("解析文件并写入缓存V1(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); - -//HighPrecisionTimer parseDbV2Timer = new(); -//parseDbV2Timer.Start(); -//lddb.Parse_V2(); -//parseDbV2Timer.Stop(); -//WriteTime("解析文件并写入缓存V1(同步): ", parseDbV2Timer.ElapsedMilliseconds()); - -///////////////////////////////////////////////////////////////////////////////////////////////////////// 并发词典数据库全类型同步搜寻 -HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); -allTypeSearchFromConcurrentDictionaryTimer.Start(); -string[] AllTypeSearchFromConcurrentDictionaryResult = lddb.AllTypeSearchFromConcurrentDictionary("str_name"); -allTypeSearchFromConcurrentDictionaryTimer.Stop(); -// 使用 foreach 输出数组的每个元素 -foreach (var str in AllTypeSearchFromConcurrentDictionaryResult) -{ - Console.WriteLine(str); -} -WriteTime("并发词典数据库全类型同步搜寻: ", allTypeSearchFromConcurrentDictionaryTimer.ElapsedMilliseconds()); - -///////////////////////////////////////////////////////////////////////////////////////////////////////// 信号量线程锁词典数据库全类型同步搜寻 -HighPrecisionTimer allTypeSearchFromSemaphoreThreadLockTimer = new(); -allTypeSearchFromSemaphoreThreadLockTimer.Start(); -string[] AllTypeSearchFromSemaphoreThreadLockResult = lddb.AllTypeSearchFromSemaphoreThreadLock("str_name"); -allTypeSearchFromSemaphoreThreadLockTimer.Stop(); -// 使用 foreach 输出数组的每个元素 -foreach (var str in AllTypeSearchFromSemaphoreThreadLockResult) -{ - Console.WriteLine(str); -} -WriteTime("信号量线程锁词典数据库全类型同步搜寻: ", allTypeSearchFromSemaphoreThreadLockTimer.ElapsedMilliseconds()); - - - - - - - +class Test +{ + private readonly LDDB lddb = new(); + private readonly string dbFilePath = "./example.lddb"; + private readonly List testData = []; + public void TestMethod() + { + ReadFile(); + ReadDatabase(); + GetAllDatabaseName(); + ParseDatabaseToTemp(); + CdAllTypeSearch(); + StlAllTypeSearch(); + TripleDictionaryTest(); + ExportTestData(); + Console.ReadLine(); + } + #region Test Method + private void ReadFile() + { + #region 读取文件 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件 + HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间 + lddb.FilePath = dbFilePath; + readDbTimer.Start(); + lddb.ReadingFile(); + readDbTimer.Stop(); + WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds()); + #endregion + } + private void ReadDatabase() + { + #region 读取数据库 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库 + HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间 + loadDbTimer.Start(); + lddb.LoadDatabase("database1"); + Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 + loadDbTimer.Stop(); + WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V1): ", loadDbTimer.ElapsedMilliseconds()); + + HighPrecisionTimer loadDbV2Timer = new(); // 从发起读取数据库到成功返回读取内容的总时间 + loadDbV2Timer.Start(); + lddb.LoadDatabase_V2("database1"); + Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 + loadDbV2Timer.Stop(); + WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V2): ", loadDbV2Timer.ElapsedMilliseconds()); + #endregion + } + private void GetAllDatabaseName() + { + #region 获取所有数据库名称 + ///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称 + HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间 + readAllDbNameTimer.Start(); + lddb.ReadAllDatabaseName(); + readAllDbNameTimer.Stop(); + if (lddb.AllDatabaseName != null) + { + foreach (var dbName in lddb.AllDatabaseName) + { + Console.WriteLine(dbName); + } + } + WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); + #endregion + } + async void ParseDatabaseToTemp() + { + #region 数据库解析缓存 + HighPrecisionTimer parseDbTimer = new(); + parseDbTimer.Start(); + lddb.Parse_V1(); + parseDbTimer.Stop(); + WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); + HighPrecisionTimer parseDbTimerAsync = new(); + parseDbTimerAsync.Start(); + await lddb.ParseAsync_V1(); + parseDbTimerAsync.Stop(); + WriteTime("解析文件并写入缓存V1(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); + #endregion + } + private void CdAllTypeSearch() + { + #region 并发词典数据库全类型搜寻 + HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); + allTypeSearchFromConcurrentDictionaryTimer.Start(); + string[] AllTypeSearchFromConcurrentDictionaryResult = lddb.AllTypeSearchFromConcurrentDictionary("str_name"); + allTypeSearchFromConcurrentDictionaryTimer.Stop(); + // 使用 foreach 输出数组的每个元素 + foreach (var str in AllTypeSearchFromConcurrentDictionaryResult) + { + Console.WriteLine(str); + } + WriteTime("并发词典数据库全类型同步搜寻: ", allTypeSearchFromConcurrentDictionaryTimer.ElapsedMilliseconds()); + #endregion + } + private void StlAllTypeSearch() + { + #region 信号量线程锁词典数据库全类型搜寻 + HighPrecisionTimer allTypeSearchFromSemaphoreThreadLockTimer = new(); + allTypeSearchFromSemaphoreThreadLockTimer.Start(); + string[] AllTypeSearchFromSemaphoreThreadLockResult = lddb.AllTypeSearchFromSemaphoreThreadLock("str_name"); + allTypeSearchFromSemaphoreThreadLockTimer.Stop(); + // 使用 foreach 输出数组的每个元素 + foreach (var str in AllTypeSearchFromSemaphoreThreadLockResult) + { + Console.WriteLine(str); + } + WriteTime("信号量线程锁词典数据库全类型搜寻: ", allTypeSearchFromSemaphoreThreadLockTimer.ElapsedMilliseconds()); + #endregion + } -ExportTestData(); -Console.ReadLine(); + private void TripleDictionaryTest() + { + HighPrecisionTimer parse = new(); + parse.Start(); + lddb.TripleDictParse(); + parse.Stop(); + WriteTime("三值字典解析并写入(同步): ", parse.ElapsedMilliseconds()); + } + #endregion -////////////////////////////////////////////////////////////////////////////////////////////////////////// Test Method + #region 工具 + private void WriteTime(string what, double time) + { + string data = what + time + "ms\n"; + Console.WriteLine(data); + testData.Add(data); + Console.WriteLine(); + } -///////////////////////////////////////////////////////////////////////////////////////////////////////// 工具 -void WriteTime(string what, double time) -{ - string data = what + time + "ms\n"; - Console.WriteLine(data); - AddTestData(data); - Console.WriteLine(); -} + private void ExportTestData() + { + // 获取当前日期和时间,格式化为 "yyyy-MM-dd HH-mm" + string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm") + ".txt"; -void AddTestData(string data) -{ - testData.Add(data); -} + // 指定文件路径 + string directoryPath = "./testdata/"; + string filePath = Path.Combine(directoryPath, fileName); -void ExportTestData() -{ - // 获取当前日期和时间,格式化为 "yyyy-MM-dd HH-mm" - string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm") + ".txt"; + // 确保目录存在,如果不存在则创建 + if (!Directory.Exists(directoryPath)) + { + Directory.CreateDirectory(directoryPath); + } - // 指定文件路径 - string directoryPath = "./testdata/"; - string filePath = Path.Combine(directoryPath, fileName); + // 将 List 中的每一行写入文件 + File.WriteAllLines(filePath, testData); - // 确保目录存在,如果不存在则创建 - if (!Directory.Exists(directoryPath)) - { - Directory.CreateDirectory(directoryPath); + Console.WriteLine($"数据已成功写入文件: {filePath}"); } - - // 将 List 中的每一行写入文件 - File.WriteAllLines(filePath, testData); - - Console.WriteLine($"数据已成功写入文件: {filePath}"); + #endregion } diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index d78d8fa..fd0bc18 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -4,14 +4,14 @@ netstandard2.1;net6.0;net7.0;net8.0;net9.0;net6.0-windows;net7.0-windows;net8.0-windows;net9.0-windows True TYLDDB.NET - 1.0.0-alpha.4pre2 + 1.0.0-alpha.4 TYLDDB-Project LICENSE True icon.png https://github.com/TYLDDB/TYLDDB-CSharp git - ttypdb.qingyi-studio.top + https://tylddb.qingyi-studio.top BSD 3-Clause License (Modified) A strongly typed constrained distributed file system that can act as a database. True @@ -19,9 +19,9 @@ DataBase QingYi-Studio README.md - - Optimize error classes: Add more refined error handling and throwing. -- Allows you to customize the buffer size when reading files in blocks. -- Add a three-value dictionary (under test). + · Apply a three-value dictionary. +· Optimizing LDDB.Parse_V1() +· Add more instantiation overloads diff --git a/TYLDDB/TYLDDB_Enum.cs b/TYLDDB/TYLDDB_Enum.cs new file mode 100644 index 0000000..382a0e6 --- /dev/null +++ b/TYLDDB/TYLDDB_Enum.cs @@ -0,0 +1,53 @@ +namespace TYLDDB +{ + /// + /// Cache mode.
+ /// 缓存模式。 + ///
+ public enum CacheMode + { + /// + /// Use concurrent dictionaries to achieve high concurrency stability.
+ /// 使用并发词典来实现高并发的稳定性。 + ///
+ ConcurrentDictionary, + + /// + /// Use semaphore based thread locks to achieve high concurrency stability.
+ /// 使用基于信号量的线程锁来实现高并发的稳定性。 + ///
+ SemaphoreThreadLock, + +#if NET8_0_OR_GREATER + /// + /// High concurrency stability is achieved by using concurrent dictionary as core and semaphore based thread lock.
+ /// 以并发词典为核,结合基于信号量的线程锁实现高并发的稳定性。 + ///
+ TripleDictionaryCache, +#endif + + /// + /// + + /// + CDAndSTL, + } + + /// + /// Database pre-parsing process.
+ /// 数据库预解析流程。 + ///
+ public enum DatabaseProcess + { + /// + /// Based on regular expressions.
+ /// 基于正则表达式。 + ///
+ V1, + + /// + /// Parse database content directly based on string segmentation.
+ /// 直接基于字符串分割解析数据库内容。 + ///
+ V2, + } +} diff --git a/TYLDDB/TYLDDB_Instantiation.cs b/TYLDDB/TYLDDB_Instantiation.cs index 8b6abb1..c171071 100644 --- a/TYLDDB/TYLDDB_Instantiation.cs +++ b/TYLDDB/TYLDDB_Instantiation.cs @@ -2,6 +2,10 @@ using TYLDDB.Utils.FastCache.ConcurrentDictionary; using TYLDDB.Utils.FastCache.SemaphoreThreadLock; +#if NET8_0_OR_GREATER +using TYLDDB.Utils.FastCache.TDCache; +#endif + namespace TYLDDB { public partial class LDDB @@ -40,6 +44,214 @@ public LDDB() stlCharDictionary = new StlCharDictionary(); stlBooleanDictionary = new StlBooleanDictionary(); #endregion + + _cacheMode = CacheMode.CDAndSTL; + +#if NET8_0_OR_GREATER + tripleDictionaryCache = new TripleDictionaryCache(); +#endif + } + + /// + /// Instantiate the LDDB class
+ /// 实例化LDDB类 + ///
+ public LDDB(CacheMode mode) + { + #region 实例化数据库操作类 + database_v1 = new Database_V1(); + database_v2 = new Database_V2(); + #endregion + + switch (mode) + { + case CacheMode.ConcurrentDictionary: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + break; + case CacheMode.SemaphoreThreadLock: + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + break; + case CacheMode.CDAndSTL: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + break; +#if NET8_0_OR_GREATER + case CacheMode.TripleDictionaryCache: + tripleDictionaryCache = new TripleDictionaryCache(); + break; +#endif + } + } + + /// + /// Instantiate the LDDB class
+ /// 实例化LDDB类 + ///
+ public LDDB(DatabaseProcess process) + { + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + + _cacheMode = CacheMode.CDAndSTL; + +#if NET8_0_OR_GREATER + tripleDictionaryCache = new TripleDictionaryCache(); +#endif + + switch (process) + { + case DatabaseProcess.V1: + database_v1 = new Database_V1(); + break; + case DatabaseProcess.V2: + database_v2 = new Database_V2(); + break; + } + } + + /// + /// Instantiate the LDDB class
+ /// 实例化LDDB类 + ///
+ public LDDB(CacheMode mode, DatabaseProcess process) + { + switch (mode) + { + case CacheMode.ConcurrentDictionary: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + _cacheMode = mode; + break; + case CacheMode.SemaphoreThreadLock: + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + + _cacheMode = mode; + break; + case CacheMode.CDAndSTL: + #region 实例化并发词典 + cdStringDictionary = new CdStringDictionary(); + cdShortDictionary = new CdShortDictionary(); + cdLongDictionary = new CdLongDictionary(); + cdIntegerDictionary = new CdIntegerDictionary(); + cdFloatDictionary = new CdFloatDictionary(); + cdDoubleDictionary = new CdDoubleDictionary(); + cdDecimalDictionary = new CdDecimalDictionary(); + cdCharDictionary = new CdCharDictionary(); + cdBooleanDictionary = new CdBooleanDictionary(); + #endregion + + #region 实例化信号词典 + stlStringDictionary = new StlStringDictionary(); + stlShortDictionary = new StlShortDictionary(); + stlLongDictionary = new StlLongDictionary(); + stlIntegerDictionary = new StlIntegerDictionary(); + stlFloatDictionary = new StlFloatDictionary(); + stlDoubleDictionary = new StlDoubleDictionary(); + stlDecimalDictionary = new StlDecimalDictionary(); + stlCharDictionary = new StlCharDictionary(); + stlBooleanDictionary = new StlBooleanDictionary(); + #endregion + + _cacheMode = mode; + break; +#if NET8_0_OR_GREATER + case CacheMode.TripleDictionaryCache: + tripleDictionaryCache = new TripleDictionaryCache(); + _cacheMode = mode; + break; +#endif + } + + switch (process) + { + case DatabaseProcess.V1: + database_v1 = new Database_V1(); + break; + case DatabaseProcess.V2: + database_v2 = new Database_V2(); + break; + } } } } diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs index 876e2c6..1097f72 100644 --- a/TYLDDB/TYLDDB_Parse.cs +++ b/TYLDDB/TYLDDB_Parse.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using TYLDDB.Parser; +using System.Threading; namespace TYLDDB { @@ -9,11 +10,531 @@ public partial class LDDB /// Reparse the entire database.
/// 重新解析整个数据库。 /// - public async Task Parse_V1() + public void Parse_V1() + { + switch (_cacheMode) + { + case CacheMode.CDAndSTL: + { + #region method + void String() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlStringDictionary.Set(key, value); + cdStringDictionary.Set(key, value); + } + } + void Short() + { + var dict = DataParser_V1.ParseShort(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlShortDictionary.Set(key, value); + cdShortDictionary.Set(key, value); + } + } + void Long() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlLongDictionary.Set(key, value); + cdLongDictionary.Set(key, value); + } + } + void Int() + { + var dict = DataParser_V1.ParseInt(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlIntegerDictionary.Set(key, value); + cdIntegerDictionary.Set(key, value); + } + } + void Float() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlFloatDictionary.Set(key, value); + cdFloatDictionary.Set(key, value); + } + } + void Double() + { + var dict = DataParser_V1.ParseDouble(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDoubleDictionary.Set(key, value); + cdDoubleDictionary.Set(key, value); + } + } + void Decimal() + { + var dict = DataParser_V1.ParseDecimal(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDecimalDictionary.Set(key, value); + cdDecimalDictionary.Set(key, value); + } + } + void Char() + { + var dict = DataParser_V1.ParseChar(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlCharDictionary.Set(key, value); + cdCharDictionary.Set(key, value); + } + } + void Bool() + { + var dict = DataParser_V1.ParseBoolean(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlBooleanDictionary.Set(key, value); + cdBooleanDictionary.Set(key, value); + } + } + #endregion + + #region thread + Thread _str = new Thread(String); + Thread _short = new Thread(Short); + Thread _long = new Thread(Long); + Thread _float = new Thread(Float); + Thread _double = new Thread(Double); + Thread _decimal = new Thread(Decimal); + Thread _char = new Thread(Char); + Thread _int = new Thread(Int); + Thread _bool = new Thread(Bool); + + _str.Start(); + _short.Start(); + _long.Start(); + _float.Start(); + _double.Start(); + _decimal.Start(); + _char.Start(); + _int.Start(); + _bool.Start(); + + _str.Join(); + _short.Join(); + _long.Join(); + _float.Join(); + _double.Join(); + _decimal.Join(); + _char.Join(); + _int.Join(); + _bool.Join(); + break; + #endregion + } + + case CacheMode.SemaphoreThreadLock: + { + #region method + void String() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlStringDictionary.Set(key, value); + } + } + void Short() + { + var dict = DataParser_V1.ParseShort(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlShortDictionary.Set(key, value); + } + } + void Long() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlLongDictionary.Set(key, value); + } + } + void Int() + { + var dict = DataParser_V1.ParseInt(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlIntegerDictionary.Set(key, value); + } + } + void Float() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlFloatDictionary.Set(key, value); + } + } + void Double() + { + var dict = DataParser_V1.ParseDouble(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDoubleDictionary.Set(key, value); + } + } + void Decimal() + { + var dict = DataParser_V1.ParseDecimal(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlDecimalDictionary.Set(key, value); + } + } + void Char() + { + var dict = DataParser_V1.ParseChar(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlCharDictionary.Set(key, value); + } + } + void Bool() + { + var dict = DataParser_V1.ParseBoolean(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + stlBooleanDictionary.Set(key, value); + } + } + #endregion + + #region thread + Thread _str = new Thread(String); + Thread _short = new Thread(Short); + Thread _long = new Thread(Long); + Thread _float = new Thread(Float); + Thread _double = new Thread(Double); + Thread _decimal = new Thread(Decimal); + Thread _char = new Thread(Char); + Thread _int = new Thread(Int); + Thread _bool = new Thread(Bool); + + _str.Start(); + _short.Start(); + _long.Start(); + _float.Start(); + _double.Start(); + _decimal.Start(); + _char.Start(); + _int.Start(); + _bool.Start(); + + _str.Join(); + _short.Join(); + _long.Join(); + _float.Join(); + _double.Join(); + _decimal.Join(); + _char.Join(); + _int.Join(); + _bool.Join(); + break; + #endregion + } + + case CacheMode.ConcurrentDictionary: + { + #region method + void String() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdStringDictionary.Set(key, value); + } + } + void Short() + { + var dict = DataParser_V1.ParseShort(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdShortDictionary.Set(key, value); + } + } + void Long() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdLongDictionary.Set(key, value); + } + } + void Int() + { + var dict = DataParser_V1.ParseInt(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdIntegerDictionary.Set(key, value); + } + } + void Float() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdFloatDictionary.Set(key, value); + } + } + void Double() + { + var dict = DataParser_V1.ParseDouble(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdDoubleDictionary.Set(key, value); + } + } + void Decimal() + { + var dict = DataParser_V1.ParseDecimal(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdDecimalDictionary.Set(key, value); + } + } + void Char() + { + var dict = DataParser_V1.ParseChar(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdCharDictionary.Set(key, value); + } + } + void Bool() + { + var dict = DataParser_V1.ParseBoolean(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdBooleanDictionary.Set(key, value); + } + } + #endregion + + #region thread + Thread _str = new Thread(String); + Thread _short = new Thread(Short); + Thread _long = new Thread(Long); + Thread _float = new Thread(Float); + Thread _double = new Thread(Double); + Thread _decimal = new Thread(Decimal); + Thread _char = new Thread(Char); + Thread _int = new Thread(Int); + Thread _bool = new Thread(Bool); + + _str.Start(); + _short.Start(); + _long.Start(); + _float.Start(); + _double.Start(); + _decimal.Start(); + _char.Start(); + _int.Start(); + _bool.Start(); + + _str.Join(); + _short.Join(); + _long.Join(); + _float.Join(); + _double.Join(); + _decimal.Join(); + _char.Join(); + _int.Join(); + _bool.Join(); + break; + #endregion + } + +#if NET8_0_OR_GREATER + case CacheMode.TripleDictionaryCache: + TripleDictParse(); + break; +#endif + } + } + + /// + /// Reparse the entire database.
+ /// 重新解析整个数据库。 + ///
+ public async Task ParseAsync_V1() { // 创建多个任务,并使用 LongRunning 来确保每个任务在独立线程中运行 - #region 并发数据库任务 + // ConcurrentDictionary Task cdStringCacheTask = Task.Factory.StartNew(() => CdString(), TaskCreationOptions.LongRunning); Task cdIntCacheTask = Task.Factory.StartNew(() => CdInt(), TaskCreationOptions.LongRunning); Task cdShortCacheTask = Task.Factory.StartNew(() => CdShort(), TaskCreationOptions.LongRunning); @@ -23,9 +544,8 @@ public async Task Parse_V1() Task cdDecimalCacheTask = Task.Factory.StartNew(() => CdDecimal(), TaskCreationOptions.LongRunning); Task cdCharCacheTask = Task.Factory.StartNew(() => CdChar(), TaskCreationOptions.LongRunning); Task cdBoolCacheTask = Task.Factory.StartNew(() => CdBool(), TaskCreationOptions.LongRunning); - #endregion - #region 信号量数据库任务 + // SemaphoreThreadLock Task stlStringCacheTask = Task.Factory.StartNew(() => StlString(), TaskCreationOptions.LongRunning); Task stlIntCacheTask = Task.Factory.StartNew(() => StlInt(), TaskCreationOptions.LongRunning); Task stlShortCacheTask = Task.Factory.StartNew(() => StlShort(), TaskCreationOptions.LongRunning); @@ -35,7 +555,6 @@ public async Task Parse_V1() Task stlDecimalCacheTask = Task.Factory.StartNew(() => StlDecimal(), TaskCreationOptions.LongRunning); Task stlCharCacheTask = Task.Factory.StartNew(() => StlChar(), TaskCreationOptions.LongRunning); Task stlBoolCacheTask = Task.Factory.StartNew(() => StlBool(), TaskCreationOptions.LongRunning); - #endregion // 等待所有任务完成 await Task.WhenAll(cdStringCacheTask, @@ -58,7 +577,7 @@ await Task.WhenAll(cdStringCacheTask, stlBoolCacheTask); // ConcurrentDictionary - void CdString() + async void CdString() { var dict = DataParser_V1.ParseString(_databaseContent); @@ -69,10 +588,10 @@ void CdString() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdStringDictionary.Set(key, value); + await cdStringDictionary.SetAsync(key, value); } } - void CdShort() + async void CdShort() { var dict = DataParser_V1.ParseShort(_databaseContent); @@ -83,10 +602,10 @@ void CdShort() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdShortDictionary.Set(key, value); + await cdShortDictionary.SetAsync(key, value); } } - void CdLong() + async void CdLong() { var dict = DataParser_V1.ParseLong(_databaseContent); @@ -97,10 +616,10 @@ void CdLong() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdLongDictionary.Set(key, value); + await cdLongDictionary.SetAsync(key, value); } } - void CdInt() + async void CdInt() { var dict = DataParser_V1.ParseInt(_databaseContent); @@ -111,10 +630,10 @@ void CdInt() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdIntegerDictionary.Set(key, value); + await cdIntegerDictionary.SetAsync(key, value); } } - void CdFloat() + async void CdFloat() { var dict = DataParser_V1.ParseFloat(_databaseContent); @@ -125,10 +644,10 @@ void CdFloat() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdFloatDictionary.Set(key, value); + await cdFloatDictionary.SetAsync(key, value); } } - void CdDouble() + async void CdDouble() { var dict = DataParser_V1.ParseDouble(_databaseContent); @@ -139,10 +658,10 @@ void CdDouble() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdDoubleDictionary.Set(key, value); + await cdDoubleDictionary.SetAsync(key, value); } } - void CdDecimal() + async void CdDecimal() { var dict = DataParser_V1.ParseDecimal(_databaseContent); @@ -153,10 +672,10 @@ void CdDecimal() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdDecimalDictionary.Set(key, value); + await cdDecimalDictionary.SetAsync(key, value); } } - void CdChar() + async void CdChar() { var dict = DataParser_V1.ParseChar(_databaseContent); @@ -167,10 +686,10 @@ void CdChar() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdCharDictionary.Set(key, value); + await cdCharDictionary.SetAsync(key, value); } } - void CdBool() + async void CdBool() { var dict = DataParser_V1.ParseBoolean(_databaseContent); @@ -181,12 +700,12 @@ void CdBool() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - cdBooleanDictionary.Set(key, value); + await cdBooleanDictionary.SetAsync(key, value); } } // SemaphoreThreadLock - void StlString() + async void StlString() { var dict = DataParser_V1.ParseString(_databaseContent); @@ -197,10 +716,10 @@ void StlString() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlStringDictionary.Set(key, value); + await stlStringDictionary.SetAsync(key, value); } } - void StlShort() + async void StlShort() { var dict = DataParser_V1.ParseShort(_databaseContent); @@ -211,10 +730,10 @@ void StlShort() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlShortDictionary.Set(key, value); + await stlShortDictionary.SetAsync(key, value); } } - void StlLong() + async void StlLong() { var dict = DataParser_V1.ParseLong(_databaseContent); @@ -225,10 +744,10 @@ void StlLong() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlLongDictionary.Set(key, value); + await stlLongDictionary.SetAsync(key, value); } } - void StlInt() + async void StlInt() { var dict = DataParser_V1.ParseInt(_databaseContent); @@ -239,10 +758,10 @@ void StlInt() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlIntegerDictionary.Set(key, value); + await stlIntegerDictionary.SetAsync(key, value); } } - void StlFloat() + async void StlFloat() { var dict = DataParser_V1.ParseFloat(_databaseContent); @@ -253,10 +772,10 @@ void StlFloat() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlFloatDictionary.Set(key, value); + await stlFloatDictionary.SetAsync(key, value); } } - void StlDouble() + async void StlDouble() { var dict = DataParser_V1.ParseDouble(_databaseContent); @@ -267,10 +786,10 @@ void StlDouble() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlDoubleDictionary.Set(key, value); + await stlDoubleDictionary.SetAsync(key, value); } } - void StlDecimal() + async void StlDecimal() { var dict = DataParser_V1.ParseDecimal(_databaseContent); @@ -281,10 +800,10 @@ void StlDecimal() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlDecimalDictionary.Set(key, value); + await stlDecimalDictionary.SetAsync(key, value); } } - void StlChar() + async void StlChar() { var dict = DataParser_V1.ParseChar(_databaseContent); @@ -295,10 +814,10 @@ void StlChar() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlCharDictionary.Set(key, value); + await stlCharDictionary.SetAsync(key, value); } } - void StlBool() + async void StlBool() { var dict = DataParser_V1.ParseBoolean(_databaseContent); @@ -309,316 +828,186 @@ void StlBool() var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - stlBooleanDictionary.Set(key, value); + await stlBooleanDictionary.SetAsync(key, value); } } } +#if NET8_0_OR_GREATER + // 三值字典操作 /// - /// Reparse the entire database.
- /// 重新解析整个数据库。 + /// Parses the database and writes to the three-value dictionary cache.
+ /// 解析数据库并写入三值字典缓存。 ///
- public async Task ParseAsync_V1() + public void TripleDictParse() { - // 创建多个任务,并使用 LongRunning 来确保每个任务在独立线程中运行 - - // ConcurrentDictionary - Task cdStringCacheTask = Task.Factory.StartNew(() => CdString(), TaskCreationOptions.LongRunning); - Task cdIntCacheTask = Task.Factory.StartNew(() => CdInt(), TaskCreationOptions.LongRunning); - Task cdShortCacheTask = Task.Factory.StartNew(() => CdShort(), TaskCreationOptions.LongRunning); - Task cdLongCacheTask = Task.Factory.StartNew(() => CdLong(), TaskCreationOptions.LongRunning); - Task cdFloatCacheTask = Task.Factory.StartNew(() => CdFloat(), TaskCreationOptions.LongRunning); - Task cdDoubleCacheTask = Task.Factory.StartNew(() => CdDouble(), TaskCreationOptions.LongRunning); - Task cdDecimalCacheTask = Task.Factory.StartNew(() => CdDecimal(), TaskCreationOptions.LongRunning); - Task cdCharCacheTask = Task.Factory.StartNew(() => CdChar(), TaskCreationOptions.LongRunning); - Task cdBoolCacheTask = Task.Factory.StartNew(() => CdBool(), TaskCreationOptions.LongRunning); - - // SemaphoreThreadLock - Task stlStringCacheTask = Task.Factory.StartNew(() => StlString(), TaskCreationOptions.LongRunning); - Task stlIntCacheTask = Task.Factory.StartNew(() => StlInt(), TaskCreationOptions.LongRunning); - Task stlShortCacheTask = Task.Factory.StartNew(() => StlShort(), TaskCreationOptions.LongRunning); - Task stlLongCacheTask = Task.Factory.StartNew(() => StlLong(), TaskCreationOptions.LongRunning); - Task stlFloatCacheTask = Task.Factory.StartNew(() => StlFloat(), TaskCreationOptions.LongRunning); - Task stlDoubleCacheTask = Task.Factory.StartNew(() => StlDouble(), TaskCreationOptions.LongRunning); - Task stlDecimalCacheTask = Task.Factory.StartNew(() => StlDecimal(), TaskCreationOptions.LongRunning); - Task stlCharCacheTask = Task.Factory.StartNew(() => StlChar(), TaskCreationOptions.LongRunning); - Task stlBoolCacheTask = Task.Factory.StartNew(() => StlBool(), TaskCreationOptions.LongRunning); - - // 等待所有任务完成 - await Task.WhenAll(cdStringCacheTask, - cdIntCacheTask, - cdShortCacheTask, - cdLongCacheTask, - cdFloatCacheTask, - cdDoubleCacheTask, - cdDecimalCacheTask, - cdCharCacheTask, - cdBoolCacheTask, - stlStringCacheTask, - stlIntCacheTask, - stlShortCacheTask, - stlLongCacheTask, - stlFloatCacheTask, - stlDoubleCacheTask, - stlDecimalCacheTask, - stlCharCacheTask, - stlBoolCacheTask); + #region 各数据类型字典 + var _str = DataParser_V1.ParseString(_databaseContent); + var _short = DataParser_V1.ParseShort(_databaseContent); + var _long = DataParser_V1.ParseLong(_databaseContent); + var _int = DataParser_V1.ParseInt(_databaseContent); + var _float = DataParser_V1.ParseFloat(_databaseContent); + var _double = DataParser_V1.ParseDouble(_databaseContent); + var _decimal = DataParser_V1.ParseDecimal(_databaseContent); + var _char = DataParser_V1.ParseChar(_databaseContent); + var _bool = DataParser_V1.ParseBoolean(_databaseContent); + #endregion - // ConcurrentDictionary - async void CdString() + #region 方法 + void Str() { - var dict = DataParser_V1.ParseString(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _str) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await cdStringDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("string", key, value); } } - async void CdShort() - { - var dict = DataParser_V1.ParseShort(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdShortDictionary.SetAsync(key, value); - } - } - async void CdLong() + void Short() { - var dict = DataParser_V1.ParseLong(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _short) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await cdLongDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("short", key, value); } } - async void CdInt() - { - var dict = DataParser_V1.ParseInt(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - // 将 key-value 对存储到缓存 - await cdIntegerDictionary.SetAsync(key, value); - } - } - async void CdFloat() + void Long() { - var dict = DataParser_V1.ParseFloat(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _long) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await cdFloatDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("long", key, value); } } - async void CdDouble() - { - var dict = DataParser_V1.ParseDouble(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdDoubleDictionary.SetAsync(key, value); - } - } - async void CdDecimal() + void Int() { - var dict = DataParser_V1.ParseDecimal(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _int) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await cdDecimalDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("int", key, value); } } - async void CdChar() - { - var dict = DataParser_V1.ParseChar(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdCharDictionary.SetAsync(key, value); - } - } - async void CdBool() + void Float() { - var dict = DataParser_V1.ParseBoolean(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _float) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await cdBooleanDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("float", key, value); } } - // SemaphoreThreadLock - async void StlString() + void Double() { - var dict = DataParser_V1.ParseString(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _double) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await stlStringDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("double", key, value); } } - async void StlShort() - { - var dict = DataParser_V1.ParseShort(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlShortDictionary.SetAsync(key, value); - } - } - async void StlLong() + void Decimal() { - var dict = DataParser_V1.ParseLong(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _decimal) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await stlLongDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("decimal", key, value); } } - async void StlInt() - { - var dict = DataParser_V1.ParseInt(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlIntegerDictionary.SetAsync(key, value); - } - } - async void StlFloat() + void Char() { - var dict = DataParser_V1.ParseFloat(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _char) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await stlFloatDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("char", key, value); } } - async void StlDouble() - { - var dict = DataParser_V1.ParseDouble(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlDoubleDictionary.SetAsync(key, value); - } - } - async void StlDecimal() - { - var dict = DataParser_V1.ParseDecimal(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlDecimalDictionary.SetAsync(key, value); - } - } - async void StlChar() + void Bool() { - var dict = DataParser_V1.ParseChar(_databaseContent); - // 遍历 dict 中的每一项 - foreach (var kvp in dict) + foreach (var kvp in _bool) { var key = kvp.Key; // 获取第一个值 (key) var value = kvp.Value; // 获取第二个值 (value) // 将 key-value 对存储到缓存 - await stlCharDictionary.SetAsync(key, value); + tripleDictionaryCache.Set("bool", key, value); } } - async void StlBool() - { - var dict = DataParser_V1.ParseBoolean(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) + #endregion - // 将 key-value 对存储到缓存 - await stlBooleanDictionary.SetAsync(key, value); - } - } + #region 线程 + #region 创建线程实例 + Thread strThread = new(Str); + Thread shortThread = new(Short); + Thread longThread = new(Long); + Thread floatThread = new(Float); + Thread doubleThread = new(Double); + Thread decimalThread = new(Decimal); + Thread charThread = new(Char); + Thread intThread = new(Int); + Thread boolThread = new(Bool); + #endregion + #region 启动线程 + strThread.Start(); + shortThread.Start(); + longThread.Start(); + floatThread.Start(); + doubleThread.Start(); + decimalThread.Start(); + charThread.Start(); + intThread.Start(); + boolThread.Start(); + #endregion + #region 等待线程执行完毕 + strThread.Join(); + shortThread.Join(); + longThread.Join(); + floatThread.Join(); + doubleThread.Join(); + decimalThread.Join(); + charThread.Join(); + intThread.Join(); + boolThread.Join(); + #endregion + #endregion } +#endif } } diff --git a/TYLDDB/TYLDDB_PrivateField.cs b/TYLDDB/TYLDDB_PrivateField.cs index 52254b2..2ccdcfe 100644 --- a/TYLDDB/TYLDDB_PrivateField.cs +++ b/TYLDDB/TYLDDB_PrivateField.cs @@ -2,6 +2,10 @@ using TYLDDB.Utils.FastCache.ConcurrentDictionary; using TYLDDB.Utils.FastCache.SemaphoreThreadLock; +#if NET8_0_OR_GREATER +using TYLDDB.Utils.FastCache.TDCache; +#endif + namespace TYLDDB { public partial class LDDB @@ -10,8 +14,9 @@ public partial class LDDB private string _filePath; // 存储文件路径 private string _fileContent; // 存储文件内容 private string _databaseContent; // 存储数据库内容 - private Database_V1 database_v1; - private Database_V2 database_v2; + private readonly Database_V1 database_v1; + private readonly Database_V2 database_v2; + private readonly CacheMode _cacheMode; private CdStringDictionary cdStringDictionary; private CdShortDictionary cdShortDictionary; private CdLongDictionary cdLongDictionary; @@ -30,6 +35,10 @@ public partial class LDDB private StlDecimalDictionary stlDecimalDictionary; private StlCharDictionary stlCharDictionary; private StlBooleanDictionary stlBooleanDictionary; - #endregion + +#if NET8_0_OR_GREATER + private readonly TripleDictionaryCache tripleDictionaryCache; +#endif +#endregion } } diff --git a/TYLDDB/Utils/Database/IDatabase.cs b/TYLDDB/Utils/Database/IDatabase.cs index c154648..baef727 100644 --- a/TYLDDB/Utils/Database/IDatabase.cs +++ b/TYLDDB/Utils/Database/IDatabase.cs @@ -3,7 +3,8 @@ namespace TYLDDB.Utils.Database { /// - /// Interfaces for operations on external databases. + /// Interfaces for operations on external databases.
+ /// 对外部数据库进行操作的接口。 ///
public interface IDatabase { diff --git a/TYLDDB/Utils/Database/V1.cs b/TYLDDB/Utils/Database/V1.cs index 85cb750..28f81ad 100644 --- a/TYLDDB/Utils/Database/V1.cs +++ b/TYLDDB/Utils/Database/V1.cs @@ -45,7 +45,7 @@ public string GetDatabaseContent(string content, string databaseName) string databaseContent = match.Groups[1].Value; // 替换多余的空格和换行符,保持引号内的空格 - string cleanedContent = Regex.Replace(databaseContent, @"\s*(?=\S)", ""); + // string cleanedContent = Regex.Replace(databaseContent, @"\s*(?=\S)", ""); return databaseContent; } diff --git a/TYLDDB/Utils/Database/V2.cs b/TYLDDB/Utils/Database/V2.cs index 4ef461d..9ae3a52 100644 --- a/TYLDDB/Utils/Database/V2.cs +++ b/TYLDDB/Utils/Database/V2.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; namespace TYLDDB.Utils.Database { @@ -27,10 +28,10 @@ public string GetDatabaseContent(string input, string databaseName) // 提取数据库内容 int contentStartIndex = startIndex + startMarker.Length; - string dbContent = input.Substring(contentStartIndex, endIndex - contentStartIndex).Trim(); + string dbContent = input[contentStartIndex..endIndex].Trim(); // 使用 StringBuilder 高效地处理每行开头的空格 - var result = new System.Text.StringBuilder(); + var result = new StringBuilder(); // 将数据库内容按行分割 string[] lines = dbContent.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); @@ -48,9 +49,6 @@ public string GetDatabaseContent(string input, string databaseName) return result.ToString().TrimEnd(); // 移除结尾的多余换行符 } - public List GetDatabaseList(string fileContent) - { - throw new System.NotImplementedException(); - } + public List GetDatabaseList(string fileContent) => throw new NotImplementedException(); } } diff --git a/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs b/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs new file mode 100644 index 0000000..b9aad8b --- /dev/null +++ b/TYLDDB/Utils/FastCache/TDCache/ITripleDictionaryCache.cs @@ -0,0 +1,45 @@ +#if NET8_0_OR_GREATER +namespace TYLDDB.Utils.FastCache.TDCache +{ + /// + /// Three-value dictionary interface.
+ /// 三值字典的接口。 + ///
+ public interface ITripleDictionaryCache + { + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// + /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public abstract bool Set(string type, string key, object value); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Value
+ public abstract object Get(string type, string key); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// + /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public abstract bool Remove(string type, string key); + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public abstract void Clear(); + } +} +#endif \ No newline at end of file diff --git a/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs new file mode 100644 index 0000000..84ad64b --- /dev/null +++ b/TYLDDB/Utils/FastCache/TDCache/TripleDictionaryCache.cs @@ -0,0 +1,69 @@ +#if NET8_0_OR_GREATER +using System; +using System.Threading; +using TYLDDB.Basic; + +namespace TYLDDB.Utils.FastCache.TDCache +{ + /// + /// Three-valued dictionary cache, based on concurrent dictionary as core, combined with semaphore based thread lock to achieve high concurrency stability.
+ /// 三值字典缓存,以并发词典为核,结合基于信号量的线程锁实现高并发的稳定性。 + ///
+ public class TripleDictionaryCache : ITripleDictionaryCache, IDisposable + { + private TripleDictionary dictionary; + + /// + /// Three-valued dictionary cache, based on concurrent dictionary as core, combined with semaphore based thread lock to achieve high concurrency stability.
+ /// 三值字典缓存,以并发词典为核,结合基于信号量的线程锁实现高并发的稳定性。 + ///
+ public TripleDictionaryCache() + { + dictionary = new TripleDictionary(); + } + + /// + /// Call directly.
+ /// 直接调用。 + ///
+ public void Dispose() + { + Clear(); + } + + /// + /// Clear all cache entries.
+ /// 清空所有缓存项。 + ///
+ public void Clear() => dictionary = new TripleDictionary(); + + /// + /// Synchronization method: Obtain the corresponding value by key.
+ /// 同步方法:根据键获取对应的值。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Value
+ public object Get(string type, string key) => dictionary.Get(type, key); + + /// + /// Remove a cache entry by its key.
+ /// 根据键移除缓存项。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Whether the removal is successful.
移除操作是否成功。
+ public bool Remove(string type, string key) => dictionary.RemoveKey(type, key); + + /// + /// Set a cache entry for a specified key.
+ /// 为指定键设置缓存项。 + ///
+ /// Data type.
数据类型。 + /// Key
键 + /// Value
值 + /// Whether the operation is successful.
操作是否成功。
+ public bool Set(string type, string key, object value) => dictionary.Add(type, key, value); + } +} +#endif \ No newline at end of file