Skip to content

Commit d14de81

Browse files
committed
Tidy up CrdtFwdataProjectSyncService
1 parent dde0b60 commit d14de81

1 file changed

Lines changed: 41 additions & 54 deletions

File tree

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics;
2-
using FwDataMiniLcmBridge;
32
using FwDataMiniLcmBridge.Api;
43
using LcmCrdt;
54
using LexCore.Sync;
@@ -11,8 +10,9 @@
1110

1211
namespace FwLiteProjectSync;
1312

14-
public class CrdtFwdataProjectSyncService(MiniLcmImport miniLcmImport, ProjectSnapshotService projectSnapshotService,
15-
ILogger<CrdtFwdataProjectSyncService> logger, MiniLcmApiValidationWrapperFactory validationWrapperFactory,
13+
public class CrdtFwdataProjectSyncService(MiniLcmImport miniLcmImport,
14+
ILogger<CrdtFwdataProjectSyncService> logger,
15+
MiniLcmApiValidationWrapperFactory validationWrapperFactory,
1616
MiniLcmApiStringNormalizationWrapperFactory normalizationWrapperFactory)
1717
{
1818
public record DryRunSyncResult(
@@ -28,50 +28,32 @@ public async Task<DryRunSyncResult> SyncDryRun(IMiniLcmApi crdtApi, FwDataMiniLc
2828

2929
public virtual async Task<SyncResult> Sync(IMiniLcmApi crdtApi, FwDataMiniLcmApi fwdataApi, ProjectSnapshot projectSnapshot, bool dryRun = false)
3030
{
31-
using var activity = FwLiteProjectSyncActivitySource.Value.StartActivity();
32-
if (crdtApi is not CrdtMiniLcmApi crdt) // maybe the argument type should be changed?
33-
throw new InvalidOperationException("CrdtApi must be of type CrdtMiniLcmApi to sync.");
34-
if (crdt.ProjectData.FwProjectId != fwdataApi.ProjectId)
35-
{
36-
activity?.SetStatus(ActivityStatusCode.Error, $"Project id mismatch, CRDT Id: {crdt.ProjectData.FwProjectId}, FWData Id: {fwdataApi.ProjectId}");
37-
throw new InvalidOperationException($"Project id mismatch, CRDT Id: {crdt.ProjectData.FwProjectId}, FWData Id: {fwdataApi.ProjectId}");
38-
}
39-
40-
// Repair any missing translation IDs before doing the full sync, so the sync doesn't have to deal with them
41-
var syncedIdCount = await CrdtRepairs.SyncMissingTranslationIds(projectSnapshot.Entries, fwdataApi, crdt, dryRun);
42-
43-
SyncResult result = await Sync(crdtApi, fwdataApi, dryRun, projectSnapshot);
44-
fwdataApi.Save();
45-
return result;
31+
return await SyncOrImportInternal(crdtApi, fwdataApi, dryRun, projectSnapshot);
4632
}
4733

4834
public async Task<DryRunSyncResult> ImportDryRun(IMiniLcmApi crdtApi, FwDataMiniLcmApi fwdataApi)
4935
{
5036
return (DryRunSyncResult)await Import(crdtApi, fwdataApi, true);
5137
}
5238

53-
public virtual async Task<SyncResult> Import(IMiniLcmApi crdtApi, FwDataMiniLcmApi fwdataApi, bool dryRun = false, bool keepSnapshotBackup = false)
39+
public virtual async Task<SyncResult> Import(IMiniLcmApi crdtApi, FwDataMiniLcmApi fwdataApi, bool dryRun = false)
40+
{
41+
return await SyncOrImportInternal(crdtApi, fwdataApi, dryRun, projectSnapshot: null);
42+
}
43+
44+
private async Task<SyncResult> SyncOrImportInternal(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi, bool dryRun, ProjectSnapshot? projectSnapshot)
5445
{
5546
using var activity = FwLiteProjectSyncActivitySource.Value.StartActivity();
5647
if (crdtApi is not CrdtMiniLcmApi crdt) // maybe the argument type should be changed?
57-
throw new InvalidOperationException("CrdtApi must be of type CrdtMiniLcmApi to import.");
58-
if (crdt.ProjectData.FwProjectId != fwdataApi.ProjectId)
59-
{
60-
activity?.SetStatus(ActivityStatusCode.Error, $"Project id mismatch, CRDT Id: {crdt.ProjectData.FwProjectId}, FWData Id: {fwdataApi.ProjectId}");
61-
throw new InvalidOperationException($"Project id mismatch, CRDT Id: {crdt.ProjectData.FwProjectId}, FWData Id: {fwdataApi.ProjectId}");
62-
}
63-
64-
var result = await ImportInternal(crdtApi, fwdataApi, dryRun, fwdataApi.EntryCount);
65-
fwdataApi.Save();
66-
if (!dryRun)
48+
throw new InvalidOperationException("CrdtApi must be of type CrdtMiniLcmApi to sync.");
49+
if (fwdataApi is not FwDataMiniLcmApi fwdata) // maybe the argument type should be changed?
50+
throw new InvalidOperationException("FwdataApi must be of type FwDataMiniLcmApi to sync.");
51+
if (crdt.ProjectData.FwProjectId != fwdata.ProjectId)
6752
{
68-
await projectSnapshotService.RegenerateProjectSnapshot(crdtApi, fwdataApi.Project, keepSnapshotBackup);
53+
activity?.SetStatus(ActivityStatusCode.Error, $"Project id mismatch, CRDT Id: {crdt.ProjectData.FwProjectId}, FWData Id: {fwdata.ProjectId}");
54+
throw new InvalidOperationException($"Project id mismatch, CRDT Id: {crdt.ProjectData.FwProjectId}, FWData Id: {fwdata.ProjectId}");
6955
}
70-
return result;
71-
}
7256

73-
private async Task<SyncResult> Sync(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi, bool dryRun, ProjectSnapshot projectSnapshot)
74-
{
7557
crdtApi = normalizationWrapperFactory.Create(validationWrapperFactory.Create(crdtApi));
7658
fwdataApi = normalizationWrapperFactory.Create(validationWrapperFactory.Create(fwdataApi));
7759

@@ -81,6 +63,30 @@ private async Task<SyncResult> Sync(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi,
8163
fwdataApi = new DryRunMiniLcmApi(fwdataApi);
8264
}
8365

66+
if (projectSnapshot is not null)
67+
{
68+
// Repair any missing translation IDs before doing the full sync, so the sync doesn't have to deal with them
69+
var syncedIdCount = await CrdtRepairs.SyncMissingTranslationIds(projectSnapshot.Entries, fwdata, crdt, dryRun);
70+
}
71+
72+
var syncResult = projectSnapshot is null
73+
? await ImportInternal(crdtApi, fwdataApi, fwdata.EntryCount)
74+
: await SyncInternal(crdtApi, fwdataApi, projectSnapshot);
75+
76+
if (!dryRun)
77+
{
78+
fwdata.Save();
79+
return syncResult;
80+
}
81+
82+
LogDryRun(crdtApi, "crdt");
83+
LogDryRun(fwdataApi, "fwdata");
84+
return new DryRunSyncResult(syncResult.CrdtChanges, syncResult.FwdataChanges,
85+
GetDryRunRecords(crdtApi), GetDryRunRecords(fwdataApi));
86+
}
87+
88+
private async Task<SyncResult> SyncInternal(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi, ProjectSnapshot projectSnapshot)
89+
{
8490
var currentFwDataWritingSystems = await fwdataApi.GetWritingSystems();
8591
var crdtChanges = await WritingSystemSync.Sync(projectSnapshot.WritingSystems, currentFwDataWritingSystems, crdtApi);
8692
var fwdataChanges = await WritingSystemSync.Sync(currentFwDataWritingSystems, await crdtApi.GetWritingSystems(), fwdataApi);
@@ -103,30 +109,14 @@ private async Task<SyncResult> Sync(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi,
103109

104110
var currentFwDataEntries = await fwdataApi.GetAllEntries().ToArrayAsync();
105111
crdtChanges += await EntrySync.SyncFull(projectSnapshot.Entries, currentFwDataEntries, crdtApi);
106-
LogDryRun(crdtApi, "crdt");
107-
108112
fwdataChanges += await EntrySync.SyncFull(currentFwDataEntries, await crdtApi.GetAllEntries().ToArrayAsync(), fwdataApi);
109-
LogDryRun(fwdataApi, "fwdata");
110113

111-
//todo push crdt changes to lexbox
112-
if (dryRun) return new DryRunSyncResult(crdtChanges, fwdataChanges, GetDryRunRecords(crdtApi), GetDryRunRecords(fwdataApi));
113114
return new SyncResult(crdtChanges, fwdataChanges);
114115
}
115116

116-
private async Task<SyncResult> ImportInternal(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi, bool dryRun, int entryCount)
117+
private async Task<SyncResult> ImportInternal(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi, int entryCount)
117118
{
118-
crdtApi = normalizationWrapperFactory.Create(validationWrapperFactory.Create(crdtApi));
119-
fwdataApi = normalizationWrapperFactory.Create(validationWrapperFactory.Create(fwdataApi));
120-
121-
if (dryRun)
122-
{
123-
crdtApi = new DryRunMiniLcmApi(crdtApi);
124-
fwdataApi = new DryRunMiniLcmApi(fwdataApi);
125-
}
126-
127119
await miniLcmImport.ImportProject(crdtApi, fwdataApi, entryCount);
128-
LogDryRun(crdtApi, "crdt");
129-
if (dryRun) return new DryRunSyncResult(entryCount, 0, GetDryRunRecords(crdtApi), []);
130120
return new SyncResult(entryCount, 0);
131121
}
132122

@@ -145,7 +135,4 @@ private void LogDryRun(IMiniLcmApi api, string type)
145135
{
146136
return ((DryRunMiniLcmApi)api).DryRunRecords;
147137
}
148-
149-
public static bool HasSyncedSuccessfully(FwDataProject project)
150-
=> ProjectSnapshotService.HasSyncedSuccessfully(project);
151138
}

0 commit comments

Comments
 (0)