1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed: Speed up disk rescaning during series refresh

This commit is contained in:
Mark McDowall 2015-06-13 00:42:16 -07:00
parent 3df5d9444b
commit 4c1e211675
7 changed files with 56 additions and 28 deletions

View File

@ -37,10 +37,15 @@ public static string CleanFilePath(this string path)
return info.FullName.TrimEnd('/').Trim('\\', ' ');
}
public static bool PathEquals(this string firstPath, string secondPath)
public static bool PathEquals(this string firstPath, string secondPath, StringComparison? comparison = null)
{
if (firstPath.Equals(secondPath, OsInfo.PathStringComparison)) return true;
return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), OsInfo.PathStringComparison);
if (!comparison.HasValue)
{
comparison = OsInfo.PathStringComparison;
}
if (firstPath.Equals(secondPath, comparison.Value)) return true;
return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), comparison.Value);
}
public static string GetRelativePath(this string parentPath, string childPath)

View File

@ -56,7 +56,7 @@ public void should_not_scan_if_series_root_folder_does_not_exist()
ExceptionVerification.ExpectedWarns(1);
Mocker.GetMock<IMediaFileTableCleanupService>()
.Verify(v => v.Clean(It.IsAny<Series>()), Times.Never());
.Verify(v => v.Clean(It.IsAny<Series>(), It.IsAny<List<string>>()), Times.Never());
}
[Test]
@ -75,7 +75,7 @@ public void should_not_scan_if_series_root_folder_is_empty()
ExceptionVerification.ExpectedWarns(1);
Mocker.GetMock<IMediaFileTableCleanupService>()
.Verify(v => v.Clean(It.IsAny<Series>()), Times.Never());
.Verify(v => v.Clean(It.IsAny<Series>(), new List<string>()), Times.Never());
}
[Test]

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
@ -25,6 +26,7 @@ public void SetUp()
.ToList();
_series = Builder<Series>.CreateNew()
.With(s => s.Path = @"C:\Test\TV\Series")
.Build();
Mocker.GetMock<IDiskProvider>()
@ -52,6 +54,11 @@ private void GivenFilesAreNotAttachedToEpisode()
.Returns(_episodes);
}
private List<string> FilesOnDisk(IEnumerable<EpisodeFile> episodeFiles)
{
return episodeFiles.Select(e => Path.Combine(_series.Path, e.RelativePath)).ToList();
}
[Test]
public void should_skip_files_that_exist_in_disk()
{
@ -60,7 +67,7 @@ public void should_skip_files_that_exist_in_disk()
GivenEpisodeFiles(episodeFiles);
Subject.Clean(_series);
Subject.Clean(_series, FilesOnDisk(episodeFiles));
Mocker.GetMock<IEpisodeService>().Verify(c => c.UpdateEpisode(It.IsAny<Episode>()), Times.Never());
}
@ -75,7 +82,7 @@ public void should_delete_non_existent_files()
GivenEpisodeFiles(episodeFiles);
Subject.Clean(_series);
Subject.Clean(_series, FilesOnDisk(episodeFiles.Where(e => e.RelativePath != DELETED_PATH)));
Mocker.GetMock<IMediaFileService>().Verify(c => c.Delete(It.Is<EpisodeFile>(e => e.RelativePath == DELETED_PATH), DeleteMediaFileReason.MissingFromDisk), Times.Exactly(2));
}
@ -91,7 +98,7 @@ public void should_delete_files_that_dont_belong_to_any_episodes()
GivenEpisodeFiles(episodeFiles);
GivenFilesAreNotAttachedToEpisode();
Subject.Clean(_series);
Subject.Clean(_series, FilesOnDisk(episodeFiles));
Mocker.GetMock<IMediaFileService>().Verify(c => c.Delete(It.IsAny<EpisodeFile>(), DeleteMediaFileReason.NoLinkedEpisodes), Times.Exactly(10));
}
@ -101,7 +108,7 @@ public void should_unlink_episode_when_episodeFile_does_not_exist()
{
GivenEpisodeFiles(new List<EpisodeFile>());
Subject.Clean(_series);
Subject.Clean(_series, new List<string>());
Mocker.GetMock<IEpisodeService>().Verify(c => c.UpdateEpisode(It.Is<Episode>(e => e.EpisodeFileId == 0)), Times.Exactly(10));
}
@ -116,7 +123,7 @@ public void should_not_update_episode_when_episodeFile_exists()
GivenEpisodeFiles(episodeFiles);
Subject.Clean(_series);
Subject.Clean(_series, FilesOnDisk(episodeFiles));
Mocker.GetMock<IEpisodeService>().Verify(c => c.UpdateEpisode(It.IsAny<Episode>()), Times.Never());
}

