Skip to content

Commit fc0dd6e

Browse files
committed
💾 Feat(SyncCodes): Better method to compare differences
1 parent f9b43df commit fc0dd6e

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

‎Utils/SyncCodes/.sync-ignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
bin/
33
obj/
44

5+
.user
6+
57
# IDE files
68
.idea/

‎Utils/SyncCodes/FileItem.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public FileItem(string path)
3636

3737
public class FileItemExistenceComparer : IEqualityComparer<FileItem>
3838
{
39-
public bool Equals(FileItem? x, FileItem? y) => x is not null && y is not null && x.Path.Equals(y.Path);
39+
public bool Equals(FileItem? x, FileItem? y) => x?.Path?.Equals(y?.Path) ?? false;
4040

4141
public int GetHashCode(FileItem obj) => obj.GetHashCode();
4242
}

‎Utils/SyncCodes/Program.cs

+21-21
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void RunServer(Context context)
8181

8282
app.MapGet(
8383
"/catalog",
84-
context.GetFiles
84+
() => context.GetFiles().Where(f => f.FileLoaded)
8585
);
8686

8787
app.MapGet(
@@ -122,7 +122,11 @@ void RunClient(Context context)
122122
var catalogResponse = await client.GetAsync(catalogApi);
123123
if (!catalogResponse.IsSuccessStatusCode)
124124
{
125-
app.Logger.LogError("Failed to fetch catalog: {statusCode}", catalogResponse.StatusCode);
125+
app.Logger.LogError(
126+
"[{time}] Failed to fetch catalog: {statusCode}",
127+
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
128+
catalogResponse.StatusCode
129+
);
126130
return;
127131
}
128132

@@ -133,25 +137,21 @@ await catalogResponse.Content.ReadAsStringAsync(),
133137

134138
var localCatalog = context.GetFiles();
135139

136-
var needToFetch = catalog.Except(localCatalog, new FileItemExistenceComparer());
137-
var needToDelete = localCatalog.Except(catalog, new FileItemExistenceComparer());
138-
// var needToUpdate = localCatalog.GroupJoin(
139-
// catalog,
140-
// x => x,
141-
// y => y,
142-
// (x, y) => new { Original = x, New = y }
143-
// )
144-
// .SelectMany(
145-
// c => c.New.DefaultIfEmpty(),
146-
// (x, y) => new { x.Original, New = y }
147-
// )
148-
// .Where(c => c.Original.Hash.Equals(c.New!.Hash) == false)
149-
// .Select(c => new { Original = c.Original, Difference = c.New })
150-
// ;
151-
152-
app.Logger.LogInformation("Need to fetch: {json}", JsonSerializer.Serialize(needToFetch, jsonSerializerOptions));
153-
app.Logger.LogInformation("Need to delete:: {json}", JsonSerializer.Serialize(needToDelete, jsonSerializerOptions));
154-
// app.Logger.LogDebug("Need to update: {json}", JsonSerializer.Serialize(needToUpdate));
140+
var differences = catalog.Concat(localCatalog).GroupBy(
141+
f => f.Path,
142+
f => f.Hash,
143+
(path, hashes) => new
144+
{
145+
Path = path,
146+
Count = hashes.Count(),
147+
LocalHash = localCatalog.FirstOrDefault(f => f.Path.Equals(path))?.Hash,
148+
RemoteHash = catalog.FirstOrDefault(f => f.Path.Equals(path))?.Hash,
149+
}
150+
);
151+
152+
// app.Logger.LogInformation("Need to fetch: {json}", JsonSerializer.Serialize(needToFetch, jsonSerializerOptions));
153+
// app.Logger.LogInformation("Need to delete:: {json}", JsonSerializer.Serialize(needToDelete, jsonSerializerOptions));
154+
app.Logger.LogDebug("Differences: {json}", JsonSerializer.Serialize(differences));
155155
};
156156
timer.Start();
157157

0 commit comments

Comments
 (0)