From 5a496a1fc8d9ee2e728d6f712ae6bdf4862183f1 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 6 May 2016 00:50:39 -0400 Subject: reduce recursive querying --- MediaBrowser.Api/BaseApiService.cs | 189 +++++++++++++++---------------------- 1 file changed, 78 insertions(+), 111 deletions(-) (limited to 'MediaBrowser.Api/BaseApiService.cs') diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 30750b374..44d459a01 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -79,7 +79,7 @@ namespace MediaBrowser.Api } } } - + /// /// To the optimized serialized result using cache. /// @@ -118,9 +118,6 @@ namespace MediaBrowser.Api return ResultFactory.GetStaticFileResult(Request, path); } - private readonly char[] _dashReplaceChars = { '?', '/', '&' }; - private const char SlugChar = '-'; - protected DtoOptions GetDtoOptions(object request) { var options = new DtoOptions(); @@ -154,152 +151,122 @@ namespace MediaBrowser.Api protected MusicArtist GetArtist(string name, ILibraryManager libraryManager) { - return libraryManager.GetArtist(DeSlugArtistName(name, libraryManager)); - } - - protected Studio GetStudio(string name, ILibraryManager libraryManager) - { - return libraryManager.GetStudio(DeSlugStudioName(name, libraryManager)); - } - - protected Genre GetGenre(string name, ILibraryManager libraryManager) - { - return libraryManager.GetGenre(DeSlugGenreName(name, libraryManager)); - } + if (name.IndexOf(BaseItem.SlugChar) != -1) + { + var result = libraryManager.GetItemList(new InternalItemsQuery + { + SlugName = name, + IncludeItemTypes = new[] { typeof(MusicArtist).Name } - protected MusicGenre GetMusicGenre(string name, ILibraryManager libraryManager) - { - return libraryManager.GetMusicGenre(DeSlugGenreName(name, libraryManager)); - } + }).OfType().FirstOrDefault(); - protected GameGenre GetGameGenre(string name, ILibraryManager libraryManager) - { - return libraryManager.GetGameGenre(DeSlugGameGenreName(name, libraryManager)); - } + if (result != null) + { + return result; + } + } - protected Person GetPerson(string name, ILibraryManager libraryManager) - { - return libraryManager.GetPerson(DeSlugPersonName(name, libraryManager)); + return libraryManager.GetArtist(name); } - /// - /// Deslugs an artist name by finding the correct entry in the library - /// - /// - /// - /// - protected string DeSlugArtistName(string name, ILibraryManager libraryManager) + protected Studio GetStudio(string name, ILibraryManager libraryManager) { - if (name.IndexOf(SlugChar) == -1) - { - return name; - } - - var items = libraryManager.GetItemList(new InternalItemsQuery + if (name.IndexOf(BaseItem.SlugChar) != -1) { - IncludeItemTypes = new[] { typeof(Audio).Name, typeof(MusicVideo).Name, typeof(MusicAlbum).Name } - }); - - return items - .OfType() - .SelectMany(i => i.AllArtists) - .DistinctNames() - .FirstOrDefault(i => + var result = libraryManager.GetItemList(new InternalItemsQuery { - i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar)); + SlugName = name, + IncludeItemTypes = new[] { typeof(Studio).Name } - return string.Equals(i, name, StringComparison.OrdinalIgnoreCase); + }).OfType().FirstOrDefault(); + + if (result != null) + { + return result; + } + } - }) ?? name; + return libraryManager.GetStudio(name); } - /// - /// Deslugs a genre name by finding the correct entry in the library - /// - protected string DeSlugGenreName(string name, ILibraryManager libraryManager) + protected Genre GetGenre(string name, ILibraryManager libraryManager) { - if (name.IndexOf(SlugChar) == -1) + if (name.IndexOf(BaseItem.SlugChar) != -1) { - return name; - } - - return libraryManager.RootFolder.GetRecursiveChildren() - .SelectMany(i => i.Genres) - .DistinctNames() - .FirstOrDefault(i => + var result = libraryManager.GetItemList(new InternalItemsQuery { - i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar)); + SlugName = name, + IncludeItemTypes = new[] { typeof(Genre).Name } - return string.Equals(i, name, StringComparison.OrdinalIgnoreCase); + }).OfType().FirstOrDefault(); + + if (result != null) + { + return result; + } + } - }) ?? name; + return libraryManager.GetGenre(name); } - protected string DeSlugGameGenreName(string name, ILibraryManager libraryManager) + protected MusicGenre GetMusicGenre(string name, ILibraryManager libraryManager) { - if (name.IndexOf(SlugChar) == -1) + if (name.IndexOf(BaseItem.SlugChar) != -1) { - return name; - } + var result = libraryManager.GetItemList(new InternalItemsQuery + { + SlugName = name, + IncludeItemTypes = new[] { typeof(MusicGenre).Name } - var items = libraryManager.GetItemList(new InternalItemsQuery - { - IncludeItemTypes = new[] { typeof(Game).Name } - }); + }).OfType().FirstOrDefault(); - return items - .SelectMany(i => i.Genres) - .DistinctNames() - .FirstOrDefault(i => + if (result != null) { - i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar)); - - return string.Equals(i, name, StringComparison.OrdinalIgnoreCase); + return result; + } + } - }) ?? name; + return libraryManager.GetMusicGenre(name); } - /// - /// Deslugs a studio name by finding the correct entry in the library - /// - protected string DeSlugStudioName(string name, ILibraryManager libraryManager) + protected GameGenre GetGameGenre(string name, ILibraryManager libraryManager) { - if (name.IndexOf(SlugChar) == -1) + if (name.IndexOf(BaseItem.SlugChar) != -1) { - return name; - } - - return libraryManager.RootFolder - .GetRecursiveChildren() - .SelectMany(i => i.Studios) - .DistinctNames() - .FirstOrDefault(i => + var result = libraryManager.GetItemList(new InternalItemsQuery { - i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar)); + SlugName = name, + IncludeItemTypes = new[] { typeof(GameGenre).Name } - return string.Equals(i, name, StringComparison.OrdinalIgnoreCase); + }).OfType().FirstOrDefault(); - }) ?? name; + if (result != null) + { + return result; + } + } + + return libraryManager.GetGameGenre(name); } - /// - /// Deslugs a person name by finding the correct entry in the library - /// - protected string DeSlugPersonName(string name, ILibraryManager libraryManager) + protected Person GetPerson(string name, ILibraryManager libraryManager) { - if (name.IndexOf(SlugChar) == -1) + if (name.IndexOf(BaseItem.SlugChar) != -1) { - return name; - } - - return libraryManager.GetPeopleNames(new InternalPeopleQuery()) - .FirstOrDefault(i => + var result = libraryManager.GetItemList(new InternalItemsQuery { - i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar)); + SlugName = name, + IncludeItemTypes = new[] { typeof(Person).Name } - return string.Equals(i, name, StringComparison.OrdinalIgnoreCase); + }).OfType().FirstOrDefault(); + + if (result != null) + { + return result; + } + } - }) ?? name; + return libraryManager.GetPerson(name); } protected string GetPathValue(int index) -- cgit v1.2.3