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

@ -25,8 +25,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
.ToList(); .ToList();
Mocker.GetMock<IAuthorService>() Mocker.GetMock<IAuthorService>()
.Setup(s => s.GetAllAuthors()) .Setup(s => s.AllAuthorPaths())
.Returns(artist); .Returns(artist.ToDictionary(x => x.Id, x => x.Path));
Mocker.GetMock<IImportListFactory>() Mocker.GetMock<IImportListFactory>()
.Setup(s => s.All()) .Setup(s => s.All())
@ -45,8 +45,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
public void should_not_return_error_when_no_artist() public void should_not_return_error_when_no_artist()
{ {
Mocker.GetMock<IAuthorService>() Mocker.GetMock<IAuthorService>()
.Setup(s => s.GetAllAuthors()) .Setup(s => s.AllAuthorPaths())
.Returns(new List<Author>()); .Returns(new Dictionary<int, string>());
Mocker.GetMock<IImportListFactory>() Mocker.GetMock<IImportListFactory>()
.Setup(s => s.All()) .Setup(s => s.All())

@ -12,6 +12,7 @@ namespace NzbDrone.Core.Books
bool AuthorPathExists(string path); bool AuthorPathExists(string path);
Author FindByName(string cleanName); Author FindByName(string cleanName);
Author FindById(string foreignAuthorId); Author FindById(string foreignAuthorId);
Dictionary<int, string> AllAuthorPaths();
Author GetAuthorByMetadataId(int authorMetadataId); Author GetAuthorByMetadataId(int authorMetadataId);
List<Author> GetAuthorsByMetadataId(IEnumerable<int> authorMetadataId); List<Author> GetAuthorsByMetadataId(IEnumerable<int> authorMetadataId);
} }
@ -55,6 +56,15 @@ namespace NzbDrone.Core.Books
return Query(s => s.CleanName == cleanName).ExclusiveOrDefault(); 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) public Author GetAuthorByMetadataId(int authorMetadataId)
{ {
return Query(s => s.AuthorMetadataId == authorMetadataId).SingleOrDefault(); return Query(s => s.AuthorMetadataId == authorMetadataId).SingleOrDefault();

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

@ -19,8 +19,8 @@ namespace NzbDrone.Core.HealthCheck.Checks
public override HealthCheck Check() 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. // 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() var mounts = _authorService.AllAuthorPaths()
.Select(author => _diskProvider.GetMount(author.Path)) .Select(path => _diskProvider.GetMount(path.Value))
.Where(m => m != null && m.MountOptions != null && m.MountOptions.IsReadOnly) .Where(m => m != null && m.MountOptions != null && m.MountOptions.IsReadOnly)
.DistinctBy(m => m.RootDirectory) .DistinctBy(m => m.RootDirectory)
.ToList(); .ToList();

@ -28,8 +28,8 @@ namespace NzbDrone.Core.HealthCheck.Checks
public override HealthCheck Check() public override HealthCheck Check()
{ {
var rootFolders = _artistService.GetAllAuthors() var rootFolders = _authorService.AllAuthorPaths()
.Select(s => _rootFolderService.GetBestRootFolderPath(s.Path)) .Select(s => _rootFolderService.GetBestRootFolderPath(s.Value))
.Distinct(); .Distinct();
var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s)) var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s))

@ -38,19 +38,19 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
return; return;
} }
var artists = _authorService.GetAllAuthors(); var artists = _authorService.AllAuthorPaths();
var imageExtensions = new List<string> { ".jpg", ".png", ".gif" }; var imageExtensions = new List<string> { ".jpg", ".png", ".gif" };
foreach (var author in artists) 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))); .Where(c => c.LastUpdated > new DateTime(2014, 12, 27) && imageExtensions.Any(x => c.RelativePath.EndsWith(x, StringComparison.InvariantCultureIgnoreCase)));
foreach (var image in images) foreach (var image in images)
{ {
try try
{ {
var path = Path.Combine(author.Path, image.RelativePath); var path = Path.Combine(author.Value, image.RelativePath);
if (!IsValid(path)) if (!IsValid(path))
{ {
_logger.Debug("Deleting invalid image file " + path); _logger.Debug("Deleting invalid image file " + path);

@ -104,22 +104,22 @@ namespace NzbDrone.Core.MediaFiles
if (message.DeleteFiles) if (message.DeleteFiles)
{ {
var author = message.Author; var author = message.Author;
var allArtists = _authorService.GetAllAuthors(); var allArtists = _authorService.AllAuthorPaths();
foreach (var s in allArtists) foreach (var s in allArtists)
{ {
if (s.Id == author.Id) if (s.Key == author.Id)
{ {
continue; 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); _logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
return; 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); _logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
return; return;

@ -22,7 +22,7 @@ namespace NzbDrone.Core.Validation.Paths
return true; 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.Common.Extensions;
using NzbDrone.Core.Books; using NzbDrone.Core.Books;
@ -24,7 +25,7 @@ namespace NzbDrone.Core.Validation.Paths
dynamic instance = context.ParentContext.InstanceToValidate; dynamic instance = context.ParentContext.InstanceToValidate;
var instanceId = (int)instance.Id; 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