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
}
public abstract class AFileOrDir(
string path,
DirOrFile type = DirOrFile.File,
NextOpType? nextOp = null
)
public abstract class AFileOrDir
{
public DirOrFile Type { get; set; } = type;
public NextOpType? NextOp { get; set; } = nextOp;
public DirOrFile Type { get; set; }
public NextOpType? NextOp { get; set; }
// private string Path = path;
/// <summary>
/// 全部为绝对路径... 占用资源会大一点但是完全OK
/// </summary>
///
private string Path = path;
public required string Path { get; set; }
/// <summary>
/// 相当于Path 包装天杀的windows在路径字符串中使用两种分隔符“/” 和“\”,这导致,即使两个字符串不相同,也可能是同一个路径。现在把它们统一起来
@ -55,10 +51,13 @@ public abstract class AFileOrDir(
/// </summary>
/// <param name="path">绝对路径</param>
/// <param name="mtime">文件的修改时间</param>/
public class File(string path, DateTime mtime, NextOpType? nextOp = null)
: AFileOrDir(path, DirOrFile.File, nextOp)
public class File : AFileOrDir
{
public DateTime MTime { get; set; } = mtime;
public File()
{
Type = DirOrFile.File;
}
public required DateTime MTime { get; set; }
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 =>
{
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);
});
//将配置信息发送到remoteServer

View file

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

View file

@ -10,168 +10,263 @@ public class FilesSeed : IDisposable
// string TestPath = Path.Combine(Directory.GetCurrentDirectory(), "../../..");
DateTime NewTime = DateTime.Now.AddSeconds(-99);
DateTime OldTime = NewTime.AddSeconds(-20);
NewDir = new Dir(
TestPath + "/NewDir",
NewDir = new Dir
{
Path = TestPath + "/NewDir",
Children =
[
new Dir($"{TestPath}/NewDir/0"),
new Dir(
$"{TestPath}/NewDir/1",
[new Common.File($"{TestPath}/NewDir/1/1.txt", NewTime)]
),
new Dir(
$"{TestPath}/NewDir/2",
new Dir { Path = $"{TestPath}/NewDir/0", Children = [] },
new Dir
{
Path = $"{TestPath}/NewDir/1",
Children =
[
new Common.File($"{TestPath}/NewDir/2/2.txt", 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 Common.File { Path = $"{TestPath}/NewDir/1/1.txt", MTime = NewTime }
]
),
new Dir(
$"{TestPath}/NewDir/2/2_2",
},
new Dir
{
Path = $"{TestPath}/NewDir/2",
Children =
[
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 { Path = $"{TestPath}/NewDir/2/2.txt", MTime = NewTime },
new Dir
{
Path = $"{TestPath}/NewDir/2/2_1",
Children =
[
new Common.File(
$"{TestPath}/NewDir/2/2_2/2_3/1.txt",
NewTime
),
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
},
]
),
]
)
]
),
]
);
DiffDir = new Dir(
$"{TestPath}/OldDir",
},
new Dir
{
Path = $"{TestPath}/NewDir/2/2_2",
Children =
[
new Dir(
$"{TestPath}/OldDir/1",
[new Common.File($"{TestPath}/OldDir/1/2_D.txt", NewTime, NextOpType.Del),]
),
new Dir(
$"{TestPath}/OldDir/2",
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
{
Path = $"{TestPath}/OldDir",
Children =
[
new Dir
{
Path = $"{TestPath}/OldDir/1",
Children =
[
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 Dir(
$"{TestPath}/OldDir/2/2_1",
new Common.File
{
Path = $"{TestPath}/OldDir/2/2.txt",
MTime = NewTime,
NextOp = NextOpType.Add
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_1",
Children =
[
new Common.File(
$"{TestPath}/OldDir/2/2_1/2.txt",
NewTime,
NextOpType.Modify
),
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_1/2.txt",
MTime = NewTime,
NextOp = NextOpType.Modify
},
]
),
new Dir(
$"{TestPath}/OldDir/2/2_2_M",
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2_M",
Children =
[
new Common.File(
$"{TestPath}/OldDir/2/2_2_M/1.txt",
OldTime,
NextOpType.Del
),
new Common.File(
$"{TestPath}/OldDir/2/2_2_M/2.txt",
OldTime,
NextOpType.Del
),
new Dir(
$"{TestPath}/OldDir/2/2_2_M/2_3",
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2_M/1.txt",
MTime = OldTime,
NextOp = NextOpType.Del
},
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2_M/2.txt",
MTime = OldTime,
NextOp = NextOpType.Del
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2_M/2_3",
Children =
[
new Common.File(
$"{TestPath}/OldDir/2/2_2_M/2_3/1.txt",
OldTime,
NextOpType.Del
),
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2_M/2_3/1.txt",
MTime = OldTime,
NextOp = NextOpType.Del
},
],
NextOpType.Del
),
NextOp = NextOpType.Del
},
],
NextOpType.Del
),
new Dir(
$"{TestPath}/OldDir/2/2_2",
NextOp = NextOpType.Del
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2",
Children =
[
new Common.File(
$"{TestPath}/OldDir/2/2_2/1.txt",
NewTime,
NextOpType.Add
),
new Common.File(
$"{TestPath}/OldDir/2/2_2/2.txt",
NewTime,
NextOpType.Add
),
new Dir(
$"{TestPath}/OldDir/2/2_2/2_3",
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2/1.txt",
MTime = NewTime,
NextOp = NextOpType.Add
},
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2/2.txt",
MTime = NewTime,
NextOp = NextOpType.Add
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2/2_3",
Children =
[
new Common.File(
$"{TestPath}/OldDir/2/2_2/2_3/1.txt",
NewTime,
NextOpType.Add
),
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2/2_3/1.txt",
MTime = NewTime,
NextOp = NextOpType.Add
},
],
NextOpType.Add
),
NextOp = NextOpType.Add
},
],
NextOpType.Add
)
NextOp = NextOpType.Add
}
]
),
},
]
);
OldDir = new Dir(
$"{TestPath}/OldDir",
};
OldDir = new Dir
{
Path = $"{TestPath}/OldDir",
Children =
[
new Dir($"{TestPath}/OldDir/0"),
new Dir(
$"{TestPath}/OldDir/1",
new Dir { Path = $"{TestPath}/OldDir/0", Children = [] },
new Dir
{
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(
$"{TestPath}/OldDir/2",
},
new Dir
{
Path = $"{TestPath}/OldDir/2",
Children =
[
new Dir(
$"{TestPath}/OldDir/2/2_1",
new Dir
{
Path = $"{TestPath}/OldDir/2/2_1",
Children =
[
new Common.File($"{TestPath}/OldDir/2/2_1/1.txt", NewTime),
new Common.File($"{TestPath}/OldDir/2/2_1/2.txt", OldTime),
new Common.File
{
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(
$"{TestPath}/OldDir/2/2_2_M",
},
new Dir
{
Path = $"{TestPath}/OldDir/2/2_2_M",
Children =
[
new Common.File($"{TestPath}/OldDir/2/2_2_M/1.txt", OldTime),
new Common.File($"{TestPath}/OldDir/2/2_2_M/2.txt", OldTime),
new Dir(
$"{TestPath}/OldDir/2/2_2_M/2_3",
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2_M/1.txt",
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(
$"{TestPath}/OldDir/2/2_2_M/2_3/1.txt",
OldTime
),
new Common.File
{
Path = $"{TestPath}/OldDir/2/2_2_M/2_3/1.txt",
MTime = OldTime
},
]
),
},
]
)
}
]
),
},
]
);
};
fileDirOp = new SimpleFileDirOp();
}

View file

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