From 0c2a1c60b1c3fdc65b2ae613d4890a0ab5765d83 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 20 Aug 2013 18:17:06 -0700 Subject: [PATCH] Speedier unmapped folders lookup. --- NzbDrone.Api/RootFolders/RootFolderModule.cs | 4 +-- NzbDrone.Common/NzbDrone.Common.csproj | 1 + NzbDrone.Common/PathEqualityComparer.cs | 20 ++++++++++++++ NzbDrone.Common/PathExtensions.cs | 7 ++--- .../RootFolders/RootFolderService.cs | 27 ++++++++++--------- 5 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 NzbDrone.Common/PathEqualityComparer.cs diff --git a/NzbDrone.Api/RootFolders/RootFolderModule.cs b/NzbDrone.Api/RootFolders/RootFolderModule.cs index 3accd37df..f160570ff 100644 --- a/NzbDrone.Api/RootFolders/RootFolderModule.cs +++ b/NzbDrone.Api/RootFolders/RootFolderModule.cs @@ -5,9 +5,9 @@ namespace NzbDrone.Api.RootFolders { public class RootFolderModule : NzbDroneRestModule { - private readonly RootFolderService _rootFolderService; + private readonly IRootFolderService _rootFolderService; - public RootFolderModule(RootFolderService rootFolderService) + public RootFolderModule(IRootFolderService rootFolderService) { _rootFolderService = rootFolderService; diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 313cdbc0a..e6d441aa7 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -91,6 +91,7 @@ + diff --git a/NzbDrone.Common/PathEqualityComparer.cs b/NzbDrone.Common/PathEqualityComparer.cs new file mode 100644 index 000000000..c70abb7b7 --- /dev/null +++ b/NzbDrone.Common/PathEqualityComparer.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Common +{ + public class PathEqualityComparer : IEqualityComparer + { + public bool Equals(string x, string y) + { + return x.PathEquals(y); + } + + public int GetHashCode(string obj) + { + return obj.CleanFilePath().GetHashCode(); + } + } +} diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 4e7d2251c..15e3b8a08 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -35,11 +35,12 @@ public static string CleanFilePath(this string path) return info.FullName.TrimEnd('/').Trim('\\', ' '); } - public static bool PathEquals(this string firstPath, string secondPath) { - Ensure.That(() => firstPath).IsValidPath(); - Ensure.That(() => secondPath).IsValidPath(); + if (OsInfo.IsLinux) + { + return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath()); + } return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), StringComparison.InvariantCultureIgnoreCase); } diff --git a/NzbDrone.Core/RootFolders/RootFolderService.cs b/NzbDrone.Core/RootFolders/RootFolderService.cs index a27fa01a7..f85a371f6 100644 --- a/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Diagnostics; +using System.Linq; using System; using System.Collections.Generic; using System.IO; @@ -40,14 +41,14 @@ public RootFolderService(IBasicRepository rootFolderRepository, _configService = configService; } - public virtual List All() + public List All() { var rootFolders = _rootFolderRepository.All().ToList(); return rootFolders; } - public virtual List AllWithUnmappedFolders() + public List AllWithUnmappedFolders() { var rootFolders = _rootFolderRepository.All().ToList(); @@ -63,7 +64,7 @@ public virtual List AllWithUnmappedFolders() return rootFolders; } - public virtual RootFolder Add(RootFolder rootFolder) + public RootFolder Add(RootFolder rootFolder) { var all = All(); @@ -87,12 +88,12 @@ public virtual RootFolder Add(RootFolder rootFolder) return rootFolder; } - public virtual void Remove(int id) + public void Remove(int id) { _rootFolderRepository.Delete(id); } - public virtual List GetUnmappedFolders(string path) + public List GetUnmappedFolders(string path) { Logger.Debug("Generating list of unmapped folders"); if (String.IsNullOrEmpty(path)) @@ -107,13 +108,13 @@ public virtual List GetUnmappedFolders(string path) return results; } - foreach (string seriesFolder in _diskProvider.GetDirectories(path)) + var seriesFolders = _diskProvider.GetDirectories(path).ToList(); + var unmappedFolders = seriesFolders.Except(series.Select(s => s.Path), new PathEqualityComparer()).ToList(); + + foreach (string unmappedFolder in unmappedFolders) { - if (!series.Any(s => s.Path.PathEquals(seriesFolder))) - { - var di = new DirectoryInfo(seriesFolder.Normalize()); - results.Add(new UnmappedFolder { Name = di.Name, Path = di.FullName }); - } + var di = new DirectoryInfo(unmappedFolder.Normalize()); + results.Add(new UnmappedFolder { Name = di.Name, Path = di.FullName }); } if (Path.GetPathRoot(path).Equals(path, StringComparison.InvariantCultureIgnoreCase)) @@ -126,7 +127,7 @@ public virtual List GetUnmappedFolders(string path) return results; } - public virtual Dictionary FreeSpaceOnDrives() + public Dictionary FreeSpaceOnDrives() { var freeSpace = new Dictionary();