From e88ebd748d98cf9bd2a3978d36254f1644ce751a Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Tue, 2 Apr 2019 22:15:18 +0200 Subject: [PATCH] Final fixes --- .../Data/BaseSqliteRepository.cs | 44 ++++++++----------- .../Data/SqliteItemRepository.cs | 40 ++++++++--------- .../Data/SqliteUserDataRepository.cs | 7 ++- .../Security/AuthenticationRepository.cs | 6 +-- Jellyfin.Server/Program.cs | 4 -- 5 files changed, 47 insertions(+), 54 deletions(-) diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index f3bd07bb09..63cef80b04 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -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 PrepareAll(IDatabaseConnection connection, IEnumerable sql) - => PrepareAllSafe(connection, sql); - public IEnumerable PrepareAllSafe(IDatabaseConnection connection, IEnumerable 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; } diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 9e96d77451..462d91e41f 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -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)(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(); - 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(); - 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(); - 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>(); 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(); - 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(); - 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(); - 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(); - 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); diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index 355755014c..6ac3989372 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -32,8 +32,13 @@ namespace Emby.Server.Implementations.Data /// Opens the connection to the database /// /// Task. - 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"); diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index efe56c0816..29afb9f646 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -348,9 +348,9 @@ namespace Emby.Server.Implementations.Security { using (var connection = GetConnection(true)) { - return connection.RunInTransaction(db => + return connection.RunInTransaction((Func)(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); } } diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index 11c09db983..9454b28d6d 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -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,