mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-20 00:55:13 +01:00
moved displaypreferences to usermanager to solve concurrency issues
This commit is contained in:
@@ -160,7 +160,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
|
||||
|
||||
if (exception != null)
|
||||
{
|
||||
_logger.ErrorException("Error processing request", exception);
|
||||
_logger.ErrorException("Error processing request for {0}", exception, req.RawUrl);
|
||||
|
||||
if (!string.IsNullOrEmpty(exception.Message))
|
||||
{
|
||||
|
||||
@@ -124,15 +124,6 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
LazyInitializer.EnsureInitialized(ref _libraryItemsCache, ref _libraryItemsCacheInitialized, ref _libraryItemsCacheSyncLock, CreateLibraryItemsCache);
|
||||
return _libraryItemsCache;
|
||||
}
|
||||
set
|
||||
{
|
||||
_libraryItemsCache = value;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
_libraryItemsCacheInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -756,26 +747,6 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
await RootFolder.ValidateChildren(progress, cancellationToken, recursive: true).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves display preferences for a Folder
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="folder">The folder.</param>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public Task SaveDisplayPreferencesForFolder(User user, Folder folder, DisplayPreferences data)
|
||||
{
|
||||
// Need to update all items with the same DisplayPreferencesId
|
||||
foreach (var child in RootFolder.GetRecursiveChildren(user)
|
||||
.OfType<Folder>()
|
||||
.Where(i => i.DisplayPreferencesId == folder.DisplayPreferencesId))
|
||||
{
|
||||
child.AddOrUpdateDisplayPreferences(user, data);
|
||||
}
|
||||
|
||||
return Kernel.DisplayPreferencesRepository.SaveDisplayPreferences(folder, CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default view.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using MediaBrowser.Common.Events;
|
||||
using System.Collections.Concurrent;
|
||||
using MediaBrowser.Common.Events;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Connectivity;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -97,6 +99,11 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
/// <value>The configuration manager.</value>
|
||||
private IServerConfigurationManager ConfigurationManager { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The _user data
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<string, Task<DisplayPreferences>> _displayPreferences = new ConcurrentDictionary<string, Task<DisplayPreferences>>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UserManager" /> class.
|
||||
/// </summary>
|
||||
@@ -156,6 +163,62 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>DisplayPreferences.</returns>
|
||||
public Task<DisplayPreferences> GetDisplayPreferences(Guid userId, Guid displayPreferencesId)
|
||||
{
|
||||
var key = userId + displayPreferencesId.ToString();
|
||||
|
||||
return _displayPreferences.GetOrAdd(key, keyName => RetrieveDisplayPreferences(userId, displayPreferencesId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>DisplayPreferences.</returns>
|
||||
private async Task<DisplayPreferences> RetrieveDisplayPreferences(Guid userId, Guid displayPreferencesId)
|
||||
{
|
||||
var displayPreferences = await Kernel.Instance.DisplayPreferencesRepository.GetDisplayPreferences(userId, displayPreferencesId).ConfigureAwait(false);
|
||||
|
||||
return displayPreferences ?? new DisplayPreferences();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves display preferences for an item
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <param name="displayPreferences">The display preferences.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public async Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken)
|
||||
{
|
||||
var key = userId + displayPreferencesId.ToString();
|
||||
var newValue = Task.FromResult(displayPreferences);
|
||||
|
||||
try
|
||||
{
|
||||
await Kernel.Instance.DisplayPreferencesRepository.SaveDisplayPreferences(userId, displayPreferencesId,
|
||||
displayPreferences,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Once it succeeds, put it into the dictionary to make it available to everyone else
|
||||
_displayPreferences.AddOrUpdate(key, newValue, delegate { return newValue; });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error saving display preferences", ex);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a User by Id
|
||||
/// </summary>
|
||||
@@ -711,6 +774,5 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
|
||||
return Kernel.UserDataRepository.SaveUserData(item, CancellationToken.None);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
@@ -80,8 +78,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
||||
|
||||
string[] queries = {
|
||||
|
||||
"create table if not exists display_prefs (item_id GUID, user_id GUID, data BLOB)",
|
||||
"create unique index if not exists idx_display_prefs on display_prefs (item_id, user_id)",
|
||||
"create table if not exists displaypreferences (id GUID, userId GUID, data BLOB)",
|
||||
"create unique index if not exists displaypreferencesindex on displaypreferences (id, userId)",
|
||||
"create table if not exists schema_version (table_name primary key, version)",
|
||||
//pragmas
|
||||
"pragma temp_store = memory"
|
||||
@@ -93,82 +91,87 @@ namespace MediaBrowser.Server.Implementations.Sqlite
|
||||
/// <summary>
|
||||
/// Save the display preferences associated with an item in the repo
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <param name="displayPreferences">The display preferences.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
public Task SaveDisplayPreferences(Folder item, CancellationToken cancellationToken)
|
||||
public Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken)
|
||||
{
|
||||
if (item == null)
|
||||
if (displayPreferences == null)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
throw new ArgumentNullException("displayPreferences");
|
||||
}
|
||||
|
||||
if (cancellationToken == null)
|
||||
{
|
||||
throw new ArgumentNullException("cancellationToken");
|
||||
}
|
||||
if (userId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("userId");
|
||||
}
|
||||
if (displayPreferencesId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("displayPreferencesId");
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var serialized = _protobufSerializer.SerializeToBytes(displayPreferences);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var cmd = connection.CreateCommand();
|
||||
|
||||
cmd.CommandText = "delete from display_prefs where item_id = @guid";
|
||||
cmd.AddParam("@guid", item.DisplayPreferencesId);
|
||||
|
||||
cmd.CommandText = "replace into displaypreferences (id, userId, data) values (@1, @2, @3)";
|
||||
cmd.AddParam("@1", displayPreferencesId);
|
||||
cmd.AddParam("@2", userId);
|
||||
cmd.AddParam("@3", serialized);
|
||||
QueueCommand(cmd);
|
||||
|
||||
if (item.DisplayPreferences != null)
|
||||
{
|
||||
foreach (var data in item.DisplayPreferences)
|
||||
{
|
||||
cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "insert into display_prefs (item_id, user_id, data) values (@1, @2, @3)";
|
||||
cmd.AddParam("@1", item.DisplayPreferencesId);
|
||||
cmd.AddParam("@2", data.UserId);
|
||||
|
||||
cmd.AddParam("@3", _protobufSerializer.SerializeToBytes(data));
|
||||
|
||||
QueueCommand(cmd);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets display preferences for an item
|
||||
/// Gets the display preferences.
|
||||
/// </summary>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <returns>IEnumerable{DisplayPreferences}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException"></exception>
|
||||
public IEnumerable<DisplayPreferences> RetrieveDisplayPreferences(Folder item)
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="displayPreferencesId">The display preferences id.</param>
|
||||
/// <returns>Task{DisplayPreferences}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">item</exception>
|
||||
public async Task<DisplayPreferences> GetDisplayPreferences(Guid userId, Guid displayPreferencesId)
|
||||
{
|
||||
if (item == null)
|
||||
if (userId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
throw new ArgumentNullException("userId");
|
||||
}
|
||||
if (displayPreferencesId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("displayPreferencesId");
|
||||
}
|
||||
|
||||
var cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "select data from display_prefs where item_id = @guid";
|
||||
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
|
||||
guidParam.Value = item.DisplayPreferencesId;
|
||||
cmd.CommandText = "select data from displaypreferences where id = @id and userId=@userId";
|
||||
|
||||
var idParam = cmd.Parameters.Add("@id", DbType.Guid);
|
||||
idParam.Value = displayPreferencesId;
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||||
var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid);
|
||||
userIdParam.Value = userId;
|
||||
|
||||
using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow).ConfigureAwait(false))
|
||||
{
|
||||
while (reader.Read())
|
||||
if (reader.Read())
|
||||
{
|
||||
using (var stream = GetStream(reader, 0))
|
||||
{
|
||||
var data = _protobufSerializer.DeserializeFromStream<DisplayPreferences>(stream);
|
||||
if (data != null)
|
||||
{
|
||||
yield return data;
|
||||
}
|
||||
return _protobufSerializer.DeserializeFromStream<DisplayPreferences>(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user