Final fixes

This commit is contained in:
Bond-009 2019-04-02 22:15:18 +02:00 committed by Bond_009
parent b6954f3bfd
commit e88ebd748d
5 changed files with 47 additions and 54 deletions

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Logging;
using SQLitePCL;
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
@ -23,23 +22,23 @@ namespace Emby.Server.Implementations.Data
protected TransactionMode ReadTransactionMode => TransactionMode.Deferred;
protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.SharedCached | ConnectionFlags.NoMutex;
protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex;
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1);
private SQLiteDatabaseConnection _writeConnection;
protected SQLiteDatabaseConnection WriteConnection;
private string _defaultWal;
protected ManagedConnection GetConnection(bool _ = false)
{
_writeLock.Wait();
if (_writeConnection != null)
WriteLock.Wait();
if (WriteConnection != null)
{
return new ManagedConnection(_writeConnection, _writeLock);
return new ManagedConnection(WriteConnection, WriteLock);
}
_writeConnection = SQLite3.Open(
WriteConnection = SQLite3.Open(
DbFilePath,
DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
null);
@ -47,38 +46,29 @@ namespace Emby.Server.Implementations.Data
if (string.IsNullOrWhiteSpace(_defaultWal))
{
_defaultWal = _writeConnection.Query("PRAGMA journal_mode").SelectScalarString().First();
_defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First();
Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
}
if (EnableTempStoreMemory)
{
_writeConnection.Execute("PRAGMA temp_store = memory");
WriteConnection.Execute("PRAGMA temp_store = memory");
}
else
{
_writeConnection.Execute("PRAGMA temp_store = file");
WriteConnection.Execute("PRAGMA temp_store = file");
}
return new ManagedConnection(_writeConnection, _writeLock);
return new ManagedConnection(WriteConnection, WriteLock);
}
public IStatement PrepareStatement(ManagedConnection connection, string sql)
=> connection.PrepareStatement(sql);
public IStatement PrepareStatementSafe(ManagedConnection connection, string sql)
=> connection.PrepareStatement(sql);
public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
=> connection.PrepareStatement(sql);
public IStatement PrepareStatementSafe(IDatabaseConnection connection, string sql)
=> connection.PrepareStatement(sql);
public IEnumerable<IStatement> PrepareAll(IDatabaseConnection connection, IEnumerable<string> sql)
=> PrepareAllSafe(connection, sql);
public IEnumerable<IStatement> PrepareAllSafe(IDatabaseConnection connection, IEnumerable<string> sql)
=> sql.Select(connection.PrepareStatement);
@ -145,6 +135,7 @@ namespace Emby.Server.Implementations.Data
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private readonly object _disposeLock = new object();
@ -162,20 +153,21 @@ namespace Emby.Server.Implementations.Data
if (dispose)
{
_writeLock.Wait();
WriteLock.Wait();
try
{
_writeConnection.Dispose();
WriteConnection.Dispose();
}
finally
{
_writeLock.Release();
WriteLock.Release();
}
_writeLock.Dispose();
WriteLock.Dispose();
}
_writeConnection = null;
WriteConnection = null;
WriteLock = null;
_disposed = true;
}

View File

