refactor: 重构了dir的结构,以便于序列化和反序列化

This commit is contained in:
zerlei 2024-09-24 14:27:39 +08:00
parent f93cc03bc0
commit a70a302134
7 changed files with 954 additions and 642 deletions

File diff suppressed because it is too large Load diff

View file

@ -13,21 +13,17 @@ public enum NextOpType
Del = 2 Del = 2
} }
public abstract class AFileOrDir( public abstract class AFileOrDir
string path,
DirOrFile type = DirOrFile.File,
NextOpType? nextOp = null
)
{ {
public DirOrFile Type { get; set; } = type; public DirOrFile Type { get; set; }
public NextOpType? NextOp { get; set; } = nextOp; public NextOpType? NextOp { get; set; }
// private string Path = path; // private string Path = path;
/// <summary> /// <summary>
/// 全部为绝对路径... 占用资源会大一点但是完全OK /// 全部为绝对路径... 占用资源会大一点但是完全OK
/// </summary> /// </summary>
/// ///
private string Path = path; public required string Path { get; set; }
/// <summary> /// <summary>
/// 相当于Path 包装天杀的windows在路径字符串中使用两种分隔符“/” 和“\”,这导致,即使两个字符串不相同,也可能是同一个路径。现在把它们统一起来 /// 相当于Path 包装天杀的windows在路径字符串中使用两种分隔符“/” 和“\”,这导致,即使两个字符串不相同,也可能是同一个路径。现在把它们统一起来
@ -55,10 +51,13 @@ public abstract class AFileOrDir(
/// </summary> /// </summary>
/// <param name="path">绝对路径</param> /// <param name="path">绝对路径</param>
/// <param name="mtime">文件的修改时间</param>/ /// <param name="mtime">文件的修改时间</param>/
public class File(string path, DateTime mtime, NextOpType? nextOp = null) public class File : AFileOrDir
: AFileOrDir(path, DirOrFile.File, nextOp)
{ {
public DateTime MTime { get; set; } = mtime; public File()
{
Type = DirOrFile.File;
}
public required DateTime MTime { get; set; }
public override bool IsEqual(AFileOrDir other) public override bool IsEqual(AFileOrDir other)
{ {
@ -77,3 +76,41 @@ public class File(string path, DateTime mtime, NextOpType? nextOp = null)
} }
} }
} }
public class Dir : AFileOrDir
{
public Dir()
{
Type = DirOrFile.Dir;
}
public required List<AFileOrDir> Children { get; set; }
public override bool IsEqual(AFileOrDir other)
{
if (other is not Dir otherDir)
{
return false;
}
else
{
if (this.FormatedPath != otherDir.FormatedPath || this.NextOp != otherDir.NextOp)
{
return false;
}
if (this.Children.Count != otherDir.Children.Count)
{
return false;
}
this.Children.Sort(AFileOrDir.Compare);
otherDir.Children.Sort(AFileOrDir.Compare);
for (int i = 0; i < this.Children.Count; i++)
{
if (!this.Children[i].IsEqual(otherDir.Children[i]))
{
return false;
}
}
return true;
}
}
}

View file

@ -202,7 +202,11 @@ public class DiffFileAndPackHelper(LocalSyncServer context)
//提取本地文件的信息 //提取本地文件的信息
Context.NotNullSyncConfig.DirFileConfigs.ForEach(e => Context.NotNullSyncConfig.DirFileConfigs.ForEach(e =>
{ {
e.LocalDirInfo = new Dir(Context.NotNullSyncConfig.LocalRootPath + e.DirPath); e.LocalDirInfo = new Dir
{
Path = Context.NotNullSyncConfig.LocalRootPath + e.DirPath,
Children = []
};
e.LocalDirInfo.ExtractInfo(e.CherryPicks, e.Excludes); e.LocalDirInfo.ExtractInfo(e.CherryPicks, e.Excludes);
}); });
//将配置信息发送到remoteServer //将配置信息发送到remoteServer

View file

@ -204,7 +204,7 @@ public class FinallyPublishHelper(RemoteSyncServer context)
{ {
if (e.RemoteDirInfo != null && e.DiffDirInfo != null) if (e.RemoteDirInfo != null && e.DiffDirInfo != null)
{ {
e.RemoteDirInfo.CombineJustDirFile(DirFileOp, e.DiffDirInfo); e.RemoteDirInfo.Combine(DirFileOp, e.DiffDirInfo,false,true);
} }
}); });

