From 2d0cc66e6bea278b7be5078130f55fc05ce756ce Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 26 Sep 2013 14:45:43 -0400 Subject: [PATCH] remove additional enumerations in get recursive children --- MediaBrowser.Controller/Entities/Folder.cs | 65 +++++++++++----------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 4d5dc5ddb3..1015a3021a 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -977,31 +977,52 @@ namespace MediaBrowser.Controller.Entities return result; } - return GetChildrenList(user, includeLinkedChildren); + var initialCount = _children.Count; + var list = new List(initialCount); + + AddChildrenToList(user, includeLinkedChildren, list, false); + + return list; } /// - /// Gets the children list. + /// Adds the children to list. /// /// The user. /// if set to true [include linked children]. - /// List{BaseItem}. - private List GetChildrenList(User user, bool includeLinkedChildren) + /// The list. + /// if set to true [recursive]. + private bool AddChildrenToList(User user, bool includeLinkedChildren, List list, bool recursive) { - var list = new List(); - + var hasLinkedChildren = false; + foreach (var child in Children) { if (child.IsVisible(user)) { list.Add(child); } + + if (recursive) + { + var folder = child as Folder; + + if (folder != null) + { + if (folder.AddChildrenToList(user, includeLinkedChildren, list, true)) + { + hasLinkedChildren = true; + } + } + } } if (includeLinkedChildren) { foreach (var child in GetLinkedChildren()) { + hasLinkedChildren = true; + if (child.IsVisible(user)) { list.Add(child); @@ -1009,9 +1030,10 @@ namespace MediaBrowser.Controller.Entities } } - return list; + return hasLinkedChildren; } + private int _lastRecursiveCount; /// /// Gets allowed recursive children of an item /// @@ -1026,12 +1048,14 @@ namespace MediaBrowser.Controller.Entities throw new ArgumentNullException(); } - var initialCount = _children.Count; + var initialCount = _lastRecursiveCount == 0 ? _children.Count : _lastRecursiveCount; var list = new List(initialCount); - AddRecursiveChildrenInternal(user, includeLinkedChildren, list); + var hasLinkedChildren = AddChildrenToList(user, includeLinkedChildren, list, true); - if (includeLinkedChildren) + _lastRecursiveCount = list.Count; + + if (includeLinkedChildren && hasLinkedChildren) { list = list.Distinct().ToList(); } @@ -1039,27 +1063,6 @@ namespace MediaBrowser.Controller.Entities return list; } - /// - /// Adds the recursive children internal. - /// - /// The user. - /// if set to true [include linked children]. - /// The list. - private void AddRecursiveChildrenInternal(User user, bool includeLinkedChildren, List list) - { - foreach (var item in GetChildrenList(user, includeLinkedChildren)) - { - list.Add(item); - - var subFolder = item as Folder; - - if (subFolder != null) - { - subFolder.AddRecursiveChildrenInternal(user, includeLinkedChildren, list); - } - } - } - /// /// Gets the linked children. ///