From c643dd072e117aec2397e695c650e8a58be2fc6c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 27 Sep 2013 08:24:28 -0400 Subject: added more direct querying to folder --- MediaBrowser.Controller/Entities/Folder.cs | 107 ++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 34 deletions(-) (limited to 'MediaBrowser.Controller') diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 6f5bc3e3b..8d518963c 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -552,23 +552,7 @@ namespace MediaBrowser.Controller.Entities [IgnoreDataMember] public IEnumerable RecursiveChildren { - get - { - foreach (var item in Children) - { - yield return item; - - if (item.IsFolder) - { - var subFolder = (Folder)item; - - foreach (var subitem in subFolder.RecursiveChildren) - { - yield return subitem; - } - } - } - } + get { return GetRecursiveChildren(); } } private List LoadChildrenInternal() @@ -686,17 +670,12 @@ namespace MediaBrowser.Controller.Entities var currentChildren = ActualChildren.ToDictionary(i => i.Id); //create a list for our validated children - var validChildren = new ConcurrentBag>(); - var newItems = new ConcurrentBag(); + var validChildren = new List>(); + var newItems = new List(); cancellationToken.ThrowIfCancellationRequested(); - var options = new ParallelOptions - { - MaxDegreeOfParallelism = 10 - }; - - Parallel.ForEach(nonCachedChildren, options, child => + foreach (var child in nonCachedChildren) { BaseItem currentChild; @@ -725,10 +704,10 @@ namespace MediaBrowser.Controller.Entities validChildren.Add(new Tuple(child, true)); } - }); + } // If any items were added or removed.... - if (!newItems.IsEmpty || currentChildren.Count != validChildren.Count) + if (newItems.Count > 0 || currentChildren.Count != validChildren.Count) { var newChildren = validChildren.Select(c => c.Item1).ToList(); @@ -793,9 +772,9 @@ namespace MediaBrowser.Controller.Entities /// if set to true [recursive]. /// if set to true [force refresh metadata]. /// Task. - private async Task RefreshChildren(IEnumerable> children, IProgress progress, CancellationToken cancellationToken, bool? recursive, bool forceRefreshMetadata = false) + private async Task RefreshChildren(IList> children, IProgress progress, CancellationToken cancellationToken, bool? recursive, bool forceRefreshMetadata = false) { - var list = children.ToList(); + var list = children; var percentages = new Dictionary(list.Count); @@ -994,7 +973,7 @@ namespace MediaBrowser.Controller.Entities /// if set to true [recursive]. /// The filter. /// true if XXXX, false otherwise - private bool AddChildrenToList(User user, bool includeLinkedChildren, List list, bool recursive, Func filter) + private bool AddChildrenToList(User user, bool includeLinkedChildren, List list, bool recursive, Func filter) { var hasLinkedChildren = false; @@ -1030,7 +1009,7 @@ namespace MediaBrowser.Controller.Entities { continue; } - + hasLinkedChildren = true; if (child.IsVisible(user)) @@ -1056,7 +1035,15 @@ namespace MediaBrowser.Controller.Entities return GetRecursiveChildren(user, null, true); } - public IEnumerable GetRecursiveChildren(User user, Func filter, bool includeLinkedChildren = true) + /// + /// Gets the recursive children. + /// + /// The user. + /// The filter. + /// if set to true [include linked children]. + /// IList{BaseItem}. + /// + public IList GetRecursiveChildren(User user, Func filter, bool includeLinkedChildren = true) { if (user == null) { @@ -1066,7 +1053,7 @@ namespace MediaBrowser.Controller.Entities var initialCount = _lastRecursiveCount == 0 ? _children.Count : _lastRecursiveCount; var list = new List(initialCount); - var hasLinkedChildren = AddChildrenToList(user, includeLinkedChildren, list, true, null); + var hasLinkedChildren = AddChildrenToList(user, includeLinkedChildren, list, true, filter); _lastRecursiveCount = list.Count; @@ -1077,7 +1064,59 @@ namespace MediaBrowser.Controller.Entities return list; } - + + /// + /// Gets the recursive children. + /// + /// IList{BaseItem}. + public IList GetRecursiveChildren() + { + return GetRecursiveChildren(null); + } + + /// + /// Gets the recursive children. + /// + /// The filter. + /// IEnumerable{BaseItem}. + public IList GetRecursiveChildren(Func filter) + { + var initialCount = _lastRecursiveCount == 0 ? _children.Count : _lastRecursiveCount; + var list = new List(initialCount); + + AddChildrenToList(list, true, filter); + + return list; + } + + /// + /// Adds the children to list. + /// + /// The list. + /// if set to true [recursive]. + /// The filter. + private void AddChildrenToList(List list, bool recursive, Func filter) + { + foreach (var child in Children) + { + if (filter == null || filter(child)) + { + list.Add(child); + } + + if (recursive) + { + var folder = child as Folder; + + if (folder != null) + { + folder.AddChildrenToList(list, true, filter); + } + } + } + } + + /// /// Gets the linked children. /// -- cgit v1.2.3