From 785deff188ba51243739b827dbe42b5645404367 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 13 Apr 2013 14:02:30 -0400 Subject: removed excess hashing in providers and made user data key-based --- .../Library/LibraryManager.cs | 7 +- .../Library/UserManager.cs | 81 ++++---------------- .../Providers/ProviderManager.cs | 28 +++---- .../Sorting/DatePlayedComparer.cs | 9 ++- .../Sorting/PlayCountComparer.cs | 9 ++- .../Sqlite/SQLiteDisplayPreferencesRepository.cs | 2 +- .../Sqlite/SQLiteUserDataRepository.cs | 86 ++++++++++++++++------ 7 files changed, 109 insertions(+), 113 deletions(-) (limited to 'MediaBrowser.Server.Implementations') diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index d397b1548..3bb5472df 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -101,6 +101,8 @@ namespace MediaBrowser.Server.Implementations.Library /// private readonly IUserManager _userManager; + private readonly IUserDataRepository _userDataRepository; + /// /// Gets or sets the configuration manager. /// @@ -136,12 +138,14 @@ namespace MediaBrowser.Server.Implementations.Library /// The task manager. /// The user manager. /// The configuration manager. - public LibraryManager(ILogger logger, ITaskManager taskManager, IUserManager userManager, IServerConfigurationManager configurationManager) + /// The user data repository. + public LibraryManager(ILogger logger, ITaskManager taskManager, IUserManager userManager, IServerConfigurationManager configurationManager, IUserDataRepository userDataRepository) { _logger = logger; _taskManager = taskManager; _userManager = userManager; ConfigurationManager = configurationManager; + _userDataRepository = userDataRepository; ByReferenceItems = new ConcurrentDictionary(); ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated; @@ -903,6 +907,7 @@ namespace MediaBrowser.Server.Implementations.Library userComparer.User = user; userComparer.UserManager = _userManager; + userComparer.UserDataRepository = _userDataRepository; return userComparer; } diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 311a31264..dbb2d7b32 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -1,6 +1,7 @@ using MediaBrowser.Common.Events; using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Persistence; @@ -86,20 +87,14 @@ namespace MediaBrowser.Server.Implementations.Library /// private readonly ILogger _logger; + private readonly IUserDataRepository _userDataRepository; + /// /// Gets or sets the configuration manager. /// /// The configuration manager. private IServerConfigurationManager ConfigurationManager { get; set; } - private readonly ConcurrentDictionary> _userData = new ConcurrentDictionary>(); - - /// - /// Gets the active user data repository - /// - /// The user data repository. - public IUserDataRepository UserDataRepository { get; set; } - /// /// Gets the active user repository /// @@ -321,13 +316,13 @@ namespace MediaBrowser.Server.Implementations.Library var connection = _activeConnections.GetOrAdd(key, keyName => new ClientConnectionInfo { - UserId = userId, + UserId = userId.ToString(), Client = clientType, DeviceName = deviceName, DeviceId = deviceId }); - connection.UserId = userId; + connection.UserId = userId.ToString(); return connection; } @@ -591,12 +586,14 @@ namespace MediaBrowser.Server.Implementations.Library UpdateNowPlayingItemId(user, clientType, deviceId, deviceName, item, positionTicks); + var key = item.GetUserDataKey(); + if (positionTicks.HasValue) { - var data = await GetUserData(user.Id, item.UserDataId).ConfigureAwait(false); + var data = await _userDataRepository.GetUserData(user.Id, key).ConfigureAwait(false); UpdatePlayState(item, data, positionTicks.Value, false); - await SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None).ConfigureAwait(false); + await _userDataRepository.SaveUserData(user.Id, key, data, CancellationToken.None).ConfigureAwait(false); } EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs @@ -631,7 +628,9 @@ namespace MediaBrowser.Server.Implementations.Library RemoveNowPlayingItemId(user, clientType, deviceId, deviceName, item); - var data = await GetUserData(user.Id, item.UserDataId).ConfigureAwait(false); + var key = item.GetUserDataKey(); + + var data = await _userDataRepository.GetUserData(user.Id, key).ConfigureAwait(false); if (positionTicks.HasValue) { @@ -644,7 +643,7 @@ namespace MediaBrowser.Server.Implementations.Library data.Played = true; } - await SaveUserData(user.Id, item.UserDataId, data, CancellationToken.None).ConfigureAwait(false); + await _userDataRepository.SaveUserData(user.Id, key, data, CancellationToken.None).ConfigureAwait(false); EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackProgressEventArgs { @@ -703,59 +702,5 @@ namespace MediaBrowser.Server.Implementations.Library data.LastPlayedDate = DateTime.UtcNow; } } - - /// - /// Saves display preferences for an item - /// - /// The user id. - /// The user data id. - /// The user data. - /// The cancellation token. - /// Task. - public async Task SaveUserData(Guid userId, Guid userDataId, UserItemData userData, CancellationToken cancellationToken) - { - var key = userId + userDataId.ToString(); - try - { - await UserDataRepository.SaveUserData(userId, userDataId, userData, cancellationToken).ConfigureAwait(false); - - var newValue = Task.FromResult(userData); - - // Once it succeeds, put it into the dictionary to make it available to everyone else - _userData.AddOrUpdate(key, newValue, delegate { return newValue; }); - } - catch (Exception ex) - { - _logger.ErrorException("Error saving user data", ex); - - throw; - } - } - - /// - /// Gets the user data. - /// - /// The user id. - /// The user data id. - /// Task{UserItemData}. - public Task GetUserData(Guid userId, Guid userDataId) - { - var key = userId + userDataId.ToString(); - - return _userData.GetOrAdd(key, keyName => RetrieveUserData(userId, userDataId)); - } - - /// - /// Retrieves the user data. - /// - /// The user id. - /// The user data id. - /// Task{UserItemData}. - private async Task RetrieveUserData(Guid userId, Guid userDataId) - { - var userdata = await UserDataRepository.GetUserData(userId, userDataId).ConfigureAwait(false); - - return userdata ?? new UserItemData(); - } } } diff --git a/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs b/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs index 8b135db5a..ff7222e7c 100644 --- a/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs +++ b/MediaBrowser.Server.Implementations/Providers/ProviderManager.cs @@ -88,12 +88,6 @@ namespace MediaBrowser.Server.Implementations.Providers Task.Run(() => ValidateCurrentlyRunningProviders()); } - /// - /// Gets or sets the supported providers key. - /// - /// The supported providers key. - private Guid SupportedProvidersKey { get; set; } - /// /// Adds the metadata providers. /// @@ -103,6 +97,11 @@ namespace MediaBrowser.Server.Implementations.Providers MetadataProviders = providers.OrderBy(e => e.Priority).ToArray(); } + /// + /// The _supported providers key + /// + private readonly Guid _supportedProvidersKey = "SupportedProviders".GetMD5(); + /// /// Runs all metadata providers for an entity, and returns true or false indicating if at least one was refreshed and requires persistence /// @@ -126,19 +125,14 @@ namespace MediaBrowser.Server.Implementations.Providers BaseProviderInfo supportedProvidersInfo; - if (SupportedProvidersKey == Guid.Empty) - { - SupportedProvidersKey = "SupportedProviders".GetMD5(); - } - - var supportedProvidersHash = string.Join("+", supportedProviders.Select(i => i.GetType().Name)).GetMD5(); - bool providersChanged = false; + var supportedProvidersValue = string.Join("+", supportedProviders.Select(i => i.GetType().Name)); + var providersChanged = false; - item.ProviderData.TryGetValue(SupportedProvidersKey, out supportedProvidersInfo); + item.ProviderData.TryGetValue(_supportedProvidersKey, out supportedProvidersInfo); if (supportedProvidersInfo != null) { // Force refresh if the supported providers have changed - providersChanged = force = force || supportedProvidersInfo.FileSystemStamp != supportedProvidersHash; + providersChanged = force = force || !string.Equals(supportedProvidersInfo.FileSystemStamp, supportedProvidersValue); // If providers have changed, clear provider info and update the supported providers hash if (providersChanged) @@ -150,7 +144,7 @@ namespace MediaBrowser.Server.Implementations.Providers if (providersChanged) { - supportedProvidersInfo.FileSystemStamp = supportedProvidersHash; + supportedProvidersInfo.FileSystemStamp = supportedProvidersValue; } if (force) item.ClearMetaValues(); @@ -206,7 +200,7 @@ namespace MediaBrowser.Server.Implementations.Providers if (providersChanged) { - item.ProviderData[SupportedProvidersKey] = supportedProvidersInfo; + item.ProviderData[_supportedProvidersKey] = supportedProvidersInfo; } return result || providersChanged; diff --git a/MediaBrowser.Server.Implementations/Sorting/DatePlayedComparer.cs b/MediaBrowser.Server.Implementations/Sorting/DatePlayedComparer.cs index 905d5413a..c634c760e 100644 --- a/MediaBrowser.Server.Implementations/Sorting/DatePlayedComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/DatePlayedComparer.cs @@ -1,5 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Sorting; using MediaBrowser.Model.Querying; using System; @@ -23,6 +24,12 @@ namespace MediaBrowser.Server.Implementations.Sorting /// The user manager. public IUserManager UserManager { get; set; } + /// + /// Gets or sets the user data repository. + /// + /// The user data repository. + public IUserDataRepository UserDataRepository { get; set; } + /// /// Compares the specified x. /// @@ -41,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// DateTime. private DateTime GetDate(BaseItem x) { - var userdata = UserManager.GetUserData(User.Id, x.UserDataId).Result; + var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey()).Result; if (userdata != null && userdata.LastPlayedDate.HasValue) { diff --git a/MediaBrowser.Server.Implementations/Sorting/PlayCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/PlayCountComparer.cs index 82e76e78d..a7cbd2149 100644 --- a/MediaBrowser.Server.Implementations/Sorting/PlayCountComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/PlayCountComparer.cs @@ -1,5 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Sorting; using MediaBrowser.Model.Querying; @@ -34,7 +35,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// DateTime. private int GetValue(BaseItem x) { - var userdata = UserManager.GetUserData(User.Id, x.UserDataId).Result; + var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey()).Result; return userdata == null ? 0 : userdata.PlayCount; } @@ -48,6 +49,12 @@ namespace MediaBrowser.Server.Implementations.Sorting get { return ItemSortBy.PlayCount; } } + /// + /// Gets or sets the user data repository. + /// + /// The user data repository. + public IUserDataRepository UserDataRepository { get; set; } + /// /// Gets or sets the user manager. /// diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs index 501635800..fbb0e4f8c 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs @@ -14,7 +14,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// /// Class SQLiteDisplayPreferencesRepository /// - class SQLiteDisplayPreferencesRepository : SqliteRepository, IDisplayPreferencesRepository + public class SQLiteDisplayPreferencesRepository : SqliteRepository, IDisplayPreferencesRepository { /// /// The repository name diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs index 467628a78..4df81aacc 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs @@ -4,6 +4,7 @@ using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using System; +using System.Collections.Concurrent; using System.Data; using System.IO; using System.Threading; @@ -16,6 +17,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// public class SQLiteUserDataRepository : SqliteRepository, IUserDataRepository { + private readonly ConcurrentDictionary> _userData = new ConcurrentDictionary>(); + /// /// The repository name /// @@ -45,9 +48,6 @@ namespace MediaBrowser.Server.Implementations.Sqlite } } - /// - /// The _protobuf serializer - /// private readonly IJsonSerializer _jsonSerializer; /// @@ -90,8 +90,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite string[] queries = { - "create table if not exists userdata (id GUID, userId GUID, data BLOB)", - "create unique index if not exists userdataindex on userdata (id, userId)", + "create table if not exists userdata (key nvarchar, userId GUID, data BLOB)", + "create unique index if not exists userdataindex on userdata (key, userId)", "create table if not exists schema_version (table_name primary key, version)", //pragmas "pragma temp_store = memory" @@ -104,20 +104,18 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// Saves the user data. /// /// The user id. - /// The user data id. + /// The key. /// The user data. /// The cancellation token. /// Task. - /// - /// userData + /// userData /// or /// cancellationToken /// or /// userId /// or - /// userDataId - /// - public async Task SaveUserData(Guid userId, Guid userDataId, UserItemData userData, CancellationToken cancellationToken) + /// userDataId + public async Task SaveUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken) { if (userData == null) { @@ -131,20 +129,49 @@ namespace MediaBrowser.Server.Implementations.Sqlite { throw new ArgumentNullException("userId"); } - if (userDataId == Guid.Empty) + if (string.IsNullOrEmpty(key)) { - throw new ArgumentNullException("userDataId"); + throw new ArgumentNullException("key"); } cancellationToken.ThrowIfCancellationRequested(); + try + { + await PersistUserData(userId, key, userData, cancellationToken).ConfigureAwait(false); + + var newValue = Task.FromResult(userData); + + // Once it succeeds, put it into the dictionary to make it available to everyone else + _userData.AddOrUpdate(key, newValue, delegate { return newValue; }); + } + catch (Exception ex) + { + Logger.ErrorException("Error saving user data", ex); + + throw; + } + } + + /// + /// Persists the user data. + /// + /// The user id. + /// The key. + /// The user data. + /// The cancellation token. + /// Task. + public async Task PersistUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + var serialized = _jsonSerializer.SerializeToBytes(userData); cancellationToken.ThrowIfCancellationRequested(); var cmd = connection.CreateCommand(); - cmd.CommandText = "replace into userdata (id, userId, data) values (@1, @2, @3)"; - cmd.AddParam("@1", userDataId); + cmd.CommandText = "replace into userdata (key, userId, data) values (@1, @2, @3)"; + cmd.AddParam("@1", key); cmd.AddParam("@2", userId); cmd.AddParam("@3", serialized); @@ -174,29 +201,40 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// Gets the user data. /// /// The user id. - /// The user data id. + /// The key. /// Task{UserItemData}. /// /// userId /// or - /// userDataId + /// key /// - public async Task GetUserData(Guid userId, Guid userDataId) + public Task GetUserData(Guid userId, string key) { if (userId == Guid.Empty) { throw new ArgumentNullException("userId"); } - if (userDataId == Guid.Empty) + if (string.IsNullOrEmpty(key)) { - throw new ArgumentNullException("userDataId"); + throw new ArgumentNullException("key"); } + + return _userData.GetOrAdd(key, keyName => RetrieveUserData(userId, key)); + } + /// + /// Retrieves the user data. + /// + /// The user id. + /// The key. + /// Task{UserItemData}. + private async Task RetrieveUserData(Guid userId, string key) + { var cmd = connection.CreateCommand(); - cmd.CommandText = "select data from userdata where id = @id and userId=@userId"; + cmd.CommandText = "select data from userdata where key = @key and userId=@userId"; - var idParam = cmd.Parameters.Add("@id", DbType.Guid); - idParam.Value = userDataId; + var idParam = cmd.Parameters.Add("@key", DbType.Guid); + idParam.Value = key; var userIdParam = cmd.Parameters.Add("@userId", DbType.Guid); userIdParam.Value = userId; @@ -212,7 +250,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite } } - return null; + return new UserItemData(); } } } -- cgit v1.2.3