Improve use of All() for Path related queries

pull/1807/head
Qstick 3 years ago
parent 796eba82ac
commit 2e4b168985

@ -34,8 +34,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
.Build().ToList();
Mocker.GetMock<IArtistService>()
.Setup(c => c.GetAllArtists())
.Returns(_artist);
.Setup(c => c.AllArtistPaths())
.Returns(_artist.ToDictionary(x => x.Id, x => x.Path));
Mocker.GetMock<IMetadataFileService>()
.Setup(c => c.GetFilesByArtist(_artist.First().Id))
@ -73,7 +73,7 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
Subject.Clean();
Mocker.GetMock<IConfigService>().VerifySet(c => c.CleanupMetadataImages = true, Times.Never());
Mocker.GetMock<IArtistService>().Verify(c => c.GetAllArtists(), Times.Never());
Mocker.GetMock<IArtistService>().Verify(c => c.AllArtistPaths(), Times.Never());
AssertImageWasNotRemoved();
}

@ -25,8 +25,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
.ToList();
Mocker.GetMock<IArtistService>()
.Setup(s => s.GetAllArtists())
.Returns(artist);
.Setup(s => s.AllArtistPaths())
.Returns(artist.ToDictionary(x => x.Id, x => x.Path));
Mocker.GetMock<IImportListFactory>()
.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<IArtistService>()
.Setup(s => s.GetAllArtists())
.Returns(new List<Artist>());
.Setup(s => s.AllArtistPaths())
.Returns(new Dictionary<int, string>());
Mocker.GetMock<IImportListFactory>()
.Setup(s => s.All())

@ -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();

@ -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))

@ -38,19 +38,19 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
return;
}
var artists = _artistService.GetAllArtists();
var artists = _artistService.AllArtistPaths();
var imageExtensions = new List<string> { ".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);

@ -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;

@ -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<int, string> AllArtistPaths();
Artist GetArtistByMetadataId(int artistMetadataId);
List<Artist> GetArtistByMetadataId(IEnumerable<int> artistMetadataId);
}
@ -66,6 +68,15 @@ namespace NzbDrone.Core.Music
return Query(s => s.ArtistMetadataId == artistMetadataId).SingleOrDefault();
}
public Dictionary<int, string> AllArtistPaths()
{
using (var conn = _database.OpenConnection())
{
var strSql = "SELECT Id AS [Key], Path AS [Value] FROM Artists";
return conn.Query<KeyValuePair<int, string>>(strSql).ToDictionary(x => x.Key, x => x.Value);
}
}
public List<Artist> GetArtistByMetadataId(IEnumerable<int> artistMetadataIds)
{
return Query(s => artistMetadataIds.Contains(s.ArtistMetadataId));

@ -26,6 +26,7 @@ namespace NzbDrone.Core.Music
List<Artist> AllForTag(int tagId);
Artist UpdateArtist(Artist artist, bool publishUpdatedEvent = true);
List<Artist> UpdateArtists(List<Artist> artist, bool useExistingRelativeFolder);
Dictionary<int, string> 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<int, string> AllArtistPaths()
{
return _artistRepository.AllArtistPaths();
}
public List<Artist> AllForTag(int tagId)
{
return GetAllArtists().Where(s => s.Tags.Contains(tagId))

@ -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));
}
}
}

@ -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);
}
}
}

Loading…
Cancel
Save