Chaged BaseItem.People to a dictionary to prevent duplicates and improve Contains performance. Tweaked ffprobe and provider execution.

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-08-24 12:24:29 -04:00
parent 0a16aeb448
commit cc25bd579b
9 changed files with 169 additions and 166 deletions

View File

@ -200,17 +200,17 @@ namespace MediaBrowser.Api
// Attach People by transforming them into BaseItemPerson (DTO) // Attach People by transforming them into BaseItemPerson (DTO)
if (item.People != null) if (item.People != null)
{ {
IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Name))).ConfigureAwait(false); IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Key))).ConfigureAwait(false);
dto.People = item.People.Select(p => dto.People = item.People.Select(p =>
{ {
BaseItemPerson baseItemPerson = new BaseItemPerson(); BaseItemPerson baseItemPerson = new BaseItemPerson();
baseItemPerson.Name = p.Name; baseItemPerson.Name = p.Key;
baseItemPerson.Overview = p.Overview; baseItemPerson.Overview = p.Value.Overview;
baseItemPerson.Type = p.Type; 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) if (ibnObject != null)
{ {

View File

@ -37,7 +37,7 @@ namespace MediaBrowser.Api.HttpHandlers
foreach (var item in allItems) 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++; count++;
} }

View File

@ -16,12 +16,14 @@ namespace MediaBrowser.Controller.FFMpeg
/// <summary> /// <summary>
/// Runs FFProbe against an Audio file, caches the result and returns the output /// Runs FFProbe against an Audio file, caches the result and returns the output
/// </summary> /// </summary>
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 // Use try catch to avoid having to use File.Exists
try try
{ {
return GetCachedResult(GetFFProbeCachePath(item)); return GetCachedResult(cachePath);
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
@ -34,7 +36,7 @@ namespace MediaBrowser.Controller.FFMpeg
FFProbeResult result = Run(item.Path); FFProbeResult result = Run(item.Path);
// Fire and forget // Fire and forget
CacheResult(result, GetFFProbeCachePath(item)); CacheResult(result, cachePath);
return result; return result;
} }
@ -65,32 +67,6 @@ namespace MediaBrowser.Controller.FFMpeg
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
/// <summary>
/// Runs FFProbe against a Video file, caches the result and returns the output
/// </summary>
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) private static FFProbeResult Run(string input)
{ {
ProcessStartInfo startInfo = new ProcessStartInfo(); ProcessStartInfo startInfo = new ProcessStartInfo();
@ -148,16 +124,9 @@ namespace MediaBrowser.Controller.FFMpeg
(sender as Process).Dispose(); (sender as Process).Dispose();
} }
private static string GetFFProbeCachePath(Audio item) private static string GetFFProbeCachePath(BaseItem item, string cacheDirectory)
{ {
string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, 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");
}
private static string GetFFProbeCachePath(Video item)
{
string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory, item.Id.ToString().Substring(0, 1));
return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".pb"); return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".pb");
} }

View File

