diff --git a/src/NzbDrone.Core.Test/DiskSpace/DiskSpaceServiceFixture.cs b/src/NzbDrone.Core.Test/DiskSpace/DiskSpaceServiceFixture.cs index d07f0470b..12878b54c 100644 --- a/src/NzbDrone.Core.Test/DiskSpace/DiskSpaceServiceFixture.cs +++ b/src/NzbDrone.Core.Test/DiskSpace/DiskSpaceServiceFixture.cs @@ -47,11 +47,11 @@ namespace NzbDrone.Core.Test.DiskSpace GivenSeries(); } - private void GivenSeries(params Series[] series) + private void GivenSeries(params string[] seriesPaths) { Mocker.GetMock() - .Setup(v => v.GetAllSeries()) - .Returns(series.ToList()); + .Setup(v => v.GetAllSeriesPaths()) + .Returns(seriesPaths.ToList()); } private void GivenExistingFolder(string folder) @@ -64,7 +64,7 @@ namespace NzbDrone.Core.Test.DiskSpace [Test] public void should_check_diskspace_for_series_folders() { - GivenSeries(new Series { Path = _seriesFolder }); + GivenSeries(_seriesFolder); GivenExistingFolder(_seriesFolder); @@ -76,7 +76,7 @@ namespace NzbDrone.Core.Test.DiskSpace [Test] public void should_check_diskspace_for_same_root_folder_only_once() { - GivenSeries(new Series { Path = _seriesFolder }, new Series { Path = _seriesFolder2 }); + GivenSeries(_seriesFolder, _seriesFolder2); GivenExistingFolder(_seriesFolder); GivenExistingFolder(_seriesFolder2); @@ -92,7 +92,7 @@ namespace NzbDrone.Core.Test.DiskSpace [Test] public void should_not_check_diskspace_for_missing_series_folders() { - GivenSeries(new Series { Path = _seriesFolder }); + GivenSeries(_seriesFolder); var freeSpace = Subject.GetFreeSpace(); diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs index 45ad31207..000c2b64c 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/RootFolderCheckFixture.cs @@ -20,8 +20,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks .ToList(); Mocker.GetMock() - .Setup(s => s.GetAllSeries()) - .Returns(series); + .Setup(s => s.GetAllSeriesPaths()) + .Returns(series.Select(s => s.Path).ToList()); Mocker.GetMock() .Setup(s => s.GetParentFolder(series.First().Path)) @@ -36,8 +36,8 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks public void should_not_return_error_when_no_series() { Mocker.GetMock() - .Setup(s => s.GetAllSeries()) - .Returns(new List()); + .Setup(s => s.GetAllSeriesPaths()) + .Returns(new List()); Subject.Check().ShouldBeOk(); } diff --git a/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs b/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs index 9ae24dc36..5847e8823 100644 --- a/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs +++ b/src/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs @@ -44,6 +44,10 @@ namespace NzbDrone.Core.Test.RootFolderTests [TestCase("//server//folder")] public void should_be_able_to_add_root_dir(string path) { + Mocker.GetMock() + .Setup(s => s.AllSeriesPaths()) + .Returns(new List()); + var root = new RootFolder { Path = path.AsOsAgnostic() }; Subject.Add(root); @@ -124,9 +128,9 @@ namespace NzbDrone.Core.Test.RootFolderTests .Setup(s => s.Get(It.IsAny())) .Returns(rootFolder); - Mocker.GetMock() - .Setup(s => s.GetAllSeries()) - .Returns(new List()); + Mocker.GetMock() + .Setup(s => s.AllSeriesPaths()) + .Returns(new List()); Mocker.GetMock() .Setup(s => s.GetDirectories(rootFolder.Path)) diff --git a/src/NzbDrone.Core/DiskSpace/DiskSpaceService.cs b/src/NzbDrone.Core/DiskSpace/DiskSpaceService.cs index f2a1e95ed..a78036e0a 100644 --- a/src/NzbDrone.Core/DiskSpace/DiskSpaceService.cs +++ b/src/NzbDrone.Core/DiskSpace/DiskSpaceService.cs @@ -42,9 +42,9 @@ namespace NzbDrone.Core.DiskSpace private IEnumerable GetSeriesRootPaths() { - return _seriesService.GetAllSeries() - .Where(s => _diskProvider.FolderExists(s.Path)) - .Select(s => _diskProvider.GetPathRoot(s.Path)) + return _seriesService.GetAllSeriesPaths() + .Where(s => _diskProvider.FolderExists(s)) + .Select(s => _diskProvider.GetPathRoot(s)) .Distinct(); } diff --git a/src/NzbDrone.Core/HealthCheck/Checks/MountCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/MountCheck.cs index 6e79be2d6..16740b0a9 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 = _seriesService.GetAllSeries() - .Select(series => _diskProvider.GetMount(series.Path)) + var mounts = _seriesService.GetAllSeriesPaths() + .Select(s => _diskProvider.GetMount(s)) .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 cbb507095..9a57d0890 100644 --- a/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs +++ b/src/NzbDrone.Core/HealthCheck/Checks/RootFolderCheck.cs @@ -1,4 +1,6 @@ +using System.Diagnostics; using System.Linq; +using NLog; using NzbDrone.Common.Disk; using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.RootFolders; @@ -26,9 +28,7 @@ namespace NzbDrone.Core.HealthCheck.Checks public override HealthCheck Check() { - var rootFolders = _seriesService.GetAllSeries() - .Select(s => _rootFolderService.GetBestRootFolderPath(s.Path)) - .Distinct(); + var rootFolders = _seriesService.GetAllSeriesPaths().Select(s => _rootFolderService.GetBestRootFolderPath(s)).Distinct(); var missingRootFolders = rootFolders.Where(s => !_diskProvider.FolderExists(s)) .ToList(); diff --git a/src/NzbDrone.Core/RootFolders/RootFolderService.cs b/src/NzbDrone.Core/RootFolders/RootFolderService.cs index cf1d1ce6e..b88e1e58d 100644 --- a/src/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/src/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -63,6 +63,7 @@ namespace NzbDrone.Core.RootFolders public List AllWithUnmappedFolders() { var rootFolders = _rootFolderRepository.All().ToList(); + var seriesPaths = _seriesRepository.AllSeriesPaths(); rootFolders.ForEach(folder => { @@ -70,7 +71,7 @@ namespace NzbDrone.Core.RootFolders { if (folder.Path.IsPathValid()) { - GetDetails(folder, true); + GetDetails(folder, seriesPaths, true); } } //We don't want an exception to prevent the root folders from loading in the UI, so they can still be deleted @@ -109,8 +110,9 @@ namespace NzbDrone.Core.RootFolders } _rootFolderRepository.Insert(rootFolder); + var seriesPaths = _seriesRepository.AllSeriesPaths(); - GetDetails(rootFolder, true); + GetDetails(rootFolder, seriesPaths, true); return rootFolder; } @@ -120,7 +122,7 @@ namespace NzbDrone.Core.RootFolders _rootFolderRepository.Delete(id); } - private List GetUnmappedFolders(string path) + private List GetUnmappedFolders(string path, List seriesPaths) { _logger.Debug("Generating list of unmapped folders"); @@ -130,7 +132,6 @@ namespace NzbDrone.Core.RootFolders } var results = new List(); - var series = _seriesRepository.All().ToList(); if (!_diskProvider.FolderExists(path)) { @@ -139,7 +140,7 @@ namespace NzbDrone.Core.RootFolders } var possibleSeriesFolders = _diskProvider.GetDirectories(path).ToList(); - var unmappedFolders = possibleSeriesFolders.Except(series.Select(s => s.Path), PathEqualityComparer.Instance).ToList(); + var unmappedFolders = possibleSeriesFolders.Except(seriesPaths, PathEqualityComparer.Instance).ToList(); foreach (string unmappedFolder in unmappedFolders) { @@ -157,7 +158,9 @@ namespace NzbDrone.Core.RootFolders public RootFolder Get(int id, bool timeout) { var rootFolder = _rootFolderRepository.Get(id); - GetDetails(rootFolder, timeout); + var seriesPaths = _seriesRepository.AllSeriesPaths(); + + GetDetails(rootFolder, seriesPaths, timeout); return rootFolder; } @@ -176,7 +179,7 @@ namespace NzbDrone.Core.RootFolders return possibleRootFolder.Path; } - private void GetDetails(RootFolder rootFolder, bool timeout) + private void GetDetails(RootFolder rootFolder, List seriesPaths, bool timeout) { Task.Run(() => { @@ -185,7 +188,7 @@ namespace NzbDrone.Core.RootFolders rootFolder.Accessible = true; rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path); rootFolder.TotalSpace = _diskProvider.GetTotalSize(rootFolder.Path); - rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path); + rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path, seriesPaths); } }).Wait(timeout ? 5000 : -1); } diff --git a/src/NzbDrone.Core/Tv/SeriesRepository.cs b/src/NzbDrone.Core/Tv/SeriesRepository.cs index bc8f8f97f..e15b2df16 100644 --- a/src/NzbDrone.Core/Tv/SeriesRepository.cs +++ b/src/NzbDrone.Core/Tv/SeriesRepository.cs @@ -16,6 +16,7 @@ namespace NzbDrone.Core.Tv Series FindByTvdbId(int tvdbId); Series FindByTvRageId(int tvRageId); Series FindByPath(string path); + List AllSeriesPaths(); } public class SeriesRepository : BasicRepository, ISeriesRepository @@ -78,6 +79,13 @@ namespace NzbDrone.Core.Tv .FirstOrDefault(); } + public List AllSeriesPaths() + { + var mapper = _database.GetDataMapper(); + + return mapper.Query("SELECT Path from Series"); + } + private Series ReturnSingleSeriesOrThrow(List series) { if (series.Count == 0) diff --git a/src/NzbDrone.Core/Tv/SeriesService.cs b/src/NzbDrone.Core/Tv/SeriesService.cs index 533a321fd..0699e9d91 100644 --- a/src/NzbDrone.Core/Tv/SeriesService.cs +++ b/src/NzbDrone.Core/Tv/SeriesService.cs @@ -24,6 +24,7 @@ namespace NzbDrone.Core.Tv Series FindByPath(string path); void DeleteSeries(int seriesId, bool deleteFiles, bool addImportListExclusion); List GetAllSeries(); + List GetAllSeriesPaths(); List AllForTag(int tagId); Series UpdateSeries(Series series, bool updateEpisodesToMatchSeason = true, bool publishUpdatedEvent = true); List UpdateSeries(List series, bool useExistingRelativeFolder); @@ -157,6 +158,11 @@ namespace NzbDrone.Core.Tv return _seriesRepository.All().ToList(); } + public List GetAllSeriesPaths() + { + return _seriesRepository.AllSeriesPaths(); + } + public List AllForTag(int tagId) { return GetAllSeries().Where(s => s.Tags.Contains(tagId)) diff --git a/src/NzbDrone.Core/Validation/Paths/SeriesAncestorValidator.cs b/src/NzbDrone.Core/Validation/Paths/SeriesAncestorValidator.cs index 850118a0d..ff014aea7 100644 --- a/src/NzbDrone.Core/Validation/Paths/SeriesAncestorValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/SeriesAncestorValidator.cs @@ -19,7 +19,7 @@ namespace NzbDrone.Core.Validation.Paths { if (context.PropertyValue == null) return true; - return !_seriesService.GetAllSeries().Any(s => context.PropertyValue.ToString().IsParentPath(s.Path)); + return !_seriesService.GetAllSeriesPaths().Any(s => context.PropertyValue.ToString().IsParentPath(s)); } } }