@ -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 , _l ogger) . ConfigureAwait ( false ) ;
_connection = await SqliteExtensions . ConnectToDb ( dbFile , L ogger) . ConfigureAwait ( false ) ;
string [ ] queries = {
@ -62,10 +60,10 @@ namespace MediaBrowser.Server.Implementations.Sync
"pragma shrink_memory"
} ;
_connection . RunQueries ( queries , _l ogger) ;
_connection . RunQueries ( queries , L ogger) ;
_connection . AddColumn ( _l ogger, "SyncJobs" , "Profile" , "TEXT" ) ;
_connection . AddColumn ( _l ogger, "SyncJobs" , "Bitrate" , "INT" ) ;
_connection . AddColumn ( L ogger, "SyncJobs" , "Profile" , "TEXT" ) ;
_connection . AddColumn ( L ogger, "SyncJobs" , "Bitrate" , "INT" ) ;
PrepareStatements ( ) ;
}
@ -298,7 +296,7 @@ namespace MediaBrowser.Server.Implementations.Sync
CheckDisposed ( ) ;
await _w riteLock. WaitAsync ( ) . ConfigureAwait ( false ) ;
await W riteLock. WaitAsync ( ) . ConfigureAwait ( false ) ;
IDbTransaction transaction = null ;
@ -344,7 +342,7 @@ namespace MediaBrowser.Server.Implementations.Sync
}
catch ( Exception e )
{
_l ogger. ErrorException ( "Failed to save record:" , e ) ;
L ogger. ErrorException ( "Failed to save record:" , e ) ;
if ( transaction ! = null )
{
@ -360,7 +358,7 @@ namespace MediaBrowser.Server.Implementations.Sync
transaction . Dispose ( ) ;
}
_w riteLock. Release ( ) ;
W riteLock. Release ( ) ;
}
}
@ -373,7 +371,7 @@ namespace MediaBrowser.Server.Implementations.Sync
CheckDisposed ( ) ;
await _w riteLock. WaitAsync ( ) . ConfigureAwait ( false ) ;
await W riteLock. WaitAsync ( ) . ConfigureAwait ( false ) ;
IDbTransaction transaction = null ;
@ -405,7 +403,7 @@ namespace MediaBrowser.Server.Implementations.Sync
}
catch ( Exception e )
{
_l ogger. ErrorException ( "Failed to save record:" , e ) ;
L ogger. ErrorException ( "Failed to save record:" , e ) ;
if ( transaction ! = null )
{
@ -421,7 +419,7 @@ namespace MediaBrowser.Server.Implementations.Sync
transaction . Dispose ( ) ;
}
_w riteLock. Release ( ) ;
W riteLock. Release ( ) ;
}
}
@ -656,7 +654,7 @@ namespace MediaBrowser.Server.Implementations.Sync
CheckDisposed ( ) ;
await _w riteLock. WaitAsync ( ) . ConfigureAwait ( false ) ;
await W riteLock. WaitAsync ( ) . ConfigureAwait ( false ) ;
IDbTransaction transaction = null ;
@ -699,7 +697,7 @@ namespace MediaBrowser.Server.Implementations.Sync
}
catch ( Exception e )
{
_l ogger. ErrorException ( "Failed to save record:" , e ) ;
L ogger. ErrorException ( "Failed to save record:" , e ) ;
if ( transaction ! = null )
{
@ -715,7 +713,7 @@ namespace MediaBrowser.Server.Implementations.Sync
transaction . Dispose ( ) ;
}
_w riteLock. Release ( ) ;
W riteLock. Release ( ) ;
}
}
@ -802,15 +800,6 @@ namespace MediaBrowser.Server.Implementations.Sync
return item ;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
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 ( ) ;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
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 ;
}
}
}