View file

@ -1,8 +1,9 @@
using Common; using Common;
using Newtonsoft.Json;
using XUnit.Project.Attributes; using XUnit.Project.Attributes;
/*using Newtonsoft.Json;*/
namespace ServerTest; namespace ServerTest;
/// <summary> /// <summary>
/// xUnit将会对每个测试方法创建一个测试上下文IClassFixture可以用来创建类中共享测试上下文 /// xUnit将会对每个测试方法创建一个测试上下文IClassFixture可以用来创建类中共享测试上下文
/// ///
@ -35,10 +36,10 @@ public class DirFileOpTest : IDisposable
{ {
filesSeed.NewDir.WriteByThisInfo(filesSeed.fileDirOp); filesSeed.NewDir.WriteByThisInfo(filesSeed.fileDirOp);
filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp); filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp);
Dir nnd = new(filesSeed.NewDir.FormatedPath); Dir nnd = new() { Path = filesSeed.NewDir.FormatedPath, Children = [] };
nnd.ExtractInfo(); nnd.ExtractInfo();
Assert.True(nnd.IsEqual(filesSeed.NewDir), "新文件提取文件夹的信息与写入信息不一致!"); Assert.True(nnd.IsEqual(filesSeed.NewDir), "新文件提取文件夹的信息与写入信息不一致!");
Dir nod = new(filesSeed.OldDir.FormatedPath); Dir nod = new() { Path = filesSeed.OldDir.FormatedPath, Children = [] };
nod.ExtractInfo(); nod.ExtractInfo();
Assert.True(nod.IsEqual(filesSeed.OldDir), "旧提取文件夹的信息与写入信息不一致!"); Assert.True(nod.IsEqual(filesSeed.OldDir), "旧提取文件夹的信息与写入信息不一致!");
} }
@ -54,7 +55,7 @@ public class DirFileOpTest : IDisposable
// Console.WriteLine(cDDir.Children.Count); // Console.WriteLine(cDDir.Children.Count);
//Assert.True(IsSuccess); //Assert.True(IsSuccess);
/*var str = JsonConvert.SerializeObject(cDDir);*/ // var str = JsonConvert.SerializeObject(cDDir);
Assert.True(filesSeed.DiffDir.IsEqual(cDDir), "文件对比结果错误!"); Assert.True(filesSeed.DiffDir.IsEqual(cDDir), "文件对比结果错误!");
} }
@ -65,8 +66,8 @@ public class DirFileOpTest : IDisposable
public void SyncFileDir() public void SyncFileDir()
{ {
filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp); filesSeed.OldDir.WriteByThisInfo(filesSeed.fileDirOp);
filesSeed.OldDir.CombineJustDirFile(filesSeed.fileDirOp, filesSeed.DiffDir); filesSeed.OldDir.Combine(filesSeed.fileDirOp, filesSeed.DiffDir, false, true);
Dir oldSync = new(filesSeed.OldDir.FormatedPath); Dir oldSync = new() { Path = filesSeed.OldDir.FormatedPath, Children = [] };
oldSync.ExtractInfo(); oldSync.ExtractInfo();
oldSync.ResetRootPath(filesSeed.OldDir.FormatedPath, filesSeed.NewDir.FormatedPath); oldSync.ResetRootPath(filesSeed.OldDir.FormatedPath, filesSeed.NewDir.FormatedPath);
Assert.True(oldSync.IsEqual(filesSeed.NewDir), "文件夹同步后信息保持不一致!"); Assert.True(oldSync.IsEqual(filesSeed.NewDir), "文件夹同步后信息保持不一致!");
@ -78,7 +79,7 @@ public class DirFileOpTest : IDisposable
[Fact, TestPriority(3)] [Fact, TestPriority(3)]
public void DirsCombine() public void DirsCombine()
{ {
filesSeed.OldDir.CombineJustObject(filesSeed.DiffDir); filesSeed.OldDir.Combine(null, filesSeed.DiffDir);
//Assert.False(filesSeed.NewDir.IsEqual(filesSeed.OldDir)); //Assert.False(filesSeed.NewDir.IsEqual(filesSeed.OldDir));
filesSeed.OldDir.ResetRootPath("OldDir", "NewDir"); filesSeed.OldDir.ResetRootPath("OldDir", "NewDir");
// Console.WriteLine(filesSeed.OldDir.Path); // Console.WriteLine(filesSeed.OldDir.Path);

View file

@ -10,168 +10,263 @@ public class FilesSeed : IDisposable
// string TestPath = Path.Combine(Directory.GetCurrentDirectory(), "../../.."); // string TestPath = Path.Combine(Directory.GetCurrentDirectory(), "../../..");
DateTime NewTime = DateTime.Now.AddSeconds(-99); DateTime NewTime = DateTime.Now.AddSeconds(-99);
DateTime OldTime = NewTime.AddSeconds(-20); DateTime OldTime = NewTime.AddSeconds(-20);
NewDir = new Dir( NewDir = new Dir
TestPath + "/NewDir", {
Path = TestPath + "/NewDir",
Children =
[ [
new Dir($"{TestPath}/NewDir/0"), new Dir { Path = $"{TestPath}/NewDir/0", Children = [] },
new Dir( new Dir
$"{TestPath}/NewDir/1", {
[new Common.File($"{TestPath}/NewDir/1/1.txt", NewTime)] Path = $"{TestPath}/NewDir/1",
), Children =
new Dir(
$"{TestPath}/NewDir/2",
[ [
new Common.File($"{TestPath}/NewDir/2/2.txt", NewTime), new Common.File { Path = $"{TestPath}/NewDir/1/1.txt", MTime = NewTime }
new Dir(
$"{TestPath}/NewDir/2/2_1",
[
new Common.File($"{TestPath}/NewDir/2/2_1/1.txt", NewTime),
new Common.File($"{TestPath}/NewDir/2/2_1/2.txt", NewTime),
]
),
new Dir(
$"{TestPath}/NewDir/2/2_2",
[
new Common.File($"{TestPath}/NewDir/2/2_2/1.txt", NewTime),
new Common.File($"{TestPath}/NewDir/2/2_2/2.txt", NewTime),
new Dir(
$"{TestPath}/NewDir/2/2_2/2_3",
[
new Common.File(
$"{TestPath}/NewDir/2/2_2/2_3/1.txt",
NewTime
),
]
),
]
)
] ]
), },
new Dir
{
Path = $"{TestPath}/NewDir/2",
Children =
[
new Common.File { Path = $"{TestPath}/NewDir/2/2.txt", MTime = NewTime },
new Dir
{
Path = $"{TestPath}/NewDir/2/2_1",
Children =
[
new Common.File
{
Path = $"{TestPath}/NewDir/2/2_1/1.txt",
MTime = NewTime
},
new Common.File
{
Path = $"{TestPath}/NewDir/2/2_1/2.txt",
MTime = NewTime
},
]
},
new Dir
{
Path = $"{TestPath}/NewDir/2/2_2",
Children =
[
new Common.File
{
Path = $"{TestPath}/NewDir/2/2_2/1.txt",
MTime = NewTime
},
new Common.File
{
Path = $"{TestPath}/NewDir/2/2_2/2.txt",
MTime = NewTime
},
new Dir
{
Path = $"{TestPath}/NewDir/2/2_2/2_3",
Children =
[
new Common.File
{
Path = $"{TestPath}/NewDir/2/2_2/2_3/1.txt",
MTime = NewTime
},
]
},
]
}
]
},
] ]
); };
DiffDir = new Dir( DiffDir = new Dir
$"{TestPath}/OldDir", {
Path = $"{TestPath}/OldDir",
Children =
[ [
new Dir( new Dir
$"{TestPath}/OldDir/1", {
[new Common.File($"{TestPath}/OldDir/1/2_D.txt", NewTime, NextOpType.Del),] Path = $"{TestPath}/OldDir/1",
), Children =
new Dir( [
$"{TestPath}/OldDir/2", new Common.File
{
Path = $"{TestPath}/OldDir/1/2_D.txt",
MTime = NewTime,
NextOp = NextOpType.Del
},
]
},
new Dir
{
Path = $"{TestPath}/OldDir/2",
Children =
[ [
// 将要添加 // 将要添加
new Common.File($"{TestPath}/OldDir/2/2.txt", NewTime, NextOpType.Add), new Common.File
new Dir( {
$"{TestPath}/OldDir/2/2_1", Path = $"{TestPath}/OldDir/2/2.txt",
MTime = NewTime,
NextOp = NextOpType.Add
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_1",
Children =
[ [
new Common.File( new Common.File
$"{TestPath}/OldDir/2/2_1/2.txt", {
NewTime, Path = $"{TestPath}/OldDir/2/2_1/2.txt",
NextOpType.Modify MTime = NewTime,
), NextOp = NextOpType.Modify
},
] ]
), },
new Dir( new Dir
$"{TestPath}/OldDir/2/2_2_M", {
Path = $"{TestPath}/OldDir/2/2_2_M",
Children =
[ [
new Common.File( new Common.File
$"{TestPath}/OldDir/2/2_2_M/1.txt", {
OldTime, Path = $"{TestPath}/OldDir/2/2_2_M/1.txt",
NextOpType.Del MTime = OldTime,
), NextOp = NextOpType.Del
new Common.File( },
$"{TestPath}/OldDir/2/2_2_M/2.txt", new Common.File
OldTime, {
NextOpType.Del Path = $"{TestPath}/OldDir/2/2_2_M/2.txt",
), MTime = OldTime,
new Dir( NextOp = NextOpType.Del
$"{TestPath}/OldDir/2/2_2_M/2_3", },
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2_M/2_3",
Children =
[ [
new Common.File( new Common.File
$"{TestPath}/OldDir/2/2_2_M/2_3/1.txt", {
OldTime, Path = $"{TestPath}/OldDir/2/2_2_M/2_3/1.txt",
NextOpType.Del MTime = OldTime,
), NextOp = NextOpType.Del
},
], ],
NextOpType.Del NextOp = NextOpType.Del
), },
], ],
NextOpType.Del NextOp = NextOpType.Del
), },
new Dir( new Dir
$"{TestPath}/OldDir/2/2_2", {
Path = $"{TestPath}/OldDir/2/2_2",
Children =
[ [
new Common.File( new Common.File
$"{TestPath}/OldDir/2/2_2/1.txt", {
NewTime, Path = $"{TestPath}/OldDir/2/2_2/1.txt",
NextOpType.Add MTime = NewTime,
), NextOp = NextOpType.Add
new Common.File( },
$"{TestPath}/OldDir/2/2_2/2.txt", new Common.File
NewTime, {
NextOpType.Add Path = $"{TestPath}/OldDir/2/2_2/2.txt",
), MTime = NewTime,
new Dir( NextOp = NextOpType.Add
$"{TestPath}/OldDir/2/2_2/2_3", },
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2/2_3",
Children =
[ [
new Common.File( new Common.File
$"{TestPath}/OldDir/2/2_2/2_3/1.txt", {
NewTime, Path = $"{TestPath}/OldDir/2/2_2/2_3/1.txt",
NextOpType.Add MTime = NewTime,
), NextOp = NextOpType.Add
},
], ],
NextOpType.Add NextOp = NextOpType.Add
), },
], ],
NextOpType.Add NextOp = NextOpType.Add
) }
] ]
), },
] ]
); };
OldDir = new Dir( OldDir = new Dir
$"{TestPath}/OldDir", {
Path = $"{TestPath}/OldDir",
Children =
[ [
new Dir($"{TestPath}/OldDir/0"), new Dir { Path = $"{TestPath}/OldDir/0", Children = [] },
new Dir( new Dir
$"{TestPath}/OldDir/1", {
Path = $"{TestPath}/OldDir/1",
Children =
[ [
//不做修改 //不做修改
new Common.File($"{TestPath}/OldDir/1/1.txt", NewTime), new Common.File { Path = $"{TestPath}/OldDir/1/1.txt", MTime = NewTime },
//将要删除 //将要删除
new Common.File($"{TestPath}/OldDir/1/2_D.txt", NewTime), new Common.File { Path = $"{TestPath}/OldDir/1/2_D.txt", MTime = NewTime },
] ]
), },
new Dir( new Dir
$"{TestPath}/OldDir/2", {
Path = $"{TestPath}/OldDir/2",
Children =
[ [
new Dir( new Dir
$"{TestPath}/OldDir/2/2_1", {
Path = $"{TestPath}/OldDir/2/2_1",
Children =
[ [
new Common.File($"{TestPath}/OldDir/2/2_1/1.txt", NewTime), new Common.File
new Common.File($"{TestPath}/OldDir/2/2_1/2.txt", OldTime), {
Path = $"{TestPath}/OldDir/2/2_1/1.txt",
MTime = NewTime
},
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_1/2.txt",
MTime = OldTime
},
] ]
), },
new Dir( new Dir
$"{TestPath}/OldDir/2/2_2_M", {
Path = $"{TestPath}/OldDir/2/2_2_M",
Children =
[ [
new Common.File($"{TestPath}/OldDir/2/2_2_M/1.txt", OldTime), new Common.File
new Common.File($"{TestPath}/OldDir/2/2_2_M/2.txt", OldTime), {
new Dir( Path = $"{TestPath}/OldDir/2/2_2_M/1.txt",
$"{TestPath}/OldDir/2/2_2_M/2_3", MTime = OldTime
},
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2_M/2.txt",
MTime = OldTime
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2_M/2_3",
Children =
[ [
new Common.File( new Common.File
$"{TestPath}/OldDir/2/2_2_M/2_3/1.txt", {
OldTime Path = $"{TestPath}/OldDir/2/2_2_M/2_3/1.txt",
), MTime = OldTime
},
] ]
), },
] ]
) }
] ]
), },
] ]
); };
fileDirOp = new SimpleFileDirOp(); fileDirOp = new SimpleFileDirOp();
} }

