From c02ac2a8ca0a01e6951d5ccca13e98a118057505 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Fri, 15 Mar 2013 15:08:49 -0400 Subject: Manage some items as single instance throughout #54 --- .../Library/LibraryManager.cs | 40 +++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 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 549ea9be2..71fe6e125 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -105,6 +105,13 @@ namespace MediaBrowser.Server.Implementations.Library /// The configuration manager. private IServerConfigurationManager ConfigurationManager { get; set; } + /// + /// A collection of items that may be referenced from multiple physical places in the library + /// (typically, multiple user roots). We store them here and be sure they all reference a + /// single instance. + /// + private ConcurrentDictionary ByReferenceItems { get; set; } + /// /// Initializes a new instance of the class. /// @@ -120,6 +127,7 @@ namespace MediaBrowser.Server.Implementations.Library _taskManager = taskManager; _userManager = userManager; ConfigurationManager = configurationManager; + ByReferenceItems = new ConcurrentDictionary(); ConfigurationManager.ConfigurationUpdated += kernel_ConfigurationUpdated; @@ -232,11 +240,35 @@ namespace MediaBrowser.Server.Implementations.Library if (item != null) { ResolverHelper.SetInitialItemValues(item, args); + + // Now handle the issue with posibly having the same item referenced from multiple physical + // places within the library. Be sure we always end up with just one instance. + if (item is IByReferenceItem) + { + item = GetOrAddByReferenceItem(item); + } } return item; } + + /// + /// Ensure supplied item has only one instance throughout + /// + /// + /// The proper instance to the item + public BaseItem GetOrAddByReferenceItem(BaseItem item) + { + // Add this item to our list if not there already + if (!ByReferenceItems.TryAdd(item.Id, item)) + { + // Already there - return the existing reference + item = ByReferenceItems[item.Id]; + } + return item; + } + /// /// Resolves a path into a BaseItem /// @@ -617,10 +649,10 @@ namespace MediaBrowser.Server.Implementations.Library // Now validate the entire media library await RootFolder.ValidateChildren(progress, cancellationToken, recursive: true).ConfigureAwait(false); - foreach (var user in _userManager.Users) - { - await user.ValidateMediaLibrary(new Progress { }, cancellationToken).ConfigureAwait(false); - } + //foreach (var user in _userManager.Users) + //{ + // await user.ValidateMediaLibrary(new Progress { }, cancellationToken).ConfigureAwait(false); + //} } /// -- cgit v1.2.3