From cc25bd579b820a91e436a358f38321d254c27451 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Fri, 24 Aug 2012 12:24:29 -0400 Subject: [PATCH] Chaged BaseItem.People to a dictionary to prevent duplicates and improve Contains performance. Tweaked ffprobe and provider execution. --- MediaBrowser.Api/ApiService.cs | 10 +- .../HttpHandlers/PersonHandler.cs | 2 +- MediaBrowser.Controller/FFMpeg/FFProbe.cs | 45 +---- MediaBrowser.Controller/Kernel.cs | 24 ++- .../Providers/AudioInfoProvider.cs | 162 +++++++++++------- .../Providers/VideoInfoProvider.cs | 43 ++--- .../Xml/BaseItemXmlParser.cs | 30 ++-- MediaBrowser.Model/Entities/BaseItem.cs | 15 +- MediaBrowser.Model/Entities/Folder.cs | 4 +- 9 files changed, 169 insertions(+), 166 deletions(-) diff --git a/MediaBrowser.Api/ApiService.cs b/MediaBrowser.Api/ApiService.cs index f52141a64b..ab0f46e423 100644 --- a/MediaBrowser.Api/ApiService.cs +++ b/MediaBrowser.Api/ApiService.cs @@ -200,17 +200,17 @@ namespace MediaBrowser.Api // Attach People by transforming them into BaseItemPerson (DTO) if (item.People != null) { - IEnumerable entities = await Task.WhenAll(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Name))).ConfigureAwait(false); + IEnumerable entities = await Task.WhenAll(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Key))).ConfigureAwait(false); dto.People = item.People.Select(p => { BaseItemPerson baseItemPerson = new BaseItemPerson(); - baseItemPerson.Name = p.Name; - baseItemPerson.Overview = p.Overview; - baseItemPerson.Type = p.Type; + baseItemPerson.Name = p.Key; + baseItemPerson.Overview = p.Value.Overview; + baseItemPerson.Type = p.Value.Type; - Person ibnObject = entities.First(i => i.Name.Equals(p.Name, StringComparison.OrdinalIgnoreCase)); + Person ibnObject = entities.First(i => i.Name.Equals(p.Key, StringComparison.OrdinalIgnoreCase)); if (ibnObject != null) { diff --git a/MediaBrowser.Api/HttpHandlers/PersonHandler.cs b/MediaBrowser.Api/HttpHandlers/PersonHandler.cs index 4db2fada19..3c34efae50 100644 --- a/MediaBrowser.Api/HttpHandlers/PersonHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/PersonHandler.cs @@ -37,7 +37,7 @@ namespace MediaBrowser.Api.HttpHandlers foreach (var item in allItems) { - if (item.People != null && item.People.Any(s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase))) + if (item.People != null && item.People.ContainsKey(name)) { count++; } diff --git a/MediaBrowser.Controller/FFMpeg/FFProbe.cs b/MediaBrowser.Controller/FFMpeg/FFProbe.cs index 831ae50a3a..791f47b298 100644 --- a/MediaBrowser.Controller/FFMpeg/FFProbe.cs +++ b/MediaBrowser.Controller/FFMpeg/FFProbe.cs @@ -16,12 +16,14 @@ namespace MediaBrowser.Controller.FFMpeg /// /// Runs FFProbe against an Audio file, caches the result and returns the output /// - public static FFProbeResult Run(Audio item) + public static FFProbeResult Run(BaseItem item, string cacheDirectory) { + string cachePath = GetFFProbeCachePath(item, cacheDirectory); + // Use try catch to avoid having to use File.Exists try { - return GetCachedResult(GetFFProbeCachePath(item)); + return GetCachedResult(cachePath); } catch (FileNotFoundException) { @@ -34,7 +36,7 @@ namespace MediaBrowser.Controller.FFMpeg FFProbeResult result = Run(item.Path); // Fire and forget - CacheResult(result, GetFFProbeCachePath(item)); + CacheResult(result, cachePath); return result; } @@ -65,32 +67,6 @@ namespace MediaBrowser.Controller.FFMpeg }).ConfigureAwait(false); } - /// - /// Runs FFProbe against a Video file, caches the result and returns the output - /// - public static FFProbeResult Run(Video item) - { - // Use try catch to avoid having to use File.Exists - try - { - return GetCachedResult(GetFFProbeCachePath(item)); - } - catch (FileNotFoundException) - { - } - catch (Exception ex) - { - Logger.LogException(ex); - } - - FFProbeResult result = Run(item.Path); - - // Fire and forget - CacheResult(result, GetFFProbeCachePath(item)); - - return result; - } - private static FFProbeResult Run(string input) { ProcessStartInfo startInfo = new ProcessStartInfo(); @@ -148,16 +124,9 @@ namespace MediaBrowser.Controller.FFMpeg (sender as Process).Dispose(); } - private static string GetFFProbeCachePath(Audio item) - { - string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, item.Id.ToString().Substring(0, 1)); - - return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".pb"); - } - - private static string GetFFProbeCachePath(Video item) + private static string GetFFProbeCachePath(BaseItem item, string cacheDirectory) { - string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory, item.Id.ToString().Substring(0, 1)); + string outputDirectory = Path.Combine(cacheDirectory, item.Id.ToString().Substring(0, 1)); return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".pb"); } diff --git a/MediaBrowser.Controller/Kernel.cs b/MediaBrowser.Controller/Kernel.cs index 44833fe8e0..c4f36e7324 100644 --- a/MediaBrowser.Controller/Kernel.cs +++ b/MediaBrowser.Controller/Kernel.cs @@ -41,7 +41,13 @@ namespace MediaBrowser.Controller /// Gets the list of currently registered metadata prvoiders /// [ImportMany(typeof(BaseMetadataProvider))] - public IEnumerable MetadataProviders { get; private set; } + private IEnumerable MetadataProvidersEnumerable { get; set; } + + /// + /// Once MEF has loaded the resolvers, sort them by priority and store them in this array + /// Given the sheer number of times they'll be iterated over it'll be faster to loop through an array + /// + private BaseMetadataProvider[] MetadataProviders { get; set; } /// /// Gets the list of currently registered entity resolvers @@ -92,9 +98,9 @@ namespace MediaBrowser.Controller // Sort the resolvers by priority EntityResolvers = EntityResolversEnumerable.OrderBy(e => e.Priority).ToArray(); - + // Sort the providers by priority - MetadataProviders = MetadataProviders.OrderBy(e => e.Priority); + MetadataProviders = MetadataProvidersEnumerable.OrderBy(e => e.Priority).ToArray(); // Initialize the metadata providers Parallel.ForEach(MetadataProviders, provider => @@ -232,13 +238,15 @@ namespace MediaBrowser.Controller /// internal async Task ExecuteMetadataProviders(BaseEntity item, ItemResolveEventArgs args, bool allowInternetProviders = true) { - // Get all supported providers - BaseMetadataProvider[] supportedProviders = Kernel.Instance.MetadataProviders.Where(i => i.Supports(item)).ToArray(); - // Run them sequentially in order of priority - for (int i = 0; i < supportedProviders.Length; i++) + for (int i = 0; i < MetadataProviders.Length; i++) { - var provider = supportedProviders[i]; + var provider = MetadataProviders[i]; + + if (!provider.Supports(item)) + { + continue; + } if (provider.RequiresInternet && (!Configuration.EnableInternetProviders || !allowInternetProviders)) { diff --git a/MediaBrowser.Controller/Providers/AudioInfoProvider.cs b/MediaBrowser.Controller/Providers/AudioInfoProvider.cs index 98168ba865..ce0759715e 100644 --- a/MediaBrowser.Controller/Providers/AudioInfoProvider.cs +++ b/MediaBrowser.Controller/Providers/AudioInfoProvider.cs @@ -12,36 +12,26 @@ using MediaBrowser.Model.Entities; namespace MediaBrowser.Controller.Providers { [Export(typeof(BaseMetadataProvider))] - public class AudioInfoProvider : BaseMetadataProvider + public class AudioInfoProvider : BaseMediaInfoProvider