From 2e4b1689852894b1dd612c47af0313f3cfde9f3d Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 13 Dec 2020 22:09:51 -0500 Subject: [PATCH] Improve use of All() for Path related queries --- .../HealthCheck/Checks/DeleteBadMediaCovers.cs | 6 +++--- .../HealthCheck/Checks/RootFolderCheckFixture.cs | 8 ++++---- src/NzbDrone.Core/HealthCheck/Checks/MountCheck.cs | 4 ++-- .../HealthCheck/Checks/RootFolderCheck.cs | 4 ++-- .../Housekeeping/Housekeepers/DeleteBadMediaCovers.cs | 6 +++--- .../MediaFiles/MediaFileDeletionService.cs | 8 ++++---- .../Music/Repositories/ArtistRepository.cs | 11 +++++++++++ src/NzbDrone.Core/Music/Services/ArtistService.cs | 6 ++++++ .../Validation/Paths/ArtistAncestorValidator.cs | 2 +- .../Validation/Paths/ArtistPathValidator.cs | 5 +++-- 10 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/DeleteBadMediaCovers.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/DeleteBadMediaCovers.cs index 6c0f70cc2..eb495bb70 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/Checks/DeleteBadMediaCovers.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/DeleteBadMediaCovers.cs @@ -34,8 +34,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks .Build().ToList(); Mocker.GetMock() - .Setup(c => c.GetAllArtists()) - .Returns(_artist); + .Setup(c => c.AllArtistPaths()) + .Returns(_artist.ToDictionary(x => x.Id, x => x.Path)); Mocker.GetMock() .Setup(c => c.GetFilesByArtist(_artist.First().Id)) @@ -73,7 +73,7 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks Subject.Clean(); Mocker.GetMock().VerifySet(c => c.CleanupMetadataImages = true, Times.Never()); - Mocker.GetMock().Verify(c => c.GetAllArtists(), Times.Never()); + Mocker.GetMock().Verify(c => c.AllArtistPaths(), Times.Never()); AssertImageWasNotRemoved(); } diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs index 2ba2d8206..745492c3f 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs @@ -25,8 +25,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks .ToList(); Mocker.GetMock() - .Setup(s => s.GetAllArtists()) - .Returns(artist); + .Setup(s => s.AllArtistPaths()) + .Returns(artist.ToDictionary(x => x.Id, x => x.Path)); Mocker.GetMock() .Setup(s => s.All()) @@ -45,8 +45,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks public void should_not_return_error_when_no_artist() { Mocker.GetMock() - .Setup(s => s.GetAllArtists()) - .Returns(new List()); + .Setup(s => s.AllArtistPaths()) + .Returns(new Dictionary()); Mocker.GetMock() .Setup(s => s.All()) diff --git a/src/NzbDrone.Core/HealthCheck/Checks/MountCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/MountCheck.cs index 91129d1c8..af0442d22 100644 --- a/src/NzbDrone.Core/HealthCheck/Checks/MountCheck.cs +++ b/src/NzbDrone.Core/HealthCheck/Checks/MountCheck.cs @@ -19,8 +19,8 @@ namespace NzbDrone.Core.HealthCheck.Checks public override HealthCheck Check() { // Not best for optimization but due to possible symlinks and junctions, we get mounts based on series path so internals can handle mount resolution. - var mounts = _artistService.GetAllArtists() - .Select(artist => _diskProvider.GetMount(artist.Path)) + var mounts = _artistService.AllArtistPaths() + .Select(path => _diskProvider.GetMount(path.Value)) .Where(m => m != null && m.MountOptions != null && m.MountOptions.IsReadOnly) .DistinctBy(m => m.RootDirectory) .ToList(); diff --git a/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs index 501630ae1..420c8fb22 100644 --- a/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs +++ b/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs @@ -29,8 +29,8 @@ namespace NzbDrone.Core.HealthCheck.Checks public override HealthCheck Check() { - var rootFolders = _artistService.GetAllArtists() - .Select(s => _rootFolderService.GetBestRootFolderPath(s.Path)) + var rootFolders = _artistService.AllArtistPaths() + .Select(s => _rootFolderService.GetBestRootFolderPath(s.Value)) .Distinct(); var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s)) diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/DeleteBadMediaCovers.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/DeleteBadMediaCovers.cs index becadec0e..ecba93842 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/DeleteBadMediaCovers.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/DeleteBadMediaCovers.cs @@ -38,19 +38,19 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers return; } - var artists = _artistService.GetAllArtists(); + var artists = _artistService.AllArtistPaths(); var imageExtensions = new List { ".jpg", ".png", ".gif" }; foreach (var artist in artists) { - var images = _metaFileService.GetFilesByArtist(artist.Id) + var images = _metaFileService.GetFilesByArtist(artist.Key) .Where(c => c.LastUpdated > new DateTime(2014, 12, 27) && imageExtensions.Any(x => c.RelativePath.EndsWith(x, StringComparison.InvariantCultureIgnoreCase))); foreach (var image in images) { try { - var path = Path.Combine(artist.Path, image.RelativePath); + var path = Path.Combine(artist.Value, image.RelativePath); if (!IsValid(path)) { _logger.Debug("Deleting invalid image file " + path); diff --git a/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs b/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs index 238f3c7e2..0a5b1e7b5 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs @@ -104,22 +104,22 @@ namespace NzbDrone.Core.MediaFiles if (message.DeleteFiles) { var artist = message.Artist; - var allArtists = _artistService.GetAllArtists(); + var allArtists = _artistService.AllArtistPaths(); foreach (var s in allArtists) { - if (s.Id == artist.Id) + if (s.Key == artist.Id) { continue; } - if (artist.Path.IsParentPath(s.Path)) + if (artist.Path.IsParentPath(s.Value)) { _logger.Error("Artist path: '{0}' is a parent of another artist, not deleting files.", artist.Path); return; } - if (artist.Path.PathEquals(s.Path)) + if (artist.Path.PathEquals(s.Value)) { _logger.Error("Artist path: '{0}' is the same as another artist, not deleting files.", artist.Path); return; diff --git a/src/NzbDrone.Core/Music/Repositories/ArtistRepository.cs b/src/NzbDrone.Core/Music/Repositories/ArtistRepository.cs index 7ee30a8f6..dfe0699e9 100644 --- a/src/NzbDrone.Core/Music/Repositories/ArtistRepository.cs +++ b/src/NzbDrone.Core/Music/Repositories/ArtistRepository.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Dapper; using NzbDrone.Common.Extensions; using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; @@ -11,6 +12,7 @@ namespace NzbDrone.Core.Music bool ArtistPathExists(string path); Artist FindByName(string cleanName); Artist FindById(string foreignArtistId); + Dictionary AllArtistPaths(); Artist GetArtistByMetadataId(int artistMetadataId); List GetArtistByMetadataId(IEnumerable artistMetadataId); } @@ -66,6 +68,15 @@ namespace NzbDrone.Core.Music return Query(s => s.ArtistMetadataId == artistMetadataId).SingleOrDefault(); } + public Dictionary AllArtistPaths() + { + using (var conn = _database.OpenConnection()) + { + var strSql = "SELECT Id AS [Key], Path AS [Value] FROM Artists"; + return conn.Query>(strSql).ToDictionary(x => x.Key, x => x.Value); + } + } + public List GetArtistByMetadataId(IEnumerable artistMetadataIds) { return Query(s => artistMetadataIds.Contains(s.ArtistMetadataId)); diff --git a/src/NzbDrone.Core/Music/Services/ArtistService.cs b/src/NzbDrone.Core/Music/Services/ArtistService.cs index 68b78a457..fbc90ad8e 100644 --- a/src/NzbDrone.Core/Music/Services/ArtistService.cs +++ b/src/NzbDrone.Core/Music/Services/ArtistService.cs @@ -26,6 +26,7 @@ namespace NzbDrone.Core.Music List AllForTag(int tagId); Artist UpdateArtist(Artist artist, bool publishUpdatedEvent = true); List UpdateArtists(List artist, bool useExistingRelativeFolder); + Dictionary AllArtistPaths(); bool ArtistPathExists(string folder); void RemoveAddOptions(Artist artist); } @@ -168,6 +169,11 @@ namespace NzbDrone.Core.Music return _cache.Get("GetAllArtists", () => _artistRepository.All().ToList(), TimeSpan.FromSeconds(30)); } + public Dictionary AllArtistPaths() + { + return _artistRepository.AllArtistPaths(); + } + public List AllForTag(int tagId) { return GetAllArtists().Where(s => s.Tags.Contains(tagId)) diff --git a/src/NzbDrone.Core/Validation/Paths/ArtistAncestorValidator.cs b/src/NzbDrone.Core/Validation/Paths/ArtistAncestorValidator.cs index fc4c73d7d..01ba3996c 100644 --- a/src/NzbDrone.Core/Validation/Paths/ArtistAncestorValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/ArtistAncestorValidator.cs @@ -22,7 +22,7 @@ namespace NzbDrone.Core.Validation.Paths return true; } - return !_artistService.GetAllArtists().Any(s => context.PropertyValue.ToString().IsParentPath(s.Path)); + return !_artistService.AllArtistPaths().Any(s => context.PropertyValue.ToString().IsParentPath(s.Value)); } } } diff --git a/src/NzbDrone.Core/Validation/Paths/ArtistPathValidator.cs b/src/NzbDrone.Core/Validation/Paths/ArtistPathValidator.cs index accd36b03..69f6c7c5d 100644 --- a/src/NzbDrone.Core/Validation/Paths/ArtistPathValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/ArtistPathValidator.cs @@ -1,4 +1,5 @@ -using FluentValidation.Validators; +using System.Linq; +using FluentValidation.Validators; using NzbDrone.Common.Extensions; using NzbDrone.Core.Music; @@ -24,7 +25,7 @@ namespace NzbDrone.Core.Validation.Paths dynamic instance = context.ParentContext.InstanceToValidate; var instanceId = (int)instance.Id; - return !_artistService.GetAllArtists().Exists(s => s.Path.PathEquals(context.PropertyValue.ToString()) && s.Id != instanceId); + return !_artistService.AllArtistPaths().Any(s => s.Value.PathEquals(context.PropertyValue.ToString()) && s.Key != instanceId); } } }