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)
|
||||
.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)
|
||||
{
|
||||
try
|
||||
{
|
||||
UpdateSearchText(voiceWork);
|
||||
UpdateSearchText(voiceWork, existingSearches, englishTags);
|
||||
}
|
||||
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
|
||||
.FirstOrDefault(s => s.VoiceWorkId == voiceWork.VoiceWorkId);
|
||||
|
||||
if (searchEntry == null)
|
||||
if (!existingSearches.TryGetValue(voiceWork.VoiceWorkId, out var searchEntry))
|
||||
{
|
||||
dbContext.VoiceWorkSearches.Add(new VoiceWorkSearch
|
||||
{
|
||||
@@ -53,11 +60,14 @@ public class VoiceWorkSearchUpdater(AppDbContext dbContext) : IVoiceWorkSearchUp
|
||||
}
|
||||
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();
|
||||
|
||||
@@ -80,12 +90,8 @@ public class VoiceWorkSearchUpdater(AppDbContext dbContext) : IVoiceWorkSearchUp
|
||||
|
||||
AppendRaw(sb, tag.Name);
|
||||
|
||||
var englishTag = dbContext.EnglishTags.FirstOrDefault(et => et.TagId == tag.TagId);
|
||||
|
||||
if (englishTag is null)
|
||||
continue;
|
||||
|
||||
AppendRaw(sb, englishTag?.Name);
|
||||
if (englishTags.TryGetValue(tag.TagId, out var englishTag))
|
||||
AppendRaw(sb, englishTag.Name);
|
||||
}
|
||||
|
||||
foreach (var creator in voiceWork.Creators.Select(vwc => vwc.Creator))
|
||||
|
||||
Reference in New Issue
Block a user