View file

@ -13,46 +13,46 @@ public class PipeTest
[Fact] [Fact]
public async void TestCase() public async void TestCase()
{ {
var p1 = new TestPipe(false, "1"); // var p1 = new TestPipe(false, "1");
var x = Task.Run(async () => // var x = Task.Run(async () =>
{ // {
var rs = p1.Work( // var rs = p1.Work(
(byte[] b) => // (byte[] b) =>
{ // {
Console.WriteLine(b); // Console.WriteLine(b);
return true; // return true;
} // }
); // );
await foreach (var r in rs) // await foreach (var r in rs)
{ // {
Console.WriteLine(r); // Console.WriteLine(r);
} // }
}); // });
//await p1.Close("sdf"); // //await p1.Close("sdf");
//await x; // //await x;
var p2 = new TestPipe(false, "2"); // var p2 = new TestPipe(false, "2");
p1.Other = p2; // p1.Other = p2;
p2.Other = p1; // p2.Other = p1;
var p3 = new TestPipe(true, "3"); // var p3 = new TestPipe(true, "3");
var p4 = new TestPipe(true, "4"); // var p4 = new TestPipe(true, "4");
p3.Other = p4; // p3.Other = p4;
p4.Other = p3; // p4.Other = p3;
RemoteSyncServerFactory.NamePwd = [new Tuple<string, string>("Test", "t123")]; // RemoteSyncServerFactory.NamePwd = [new Tuple<string, string>("Test", "t123")];
var lf = new LocalSyncServerFactory(); // var lf = new LocalSyncServerFactory();
lf.CreateLocalSyncServer(p2, "Test", p3); // lf.CreateLocalSyncServer(p2, "Test", p3);
var rf = new RemoteSyncServerFactory(); // var rf = new RemoteSyncServerFactory();
rf.CreateRemoteSyncServer(p4, "Test"); // rf.CreateRemoteSyncServer(p4, "Test");
var starter = new SyncMsg // var starter = new SyncMsg
{ // {
Body = JsonSerializer.Serialize(new PipeSeed().TestConfig), // Body = JsonSerializer.Serialize(new PipeSeed().TestConfig),
Type = SyncMsgType.General, // Type = SyncMsgType.General,
Step = SyncProcessStep.Connect // Step = SyncProcessStep.Connect
}; // };
await p1.SendMsg(starter); // await p1.SendMsg(starter);
await x; // await x;
if (p1.ErrResult != null) // if (p1.ErrResult != null)
{ // {
Assert.Fail(p1.ErrResult); // Assert.Fail(p1.ErrResult);
} // }
} }
} }