Skip to content

Commit

Permalink
Address reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Dec 2, 2024
1 parent e6ee2a0 commit 613dc40
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public async Task CheckEntitiesAsync(IRepository<T> entities, CancellationToken
var now = DateTime.UtcNow;
IEnumerable<T> uninitializedEntities = await entities.GetAllAsync(
e =>
e.IsInitialized != null
&& !e.IsInitialized.Value
&& e.DateCreated != null
&& (now - e.DateCreated) > _timeout,
e.DateCreated != null
&& e.DateCreated < now - _timeout
&& e.IsInitialized != null
&& !e.IsInitialized.Value,
cancellationToken
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ this IMongoDataAccessConfigurator configurator
await c.Indexes.CreateOrUpdateAsync(
new CreateIndexModel<Engine>(Builders<Engine>.IndexKeys.Ascending(e => e.Owner))
);
await c.Indexes.CreateOrUpdateAsync(
new CreateIndexModel<Engine>(Builders<Engine>.IndexKeys.Ascending(e => e.DateCreated))
);
}
);
configurator.AddRepository<Build>(
"translation.builds",
init: c =>
c.Indexes.CreateOrUpdateAsync(
init: async c =>
{
await c.Indexes.CreateOrUpdateAsync(
new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Ascending(b => b.EngineRef))
)
);
await c.Indexes.CreateOrUpdateAsync(
new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Ascending(b => b.DateCreated))
);
}
);
configurator.AddRepository<Pretranslation>(
"translation.pretranslations",
Expand Down
11 changes: 11 additions & 0 deletions src/Serval/src/Serval.Translation/Services/BuildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ public async Task<IEnumerable<Build>> GetAllAsync(string parentId, CancellationT
);
}

public override async Task<Build> GetAsync(string id, CancellationToken cancellationToken = default)
{
Build? build = await Entities.GetAsync(
e => e.Id == id && (e.IsInitialized == null || e.IsInitialized.Value),
cancellationToken
);
if (build == null)
throw new EntityNotFoundException($"Could not find the {typeof(Build).Name} '{id}'.");
return build;
}

public Task<Build?> GetActiveAsync(string parentId, CancellationToken cancellationToken = default)
{
return Entities.GetAsync(
Expand Down
33 changes: 27 additions & 6 deletions src/Serval/src/Serval.Translation/Services/EngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public override async Task<Engine> GetAsync(string id, CancellationToken cancell
return engine;
}

public override async Task<IEnumerable<Engine>> GetAllAsync(
string id,
CancellationToken cancellationToken = default
)
{
return await Entities.GetAllAsync(
e => e.Id == id && (e.IsInitialized == null || e.IsInitialized.Value),
cancellationToken
);
}

public async Task<Models.TranslationResult> TranslateAsync(
string engineId,
string segment,
Expand Down Expand Up @@ -397,7 +408,11 @@ public async Task<ModelDownloadUrl> GetModelDownloadUrlAsync(

public Task AddCorpusAsync(string engineId, Models.Corpus corpus, CancellationToken cancellationToken = default)
{
return Entities.UpdateAsync(engineId, u => u.Add(e => e.Corpora, corpus), cancellationToken: cancellationToken);
return Entities.UpdateAsync(
e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value),
u => u.Add(e => e.Corpora, corpus),
cancellationToken: cancellationToken
);
}

public async Task<Models.Corpus> UpdateCorpusAsync(
Expand All @@ -409,7 +424,10 @@ public Task AddCorpusAsync(string engineId, Models.Corpus corpus, CancellationTo
)
{
Engine? engine = await Entities.UpdateAsync(
e => e.Id == engineId && e.Corpora.Any(c => c.Id == corpusId),
e =>
e.Id == engineId
&& (e.IsInitialized == null || e.IsInitialized.Value)
&& e.Corpora.Any(c => c.Id == corpusId),
u =>
{
if (sourceFiles is not null)
Expand All @@ -436,7 +454,7 @@ await _dataAccessContext.WithTransactionAsync(
async (ct) =>
{
originalEngine = await Entities.UpdateAsync(
engineId,
e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value),
u => u.RemoveAll(e => e.Corpora, c => c.Id == corpusId),
returnOriginal: true,
cancellationToken: ct
Expand Down Expand Up @@ -471,7 +489,7 @@ public Task AddParallelCorpusAsync(
)
{
return Entities.UpdateAsync(
engineId,
e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value),
u => u.Add(e => e.ParallelCorpora, corpus),
cancellationToken: cancellationToken
);
Expand All @@ -486,7 +504,10 @@ public Task AddParallelCorpusAsync(
)
{
Engine? engine = await Entities.UpdateAsync(
e => e.Id == engineId && e.ParallelCorpora.Any(c => c.Id == parallelCorpusId),
e =>
e.Id == engineId
&& (e.IsInitialized == null || e.IsInitialized.Value)
&& e.ParallelCorpora.Any(c => c.Id == parallelCorpusId),
u =>
{
if (sourceCorpora is not null)
Expand Down Expand Up @@ -517,7 +538,7 @@ await _dataAccessContext.WithTransactionAsync(
async (ct) =>
{
originalEngine = await Entities.UpdateAsync(
engineId,
e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value),
u => u.RemoveAll(e => e.ParallelCorpora, c => c.Id == parallelCorpusId),
returnOriginal: true,
cancellationToken: ct
Expand Down

0 comments on commit 613dc40

Please sign in to comment.