Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mini lcm refactor #1046

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.Options;
using MiniLcm;
using MiniLcm.Models;

namespace FwDataMiniLcmBridge.Tests.Fixtures;

Expand Down
6 changes: 3 additions & 3 deletions backend/FwLite/FwDataMiniLcmBridge.Tests/PartOfSpeechTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FwDataMiniLcmBridge.Api;
using FwDataMiniLcmBridge.Tests.Fixtures;
using MiniLcm;
using MiniLcm.Models;

namespace FwDataMiniLcmBridge.Tests;

Expand Down Expand Up @@ -64,12 +65,11 @@ public async Task Sense_UpdatesPartOfSpeech()
var sense = entry.Senses.First(s => !string.IsNullOrEmpty(s.PartOfSpeech));
var newPartOfSpeech = await _api.GetPartsOfSpeech().FirstAsync(po => po.Id != sense.PartOfSpeechId);

var update = _api.CreateUpdateBuilder<Sense>()
var update = new UpdateObjectInput<Sense>()
.Set(s => s.PartOfSpeech,
newPartOfSpeech.Name
["en"]) //this won't actually update the part of speech, but it shouldn't cause an issue either.
.Set(s => s.PartOfSpeechId, newPartOfSpeech.Id)
.Build();
.Set(s => s.PartOfSpeechId, newPartOfSpeech.Id);
await _api.UpdateSense(entry.Id, sense.Id, update);

entry = await _api.GetEntry(entry.Id);
Expand Down
11 changes: 5 additions & 6 deletions backend/FwLite/FwDataMiniLcmBridge.Tests/SemanticDomainTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FwDataMiniLcmBridge.Api;
using FwDataMiniLcmBridge.Tests.Fixtures;
using MiniLcm;
using MiniLcm.Models;

namespace FwDataMiniLcmBridge.Tests;

Expand Down Expand Up @@ -73,9 +74,8 @@ public async Task Sense_AddSemanticDomain()
var currentSemanticDomain = sense.SemanticDomains.First();
var newSemanticDomain = await _api.GetSemanticDomains().FirstAsync(sd => sd.Id != currentSemanticDomain.Id);

var update = _api.CreateUpdateBuilder<Sense>()
.Add(s => s.SemanticDomains, newSemanticDomain)
.Build();
var update = new UpdateObjectInput<Sense>()
.Add(s => s.SemanticDomains, newSemanticDomain);
await _api.UpdateSense(entry.Id, sense.Id, update);

entry = await _api.GetEntry(entry.Id);
Expand All @@ -91,9 +91,8 @@ public async Task Sense_RemoveSemanticDomain()
var sense = entry.Senses.First(s => s.SemanticDomains.Any());
var domainToRemove = sense.SemanticDomains[0];

var update = _api.CreateUpdateBuilder<Sense>()
.Remove(s => s.SemanticDomains, 0)
.Build();
var update = new UpdateObjectInput<Sense>()
.Remove(s => s.SemanticDomains, 0);
await _api.UpdateSense(entry.Id, sense.Id, update);

entry = await _api.GetEntry(entry.Id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FwDataMiniLcmBridge.Api;
using FwDataMiniLcmBridge.Tests.Fixtures;
using MiniLcm;
using MiniLcm.Models;

namespace FwDataMiniLcmBridge.Tests;

Expand Down
9 changes: 2 additions & 7 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FwDataMiniLcmBridge.Api.UpdateProxy;
using Microsoft.Extensions.Logging;
using MiniLcm;
using MiniLcm.Models;
using SIL.LCModel;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.LCModel.Core.Text;
Expand All @@ -12,7 +13,7 @@

namespace FwDataMiniLcmBridge.Api;

public class FwDataMiniLcmApi(Lazy<LcmCache> cacheLazy, bool onCloseSave, ILogger<FwDataMiniLcmApi> logger, FwDataProject project) : ILexboxApi, IDisposable
public class FwDataMiniLcmApi(Lazy<LcmCache> cacheLazy, bool onCloseSave, ILogger<FwDataMiniLcmApi> logger, FwDataProject project) : IMiniLcmApi, IDisposable
{
private LcmCache Cache => cacheLazy.Value;
public FwDataProject Project { get; } = project;
Expand Down Expand Up @@ -538,10 +539,4 @@ private static void ValidateOwnership(ILexExampleSentence lexExampleSentence, Gu
lexExampleSentence.Owner.ClassName);
}
}