@ -317,7 +317,7 @@ namespace Emby.Server.Implementations.Data
connection.RunQueries(postQueries);
}
userDataRepo.Initialize(userManager);
userDataRepo.Initialize(userManager, WriteLock, WriteConnection);
}
private static readonly string[] _retriveItemColumns =
@ -551,16 +551,16 @@ namespace Emby.Server.Implementations.Data
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
connection.RunInTransaction((Action<IDatabaseConnection>)(db =>
{
using (var saveImagesStatement = PrepareStatement(db, "Update TypedBaseItems set Images=@Images where guid=@Id"))
using (var saveImagesStatement = base.PrepareStatement((IDatabaseConnection)db, (string)"Update TypedBaseItems set Images=@Images where guid=@Id"))
{
saveImagesStatement.TryBind("@Id", item.Id.ToGuidBlob());
saveImagesStatement.TryBind("@Images", SerializeImages(item));
saveImagesStatement.MoveNext();
}
}, TransactionMode);
}), TransactionMode);
}
}
@ -1186,7 +1186,7 @@ namespace Emby.Server.Implementations.Data
using (var connection = GetConnection(true))
{
using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
using (var statement = PrepareStatement(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
{
statement.TryBind("@guid", id);
@ -1901,7 +1901,7 @@ namespace Emby.Server.Implementations.Data
{
var list = new List<ChapterInfo>();
using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
{
statement.TryBind("@ItemId", item.Id);
@ -1928,7 +1928,7 @@ namespace Emby.Server.Implementations.Data
using (var connection = GetConnection(true))
{
using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
{
statement.TryBind("@ItemId", item.Id);
statement.TryBind("@ChapterIndex", index);
@ -2028,7 +2028,7 @@ namespace Emby.Server.Implementations.Data
}
insertText.Length -= 1; // Remove last ,
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);
@ -2533,7 +2533,7 @@ namespace Emby.Server.Implementations.Data
using (var connection = GetConnection(true))
{
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -2604,7 +2604,7 @@ namespace Emby.Server.Implementations.Data
{
var list = new List<BaseItem>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -3054,7 +3054,7 @@ namespace Emby.Server.Implementations.Data
{
var list = new List<Guid>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -3119,7 +3119,7 @@ namespace Emby.Server.Implementations.Data
var list = new List<Tuple<Guid, string>>();
using (var connection = GetConnection(true))
{
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -4983,7 +4983,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
using (var connection = GetConnection(true))
{
var list = new List<string>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
// Run this again to bind the params
GetPeopleWhereClauses(query, statement);
@ -5021,7 +5021,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{
var list = new List<PersonInfo>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
// Run this again to bind the params
GetPeopleWhereClauses(query, statement);
@ -5146,7 +5146,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
insertText.AppendFormat("(@ItemId, @AncestorId{0}, @AncestorIdText{0})", i.ToString(CultureInfo.InvariantCulture));
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", itemIdBlob);
@ -5247,7 +5247,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{
var list = new List<string>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
foreach (var row in statement.ExecuteQuery())
{
@ -5651,7 +5651,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
isSubsequentRow = true;
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);
@ -5735,7 +5735,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
isSubsequentRow = true;
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);
@ -5817,7 +5817,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{
var list = new List<MediaStream>();
using (var statement = PrepareStatementSafe(connection, cmdText))
using (var statement = PrepareStatement(connection, cmdText))
{
statement.TryBind("@ItemId", query.ItemId.ToGuidBlob());
@ -5902,7 +5902,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
insertText.Append(")");
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);

View File

@ -32,8 +32,13 @@ namespace Emby.Server.Implementations.Data
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public void Initialize(IUserManager userManager)
public void Initialize(IUserManager userManager, SemaphoreSlim dbLock, SQLiteDatabaseConnection dbConnection)
{
WriteLock.Dispose();
WriteLock = dbLock;
WriteConnection?.Dispose();
WriteConnection = dbConnection;
using (var connection = GetConnection())
{
var userDatasTableExists = TableExists(connection, "UserDatas");

View File

@ -348,9 +348,9 @@ namespace Emby.Server.Implementations.Security
{
using (var connection = GetConnection(true))
{
return connection.RunInTransaction(db =>
return connection.RunInTransaction((Func<IDatabaseConnection, DeviceOptions>)(db =>
{
using (var statement = PrepareStatementSafe(db, "select CustomName from Devices where Id=@DeviceId"))
using (var statement = base.PrepareStatement((IDatabaseConnection)db, (string)"select CustomName from Devices where Id=@DeviceId"))
{
statement.TryBind("@DeviceId", deviceId);
@ -367,7 +367,7 @@ namespace Emby.Server.Implementations.Security
return result;
}
}, ReadTransactionMode);
}), ReadTransactionMode);
}
}

View File

@ -128,10 +128,6 @@ namespace Jellyfin.Server
#pragma warning restore CA5359
Batteries_V2.Init();
if (raw.sqlite3_enable_shared_cache(1) != raw.SQLITE_OK)
{
Console.WriteLine("WARN: Failed to enable shared cache for SQLite");
}
using (var appHost = new CoreAppHost(
appPaths,