From bec95999e3ae7cc82e34b7c221405cd605957141 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Thu, 9 Jan 2025 15:03:40 +0800 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20=E9=94=99=E8=AF=AF=E7=B1=BB?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/Basic/Exception.cs | 108 -------------------- TYLDDB/Basic/Exception/DatabaseException.cs | 28 +++++ TYLDDB/Basic/Exception/Exception.cs | 81 +++++++++++++++ TYLDDB/Basic/Exception/FileException.cs | 54 ++++++++++ TYLDDB/Basic/Exception/WriteException.cs | 28 +++++ TYLDDB/TYLDDB.cs | 4 +- TYLDDB/TYLDDB.csproj | 7 +- TYLDDB/Utils/Database/V1.cs | 4 +- TYLDDB/Utils/Database/V2.cs | 1 - TYLDDB/Utils/Reader.cs | 72 ++++++++++--- TYLDDB/Utils/Writer.cs | 108 ++++++++++++-------- 11 files changed, 323 insertions(+), 172 deletions(-) delete mode 100644 TYLDDB/Basic/Exception.cs create mode 100644 TYLDDB/Basic/Exception/DatabaseException.cs create mode 100644 TYLDDB/Basic/Exception/Exception.cs create mode 100644 TYLDDB/Basic/Exception/FileException.cs create mode 100644 TYLDDB/Basic/Exception/WriteException.cs diff --git a/TYLDDB/Basic/Exception.cs b/TYLDDB/Basic/Exception.cs deleted file mode 100644 index 0d7924d..0000000 --- a/TYLDDB/Basic/Exception.cs +++ /dev/null @@ -1,108 +0,0 @@ -using System; - -namespace TYLDDB.Basic -{ - /// - /// The main error module, most of them use this
- /// 主要的错误模块,大部分都使用这个 - ///
- public class TylddbException : Exception - { - /// - /// The main error module, most of them use this
- /// 主要的错误模块,大部分都使用这个 - ///
- public TylddbException(string message) : base(message) { } - } - - /// - /// File opening failure
- /// 文件打开失败 - ///
- public class FileOpenFailException : TylddbException - { - /// - /// File opening failure
- /// 文件打开失败 - ///
- public FileOpenFailException(string message) : base(message) { } - } - - /// - /// File read failure
- /// 文件读取失败 - ///
- public class FileReadingFailException : TylddbException - { - /// - /// File read failure
- /// 文件读取失败 - ///
- public FileReadingFailException(string message) : base(message) { } - } - - /// - /// File not found
- /// 文件未找到 - ///
- public class FileNotFoundException : TylddbException - { - /// - /// File not found
- /// 文件未找到 - ///
- public FileNotFoundException(string message) : base(message) { } - } - - /// - /// The file path is null or space
- /// 文件路径为null或空白 - ///
- public class FilePathIsNullOrWhiteSpace : TylddbException - { - /// - /// The file path is null or space
- /// 文件路径为null或空白 - ///
- public FilePathIsNullOrWhiteSpace(string message) : base(message) { } - } - - /// - /// Incorrect or invalid data
- /// 错误或无效的数据 - ///
- public class InvalidDataException : TylddbException - { - /// - /// Incorrect or invalid data
- /// 错误或无效的数据 - ///
- public InvalidDataException(string message) : base(message) { } - } - - /// - /// Database not found
- /// 未找到数据库 - ///
- public class DatabaseNotFoundException : TylddbException - { - /// - /// Database not found
- /// 未找到数据库 - ///
- public DatabaseNotFoundException(string message) : base(message) { } - } - - /// - /// Description Failed to obtain the database content
- /// 数据库内容获取失败 - ///
- public class GetDatabaseContentErrorException : TylddbException - { - /// - /// Description Failed to obtain the database content
- /// 数据库内容获取失败 - ///
- public GetDatabaseContentErrorException(string message) : base(message) { } - } -} diff --git a/TYLDDB/Basic/Exception/DatabaseException.cs b/TYLDDB/Basic/Exception/DatabaseException.cs new file mode 100644 index 0000000..5f0d9f9 --- /dev/null +++ b/TYLDDB/Basic/Exception/DatabaseException.cs @@ -0,0 +1,28 @@ +namespace TYLDDB.Basic.Exception +{ + /// + /// Database not found
+ /// 未找到数据库 + ///
+ public class DatabaseNotFoundException : TylddbException + { + /// + /// Database not found
+ /// 未找到数据库 + ///
+ public DatabaseNotFoundException(string message) : base(message) { } + } + + /// + /// Description Failed to obtain the database content
+ /// 数据库内容获取失败 + ///
+ public class GetDatabaseContentErrorException : TylddbException + { + /// + /// Description Failed to obtain the database content
+ /// 数据库内容获取失败 + ///
+ public GetDatabaseContentErrorException(string message) : base(message) { } + } +} diff --git a/TYLDDB/Basic/Exception/Exception.cs b/TYLDDB/Basic/Exception/Exception.cs new file mode 100644 index 0000000..35484e9 --- /dev/null +++ b/TYLDDB/Basic/Exception/Exception.cs @@ -0,0 +1,81 @@ +namespace TYLDDB.Basic.Exception +{ + /// + /// The main error module, most of them use this
+ /// 主要的错误模块,大部分都使用这个 + ///
+ public class TylddbException : System.Exception + { + /// + /// The main error module, most of them use this
+ /// 主要的错误模块,大部分都使用这个 + ///
+ public TylddbException(string message) : base(message) { } + } + + /// + /// Error module about file.
+ /// 有关文件的错误模块。 + ///
+ public class FileException : TylddbException + { + /// + /// Error module about file.
+ /// 有关文件的错误模块。 + ///
+ public FileException(string message) : base(message) { } + } + + /// + /// Error module about write file function.
+ /// 有关写入文件功能的错误模块。 + ///
+ public class WriteException : TylddbException + { + /// + /// Error module about write file function.
+ /// 有关写入文件功能的错误模块。 + ///
+ public WriteException(string message) : base(message) { } + } + + /// + /// Error module about write file function.
+ /// 有关写入文件功能的错误模块。 + ///
+ public class ReadException : TylddbException + { + /// + /// Error module about write file function.
+ /// 有关写入文件功能的错误模块。 + ///
+ public ReadException(string message) : base(message) { } + } + + /// + /// Error module about database.
+ /// 有关数据库的错误模块。 + ///
+ public class DatabaseException : TylddbException + { + /// + /// Error module about database.
+ /// 有关数据库的错误模块。 + ///
+ public DatabaseException(string message) : base(message) { } + } + + // 未分类的错误 + /// + /// Incorrect or invalid data
+ /// 错误或无效的数据 + ///
+ public class InvalidDataException : TylddbException + { + /// + /// Incorrect or invalid data
+ /// 错误或无效的数据 + ///
+ public InvalidDataException(string message) : base(message) { } + } +} diff --git a/TYLDDB/Basic/Exception/FileException.cs b/TYLDDB/Basic/Exception/FileException.cs new file mode 100644 index 0000000..b8d1c75 --- /dev/null +++ b/TYLDDB/Basic/Exception/FileException.cs @@ -0,0 +1,54 @@ +namespace TYLDDB.Basic.Exception +{ + /// + /// File opening failure
+ /// 文件打开失败 + ///
+ public class FileOpenFailException : FileException + { + /// + /// File opening failure
+ /// 文件打开失败 + ///
+ public FileOpenFailException(string message) : base(message) { } + } + + /// + /// File read failure
+ /// 文件读取失败 + ///
+ public class FileReadingFailException : FileException + { + /// + /// File read failure
+ /// 文件读取失败 + ///
+ public FileReadingFailException(string message) : base(message) { } + } + + /// + /// File not found
+ /// 文件未找到 + ///
+ public class FileNotFoundException : FileException + { + /// + /// File not found
+ /// 文件未找到 + ///
+ public FileNotFoundException(string message) : base(message) { } + } + + /// + /// The file path is null or space
+ /// 文件路径为null或空白 + ///
+ public class FilePathIsNullOrWhiteSpace : FileException + { + /// + /// The file path is null or space
+ /// 文件路径为null或空白 + ///
+ public FilePathIsNullOrWhiteSpace(string message) : base(message) { } + } +} diff --git a/TYLDDB/Basic/Exception/WriteException.cs b/TYLDDB/Basic/Exception/WriteException.cs new file mode 100644 index 0000000..5e593ae --- /dev/null +++ b/TYLDDB/Basic/Exception/WriteException.cs @@ -0,0 +1,28 @@ +namespace TYLDDB.Basic.Exception +{ + /// + /// Error database content to file.
+ /// 写入数据库内容到文件时的错误。 + ///
+ public class WriteStringToFileException : WriteException + { + /// + /// Error database content to file.
+ /// 写入数据库内容到文件时的错误。 + ///
+ public WriteStringToFileException(string message) : base(message) { } + } + + /// + /// Block-writing error in database to file.
+ /// 分块写入数据库到文件的错误。 + ///
+ public class WriteStringToFileInChunksException : WriteException + { + /// + /// Block-writing error in database to file.
+ /// 分块写入数据库到文件的错误。 + ///
+ public WriteStringToFileInChunksException(string message) : base(message) { } + } +} diff --git a/TYLDDB/TYLDDB.cs b/TYLDDB/TYLDDB.cs index e04cf39..dce6007 100644 --- a/TYLDDB/TYLDDB.cs +++ b/TYLDDB/TYLDDB.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using TYLDDB.Basic; +using TYLDDB.Basic.Exception; using TYLDDB.Parser; using TYLDDB.Utils; using TYLDDB.Utils.Database; @@ -111,7 +111,7 @@ private static bool ValidateFilePath(string path) if (string.IsNullOrWhiteSpace(path)) { return false; - throw new FilePathIsNullOrWhiteSpace("文件路径不能为 null 或空白"); + throw new FilePathIsNullOrWhiteSpace("The file path cannot be null or blank."); } return true; } diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index 2ccd26d..ef0905d 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net8.0;net9.0 True TYLDDB.NET - 1.0.0-alpha.4pre1 + 1.0.0-alpha.4pre2 TYLDDB-Project LICENSE True @@ -19,8 +19,9 @@ DataBase QingYi-Studio README.md - Add a database parser (data type only, internal and distributed databases are not available). -Fast cache underlying interface optimization. + - Optimize error classes: Add more refined error handling and throwing. +- Allows you to customize the buffer size when reading files in blocks. + diff --git a/TYLDDB/Utils/Database/V1.cs b/TYLDDB/Utils/Database/V1.cs index b33c9c4..85cb750 100644 --- a/TYLDDB/Utils/Database/V1.cs +++ b/TYLDDB/Utils/Database/V1.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; using System.Text.RegularExpressions; -using TYLDDB.Basic; +using TYLDDB.Basic.Exception; namespace TYLDDB.Utils.Database { @@ -52,7 +52,7 @@ public string GetDatabaseContent(string content, string databaseName) else { // 如果未找到匹配项,抛出自定义异常 - throw new GetDatabaseContentErrorException($"未找到数据库 '{databaseName}' 的内容。"); + throw new GetDatabaseContentErrorException($"No content found for database '{databaseName}'."); } } } diff --git a/TYLDDB/Utils/Database/V2.cs b/TYLDDB/Utils/Database/V2.cs index 071d07a..4ef461d 100644 --- a/TYLDDB/Utils/Database/V2.cs +++ b/TYLDDB/Utils/Database/V2.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; namespace TYLDDB.Utils.Database { diff --git a/TYLDDB/Utils/Reader.cs b/TYLDDB/Utils/Reader.cs index 68e9949..41eee24 100644 --- a/TYLDDB/Utils/Reader.cs +++ b/TYLDDB/Utils/Reader.cs @@ -1,5 +1,7 @@ -using System.IO; +using System; +using System.IO; using System.Text; +using TYLDDB.Basic.Exception; namespace TYLDDB.Utils { @@ -17,24 +19,66 @@ public struct Reader /// File content
文件内容
public static string ReadFile(string filePath) { - // 这里采用分块读取文件的方式,避免一次性加载大文件 - const int bufferSize = 65536; // 64 KB缓冲区 - byte[] buffer = new byte[bufferSize]; - StringBuilder fileContent = new StringBuilder(); - - using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + try { - int bytesRead; - while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) + // 这里采用分块读取文件的方式,避免一次性加载大文件 + const int bufferSize = 65536; // 64 KB缓冲区 + byte[] buffer = new byte[bufferSize]; + StringBuilder fileContent = new StringBuilder(); + + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { - // 将每一块数据转换成字符串并累积到 fileContent 中 - string chunkContent = Encoding.UTF8.GetString(buffer, 0, bytesRead); - fileContent.Append(chunkContent); + int bytesRead; + while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) + { + // 将每一块数据转换成字符串并累积到 fileContent 中 + string chunkContent = Encoding.UTF8.GetString(buffer, 0, bytesRead); + fileContent.Append(chunkContent); + } } + + // 返回文件的全部内容 + return fileContent.ToString(); + } + catch(Exception e) + { + throw new ReadException(e.Message); } + } - // 返回文件的全部内容 - return fileContent.ToString(); + /// + /// Read file.(Custom buffer)
+ /// 读取文件。(自定义缓冲区) + ///
+ /// File path
文件路径 + /// Buffer size
缓冲区大小 + /// File content
文件内容
+ public static string ReadFile(string filePath, int bufferSize) + { + try + { + // 这里采用分块读取文件的方式,避免一次性加载大文件 + byte[] buffer = new byte[bufferSize]; + StringBuilder fileContent = new StringBuilder(); + + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + int bytesRead; + while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) + { + // 将每一块数据转换成字符串并累积到 fileContent 中 + string chunkContent = Encoding.UTF8.GetString(buffer, 0, bytesRead); + fileContent.Append(chunkContent); + } + } + + // 返回文件的全部内容 + return fileContent.ToString(); + } + catch (Exception e) + { + throw new ReadException(e.Message); + } } } } diff --git a/TYLDDB/Utils/Writer.cs b/TYLDDB/Utils/Writer.cs index a2eb667..b7def69 100644 --- a/TYLDDB/Utils/Writer.cs +++ b/TYLDDB/Utils/Writer.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text; +using TYLDDB.Basic.Exception; namespace TYLDDB.Utils { @@ -16,8 +17,12 @@ namespace TYLDDB.Utils /// 适用于在较快的硬盘中写入内容,如固态硬盘。 /// 适用于在较快的硬盘中异步写入内容,如固态硬盘。 /// - public struct Writer + public readonly struct Writer { + private static readonly int bufferSize = 64 * 1024; // 64KB 缓冲区 + + private static int offset = 0; + /// /// Write large strings to a file synchronously.
/// 同步写入大字符串到文件。 @@ -26,14 +31,18 @@ public struct Writer /// Written content
写入的内容 public static void WriteStringToFile(string filePath, string content) { - // 使用64KB的缓冲区 - int bufferSize = 64 * 1024; // 64KB 缓冲区 - - // 使用FileStream打开文件,确保文件完全覆盖而不是追加 - using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize); - using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, bufferSize); - // 写入内容 - writer.Write(content); + try + { + // 使用FileStream打开文件,确保文件完全覆盖而不是追加 + using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize); + using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, bufferSize); + // 写入内容 + writer.Write(content); + } + catch(Exception ex) + { + throw new WriteStringToFileException(ex.Message); + } } /// @@ -44,14 +53,18 @@ public static void WriteStringToFile(string filePath, string content) /// Written content
写入的内容 public static async void WriteStringToFileAsync(string filePath, string content) { - // 使用64KB的缓冲区 - int bufferSize = 64 * 1024; // 64KB 缓冲区 - - // 使用FileStream打开文件,确保文件完全覆盖而不是追加 - using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize); - using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, bufferSize); - // 异步写入内容 - await writer.WriteAsync(content); + try + { + // 使用FileStream打开文件,确保文件完全覆盖而不是追加 + using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize); + using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, bufferSize); + // 异步写入内容 + await writer.WriteAsync(content); + } + catch(Exception ex) +{ + throw new WriteStringToFileException(ex.Message); + } } /// @@ -63,20 +76,25 @@ public static async void WriteStringToFileAsync(string filePath, string content) /// Block size
分块大小 public static void WriteStringToFileInChunks(string filePath, string content, int chunkSize = 64 * 1024) { + try + { + // 将字符串转换为字节数组 + byte[] contentBytes = Encoding.UTF8.GetBytes(content); - // 将字符串转换为字节数组 - byte[] contentBytes = Encoding.UTF8.GetBytes(content); - - using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, chunkSize); - using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, chunkSize); - int totalLength = contentBytes.Length; - int offset = 0; + using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, chunkSize); + using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, chunkSize); + int totalLength = contentBytes.Length; - while (offset < totalLength) + while (offset < totalLength) + { + int length = Math.Min(chunkSize, totalLength - offset); + writer.Write(Encoding.UTF8.GetString(contentBytes, offset, length)); + offset += length; + } + } + catch(Exception ex) { - int length = Math.Min(chunkSize, totalLength - offset); - writer.Write(Encoding.UTF8.GetString(contentBytes, offset, length)); - offset += length; + throw new WriteStringToFileInChunksException(ex.Message); } } @@ -89,24 +107,30 @@ public static void WriteStringToFileInChunks(string filePath, string content, in /// Block size
分块大小 public static async void WriteStringToFileInChunksAsync(string filePath, string content, int chunkSize = 64 * 1024) { - // 将字符串转换为字节数组 - byte[] contentBytes = Encoding.UTF8.GetBytes(content); + try + { + // 将字符串转换为字节数组 + byte[] contentBytes = Encoding.UTF8.GetBytes(content); - using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, chunkSize); - using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, chunkSize); - int totalLength = contentBytes.Length; - int offset = 0; + using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, chunkSize); + using StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, chunkSize); + int totalLength = contentBytes.Length; - while (offset < totalLength) - { - int length = Math.Min(chunkSize, totalLength - offset); - string chunk = Encoding.UTF8.GetString(contentBytes, offset, length); + while (offset < totalLength) + { + int length = Math.Min(chunkSize, totalLength - offset); + string chunk = Encoding.UTF8.GetString(contentBytes, offset, length); - // 异步写入当前块 - await writer.WriteAsync(chunk); + // 异步写入当前块 + await writer.WriteAsync(chunk); - // 更新偏移量 - offset += length; + // 更新偏移量 + offset += length; + } + } + catch (Exception ex) + { + throw new WriteStringToFileInChunksException(ex.Message); } } } From 0e6dd7ac9ce28bb74ec4a13a11bb8c2d07e62072 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Fri, 10 Jan 2025 12:33:09 +0800 Subject: [PATCH 02/11] docs: update package release notes --- TYLDDB/TYLDDB.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index ef0905d..2f2e800 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -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. - + - Optimize error classes: Add more refined error handling and throwing. +- Allows you to customize the buffer size when reading files in blocks. +- Add pipe-based file read and write classes for testing. From de9f5d16a4f268f375bc642700c148ac8842058c Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Fri, 10 Jan 2025 12:40:59 +0800 Subject: [PATCH 03/11] refactor: Optimize code structure. --- TYLDDB/TYLDDB.cs | 65 ++-------------------------------- TYLDDB/TYLDDB_Instantiation.cs | 42 ++++++++++++++++++++++ TYLDDB/TYLDDB_PrivateField.cs | 34 ++++++++++++++++++ 3 files changed, 79 insertions(+), 62 deletions(-) create mode 100644 TYLDDB/TYLDDB_Instantiation.cs create mode 100644 TYLDDB/TYLDDB_PrivateField.cs diff --git a/TYLDDB/TYLDDB.cs b/TYLDDB/TYLDDB.cs index dce6007..ad97a66 100644 --- a/TYLDDB/TYLDDB.cs +++ b/TYLDDB/TYLDDB.cs @@ -4,9 +4,6 @@ using TYLDDB.Basic.Exception; using TYLDDB.Parser; using TYLDDB.Utils; -using TYLDDB.Utils.Database; -using TYLDDB.Utils.FastCache.ConcurrentDictionary; -using TYLDDB.Utils.FastCache.SemaphoreThreadLock; namespace TYLDDB { @@ -14,66 +11,8 @@ namespace TYLDDB /// The core class of the database.
/// 数据库的核心类。 ///
- public class LDDB + public partial class LDDB { - /// - /// Instantiate the LDDB class
- /// 实例化LDDB类 - ///
- public LDDB() - { - // 实例化数据库操作类 - database_v1 = new Database_V1(); - database_v2 = new Database_V2(); - - // 实例化并发词典 - 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(); - - // 实例化信号词典 - 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(); - } - - ///////////////////////////////////////////////////// 私有字段 - private string _filePath; // 存储文件路径 - private string _fileContent; // 存储文件内容 - private string _databaseContent; // 存储数据库内容 - private Database_V1 database_v1; - private Database_V2 database_v2; - private CdStringDictionary cdStringDictionary; - private CdShortDictionary cdShortDictionary; - private CdLongDictionary cdLongDictionary; - private CdIntegerDictionary cdIntegerDictionary; - private CdFloatDictionary cdFloatDictionary; - private CdDoubleDictionary cdDoubleDictionary; - private CdDecimalDictionary cdDecimalDictionary; - private CdCharDictionary cdCharDictionary; - private CdBooleanDictionary cdBooleanDictionary; - private StlStringDictionary stlStringDictionary; - private StlShortDictionary stlShortDictionary; - private StlLongDictionary stlLongDictionary; - private StlIntegerDictionary stlIntegerDictionary; - private StlFloatDictionary stlFloatDictionary; - private StlDoubleDictionary stlDoubleDictionary; - private StlDecimalDictionary stlDecimalDictionary; - private StlCharDictionary stlCharDictionary; - private StlBooleanDictionary stlBooleanDictionary; - ///////////////////////////////////////////////////// 公开字段 /// /// Set the path where you want to read the file
@@ -100,6 +39,8 @@ public string FilePath ///
public List AllDatabaseName; + ///////////////////////////////////////////////////// 方法 + /// /// 验证文件路径是否为null或空 /// diff --git a/TYLDDB/TYLDDB_Instantiation.cs b/TYLDDB/TYLDDB_Instantiation.cs new file mode 100644 index 0000000..db93b6b --- /dev/null +++ b/TYLDDB/TYLDDB_Instantiation.cs @@ -0,0 +1,42 @@ +using TYLDDB.Utils.Database; +using TYLDDB.Utils.FastCache.ConcurrentDictionary; +using TYLDDB.Utils.FastCache.SemaphoreThreadLock; + +namespace TYLDDB +{ + public partial class LDDB + { + /// + /// Instantiate the LDDB class
+ /// 实例化LDDB类 + ///
+ public LDDB() + { + // 实例化数据库操作类 + database_v1 = new Database_V1(); + database_v2 = new Database_V2(); + + // 实例化并发词典 + 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(); + + // 实例化信号词典 + 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(); + } + } +} diff --git a/TYLDDB/TYLDDB_PrivateField.cs b/TYLDDB/TYLDDB_PrivateField.cs new file mode 100644 index 0000000..9117b8e --- /dev/null +++ b/TYLDDB/TYLDDB_PrivateField.cs @@ -0,0 +1,34 @@ +using TYLDDB.Utils.Database; +using TYLDDB.Utils.FastCache.ConcurrentDictionary; +using TYLDDB.Utils.FastCache.SemaphoreThreadLock; + +namespace TYLDDB +{ + public partial class LDDB + { + ///////////////////////////////////////////////////// 私有字段 + private string _filePath; // 存储文件路径 + private string _fileContent; // 存储文件内容 + private string _databaseContent; // 存储数据库内容 + private Database_V1 database_v1; + private Database_V2 database_v2; + private CdStringDictionary cdStringDictionary; + private CdShortDictionary cdShortDictionary; + private CdLongDictionary cdLongDictionary; + private CdIntegerDictionary cdIntegerDictionary; + private CdFloatDictionary cdFloatDictionary; + private CdDoubleDictionary cdDoubleDictionary; + private CdDecimalDictionary cdDecimalDictionary; + private CdCharDictionary cdCharDictionary; + private CdBooleanDictionary cdBooleanDictionary; + private StlStringDictionary stlStringDictionary; + private StlShortDictionary stlShortDictionary; + private StlLongDictionary stlLongDictionary; + private StlIntegerDictionary stlIntegerDictionary; + private StlFloatDictionary stlFloatDictionary; + private StlDoubleDictionary stlDoubleDictionary; + private StlDecimalDictionary stlDecimalDictionary; + private StlCharDictionary stlCharDictionary; + private StlBooleanDictionary stlBooleanDictionary; + } +} From aa087671100ced1af3f9dbb9f168c4e6d80d62ac Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Fri, 10 Jan 2025 12:45:35 +0800 Subject: [PATCH 04/11] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96Parse?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB.Test/Program.cs | 4 +- TYLDDB/Parser/{Parser.cs => V1.cs} | 2 +- TYLDDB/TYLDDB.cs | 614 ---------------------------- TYLDDB/TYLDDB_Parse.cs | 622 +++++++++++++++++++++++++++++ 4 files changed, 625 insertions(+), 617 deletions(-) rename TYLDDB/Parser/{Parser.cs => V1.cs} (99%) create mode 100644 TYLDDB/TYLDDB_Parse.cs diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index ada0a5c..f317288 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -47,12 +47,12 @@ ///////////////////////////////////////////////////////////////////////////////////////////////////////// 数据库解析缓存 HighPrecisionTimer parseDbTimer = new(); // 从发起解析文件到成功解析并写入缓存的总时间(同步) parseDbTimer.Start(); -await lddb.Parse(); +await lddb.Parse_V1(); parseDbTimer.Stop(); WriteTime("从发起解析文件到成功解析并写入缓存的总时间(同步): ", parseDbTimer.ElapsedMilliseconds()); HighPrecisionTimer parseDbTimerAsync = new(); // 从发起解析文件到成功解析并写入缓存的总时间(异步) parseDbTimerAsync.Start(); -await lddb.ParseAsync(); +await lddb.ParseAsync_V1(); parseDbTimerAsync.Stop(); WriteTime("从发起解析文件到成功解析并写入缓存的总时间(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); diff --git a/TYLDDB/Parser/Parser.cs b/TYLDDB/Parser/V1.cs similarity index 99% rename from TYLDDB/Parser/Parser.cs rename to TYLDDB/Parser/V1.cs index a0d522d..82f7036 100644 --- a/TYLDDB/Parser/Parser.cs +++ b/TYLDDB/Parser/V1.cs @@ -9,7 +9,7 @@ namespace TYLDDB.Parser /// Data type parser.
/// 数据类型解析器。 ///
- public class DataParser + public class DataParser_V1 { // 定义正则表达式来匹配键值对(类型::"key"="value") private readonly static string pattern = @"(\w+)\s*::\s*""([^""]+)""\s*=\s*""([^""]+)"";"; diff --git a/TYLDDB/TYLDDB.cs b/TYLDDB/TYLDDB.cs index ad97a66..43635c5 100644 --- a/TYLDDB/TYLDDB.cs +++ b/TYLDDB/TYLDDB.cs @@ -97,620 +97,6 @@ public void LoadDatabase_V2(string db) ///
public void ReadAllDatabaseName() => AllDatabaseName = database_v1.GetDatabaseList(_fileContent); - /// - /// Reparse the entire database.
- /// 重新解析整个数据库。 - ///
- public async Task Parse() - { - // 创建多个任务,并使用 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); - - // ConcurrentDictionary - void CdString() - { - var dict = DataParser.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 CdShort() - { - var dict = DataParser.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 CdLong() - { - var dict = DataParser.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 CdInt() - { - var dict = DataParser.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 CdFloat() - { - var dict = DataParser.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 CdDouble() - { - var dict = DataParser.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 CdDecimal() - { - var dict = DataParser.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 CdChar() - { - var dict = DataParser.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 CdBool() - { - var dict = DataParser.ParseBoolean(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - cdBooleanDictionary.Set(key, value); - } - } - - // SemaphoreThreadLock - void StlString() - { - var dict = DataParser.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 StlShort() - { - var dict = DataParser.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 StlLong() - { - var dict = DataParser.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 StlInt() - { - var dict = DataParser.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 StlFloat() - { - var dict = DataParser.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 StlDouble() - { - var dict = DataParser.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 StlDecimal() - { - var dict = DataParser.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 StlChar() - { - var dict = DataParser.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 StlBool() - { - var dict = DataParser.ParseBoolean(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - stlBooleanDictionary.Set(key, value); - } - } - } - - /// - /// Reparse the entire database.
- /// 重新解析整个数据库。 - ///
- public async Task ParseAsync() - { - // 创建多个任务,并使用 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); - - // ConcurrentDictionary - async void CdString() - { - var dict = DataParser.ParseString(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdStringDictionary.SetAsync(key, value); - } - } - async void CdShort() - { - var dict = DataParser.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() - { - var dict = DataParser.ParseLong(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdLongDictionary.SetAsync(key, value); - } - } - async void CdInt() - { - var dict = DataParser.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() - { - var dict = DataParser.ParseFloat(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdFloatDictionary.SetAsync(key, value); - } - } - async void CdDouble() - { - var dict = DataParser.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() - { - var dict = DataParser.ParseDecimal(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdDecimalDictionary.SetAsync(key, value); - } - } - async void CdChar() - { - var dict = DataParser.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() - { - var dict = DataParser.ParseBoolean(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await cdBooleanDictionary.SetAsync(key, value); - } - } - - // SemaphoreThreadLock - async void StlString() - { - var dict = DataParser.ParseString(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlStringDictionary.SetAsync(key, value); - } - } - async void StlShort() - { - var dict = DataParser.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() - { - var dict = DataParser.ParseLong(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlLongDictionary.SetAsync(key, value); - } - } - async void StlInt() - { - var dict = DataParser.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() - { - var dict = DataParser.ParseFloat(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlFloatDictionary.SetAsync(key, value); - } - } - async void StlDouble() - { - var dict = DataParser.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.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() - { - var dict = DataParser.ParseChar(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlCharDictionary.SetAsync(key, value); - } - } - async void StlBool() - { - var dict = DataParser.ParseBoolean(_databaseContent); - - // 遍历 dict 中的每一项 - foreach (var kvp in dict) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - await stlBooleanDictionary.SetAsync(key, value); - } - } - } - /// /// Finds the value associated with a given key from multiple types of concurrent dictionaries and returns all non-empty values as an array of strings.
/// 从多个类型的并发词典中查找与给定键相关的值,并将所有非空的值以字符串数组的形式返回。 diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs new file mode 100644 index 0000000..c5d9329 --- /dev/null +++ b/TYLDDB/TYLDDB_Parse.cs @@ -0,0 +1,622 @@ +using System.Threading.Tasks; +using TYLDDB.Parser; + +namespace TYLDDB +{ + public partial class LDDB + { + /// + /// Reparse the entire database.
+ /// 重新解析整个数据库。 + ///
+ public async Task Parse_V1() + { + // 创建多个任务,并使用 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); + + // ConcurrentDictionary + void CdString() + { + 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 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 对存储到缓存 + cdShortDictionary.Set(key, value); + } + } + void CdLong() + { + 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 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 对存储到缓存 + cdIntegerDictionary.Set(key, value); + } + } + void CdFloat() + { + 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 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 对存储到缓存 + cdDoubleDictionary.Set(key, value); + } + } + void CdDecimal() + { + 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 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 对存储到缓存 + cdCharDictionary.Set(key, value); + } + } + void CdBool() + { + 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); + } + } + + // SemaphoreThreadLock + void StlString() + { + 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 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 对存储到缓存 + stlShortDictionary.Set(key, value); + } + } + void StlLong() + { + 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 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 对存储到缓存 + stlIntegerDictionary.Set(key, value); + } + } + void StlFloat() + { + 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 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 对存储到缓存 + stlDoubleDictionary.Set(key, value); + } + } + 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 对存储到缓存 + stlDecimalDictionary.Set(key, value); + } + } + void StlChar() + { + 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 StlBool() + { + 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); + } + } + } + + /// + /// Reparse the entire database.
+ /// 重新解析整个数据库。 + ///
+ public async Task ParseAsync_V1() + { + // 创建多个任务,并使用 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); + + // ConcurrentDictionary + async void CdString() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await cdStringDictionary.SetAsync(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() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await cdLongDictionary.SetAsync(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() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await cdFloatDictionary.SetAsync(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() + { + 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 cdDecimalDictionary.SetAsync(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() + { + var dict = DataParser_V1.ParseBoolean(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await cdBooleanDictionary.SetAsync(key, value); + } + } + + // SemaphoreThreadLock + async void StlString() + { + var dict = DataParser_V1.ParseString(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await stlStringDictionary.SetAsync(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() + { + var dict = DataParser_V1.ParseLong(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await stlLongDictionary.SetAsync(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() + { + var dict = DataParser_V1.ParseFloat(_databaseContent); + + // 遍历 dict 中的每一项 + foreach (var kvp in dict) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + await stlFloatDictionary.SetAsync(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() + { + 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 stlCharDictionary.SetAsync(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) + + // 将 key-value 对存储到缓存 + await stlBooleanDictionary.SetAsync(key, value); + } + } + } + } +} From 09c7f4ec1c2aab14605479617595b556b1be9807 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 11 Jan 2025 21:07:28 +0800 Subject: [PATCH 05/11] =?UTF-8?q?feat:=20=E6=97=A0=E6=AD=A3=E5=88=99?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/Parser/V2.cs | 728 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 728 insertions(+) create mode 100644 TYLDDB/Parser/V2.cs diff --git a/TYLDDB/Parser/V2.cs b/TYLDDB/Parser/V2.cs new file mode 100644 index 0000000..6b4678c --- /dev/null +++ b/TYLDDB/Parser/V2.cs @@ -0,0 +1,728 @@ +using System.Collections.Generic; +using System; + +namespace TYLDDB.Parser +{ + /// + /// Data type parser.
+ /// 数据类型解析器。 + ///
+ public class DataParser_V2 + { + // TODO: 需要处理if中的continue,if (typeEndIndex == -1 || equalSignIndex == -1)判断不建议直接continue!可以尝试抛出一个错误,在外部做错误处理! + + /// + /// Parses the given content and finds all matching key-value pairs. This method is resolved by calling other methods, such as .
+ /// 解析给定的内容并查找所有匹配的键值对。该方法为调用其它方法进行解析,如。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// Data types thet need to be resolved.
需要解析的数据类型。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary Parse(string content, DataParser_Type type) + { + switch (type) + { + case DataParser_Type.String: + return ParseString(content) as Dictionary; + case DataParser_Type.Int: + return ParseInt(content) as Dictionary; + case DataParser_Type.Short: + return ParseShort(content) as Dictionary; + case DataParser_Type.Long: + return ParseLong(content) as Dictionary; + case DataParser_Type.Float: + return ParseFloat(content) as Dictionary; + case DataParser_Type.Double: + return ParseDouble(content) as Dictionary; + case DataParser_Type.Bool: + return ParseBoolean(content) as Dictionary; + case DataParser_Type.Char: + return ParseChar(content) as Dictionary; + case DataParser_Type.Decimal: + return ParseDecimal(content) as Dictionary; + default: + var result = new Dictionary(); + return result; + } + } + + /// + /// Parses the given content and finds all matching key-value pairs. This method uses built-in integrated parsing rather than invoking other method parsing.
+ /// 解析给定的内容并查找所有匹配的键值对。该方法采用内置集成式解析而非调用其它方法解析。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// Data types thet need to be resolved.
需要解析的数据类型。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary IntegratedParse(string content, DataParser_Type type) + { + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + switch (type) + { + case DataParser_Type.String: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + + // 如果类型匹配,添加到字典 + if (string.Equals("string", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = matchedValue; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Int: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + int value = int.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("int", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Short: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + short value = short.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("short", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Long: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + long value = long.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("long", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Float: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + float value = float.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("float", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Double: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + double value = double.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("double", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Bool: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + bool value = bool.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("boolean", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Char: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + char value = char.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("char", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + case DataParser_Type.Decimal: + { + var resultTemp = new Dictionary(); + + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + decimal value = decimal.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("decimal", matchedType, StringComparison.OrdinalIgnoreCase)) + { + resultTemp[matchedKey] = value; + result = resultTemp as Dictionary; + } + + break; + } + + default: + return result; + } + } + + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseString(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + + // 如果类型匹配,添加到字典 + if (string.Equals("string", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = matchedValue; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseInt(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + int value = int.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("int", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseShort(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + short value = short.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("short", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseLong(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + long value = long.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("long", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseFloat(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + float value = float.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("float", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseDouble(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + double value = double.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("double", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseBoolean(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + bool value = bool.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("boolean", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseChar(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + char value = char.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("char", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + + /// + /// Parses the given content and finds all matching key-value pairs.
+ /// 解析给定的内容并查找所有匹配的键值对。 + ///
+ /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 + /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
+ public static Dictionary ParseDecimal(string content) + { + // 创建一个字典来存储找到的所有键值对 + var result = new Dictionary(); + + // 分割内容为行,逐行分析 + var lines = content.Split(';'); + + foreach (var line in lines) + { + // 去除空格并忽略空行 + string trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine)) + { + continue; + } + + // 查找 :: 和 = 的位置 + int typeEndIndex = trimmedLine.IndexOf("::"); + int equalSignIndex = trimmedLine.IndexOf("="); + + if (typeEndIndex == -1 || equalSignIndex == -1) + { + continue; + } + // 提取类型、键和值 + string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); + string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); + string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); + decimal value = decimal.Parse(matchedValue); + + // 如果类型匹配,添加到字典 + if (string.Equals("decimal", matchedType, StringComparison.OrdinalIgnoreCase)) + { + result[matchedKey] = value; + } + } + + // 返回结果字典,若没有匹配项则返回空字典 + return result; + } + } + + /// + /// Parse type Settings for data type parser V2.
+ /// 数据类型解析器V2的解析类型设置。 + ///
+ public enum DataParser_Type + { + /// + /// String.
+ /// 字符串。 + ///
+ String, + /// + /// Integer.
+ /// 整数类型。 + ///
+ Int, + /// + /// Short.
+ /// 短整数类型。 + ///
+ Short, + /// + /// Long.
+ /// 长整数类型。 + ///
+ Long, + /// + /// Float.
+ /// 浮点数。 + ///
+ Float, + /// + /// Double.
+ /// 双精度浮点数。 + ///
+ Double, + /// + /// Boolean.
+ /// 布尔值。 + ///
+ Bool, + /// + /// Char. + /// + Char, + /// + /// Decimal.
+ /// 高精度十进制数。 + ///
+ Decimal, + } +} From bf944adad31a746eb732c3e18e20bc26d25de43a Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 11 Jan 2025 21:07:42 +0800 Subject: [PATCH 06/11] =?UTF-8?q?chore:=20=E5=88=A0=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84using?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB/TYLDDB.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/TYLDDB/TYLDDB.cs b/TYLDDB/TYLDDB.cs index 43635c5..fae863d 100644 --- a/TYLDDB/TYLDDB.cs +++ b/TYLDDB/TYLDDB.cs @@ -1,8 +1,6 @@ using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using TYLDDB.Basic.Exception; -using TYLDDB.Parser; using TYLDDB.Utils; namespace TYLDDB From c5af6e5452427e71e9be45dd8955b5e76659ae5d Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sun, 12 Jan 2025 14:21:09 +0800 Subject: [PATCH 07/11] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0V2=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB.Test/Program.cs | 13 +++-- TYLDDB/Parser/V2.cs | 2 + TYLDDB/TYLDDB_Parse.cs | 119 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 4 deletions(-) diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index f317288..2ea9ff7 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -45,16 +45,21 @@ WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); ///////////////////////////////////////////////////////////////////////////////////////////////////////// 数据库解析缓存 -HighPrecisionTimer parseDbTimer = new(); // 从发起解析文件到成功解析并写入缓存的总时间(同步) +HighPrecisionTimer parseDbTimer = new(); parseDbTimer.Start(); await lddb.Parse_V1(); parseDbTimer.Stop(); -WriteTime("从发起解析文件到成功解析并写入缓存的总时间(同步): ", parseDbTimer.ElapsedMilliseconds()); -HighPrecisionTimer parseDbTimerAsync = new(); // 从发起解析文件到成功解析并写入缓存的总时间(异步) +WriteTime("解析文件并写入缓存V1(同步): ", parseDbTimer.ElapsedMilliseconds()); +HighPrecisionTimer parseDbTimerAsync = new(); parseDbTimerAsync.Start(); await lddb.ParseAsync_V1(); parseDbTimerAsync.Stop(); -WriteTime("从发起解析文件到成功解析并写入缓存的总时间(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); +WriteTime("解析文件并写入缓存V1(异步): ", parseDbTimerAsync.ElapsedMilliseconds()); +HighPrecisionTimer parseDbV2Timer = new(); +parseDbV2Timer.Start(); +lddb.Parse_V2(); +parseDbV2Timer.Stop(); +WriteTime("解析文件并写入缓存V1(同步): ", parseDbV2Timer.ElapsedMilliseconds()); ///////////////////////////////////////////////////////////////////////////////////////////////////////// 并发词典数据库全类型同步搜寻 HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); diff --git a/TYLDDB/Parser/V2.cs b/TYLDDB/Parser/V2.cs index 6b4678c..8988a78 100644 --- a/TYLDDB/Parser/V2.cs +++ b/TYLDDB/Parser/V2.cs @@ -11,6 +11,8 @@ public class DataParser_V2 { // TODO: 需要处理if中的continue,if (typeEndIndex == -1 || equalSignIndex == -1)判断不建议直接continue!可以尝试抛出一个错误,在外部做错误处理! + // TODO:添加用于实例化的方法 + /// /// Parses the given content and finds all matching key-value pairs. This method is resolved by calling other methods, such as .
/// 解析给定的内容并查找所有匹配的键值对。该方法为调用其它方法进行解析,如。 diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs index c5d9329..f44b805 100644 --- a/TYLDDB/TYLDDB_Parse.cs +++ b/TYLDDB/TYLDDB_Parse.cs @@ -618,5 +618,124 @@ async void StlBool() } } } + + /// + /// Reparse the entire database.
+ /// 重新解析整个数据库。 + ///
+ public void Parse_V2() + { + var _str = DataParser_V2.ParseString(_databaseContent); + var _int = DataParser_V2.ParseInt(_databaseContent); + var _short = DataParser_V2.ParseShort(_databaseContent); + var _long = DataParser_V2.ParseLong(_databaseContent); + var _float = DataParser_V2.ParseFloat(_databaseContent); + var _double = DataParser_V2.ParseDouble(_databaseContent); + var _bool = DataParser_V2.ParseBoolean(_databaseContent); + var _char = DataParser_V2.ParseChar(_databaseContent); + var _decimal = DataParser_V2.ParseDecimal(_databaseContent); + + foreach (var kvp in _str) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdStringDictionary.Set(key, value); + stlStringDictionary.Set(key, value); + } + + foreach (var kvp in _int) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdIntegerDictionary.Set(key, value); + stlIntegerDictionary.Set(key, value); + } + + foreach (var kvp in _short) + { + var key = kvp.Key; // 获取第一个值 (key) + var value = kvp.Value; // 获取第二个值 (value) + + // 将 key-value 对存储到缓存 + cdShortDictionary.Set(key, value); + stlShortDictionary.Set(key, value); + } + + foreach (var kvp in _long) + { + var key = kvp.Key; + var value = kvp.Value; + + cdLongDictionary.Set(key, value); + stlLongDictionary.Set(key, value); + } + + foreach(var kvp in _float) + { + var key = kvp.Key; + var value = kvp.Value; + + cdFloatDictionary.Set(key, value); + stlFloatDictionary.Set(key, value); + } + + foreach(var kvp in _double) + { + var key = kvp.Key; + var value = kvp.Value; + + cdDoubleDictionary.Set(key, value); + stlDoubleDictionary.Set(key, value); + } + + foreach(var kvp in _bool) + { + var key = kvp.Key; + var value = kvp.Value; + + cdBooleanDictionary.Set(key, value); + stlBooleanDictionary.Set(key, value); + } + + foreach(var kvp in _char) + { + var key = kvp.Key; + var value = kvp.Value; + + cdCharDictionary.Set(key, value); + stlCharDictionary.Set(key, value); + } + + foreach(var kvp in _decimal) + { + var key = kvp.Key; + var value = kvp.Value; + + cdDecimalDictionary.Set(key, value); + stlDecimalDictionary.Set(key, value); + } + } + + /// + /// Reparse the entire database.
+ /// 重新解析整个数据库。 + ///
+ public void Parse_V2Integrated() + { + + } + + /// + /// Reparse the entire database.
+ /// 重新解析整个数据库。 + ///
+ public void Parse_V2Nonintegrated() + { + + } } } From 9745291fc8a83e6fd2e2694221b9f927ee3a0230 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sun, 12 Jan 2025 14:58:38 +0800 Subject: [PATCH 08/11] =?UTF-8?q?test:=20=E4=BD=BF=E7=94=A8region=E7=BB=99?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=88=86=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB.Test/Program.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index 2ea9ff7..be22b70 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -4,9 +4,12 @@ string dbFilePath = "./example.lddb"; List testData = []; +#region 实例化 ///////////////////////////////////////////////////////////////////////////////////////////////////////// 实例化 var lddb = new LDDB(); +#endregion +#region 读取文件 ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件 HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间 lddb.FilePath = dbFilePath; @@ -14,7 +17,9 @@ lddb.ReadingFile(); readDbTimer.Stop(); WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds()); +#endregion +#region 读取数据库 ///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库 HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间 loadDbTimer.Start(); @@ -29,7 +34,9 @@ Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容 loadDbV2Timer.Stop(); WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为(V2): ", loadDbV2Timer.ElapsedMilliseconds()); +#endregion +#region 获取所有数据库名称 ///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称 HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间 readAllDbNameTimer.Start(); @@ -43,6 +50,7 @@ } } WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds()); +#endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////// 数据库解析缓存 HighPrecisionTimer parseDbTimer = new(); From 3bfcd42758ed243595785d9db0776cfcf1358257 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sun, 12 Jan 2025 15:24:59 +0800 Subject: [PATCH 09/11] =?UTF-8?q?fix:=20=E7=94=B1=E4=BA=8E=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E4=B8=A5=E9=87=8Dbug=E7=A7=BB=E9=99=A4V2=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TYLDDB.Test/Program.cs | 11 +- TYLDDB/Parser/V2.cs | 730 ----------------------------------------- TYLDDB/TYLDDB_Parse.cs | 119 ------- 3 files changed, 6 insertions(+), 854 deletions(-) delete mode 100644 TYLDDB/Parser/V2.cs diff --git a/TYLDDB.Test/Program.cs b/TYLDDB.Test/Program.cs index be22b70..5f4d0b1 100644 --- a/TYLDDB.Test/Program.cs +++ b/TYLDDB.Test/Program.cs @@ -63,11 +63,12 @@ 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 parseDbV2Timer = new(); +//parseDbV2Timer.Start(); +//lddb.Parse_V2(); +//parseDbV2Timer.Stop(); +//WriteTime("解析文件并写入缓存V1(同步): ", parseDbV2Timer.ElapsedMilliseconds()); ///////////////////////////////////////////////////////////////////////////////////////////////////////// 并发词典数据库全类型同步搜寻 HighPrecisionTimer allTypeSearchFromConcurrentDictionaryTimer = new(); diff --git a/TYLDDB/Parser/V2.cs b/TYLDDB/Parser/V2.cs deleted file mode 100644 index 8988a78..0000000 --- a/TYLDDB/Parser/V2.cs +++ /dev/null @@ -1,730 +0,0 @@ -using System.Collections.Generic; -using System; - -namespace TYLDDB.Parser -{ - /// - /// Data type parser.
- /// 数据类型解析器。 - ///
- public class DataParser_V2 - { - // TODO: 需要处理if中的continue,if (typeEndIndex == -1 || equalSignIndex == -1)判断不建议直接continue!可以尝试抛出一个错误,在外部做错误处理! - - // TODO:添加用于实例化的方法 - - /// - /// Parses the given content and finds all matching key-value pairs. This method is resolved by calling other methods, such as .
- /// 解析给定的内容并查找所有匹配的键值对。该方法为调用其它方法进行解析,如。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// Data types thet need to be resolved.
需要解析的数据类型。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary Parse(string content, DataParser_Type type) - { - switch (type) - { - case DataParser_Type.String: - return ParseString(content) as Dictionary; - case DataParser_Type.Int: - return ParseInt(content) as Dictionary; - case DataParser_Type.Short: - return ParseShort(content) as Dictionary; - case DataParser_Type.Long: - return ParseLong(content) as Dictionary; - case DataParser_Type.Float: - return ParseFloat(content) as Dictionary; - case DataParser_Type.Double: - return ParseDouble(content) as Dictionary; - case DataParser_Type.Bool: - return ParseBoolean(content) as Dictionary; - case DataParser_Type.Char: - return ParseChar(content) as Dictionary; - case DataParser_Type.Decimal: - return ParseDecimal(content) as Dictionary; - default: - var result = new Dictionary(); - return result; - } - } - - /// - /// Parses the given content and finds all matching key-value pairs. This method uses built-in integrated parsing rather than invoking other method parsing.
- /// 解析给定的内容并查找所有匹配的键值对。该方法采用内置集成式解析而非调用其它方法解析。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// Data types thet need to be resolved.
需要解析的数据类型。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary IntegratedParse(string content, DataParser_Type type) - { - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - switch (type) - { - case DataParser_Type.String: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - - // 如果类型匹配,添加到字典 - if (string.Equals("string", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = matchedValue; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Int: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - int value = int.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("int", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Short: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - short value = short.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("short", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Long: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - long value = long.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("long", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Float: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - float value = float.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("float", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Double: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - double value = double.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("double", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Bool: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - bool value = bool.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("boolean", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Char: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - char value = char.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("char", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - case DataParser_Type.Decimal: - { - var resultTemp = new Dictionary(); - - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - decimal value = decimal.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("decimal", matchedType, StringComparison.OrdinalIgnoreCase)) - { - resultTemp[matchedKey] = value; - result = resultTemp as Dictionary; - } - - break; - } - - default: - return result; - } - } - - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseString(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - - // 如果类型匹配,添加到字典 - if (string.Equals("string", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = matchedValue; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseInt(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - int value = int.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("int", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseShort(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - short value = short.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("short", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseLong(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - long value = long.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("long", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseFloat(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - float value = float.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("float", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseDouble(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - double value = double.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("double", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseBoolean(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - bool value = bool.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("boolean", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseChar(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - char value = char.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("char", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - - /// - /// Parses the given content and finds all matching key-value pairs.
- /// 解析给定的内容并查找所有匹配的键值对。 - ///
- /// The content string to be parsed, containing key-value pairs.
要解析的内容字符串,包含键值对。 - /// A dictionary containing all matched key-value pairs. If no matching key-value pair is found, an empty dictionary is returned.
包含所有匹配的键值对的字典。如果没有找到匹配的键值对,返回空字典。
- public static Dictionary ParseDecimal(string content) - { - // 创建一个字典来存储找到的所有键值对 - var result = new Dictionary(); - - // 分割内容为行,逐行分析 - var lines = content.Split(';'); - - foreach (var line in lines) - { - // 去除空格并忽略空行 - string trimmedLine = line.Trim(); - if (string.IsNullOrEmpty(trimmedLine)) - { - continue; - } - - // 查找 :: 和 = 的位置 - int typeEndIndex = trimmedLine.IndexOf("::"); - int equalSignIndex = trimmedLine.IndexOf("="); - - if (typeEndIndex == -1 || equalSignIndex == -1) - { - continue; - } - // 提取类型、键和值 - string matchedType = trimmedLine.Substring(0, typeEndIndex).Trim(); - string matchedKey = trimmedLine.Substring(typeEndIndex + 2, equalSignIndex - typeEndIndex - 2).Trim().Trim('"'); - string matchedValue = trimmedLine.Substring(equalSignIndex + 1).Trim().Trim('"'); - decimal value = decimal.Parse(matchedValue); - - // 如果类型匹配,添加到字典 - if (string.Equals("decimal", matchedType, StringComparison.OrdinalIgnoreCase)) - { - result[matchedKey] = value; - } - } - - // 返回结果字典,若没有匹配项则返回空字典 - return result; - } - } - - /// - /// Parse type Settings for data type parser V2.
- /// 数据类型解析器V2的解析类型设置。 - ///
- public enum DataParser_Type - { - /// - /// String.
- /// 字符串。 - ///
- String, - /// - /// Integer.
- /// 整数类型。 - ///
- Int, - /// - /// Short.
- /// 短整数类型。 - ///
- Short, - /// - /// Long.
- /// 长整数类型。 - ///
- Long, - /// - /// Float.
- /// 浮点数。 - ///
- Float, - /// - /// Double.
- /// 双精度浮点数。 - ///
- Double, - /// - /// Boolean.
- /// 布尔值。 - ///
- Bool, - /// - /// Char. - /// - Char, - /// - /// Decimal.
- /// 高精度十进制数。 - ///
- Decimal, - } -} diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs index f44b805..c5d9329 100644 --- a/TYLDDB/TYLDDB_Parse.cs +++ b/TYLDDB/TYLDDB_Parse.cs @@ -618,124 +618,5 @@ async void StlBool() } } } - - /// - /// Reparse the entire database.
- /// 重新解析整个数据库。 - ///
- public void Parse_V2() - { - var _str = DataParser_V2.ParseString(_databaseContent); - var _int = DataParser_V2.ParseInt(_databaseContent); - var _short = DataParser_V2.ParseShort(_databaseContent); - var _long = DataParser_V2.ParseLong(_databaseContent); - var _float = DataParser_V2.ParseFloat(_databaseContent); - var _double = DataParser_V2.ParseDouble(_databaseContent); - var _bool = DataParser_V2.ParseBoolean(_databaseContent); - var _char = DataParser_V2.ParseChar(_databaseContent); - var _decimal = DataParser_V2.ParseDecimal(_databaseContent); - - foreach (var kvp in _str) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - cdStringDictionary.Set(key, value); - stlStringDictionary.Set(key, value); - } - - foreach (var kvp in _int) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - cdIntegerDictionary.Set(key, value); - stlIntegerDictionary.Set(key, value); - } - - foreach (var kvp in _short) - { - var key = kvp.Key; // 获取第一个值 (key) - var value = kvp.Value; // 获取第二个值 (value) - - // 将 key-value 对存储到缓存 - cdShortDictionary.Set(key, value); - stlShortDictionary.Set(key, value); - } - - foreach (var kvp in _long) - { - var key = kvp.Key; - var value = kvp.Value; - - cdLongDictionary.Set(key, value); - stlLongDictionary.Set(key, value); - } - - foreach(var kvp in _float) - { - var key = kvp.Key; - var value = kvp.Value; - - cdFloatDictionary.Set(key, value); - stlFloatDictionary.Set(key, value); - } - - foreach(var kvp in _double) - { - var key = kvp.Key; - var value = kvp.Value; - - cdDoubleDictionary.Set(key, value); - stlDoubleDictionary.Set(key, value); - } - - foreach(var kvp in _bool) - { - var key = kvp.Key; - var value = kvp.Value; - - cdBooleanDictionary.Set(key, value); - stlBooleanDictionary.Set(key, value); - } - - foreach(var kvp in _char) - { - var key = kvp.Key; - var value = kvp.Value; - - cdCharDictionary.Set(key, value); - stlCharDictionary.Set(key, value); - } - - foreach(var kvp in _decimal) - { - var key = kvp.Key; - var value = kvp.Value; - - cdDecimalDictionary.Set(key, value); - stlDecimalDictionary.Set(key, value); - } - } - - /// - /// Reparse the entire database.
- /// 重新解析整个数据库。 - ///
- public void Parse_V2Integrated() - { - - } - - /// - /// Reparse the entire database.
- /// 重新解析整个数据库。 - ///
- public void Parse_V2Nonintegrated() - { - - } } } From d15692d7f5461d06523b56ccb22a2b6ee5a17e54 Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 00:11:07 +0800 Subject: [PATCH 10/11] =?UTF-8?q?feat:=20=E4=B8=89=E5=80=BC=E5=AD=97?= =?UTF-8?q?=E5=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Exception/TripleDictionaryException.cs | 16 ++ .../{Exception.cs => TylddbException.cs} | 13 ++ TYLDDB/Basic/TripleDictionary.cs | 187 ++++++++++++++++++ TYLDDB/TYLDDB.csproj | 6 +- 4 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 TYLDDB/Basic/Exception/TripleDictionaryException.cs rename TYLDDB/Basic/Exception/{Exception.cs => TylddbException.cs} (87%) create mode 100644 TYLDDB/Basic/TripleDictionary.cs diff --git a/TYLDDB/Basic/Exception/TripleDictionaryException.cs b/TYLDDB/Basic/Exception/TripleDictionaryException.cs new file mode 100644 index 0000000..28fec7b --- /dev/null +++ b/TYLDDB/Basic/Exception/TripleDictionaryException.cs @@ -0,0 +1,16 @@ +namespace TYLDDB.Basic.Exception +{ +#if NET8_0_OR_GREATER + /// + /// An exception class to the three-value dictionary.
+ /// 三值字典的例外类。 + ///
+ public class TripleDictionaryException(string message) : DictionaryException(message) { } + + /// + /// The specified key was not found.
+ /// 未找到指定的键。 + ///
+ public class TripleDictionaryKeyNotFoundException(string message) : TripleDictionaryException(message) { } +#endif +} diff --git a/TYLDDB/Basic/Exception/Exception.cs b/TYLDDB/Basic/Exception/TylddbException.cs similarity index 87% rename from TYLDDB/Basic/Exception/Exception.cs rename to TYLDDB/Basic/Exception/TylddbException.cs index 35484e9..f2b382e 100644 --- a/TYLDDB/Basic/Exception/Exception.cs +++ b/TYLDDB/Basic/Exception/TylddbException.cs @@ -65,6 +65,19 @@ public class DatabaseException : TylddbException public DatabaseException(string message) : base(message) { } } + /// + /// Dictionary exception class.
+ /// 字典的错误类。 + ///
+ public class DictionaryException : TylddbException + { + /// + /// Dictionary exception class.
+ /// 字典的错误类。 + ///
+ public DictionaryException(string message) : base(message) { } + } + // 未分类的错误 /// /// Incorrect or invalid data
diff --git a/TYLDDB/Basic/TripleDictionary.cs b/TYLDDB/Basic/TripleDictionary.cs new file mode 100644 index 0000000..12ea9b1 --- /dev/null +++ b/TYLDDB/Basic/TripleDictionary.cs @@ -0,0 +1,187 @@ +using System.Collections.Generic; +using System; +using System.Collections.Concurrent; +using System.Linq; +using TYLDDB.Basic.Exception; + +namespace TYLDDB.Basic +{ +#if NET8_0_OR_GREATER + /// + /// Three-value dictionary.
+ /// 三值字典。 + ///
+ /// The data type of the value.
值的数据类型。
+ public class TripleDictionary + { + private readonly ConcurrentDictionary, TValue> _dictionary; + + /// + /// Three-value dictionary.
+ /// 三值字典。 + ///
+ public TripleDictionary() + { + _dictionary = new ConcurrentDictionary, TValue>(); + } + + /// + /// Add an element, only if the key for the combination does not exist.
+ /// 添加元素,只有当该组合的键不存在时才添加。 + ///
+ /// Data type.
数据类型。 + /// Key.
键。 + /// Value.
值。 + /// Whether the value is added successfully.
是否成功添加。
+ public bool Add(string type, string key, TValue value) + { + var keyTuple = new Tuple(type, key); + + // 检查是否已经存在相同的键组合 + if (_dictionary.ContainsKey(keyTuple)) + { + return false; // 如果已存在相同的键组合,返回 false 表示添加失败 + } + + // 如果没有该键组合,执行添加操作 + _dictionary[keyTuple] = value; + return true; // 添加成功 + } + + /// + /// Only values are updated, types and keys cannot be updated.
+ /// 只更新值,不能更新类型和键。 + ///
+ /// Data type.
数据类型。 + /// Key.
键。 + /// New value.
新值。 + /// Whether the update is successful.
是否成功更新。
+ public bool UpdateValue(string type, string key, TValue newValue) + { + var keyTuple = new Tuple(type, key); + + // 检查该键组合是否存在 + if (!_dictionary.ContainsKey(keyTuple)) + { + return false; // 如果不存在,返回 false + } + + // 如果存在,更新值 + _dictionary[keyTuple] = newValue; + return true; // 更新成功 + } + + /// + /// Get the element.
+ /// 获取元素。 + ///
+ /// Data type.
数据类型。 + /// Key.
键。 + /// Value.
值。
+ /// The specified key was not found.
未找到指定的键。
+ public TValue Get(string type, string key) + { + var keyTuple = new Tuple(type, key); + if (_dictionary.TryGetValue(keyTuple, out TValue value)) + { + return value; + } + else + { + throw new TripleDictionaryKeyNotFoundException("The key combination was not found."); + } + } + + /// + /// Checks whether the specified type and key combination are included.
+ /// 检查是否包含指定的类型和键组合。 + ///
+ /// Data type.
数据类型。 + /// Key.
键。 + /// Whether the key is included.
是否包含该键。
+ public bool ContainsKey(string type, string key) => _dictionary.ContainsKey(new Tuple(type, key)); + + /// + /// Deleting a certain type removes all the key values of the class.
+ /// 删除某个类型会删除该类型下的所有键值对。 + ///
+ /// Data type.
数据类型。 + /// Whether the data type is removed successfully.
是否成功移除该数据类型。
+ public bool RemoveType(string type) + { + var keysToRemove = _dictionary.Keys.Where(k => k.Item1 == type).ToList(); + + if (keysToRemove.Count == 0) + { + return false; // 如果该类型没有键值对,返回 false + } + + foreach (var key in keysToRemove) + { + _dictionary.TryRemove(key, out _); + } + return true; + } + + /// + /// Deletes the specified key and corresponding value from the specified type.
+ /// 删除指定类型中的指定键和对应的值。 + ///
+ /// Data type.
数据类型。 + /// Key.
键。 + /// Whether the key is successfully removed.
是否成功移除该键。
+ public bool RemoveKey(string type, string key) + { + var keyTuple = new Tuple(type, key); + + // 尝试移除指定的键值对 + if (_dictionary.TryRemove(keyTuple, out _)) + { + return true; // 删除成功 + } + else + { + return false; // 如果没有找到该键组合,返回 false + } + } + + /// + /// Removes the specified key and corresponding value from all types.
+ /// 删除所有类型中的指定键和对应的值。 + ///
+ /// Key.
键。 + /// Whether the key is successfully removed.
是否成功移除该键。
+ public bool RemoveKey(string key) + { + bool removed = false; + + // 查找所有包含该 Key 的项,并删除它们 + var keysToRemove = _dictionary.Where(entry => entry.Key.Item2 == key) + .Select(entry => entry.Key) + .ToList(); + + foreach (var keyTuple in keysToRemove) + { + if (_dictionary.TryRemove(keyTuple, out _)) + { + removed = true; + } + } + + return removed; // 如果至少删除了一个键值对,返回 true + } + + /// + /// Prints all types of key-value pairs.
+ /// 打印所有类型的键值对。 + ///
+ public void PrintAll() + { + foreach (var entry in _dictionary) + { + Console.WriteLine($"Type: {entry.Key.Item1}, Key: {entry.Key.Item2} -> Value: {entry.Value}"); + } + } + } +#endif +} diff --git a/TYLDDB/TYLDDB.csproj b/TYLDDB/TYLDDB.csproj index 2f2e800..d78d8fa 100644 --- a/TYLDDB/TYLDDB.csproj +++ b/TYLDDB/TYLDDB.csproj @@ -1,7 +1,7 @@ - + - netstandard2.1;net6.0;net8.0;net9.0 + 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 @@ -21,7 +21,7 @@ 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 pipe-based file read and write classes for testing. +- Add a three-value dictionary (under test). From f5971af7abb327295474398089e71b891cd394bc Mon Sep 17 00:00:00 2001 From: Grey Wind Date: Sat, 18 Jan 2025 00:13:59 +0800 Subject: [PATCH 11/11] style: add region --- TYLDDB/TYLDDB.cs | 6 ++++-- TYLDDB/TYLDDB_Instantiation.cs | 9 ++++++--- TYLDDB/TYLDDB_Parse.cs | 6 ++++-- TYLDDB/TYLDDB_PrivateField.cs | 3 ++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/TYLDDB/TYLDDB.cs b/TYLDDB/TYLDDB.cs index fae863d..93c1007 100644 --- a/TYLDDB/TYLDDB.cs +++ b/TYLDDB/TYLDDB.cs @@ -11,7 +11,7 @@ namespace TYLDDB ///
public partial class LDDB { - ///////////////////////////////////////////////////// 公开字段 + #region 公开字段 /// /// Set the path where you want to read the file
/// 设置希望读取文件的路径 @@ -36,8 +36,9 @@ public string FilePath /// 当前文件内所有数据库的名称 ///
public List AllDatabaseName; + #endregion - ///////////////////////////////////////////////////// 方法 + #region 方法 /// /// 验证文件路径是否为null或空 @@ -148,5 +149,6 @@ public string[] AllTypeSearchFromSemaphoreThreadLock(string key) return resultArray; } + #endregion } } diff --git a/TYLDDB/TYLDDB_Instantiation.cs b/TYLDDB/TYLDDB_Instantiation.cs index db93b6b..8b6abb1 100644 --- a/TYLDDB/TYLDDB_Instantiation.cs +++ b/TYLDDB/TYLDDB_Instantiation.cs @@ -12,11 +12,12 @@ public partial class LDDB /// public LDDB() { - // 实例化数据库操作类 + #region 实例化数据库操作类 database_v1 = new Database_V1(); database_v2 = new Database_V2(); + #endregion - // 实例化并发词典 + #region 实例化并发词典 cdStringDictionary = new CdStringDictionary(); cdShortDictionary = new CdShortDictionary(); cdLongDictionary = new CdLongDictionary(); @@ -26,8 +27,9 @@ public LDDB() cdDecimalDictionary = new CdDecimalDictionary(); cdCharDictionary = new CdCharDictionary(); cdBooleanDictionary = new CdBooleanDictionary(); + #endregion - // 实例化信号词典 + #region 实例化信号词典 stlStringDictionary = new StlStringDictionary(); stlShortDictionary = new StlShortDictionary(); stlLongDictionary = new StlLongDictionary(); @@ -37,6 +39,7 @@ public LDDB() stlDecimalDictionary = new StlDecimalDictionary(); stlCharDictionary = new StlCharDictionary(); stlBooleanDictionary = new StlBooleanDictionary(); + #endregion } } } diff --git a/TYLDDB/TYLDDB_Parse.cs b/TYLDDB/TYLDDB_Parse.cs index c5d9329..876e2c6 100644 --- a/TYLDDB/TYLDDB_Parse.cs +++ b/TYLDDB/TYLDDB_Parse.cs @@ -13,7 +13,7 @@ public async Task Parse_V1() { // 创建多个任务,并使用 LongRunning 来确保每个任务在独立线程中运行 - // ConcurrentDictionary + #region 并发数据库任务 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,8 +23,9 @@ 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 - // SemaphoreThreadLock + #region 信号量数据库任务 Task stlStringCacheTask = Task.Factory.StartNew(() => StlString(), TaskCreationOptions.LongRunning); Task stlIntCacheTask = Task.Factory.StartNew(() => StlInt(), TaskCreationOptions.LongRunning); Task stlShortCacheTask = Task.Factory.StartNew(() => StlShort(), TaskCreationOptions.LongRunning); @@ -34,6 +35,7 @@ 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, diff --git a/TYLDDB/TYLDDB_PrivateField.cs b/TYLDDB/TYLDDB_PrivateField.cs index 9117b8e..52254b2 100644 --- a/TYLDDB/TYLDDB_PrivateField.cs +++ b/TYLDDB/TYLDDB_PrivateField.cs @@ -6,7 +6,7 @@ namespace TYLDDB { public partial class LDDB { - ///////////////////////////////////////////////////// 私有字段 + #region 私有字段 private string _filePath; // 存储文件路径 private string _fileContent; // 存储文件内容 private string _databaseContent; // 存储数据库内容 @@ -30,5 +30,6 @@ public partial class LDDB private StlDecimalDictionary stlDecimalDictionary; private StlCharDictionary stlCharDictionary; private StlBooleanDictionary stlBooleanDictionary; + #endregion } }