From 4986722c7305ebce02c37c80363a909d372ff7a3 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 5 Apr 2013 15:02:48 -0400 Subject: made display preferences uniquely identifiable --- .../Sqlite/SQLiteDisplayPreferencesRepository.cs | 39 +++++++--------------- 1 file changed, 12 insertions(+), 27 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs') diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs index 8a15d4028..5f6ac39ee 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs @@ -78,8 +78,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite string[] queries = { - "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 displaypreferences (id GUID, data BLOB)", + "create unique index if not exists displaypreferencesindex on displaypreferences (id)", "create table if not exists schema_version (table_name primary key, version)", //pragmas "pragma temp_store = memory" @@ -91,29 +91,23 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// /// Save the display preferences associated with an item in the repo /// - /// The user id. - /// The display preferences id. /// The display preferences. /// The cancellation token. /// Task. /// item - public Task SaveDisplayPreferences(Guid userId, Guid displayPreferencesId, DisplayPreferences displayPreferences, CancellationToken cancellationToken) + public Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken) { if (displayPreferences == null) { throw new ArgumentNullException("displayPreferences"); } - if (cancellationToken == null) - { - throw new ArgumentNullException("cancellationToken"); - } - if (userId == Guid.Empty) + if (displayPreferences.Id == Guid.Empty) { - throw new ArgumentNullException("userId"); + throw new ArgumentNullException("displayPreferences.Id"); } - if (displayPreferencesId == Guid.Empty) + if (cancellationToken == null) { - throw new ArgumentNullException("displayPreferencesId"); + throw new ArgumentNullException("cancellationToken"); } cancellationToken.ThrowIfCancellationRequested(); @@ -125,10 +119,9 @@ namespace MediaBrowser.Server.Implementations.Sqlite cancellationToken.ThrowIfCancellationRequested(); var cmd = connection.CreateCommand(); - cmd.CommandText = "replace into displaypreferences (id, userId, data) values (@1, @2, @3)"; - cmd.AddParam("@1", displayPreferencesId); - cmd.AddParam("@2", userId); - cmd.AddParam("@3", serialized); + cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @3)"; + cmd.AddParam("@1", displayPreferences.Id); + cmd.AddParam("@2", serialized); QueueCommand(cmd); }); } @@ -136,30 +129,22 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// /// Gets the display preferences. /// - /// The user id. /// The display preferences id. /// Task{DisplayPreferences}. /// item - public async Task GetDisplayPreferences(Guid userId, Guid displayPreferencesId) + public async Task GetDisplayPreferences(Guid displayPreferencesId) { - if (userId == Guid.Empty) - { - throw new ArgumentNullException("userId"); - } if (displayPreferencesId == Guid.Empty) { throw new ArgumentNullException("displayPreferencesId"); } var cmd = connection.CreateCommand(); - cmd.CommandText = "select data from displaypreferences where id = @id and userId=@userId"; + cmd.CommandText = "select data from displaypreferences where id = @id"; var idParam = cmd.Parameters.Add("@id", DbType.Guid); idParam.Value = displayPreferencesId; - 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)) { if (reader.Read()) -- cgit v1.2.3 From f4f3d1255e34bced614e125dc8d8b05c29e41ead Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 5 Apr 2013 16:24:46 -0400 Subject: fix display preferences save --- .../Sqlite/SQLiteDisplayPreferencesRepository.cs | 39 ++++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs') diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs index 5f6ac39ee..c6f35a978 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs @@ -95,7 +95,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// The cancellation token. /// Task. /// item - public Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken) + public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken) { if (displayPreferences == null) { @@ -111,19 +111,36 @@ namespace MediaBrowser.Server.Implementations.Sqlite } cancellationToken.ThrowIfCancellationRequested(); - - return Task.Run(() => + + var serialized = _protobufSerializer.SerializeToBytes(displayPreferences); + + cancellationToken.ThrowIfCancellationRequested(); + + var cmd = connection.CreateCommand(); + cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @2)"; + cmd.AddParam("@1", displayPreferences.Id); + cmd.AddParam("@2", serialized); + + using (var tran = connection.BeginTransaction()) { - var serialized = _protobufSerializer.SerializeToBytes(displayPreferences); + try + { + cmd.Transaction = tran; - cancellationToken.ThrowIfCancellationRequested(); + await cmd.ExecuteNonQueryAsync(cancellationToken); - var cmd = connection.CreateCommand(); - cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @3)"; - cmd.AddParam("@1", displayPreferences.Id); - cmd.AddParam("@2", serialized); - QueueCommand(cmd); - }); + tran.Commit(); + } + catch (OperationCanceledException) + { + tran.Rollback(); + } + catch (Exception e) + { + Logger.ErrorException("Failed to commit transaction.", e); + tran.Rollback(); + } + } } /// -- cgit v1.2.3 From 9794c8fb1adc01823a9bdd469b470390fee0ebcd Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 5 Apr 2013 16:49:14 -0400 Subject: #99 - Active user list wrong --- .../Library/UserManager.cs | 38 +++++++---------- .../Sqlite/SQLiteDisplayPreferencesRepository.cs | 12 ++++++ .../Sqlite/SQLiteRepository.cs | 48 +++++++++++++++++----- 3 files changed, 63 insertions(+), 35 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs') diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 682ec9a8c..9293d8199 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -25,8 +25,8 @@ namespace MediaBrowser.Server.Implementations.Library /// /// The _active connections /// - private readonly List _activeConnections = - new List(); + private readonly ConcurrentDictionary _activeConnections = + new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); /// /// The _users @@ -69,7 +69,7 @@ namespace MediaBrowser.Server.Implementations.Library /// All connections. public IEnumerable AllConnections { - get { return _activeConnections.Where(c => GetUserById(c.UserId) != null).OrderByDescending(c => c.LastActivityDate); } + get { return _activeConnections.Values.OrderByDescending(c => c.LastActivityDate); } } /// @@ -313,29 +313,19 @@ namespace MediaBrowser.Server.Implementations.Library /// ClientConnectionInfo. private ClientConnectionInfo GetConnection(Guid userId, string clientType, string deviceId, string deviceName) { - lock (_activeConnections) - { - var conn = _activeConnections.FirstOrDefault(c => string.Equals(c.Client, clientType, StringComparison.OrdinalIgnoreCase) && string.Equals(deviceId, c.DeviceId)); + var key = clientType + deviceId; - if (conn == null) - { - conn = new ClientConnectionInfo - { - UserId = userId, - Client = clientType, - DeviceName = deviceName, - DeviceId = deviceId - }; - - _activeConnections.Add(conn); - } - else - { - conn.UserId = userId; - } + var connection = _activeConnections.GetOrAdd(key, keyName => new ClientConnectionInfo + { + UserId = userId, + Client = clientType, + DeviceName = deviceName, + DeviceId = deviceId + }); - return conn; - } + connection.UserId = userId; + + return connection; } /// diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs index c6f35a978..f471365ce 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs @@ -33,6 +33,18 @@ namespace MediaBrowser.Server.Implementations.Sqlite } } + /// + /// Gets a value indicating whether [enable delayed commands]. + /// + /// true if [enable delayed commands]; otherwise, false. + protected override bool EnableDelayedCommands + { + get + { + return false; + } + } + /// /// The _protobuf serializer /// diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs index c5320a1f6..376cf5065 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs @@ -43,6 +43,18 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// The logger. protected ILogger Logger { get; private set; } + /// + /// Gets a value indicating whether [enable delayed commands]. + /// + /// true if [enable delayed commands]; otherwise, false. + protected virtual bool EnableDelayedCommands + { + get + { + return true; + } + } + /// /// Initializes a new instance of the class. /// @@ -85,8 +97,11 @@ namespace MediaBrowser.Server.Implementations.Sqlite await connection.OpenAsync().ConfigureAwait(false); - // Run once - FlushTimer = new Timer(Flush, null, TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1)); + if (EnableDelayedCommands) + { + // Run once + FlushTimer = new Timer(Flush, null, TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1)); + } } /// @@ -147,16 +162,9 @@ namespace MediaBrowser.Server.Implementations.Sqlite { if (connection != null) { - // If we're not already flushing, do it now - if (!IsFlushing) - { - Flush(null); - } - - // Don't dispose in the middle of a flush - while (IsFlushing) + if (EnableDelayedCommands) { - Thread.Sleep(25); + FlushOnDispose(); } if (connection.IsOpen()) @@ -181,6 +189,24 @@ namespace MediaBrowser.Server.Implementations.Sqlite } } + /// + /// Flushes the on dispose. + /// + private void FlushOnDispose() + { + // If we're not already flushing, do it now + if (!IsFlushing) + { + Flush(null); + } + + // Don't dispose in the middle of a flush + while (IsFlushing) + { + Thread.Sleep(25); + } + } + /// /// Queues the command. /// -- cgit v1.2.3