update save methods

pull/702/head
Luke Pulverenti 8 years ago
parent ffb1ec76a7
commit 52227ce00d

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

@ -12,7 +12,7 @@ namespace Emby.Server.Implementations.Data
public abstract class BaseSqliteRepository : IDisposable public abstract class BaseSqliteRepository : IDisposable
{ {
protected string DbFilePath { get; set; } 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 ILogger Logger { get; private set; }
protected BaseSqliteRepository(ILogger logger) protected BaseSqliteRepository(ILogger logger)
@ -130,9 +130,10 @@ namespace Emby.Server.Implementations.Data
{ {
lock (_disposeLock) lock (_disposeLock)
{ {
WriteLock.Wait(); using (WriteLock.Write())
{
CloseConnection(); CloseConnection();
}
} }
} }
catch (Exception ex) 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);
}
}
} }

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

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

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

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

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

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

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

Loading…
Cancel
Save