diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index f8a1b32afe..de46ab9655 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -45,8 +45,6 @@ namespace Emby.Server.Implementations.Activity { using (var connection = GetConnection()) { - RunDefaultInitialization(connection); - connection.RunQueries(new[] { "create table if not exists ActivityLog (Id INTEGER PRIMARY KEY, Name TEXT NOT NULL, Overview TEXT, ShortOverview TEXT, Type TEXT NOT NULL, ItemId TEXT, UserId TEXT, DateCreated DATETIME NOT NULL, LogSeverity TEXT NOT NULL)", diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index c5af156bbd..4da6665c2c 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -9,27 +9,37 @@ namespace Emby.Server.Implementations.Data { public abstract class BaseSqliteRepository : IDisposable { - protected string DbFilePath { get; set; } - - protected ILogger Logger { get; } + private bool _disposed = false; protected BaseSqliteRepository(ILogger logger) { Logger = logger; } + protected string DbFilePath { get; set; } + + protected ILogger Logger { get; } + + protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex; + protected TransactionMode TransactionMode => TransactionMode.Deferred; protected TransactionMode ReadTransactionMode => TransactionMode.Deferred; - protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex; + protected virtual int? CacheSize => null; + + protected virtual string JournalMode => "WAL"; + + protected virtual int? PageSize => null; + + protected virtual TempStoreMode TempStore => TempStoreMode.Default; + + protected virtual SynchronousMode? Synchronous => null; protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1); protected SQLiteDatabaseConnection WriteConnection; - private string _defaultWal; - protected ManagedConnection GetConnection(bool _ = false) { WriteLock.Wait(); @@ -43,23 +53,28 @@ namespace Emby.Server.Implementations.Data DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite, null); - - if (string.IsNullOrWhiteSpace(_defaultWal)) + if (CacheSize.HasValue) { - _defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First(); + WriteConnection.Execute("PRAGMA cache_size=" + (int)CacheSize.Value); + } - Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal); + if (!string.IsNullOrWhiteSpace(JournalMode)) + { + WriteConnection.Execute("PRAGMA journal_mode=" + JournalMode); } - if (EnableTempStoreMemory) + if (Synchronous.HasValue) { - WriteConnection.Execute("PRAGMA temp_store = memory"); + WriteConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value); } - else + + if (PageSize.HasValue) { - WriteConnection.Execute("PRAGMA temp_store = file"); + WriteConnection.Execute("PRAGMA page_size=" + (int)PageSize.Value); } + WriteConnection.Execute("PRAGMA temp_store=" + (int)TempStore); + return new ManagedConnection(WriteConnection, WriteLock); } @@ -92,38 +107,6 @@ namespace Emby.Server.Implementations.Data }, ReadTransactionMode); } - protected void RunDefaultInitialization(ManagedConnection db) - { - var queries = new List - { - "PRAGMA journal_mode=WAL", - "PRAGMA page_size=4096", - "PRAGMA synchronous=Normal" - }; - - if (EnableTempStoreMemory) - { - queries.AddRange(new List - { - "pragma default_temp_store = memory", - "pragma temp_store = memory" - }); - } - else - { - queries.AddRange(new List - { - "pragma temp_store = file" - }); - } - - db.ExecuteAll(string.Join(";", queries)); - Logger.LogInformation("PRAGMA synchronous=" + db.Query("PRAGMA synchronous").SelectScalarString().First()); - } - - protected virtual bool EnableTempStoreMemory => true; - - private bool _disposed; protected void CheckDisposed() { if (_disposed) @@ -199,4 +182,19 @@ namespace Emby.Server.Implementations.Data connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL"); } } + + public enum SynchronousMode + { + Off = 0, + Normal = 1, + Full = 2, + Extra = 3 + } + + public enum TempStoreMode + { + Default = 0, + File = 1, + Memory = 2 + } } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 1d44b0b292..01ef9851dc 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -63,8 +63,6 @@ namespace Emby.Server.Implementations.Data { using (var connection = GetConnection()) { - RunDefaultInitialization(connection); - string[] queries = { "create table if not exists userdisplaypreferences (id GUID NOT NULL, userId GUID NOT NULL, client text NOT NULL, data BLOB NOT NULL)", diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 5dc104347a..8a56e16bb7 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -92,7 +92,7 @@ namespace Emby.Server.Implementations.Data private const string ChaptersTableName = "Chapters2"; - protected override bool EnableTempStoreMemory => true; + protected override TempStoreMode TempStore => TempStoreMode.Memory; /// /// Opens the connection to the database @@ -101,8 +101,6 @@ namespace Emby.Server.Implementations.Data { using (var connection = GetConnection()) { - RunDefaultInitialization(connection); - const string createMediaStreamsTableCommand = "create table if not exists mediastreams (ItemId GUID, StreamIndex INT, StreamType TEXT, Codec TEXT, Language TEXT, ChannelLayout TEXT, Profile TEXT, AspectRatio TEXT, Path TEXT, IsInterlaced BIT, BitRate INT NULL, Channels INT NULL, SampleRate INT NULL, IsDefault BIT, IsForced BIT, IsExternal BIT, Height INT NULL, Width INT NULL, AverageFrameRate FLOAT NULL, RealFrameRate FLOAT NULL, Level FLOAT NULL, PixelFormat TEXT, BitDepth INT NULL, IsAnamorphic BIT NULL, RefFrames INT NULL, CodecTag TEXT NULL, Comment TEXT NULL, NalLengthSize TEXT NULL, IsAvc BIT NULL, Title TEXT NULL, TimeBase TEXT NULL, CodecTimeBase TEXT NULL, ColorPrimaries TEXT NULL, ColorSpace TEXT NULL, ColorTransfer TEXT NULL, PRIMARY KEY (ItemId, StreamIndex))"; diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index 0580203c56..4035bb99d1 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -128,8 +128,6 @@ namespace Emby.Server.Implementations.Data return list; } - protected override bool EnableTempStoreMemory => true; - /// /// Saves the user data. /// diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index a0c6d2903b..cd364e7f44 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -42,8 +42,6 @@ namespace Emby.Server.Implementations.Data { using (var connection = GetConnection()) { - RunDefaultInitialization(connection); - var localUsersTableExists = TableExists(connection, "LocalUsersv2"); connection.RunQueries(new[] { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index c8ecd7e6e6..545e11bf9f 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -25,8 +25,6 @@ namespace Emby.Server.Implementations.Security { using (var connection = GetConnection()) { - RunDefaultInitialization(connection); - var tableNewlyCreated = !TableExists(connection, "Tokens"); string[] queries = {