Improved performance of voice work search updater.
This commit is contained in:
@@ -21,11 +21,21 @@ public class VoiceWorkSearchUpdater(AppDbContext dbContext) : IVoiceWorkSearchUp
|
|||||||
.Include(vw => vw.EnglishVoiceWorks)
|
.Include(vw => vw.EnglishVoiceWorks)
|
||||||
.ToListAsync(cancellationToken);
|
.ToListAsync(cancellationToken);
|
||||||
|
|
||||||
|
Dictionary<int, VoiceWorkSearch> existingSearches = await dbContext.VoiceWorkSearches
|
||||||
|
.Where(s => voiceWorkIds.Contains(s.VoiceWorkId))
|
||||||
|
.ToDictionaryAsync(s => s.VoiceWorkId, cancellationToken);
|
||||||
|
|
||||||
|
int[] tagIds = [.. batch.SelectMany(vw => vw.Tags).Select(vwt => vwt.TagId).Distinct()];
|
||||||
|
|
||||||
|
Dictionary<int, EnglishTag> englishTags = await dbContext.EnglishTags
|
||||||
|
.Where(et => tagIds.Contains(et.TagId))
|
||||||
|
.ToDictionaryAsync(et => et.TagId, cancellationToken);
|
||||||
|
|
||||||
foreach (var voiceWork in batch)
|
foreach (var voiceWork in batch)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdateSearchText(voiceWork);
|
UpdateSearchText(voiceWork, existingSearches, englishTags);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -33,17 +43,14 @@ public class VoiceWorkSearchUpdater(AppDbContext dbContext) : IVoiceWorkSearchUp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dbContext.SaveChanges();
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateSearchText(VoiceWork voiceWork)
|
private void UpdateSearchText(VoiceWork voiceWork, Dictionary<int, VoiceWorkSearch> existingSearches, Dictionary<int, EnglishTag> englishTags)
|
||||||
{
|
{
|
||||||
string searchText = GetSearchText(voiceWork);
|
string searchText = GetSearchText(voiceWork, englishTags);
|
||||||
|
|
||||||
var searchEntry = dbContext.VoiceWorkSearches
|
if (!existingSearches.TryGetValue(voiceWork.VoiceWorkId, out var searchEntry))
|
||||||
.FirstOrDefault(s => s.VoiceWorkId == voiceWork.VoiceWorkId);
|
|
||||||
|
|
||||||
if (searchEntry == null)
|
|
||||||
{
|
{
|
||||||
dbContext.VoiceWorkSearches.Add(new VoiceWorkSearch
|
dbContext.VoiceWorkSearches.Add(new VoiceWorkSearch
|
||||||
{
|
{
|
||||||
@@ -53,11 +60,14 @@ public class VoiceWorkSearchUpdater(AppDbContext dbContext) : IVoiceWorkSearchUp
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
searchEntry.SearchText = searchText;
|
if (!string.Equals(searchEntry.SearchText, searchText, StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
searchEntry.SearchText = searchText;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetSearchText(VoiceWork voiceWork)
|
private string GetSearchText(VoiceWork voiceWork, Dictionary<int, EnglishTag> englishTags)
|
||||||
{
|
{
|
||||||
var english = voiceWork.EnglishVoiceWorks.FirstOrDefault();
|
var english = voiceWork.EnglishVoiceWorks.FirstOrDefault();
|
||||||
|
|
||||||
@@ -80,12 +90,8 @@ public class VoiceWorkSearchUpdater(AppDbContext dbContext) : IVoiceWorkSearchUp
|
|||||||
|
|
||||||
AppendRaw(sb, tag.Name);
|
AppendRaw(sb, tag.Name);
|
||||||
|
|
||||||
var englishTag = dbContext.EnglishTags.FirstOrDefault(et => et.TagId == tag.TagId);
|
if (englishTags.TryGetValue(tag.TagId, out var englishTag))
|
||||||
|
AppendRaw(sb, englishTag.Name);
|
||||||
if (englishTag is null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
AppendRaw(sb, englishTag?.Name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var creator in voiceWork.Creators.Select(vwc => vwc.Creator))
|
foreach (var creator in voiceWork.Creators.Select(vwc => vwc.Creator))
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" Version="8.0.1">
|
<PackageReference Include="coverlet.collector" Version="10.0.0">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
|
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="10.0.0" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="10.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||||
<PackageReference Include="Spectre.Console" Version="0.55.0" />
|
<PackageReference Include="Spectre.Console" Version="0.55.2" />
|
||||||
<PackageReference Include="System.CommandLine" Version="2.0.6" />
|
<PackageReference Include="System.CommandLine" Version="2.0.6" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user