From 11cc5cbcab362048644b873a10d0ea789a317d71 Mon Sep 17 00:00:00 2001 From: Jay Malhotra <5047192+SapiensAnatis@users.noreply.github.com> Date: Wed, 13 Mar 2024 21:45:08 +0000 Subject: [PATCH] Optimize UnitRepository duplicate checking (#719) - Only load up relevant rows --- .../Repositories/UnitRepository.cs | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/DragaliaAPI/DragaliaAPI.Database/Repositories/UnitRepository.cs b/DragaliaAPI/DragaliaAPI.Database/Repositories/UnitRepository.cs index 86ed2db68..e62914482 100644 --- a/DragaliaAPI/DragaliaAPI.Database/Repositories/UnitRepository.cs +++ b/DragaliaAPI/DragaliaAPI.Database/Repositories/UnitRepository.cs @@ -103,12 +103,19 @@ ILogger logger public async Task> AddCharas(IEnumerable idList) { List enumeratedIdList = idList.ToList(); + IEnumerable 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 ownedCharas = await Charas.Select(x => x.CharaId).ToListAsync(); + List ownedCharas = await Charas + .Select(x => x.CharaId) + .Where(x => enumeratedIdList.Contains(x)) + .ToListAsync(); + List 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(); @@ -149,6 +156,17 @@ ILogger logger } return newMapping.Select(x => (x.Id, x.IsNew)); + + static IEnumerable GetFirstCharaStories(IEnumerable charaIdList) + { + foreach (Charas c in charaIdList) + { + if (MasterAsset.CharaStories.TryGetValue((int)c, out StoryData? storyData)) + { + yield return storyData.storyIds[0]; + } + } + } } public async Task AddCharas(Charas id) @@ -160,9 +178,13 @@ public async Task AddCharas(Charas id) { List enumeratedIdList = idList.ToList(); - List ownedDragons = await Dragons.Select(x => x.DragonId).ToListAsync(); + List ownedDragons = await Dragons + .Select(x => x.DragonId) + .Where(x => enumeratedIdList.Contains(x)) + .ToListAsync(); List ownedReliabilities = await DragonReliabilities .Select(x => x.DragonId) + .Where(x => enumeratedIdList.Contains(x)) .ToListAsync(); List newMapping = MarkNewDragons(