View File

@ -686,5 +686,17 @@ public void should_use_Sonarr_as_release_group_when_not_available()
Subject.BuildFileName(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("Sonarr");
}
[TestCase("0SEC")]
[TestCase("2HD")]
[TestCase("IMMERSE")]
public void should_use_existing_casing_for_release_group(string releaseGroup)
{
_episodeFile.ReleaseGroup = releaseGroup;
_namingConfig.StandardEpisodeFormat = "{Release Group}";
Subject.BuildFileName(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be(releaseGroup);
}
}
}

View File

@ -80,7 +80,6 @@ public void Scan(Series series)
}
_logger.ProgressInfo("Scanning disk for {0}", series.Title);
_mediaFileTableCleanupService.Clean(series);
if (!_diskProvider.FolderExists(series.Path))
{
@ -106,6 +105,9 @@ public void Scan(Series series)
videoFilesStopwatch.Stop();
_logger.Trace("Finished getting episode files for: {0} [{1}]", series, videoFilesStopwatch.Elapsed);
_logger.Debug("{0} Cleaning up media files in DB", series);
_mediaFileTableCleanupService.Clean(series, mediaFileList);
var decisionsStopwatch = Stopwatch.StartNew();
var decisions = _importDecisionMaker.GetImportDecisions(mediaFileList, series);
decisionsStopwatch.Stop();

View File

@ -1,55 +1,55 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.MediaFiles
{
public interface IMediaFileTableCleanupService
{
void Clean(Series series);
void Clean(Series series, List<string> filesOnDisk);
}
public class MediaFileTableCleanupService : IMediaFileTableCleanupService
{
private readonly IMediaFileService _mediaFileService;
private readonly IDiskProvider _diskProvider;
private readonly IEpisodeService _episodeService;
private readonly Logger _logger;
public MediaFileTableCleanupService(IMediaFileService mediaFileService,
IDiskProvider diskProvider,
IEpisodeService episodeService,
ISeriesService seriesService,
Logger logger)
{
_mediaFileService = mediaFileService;
_diskProvider = diskProvider;
_episodeService = episodeService;
_logger = logger;
}
public void Clean(Series series)
public void Clean(Series series, List<string> filesOnDisk)
{
var seriesFile = _mediaFileService.GetFilesBySeries(series.Id);
var seriesFiles = _mediaFileService.GetFilesBySeries(series.Id);
var episodes = _episodeService.GetEpisodeBySeries(series.Id);
foreach (var episodeFile in seriesFile)
var filesOnDiskKeys = new HashSet<String>(filesOnDisk, PathEqualityComparer.Instance);
foreach (var seriesFile in seriesFiles)
{
var episodeFile = seriesFile;
var episodeFilePath = Path.Combine(series.Path, episodeFile.RelativePath);
try
{
if (!_diskProvider.FileExists(episodeFilePath))
if (!filesOnDiskKeys.Contains(episodeFilePath))
{
_logger.Debug("File [{0}] no longer exists on disk, removing from db", episodeFilePath);
_mediaFileService.Delete(episodeFile, DeleteMediaFileReason.MissingFromDisk);
_mediaFileService.Delete(seriesFile, DeleteMediaFileReason.MissingFromDisk);
continue;
}
if (!episodes.Any(e => e.EpisodeFileId == episodeFile.Id))
if (episodes.None(e => e.EpisodeFileId == episodeFile.Id))
{
_logger.Debug("File [{0}] is not assigned to any episodes, removing from db", episodeFilePath);
_mediaFileService.Delete(episodeFile, DeleteMediaFileReason.NoLinkedEpisodes);
@ -73,9 +73,11 @@ public void Clean(Series series)
}
}
foreach (var episode in episodes)
foreach (var e in episodes)
{
if (episode.EpisodeFileId > 0 && !seriesFile.Any(f => f.Id == episode.EpisodeFileId))
var episode = e;
if (episode.EpisodeFileId > 0 && seriesFiles.None(f => f.Id == episode.EpisodeFileId))
{
episode.EpisodeFileId = 0;
_episodeService.UpdateEpisode(episode);

View File

@ -89,7 +89,7 @@ private IEnumerable<RenameEpisodeFilePreview> GetPreviews(Series series, List<Ep
var newName = _filenameBuilder.BuildFileName(episodesInFile, series, file);
var newPath = _filenameBuilder.BuildFilePath(series, seasonNumber, newName, Path.GetExtension(episodeFilePath));
if (!episodeFilePath.PathEquals(newPath))
if (!episodeFilePath.PathEquals(newPath, StringComparison.Ordinal))
{
yield return new RenameEpisodeFilePreview
{