public UpdateBuilder<T> CreateUpdateBuilder<T>() where T : class
{
return new UpdateBuilder<T>();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections;
using MiniLcm;
using MiniLcm.Models;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.LCModel.Core.Text;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MiniLcm;
using MiniLcm.Models;
using SIL.LCModel;
using SIL.LCModel.Core.KernelInterfaces;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MiniLcm;
using MiniLcm.Models;
using SIL.LCModel;
using SIL.LCModel.Core.Text;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
 using System.Diagnostics.CodeAnalysis;
using MiniLcm;
using MiniLcm.Models;
using SIL.LCModel;
using SIL.LCModel.DomainServices;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FwDataMiniLcmBridge.LcmUtils;
using Microsoft.Extensions.Options;
using MiniLcm;
using MiniLcm.Models;

namespace FwDataMiniLcmBridge;

Expand Down
2 changes: 1 addition & 1 deletion backend/FwLite/FwDataMiniLcmBridge/FwDataBridgeKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static IServiceCollection AddFwDataBridge(this IServiceCollection service
services.AddSingleton<FwDataFactory>();
services.AddSingleton<FieldWorksProjectList>();
services.AddSingleton<IProjectLoader, ProjectLoader>();
services.AddKeyedScoped<ILexboxApi>(FwDataApiKey, (provider, o) => provider.GetRequiredService<FwDataFactory>().GetCurrentFwDataMiniLcmApi(true));
services.AddKeyedScoped<IMiniLcmApi>(FwDataApiKey, (provider, o) => provider.GetRequiredService<FwDataFactory>().GetCurrentFwDataMiniLcmApi(true));
services.AddSingleton<FwDataProjectContext>();
return services;
}
Expand Down
2 changes: 1 addition & 1 deletion backend/FwLite/FwDataMiniLcmBridge/FwDataProject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MiniLcm;
using MiniLcm.Models;

namespace FwDataMiniLcmBridge;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MiniLcm;
using MiniLcm.Models;
using Soenneker.Utils.AutoBogus.Context;
using Soenneker.Utils.AutoBogus.Override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public async Task InitializeAsync()
var crdtProject = await _services.ServiceProvider.GetRequiredService<ProjectsService>()
.CreateProject(new(_projectName, projectGuid));
_services.ServiceProvider.GetRequiredService<ProjectContext>().Project = crdtProject;
CrdtApi = _services.ServiceProvider.GetRequiredService<ILexboxApi>();
CrdtApi = _services.ServiceProvider.GetRequiredService<IMiniLcmApi>();
}

public async Task DisposeAsync()
{
await _services.DisposeAsync();
}

public ILexboxApi CrdtApi { get; set; } = null!;
public IMiniLcmApi CrdtApi { get; set; } = null!;
public FwDataMiniLcmApi FwDataApi { get; set; } = null!;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MiniLcm;
using MiniLcm.Models;
using Spart.Parsers;
using SystemTextJsonPatch.Operations;

Expand Down
9 changes: 3 additions & 6 deletions backend/FwLite/FwLiteProjectSync.Tests/SyncTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FwLiteProjectSync.Tests.Fixtures;
using MiniLcm;
using MiniLcm.Models;
using SystemTextJsonPatch;

namespace FwLiteProjectSync.Tests;
Expand Down Expand Up @@ -88,13 +89,9 @@ public async Task UpdatingAnEntryInEachProjectSyncsAcrossBoth()
await fwdataApi.CreateWritingSystem(WritingSystemType.Vernacular, new WritingSystem() { Id = new WritingSystemId("fr"), Name = "French", Abbreviation = "fr", Font = "Arial" });
await _syncService.Sync(crdtApi, fwdataApi);

var jsonPatchDocument = new JsonPatchDocument<Entry>();
jsonPatchDocument.Replace(entry => entry.LexemeForm["es"], "Manzana");
await crdtApi.UpdateEntry(_testEntry.Id, new JsonPatchUpdateInput<Entry>(jsonPatchDocument));
await crdtApi.UpdateEntry(_testEntry.Id, new UpdateObjectInput<Entry>().Set(entry => entry.LexemeForm["es"],"Manzana"));

