update save methods

This commit is contained in:
Luke Pulverenti 2016-11-19 03:40:13 -05:00
parent ffb1ec76a7
commit 52227ce00d
9 changed files with 117 additions and 60 deletions

View File

@ -51,7 +51,7 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException("entry");
}
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -76,7 +76,7 @@ namespace Emby.Server.Implementations.Activity
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
{
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{

View File

@ -12,7 +12,7 @@ namespace Emby.Server.Implementations.Data
public abstract class BaseSqliteRepository : IDisposable
{
protected string DbFilePath { get; set; }
protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1);
protected ReaderWriterLockSlim WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
protected ILogger Logger { get; private set; }
protected BaseSqliteRepository(ILogger logger)
@ -130,9 +130,10 @@ namespace Emby.Server.Implementations.Data
{
lock (_disposeLock)
{
WriteLock.Wait();
CloseConnection();
using (WriteLock.Write())
{
CloseConnection();
}
}
}
catch (Exception ex)
@ -178,4 +179,51 @@ namespace Emby.Server.Implementations.Data
}));
}
}
public static class ReaderWriterLockSlimExtensions
{
private sealed class ReadLockToken : IDisposable
{
private ReaderWriterLockSlim _sync;
public ReadLockToken(ReaderWriterLockSlim sync)
{
_sync = sync;
sync.EnterReadLock();
}
public void Dispose()
{
if (_sync != null)
{
_sync.ExitReadLock();
_sync = null;
}
}
}
private sealed class WriteLockToken : IDisposable
{
private ReaderWriterLockSlim _sync;
public WriteLockToken(ReaderWriterLockSlim sync)
{
_sync = sync;
sync.EnterWriteLock();
}
public void Dispose()
{
if (_sync != null)
{
_sync.ExitWriteLock();
_sync = null;
}
}
}
public static IDisposable Read(this ReaderWriterLockSlim obj)
{
return new ReadLockToken(obj);
}
public static IDisposable Write(this ReaderWriterLockSlim obj)
{
return new WriteLockToken(obj);
}
}
}

View File

@ -86,7 +86,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -127,7 +127,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -159,24 +159,27 @@ namespace Emby.Server.Implementations.Data
var guidId = displayPreferencesId.GetMD5();
using (var connection = CreateConnection(true))
using (WriteLock.Read())
{
var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
var paramList = new List<object>();
paramList.Add(guidId.ToGuidParamValue());
paramList.Add(userId.ToGuidParamValue());
paramList.Add(client);
foreach (var row in connection.Query(commandText, paramList.ToArray()))
using (var connection = CreateConnection(true))
{
return Get(row);
var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
var paramList = new List<object>();
paramList.Add(guidId.ToGuidParamValue());
paramList.Add(userId.ToGuidParamValue());
paramList.Add(client);
foreach (var row in connection.Query(commandText, paramList.ToArray()))
{
return Get(row);
}
return new DisplayPreferences
{
Id = guidId.ToString("N")
};
}
return new DisplayPreferences
{
Id = guidId.ToString("N")
};
}
}
@ -190,16 +193,19 @@ namespace Emby.Server.Implementations.Data
{
var list = new List<DisplayPreferences>();
using (var connection = CreateConnection(true))
using (WriteLock.Read())
{
var commandText = "select data from userdisplaypreferences where userId=?";
var paramList = new List<object>();
paramList.Add(userId.ToGuidParamValue());
foreach (var row in connection.Query(commandText, paramList.ToArray()))
using (var connection = CreateConnection(true))
{
list.Add(Get(row));
var commandText = "select data from userdisplaypreferences where userId=?";
var paramList = new List<object>();
paramList.Add(userId.ToGuidParamValue());
foreach (var row in connection.Query(commandText, paramList.ToArray()))
{
list.Add(Get(row));
}
}
}

View File

@ -83,7 +83,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -107,18 +107,21 @@ namespace Emby.Server.Implementations.Data
{
var list = new List<User>();
using (var connection = CreateConnection(true))
using (WriteLock.Read())
{
foreach (var row in connection.Query("select guid,data from users"))
using (var connection = CreateConnection(true))
{
var id = row[0].ReadGuid();
using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob()))
foreach (var row in connection.Query("select guid,data from users"))
{
stream.Position = 0;
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
user.Id = id;
list.Add(user);
var id = row[0].ReadGuid();
using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob()))
{
stream.Position = 0;
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
user.Id = id;
list.Add(user);
}
}
}
}
@ -142,7 +145,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{

View File

@ -48,7 +48,7 @@ namespace Emby.Server.Implementations.Notifications
{
var result = new NotificationResult();
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
@ -103,7 +103,7 @@ namespace Emby.Server.Implementations.Notifications
{
var result = new NotificationsSummary();
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
@ -214,7 +214,7 @@ namespace Emby.Server.Implementations.Notifications
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -273,7 +273,7 @@ namespace Emby.Server.Implementations.Notifications
{
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -289,7 +289,7 @@ namespace Emby.Server.Implementations.Notifications
{
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{

View File

@ -63,7 +63,7 @@ namespace Emby.Server.Implementations.Security
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -195,7 +195,7 @@ namespace Emby.Server.Implementations.Security
throw new ArgumentNullException("id");
}
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{

View File

@ -50,7 +50,7 @@ namespace Emby.Server.Implementations.Social
throw new ArgumentNullException("info.Id");
}
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -75,7 +75,7 @@ namespace Emby.Server.Implementations.Social
throw new ArgumentNullException("id");
}
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{

View File

@ -95,7 +95,7 @@ namespace Emby.Server.Implementations.Sync
throw new ArgumentNullException("id");
}
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
@ -206,7 +206,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -259,7 +259,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{
@ -281,7 +281,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed();
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
@ -379,7 +379,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed();
lock (WriteLock)
using (WriteLock.Read())
{
var guid = new Guid(id);
@ -407,7 +407,7 @@ namespace Emby.Server.Implementations.Sync
throw new ArgumentNullException("query");
}
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
@ -487,7 +487,7 @@ namespace Emby.Server.Implementations.Sync
var now = DateTime.UtcNow;
lock (WriteLock)
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
@ -650,7 +650,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed();
lock (WriteLock)
using (WriteLock.Write())
{
using (var connection = CreateConnection())
{

View File

@ -996,7 +996,7 @@ namespace MediaBrowser.MediaEncoding.Probing
{
_splitWhiteList = new List<string>
{
"AC/DV"
"AC/DC"
};
}