improved startup delay

This commit is contained in:
Luke Pulverenti 2013-12-03 16:11:09 -05:00
parent 245e92c9cc
commit c38fef110e
3 changed files with 12 additions and 36 deletions

View File

@ -90,7 +90,7 @@ namespace MediaBrowser.Controller.Entities
item.Id = item.Path.GetMBId(item.GetType()); item.Id = item.Path.GetMBId(item.GetType());
} }
if (_children.Any(i => i.Id == item.Id)) if (ActualChildren.Any(i => i.Id == item.Id))
{ {
throw new ArgumentException(string.Format("A child with the Id {0} already exists.", item.Id)); throw new ArgumentException(string.Format("A child with the Id {0} already exists.", item.Id));
} }
@ -108,14 +108,14 @@ namespace MediaBrowser.Controller.Entities
await LibraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false); await LibraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
await ItemRepository.SaveChildren(Id, _children.Select(i => i.Id).ToList(), cancellationToken).ConfigureAwait(false); await ItemRepository.SaveChildren(Id, ActualChildren.Select(i => i.Id).ToList(), cancellationToken).ConfigureAwait(false);
} }
protected void AddChildrenInternal(IEnumerable<BaseItem> children) protected void AddChildrenInternal(IEnumerable<BaseItem> children)
{ {
lock (_childrenSyncLock) lock (_childrenSyncLock)
{ {
var newChildren = _children.ToList(); var newChildren = ActualChildren.ToList();
newChildren.AddRange(children); newChildren.AddRange(children);
_children = newChildren; _children = newChildren;
} }
@ -124,7 +124,7 @@ namespace MediaBrowser.Controller.Entities
{ {
lock (_childrenSyncLock) lock (_childrenSyncLock)
{ {
var newChildren = _children.ToList(); var newChildren = ActualChildren.ToList();
newChildren.Add(child); newChildren.Add(child);
_children = newChildren; _children = newChildren;
} }
@ -134,7 +134,7 @@ namespace MediaBrowser.Controller.Entities
{ {
lock (_childrenSyncLock) lock (_childrenSyncLock)
{ {
_children = _children.Except(children).ToList(); _children = ActualChildren.Except(children).ToList();
} }
} }
@ -519,7 +519,7 @@ namespace MediaBrowser.Controller.Entities
/// <summary> /// <summary>
/// The children /// The children
/// </summary> /// </summary>
private IReadOnlyList<BaseItem> _children = new List<BaseItem>(); private IReadOnlyList<BaseItem> _children;
/// <summary> /// <summary>
/// The _children sync lock /// The _children sync lock
/// </summary> /// </summary>
@ -532,15 +532,10 @@ namespace MediaBrowser.Controller.Entities
{ {
get get
{ {
return _children; return _children ?? (_children = LoadChildrenInternal());
} }
} }
public void LoadSavedChildren()
{
_children = LoadChildrenInternal();
}
/// <summary> /// <summary>
/// thread-safe access to the actual children of this folder - without regard to user /// thread-safe access to the actual children of this folder - without regard to user
/// </summary> /// </summary>
@ -758,7 +753,7 @@ namespace MediaBrowser.Controller.Entities
AddChildrenInternal(newItems); AddChildrenInternal(newItems);
await ItemRepository.SaveChildren(Id, _children.Select(i => i.Id).ToList(), cancellationToken).ConfigureAwait(false); await ItemRepository.SaveChildren(Id, ActualChildren.Select(i => i.Id).ToList(), cancellationToken).ConfigureAwait(false);
//force the indexes to rebuild next time //force the indexes to rebuild next time
if (IndexCache != null) if (IndexCache != null)
@ -1007,8 +1002,7 @@ namespace MediaBrowser.Controller.Entities
return result; return result;
} }
var initialCount = _children.Count; var list = new List<BaseItem>();
var list = new List<BaseItem>(initialCount);
AddChildrenToList(user, includeLinkedChildren, list, false, null); AddChildrenToList(user, includeLinkedChildren, list, false, null);
@ -1070,7 +1064,6 @@ namespace MediaBrowser.Controller.Entities
return hasLinkedChildren; return hasLinkedChildren;
} }
private int _lastRecursiveCount;
/// <summary> /// <summary>
/// Gets allowed recursive children of an item /// Gets allowed recursive children of an item
/// </summary> /// </summary>
@ -1098,13 +1091,10 @@ namespace MediaBrowser.Controller.Entities
throw new ArgumentNullException("user"); throw new ArgumentNullException("user");
} }
var initialCount = _lastRecursiveCount == 0 ? _children.Count : _lastRecursiveCount; var list = new List<BaseItem>();
var list = new List<BaseItem>(initialCount);
var hasLinkedChildren = AddChildrenToList(user, includeLinkedChildren, list, true, filter); var hasLinkedChildren = AddChildrenToList(user, includeLinkedChildren, list, true, filter);
_lastRecursiveCount = list.Count;
return hasLinkedChildren ? list.DistinctBy(i => i.Id).ToList() : list; return hasLinkedChildren ? list.DistinctBy(i => i.Id).ToList() : list;
} }
@ -1124,8 +1114,7 @@ namespace MediaBrowser.Controller.Entities
/// <returns>IEnumerable{BaseItem}.</returns> /// <returns>IEnumerable{BaseItem}.</returns>
public IList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter) public IList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter)
{ {
var initialCount = _lastRecursiveCount == 0 ? _children.Count : _lastRecursiveCount; var list = new List<BaseItem>();
var list = new List<BaseItem>(initialCount);
AddChildrenToList(list, true, filter); AddChildrenToList(list, true, filter);

View File

@ -40,7 +40,6 @@ namespace MediaBrowser.Controller.Entities
IndexName = indexName; IndexName = indexName;
Parent = parent; Parent = parent;
LoadSavedChildren();
} }
/// <summary> /// <summary>

View File

@ -1407,19 +1407,7 @@ namespace MediaBrowser.Server.Implementations.Library
/// <returns>BaseItem.</returns> /// <returns>BaseItem.</returns>
public BaseItem RetrieveItem(Guid id) public BaseItem RetrieveItem(Guid id)
{ {
var item = ItemRepository.RetrieveItem(id); return ItemRepository.RetrieveItem(id);
if (item != null && item.IsFolder)
{
LoadSavedChildren(item as Folder);
}
return item;
}
private void LoadSavedChildren(Folder item)
{
item.LoadSavedChildren();
} }
private readonly ConcurrentDictionary<string, SemaphoreSlim> _fileLocks = new ConcurrentDictionary<string, SemaphoreSlim>(); private readonly ConcurrentDictionary<string, SemaphoreSlim> _fileLocks = new ConcurrentDictionary<string, SemaphoreSlim>();