2015-03-15 19:10:27 +00:00
using MediaBrowser.Controller ;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Controller.Sync ;
2015-01-17 04:29:53 +00:00
using MediaBrowser.Model.Dto ;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Model.Logging ;
using MediaBrowser.Model.Querying ;
2015-01-17 04:29:53 +00:00
using MediaBrowser.Model.Serialization ;
2014-07-26 17:30:15 +00:00
using MediaBrowser.Model.Sync ;
using MediaBrowser.Server.Implementations.Persistence ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Threading.Tasks ;
namespace MediaBrowser.Server.Implementations.Sync
{
2015-11-09 03:41:45 +00:00
public class SyncRepository : BaseSqliteRepository , ISyncRepository
2014-07-26 17:30:15 +00:00
{
private readonly CultureInfo _usCulture = new CultureInfo ( "en-US" ) ;
2015-01-17 04:29:53 +00:00
private readonly IJsonSerializer _json ;
2016-06-11 15:55:05 +00:00
public SyncRepository ( ILogManager logManager , IJsonSerializer json , IServerApplicationPaths appPaths , IDbConnector connector )
: base ( logManager , connector )
2014-07-26 17:30:15 +00:00
{
2015-01-17 04:29:53 +00:00
_json = json ;
2016-06-11 15:55:05 +00:00
DbFilePath = Path . Combine ( appPaths . DataPath , "sync14.db" ) ;
2014-07-26 17:30:15 +00:00
}
2016-08-03 06:38:19 +00:00
private class SyncSummary
{
public Dictionary < string , int > Items { get ; set ; }
public SyncSummary ( )
{
Items = new Dictionary < string , int > ( ) ;
}
}
2016-06-11 15:55:05 +00:00
public async Task Initialize ( )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
string [ ] queries = {
2014-07-26 17:30:15 +00:00
2015-03-15 19:10:27 +00:00
"create table if not exists SyncJobs (Id GUID PRIMARY KEY, TargetId TEXT NOT NULL, Name TEXT NOT NULL, Profile TEXT, Quality TEXT, Bitrate INT, Status TEXT NOT NULL, Progress FLOAT, UserId TEXT NOT NULL, ItemIds TEXT NOT NULL, Category TEXT, ParentId TEXT, UnwatchedOnly BIT, ItemLimit INT, SyncNewContent BIT, DateCreated DateTime, DateLastModified DateTime, ItemCount int)" ,
2014-07-26 17:30:15 +00:00
2016-04-27 20:24:53 +00:00
"create table if not exists SyncJobItems (Id GUID PRIMARY KEY, ItemId TEXT, ItemName TEXT, MediaSourceId TEXT, JobId TEXT, TemporaryPath TEXT, OutputPath TEXT, Status TEXT, TargetId TEXT, DateCreated DateTime, Progress FLOAT, AdditionalFiles TEXT, MediaSource TEXT, IsMarkedForRemoval BIT, JobItemIndex INT, ItemDateModifiedTicks BIGINT)" ,
2016-08-03 06:38:19 +00:00
"drop index if exists idx_SyncJobItems2" ,
"drop index if exists idx_SyncJobItems3" ,
2016-08-26 17:24:04 +00:00
"drop index if exists idx_SyncJobs1" ,
"drop index if exists idx_SyncJobs" ,
"drop index if exists idx_SyncJobItems1" ,
2016-08-03 06:38:19 +00:00
"create index if not exists idx_SyncJobItems4 on SyncJobItems(TargetId,ItemId,Status,Progress,DateCreated)" ,
2016-08-26 17:24:04 +00:00
"create index if not exists idx_SyncJobItems5 on SyncJobItems(TargetId,Status,ItemId,Progress)" ,
"create index if not exists idx_SyncJobs2 on SyncJobs(TargetId,Status,ItemIds,Progress)" ,
2014-07-26 17:30:15 +00:00
"pragma shrink_memory"
} ;
2016-06-11 15:55:05 +00:00
connection . RunQueries ( queries , Logger ) ;
2016-02-10 18:13:24 +00:00
2016-06-11 15:55:05 +00:00
connection . AddColumn ( Logger , "SyncJobs" , "Profile" , "TEXT" ) ;
connection . AddColumn ( Logger , "SyncJobs" , "Bitrate" , "INT" ) ;
connection . AddColumn ( Logger , "SyncJobItems" , "ItemDateModifiedTicks" , "BIGINT" ) ;
}
2014-07-26 17:30:15 +00:00
}
2015-03-15 19:10:27 +00:00
private const string BaseJobSelectText = "select Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount from SyncJobs" ;
2016-04-27 20:29:27 +00:00
private const string BaseJobItemSelectText = "select Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex, ItemDateModifiedTicks from SyncJobItems" ;
2014-07-26 17:30:15 +00:00
public SyncJob GetJob ( string id )
{
if ( string . IsNullOrEmpty ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
2015-02-07 03:25:23 +00:00
CheckDisposed ( ) ;
2016-06-11 15:55:05 +00:00
2014-07-26 17:30:15 +00:00
var guid = new Guid ( id ) ;
2014-12-13 03:56:30 +00:00
if ( guid = = Guid . Empty )
{
throw new ArgumentNullException ( "id" ) ;
}
2014-12-30 16:36:49 +00:00
2016-06-11 15:55:05 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
{
cmd . CommandText = BaseJobSelectText + " where Id=@Id" ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
cmd . Parameters . Add ( cmd , "@Id" , DbType . Guid ) . Value = guid ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow ) )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
if ( reader . Read ( ) )
{
return GetJob ( reader ) ;
}
2014-07-26 17:30:15 +00:00
}
}
2016-06-11 15:55:05 +00:00
return null ;
}
2014-07-26 17:30:15 +00:00
}
private SyncJob GetJob ( IDataReader reader )
{
var info = new SyncJob
{
Id = reader . GetGuid ( 0 ) . ToString ( "N" ) ,
TargetId = reader . GetString ( 1 ) ,
Name = reader . GetString ( 2 )
} ;
if ( ! reader . IsDBNull ( 3 ) )
{
2015-03-15 01:42:09 +00:00
info . Profile = reader . GetString ( 3 ) ;
2014-07-26 17:30:15 +00:00
}
if ( ! reader . IsDBNull ( 4 ) )
{
2015-03-15 01:42:09 +00:00
info . Quality = reader . GetString ( 4 ) ;
2014-07-26 17:30:15 +00:00
}
if ( ! reader . IsDBNull ( 5 ) )
{
2015-03-15 19:10:27 +00:00
info . Bitrate = reader . GetInt32 ( 5 ) ;
2014-07-26 17:30:15 +00:00
}
if ( ! reader . IsDBNull ( 6 ) )
{
2015-03-15 19:10:27 +00:00
info . Status = ( SyncJobStatus ) Enum . Parse ( typeof ( SyncJobStatus ) , reader . GetString ( 6 ) , true ) ;
2014-07-26 17:30:15 +00:00
}
if ( ! reader . IsDBNull ( 7 ) )
{
2015-03-15 19:10:27 +00:00
info . Progress = reader . GetDouble ( 7 ) ;
2014-07-26 17:30:15 +00:00
}
if ( ! reader . IsDBNull ( 8 ) )
{
2015-03-15 19:10:27 +00:00
info . UserId = reader . GetString ( 8 ) ;
2014-07-26 17:30:15 +00:00
}
if ( ! reader . IsDBNull ( 9 ) )
{
2015-03-15 19:10:27 +00:00
info . RequestedItemIds = reader . GetString ( 9 ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) . ToList ( ) ;
2014-07-26 17:30:15 +00:00
}
2014-12-16 05:01:57 +00:00
if ( ! reader . IsDBNull ( 10 ) )
{
2015-03-15 19:10:27 +00:00
info . Category = ( SyncCategory ) Enum . Parse ( typeof ( SyncCategory ) , reader . GetString ( 10 ) , true ) ;
2014-12-16 05:01:57 +00:00
}
if ( ! reader . IsDBNull ( 11 ) )
{
2015-03-15 19:10:27 +00:00
info . ParentId = reader . GetString ( 11 ) ;
2015-03-15 01:42:09 +00:00
}
if ( ! reader . IsDBNull ( 12 ) )
{
2015-03-15 19:10:27 +00:00
info . UnwatchedOnly = reader . GetBoolean ( 12 ) ;
}
if ( ! reader . IsDBNull ( 13 ) )
{
info . ItemLimit = reader . GetInt32 ( 13 ) ;
2014-12-16 05:01:57 +00:00
}
2015-03-15 19:10:27 +00:00
info . SyncNewContent = reader . GetBoolean ( 14 ) ;
2014-07-26 17:30:15 +00:00
2015-03-15 19:10:27 +00:00
info . DateCreated = reader . GetDateTime ( 15 ) . ToUniversalTime ( ) ;
info . DateLastModified = reader . GetDateTime ( 16 ) . ToUniversalTime ( ) ;
info . ItemCount = reader . GetInt32 ( 17 ) ;
2014-07-26 17:30:15 +00:00
return info ;
}
public Task Create ( SyncJob job )
{
2016-06-11 15:55:05 +00:00
return InsertOrUpdate ( job , true ) ;
2015-02-03 06:57:45 +00:00
}
public Task Update ( SyncJob job )
{
2016-06-11 15:55:05 +00:00
return InsertOrUpdate ( job , false ) ;
2014-07-26 17:30:15 +00:00
}
2016-06-11 15:55:05 +00:00
private async Task InsertOrUpdate ( SyncJob job , bool insert )
2014-07-26 17:30:15 +00:00
{
if ( job = = null )
{
throw new ArgumentNullException ( "job" ) ;
}
2015-02-07 03:25:23 +00:00
CheckDisposed ( ) ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
{
if ( insert )
{
cmd . CommandText = "insert into SyncJobs (Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount) values (@Id, @TargetId, @Name, @Profile, @Quality, @Bitrate, @Status, @Progress, @UserId, @ItemIds, @Category, @ParentId, @UnwatchedOnly, @ItemLimit, @SyncNewContent, @DateCreated, @DateLastModified, @ItemCount)" ;
cmd . Parameters . Add ( cmd , "@Id" ) ;
cmd . Parameters . Add ( cmd , "@TargetId" ) ;
cmd . Parameters . Add ( cmd , "@Name" ) ;
cmd . Parameters . Add ( cmd , "@Profile" ) ;
cmd . Parameters . Add ( cmd , "@Quality" ) ;
cmd . Parameters . Add ( cmd , "@Bitrate" ) ;
cmd . Parameters . Add ( cmd , "@Status" ) ;
cmd . Parameters . Add ( cmd , "@Progress" ) ;
cmd . Parameters . Add ( cmd , "@UserId" ) ;
cmd . Parameters . Add ( cmd , "@ItemIds" ) ;
cmd . Parameters . Add ( cmd , "@Category" ) ;
cmd . Parameters . Add ( cmd , "@ParentId" ) ;
cmd . Parameters . Add ( cmd , "@UnwatchedOnly" ) ;
cmd . Parameters . Add ( cmd , "@ItemLimit" ) ;
cmd . Parameters . Add ( cmd , "@SyncNewContent" ) ;
cmd . Parameters . Add ( cmd , "@DateCreated" ) ;
cmd . Parameters . Add ( cmd , "@DateLastModified" ) ;
cmd . Parameters . Add ( cmd , "@ItemCount" ) ;
}
else
{
cmd . CommandText = "update SyncJobs set TargetId=@TargetId,Name=@Name,Profile=@Profile,Quality=@Quality,Bitrate=@Bitrate,Status=@Status,Progress=@Progress,UserId=@UserId,ItemIds=@ItemIds,Category=@Category,ParentId=@ParentId,UnwatchedOnly=@UnwatchedOnly,ItemLimit=@ItemLimit,SyncNewContent=@SyncNewContent,DateCreated=@DateCreated,DateLastModified=@DateLastModified,ItemCount=@ItemCount where Id=@Id" ;
cmd . Parameters . Add ( cmd , "@Id" ) ;
cmd . Parameters . Add ( cmd , "@TargetId" ) ;
cmd . Parameters . Add ( cmd , "@Name" ) ;
cmd . Parameters . Add ( cmd , "@Profile" ) ;
cmd . Parameters . Add ( cmd , "@Quality" ) ;
cmd . Parameters . Add ( cmd , "@Bitrate" ) ;
cmd . Parameters . Add ( cmd , "@Status" ) ;
cmd . Parameters . Add ( cmd , "@Progress" ) ;
cmd . Parameters . Add ( cmd , "@UserId" ) ;
cmd . Parameters . Add ( cmd , "@ItemIds" ) ;
cmd . Parameters . Add ( cmd , "@Category" ) ;
cmd . Parameters . Add ( cmd , "@ParentId" ) ;
cmd . Parameters . Add ( cmd , "@UnwatchedOnly" ) ;
cmd . Parameters . Add ( cmd , "@ItemLimit" ) ;
cmd . Parameters . Add ( cmd , "@SyncNewContent" ) ;
cmd . Parameters . Add ( cmd , "@DateCreated" ) ;
cmd . Parameters . Add ( cmd , "@DateLastModified" ) ;
cmd . Parameters . Add ( cmd , "@ItemCount" ) ;
}
2015-02-03 06:57:45 +00:00
2016-06-11 15:55:05 +00:00
IDbTransaction transaction = null ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
try
{
transaction = connection . BeginTransaction ( ) ;
var index = 0 ;
cmd . GetParameter ( index + + ) . Value = new Guid ( job . Id ) ;
cmd . GetParameter ( index + + ) . Value = job . TargetId ;
cmd . GetParameter ( index + + ) . Value = job . Name ;
cmd . GetParameter ( index + + ) . Value = job . Profile ;
cmd . GetParameter ( index + + ) . Value = job . Quality ;
cmd . GetParameter ( index + + ) . Value = job . Bitrate ;
cmd . GetParameter ( index + + ) . Value = job . Status . ToString ( ) ;
cmd . GetParameter ( index + + ) . Value = job . Progress ;
cmd . GetParameter ( index + + ) . Value = job . UserId ;
cmd . GetParameter ( index + + ) . Value = string . Join ( "," , job . RequestedItemIds . ToArray ( ) ) ;
cmd . GetParameter ( index + + ) . Value = job . Category ;
cmd . GetParameter ( index + + ) . Value = job . ParentId ;
cmd . GetParameter ( index + + ) . Value = job . UnwatchedOnly ;
cmd . GetParameter ( index + + ) . Value = job . ItemLimit ;
cmd . GetParameter ( index + + ) . Value = job . SyncNewContent ;
cmd . GetParameter ( index + + ) . Value = job . DateCreated ;
cmd . GetParameter ( index + + ) . Value = job . DateLastModified ;
cmd . GetParameter ( index + + ) . Value = job . ItemCount ;
cmd . Transaction = transaction ;
cmd . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to save record:" , e ) ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
2014-07-26 17:30:15 +00:00
}
}
}
2014-12-11 06:20:28 +00:00
public async Task DeleteJob ( string id )
{
if ( string . IsNullOrWhiteSpace ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
2015-02-07 03:25:23 +00:00
CheckDisposed ( ) ;
2014-12-11 06:20:28 +00:00
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
2014-12-11 06:20:28 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var deleteJobCommand = connection . CreateCommand ( ) )
2014-12-11 06:20:28 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var deleteJobItemsCommand = connection . CreateCommand ( ) )
{
IDbTransaction transaction = null ;
try
{
// _deleteJobCommand
deleteJobCommand . CommandText = "delete from SyncJobs where Id=@Id" ;
deleteJobCommand . Parameters . Add ( deleteJobCommand , "@Id" ) ;
transaction = connection . BeginTransaction ( ) ;
deleteJobCommand . GetParameter ( 0 ) . Value = new Guid ( id ) ;
deleteJobCommand . Transaction = transaction ;
deleteJobCommand . ExecuteNonQuery ( ) ;
// _deleteJobItemsCommand
deleteJobItemsCommand . CommandText = "delete from SyncJobItems where JobId=@JobId" ;
deleteJobItemsCommand . Parameters . Add ( deleteJobItemsCommand , "@JobId" ) ;
deleteJobItemsCommand . GetParameter ( 0 ) . Value = id ;
deleteJobItemsCommand . Transaction = transaction ;
deleteJobItemsCommand . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to save record:" , e ) ;
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
}
2014-12-11 06:20:28 +00:00
}
}
}
2014-07-26 17:30:15 +00:00
public QueryResult < SyncJob > GetJobs ( SyncJobQuery query )
{
if ( query = = null )
{
throw new ArgumentNullException ( "query" ) ;
}
2015-02-07 03:25:23 +00:00
CheckDisposed ( ) ;
2016-06-11 15:55:05 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
{
cmd . CommandText = BaseJobSelectText ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
var whereClauses = new List < string > ( ) ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
if ( query . Statuses . Length > 0 )
{
var statuses = string . Join ( "," , query . Statuses . Select ( i = > "'" + i . ToString ( ) + "'" ) . ToArray ( ) ) ;
2015-02-04 11:56:48 +00:00
2016-06-11 15:55:05 +00:00
whereClauses . Add ( string . Format ( "Status in ({0})" , statuses ) ) ;
}
if ( ! string . IsNullOrWhiteSpace ( query . TargetId ) )
{
whereClauses . Add ( "TargetId=@TargetId" ) ;
cmd . Parameters . Add ( cmd , "@TargetId" , DbType . String ) . Value = query . TargetId ;
}
2016-08-17 19:28:43 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . ExcludeTargetIds ) )
{
var excludeIds = ( query . ExcludeTargetIds ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
if ( excludeIds . Length = = 1 )
{
whereClauses . Add ( "TargetId<>@ExcludeTargetId" ) ;
cmd . Parameters . Add ( cmd , "@ExcludeTargetId" , DbType . String ) . Value = excludeIds [ 0 ] ;
}
else if ( excludeIds . Length > 1 )
{
whereClauses . Add ( "TargetId<>@ExcludeTargetId" ) ;
cmd . Parameters . Add ( cmd , "@ExcludeTargetId" , DbType . String ) . Value = excludeIds [ 0 ] ;
}
}
2016-06-11 15:55:05 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . UserId ) )
{
whereClauses . Add ( "UserId=@UserId" ) ;
cmd . Parameters . Add ( cmd , "@UserId" , DbType . String ) . Value = query . UserId ;
}
if ( query . SyncNewContent . HasValue )
{
whereClauses . Add ( "SyncNewContent=@SyncNewContent" ) ;
cmd . Parameters . Add ( cmd , "@SyncNewContent" , DbType . Boolean ) . Value = query . SyncNewContent . Value ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
cmd . CommandText + = " mainTable" ;
2015-03-15 04:39:29 +00:00
2016-06-11 15:55:05 +00:00
var whereTextWithoutPaging = whereClauses . Count = = 0 ?
string . Empty :
" where " + string . Join ( " AND " , whereClauses . ToArray ( ) ) ;
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
var startIndex = query . StartIndex ? ? 0 ;
if ( startIndex > 0 )
{
whereClauses . Add ( string . Format ( "Id NOT IN (SELECT Id FROM SyncJobs ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC LIMIT {0})" ,
startIndex . ToString ( _usCulture ) ) ) ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
if ( whereClauses . Count > 0 )
{
cmd . CommandText + = " where " + string . Join ( " AND " , whereClauses . ToArray ( ) ) ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
cmd . CommandText + = " ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC" ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
if ( query . Limit . HasValue )
{
cmd . CommandText + = " LIMIT " + query . Limit . Value . ToString ( _usCulture ) ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
cmd . CommandText + = "; select count (Id) from SyncJobs" + whereTextWithoutPaging ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
var list = new List < SyncJob > ( ) ;
var count = 0 ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess ) )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
while ( reader . Read ( ) )
{
list . Add ( GetJob ( reader ) ) ;
}
if ( reader . NextResult ( ) & & reader . Read ( ) )
{
count = reader . GetInt32 ( 0 ) ;
}
2014-07-26 17:30:15 +00:00
}
2016-06-11 15:55:05 +00:00
return new QueryResult < SyncJob > ( )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
Items = list . ToArray ( ) ,
TotalRecordCount = count
} ;
2014-07-26 17:30:15 +00:00
}
}
}
public SyncJobItem GetJobItem ( string id )
{
if ( string . IsNullOrEmpty ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
2015-02-07 03:25:23 +00:00
CheckDisposed ( ) ;
2016-06-11 15:55:05 +00:00
2014-07-26 17:30:15 +00:00
var guid = new Guid ( id ) ;
2016-06-11 15:55:05 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
{
cmd . CommandText = BaseJobItemSelectText + " where Id=@Id" ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
cmd . Parameters . Add ( cmd , "@Id" , DbType . Guid ) . Value = guid ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow ) )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
if ( reader . Read ( ) )
{
return GetJobItem ( reader ) ;
}
2014-07-26 17:30:15 +00:00
}
}
2016-06-11 15:55:05 +00:00
return null ;
}
2014-07-26 17:30:15 +00:00
}
2015-01-24 19:03:55 +00:00
private QueryResult < T > GetJobItemReader < T > ( SyncJobItemQuery query , string baseSelectText , Func < IDataReader , T > itemFactory )
2014-12-11 06:20:28 +00:00
{
2014-12-13 03:56:30 +00:00
if ( query = = null )
2014-12-11 06:20:28 +00:00
{
2014-12-13 03:56:30 +00:00
throw new ArgumentNullException ( "query" ) ;
2014-12-11 06:20:28 +00:00
}
2016-06-11 15:55:05 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2014-12-11 06:20:28 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
{
cmd . CommandText = baseSelectText ;
2014-12-11 06:20:28 +00:00
2016-06-11 15:55:05 +00:00
var whereClauses = new List < string > ( ) ;
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . JobId ) )
{
whereClauses . Add ( "JobId=@JobId" ) ;
cmd . Parameters . Add ( cmd , "@JobId" , DbType . String ) . Value = query . JobId ;
}
if ( ! string . IsNullOrWhiteSpace ( query . ItemId ) )
{
whereClauses . Add ( "ItemId=@ItemId" ) ;
cmd . Parameters . Add ( cmd , "@ItemId" , DbType . String ) . Value = query . ItemId ;
}
if ( ! string . IsNullOrWhiteSpace ( query . TargetId ) )
{
whereClauses . Add ( "TargetId=@TargetId" ) ;
cmd . Parameters . Add ( cmd , "@TargetId" , DbType . String ) . Value = query . TargetId ;
}
2014-12-28 06:21:39 +00:00
2016-06-11 15:55:05 +00:00
if ( query . Statuses . Length > 0 )
{
var statuses = string . Join ( "," , query . Statuses . Select ( i = > "'" + i . ToString ( ) + "'" ) . ToArray ( ) ) ;
2014-12-30 19:16:01 +00:00
2016-06-11 15:55:05 +00:00
whereClauses . Add ( string . Format ( "Status in ({0})" , statuses ) ) ;
}
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
var whereTextWithoutPaging = whereClauses . Count = = 0 ?
string . Empty :
" where " + string . Join ( " AND " , whereClauses . ToArray ( ) ) ;
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
var startIndex = query . StartIndex ? ? 0 ;
if ( startIndex > 0 )
{
whereClauses . Add ( string . Format ( "Id NOT IN (SELECT Id FROM SyncJobItems ORDER BY JobItemIndex, DateCreated LIMIT {0})" ,
startIndex . ToString ( _usCulture ) ) ) ;
}
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
if ( whereClauses . Count > 0 )
{
cmd . CommandText + = " where " + string . Join ( " AND " , whereClauses . ToArray ( ) ) ;
}
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
cmd . CommandText + = " ORDER BY JobItemIndex, DateCreated" ;
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
if ( query . Limit . HasValue )
{
cmd . CommandText + = " LIMIT " + query . Limit . Value . ToString ( _usCulture ) ;
}
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
cmd . CommandText + = "; select count (Id) from SyncJobItems" + whereTextWithoutPaging ;
2014-12-13 03:56:30 +00:00
2016-06-11 15:55:05 +00:00
var list = new List < T > ( ) ;
var count = 0 ;
2014-12-11 06:20:28 +00:00
2016-06-11 15:55:05 +00:00
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess ) )
2014-12-11 06:20:28 +00:00
{
2016-06-11 15:55:05 +00:00
while ( reader . Read ( ) )
{
list . Add ( itemFactory ( reader ) ) ;
}
if ( reader . NextResult ( ) & & reader . Read ( ) )
{
count = reader . GetInt32 ( 0 ) ;
}
2014-12-13 03:56:30 +00:00
}
2016-06-11 15:55:05 +00:00
return new QueryResult < T > ( )
2014-12-13 03:56:30 +00:00
{
2016-06-11 15:55:05 +00:00
Items = list . ToArray ( ) ,
TotalRecordCount = count
} ;
2014-12-11 06:20:28 +00:00
}
}
}
2016-08-03 06:38:19 +00:00
public Dictionary < string , SyncedItemProgress > GetSyncedItemProgresses ( SyncJobItemQuery query )
2015-01-24 19:03:55 +00:00
{
2016-08-03 06:38:19 +00:00
var result = new Dictionary < string , SyncedItemProgress > ( ) ;
using ( var connection = CreateConnection ( true ) . Result )
{
using ( var cmd = connection . CreateCommand ( ) )
{
2016-08-03 07:07:57 +00:00
cmd . CommandText = "select ItemId,Status,Progress from SyncJobItems" ;
2016-08-03 06:38:19 +00:00
var whereClauses = new List < string > ( ) ;
if ( ! string . IsNullOrWhiteSpace ( query . TargetId ) )
{
whereClauses . Add ( "TargetId=@TargetId" ) ;
cmd . Parameters . Add ( cmd , "@TargetId" , DbType . String ) . Value = query . TargetId ;
}
if ( query . Statuses . Length > 0 )
{
var statuses = string . Join ( "," , query . Statuses . Select ( i = > "'" + i . ToString ( ) + "'" ) . ToArray ( ) ) ;
whereClauses . Add ( string . Format ( "Status in ({0})" , statuses ) ) ;
}
if ( whereClauses . Count > 0 )
{
cmd . CommandText + = " where " + string . Join ( " AND " , whereClauses . ToArray ( ) ) ;
}
2016-08-26 17:24:04 +00:00
cmd . CommandText + = ";" + cmd . CommandText
. Replace ( "select ItemId,Status,Progress from SyncJobItems" , "select ItemIds,Status,Progress from SyncJobs" )
. Replace ( "'Synced'" , "'Completed','CompletedWithError'" ) ;
Logger . Debug ( cmd . CommandText ) ;
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess ) )
2016-08-03 06:38:19 +00:00
{
while ( reader . Read ( ) )
{
2016-08-26 17:24:04 +00:00
AddStatusResult ( reader , result , false ) ;
}
2016-08-03 06:38:19 +00:00
2016-08-26 17:24:04 +00:00
if ( reader . NextResult ( ) )
{
while ( reader . Read ( ) )
2016-08-03 06:38:19 +00:00
{
2016-08-26 17:24:04 +00:00
AddStatusResult ( reader , result , true ) ;
2016-08-03 06:38:19 +00:00
}
}
}
}
}
return result ;
2015-01-24 19:03:55 +00:00
}
2016-08-26 17:24:04 +00:00
private void AddStatusResult ( IDataReader reader , Dictionary < string , SyncedItemProgress > result , bool multipleIds )
{
if ( reader . IsDBNull ( 0 ) )
{
return ;
}
var itemIds = new List < string > ( ) ;
var ids = reader . GetString ( 0 ) ;
if ( multipleIds )
{
itemIds = ids . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) . ToList ( ) ;
}
else
{
itemIds . Add ( ids ) ;
}
if ( ! reader . IsDBNull ( 1 ) )
{
SyncJobItemStatus status ;
var statusString = reader . GetString ( 1 ) ;
if ( string . Equals ( statusString , "Completed" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( statusString , "CompletedWithError" , StringComparison . OrdinalIgnoreCase ) )
{
status = SyncJobItemStatus . Synced ;
}
else
{
status = ( SyncJobItemStatus ) Enum . Parse ( typeof ( SyncJobItemStatus ) , statusString , true ) ;
}
if ( status = = SyncJobItemStatus . Synced )
{
foreach ( var itemId in itemIds )
{
result [ itemId ] = new SyncedItemProgress
{
Status = SyncJobItemStatus . Synced
} ;
}
}
else
{
double progress = reader . IsDBNull ( 2 ) ? 0.0 : reader . GetDouble ( 2 ) ;
foreach ( var itemId in itemIds )
{
SyncedItemProgress currentStatus ;
if ( ! result . TryGetValue ( itemId , out currentStatus ) | | ( currentStatus . Status ! = SyncJobItemStatus . Synced & & progress > = currentStatus . Progress ) )
{
result [ itemId ] = new SyncedItemProgress
{
Status = status ,
Progress = progress
} ;
}
}
}
}
}
2015-01-24 19:03:55 +00:00
public QueryResult < SyncJobItem > GetJobItems ( SyncJobItemQuery query )
{
return GetJobItemReader ( query , BaseJobItemSelectText , GetJobItem ) ;
}
2014-07-26 17:30:15 +00:00
public Task Create ( SyncJobItem jobItem )
{
2016-06-11 15:55:05 +00:00
return InsertOrUpdate ( jobItem , true ) ;
2015-02-04 11:56:48 +00:00
}
public Task Update ( SyncJobItem jobItem )
{
2016-06-11 15:55:05 +00:00
return InsertOrUpdate ( jobItem , false ) ;
2014-07-26 17:30:15 +00:00
}
2016-06-11 15:55:05 +00:00
private async Task InsertOrUpdate ( SyncJobItem jobItem , bool insert )
2014-07-26 17:30:15 +00:00
{
if ( jobItem = = null )
{
throw new ArgumentNullException ( "jobItem" ) ;
}
2015-02-07 03:25:23 +00:00
CheckDisposed ( ) ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
2014-07-26 17:30:15 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
{
if ( insert )
{
cmd . CommandText = "insert into SyncJobItems (Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex, ItemDateModifiedTicks) values (@Id, @ItemId, @ItemName, @MediaSourceId, @JobId, @TemporaryPath, @OutputPath, @Status, @TargetId, @DateCreated, @Progress, @AdditionalFiles, @MediaSource, @IsMarkedForRemoval, @JobItemIndex, @ItemDateModifiedTicks)" ;
cmd . Parameters . Add ( cmd , "@Id" ) ;
cmd . Parameters . Add ( cmd , "@ItemId" ) ;
cmd . Parameters . Add ( cmd , "@ItemName" ) ;
cmd . Parameters . Add ( cmd , "@MediaSourceId" ) ;
cmd . Parameters . Add ( cmd , "@JobId" ) ;
cmd . Parameters . Add ( cmd , "@TemporaryPath" ) ;
cmd . Parameters . Add ( cmd , "@OutputPath" ) ;
cmd . Parameters . Add ( cmd , "@Status" ) ;
cmd . Parameters . Add ( cmd , "@TargetId" ) ;
cmd . Parameters . Add ( cmd , "@DateCreated" ) ;
cmd . Parameters . Add ( cmd , "@Progress" ) ;
cmd . Parameters . Add ( cmd , "@AdditionalFiles" ) ;
cmd . Parameters . Add ( cmd , "@MediaSource" ) ;
cmd . Parameters . Add ( cmd , "@IsMarkedForRemoval" ) ;
cmd . Parameters . Add ( cmd , "@JobItemIndex" ) ;
cmd . Parameters . Add ( cmd , "@ItemDateModifiedTicks" ) ;
}
else
{
// cmd
cmd . CommandText = "update SyncJobItems set ItemId=@ItemId,ItemName=@ItemName,MediaSourceId=@MediaSourceId,JobId=@JobId,TemporaryPath=@TemporaryPath,OutputPath=@OutputPath,Status=@Status,TargetId=@TargetId,DateCreated=@DateCreated,Progress=@Progress,AdditionalFiles=@AdditionalFiles,MediaSource=@MediaSource,IsMarkedForRemoval=@IsMarkedForRemoval,JobItemIndex=@JobItemIndex,ItemDateModifiedTicks=@ItemDateModifiedTicks where Id=@Id" ;
cmd . Parameters . Add ( cmd , "@Id" ) ;
cmd . Parameters . Add ( cmd , "@ItemId" ) ;
cmd . Parameters . Add ( cmd , "@ItemName" ) ;
cmd . Parameters . Add ( cmd , "@MediaSourceId" ) ;
cmd . Parameters . Add ( cmd , "@JobId" ) ;
cmd . Parameters . Add ( cmd , "@TemporaryPath" ) ;
cmd . Parameters . Add ( cmd , "@OutputPath" ) ;
cmd . Parameters . Add ( cmd , "@Status" ) ;
cmd . Parameters . Add ( cmd , "@TargetId" ) ;
cmd . Parameters . Add ( cmd , "@DateCreated" ) ;
cmd . Parameters . Add ( cmd , "@Progress" ) ;
cmd . Parameters . Add ( cmd , "@AdditionalFiles" ) ;
cmd . Parameters . Add ( cmd , "@MediaSource" ) ;
cmd . Parameters . Add ( cmd , "@IsMarkedForRemoval" ) ;
cmd . Parameters . Add ( cmd , "@JobItemIndex" ) ;
cmd . Parameters . Add ( cmd , "@ItemDateModifiedTicks" ) ;
}
2015-02-04 11:56:48 +00:00
2016-06-11 15:55:05 +00:00
IDbTransaction transaction = null ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
try
{
transaction = connection . BeginTransaction ( ) ;
var index = 0 ;
cmd . GetParameter ( index + + ) . Value = new Guid ( jobItem . Id ) ;
cmd . GetParameter ( index + + ) . Value = jobItem . ItemId ;
cmd . GetParameter ( index + + ) . Value = jobItem . ItemName ;
cmd . GetParameter ( index + + ) . Value = jobItem . MediaSourceId ;
cmd . GetParameter ( index + + ) . Value = jobItem . JobId ;
cmd . GetParameter ( index + + ) . Value = jobItem . TemporaryPath ;
cmd . GetParameter ( index + + ) . Value = jobItem . OutputPath ;
cmd . GetParameter ( index + + ) . Value = jobItem . Status . ToString ( ) ;
cmd . GetParameter ( index + + ) . Value = jobItem . TargetId ;
cmd . GetParameter ( index + + ) . Value = jobItem . DateCreated ;
cmd . GetParameter ( index + + ) . Value = jobItem . Progress ;
cmd . GetParameter ( index + + ) . Value = _json . SerializeToString ( jobItem . AdditionalFiles ) ;
cmd . GetParameter ( index + + ) . Value = jobItem . MediaSource = = null ? null : _json . SerializeToString ( jobItem . MediaSource ) ;
cmd . GetParameter ( index + + ) . Value = jobItem . IsMarkedForRemoval ;
cmd . GetParameter ( index + + ) . Value = jobItem . JobItemIndex ;
cmd . GetParameter ( index + + ) . Value = jobItem . ItemDateModifiedTicks ;
cmd . Transaction = transaction ;
cmd . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to save record:" , e ) ;
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-07-26 17:30:15 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
2014-07-26 17:30:15 +00:00
}
}
}
2014-12-13 03:56:30 +00:00
private SyncJobItem GetJobItem ( IDataReader reader )
2014-07-26 17:30:15 +00:00
{
var info = new SyncJobItem
{
Id = reader . GetGuid ( 0 ) . ToString ( "N" ) ,
2014-12-27 22:52:41 +00:00
ItemId = reader . GetString ( 1 )
2014-07-26 17:30:15 +00:00
} ;
2014-12-27 22:52:41 +00:00
if ( ! reader . IsDBNull ( 2 ) )
{
2014-12-31 06:24:49 +00:00
info . ItemName = reader . GetString ( 2 ) ;
2014-12-27 22:52:41 +00:00
}
2014-12-30 16:36:49 +00:00
2014-12-31 06:24:49 +00:00
if ( ! reader . IsDBNull ( 3 ) )
2014-07-26 17:30:15 +00:00
{
2014-12-31 06:24:49 +00:00
info . MediaSourceId = reader . GetString ( 3 ) ;
2014-07-26 17:30:15 +00:00
}
2014-12-31 06:24:49 +00:00
info . JobId = reader . GetString ( 4 ) ;
2015-01-17 04:29:53 +00:00
if ( ! reader . IsDBNull ( 5 ) )
{
info . TemporaryPath = reader . GetString ( 5 ) ;
}
2015-01-06 03:25:23 +00:00
if ( ! reader . IsDBNull ( 6 ) )
2014-07-26 17:30:15 +00:00
{
2015-01-06 03:25:23 +00:00
info . OutputPath = reader . GetString ( 6 ) ;
2014-07-26 17:30:15 +00:00
}
2015-01-06 03:25:23 +00:00
if ( ! reader . IsDBNull ( 7 ) )
2014-12-31 06:24:49 +00:00
{
2015-01-06 03:25:23 +00:00
info . Status = ( SyncJobItemStatus ) Enum . Parse ( typeof ( SyncJobItemStatus ) , reader . GetString ( 7 ) , true ) ;
2014-12-31 06:24:49 +00:00
}
2014-08-10 22:13:17 +00:00
2015-01-06 03:25:23 +00:00
info . TargetId = reader . GetString ( 8 ) ;
2014-12-13 03:56:30 +00:00
2015-01-23 06:15:15 +00:00
info . DateCreated = reader . GetDateTime ( 9 ) . ToUniversalTime ( ) ;
2014-12-31 06:24:49 +00:00
2015-01-06 03:25:23 +00:00
if ( ! reader . IsDBNull ( 10 ) )
2014-12-13 03:56:30 +00:00
{
2015-01-06 03:25:23 +00:00
info . Progress = reader . GetDouble ( 10 ) ;
2014-12-13 03:56:30 +00:00
}
2014-12-30 16:36:49 +00:00
2015-01-17 04:29:53 +00:00
if ( ! reader . IsDBNull ( 11 ) )
{
var json = reader . GetString ( 11 ) ;
if ( ! string . IsNullOrWhiteSpace ( json ) )
{
info . AdditionalFiles = _json . DeserializeFromString < List < ItemFileInfo > > ( json ) ;
}
}
if ( ! reader . IsDBNull ( 12 ) )
{
var json = reader . GetString ( 12 ) ;
if ( ! string . IsNullOrWhiteSpace ( json ) )
{
info . MediaSource = _json . DeserializeFromString < MediaSourceInfo > ( json ) ;
}
}
2015-01-21 03:54:45 +00:00
info . IsMarkedForRemoval = reader . GetBoolean ( 13 ) ;
2015-01-23 06:15:15 +00:00
info . JobItemIndex = reader . GetInt32 ( 14 ) ;
2015-01-24 19:03:55 +00:00
2016-04-27 20:24:53 +00:00
if ( ! reader . IsDBNull ( 15 ) )
{
info . ItemDateModifiedTicks = reader . GetInt64 ( 15 ) ;
}
2014-07-26 17:30:15 +00:00
return info ;
}
}
}