From f9e9f5bef625bd994ae6a3f9356e958d228dc43d Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 8 Nov 2015 22:41:45 -0500 Subject: [PATCH] show more music favorites --- .../Sync/SyncRepository.cs | 85 +++++++------------ 1 file changed, 31 insertions(+), 54 deletions(-) diff --git a/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs b/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs index 75c9290164..d266a534d7 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs @@ -17,12 +17,9 @@ using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.Sync { - public class SyncRepository : ISyncRepository, IDisposable + public class SyncRepository : BaseSqliteRepository, ISyncRepository { private IDbConnection _connection; - private readonly ILogger _logger; - private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); - private readonly IServerApplicationPaths _appPaths; private readonly CultureInfo _usCulture = new CultureInfo("en-US"); private IDbCommand _insertJobCommand; @@ -34,19 +31,20 @@ namespace MediaBrowser.Server.Implementations.Sync private IDbCommand _updateJobItemCommand; private readonly IJsonSerializer _json; + private readonly IServerApplicationPaths _appPaths; - public SyncRepository(ILogger logger, IServerApplicationPaths appPaths, IJsonSerializer json) + public SyncRepository(ILogManager logManager, IJsonSerializer json, IServerApplicationPaths appPaths) + : base(logManager) { - _logger = logger; - _appPaths = appPaths; _json = json; + _appPaths = appPaths; } public async Task Initialize() { var dbFile = Path.Combine(_appPaths.DataPath, "sync14.db"); - _connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false); + _connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false); string[] queries = { @@ -62,10 +60,10 @@ namespace MediaBrowser.Server.Implementations.Sync "pragma shrink_memory" }; - _connection.RunQueries(queries, _logger); + _connection.RunQueries(queries, Logger); - _connection.AddColumn(_logger, "SyncJobs", "Profile", "TEXT"); - _connection.AddColumn(_logger, "SyncJobs", "Bitrate", "INT"); + _connection.AddColumn(Logger, "SyncJobs", "Profile", "TEXT"); + _connection.AddColumn(Logger, "SyncJobs", "Bitrate", "INT"); PrepareStatements(); } @@ -298,7 +296,7 @@ namespace MediaBrowser.Server.Implementations.Sync CheckDisposed(); - await _writeLock.WaitAsync().ConfigureAwait(false); + await WriteLock.WaitAsync().ConfigureAwait(false); IDbTransaction transaction = null; @@ -344,7 +342,7 @@ namespace MediaBrowser.Server.Implementations.Sync } catch (Exception e) { - _logger.ErrorException("Failed to save record:", e); + Logger.ErrorException("Failed to save record:", e); if (transaction != null) { @@ -360,7 +358,7 @@ namespace MediaBrowser.Server.Implementations.Sync transaction.Dispose(); } - _writeLock.Release(); + WriteLock.Release(); } } @@ -373,7 +371,7 @@ namespace MediaBrowser.Server.Implementations.Sync CheckDisposed(); - await _writeLock.WaitAsync().ConfigureAwait(false); + await WriteLock.WaitAsync().ConfigureAwait(false); IDbTransaction transaction = null; @@ -405,7 +403,7 @@ namespace MediaBrowser.Server.Implementations.Sync } catch (Exception e) { - _logger.ErrorException("Failed to save record:", e); + Logger.ErrorException("Failed to save record:", e); if (transaction != null) { @@ -421,7 +419,7 @@ namespace MediaBrowser.Server.Implementations.Sync transaction.Dispose(); } - _writeLock.Release(); + WriteLock.Release(); } } @@ -656,7 +654,7 @@ namespace MediaBrowser.Server.Implementations.Sync CheckDisposed(); - await _writeLock.WaitAsync().ConfigureAwait(false); + await WriteLock.WaitAsync().ConfigureAwait(false); IDbTransaction transaction = null; @@ -699,7 +697,7 @@ namespace MediaBrowser.Server.Implementations.Sync } catch (Exception e) { - _logger.ErrorException("Failed to save record:", e); + Logger.ErrorException("Failed to save record:", e); if (transaction != null) { @@ -715,7 +713,7 @@ namespace MediaBrowser.Server.Implementations.Sync transaction.Dispose(); } - _writeLock.Release(); + WriteLock.Release(); } } @@ -802,15 +800,6 @@ namespace MediaBrowser.Server.Implementations.Sync return item; } - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - private bool _disposed; private void CheckDisposed() { @@ -820,38 +809,26 @@ namespace MediaBrowser.Server.Implementations.Sync } } - private readonly object _disposeLock = new object(); - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool dispose) + protected override void Dispose(bool dispose) { if (dispose) { _disposed = true; + } + base.Dispose(dispose); + } - try - { - lock (_disposeLock) - { - if (_connection != null) - { - if (_connection.IsOpen()) - { - _connection.Close(); - } - - _connection.Dispose(); - _connection = null; - } - } - } - catch (Exception ex) + protected override void CloseConnection() + { + if (_connection != null) + { + if (_connection.IsOpen()) { - _logger.ErrorException("Error disposing database", ex); + _connection.Close(); } + + _connection.Dispose(); + _connection = null; } } }