From 319a956b3870e162b3072b52ea8c7310d5091bc6 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 6 Aug 2016 00:38:01 -0400 Subject: [PATCH] update validators --- MediaBrowser.Api/VideosService.cs | 18 +++++++--- .../Library/ILibraryManager.cs | 1 + .../Persistence/IItemRepository.cs | 1 + .../Library/LibraryManager.cs | 11 ++++++ .../Library/Validators/ArtistsValidator.cs | 34 +++++++++++-------- .../Library/Validators/GameGenresValidator.cs | 17 +++++----- .../Library/Validators/GenresValidator.cs | 17 +++++----- .../Validators/MusicGenresValidator.cs | 18 +++++----- .../Library/Validators/StudiosValidator.cs | 16 ++++----- .../Persistence/SqliteItemRepository.cs | 30 ++++++++++------ 10 files changed, 99 insertions(+), 64 deletions(-) diff --git a/MediaBrowser.Api/VideosService.cs b/MediaBrowser.Api/VideosService.cs index c8dbb7bb2..4a5eb1eab 100644 --- a/MediaBrowser.Api/VideosService.cs +++ b/MediaBrowser.Api/VideosService.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using CommonIO; +using MediaBrowser.Model.Dto; namespace MediaBrowser.Api { @@ -81,11 +82,18 @@ namespace MediaBrowser.Api var dtoOptions = GetDtoOptions(request); - var video = (Video)item; - - var items = video.GetAdditionalParts() - .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, video)) - .ToArray(); + var video = item as Video; + BaseItemDto[] items; + if (video != null) + { + items = video.GetAdditionalParts() + .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, video)) + .ToArray(); + } + else + { + items = new BaseItemDto[] { }; + } var result = new ItemsResult { diff --git a/MediaBrowser.Controller/Library/ILibraryManager.cs b/MediaBrowser.Controller/Library/ILibraryManager.cs index 70bd3f081..ff7f2fe67 100644 --- a/MediaBrowser.Controller/Library/ILibraryManager.cs +++ b/MediaBrowser.Controller/Library/ILibraryManager.cs @@ -562,5 +562,6 @@ namespace MediaBrowser.Controller.Library QueryResult> GetStudios(InternalItemsQuery query); QueryResult> GetArtists(InternalItemsQuery query); QueryResult> GetAlbumArtists(InternalItemsQuery query); + QueryResult> GetAllArtists(InternalItemsQuery query); } } \ No newline at end of file diff --git a/MediaBrowser.Controller/Persistence/IItemRepository.cs b/MediaBrowser.Controller/Persistence/IItemRepository.cs index 78138999c..437f0e157 100644 --- a/MediaBrowser.Controller/Persistence/IItemRepository.cs +++ b/MediaBrowser.Controller/Persistence/IItemRepository.cs @@ -169,6 +169,7 @@ namespace MediaBrowser.Controller.Persistence QueryResult> GetStudios(InternalItemsQuery query); QueryResult> GetArtists(InternalItemsQuery query); QueryResult> GetAlbumArtists(InternalItemsQuery query); + QueryResult> GetAllArtists(InternalItemsQuery query); } } diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 7af495f5a..055fde504 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -1385,6 +1385,17 @@ namespace MediaBrowser.Server.Implementations.Library return ItemRepository.GetMusicGenres(query); } + public QueryResult> GetAllArtists(InternalItemsQuery query) + { + if (query.User != null) + { + AddUserToQuery(query, query.User); + } + + SetTopParentOrAncestorIds(query); + return ItemRepository.GetAllArtists(query); + } + public QueryResult> GetArtists(InternalItemsQuery query) { if (query.User != null) diff --git a/MediaBrowser.Server.Implementations/Library/Validators/ArtistsValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/ArtistsValidator.cs index 2b68f98ca..353be1a44 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/ArtistsValidator.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/ArtistsValidator.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Controller.Entities; namespace MediaBrowser.Server.Implementations.Library.Validators { @@ -43,36 +44,39 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var allSongs = _libraryManager.RootFolder - .GetRecursiveChildren(i => !i.IsFolder && i is IHasArtist) - .Cast() + var items = _libraryManager.GetAllArtists(new InternalItemsQuery()) + .Items + .Select(i => i.Item1) .ToList(); - var allArtists = _libraryManager.GetArtists(allSongs).ToList(); - var numComplete = 0; - var numArtists = allArtists.Count; + var count = items.Count; - foreach (var artistItem in allArtists) + foreach (var item in items) { - cancellationToken.ThrowIfCancellationRequested(); - try { - await artistItem.RefreshMetadata(cancellationToken).ConfigureAwait(false); + await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); } - catch (IOException ex) + catch (OperationCanceledException) { - _logger.ErrorException("Error validating Artist {0}", ex, artistItem.Name); + // Don't clutter the log + break; + } + catch (Exception ex) + { + _logger.ErrorException("Error refreshing {0}", ex, item.Name); } - // Update progress numComplete++; double percent = numComplete; - percent /= numArtists; + percent /= count; + percent *= 100; - progress.Report(100 * percent); + progress.Report(percent); } + + progress.Report(100); } } } diff --git a/MediaBrowser.Server.Implementations/Library/Validators/GameGenresValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/GameGenresValidator.cs index a149da651..72864790b 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/GameGenresValidator.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/GameGenresValidator.cs @@ -34,21 +34,22 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var items = _libraryManager.RootFolder.GetRecursiveChildren(i => i is Game) - .SelectMany(i => i.Genres) - .DistinctNames() + var items = _libraryManager.GetGameGenres(new InternalItemsQuery + { + IncludeItemTypes = new[] { typeof(Game).Name } + }) + .Items + .Select(i => i.Item1) .ToList(); var numComplete = 0; var count = items.Count; - foreach (var name in items) + foreach (var item in items) { try { - var itemByName = _libraryManager.GetGameGenre(name); - - await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false); + await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { @@ -57,7 +58,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators } catch (Exception ex) { - _logger.ErrorException("Error refreshing {0}", ex, name); + _logger.ErrorException("Error refreshing {0}", ex, item.Name); } numComplete++; diff --git a/MediaBrowser.Server.Implementations/Library/Validators/GenresValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/GenresValidator.cs index fac5cfc35..6a62d7fe4 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/GenresValidator.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/GenresValidator.cs @@ -35,21 +35,22 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var items = _libraryManager.RootFolder.GetRecursiveChildren(i => !(i is IHasMusicGenres) && !(i is Game)) - .SelectMany(i => i.Genres) - .DistinctNames() + var items = _libraryManager.GetGenres(new InternalItemsQuery + { + ExcludeItemTypes = new[] { typeof(Audio).Name, typeof(MusicArtist).Name, typeof(MusicAlbum).Name, typeof(MusicVideo).Name, typeof(Game).Name } + }) + .Items + .Select(i => i.Item1) .ToList(); var numComplete = 0; var count = items.Count; - foreach (var name in items) + foreach (var item in items) { try { - var itemByName = _libraryManager.GetGenre(name); - - await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false); + await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { @@ -58,7 +59,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators } catch (Exception ex) { - _logger.ErrorException("Error refreshing {0}", ex, name); + _logger.ErrorException("Error refreshing {0}", ex, item.Name); } numComplete++; diff --git a/MediaBrowser.Server.Implementations/Library/Validators/MusicGenresValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/MusicGenresValidator.cs index 81433e5b3..2668d84e9 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/MusicGenresValidator.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/MusicGenresValidator.cs @@ -5,6 +5,7 @@ using System; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Controller.Entities; namespace MediaBrowser.Server.Implementations.Library.Validators { @@ -34,21 +35,22 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var items = _libraryManager.RootFolder.GetRecursiveChildren(i => i is IHasMusicGenres) - .SelectMany(i => i.Genres) - .DistinctNames() + var items = _libraryManager.GetMusicGenres(new InternalItemsQuery + { + IncludeItemTypes = new[] { typeof(Audio).Name, typeof(MusicArtist).Name, typeof(MusicAlbum).Name, typeof(MusicVideo).Name } + }) + .Items + .Select(i => i.Item1) .ToList(); var numComplete = 0; var count = items.Count; - foreach (var name in items) + foreach (var item in items) { try { - var itemByName = _libraryManager.GetMusicGenre(name); - - await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false); + await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { @@ -57,7 +59,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators } catch (Exception ex) { - _logger.ErrorException("Error refreshing {0}", ex, name); + _logger.ErrorException("Error refreshing {0}", ex, item.Name); } numComplete++; diff --git a/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs index 259878566..722b74891 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs @@ -1,10 +1,10 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Model.Logging; using System; -using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Controller.Entities; namespace MediaBrowser.Server.Implementations.Library.Validators { @@ -34,21 +34,19 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var items = _libraryManager.RootFolder.GetRecursiveChildren(i => true) - .SelectMany(i => i.Studios) - .DistinctNames() + var items = _libraryManager.GetStudios(new InternalItemsQuery()) + .Items + .Select(i => i.Item1) .ToList(); var numComplete = 0; var count = items.Count; - foreach (var name in items) + foreach (var item in items) { try { - var itemByName = _libraryManager.GetStudio(name); - - await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false); + await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { @@ -57,7 +55,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators } catch (Exception ex) { - _logger.ErrorException("Error refreshing {0}", ex, name); + _logger.ErrorException("Error refreshing {0}", ex, item.Name); } numComplete++; diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs index 2ef878a20..21785bcbd 100644 --- a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs +++ b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs @@ -3817,37 +3817,42 @@ namespace MediaBrowser.Server.Implementations.Persistence } } + public QueryResult> GetAllArtists(InternalItemsQuery query) + { + return GetItemValues(query, new[] { 0, 1 }, typeof(MusicArtist).FullName); + } + public QueryResult> GetArtists(InternalItemsQuery query) { - return GetItemValues(query, 0, typeof(MusicArtist).FullName); + return GetItemValues(query, new[] { 0 }, typeof(MusicArtist).FullName); } public QueryResult> GetAlbumArtists(InternalItemsQuery query) { - return GetItemValues(query, 1, typeof(MusicArtist).FullName); + return GetItemValues(query, new[] { 1 }, typeof(MusicArtist).FullName); } public QueryResult> GetStudios(InternalItemsQuery query) { - return GetItemValues(query, 3, typeof(Studio).FullName); + return GetItemValues(query, new[] { 3 }, typeof(Studio).FullName); } public QueryResult> GetGenres(InternalItemsQuery query) { - return GetItemValues(query, 2, typeof(Genre).FullName); + return GetItemValues(query, new[] { 2 }, typeof(Genre).FullName); } public QueryResult> GetGameGenres(InternalItemsQuery query) { - return GetItemValues(query, 2, typeof(GameGenre).FullName); + return GetItemValues(query, new[] { 2 }, typeof(GameGenre).FullName); } public QueryResult> GetMusicGenres(InternalItemsQuery query) { - return GetItemValues(query, 2, typeof(MusicGenre).FullName); + return GetItemValues(query, new[] { 2 }, typeof(MusicGenre).FullName); } - private QueryResult> GetItemValues(InternalItemsQuery query, int itemValueType, string returnType) + private QueryResult> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType) { if (query == null) { @@ -3863,6 +3868,10 @@ namespace MediaBrowser.Server.Implementations.Persistence var now = DateTime.UtcNow; + var typeClause = itemValueTypes.Length == 1 ? + ("Type=" + itemValueTypes[0].ToString(CultureInfo.InvariantCulture)) : + ("Type in (" + string.Join(",", itemValueTypes.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToArray()) + ")"); + using (var cmd = _connection.CreateCommand()) { var itemCountColumns = new List>(); @@ -3887,7 +3896,7 @@ namespace MediaBrowser.Server.Implementations.Persistence }; var whereClauses = GetWhereClauses(typeSubQuery, cmd, "itemTypes"); - whereClauses.Add("guid in (select ItemId from ItemValues where ItemValues.CleanValue=A.CleanName AND Type=@ItemValueType)"); + whereClauses.Add("guid in (select ItemId from ItemValues where ItemValues.CleanValue=A.CleanName AND " + typeClause + ")"); var typeWhereText = whereClauses.Count == 0 ? string.Empty : @@ -3929,12 +3938,12 @@ namespace MediaBrowser.Server.Implementations.Persistence if (typesToCount.Count == 0) { - whereText += " And CleanName In (Select CleanValue from ItemValues where Type=@ItemValueType AND ItemId in (select guid from TypedBaseItems" + innerWhereText + "))"; + whereText += " And CleanName In (Select CleanValue from ItemValues where " + typeClause + " AND ItemId in (select guid from TypedBaseItems" + innerWhereText + "))"; } else { //whereText += " And itemTypes not null"; - whereText += " And CleanName In (Select CleanValue from ItemValues where Type=@ItemValueType AND ItemId in (select guid from TypedBaseItems" + innerWhereText + "))"; + whereText += " And CleanName In (Select CleanValue from ItemValues where " + typeClause + " AND ItemId in (select guid from TypedBaseItems" + innerWhereText + "))"; } var outerQuery = new InternalItemsQuery(query.User) @@ -3964,7 +3973,6 @@ namespace MediaBrowser.Server.Implementations.Persistence cmd.CommandText += " group by PresentationUniqueKey"; cmd.Parameters.Add(cmd, "@SelectType", DbType.String).Value = returnType; - cmd.Parameters.Add(cmd, "@ItemValueType", DbType.Int32).Value = itemValueType; if (EnableJoinUserData(query)) {