@ -15,11 +15,18 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
public class SqliteUserDataRepository : BaseSqliteRepository , IUserDataRepository
{
private IDbConnection _connection ;
public SqliteUserDataRepository ( ILogManager logManager , IApplicationPaths appPaths , IDbConnector connector ) : base ( logManager , connector )
{
DbFilePath = Path . Combine ( appPaths . DataPath , "userdata_v2.db" ) ;
}
protected override bool EnableConnectionPooling
{
get { return false ; }
}
/// <summary>
/// Gets the name of the repository
/// </summary>
@ -36,23 +43,27 @@ namespace MediaBrowser.Server.Implementations.Persistence
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public async Task Initialize ( IDbConnector dbConnector )
{
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
public async Task Initialize ( )
{
_connection = await CreateConnection ( false ) . ConfigureAwait ( false ) ;
string [ ] queries = {
"create table if not exists userdata (key nvarchar, userId GUID, rating float null, played bit, playCount int, isFavorite bit, playbackPositionTicks bigint, lastPlayedDate datetime null)" ,
"create index if not exists idx_userdata on userdata(key)" ,
"create unique index if not exists userdataindex on userdata (key, userId)"
"create unique index if not exists userdataindex on userdata (key, userId)" ,
//pragmas
"pragma temp_store = memory" ,
"pragma shrink_memory"
} ;
connection . RunQueries ( queries , Logger ) ;
_ connection. RunQueries ( queries , Logger ) ;
connection . AddColumn ( Logger , "userdata" , "AudioStreamIndex" , "int" ) ;
connection . AddColumn ( Logger , "userdata" , "SubtitleStreamIndex" , "int" ) ;
}
_connection . AddColumn ( Logger , "userdata" , "AudioStreamIndex" , "int" ) ;
_connection . AddColumn ( Logger , "userdata" , "SubtitleStreamIndex" , "int" ) ;
}
/// <summary>
@ -114,15 +125,15 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
await WriteLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
IDbTransaction transaction = null ;
try
{
transaction = connection. BeginTransaction ( ) ;
transaction = _ connection. BeginTransaction ( ) ;
using ( var cmd = connection. CreateCommand ( ) )
using ( var cmd = _ connection. CreateCommand ( ) )
{
cmd . CommandText = "replace into userdata (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (@key, @userId, @rating,@played,@playCount,@isFavorite,@playbackPositionTicks,@lastPlayedDate,@AudioStreamIndex,@SubtitleStreamIndex)" ;
@ -170,7 +181,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
transaction . Dispose ( ) ;
}
}
WriteLock . Release ( ) ;
}
}
@ -185,17 +197,17 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
await WriteLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
IDbTransaction transaction = null ;
try
{
transaction = connection. BeginTransaction ( ) ;
transaction = _ connection. BeginTransaction ( ) ;
foreach ( var userItemData in userData )
{
using ( var cmd = connection. CreateCommand ( ) )
using ( var cmd = _ connection. CreateCommand ( ) )
{
cmd . CommandText = "replace into userdata (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (@key, @userId, @rating,@played,@playCount,@isFavorite,@playbackPositionTicks,@lastPlayedDate,@AudioStreamIndex,@SubtitleStreamIndex)" ;
@ -246,7 +258,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
transaction . Dispose ( ) ;
}
}
WriteLock . Release ( ) ;
}
}
@ -272,9 +285,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
throw new ArgumentNullException ( "key" ) ;
}
using ( var connection = CreateConnection ( true ) . Result )
{
using ( var cmd = connection . CreateCommand ( ) )
using ( var cmd = _connection . CreateCommand ( ) )
{
cmd . CommandText = "select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key = @key and userId=@userId" ;
@ -292,7 +303,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
return null ;
}
}
}
public UserItemData GetUserData ( Guid userId , List < string > keys )
{
@ -305,9 +315,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
throw new ArgumentNullException ( "keys" ) ;
}
using ( var connection = CreateConnection ( true ) . Result )
{
using ( var cmd = connection . CreateCommand ( ) )
using ( var cmd = _connection . CreateCommand ( ) )
{
var index = 0 ;
var excludeIds = new List < string > ( ) ;
@ -341,7 +349,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
return null ;
}
}
}
/// <summary>
/// Return all user-data associated with the given user
@ -355,11 +362,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
throw new ArgumentNullException ( "userId" ) ;
}
var list = new List < UserItemData > ( ) ;
using ( var connection = CreateConnection ( true ) . Result )
{
using ( var cmd = connection . CreateCommand ( ) )
using ( var cmd = _connection . CreateCommand ( ) )
{
cmd . CommandText = "select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where userId=@userId" ;
@ -369,15 +372,12 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
while ( reader . Read ( ) )
{
list . Add ( ReadRow ( reader ) ) ;
yield return ReadRow ( reader ) ;
}
}
}
}
return list ;
}
/// <summary>
/// Read a row from the specified reader into the provided userData object
/// </summary>
@ -416,5 +416,19 @@ namespace MediaBrowser.Server.Implementations.Persistence
return userData ;
}
protected override void CloseConnection ( )
{
if ( _connection ! = null )
{
if ( _connection . IsOpen ( ) )
{
_connection . Close ( ) ;
}
_connection . Dispose ( ) ;
_connection = null ;
}
}
}
}