From d580abcd6526b67f7e36514bab1ce8edf39b7674 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 19 Sep 2013 11:12:54 -0400 Subject: [PATCH] cleanup repeated querying within CollectionFolder --- .../Entities/CollectionFolder.cs | 89 +++++++++++-------- .../Library/LibraryManager.cs | 12 +++ 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/MediaBrowser.Controller/Entities/CollectionFolder.cs b/MediaBrowser.Controller/Entities/CollectionFolder.cs index 6d619ce04..bc50ce618 100644 --- a/MediaBrowser.Controller/Entities/CollectionFolder.cs +++ b/MediaBrowser.Controller/Entities/CollectionFolder.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -69,37 +68,41 @@ namespace MediaBrowser.Controller.Entities return NullTaskResult; } + private List _linkedChildren; + /// /// Our children are actually just references to the ones in the physical root... /// /// The linked children. public override List LinkedChildren { - get - { - Dictionary locationsDicionary; - - try - { - locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); - } - catch (IOException ex) - { - Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path); - return new List(); - } - - return LibraryManager.RootFolder.Children - .OfType() - .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path)) - .SelectMany(c => c.LinkedChildren).ToList(); - - } + get { return _linkedChildren ?? (_linkedChildren = GetLinkedChildrenInternal()); } set { base.LinkedChildren = value; } } + private List GetLinkedChildrenInternal() + { + Dictionary locationsDicionary; + + try + { + locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); + } + catch (IOException ex) + { + Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path); + return new List(); + } + + return LibraryManager.RootFolder.Children + .OfType() + .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path)) + .SelectMany(c => c.LinkedChildren).ToList(); + } + + private IEnumerable _actualChildren; /// /// Our children are actually just references to the ones in the physical root... @@ -107,26 +110,34 @@ namespace MediaBrowser.Controller.Entities /// The actual children. protected override IEnumerable ActualChildren { - get + get { return _actualChildren ?? (_actualChildren = GetActualChildren()); } + } + + private IEnumerable GetActualChildren() + { + Dictionary locationsDicionary; + + try { - Dictionary locationsDicionary; - - try - { - locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); - } - catch (IOException ex) - { - Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path); - return new BaseItem[] { }; - } - - return - LibraryManager.RootFolder.Children - .OfType() - .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path)) - .SelectMany(c => c.Children); + locationsDicionary = ResolveArgs.PhysicalLocations.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); } + catch (IOException ex) + { + Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path); + return new BaseItem[] { }; + } + + return + LibraryManager.RootFolder.Children + .OfType() + .Where(i => i.Path != null && locationsDicionary.ContainsKey(i.Path)) + .SelectMany(c => c.Children); + } + + public void ResetDynamicChildren() + { + _actualChildren = null; + _linkedChildren = null; } } } diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 27d6953d7..a5b792726 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -1283,6 +1283,8 @@ namespace MediaBrowser.Server.Implementations.Library UpdateItemInLibraryCache(item); } + UpdateCollectionFolders(); + if (ItemAdded != null) { foreach (var item in list) @@ -1336,6 +1338,8 @@ namespace MediaBrowser.Server.Implementations.Library /// The item. public void ReportItemRemoved(BaseItem item) { + UpdateCollectionFolders(); + if (ItemRemoved != null) { try @@ -1349,6 +1353,14 @@ namespace MediaBrowser.Server.Implementations.Library } } + private void UpdateCollectionFolders() + { + foreach (var folder in _userManager.Users.SelectMany(i => i.RootFolder.Children).OfType().ToList()) + { + folder.ResetDynamicChildren(); + } + } + /// /// Retrieves the item. ///