From 803e8b4a2eb5fcf1b5a3679fe551d541620d4743 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 11 Sep 2013 13:54:59 -0400 Subject: improved performance of item counts --- .../Library/LibraryManager.cs | 65 ++++++++++++++++------ 1 file changed, 48 insertions(+), 17 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Library/LibraryManager.cs') diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index d017a5f7e..602f81c33 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -581,6 +581,11 @@ namespace MediaBrowser.Server.Implementations.Library (UserRootFolder)ResolvePath(new DirectoryInfo(userRootPath))); } + public Person GetPersonSync(string name) + { + return GetItemByName(ConfigurationManager.ApplicationPaths.PeoplePath, name); + } + /// /// Gets a Person /// @@ -752,6 +757,37 @@ namespace MediaBrowser.Server.Implementations.Library /// private readonly ConcurrentDictionary _itemsByName = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + private T GetItemByName(string path, string name) + where T : BaseItem, new() + { + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentNullException(); + } + + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException(); + } + + var validFilename = FileSystem.GetValidFilename(name); + + var key = Path.Combine(path, validFilename); + + BaseItem obj; + + if (!_itemsByName.TryGetValue(key, out obj)) + { + var tuple = CreateItemByName(key, name); + + obj = tuple.Item2; + + _itemsByName.AddOrUpdate(key, obj, (keyName, oldValue) => obj); + } + + return obj as T; + } + /// /// Generically retrieves an IBN item /// @@ -777,13 +813,15 @@ namespace MediaBrowser.Server.Implementations.Library throw new ArgumentNullException(); } - var key = Path.Combine(path, FileSystem.GetValidFilename(name)); + var validFilename = FileSystem.GetValidFilename(name); + + var key = Path.Combine(path, validFilename); BaseItem obj; if (!_itemsByName.TryGetValue(key, out obj)) { - var tuple = CreateItemByName(path, name, cancellationToken); + var tuple = CreateItemByName(key, name); obj = tuple.Item2; @@ -815,16 +853,11 @@ namespace MediaBrowser.Server.Implementations.Library /// /// The path. /// The name. - /// The cancellation token. /// Task{``0}. /// Path not created: + path - private Tuple CreateItemByName(string path, string name, CancellationToken cancellationToken) + private Tuple CreateItemByName(string path, string name) where T : BaseItem, new() { - cancellationToken.ThrowIfCancellationRequested(); - - path = Path.Combine(path, FileSystem.GetValidFilename(name)); - var fileInfo = new DirectoryInfo(path); var isNew = false; @@ -842,8 +875,6 @@ namespace MediaBrowser.Server.Implementations.Library isNew = true; } - cancellationToken.ThrowIfCancellationRequested(); - var type = typeof(T); var id = path.GetMBId(type); @@ -866,7 +897,7 @@ namespace MediaBrowser.Server.Implementations.Library // Set this now so we don't cause additional file system access during provider executions item.ResetResolveArgs(fileInfo); - return new Tuple(isNew, item); + return new Tuple(isNew, item); } /// @@ -878,16 +909,12 @@ namespace MediaBrowser.Server.Implementations.Library /// Task. public async Task ValidatePeople(CancellationToken cancellationToken, IProgress progress) { - const int maxTasks = 10; + const int maxTasks = 5; var tasks = new List(); - var includedPersonTypes = new[] { PersonType.Actor, PersonType.Director, PersonType.GuestStar, PersonType.Writer, PersonType.Producer } - .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); - var people = RootFolder.RecursiveChildren - .Where(c => c.People != null) - .SelectMany(c => c.People.Where(p => includedPersonTypes.ContainsKey(p.Type ?? string.Empty) || includedPersonTypes.ContainsKey(p.Role ?? string.Empty))) + .SelectMany(c => c.People) .DistinctBy(p => p.Name, StringComparer.OrdinalIgnoreCase) .ToList(); @@ -1050,6 +1077,10 @@ namespace MediaBrowser.Server.Implementations.Library await RunPostScanTasks(progress, cancellationToken).ConfigureAwait(false); progress.Report(100); + + // Bad practice, i know. But we keep a lot in memory, unfortunately. + GC.Collect(2, GCCollectionMode.Forced, true); + GC.Collect(2, GCCollectionMode.Forced, true); } /// -- cgit v1.2.3