diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 16bfff564..b32775655 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -691,16 +691,10 @@ namespace MediaBrowser.Controller.Entities } } - var saveTasks = new List(); + await LibraryManager.CreateItems(newItems, cancellationToken).ConfigureAwait(false); foreach (var item in newItems) { - if (saveTasks.Count > 50) - { - await Task.WhenAll(saveTasks).ConfigureAwait(false); - saveTasks.Clear(); - } - if (!_children.TryAdd(item.Id, item)) { Logger.Error("Failed to add {0}", item.Name); @@ -709,12 +703,8 @@ namespace MediaBrowser.Controller.Entities { Logger.Debug("** " + item.Name + " Added to library."); } - - saveTasks.Add(LibraryManager.CreateItem(item, CancellationToken.None)); } - await Task.WhenAll(saveTasks).ConfigureAwait(false); - await LibraryManager.SaveChildren(Id, newChildren, CancellationToken.None).ConfigureAwait(false); //force the indexes to rebuild next time diff --git a/MediaBrowser.Controller/Library/ILibraryManager.cs b/MediaBrowser.Controller/Library/ILibraryManager.cs index 0917fa276..90c96c1ee 100644 --- a/MediaBrowser.Controller/Library/ILibraryManager.cs +++ b/MediaBrowser.Controller/Library/ILibraryManager.cs @@ -184,6 +184,14 @@ namespace MediaBrowser.Controller.Library /// Task. Task CreateItem(BaseItem item, CancellationToken cancellationToken); + /// + /// Creates the items. + /// + /// The items. + /// The cancellation token. + /// Task. + Task CreateItems(IEnumerable items, CancellationToken cancellationToken); + /// /// Updates the item. /// diff --git a/MediaBrowser.Controller/Persistence/IItemRepository.cs b/MediaBrowser.Controller/Persistence/IItemRepository.cs index 6f71ee9be..bf3bc626a 100644 --- a/MediaBrowser.Controller/Persistence/IItemRepository.cs +++ b/MediaBrowser.Controller/Persistence/IItemRepository.cs @@ -64,5 +64,13 @@ namespace MediaBrowser.Controller.Persistence /// The critic reviews. /// Task. Task SaveCriticReviews(Guid itemId, IEnumerable criticReviews); + + /// + /// Saves the items. + /// + /// The items. + /// The cancellation token. + /// Task. + Task SaveItems(IEnumerable items, CancellationToken cancellationToken); } } diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 72cf42c14..fbf2dfc70 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -1036,21 +1036,40 @@ namespace MediaBrowser.Server.Implementations.Library /// The item. /// The cancellation token. /// Task. - public async Task CreateItem(BaseItem item, CancellationToken cancellationToken) + public Task CreateItem(BaseItem item, CancellationToken cancellationToken) { - await SaveItem(item, cancellationToken).ConfigureAwait(false); + return CreateItems(new[] { item }, cancellationToken); + } - UpdateItemInLibraryCache(item); + /// + /// Creates the items. + /// + /// The items. + /// The cancellation token. + /// Task. + public async Task CreateItems(IEnumerable items, CancellationToken cancellationToken) + { + var list = items.ToList(); + + await ItemRepository.SaveItems(list, cancellationToken).ConfigureAwait(false); + + foreach (var item in list) + { + UpdateItemInLibraryCache(item); + } if (ItemAdded != null) { - try + foreach (var item in list) { - ItemAdded(this, new ItemChangeEventArgs { Item = item }); - } - catch (Exception ex) - { - _logger.ErrorException("Error in ItemUpdated event handler", ex); + try + { + ItemAdded(this, new ItemChangeEventArgs { Item = item }); + } + catch (Exception ex) + { + _logger.ErrorException("Error in ItemUpdated event handler", ex); + } } } } @@ -1063,7 +1082,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Task. public async Task UpdateItem(BaseItem item, CancellationToken cancellationToken) { - await SaveItem(item, cancellationToken).ConfigureAwait(false); + await ItemRepository.SaveItem(item, cancellationToken).ConfigureAwait(false); UpdateItemInLibraryCache(item); @@ -1099,17 +1118,6 @@ namespace MediaBrowser.Server.Implementations.Library } } - /// - /// Saves the item. - /// - /// The item. - /// The cancellation token. - /// Task. - private Task SaveItem(BaseItem item, CancellationToken cancellationToken) - { - return ItemRepository.SaveItem(item, cancellationToken); - } - /// /// Retrieves the item. /// diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteItemRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteItemRepository.cs index 23ab10499..0ca4dda77 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteItemRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteItemRepository.cs @@ -135,7 +135,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// /// The _write lock /// - private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1,1); + private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); /// /// Prepares the statements. @@ -172,13 +172,34 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// The cancellation token. /// Task. /// item - public async Task SaveItem(BaseItem item, CancellationToken cancellationToken) + public Task SaveItem(BaseItem item, CancellationToken cancellationToken) { if (item == null) { throw new ArgumentNullException("item"); } + return SaveItems(new[] { item }, cancellationToken); + } + + /// + /// Saves the items. + /// + /// The items. + /// The cancellation token. + /// Task. + /// + /// items + /// or + /// cancellationToken + /// + public async Task SaveItems(IEnumerable items, CancellationToken cancellationToken) + { + if (items == null) + { + throw new ArgumentNullException("items"); + } + if (cancellationToken == null) { throw new ArgumentNullException("cancellationToken"); @@ -186,8 +207,6 @@ namespace MediaBrowser.Server.Implementations.Sqlite cancellationToken.ThrowIfCancellationRequested(); - var serialized = _jsonSerializer.SerializeToBytes(item); - await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false); SQLiteTransaction transaction = null; @@ -196,13 +215,18 @@ namespace MediaBrowser.Server.Implementations.Sqlite { transaction = Connection.BeginTransaction(); - _saveItemCommand.Parameters[0].Value = item.Id; - _saveItemCommand.Parameters[1].Value = item.GetType().FullName; - _saveItemCommand.Parameters[2].Value = serialized; + foreach (var item in items) + { + cancellationToken.ThrowIfCancellationRequested(); + + _saveItemCommand.Parameters[0].Value = item.Id; + _saveItemCommand.Parameters[1].Value = item.GetType().FullName; + _saveItemCommand.Parameters[2].Value = _jsonSerializer.SerializeToBytes(item); - _saveItemCommand.Transaction = transaction; + _saveItemCommand.Transaction = transaction; - await _saveItemCommand.ExecuteNonQueryAsync(cancellationToken); + await _saveItemCommand.ExecuteNonQueryAsync(cancellationToken); + } transaction.Commit(); } @@ -400,7 +424,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite foreach (var child in children) { _saveChildrenCommand.Transaction = transaction; - + _saveChildrenCommand.Parameters[0].Value = id; _saveChildrenCommand.Parameters[1].Value = child.Id; diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs index bd60c834f..ee2fcc10c 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs @@ -1,11 +1,9 @@ using MediaBrowser.Model.Logging; using System; -using System.Collections.Concurrent; using System.Data; using System.Data.Common; using System.Data.SQLite; using System.IO; -using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.Sqlite