Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TYLDDB-C#

C# version.

A strongly typed constrained distributed file system that can act as a database.
12 changes: 12 additions & 0 deletions TYLDDB-CSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TYLDDB.Test", "TYLDDB.Test\TYLDDB.Test.csproj", "{FDFD566C-CB13-43EB-970E-0EC0A21E36C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TYLDDB.Utils.FastCache.Test", "TYLDDB.Utils.FastCache.Test\TYLDDB.Utils.FastCache.Test.csproj", "{A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,10 +30,18 @@ Global
{FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDFD566C-CB13-43EB-970E-0EC0A21E36C0}.Release|Any CPU.Build.0 = Release|Any CPU
{A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FDFD566C-CB13-43EB-970E-0EC0A21E36C0} = {95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3}
{A58C0A6B-9BD4-4B29-9B5F-5FDFB849D23B} = {95BC8FAF-AB03-44A3-A7E7-8E42FA7138E3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {19390089-354C-41AD-AF7D-9FB3998A88EA}
EndGlobalSection
Expand Down
16 changes: 8 additions & 8 deletions TYLDDB.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
using TYLDDB;
using TYLDDB.Test;

string dbFilePath = "./example.lddb";

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add the 'const' modifier to 'dbFilePath'. Warning test

Add the 'const' modifier to 'dbFilePath'.

///////////////////////////////////////////////////////////////////////////////////////////////////////// 实例化
LDDB lddb = new LDDB();

///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取文件
HighPrecisionTimer readDbTimer = new(); // 从发起读取文件指令到成功读取的总时间
HighPrecisionTimer readDbTimer = new(); // 从发起读取文件到成功读取的总时间
lddb.FilePath = dbFilePath;
readDbTimer.Start();
lddb.FilePath = "./example.lddb";
lddb.ReadingFile();
readDbTimer.Stop();
WriteTime("从发起读取文件指令到成功读取的总时间为: ", readDbTimer.ElapsedMilliseconds());

///////////////////////////////////////////////////////////////////////////////////////////////////////// 读取数据库
HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库指令到成功返回读取内容的总时间
HighPrecisionTimer loadDbTimer = new(); // 从发起读取数据库到成功返回读取内容的总时间
loadDbTimer.Start();
lddb.LoadDatabase("database1");
Console.WriteLine(lddb.GetLoadingDatabaseContent()); // 输出database1内容
loadDbTimer.Stop();
WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为: ", loadDbTimer.ElapsedMilliseconds());

///////////////////////////////////////////////////////////////////////////////////////////////////////// 获取所有数据库名称
HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库指令到成功返回读取内容的总时间
HighPrecisionTimer readAllDbNameTimer = new(); // 从发起读取数据库名称到成功返回读取内容的总时间
readAllDbNameTimer.Start();
lddb.ReadAllDatabaseName();
readAllDbNameTimer.Stop();
Expand All @@ -32,7 +34,7 @@
Console.WriteLine(dbName);
}
}
WriteTime("从发起读取数据库指令到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds());
WriteTime("从发起读取数据库名称到成功返回读取内容的总时间为: ", readAllDbNameTimer.ElapsedMilliseconds());



Expand All @@ -50,9 +52,7 @@

Console.ReadLine();




////////////////////////////////////////////////////////////////////////////////////////////////////////// Test Method

///////////////////////////////////////////////////////////////////////////////////////////////////////// 工具
static void WriteTime(string what, double time)
Expand Down
44 changes: 44 additions & 0 deletions TYLDDB.Utils.FastCache.Test/HighPrecisionTimer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Diagnostics;

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'. Warning test

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'. Note test

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'.

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Remove this unnecessary 'using'. Warning test

Remove this unnecessary 'using'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'. Note test

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'.

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add or update the header of this file. Warning test

Add or update the header of this file.

namespace TYLDDB.Utils.FastCache.Test
{
internal class HighPrecisionTimer
{
private Stopwatch stopwatch;

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Make 'stopwatch' 'readonly'. Note test

Make 'stopwatch' 'readonly'.

public HighPrecisionTimer()
{
stopwatch = new Stopwatch();
}

// 启动计时器
public void Start()
{
stopwatch.Start();
}

// 停止计时器
public void Stop()
{
stopwatch.Stop();
}

// 重置计时器
public void Reset()
{
stopwatch.Reset();
}

// 获取已用时间(毫秒)
public double ElapsedMilliseconds()
{
return stopwatch.Elapsed.TotalMilliseconds;
}

// 获取已用时间(秒)
public double ElapsedSeconds()
{
return stopwatch.Elapsed.TotalSeconds;
}
}
}
76 changes: 76 additions & 0 deletions TYLDDB.Utils.FastCache.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using TYLDDB.Utils.FastCache;

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add or update the header of this file. Warning test

Add or update the header of this file.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'. Note test

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'.

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'. Warning test

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'. Note test

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'.
using TYLDDB.Utils.FastCache.Test;

SemaphoreSlimDefault();

Console.ReadLine();

// 普通线程锁的数据库读写运行
static async void SemaphoreSlimDefault()
{
var setTime = new HighPrecisionTimer();
var setAsyncTime = new HighPrecisionTimer();
var getByKeyTime = new HighPrecisionTimer();
var getByKeyAsyncTime = new HighPrecisionTimer();
var getKeysByValueTime = new HighPrecisionTimer();
var getKeysByValueAsyncTime = new HighPrecisionTimer();
var getAllCacheTime = new HighPrecisionTimer();
var getAllCacheAsyncTime = new HighPrecisionTimer();

var cache = new Cache();

setTime.Start();
cache.Set("TESTKEY1", "TESTVALUE1");
setTime.Stop();

setAsyncTime.Start();
await cache.SetAsync("TESTKEY2", "TESTVALUE2");
setAsyncTime.Stop();

getByKeyTime.Start();
Console.WriteLine("TESTKEY1对应的值:" + cache.GetByKey("TESTKEY1"));
getByKeyTime.Stop();

getByKeyAsyncTime.Start();
Console.WriteLine("TESTKEY2对应的值:" + await cache.GetByKeyAsync("TESTKEY2"));
getByKeyAsyncTime.Stop();

cache.Set("TESTKEY3", "TESTVALUE2");

Console.WriteLine("TESTVALUE2对应的所有键:");
getKeysByValueTime.Start();
Console.WriteLine(string.Join(", ", cache.GetKeysByValue("TESTVALUE2")));
getKeysByValueTime.Stop();

Console.WriteLine("TESTVALUE2对应的所有键:");
getKeysByValueAsyncTime.Start();
Console.WriteLine(string.Join(", ", await cache.GetKeysByValueAsync("TESTVALUE2")));
getKeysByValueAsyncTime.Stop();

// 获取并输出所有缓存项(同步方法)
getAllCacheTime.Start();
var allCacheSync = cache.GetAllCache();
Console.WriteLine("同步获取所有缓存项:");
foreach (var kvp in allCacheSync)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
getAllCacheTime.Stop();

// 获取并输出所有缓存项(异步方法)
getAllCacheAsyncTime.Start();
var allCacheAsync = await cache.GetAllCacheAsync();
Console.WriteLine("异步获取所有缓存项:");
foreach (var kvp in allCacheAsync)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
getAllCacheAsyncTime.Stop();


Console.WriteLine("时间消耗:");
Console.WriteLine($"设置键值(同步):{setTime.ElapsedMilliseconds()}ms 设置键值(异步):{setAsyncTime.ElapsedMilliseconds()}ms");
Console.WriteLine($"根据键获取值(同步):{getByKeyTime.ElapsedMilliseconds()}ms 根据键获取值(异步):{getByKeyAsyncTime.ElapsedMilliseconds()}ms");
Console.WriteLine($"根据值获取键(同步):{getKeysByValueTime.ElapsedMilliseconds()}ms 根据值获取键(异步):{getKeysByValueAsyncTime.ElapsedMilliseconds()}ms");
Console.WriteLine($"获取所有缓存项(同步):{getAllCacheTime.ElapsedMilliseconds()}ms 获取所有缓存项(异步):{getAllCacheAsyncTime.ElapsedMilliseconds()}ms");
}
14 changes: 14 additions & 0 deletions TYLDDB.Utils.FastCache.Test/TYLDDB.Utils.FastCache.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\TYLDDB\TYLDDB.csproj" />
</ItemGroup>

</Project>
43 changes: 0 additions & 43 deletions TYLDDB/Basic/Exception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,6 @@

namespace TYLDDB.Basic
{
#if NET8_0_OR_GREATER
/// <summary>
/// The main error module, most of them use this<br/>
/// 主要的错误模块,大部分都使用这个
/// </summary>
public class TylddbException(string message) : Exception(message) { }
/// <summary>
/// File opening failure<br/>
/// 文件打开失败
/// </summary>
public class FileOpenFailException(string message) : TylddbException(message) { }
/// <summary>
/// File read failure<br/>
/// 文件读取失败
/// </summary>
public class FileReadingFailException(string message) : TylddbException(message) { }
/// <summary>
/// File not found<br/>
/// 文件未找到
/// </summary>
public class FileNotFoundException(string message) : TylddbException(message) { }
/// <summary>
/// The file path is null or space<br/>
/// 文件路径为null或空白
/// </summary>
public class FilePathIsNullOrWhiteSpace(string message) : TylddbException(message) { }
/// <summary>
/// Incorrect or invalid data<br/>
/// 错误或无效的数据
/// </summary>
public class InvalidDataException(string message) : TylddbException(message) { }
/// <summary>
/// Database not found<br/>
/// 未找到数据库
/// </summary>
public class DatabaseNotFoundException(string message) : TylddbException(message) { }
/// <summary>
/// Description Failed to obtain the database content<br/>
/// 数据库内容获取失败
/// </summary>
public class GetDatabaseContentErrorException(string message) : TylddbException(message) { }
#else
/// <summary>
/// The main error module, most of them use this<br/>
/// 主要的错误模块,大部分都使用这个
Expand Down Expand Up @@ -140,5 +98,4 @@ public class GetDatabaseContentErrorException : TylddbException
/// </summary>
public GetDatabaseContentErrorException(string message) : base(message) { }
}
#endif
}
43 changes: 43 additions & 0 deletions TYLDDB/How-To-Use.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# How to use TYLDDB

## Basic content

### Read database

#### Instantiate the LDDB class

```c#
LDDB lddb = new LDDB();
```

If you are using a higher version .NET, you can also write a little simpler.

```c#
LDDB lddb = new();
```

#### Read file

```c#
lddb.FilePath = "./example.lddb";
lddb.ReadingFile();
```

The contents of the file are then loaded into memory.

#### Select the database to read

```c#
lddb.LoadDatabase("database1");
```

This allows you to select the contents of the database for the next operation

If you want to get all the contents of this database, you can use `lddb.GetLoadingDatabaseContent()` , which returns a string.

#### Gets all database names

```c#
lddb.ReadAllDatabaseName();
```

31 changes: 18 additions & 13 deletions TYLDDB/TYLDDB.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Add or update the header of this file. Warning

Add or update the header of this file.

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'. Warning

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'. Note

Provide a 'CLSCompliant' attribute for assembly 'srcassembly.dll'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'. Note

Provide a 'ComVisible' attribute for assembly 'srcassembly.dll'.
using System.Collections.Generic;
using TYLDDB.Basic;
using TYLDDB.Utils;

Expand All @@ -10,19 +10,25 @@
/// </summary>
public class LDDB
{
private string _filePath; // 私有字段存储文件路径
private string _fileContent; // 私有字段存储文件内容
private string _database;
private string _databaseContent;
/// <summary>
/// Instantiate the LDDB class<br />
/// 实例化LDDB类
/// </summary>
public LDDB()

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Remove this redundant constructor. Warning

Remove this redundant constructor.
{
// TODO

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Complete the task associated to this 'TODO' comment. Warning

Complete the task associated to this 'TODO' comment.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
}

///////////////////////////////////////////////////// 私有字段
private string _filePath; // 存储文件路径
private string _fileContent; // 存储文件内容
private string _database; // 存储正在访问的数据库
private string _databaseContent; // 存储数据库内容
private bool _isRead = false; // 是否已调用读取文件

#if NET6_0_OR_GREATER
private Database database = new();
#else
private event Action OnFileReadComplete;

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Refactor this delegate to use 'System.EventHandler'. Note

Refactor this delegate to use 'System.EventHandler'.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Change the signature of that event handler to match the specified signature. Note

Change the signature of that event handler to match the specified signature.

Check notice

Code scanning / Sonarscharp (reported by Codacy)

Remove the unused event 'OnFileReadComplete' or invoke it. Note

Remove the unused event 'OnFileReadComplete' or invoke it.
private Database database = new Database();
#endif

///////////////////////////////////////////////////// 公开字段
/// <summary>
/// Set the path where you want to read the file<br/>
/// 设置希望读取文件的路径
Expand All @@ -36,7 +42,6 @@
_filePath = value; // 只有通过验证后才设置值
}
}

/// <summary>
/// Names of all databases in the current file<br />
/// 当前文件内所有数据库的名称
Expand All @@ -62,7 +67,7 @@
/// </summary>
public void ReadingFile()
{
_fileContent = ReadFile.ReadTylddbFile(FilePath);
_fileContent = Reader.ReadFile(FilePath);
_isRead = true;
}

Expand Down
Loading
Loading