@ -41,7 +41,13 @@ namespace MediaBrowser.Controller
/// Gets the list of currently registered metadata prvoiders /// Gets the list of currently registered metadata prvoiders
/// </summary> /// </summary>
[ImportMany(typeof(BaseMetadataProvider))] [ImportMany(typeof(BaseMetadataProvider))]
public IEnumerable<BaseMetadataProvider> MetadataProviders { get; private set; } private IEnumerable<BaseMetadataProvider> MetadataProvidersEnumerable { get; set; }
/// <summary>
/// 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
/// </summary>
private BaseMetadataProvider[] MetadataProviders { get; set; }
/// <summary> /// <summary>
/// Gets the list of currently registered entity resolvers /// Gets the list of currently registered entity resolvers
@ -92,9 +98,9 @@ namespace MediaBrowser.Controller
// Sort the resolvers by priority // Sort the resolvers by priority
EntityResolvers = EntityResolversEnumerable.OrderBy(e => e.Priority).ToArray(); EntityResolvers = EntityResolversEnumerable.OrderBy(e => e.Priority).ToArray();
// Sort the providers by priority // Sort the providers by priority
MetadataProviders = MetadataProviders.OrderBy(e => e.Priority); MetadataProviders = MetadataProvidersEnumerable.OrderBy(e => e.Priority).ToArray();
// Initialize the metadata providers // Initialize the metadata providers
Parallel.ForEach(MetadataProviders, provider => Parallel.ForEach(MetadataProviders, provider =>
@ -232,13 +238,15 @@ namespace MediaBrowser.Controller
/// </summary> /// </summary>
internal async Task ExecuteMetadataProviders(BaseEntity item, ItemResolveEventArgs args, bool allowInternetProviders = true) 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 // 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)) if (provider.RequiresInternet && (!Configuration.EnableInternetProviders || !allowInternetProviders))
{ {

View File

@ -12,36 +12,26 @@ using MediaBrowser.Model.Entities;
namespace MediaBrowser.Controller.Providers namespace MediaBrowser.Controller.Providers
{ {
[Export(typeof(BaseMetadataProvider))] [Export(typeof(BaseMetadataProvider))]
public class AudioInfoProvider : BaseMetadataProvider public class AudioInfoProvider : BaseMediaInfoProvider<Audio>
{ {
public override bool Supports(BaseEntity item)
{
return item is Audio;
}
public override MetadataProviderPriority Priority public override MetadataProviderPriority Priority
{ {
get { return MetadataProviderPriority.First; } get { return MetadataProviderPriority.First; }
} }
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args) protected override string CacheDirectory
{ {
await Task.Run(() => get { return Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory; }
{
Audio audio = item as Audio;
Fetch(audio, FFProbe.Run(audio));
});
} }
private void Fetch(Audio audio, FFProbeResult data) protected override void Fetch(Audio audio, FFProbeResult data)
{ {
if (data == null) if (data == null)
{ {
Logger.LogInfo("Null FFProbeResult for {0} {1}", audio.Id, audio.Name); Logger.LogInfo("Null FFProbeResult for {0} {1}", audio.Id, audio.Name);
return; return;
} }
MediaStream stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase)); MediaStream stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
string bitrate = null; string bitrate = null;
@ -96,9 +86,7 @@ namespace MediaBrowser.Controller.Providers
if (!string.IsNullOrEmpty(composer)) if (!string.IsNullOrEmpty(composer))
{ {
var list = audio.People ?? new List<PersonInfo>(); audio.AddPerson(new PersonInfo() { Name = composer, Type = "Composer" });
list.Add(new PersonInfo() { Name = composer, Type = "Composer" });
audio.People = list;
} }
audio.Album = GetDictionaryValue(tags, "album"); audio.Album = GetDictionaryValue(tags, "album");
@ -132,7 +120,7 @@ namespace MediaBrowser.Controller.Providers
audio.Studios = list; audio.Studios = list;
} }
} }
private void FetchGenres(Audio audio, Dictionary<string, string> tags) private void FetchGenres(Audio audio, Dictionary<string, string> tags)
{ {
string val = GetDictionaryValue(tags, "genre"); string val = GetDictionaryValue(tags, "genre");
@ -163,30 +151,102 @@ namespace MediaBrowser.Controller.Providers
return null; return null;
} }
}
internal static string GetDictionaryValue(Dictionary<string, string> tags, string key) public abstract class BaseMediaInfoProvider<T> : BaseMetadataProvider
where T : BaseItem
{
protected abstract string CacheDirectory { get; }
public override bool Supports(BaseEntity item)
{
return item is T;
}
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
{
await Task.Run(() =>
{
T myItem = item as T;
if (CanSkipFFProbe(myItem))
{
return;
}
FFProbeResult result = FFProbe.Run(myItem, CacheDirectory);
if (result.format.tags != null)
{
result.format.tags = ConvertDictionaryToCaseInSensitive(result.format.tags);
}
foreach (MediaStream stream in result.streams)
{
if (stream.tags != null)
{
stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags);
}
}
Fetch(myItem, result);
});
}
protected abstract void Fetch(T item, FFProbeResult result);
public override void Init()
{
base.Init();
EnsureCacheSubFolders(CacheDirectory);
}
private void EnsureCacheSubFolders(string root)
{
// Do this now so that we don't have to do this on every operation, which would require us to create a lock in order to maintain thread-safety
for (int i = 0; i <= 9; i++)
{
EnsureDirectory(Path.Combine(root, i.ToString()));
}
EnsureDirectory(Path.Combine(root, "a"));
EnsureDirectory(Path.Combine(root, "b"));
EnsureDirectory(Path.Combine(root, "c"));
EnsureDirectory(Path.Combine(root, "d"));
EnsureDirectory(Path.Combine(root, "e"));
EnsureDirectory(Path.Combine(root, "f"));
}
private void EnsureDirectory(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
protected virtual bool CanSkipFFProbe(T item)
{
return false;
}
protected string GetDictionaryValue(Dictionary<string, string> tags, string key)
{ {
if (tags == null) if (tags == null)
{ {
return null; return null;
} }
string[] keys = tags.Keys.ToArray(); if (!tags.ContainsKey(key))
for (int i = 0; i < keys.Length; i++)
{ {
string currentKey = keys[i]; return null;
if (key.Equals(currentKey, StringComparison.OrdinalIgnoreCase))
{
return tags[currentKey];
}
} }
return null; return tags[key];
} }
private int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key) protected int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
{ {
string val = GetDictionaryValue(tags, key); string val = GetDictionaryValue(tags, key);
@ -203,7 +263,7 @@ namespace MediaBrowser.Controller.Providers
return null; return null;
} }
private DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key) protected DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
{ {
string val = GetDictionaryValue(tags, key); string val = GetDictionaryValue(tags, key);
@ -219,43 +279,17 @@ namespace MediaBrowser.Controller.Providers
return null; return null;
} }
private string GetOutputCachePath(BaseItem item) private Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict)
{ {
string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, item.Id.ToString().Substring(0, 1)); Dictionary<string, string> newDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js"); foreach (string key in dict.Keys)
}
public override void Init()
{
base.Init();
EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory);
}
internal static void EnsureCacheSubFolders(string root)
{
// Do this now so that we don't have to do this on every operation, which would require us to create a lock in order to maintain thread-safety
for (int i = 0; i <= 9; i++)
{ {
EnsureDirectory(Path.Combine(root, i.ToString())); newDict[key] = dict[key];
} }
EnsureDirectory(Path.Combine(root, "a")); return newDict;
EnsureDirectory(Path.Combine(root, "b"));
EnsureDirectory(Path.Combine(root, "c"));
EnsureDirectory(Path.Combine(root, "d"));
EnsureDirectory(Path.Combine(root, "e"));
EnsureDirectory(Path.Combine(root, "f"));
}
private static void EnsureDirectory(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
} }
} }
} }