var fwdataPatch = new JsonPatchDocument<Entry>();
fwdataPatch.Replace(entry => entry.LexemeForm["fr"], "Pomme");
await fwdataApi.UpdateEntry(_testEntry.Id, new JsonPatchUpdateInput<Entry>(fwdataPatch));
await fwdataApi.UpdateEntry(_testEntry.Id, new UpdateObjectInput<Entry>().Set(entry => entry.LexemeForm["fr"], "Pomme"));
var results = await _syncService.Sync(crdtApi, fwdataApi);
results.CrdtChanges.Should().Be(1);
results.FwdataChanges.Should().Be(1);
Expand Down
2 changes: 1 addition & 1 deletion backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FwLiteProjectSync.Tests.Fixtures;
using MiniLcm;
using MiniLcm.Models;
using Soenneker.Utils.AutoBogus;
using Soenneker.Utils.AutoBogus.Config;

Expand Down
27 changes: 14 additions & 13 deletions backend/FwLite/FwLiteProjectSync/CrdtFwdataProjectSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MiniLcm;
using MiniLcm.Models;
using SystemTextJsonPatch;
using SystemTextJsonPatch.Operations;

Expand All @@ -13,7 +14,7 @@ public class CrdtFwdataProjectSyncService(IOptions<LcmCrdtConfig> lcmCrdtConfig,
{
public record SyncResult(int CrdtChanges, int FwdataChanges);

public async Task<SyncResult> Sync(ILexboxApi crdtApi, FwDataMiniLcmApi fwdataApi, bool dryRun = false)
public async Task<SyncResult> Sync(IMiniLcmApi crdtApi, FwDataMiniLcmApi fwdataApi, bool dryRun = false)
{
var projectSnapshot = await GetProjectSnapshot(fwdataApi.Project.Name);
SyncResult result = await Sync(crdtApi, fwdataApi, dryRun, fwdataApi.EntryCount, projectSnapshot);
Expand All @@ -26,7 +27,7 @@ await SaveProjectSnapshot(fwdataApi.Project.Name,
return result;
}

private async Task<SyncResult> Sync(ILexboxApi crdtApi, ILexboxApi fwdataApi, bool dryRun, int entryCount, ProjectSnapshot? projectSnapshot)
private async Task<SyncResult> Sync(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi, bool dryRun, int entryCount, ProjectSnapshot? projectSnapshot)
{
if (dryRun)
{
Expand All @@ -53,7 +54,7 @@ private async Task<SyncResult> Sync(ILexboxApi crdtApi, ILexboxApi fwdataApi, bo
return new SyncResult(crdtChanges, fwdataChanges);
}

private void LogDryRun(ILexboxApi api, string type)
private void LogDryRun(IMiniLcmApi api, string type)
{
if (api is not DryRunMiniLcmApi dryRunApi) return;
foreach (var dryRunRecord in dryRunApi.DryRunRecords)
Expand Down Expand Up @@ -83,7 +84,7 @@ private async Task SaveProjectSnapshot(string projectName, ProjectSnapshot proje

private async Task<int> EntrySync(Entry[] currentEntries,
Entry[] previousEntries,
ILexboxApi api)
IMiniLcmApi api)
{
return await DiffCollection(api,
previousEntries,
Expand All @@ -110,7 +111,7 @@ private async Task<int> EntrySync(Entry[] currentEntries,
private async Task<int> SenseSync(Guid entryId,
IList<Sense> currentSenses,
IList<Sense> previousSenses,
ILexboxApi api)
IMiniLcmApi api)
{
return await DiffCollection(api,
previousSenses,
Expand Down Expand Up @@ -142,7 +143,7 @@ private async Task<int> ExampleSentenceSync(Guid entryId,
Guid senseId,
IList<ExampleSentence> currentExampleSentences,
IList<ExampleSentence> previousExampleSentences,
ILexboxApi api)
IMiniLcmApi api)
{
return await DiffCollection(api,
previousExampleSentences,
Expand Down Expand Up @@ -182,7 +183,7 @@ private async Task<int> ExampleSentenceSync(Guid entryId,
previousEntry.LiteralMeaning,
currentEntry.LiteralMeaning));
if (patchDocument.Operations.Count == 0) return null;
return new JsonPatchUpdateInput<Entry>(patchDocument);
return new UpdateObjectInput<Entry>(patchDocument);
}

public static async Task<UpdateObjectInput<Sense>?> SenseDiffToUpdate(Sense previousSense, Sense currentSense)
Expand Down Expand Up @@ -223,7 +224,7 @@ await DiffCollection(null!,
return Task.FromResult(0);
});
if (patchDocument.Operations.Count == 0) return null;
return new JsonPatchUpdateInput<Sense>(patchDocument);
return new UpdateObjectInput<Sense>(patchDocument);
}

public static UpdateObjectInput<ExampleSentence>? ExampleDiffToUpdate(ExampleSentence previousExampleSentence,
Expand All @@ -242,7 +243,7 @@ await DiffCollection(null!,
}

if (patchDocument.Operations.Count == 0) return null;
return new JsonPatchUpdateInput<ExampleSentence>(patchDocument);
return new UpdateObjectInput<ExampleSentence>(patchDocument);
}

public static IEnumerable<Operation<T>> GetMultiStringDiff<T>(string path, MultiString previous, MultiString current)
Expand Down Expand Up @@ -271,12 +272,12 @@ public static IEnumerable<Operation<T>> GetMultiStringDiff<T>(string path, Multi
}

private static async Task<int> DiffCollection<T>(
ILexboxApi api,
IMiniLcmApi api,
IList<T> previous,
IList<T> current,
Func<ILexboxApi, T, Task<int>> add,
Func<ILexboxApi, T, Task<int>> remove,
Func<ILexboxApi, T, T, Task<int>> replace) where T : IObjectWithId
Func<IMiniLcmApi, T, Task<int>> add,
Func<IMiniLcmApi, T, Task<int>> remove,
Func<IMiniLcmApi, T, T, Task<int>> replace) where T : IObjectWithId
{
var changes = 0;
var currentEntriesDict = current.ToDictionary(entry => entry.Id);
Expand Down
8 changes: 2 additions & 6 deletions backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using MiniLcm;
using MiniLcm.Models;

namespace FwLiteProjectSync;

public class DryRunMiniLcmApi(ILexboxApi api) : ILexboxApi
public class DryRunMiniLcmApi(IMiniLcmApi api) : IMiniLcmApi
{
public List<DryRunRecord> DryRunRecords { get; } = [];

Expand Down Expand Up @@ -136,9 +137,4 @@ public Task DeleteExampleSentence(Guid entryId, Guid senseId, Guid exampleSenten
DryRunRecords.Add(new DryRunRecord(nameof(DeleteExampleSentence), $"Delete example sentence {exampleSentenceId}"));
return Task.CompletedTask;
}

public UpdateBuilder<T> CreateUpdateBuilder<T>() where T : class
{
return api.CreateUpdateBuilder<T>();
}
}
5 changes: 3 additions & 2 deletions backend/FwLite/FwLiteProjectSync/MiniLcmImport.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using LcmCrdt;
using Microsoft.Extensions.Logging;
using MiniLcm;
using MiniLcm.Models;

namespace FwLiteProjectSync;

public class MiniLcmImport(ILogger<MiniLcmImport> logger)
{
public async Task ImportProject(ILexboxApi importTo, ILexboxApi importFrom, int entryCount)
public async Task ImportProject(IMiniLcmApi importTo, IMiniLcmApi importFrom, int entryCount)
{
var writingSystems = await importFrom.GetWritingSystems();
foreach (var ws in writingSystems.Analysis)
Expand All @@ -30,7 +31,7 @@ public async Task ImportProject(ILexboxApi importTo, ILexboxApi importFrom, int

var semanticDomains = importFrom.GetSemanticDomains();
var entries = importFrom.GetEntries(new QueryOptions(Count: 100_000, Offset: 0));
if (importTo is CrdtLexboxApi crdtLexboxApi)
if (importTo is CrdtMiniLcmApi crdtLexboxApi)
{
logger.LogInformation("Importing semantic domains");
await crdtLexboxApi.BulkImportSemanticDomains(semanticDomains.ToBlockingEnumerable());
Expand Down
2 changes: 1 addition & 1 deletion backend/FwLite/FwLiteProjectSync/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
await services.GetRequiredService<CurrentProjectService>().PopulateProjectDataCache();
var syncService = services.GetRequiredService<CrdtFwdataProjectSyncService>();

var result = await syncService.Sync(services.GetRequiredService<ILexboxApi>(), fwdataApi, dryRun);
var result = await syncService.Sync(services.GetRequiredService<IMiniLcmApi>(), fwdataApi, dryRun);
logger.LogInformation("Sync result, CrdtChanges: {CrdtChanges}, FwdataChanges: {FwdataChanges}", result.CrdtChanges, result.FwdataChanges);
},
crdtOption,
Expand Down
Loading