Skip to content

Commit 05b02cc

Browse files
committed
💾 Feat(SyncCodes): Better comparison of FileItem
1 parent de64edf commit 05b02cc

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

‎Utils/SyncCodes/FileItem.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,18 @@ public FileItem(string path)
3131
FileLoaded = false;
3232
}
3333
}
34+
}
35+
36+
public class FileItemExistenceComparer : IEqualityComparer<FileItem>
37+
{
38+
public bool Equals(FileItem? x, FileItem? y) => x is not null && y is not null && x.Path.Equals(y.Path);
3439

35-
public override bool Equals(object? obj) => obj is FileItem item && FileLoaded && item.FileLoaded && Hash.Equals(item.Hash);
40+
public int GetHashCode(FileItem obj) => obj.GetHashCode();
41+
}
42+
43+
public class FileItemComparer : IEqualityComparer<FileItem>
44+
{
45+
public bool Equals(FileItem? x, FileItem? y) => x is not null && y is not null && x.FileLoaded && y.FileLoaded && x.Path.Equals(y.Path) && x.Hash.Equals(y.Hash);
3646

37-
public override int GetHashCode() => Convert.FromBase64String(Hash).GetHashCode();
47+
public int GetHashCode(FileItem obj) => obj.GetHashCode();
3848
}

‎Utils/SyncCodes/Program.cs

+19-19
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,25 @@ await catalogResponse.Content.ReadAsStringAsync()
126126

127127
var localCatalog = context.GetFiles();
128128

129-
var needToFetch = catalog.Except(localCatalog);
130-
var needToDelete = localCatalog.Except(catalog);
131-
var needToUpdate = localCatalog.GroupJoin(
132-
catalog,
133-
x => x,
134-
y => y,
135-
(x, y) => new { Original = x, New = y }
136-
)
137-
.SelectMany(
138-
c => c.New.DefaultIfEmpty(),
139-
(x, y) => new { x.Original, New = y }
140-
)
141-
.Where(c => c.Original.Hash.Equals(c.New!.Hash) == false)
142-
.Select(c => new { Original = c.Original, Difference = c.New })
143-
;
144-
145-
app.Logger.LogDebug("Need to fetch: {json}", JsonSerializer.Serialize(needToFetch));
146-
app.Logger.LogDebug("Need to delete:: {json}", JsonSerializer.Serialize(needToDelete));
147-
app.Logger.LogDebug("Need to update: {json}", JsonSerializer.Serialize(needToUpdate));
129+
var needToFetch = catalog.Except(localCatalog, new FileItemExistenceComparer());
130+
var needToDelete = localCatalog.Except(catalog, new FileItemExistenceComparer());
131+
// var needToUpdate = localCatalog.GroupJoin(
132+
// catalog,
133+
// x => x,
134+
// y => y,
135+
// (x, y) => new { Original = x, New = y }
136+
// )
137+
// .SelectMany(
138+
// c => c.New.DefaultIfEmpty(),
139+
// (x, y) => new { x.Original, New = y }
140+
// )
141+
// .Where(c => c.Original.Hash.Equals(c.New!.Hash) == false)
142+
// .Select(c => new { Original = c.Original, Difference = c.New })
143+
// ;
144+
145+
app.Logger.LogInformation("Need to fetch: {json}", JsonSerializer.Serialize(needToFetch));
146+
app.Logger.LogInformation("Need to delete:: {json}", JsonSerializer.Serialize(needToDelete));
147+
// app.Logger.LogDebug("Need to update: {json}", JsonSerializer.Serialize(needToUpdate));
148148
};
149149
timer.Start();
150150

0 commit comments

Comments
 (0)