View File

@ -11,13 +11,8 @@ using MediaBrowser.Model.Entities;
namespace MediaBrowser.Controller.Providers namespace MediaBrowser.Controller.Providers
{ {
[Export(typeof(BaseMetadataProvider))] [Export(typeof(BaseMetadataProvider))]
public class VideoInfoProvider : BaseMetadataProvider public class VideoInfoProvider : BaseMediaInfoProvider<Video>
{ {
public override bool Supports(BaseEntity item)
{
return item is Video;
}
public override MetadataProviderPriority Priority public override MetadataProviderPriority Priority
{ {
// Give this second priority // Give this second priority
@ -25,28 +20,12 @@ namespace MediaBrowser.Controller.Providers
get { return MetadataProviderPriority.Second; } get { return MetadataProviderPriority.Second; }
} }
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args) protected override string CacheDirectory
{ {
await Task.Run(() => get { return Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory; }
{
Video video = item as Video;
if (video.VideoType != VideoType.VideoFile)
{
// Not supported yet
return;
}
if (CanSkip(video))
{
return;
}
Fetch(video, FFProbe.Run(video));
});
} }
private void Fetch(Video video, FFProbeResult data) protected override void Fetch(Video video, FFProbeResult data)
{ {
if (data == null) if (data == null)
{ {
@ -126,7 +105,7 @@ namespace MediaBrowser.Controller.Providers
audio.SampleRate = int.Parse(stream.sample_rate); audio.SampleRate = int.Parse(stream.sample_rate);
} }
audio.Language = AudioInfoProvider.GetDictionaryValue(stream.tags, "language"); audio.Language = GetDictionaryValue(stream.tags, "language");
List<AudioStream> streams = video.AudioStreams ?? new List<AudioStream>(); List<AudioStream> streams = video.AudioStreams ?? new List<AudioStream>();
streams.Add(audio); streams.Add(audio);
@ -136,8 +115,14 @@ namespace MediaBrowser.Controller.Providers
/// <summary> /// <summary>
/// Determines if there's already enough info in the Video object to allow us to skip running ffprobe /// Determines if there's already enough info in the Video object to allow us to skip running ffprobe
/// </summary> /// </summary>
private bool CanSkip(Video video) protected override bool CanSkipFFProbe(Video video)
{ {
if (video.VideoType != VideoType.VideoFile)
{
// Not supported yet
return true;
}
if (video.AudioStreams == null || !video.AudioStreams.Any()) if (video.AudioStreams == null || !video.AudioStreams.Any())
{ {
return false; return false;
@ -175,8 +160,6 @@ namespace MediaBrowser.Controller.Providers
{ {
base.Init(); base.Init();
AudioInfoProvider.EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory);
// This is an optimzation. Do this now so that it doesn't have to be done upon first serialization. // This is an optimzation. Do this now so that it doesn't have to be done upon first serialization.
ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(FFProbeResult), true); ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(FFProbeResult), true);
ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(MediaStream), true); ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(MediaStream), true);

View File

@ -157,28 +157,28 @@ namespace MediaBrowser.Controller.Xml
case "Director": case "Director":
{ {
var list = item.People ?? new List<PersonInfo>(); foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Director" }))
list.AddRange(GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Director" })); {
item.AddPerson(p);
item.People = list; }
break; break;
} }
case "Writer": case "Writer":
{ {
var list = item.People ?? new List<PersonInfo>(); foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Writer" }))
list.AddRange(GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Writer" })); {
item.AddPerson(p);
item.People = list; }
break; break;
} }
case "Actors": case "Actors":
case "GuestStars": case "GuestStars":
{ {
var list = item.People ?? new List<PersonInfo>(); foreach (PersonInfo p in GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Actor" }))
list.AddRange(GetSplitValues(reader.ReadElementContentAsString(), '|').Select(v => new PersonInfo() { Name = v, Type = "Actor" })); {
item.AddPerson(p);
item.People = list; }
break; break;
} }
@ -556,8 +556,6 @@ namespace MediaBrowser.Controller.Xml
private void FetchDataFromPersonsNode(XmlReader reader, T item) private void FetchDataFromPersonsNode(XmlReader reader, T item)
{ {
var list = item.People ?? new List<PersonInfo>();
reader.MoveToContent(); reader.MoveToContent();
while (reader.Read()) while (reader.Read())
@ -568,7 +566,7 @@ namespace MediaBrowser.Controller.Xml
{ {
case "Person": case "Person":
{ {
list.Add(GetPersonFromXmlNode(reader.ReadSubtree())); item.AddPerson(GetPersonFromXmlNode(reader.ReadSubtree()));
break; break;
} }
@ -578,8 +576,6 @@ namespace MediaBrowser.Controller.Xml
} }
} }
} }
item.People = list;
} }
private void FetchFromStudiosNode(XmlReader reader, T item) private void FetchFromStudiosNode(XmlReader reader, T item)

View File

@ -58,7 +58,10 @@ namespace MediaBrowser.Model.Entities
public string Overview { get; set; } public string Overview { get; set; }
public List<string> Taglines { get; set; } public List<string> Taglines { get; set; }
public List<PersonInfo> People { get; set; } /// <summary>
/// Using a Dictionary to prevent duplicates
/// </summary>
public Dictionary<string,PersonInfo> People { get; set; }
public List<string> Studios { get; set; } public List<string> Studios { get; set; }
@ -152,5 +155,15 @@ namespace MediaBrowser.Model.Entities
{ {
return (DateTime.Now - DateCreated).TotalDays < user.RecentItemDays; return (DateTime.Now - DateCreated).TotalDays < user.RecentItemDays;
} }
public void AddPerson(PersonInfo person)
{
if (People == null)
{
People = new Dictionary<string, PersonInfo>(StringComparer.OrdinalIgnoreCase);
}
People[person.Name] = person;
}
} }
} }

View File

@ -97,7 +97,7 @@ namespace MediaBrowser.Model.Entities
{ {
if (c.People != null) if (c.People != null)
{ {
return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase)); return c.People.ContainsKey(person);
} }
return false; return false;
@ -114,7 +114,7 @@ namespace MediaBrowser.Model.Entities
{ {
if (c.People != null) if (c.People != null)
{ {
return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase) && p.Type == personType); return c.People.ContainsKey(person) && c.People[person].Type.Equals(personType, StringComparison.OrdinalIgnoreCase);
} }
return false; return false;