From 3c8f7d380f6f2b719dd4ededebcc2505ef24baad Mon Sep 17 00:00:00 2001 From: "Petrus.Z" Date: Wed, 17 Nov 2021 20:56:14 +0800 Subject: [PATCH] Improve performance Signed-off-by: Petrus.Z --- .../Validators/CollectionPostScanTask.cs | 100 ++++++++---------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/Emby.Server.Implementations/Library/Validators/CollectionPostScanTask.cs b/Emby.Server.Implementations/Library/Validators/CollectionPostScanTask.cs index 3deaf7fe78..37b0c439c0 100644 --- a/Emby.Server.Implementations/Library/Validators/CollectionPostScanTask.cs +++ b/Emby.Server.Implementations/Library/Validators/CollectionPostScanTask.cs @@ -10,7 +10,7 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Model.Querying; using Jellyfin.Data.Enums; using Microsoft.Extensions.Logging; -using System.Diagnostics; +using MediaBrowser.Model.Entities; namespace Emby.Server.Implementations.Library.Validators { @@ -47,14 +47,6 @@ namespace Emby.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var movies = _libraryManager.GetItemList(new InternalItemsQuery - { - IncludeItemTypes = new[] { nameof(Movie) }, - IsVirtualItem = false, - OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }, - Recursive = true - }); - var boxSets = _libraryManager.GetItemList(new InternalItemsQuery { IncludeItemTypes = new[] { nameof(BoxSet) }, @@ -62,36 +54,47 @@ namespace Emby.Server.Implementations.Library.Validators Recursive = true }); - var numComplete = 0; - var count = movies.Count; - var collectionNameMoviesMap = new Dictionary>(); - foreach (var m in movies) - { - if (m is Movie movie && !string.IsNullOrEmpty(movie.CollectionName)) - { - if (collectionNameMoviesMap.TryGetValue(movie.CollectionName, out var movieList)) - { - if (!movieList.Any(m => m.Id == movie.Id)) - { - movieList.Add(movie); - } - } - else - { - collectionNameMoviesMap[movie.CollectionName] = new List { movie }; - } + foreach (var library in _libraryManager.RootFolder.Children.ToList()) { + if (!_libraryManager.GetLibraryOptions(library).AutoCollection) { + continue; } - numComplete++; - double percent = numComplete; - percent /= count * 2; - percent *= 100; + var movies = _libraryManager.GetItemList(new InternalItemsQuery + { + MediaTypes = new string[] { MediaType.Video }, + IncludeItemTypes = new[] { nameof(Movie) }, + IsVirtualItem = false, + OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }, + SourceTypes = new[] { SourceType.Library }, + Parent = library, + Recursive = true + }); - progress.Report(percent); + foreach (var m in movies) + { + if (m is Movie movie && !string.IsNullOrEmpty(movie.CollectionName)) + { + if (collectionNameMoviesMap.TryGetValue(movie.CollectionName, out var movieList)) + { + if (!movieList.Any(m => m.Id == movie.Id)) + { + movieList.Add(movie); + } + } + else + { + collectionNameMoviesMap[movie.CollectionName] = new List { movie }; + } + + } + } } + var numComplete = 0; + var count = collectionNameMoviesMap.Count; + foreach (var (collectionName, movieList) in collectionNameMoviesMap) { try @@ -102,28 +105,23 @@ namespace Emby.Server.Implementations.Library.Validators // won't automatically create collection if only one movie in it if (movieList.Count >= 2) { - var movieIds = FliterMoviesByOption(movieList); - if (movieIds.Count >= 2) { - // at least 2 movies have AutoCollection option enable - boxSet = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions - { - Name = collectionName, - IsLocked = true - }); + boxSet = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions + { + Name = collectionName, + IsLocked = true + }); - await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds); - } + await _collectionManager.AddToCollectionAsync(boxSet.Id, movieList.Select(m => m.Id)); } } else { - var movieIds = FliterMoviesByOption(movieList); - await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds); + await _collectionManager.AddToCollectionAsync(boxSet.Id, movieList.Select(m => m.Id)); } numComplete++; double percent = numComplete; - percent /= count * 2; + percent /= count; percent *= 100; progress.Report(percent); @@ -136,17 +134,5 @@ namespace Emby.Server.Implementations.Library.Validators progress.Report(100); } - - private List FliterMoviesByOption(List movieList) { - List movieIds = new List(); - foreach (var movie in movieList) - { - if (_libraryManager.GetLibraryOptions(movie).AutoCollection) - { - movieIds.Add(movie.Id); - } - } - return movieIds; - } } } \ No newline at end of file