mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-20 09:04:42 +01:00
bulk save items when possible
This commit is contained in:
@@ -1036,21 +1036,40 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
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);
|
||||
/// <summary>
|
||||
/// Creates the items.
|
||||
/// </summary>
|
||||
/// <param name="items">The items.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public async Task CreateItems(IEnumerable<BaseItem> 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
|
||||
/// <returns>Task.</returns>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the item.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
private Task SaveItem(BaseItem item, CancellationToken cancellationToken)
|
||||
{
|
||||
return ItemRepository.SaveItem(item, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the item.
|
||||
/// </summary>
|
||||
|
||||
@@ -135,7 +135,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
||||
/// <summary>
|
||||
/// The _write lock
|
||||
/// </summary>
|
||||
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1,1);
|
||||
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// Prepares the statements.
|
||||
@@ -172,13 +172,34 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the items.
|
||||
/// </summary>
|
||||
/// <param name="items">The items.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">
|
||||
/// items
|
||||
/// or
|
||||
/// cancellationToken
|
||||
/// </exception>
|
||||
public async Task SaveItems(IEnumerable<BaseItem> 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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user