Skip to content

Commit

Permalink
Optimize UnitRepository duplicate checking (#719)
Browse files Browse the repository at this point in the history
- Only load up relevant rows
  • Loading branch information
SapiensAnatis authored Mar 13, 2024
1 parent ff2a0c9 commit 11cc5cb
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions DragaliaAPI/DragaliaAPI.Database/Repositories/UnitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,19 @@ ILogger<UnitRepository> logger
public async Task<IEnumerable<(Charas id, bool isNew)>> AddCharas(IEnumerable<Charas> idList)
{
List<Charas> enumeratedIdList = idList.ToList();
IEnumerable<int> storyIdList = GetFirstCharaStories(enumeratedIdList);

// Generate result. The first occurrence of a character in the list should be new (if not in the DB)
// but subsequent results should then not be labelled as new.
List<Charas> ownedCharas = await Charas.Select(x => x.CharaId).ToListAsync();
List<Charas> ownedCharas = await Charas
.Select(x => x.CharaId)
.Where(x => enumeratedIdList.Contains(x))
.ToListAsync();

List<int> ownedCharaStories = await this
.apiContext.PlayerStoryState.Where(x => x.StoryType == StoryTypes.Chara)
.apiContext.PlayerStoryState.Where(x =>
x.StoryType == StoryTypes.Chara && storyIdList.Contains(x.StoryId)
)
.Select(x => x.StoryId)
.ToListAsync();

Expand Down Expand Up @@ -149,6 +156,17 @@ ILogger<UnitRepository> logger
}

return newMapping.Select(x => (x.Id, x.IsNew));

static IEnumerable<int> GetFirstCharaStories(IEnumerable<Charas> charaIdList)
{
foreach (Charas c in charaIdList)
{
if (MasterAsset.CharaStories.TryGetValue((int)c, out StoryData? storyData))
{
yield return storyData.storyIds[0];
}
}
}
}

public async Task<bool> AddCharas(Charas id)
Expand All @@ -160,9 +178,13 @@ public async Task<bool> AddCharas(Charas id)
{
List<Dragons> enumeratedIdList = idList.ToList();

List<Dragons> ownedDragons = await Dragons.Select(x => x.DragonId).ToListAsync();
List<Dragons> ownedDragons = await Dragons
.Select(x => x.DragonId)
.Where(x => enumeratedIdList.Contains(x))
.ToListAsync();
List<Dragons> ownedReliabilities = await DragonReliabilities
.Select(x => x.DragonId)
.Where(x => enumeratedIdList.Contains(x))
.ToListAsync();

List<DragonNewCheckResult> newMapping = MarkNewDragons(
Expand Down

0 comments on commit 11cc5cb

Please sign in to comment.