Improve use of All() for Path related queries

Signed-off-by: Robin Dadswell <robin@dadswell.email>
pull/770/head
Qstick 4 years ago
parent cf0439d4c5
commit 3af8051e3c

@ -33,9 +33,9 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
_metadata = Builder<MetadataFile>.CreateListOfSize(1)
.Build().ToList();
Mocker.GetMock<IAuthorService>()
.Setup(c => c.GetAllAuthors())
.Returns(_artist);
Mocker.GetMock<IArtistService>()
.Setup(c => c.AllArtistPaths())
.Returns(_artist.ToDictionary(x => x.Id, x => x.Path));
Mocker.GetMock<IMetadataFileService>()
.Setup(c => c.GetFilesByAuthor(_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<IAuthorService>().Verify(c => c.GetAllAuthors(), Times.Never());
Mocker.GetMock<IArtistService>().Verify(c => c.GetAllAuthors(), Times.Never());
AssertImageWasNotRemoved();
}

@ -25,8 +25,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
.ToList();
Mocker.GetMock<IAuthorService>()
.Setup(s => s.GetAllAuthors())
.Returns(artist);
.Setup(s => s.AllAuthorPaths())
.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<IAuthorService>()
.Setup(s => s.GetAllAuthors())
.Returns(new List<Author>());
.Setup(s => s.AllAuthorPaths())
.Returns(new Dictionary<int, string>());
Mocker.GetMock<IImportListFactory>()
.Setup(s => s.All())

@ -12,6 +12,7 @@ namespace NzbDrone.Core.Books
bool AuthorPathExists(string path);
Author FindByName(string cleanName);
Author FindById(string foreignAuthorId);
Dictionary<int, string> AllAuthorPaths();
Author GetAuthorByMetadataId(int authorMetadataId);
List<Author> GetAuthorsByMetadataId(IEnumerable<int> authorMetadataId);
}
@ -55,6 +56,15 @@ namespace NzbDrone.Core.Books
return Query(s => s.CleanName == cleanName).ExclusiveOrDefault();
}
public Dictionary<int, string> AllAuthorPaths()
{
using (var conn = _database.OpenConnection())
{
var strSql = "SELECT Id AS [Key], Path AS [Value] FROM Authors";
return conn.Query<KeyValuePair<int, string>>(strSql).ToDictionary(x => x.Key, x => x.Value);
}
}
public Author GetAuthorByMetadataId(int authorMetadataId)
{
return Query(s => s.AuthorMetadataId == authorMetadataId).SingleOrDefault();

@ -27,6 +27,7 @@ namespace NzbDrone.Core.Books
List<Author> AllForTag(int tagId);
Author UpdateAuthor(Author author);
List<Author> UpdateAuthors(List<Author> authors, bool useExistingRelativeFolder);
Dictionary<int, string> AllAuthorPaths();
bool AuthorPathExists(string folder);
void RemoveAddOptions(Author author);
}
@ -194,6 +195,11 @@ namespace NzbDrone.Core.Books
return _cache.Get("GetAllAuthors", () => _authorRepository.All().ToList(), TimeSpan.FromSeconds(30));
}
public Dictionary<int, string> AllAuthorPaths()
{
return _authorRepository.AllAuthorPaths();
}
public List<Author> AllForTag(int tagId)
{
return GetAllAuthors().Where(s => s.Tags.Contains(tagId))

@ -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 = _authorService.GetAllAuthors()
.Select(author => _diskProvider.GetMount(author.Path))
var mounts = _authorService.AllAuthorPaths()
.Select(path => _diskProvider.GetMount(path.Value))
.Where(m => m != null && m.MountOptions != null && m.MountOptions.IsReadOnly)
.DistinctBy(m => m.RootDirectory)
.ToList();

@ -28,8 +28,8 @@ namespace NzbDrone.Core.HealthCheck.Checks
public override HealthCheck Check()
{
var rootFolders = _artistService.GetAllAuthors()
.Select(s => _rootFolderService.GetBestRootFolderPath(s.Path))
var rootFolders = _authorService.AllAuthorPaths()
.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 = _authorService.GetAllAuthors();
var artists = _authorService.AllAuthorPaths();
var imageExtensions = new List<string> { ".jpg", ".png", ".gif" };
foreach (var author in artists)
{
var images = _metaFileService.GetFilesByAuthor(author.Id)
var images = _metaFileService.GetFilesByAuthor(author.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(author.Path, image.RelativePath);
var path = Path.Combine(author.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 author = message.Author;
var allArtists = _authorService.GetAllAuthors();
var allArtists = _authorService.AllAuthorPaths();
foreach (var s in allArtists)
{
if (s.Id == author.Id)
if (s.Key == author.Id)
{
continue;
}
if (author.Path.IsParentPath(s.Path))
if (author.Path.IsParentPath(s.Value))
{
_logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
return;
}
if (author.Path.PathEquals(s.Path))
if (author.Path.PathEquals(s.Value))
{
_logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
return;

@ -22,7 +22,7 @@ namespace NzbDrone.Core.Validation.Paths
return true;
}
return !_authorService.GetAllAuthors().Any(s => context.PropertyValue.ToString().IsParentPath(s.Path));
return !_authorService.AllAuthorPaths().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.Books;
@ -24,7 +25,7 @@ namespace NzbDrone.Core.Validation.Paths
dynamic instance = context.ParentContext.InstanceToValidate;
var instanceId = (int)instance.Id;
return !_authorService.GetAllAuthors().Exists(s => s.Path.PathEquals(context.PropertyValue.ToString()) && s.Id != instanceId);
return !_authorService.AllAuthorPaths().Any(s => s.Value.PathEquals(context.PropertyValue.ToString()) && s.Key != instanceId);
}
}
}

Loading…
Cancel
Save